Files
experimental-evaluation-of-…/chapters/03_background/03_01_wasm.tex
T

52 lines
4.9 KiB
TeX

%! TeX program = lualatex
%! TeX root = ../../thesis.tex
\documentclass[../../thesis.tex]{subfiles}
\begin{document}
\section{WebAssembly}\label{sec:wasm}
The open \Acrfull{wasm} standard defines a portable virtual instruction set architecture, binary code format and text format for execution inside a virtual (stack) machine~\autocite{wasm3spec}.
\Gls{wasm} instructions operate on an operand stack instead of registers: values are pushed and consumed through stack operations, similar to Java's virtual machine.
It is developed and maintained by the World Wide Web Consortium (W3C)\footnote{\url[2026-07-01]{https://w3.org/}} to support high-performance applications in web-based environments.
While the initial implementations of \Gls{wasm} runtime environments were confined to web browsers\footnote{In 2016, experimental \Gls{wasm} runtimes were implemented in Firefox, Google Chrome and Microsoft Edge: \url[2026-07-01]{https://hacks.mozilla.org/2016/03/a-webassembly-milestone/}}, \Gls{wasm} does not make any web-specific assumptions, so many different standalone runtimes like \textquote{Wasmtime}\footnote{\url[2026-07-01]{https://github.com/bytecodealliance/wasmtime/}} or the \Acrfull{wamr} (see \autoref{sec:wamr}) have emerged since.
Besides instructions or execution behavior, two file formats are defined by the \Gls{wasm} standard: the \Gls{wasm} \textquote{Binary Format} \iffalse{}(see \autoref{lst:wasmexample})\fi for space-efficient representation and fast transmission, and the Lisp-like \Gls{wasm} \textquote{Text Format} for human readability (see \autoref{lst:watexample}).
Both formats are equivalent, they represent the same underlying content differently for alternate purposes.
% \begin{codeblock}[label=lst:wasmexample]{\Gls{wasm} Binary Format}{.wasm}
% \inputminted{hex}{\subfix{listings/wat_example.hex}}
% \end{codeblock}
\begin{codeblock}[label=lst:watexample]{\Gls{wasm} Text Format}{.wat}
\inputminted{wat}{\subfix{listings/wat_example.wat}}
\end{codeblock}
% TODO: Info on wat (module, type, func, memory, global, export)
\autoref{lst:watexample} shows a minimal \Gls{wasm} module in text format.
The \code{module} declaration groups all definitions of the compilation unit.
A \code{type} declares the shared function signature, the \code{func} then references this type and provides its implementation.
The module uses two pages of \code{memory} and defines the stack pointer as a mutable \code{global}.
At the end, the \textquote{memory} and \textquote{main} function are exported, so the host environment can invoke the module and access its state.
Not visible in the above example are \code{import} statements, which allow the \Gls{wasm} program to access functions, variables or memory from the host environment.
In a typical workflow, a program written in a high-level language like C or Rust gets compiled to the \Gls{wasm} binary format using an LLVM-based toolchain, the binary is then executed in a web-based or standalone runtime environment.
The binaries mainly consist of \textit{values}, \textit{instructions}, \textit{functions} and \textit{memory}, bundled into \textit{modules}. % TODO: This is already visible in the watexample...
To execute a program, the module is loaded from its binary format representation, \textit{decoded}, \textit{validated}, \textit{instantiated} and lastly, \textit{invoked}.
During runtime, \Gls{wasm} provides memory safety, control flow integrity and independent execution (sandboxing)\footnote{\url[2026-07-01]{https://webassembly.org/docs/security/}}.
Memory safety is achieved through a bounds-checked linear memory with an inaccessible call stack\footnote{The call stack is not part of \Gls{wasm}'s linear memory but the execution environment: \url[2026-07-01]{https://bytecodealliance.github.io/wamr.dev/blog/the-wamr-memory-model/}}, preventing arbitrary memory accesses and buffer overflows.
The linear memory is a contiguous and growable byte array that is shared between the module and host.
Control flow integrity stems from structured control flow: branches target verifiable positions and function calls are index-based and verified against the function table\footnote{\url[2026-07-01]{https://clang.llvm.org/docs/ControlFlowIntegrity.html}}.
Additionally, the running program cannot observe its (immutable) source code to prevent control flow hijacking.
Sandboxing is enforced by isolating each module's state: a module can only interact with the outside world through explicitly imported functions and resources provided by its host runtime.
Its safety features, portability, language/hardware independence and well-definedness make \Gls{wasm} an interesting platform even for resource-constrained and security-critical systems.
% \subsection{\Glsdesc*{wasm} Binary Format}\label{ssec:wasmbinaryformat}
% \subsection{\Glsdesc*{wasm} Text Format}\label{ssec:wasmtextformat}
% \subsection{\Glsdesc*{wasm} Memories}\label{ssec:wasmmemories}
% \subsection{\Glsdesc*{wasm} Control Flow}\label{ssec:wasmcontrolflow}
\end{document}