## 2 Understanding WebAssembly
### WebAssembly Architecture  
Wasm's architecture is one of its key strengths, contributing significantly to its speed, security, and versatility. It operates through a stack-based virtual machine. This means that unlike register-based code, which requires different instructions to execute on various physical machine architectures, Wasm code can be run consistently across different platforms. Wasm modules declare the types, functions, tables, memories, and globals that they use, and these elements interact in well-defined ways. Furthermore, the architecture includes features for direct memory access and control flow, providing for efficient execution. Figure 2 presents the data flow architecture of Wasm. * **Overview** Wasm is a stack-based virtual machine, which has significant implications for its design and operation. Stack-based machines use a stack to manage computations, which means that operands are pushed onto the stack, operations are performed on the top elements of the stack, and results are stored on the stack. This design makes the architecture of the virtual machine simple and efficient. * **Stack-Based Design** The stack-based design of Wasm is a significant factor contributing to its portability and efficiency. Stack machines are inherently simpler than register machines, as they have no need to manage and allocate registers. Furthermore, stack machines are agnostic to the specifics of the host's physical machine architecture. This means that the same Wasm code can be run consistently across different platforms, enhancing its portability. Wasm's design is based on a stack machine model. This means that it performs computations using a stack data structure where operands are pushed onto the stack; operators pop operands off the stack and push the results back onto it. This design enhances the portability of the Wasm binary format and helps to simplify the process of validation and execution. In addition, the stack machine model results in highly compact bytecode. In a register-based machine model it is necessary to specify the register for each operand and result. In contrast, a stack-based machine model implicitly uses the top of the stack, eliminating the need for such specifications and yielding a smaller binary size. * **WebAssembly Modules** Wasm code is organized into modules, which are the unit of deployment, loading, and compilation. These modules are stateless and can be statically analyzed, which contributes to both the safety and efficiency of Wasm. Modules declare the types, functions, tables, memories, and globals that they use, and these declarations form the basis of Wasm's structured control flow. In Wasm, a module is a binary format file that contains a program's compiled code along with all the data and information necessary to execute it. Each module includes a series of sections that specify types, functions, imports, exports, memory, data, tables, elements, globals, and more. Wasm modules are stateless and side-effect-free, meaning that they do not have access to any state or perform any operations unless they explicitly receive access from the host environment. This characteristic ensures isolation between modules and their host environment, improving security. * **Linear Memory Model** Wasm uses a linear memory model. Each Wasm module has access to a single resizable array of bytes, known as its linear memory. This memory is sandboxed from the host environment and other instances, and is accessed using integer indices. This design simplifies memory management and improves safety by isolating the memory of different instances. This memory is initialized during instantiation of the module, and can be accessed and manipulated through memory instructions provided by Wasm. While this model may seem limiting, it is important to note that each Wasm module can have its own memory, allowing for isolation of different Wasm programs.