A WebAssembly Interpreter That Fits in a QR Code: A 2,944-Byte Implementation
A groundbreaking achievement in size coding has been unveiled: a WebAssembly interpreter that fits within a single QR code, just 2,944 bytes. This article details the implementation, which avoids libc and utilizes linker scripts and inline assembly.
A developer’s ambitious project to fit a WebAssembly (Wasm) interpreter into a single QR code has been garnering attention. The interpreter is an astonishingly compact 2,944 bytes in size. To put that into perspective, it’s less than 0.006% of the size of Wasmtime, a major Wasm runtime environment, and even smaller than a minimal “Hello, world!” program written in C.
Pushing the Limits of QR Codes
The maximum data capacity of a QR code is 2,953 bytes. Within this tight constraint, the developer managed to create an interpreter capable of executing Wasm code.
“If the project were this simple, I would have written it entirely in assembly language. But that would have been catastrophic for development speed,” the developer explained. In the world of size coding, the speed of iteration and experimentation is crucial.
Battling Metadata
Even when using optimization flags like -Os with GCC, the smallest C programs still tend to be too large. The issue lies not with the language itself but with “metadata” such as symbol information, ELF sections, and dynamic linking.
By leveraging linker scripts, the developer consolidated code, global variables, and constants into a single section. Unnecessary alignments were eliminated, squeezing the binary to its minimum possible size. This optimization made the code writable and the data executable, but the developer clarified, “Security was not a goal in this experiment.”
Additionally, the strip --strip-section-headers command was used to remove symbol and section information, leaving only the bare minimum segment information required for the kernel to execute the program.
Ditching libc and Direct System Calls
The most significant size reduction came from abandoning libc altogether. Traditionally, libc manages program initialization and system call invocations, but Linux offers a stable user-level ABI.
The developer created a mere 23-byte stub using inline assembly to call the main function directly. System calls like read and write were also implemented using inline assembly’s syscall instruction, reducing their size to just a few bytes.
“x86 has legacy CISC instructions that serve as a hidden advantage,” the developer added. These older instruction formats contributed to the extreme size reductions achieved.
Functionality and Limitations
This interpreter implements a subset of Wasm functionality known as “Lime1” and part of the WASI (WebAssembly System Interface). It has been used to successfully run a guessing game written in Rust as well as the official release of QuickJS, a JavaScript engine.
Of course, there are limitations. The interpreter does not support all Wasm features, its execution speed is slow, and it offers no security guarantees. However, the mere fact that Wasm code can be interpreted and executed in such a tiny package is a remarkable feat of engineering.
The Fascination of Size Coding
This project exemplifies the allure of “size coding,” the art of minimizing binary size to the extreme. Every byte shaved off requires a deep understanding of low-level computing mechanisms.
“It was fun, and I wanted to share this technology,” the developer reflected. This experiment, which combines the familiar technology of QR codes with the modern execution environment of WebAssembly, highlights the extraordinary possibilities of programming.
FAQ
Q: What are the benefits of fitting a WebAssembly interpreter into a QR code?
A: It simplifies physical distribution and sharing. A Wasm runtime environment can be accessed simply by scanning a QR code, which makes it useful in environments with limited infrastructure or where downloads are restricted for security reasons. It also has educational and research value in the field of size coding technology.
Q: Can this interpreter be used in practical systems?
A: At present, it is not practical for general use. The developer explicitly stated that it is “slow and lacks security guarantees.” However, since it implements part of WASI, it can function in limited scenarios. The project’s true value lies in demonstrating that Wasm interpretation is possible within an extreme 2,944-byte size constraint.
Q: What is size coding?
A: Size coding is a programming technique or competition that focuses on minimizing the binary size of programs. It is commonly practiced in game jams, Capture the Flag (CTF) events, and the demoscene. In this project, techniques ranging from C programming to linker scripts and inline assembly were employed to achieve extreme size reductions.
Comments