WASM

Definition

WebAssembly (WASM) is a binary instruction format designed for safe and efficient execution on web browsers. It enables high-performance applications to run on the web by allowing code written in languages like C, C++, and Rust to be compiled into a binary format that can be executed in a web environment. WASM is designed to be a portable, low-level code format that can be executed at near-native speed, providing a way to run complex applications in a secure and sandboxed environment.

Secure Settings Example

// Example of securely loading a WASM module in JavaScript
fetch('module.wasm')
  .then(response => response.arrayBuffer())
  .then(bytes => WebAssembly.instantiate(bytes, {
    env: {
      // Define imports with strict boundaries
      memory: new WebAssembly.Memory({ initial: 256, maximum: 512 }),
      table: new WebAssembly.Table({ initial: 0, element: 'anyfunc' }),
      abort: () => { throw new Error('abort called'); }
    }
  }))
  .then(results => {
    // Use the instantiated module
    const { instance } = results;
    console.log('WASM module loaded securely');
  })
  .catch(console.error);

Insecure Settings Example

// Example of insecurely loading a WASM module in JavaScript
fetch('module.wasm')
  .then(response => response.arrayBuffer())
  .then(bytes => WebAssembly.instantiate(bytes, {
    env: {
      // No memory limits set, potentially allowing excessive memory usage
      memory: new WebAssembly.Memory({ initial: 256 }),
      // No error handling for aborts
      abort: () => {}
    }
  }))
  .then(results => {
    // Use the instantiated module
    const { instance } = results;
    console.log('WASM module loaded');
  })
  .catch(console.error);