VM WASI and FFI boundaries
Prerequisite: node-v8-libuv-and-runtime-boundaries. Objective: Classify isolation limitations. Tested locally on Node.js 22.16.0 Linux. Each example is independent. Use a fresh disposable working directory, save as main.mjs, then run node main.mjs. The harness uses a five-second timeout and synthetic environment. These are static code blocks, not ByteMind interactive execution.
How it works
vm.createContext and vm.runInContext can run fixed trusted scripts with an explicit context. A timeout constrains some synchronous execution but is not a comprehensive resource or security boundary. WebAssembly modules can import host functions, so the host interface and embedding matter. WASI support and capability semantics vary by implementation and version. Foreign function interfaces (FFI) call native code and require ABI, memory ownership and platform review. Node-API and FFI are not interchangeable. This lesson uses only a literal trusted expression and a tiny reviewed WebAssembly binary with no imports, and does not instantiate WASI or load native libraries.
Separate trusted context state
import vm from 'node:vm';import assert from 'node:assert/strict';const a=vm.createContext({count:1}),b=vm.createContext({count:9});vm.runInContext('count += 2',a,{timeout:100});assert.equal(b.count,9);console.log(a.count,b.count);Expected output:
3 9The contexts have different supplied global state. This is not a security test.
Run an inert WebAssembly function returning 7
import assert from 'node:assert/strict';// Reviewed module: (module (func (export "value") (result i32) i32.const 7))const bytes=Uint8Array.from([0,97,115,109,1,0,0,0,1,5,1,96,0,1,127,3,2,1,0,7,9,1,5,118,97,108,117,101,0,0,10,6,1,4,0,65,7,11]);const module=await WebAssembly.compile(bytes);assert.deepEqual(WebAssembly.Module.imports(module),[]);const instance=await WebAssembly.instantiate(module);console.log(instance.exports.value());Expected output:
7This fixed module has no imports and returns one integer; it makes no WASI calls.
Common mistakes
Never expose arbitrary learner code to VM or native APIs as a substitute for reviewed process/VM isolation. No sandbox bypass experiments.
Practice
Compare VM context, WebAssembly host imports and a separate isolated process by what each does and does not guarantee.
Reference candidate: node-vm-wasi-and-ffi-boundaries-lookup (route unverified). Documentation: https://nodejs.org/download/release/v22.16.0/docs/api/vm.html (opened); https://nodejs.org/download/release/v22.16.0/docs/api/wasi.html (unverified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
VM WASI and FFI boundaries — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
What does the context example establish?
What determines what host functions a Wasm module may call?
What is tested by the constant Wasm fixture?