Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What is WebAssembly?

What is WebAssembly?

Brief overview of what WebAssembly is and how it can be useful in Go applications.

Anuraag Agrawal

August 10, 2023
Tweet

More Decks by Anuraag Agrawal

Other Decks in Technology

Transcript

  1. Self-introduction • OSS Enthusiast • Armeria - HTTP / gRPC

    framework • Zipkin, OpenTelemetry - observability / tracing • Coraza - Web Application Firewall • wazero - Pure Go WebAssembly runtime http://github.com/anuraaga Drink a new type of craft beer
  2. WebAssembly is not Assembly • Stack-based virtual machine bytecode ◦

    So is Java • No semantics ◦ No operations on structs • Some hardware bias ◦ SIMD instructions Common hardware are register machines, not stack machines
  3. WebAssembly bytecode C source code WebAssembly .wat text format WebAssembly

    .wasm binary format int factorial(int n) { if (n == 0) return 1; else return n * factorial(n-1); } (func (param i64) (result i64) local.get 0 i64.eqz if (result i64) i64.const 1 else local.get 0 local.get 0 i64.const 1 i64.sub call 0 i64.mul end) 00 61 73 6D 01 00 00 00 01 00 01 60 01 73 01 73 06 03 00 01 00 02 0A 00 01 00 00 20 00 50 04 7E 42 01 05 20 00 20 00 42 01 7D 10 00 7E 0B 0B 15 17
  4. So not fast? • Often no: https://zaplib.com/docs/blog_post_mortem.html • Overhead when

    calling functions across sandbox • Self-contained apps might be “pretty fast”
  5. WebAssembly has a bias but is not only for web

    • WebAssembly is a web standard ◦ Biggest contributors are Google, Mozilla, Fastly ◦ Much tooling focuses on integrating wasm and browser • Language runtimes can allow running outside the browser ◦ Go: wazero, Rust: wasmtime, JS: V8, etc ◦ Tooling still immature for integrating wasm and host process
  6. What is WebAssembly? • General bytecode for targeting from any

    programming language ◦ C++, Rust mature, Go, Javascript less mature • Sandboxed execution model ◦ No access to host memory, syscalls without explicitly exposing through ABI • No structure, standard library, etc ◦ Compile the entire language into binary Compile existing binary for loading in web (not as fast as native but maybe enough) Allow extending apps in a safe way (or only way, e.g. Go)
  7. Host WebAssembly Runtime Sandbox Memory Compiled wasm str ld Wasm

    binary Read Compile Host function Guest function
  8. Why WebAssembly? Good: • Can extend otherwise unextendable languages •

    Sandbox with well defined interface to pass Bad: • Languages are immature • Overall developer experience is poor Ugly: • Writing an ABI for polyglot is really hard
  9. What is an ABI? • Interface to allow interaction between

    two separately compiled executables • Generally refers to code executing in the same process ◦ IPC is the interface between different processes on a single machine ◦ RPC is the interface between different processes (often) on different machines
  10. Envoy Wasm Plugins • C++ binary, extension could be possible

    with DLL but tricky • Sandbox is appreciated https://github.com/corazawaf/coraza-proxy-wasm