Skip to content

gmh5225/wah

 
 

Repository files navigation

WebAssembly interpreter in a Header (WAH)

This is WAH, a WebAssembly interpreter implemented in a single C header file. It supports the WebAssembly 3.0 deterministic profile, including the GC, exception handling, relaxed SIMD, multi-memory, memory64/table64, tail calls, and extended const proposals.

Quick Start

WAH is a single-header library. Define WAH_IMPLEMENTATION in exactly one C file to include the implementation:

#define WAH_IMPLEMENTATION
#include "wah.h"
#include <stdio.h>

int main(int argc, char *argv[]) {
    // Read a .wasm file into memory
    FILE *f = fopen(argv[1], "rb");
    fseek(f, 0, SEEK_END);
    size_t size = ftell(f);
    fseek(f, 0, SEEK_SET);
    uint8_t *buf = malloc(size);
    fread(buf, 1, size, f);
    fclose(f);

    // Parse and create an execution context
    wah_module_t mod = {0};
    wah_exec_context_t ctx = {0};
    wah_parse_module(&mod, buf, size, NULL);
    wah_exec_context_create(&ctx, &mod, NULL);
    free(buf);

    // Call an exported function by name
    wah_value_t params[2] = {{.i32 = 3}, {.i32 = 4}};
    wah_value_t result;
    wah_error_t err = wah_call_by_name(&ctx, "add", params, 2, &result);
    if (err) {
        fprintf(stderr, "error: %s\n", wah_strerror(err));
        return 1;
    }
    printf("add(3, 4) = %d\n", result.i32); // 7

    wah_exec_context_destroy(&ctx);
    wah_module_destroy(&mod);
    return 0;
}

Coverage

WAH passes all currently included files in the official WebAssembly spectest corpus with zero failures (based on spec commit c840c58). One local test-data fix is applied: const.bin.wast corrected per WebAssembly/spec#2150, using regenerated Linux-produced binaries to avoid the upstream generator bug.

Under the deterministic profile, all [!DET]-excluded nondeterminism is eliminated: generated NaN values are canonical and positive, and relaxed SIMD instructions have fixed deterministic behavior. Spec-allowed nondeterminism (memory.grow/table.grow failure due to resource exhaustion) remains.

WAH also comes with a large battery of additional tests covering all major features, edge cases and fault injection scenarios like OOM. Special care was taken to allow untrusted WebAssembly modules to be executed with WAH; it supports both space-based and time-based execution limits and has been fuzzed with multiple harnesses for safety.

License

WAH is a Public Domain (equivalent) software. The vast majority of its code has been generated by Gemini CLI and Claude Code, albeit with substantial human guidance and fixes, making it a copyrightable work.

About

WebAssembly interpreter in a Header (WAH)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • WebAssembly 85.6%
  • C 14.2%
  • Other 0.2%