|
| 1 | +\documentclass{article} |
| 2 | + |
| 3 | +\usepackage{amsmath} |
| 4 | +\usepackage{amssymb} |
| 5 | +\usepackage{hyperref} |
| 6 | + |
| 7 | +\title{SNIFs: Safe Native Implemented Functions for the BEAM via WebAssembly Sandboxing} |
| 8 | +\author{Jonathan D. A. Jewell} |
| 9 | +\date{2026} |
| 10 | + |
| 11 | +\begin{document} |
| 12 | + |
| 13 | +\maketitle |
| 14 | + |
| 15 | +\begin{abstract} |
| 16 | +Native Implemented Functions (NIFs) in the BEAM virtual machine enable integration with native code, but any fault within a NIF terminates the entire VM. This paper examines a practical approach to enforcing crash isolation by compiling native code to WebAssembly and executing it within a sandboxed runtime. |
| 17 | + |
| 18 | +Using Zig targeting WASM and the \texttt{wasmex} interface, we show that a range of failure modes—including out-of-bounds access, explicit panics, unreachable instructions, integer overflow, and divide-by-zero—are trapped and returned as \texttt{\{:error, reason\}} tuples without terminating the BEAM VM. |
| 19 | + |
| 20 | +We formalise a crash-isolation property under the operational semantics of WebAssembly and validate it empirically using an integration test suite (11/11 tests passing). In addition, the accompanying artifact includes machine-checked verification of core interface invariants (ABI correctness and API type safety) using Idris2 and Lean~4 (7 proofs total). |
| 21 | + |
| 22 | +These results indicate that WebAssembly sandboxing provides a viable mechanism for isolating native faults in BEAM systems. |
| 23 | +\end{abstract} |
| 24 | + |
| 25 | +\section{Introduction} |
| 26 | + |
| 27 | +The BEAM virtual machine provides a robust concurrency model, but Native Implemented Functions (NIFs) bypass many of its safety guarantees. A crash within a NIF typically terminates the entire VM, making NIFs a significant reliability risk. |
| 28 | + |
| 29 | +This work evaluates an approach in which native code is compiled to WebAssembly and executed within a sandboxed runtime. Instead of propagating faults to the host, WebAssembly traps are captured and translated into error values at the BEAM level. |
| 30 | + |
| 31 | +The contribution of this paper is not a new virtual machine or language extension, but a concrete demonstration that existing WebAssembly tooling can be used to enforce crash isolation for NIF-style integrations, together with a reproducible artifact and supporting formalisation. |
| 32 | + |
| 33 | +\section{Architecture} |
| 34 | + |
| 35 | +The SNIF architecture consists of three components: |
| 36 | + |
| 37 | +\begin{itemize} |
| 38 | + \item Native functions written in Zig and compiled to WebAssembly. |
| 39 | + \item A WebAssembly runtime (Wasmtime via \texttt{wasmex}) embedded within the BEAM. |
| 40 | + \item A translation layer mapping traps to BEAM-level error values. |
| 41 | +\end{itemize} |
| 42 | + |
| 43 | +All guest execution occurs within WebAssembly linear memory. Faults such as out-of-bounds access or explicit traps are handled by the runtime and do not directly affect the host process. |
| 44 | + |
| 45 | +A critical requirement is the use of safe compilation settings. In particular, Zig must be compiled with \texttt{-OReleaseSafe}; unsafe modes remove bounds checks and invalidate the safety guarantees relied upon here. |
| 46 | + |
| 47 | +\section{Evaluation} |
| 48 | + |
| 49 | +We evaluate the approach using a set of representative failure modes: |
| 50 | + |
| 51 | +\begin{itemize} |
| 52 | + \item Out-of-bounds memory access |
| 53 | + \item Explicit panic |
| 54 | + \item Unreachable instruction |
| 55 | + \item Integer overflow |
| 56 | + \item Divide-by-zero |
| 57 | +\end{itemize} |
| 58 | + |
| 59 | +All failure cases are exercised via integration tests within a BEAM application. In all cases, the WebAssembly runtime traps and returns an error value without terminating the BEAM VM. |
| 60 | + |
| 61 | +Across the test suite, 11/11 integration tests pass under safe compilation settings. |
| 62 | + |
| 63 | +\section{Crash Isolation Property} |
| 64 | + |
| 65 | +We describe the following property informally: |
| 66 | + |
| 67 | +\textbf{Crash Isolation.} For any SNIF call executed via the WebAssembly runtime, any fault in guest execution results in a trapped execution state that is translated into a BEAM-level error value, without terminating the host VM. |
| 68 | + |
| 69 | +This property relies on the trapping semantics of WebAssembly as implemented by the runtime (Wasmtime). Under these semantics, faults in guest execution do not escape the sandboxed environment. |
| 70 | + |
| 71 | +\section{Formal Verification} |
| 72 | + |
| 73 | +The accompanying artifact includes machine-checked verification of key interface invariants using Idris2 and Lean~4. |
| 74 | + |
| 75 | +These proofs cover: |
| 76 | + |
| 77 | +\begin{itemize} |
| 78 | + \item ABI correctness (data layout and calling conventions) |
| 79 | + \item Memory safety invariants (pointer validity and bounds constraints) |
| 80 | + \item API type safety (correctness of SNIF result representations) |
| 81 | +\end{itemize} |
| 82 | + |
| 83 | +All core proofs (7 in total) are complete and mechanically verified. These artifacts provide an executable specification of the SNIF interface, complementing the informal reasoning presented in this paper. |
| 84 | + |
| 85 | +\section{Limitations} |
| 86 | + |
| 87 | +This work relies on the correctness of the WebAssembly runtime. Bugs in the runtime or violations of its safety guarantees would invalidate the isolation property. |
| 88 | + |
| 89 | +Additionally, unsafe compilation modes remove necessary checks and can lead to silent incorrect results rather than traps. |
| 90 | + |
| 91 | +Performance is not evaluated in detail here; the focus is on correctness and isolation rather than throughput. |
| 92 | + |
| 93 | +\section{Conclusion} |
| 94 | + |
| 95 | +This paper demonstrates that WebAssembly sandboxing can be used to enforce crash isolation for NIF-style integrations in the BEAM. By combining safe compilation, runtime trapping semantics, and a translation layer, native faults can be contained and represented as ordinary error values. |
| 96 | + |
| 97 | +The accompanying artifact provides a complete, reproducible implementation together with formal verification of key invariants, supporting the use of this approach in practice. |
| 98 | + |
| 99 | +\bibliographystyle{plain} |
| 100 | +\bibliography{references} |
| 101 | + |
| 102 | +\end{document} |
0 commit comments