Chicory: Write to WebAssembly, Overcome JVM Shortcomings
Chicory was created to bring some of the benefits of WebAssembly to native JVM environments. It's like a virtual machine within a virtual machine.
Jul 19th, 2024 12:33pm by
Featured image via Unsplash.
Meanwhile, Chicory was created to bring some of the benefits of Wasm to native JVM. It’s like a virtual machine within a virtual machine, given the security, the tunnel, the sandbox aspects of WebAssembly. With an additional closed module that the JVM offers, the user benefits from a double sandbox, so to speak.
As described on Chicory’s readme.md file on GitHub, Chicory is a JVM-native WebAssembly runtime. It was created so WebAssembly programs can run with zero native dependencies or Java Native Interface (JNI). “Chicory can run Wasm anywhere that the JVM can go. It is designed with simplicity and safety in mind,” its creators write.
Wasm Help
On its own, Wasm can’t do anything but compute, explained Benjamin Eckel, co-creator of Chicory and CTO and co-founder of Dylibso — which makes Extism, a framework designed to enable software end users to enhance existing applications with Wasm. Since, in theory, it cannot affect the outside world, WebAssembly might seem like a weakness, but this is actually Wasm’s greatest strength, Eckel said. By default, programs are sandboxed and have no capabilities. If a program needs capabilities, capabilities must be provided. This puts the developer in the position of the operating system. A module can ask for a capability by listing an “import” function in the bytecode format. This import can be fulfilled with a host function written in Java. Regardless of the language of the module, the module can call this Java function when needed. Host functions can be thought of as syscalls or a language’s standard library, but host functions are determined and implemented in Java, Eckel said. If linking to a native object in the JVM, the object must be shipped with the application or library, Eckel noted. One of the main reasons for using the JVM is that it compiles to a platform-independent bytecode, which is the primary benefit of writing your application for the JVM. However, this creates the problem of compiling for every OS, architecture, and libc that needs to be shipped, he said. On the runtime side, to communicate with some shared object, most systems require using a foreign function interface. In Java, there are a few different names for this, but it is roughly the same concept. There is significant complexity and many problems when execution leaves the safety and observability of the JVM, Eckel said. If staying within the JVM boundaries, guaranteed memory safety is provided, which has been a reliable feature for a long time, according to Eckel. Fault isolation is also provided, meaning that if a Wasm program is like JVM bytecode, it cannot crash the JVM, which is a major advantage for many applications. Additionally, a super-advanced JIT is available. When using a foreign function interface (FFI), the JIT from the JVM sees the program as a series of holes, going in and out. However, if everything was just one continuous stream of JVM bytecode, the benefits would be much greater, he said. Memory vulnerabilities are increasingly a concern, and the U.S. government has signaled they will start enforcing use of memory-safe languages, Eckel said in an email response. Meanwhile, by default, Java code is memory-safe and the implementation of the JVM just needs to be checked. “But, if you call native code, you leave the safety of the JVM,” Eckel said. “It’s more opportunity for attackers and more opportunities for mistakes.”Inspiration

Andrea Peruffo, principal software engineer, for Red Hat, explains how to get started with Chicory.
Up and Running
Chicory is not able to overcome Java’s inherent slowness, compared to other languages, yet it remains potentially practical for the reason described above. Still, Chicory is a new project and work remains to be done. Thus far, it can (according to the documentation): Bootstrap a bytecode interpreter and test suite and features:- Wasm binary parser.
- Simple bytecode interpreter
- Establish basic coding and testing patterns
- Generate JUnit tests from a wasm test suite.
By the end of summer, it should be able to:
- Make all tests green with the interpreter (important for correctness).
- Implement validation logic (important for safety).
- Draft of the v1.0 API (important for stability and dx).
- Completion of about 30,000 tests to ensure Wasm specifications are met.
- Bytecode validation is 95% complete.
- The ahead-of-time compilation is in production and has proven to be much faster than the interpreter mode, the project’s creators say.
Set-Up
While I was not yet able to get Chicory to load and run on my Windows laptop, stay tuned for a review about how to set it up and run. Chicory’s readme.md provides solid setup instructions to get started, in the meantime: Add thecom.dylibso.chicory:runtime dependency to the dependency management system to use the runtime:
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
<version>0.0.10</version>
</dependency>
'com.dylibso.chicory:runtime:0.0.10'
https://repo1.maven.org/maven2/com/dylibso/chicory/cli/<version>/cli-<version>.sh
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.