Wasm GC: What Exactly Is It

(and Why Should I Care)

Ivan Mikushin, VMware

Who am I

  • Staff Engineer @ VMware

  • Recognized Contributor @ Bytecode Alliance

  • Working on Wasm GC support in wasm-tools
    (as a side-project)

What got me here

Curiosity ๐Ÿ˜บ

What got me here

  • ๐Ÿ•ถ๏ธ I've been watching WebAssembly since 2018
  • ๐Ÿ”ฎ Recent Interest: Zero-Knowledge Cryptography

Zero-Knowledge Cryptography

Verifiable Computation

Example: Circom

pragma circom 2.0.0;

template A(n){
   signal input in;
   signal output out;
   var array[n];

   out <== array[in];
}

component main = A(10);
Error: Non-quadratic constraint was detected statically, 
using unknown index will cause the constraint to be 
non-quadratic

General Purpose Verifiable Compute

  • ๐Ÿ’ก Any program execution should be provable
    (not just ones in circuit languages)

  • ๐Ÿ’ก In one language (Wasm)

  • ๐Ÿ’ก Just compile everything to Wasm!

  • ๐Ÿ˜พ Problem: most popular languages require GC

# TODO

  • What is Wasm GC

  • Why should I care

Why I care

Most popular programming languages

  • ๐Ÿ”ฎ will be compiled to Wasm

  • ๐Ÿ˜พ require GC

... ๐Ÿ”ฎ will be compiled to Wasm

  • Empirical law

    • What can be done in JavaScript
    • will be done in JavaScript
  • will (soonโ„ข๏ธ) read:

    • What can be compiled to Wasm
    • will be compiled to Wasm

... ๐Ÿ˜พ require GC

  • Java (and JVM-based)
  • JavaScript (and compiled to JS)
  • Python
  • Ruby
  • Go
  • ... (many more)

# TODO

  • What is Wasm GC

  • โœ… Why should I care

What is GC

Garbage Collection

Per Wikipedia:

  • A form of automatic memory management.

  • Garbage collector attempts to reclaim allocated memory, that is no longer referenced.

  • Invented by John McCarthy around 1959 to simplify manual memory management in Lisp.

What is Wasm GC?

"proposal to to add garbage collection (GC) support to WebAssembly"

What is Wasm GC not?

  • A garbage collector specification

    • Web-based runtimes already have
      high performance GCs

    • Just need heap types to make use of them

  • Seamless interoperability between multiple languages

So, what is Wasm GC?

  • A bunch of new types:
    New heap types and new ways to define types.

  • A bunch of new instructions:
    ~30 new instructions to manipulate reference types.

(MVP โ€” Minimum Viable Product)

MVP

  • Reference Types

  • Heap Types

  • Subtypes

  • Recursive Types

  • New Instructions

(in the future, maybe)

  • Type parameters (polymorphism, generics)

  • Threads and shared references

  • Weak references

  • (a ... ton more)

Current scope (MVP)

  • Reference Types

  • Heap Types

  • Subtypes

  • Recursive Types

  • New Instructions

Kinds of Memory in Wasm

  • Stack
  • Linear memory
  • Heap โ€” added with GC

Heap

A pool of memory used to fulfill memory allocation requests (Wikipedia)

Disjoint hierarchies of heap types

  • function types โ€” classify functions (within the module)
  • external types โ€” external references (possibly owned by the embedder)
  • aggregate types โ€” dynamically allocated managed data (such as structures, arrays, or unboxed scalars)

Type Hierarchy

Already existing heap types

Indexed Types

Examples:

  (type $A (struct (field (mut i32)) (field i64)))

  (type $B (array (mut i32)))
// Go

type A struct {
  f0 int32
  f1 int64
}

type B []int32

Reference Types

WAT syntax: (ref null? <heap-type>)

Examples:

  (type $A (struct)) ;; index = 0

  (func $f1 (param (ref null $A)))
  (func $f2 (result (ref 0))) ;; same as `(ref $A)`
// Go

type A struct {}

func f1(a *A)  {/* */}
func f2() *A {/* */}

Shorthands

  • funcref == (ref null func)
  • externref == (ref null extern)
  • anyref == (ref null any)
  • nullref == (ref null none)
  • nullexternref == (ref null noextern)
  • nullfuncref == (ref null nofunc)
  • eqref == (ref null eq)
  • structref == (ref null struct)
  • arrayref == (ref null array)
  • i31ref == (ref null i31)

Subtypes

Examples:

  (type $A (struct))
  (type $B (sub $A (struct)))
  (type $C (sub final $A (struct)))
// Java

class A {}
class B extends A {}
final class C extends A {}

Recursive Type Groups

Examples:

  (rec
    (type $A (struct (field $b (ref null $B))))
    (type $B (struct (field $a (ref null $A))))
  )
  
  (type $C (struct (field $f0 i32) (field $c (ref null $C))))
// Go

type A struct {
  b *B
}
type B struct {
  a *A
}
type C struct {
  f0 int32
  c  *C
}

New Instructions

~30 new instructions:

  • i31.* -- creating and manipulating "unboxed scalars" (31-bit integer values).
  • array.* and struct.* -- creating and manipulating arrays and structs.
  • ref.* -- casting and testing values of reference types.
  • br_on_* -- conditional branching depending on values of reference types.
  • extern.* -- external reference conversion.

# TODO

  • โœ… What is Wasm GC

  • โœ… Why should I care

A prediction ... ๐Ÿ”ฎ

Compilers to Wasm with GC support are coming

  • Go
  • JavaScript and JS-compilable languages
  • Java and JVM-based languages
  • Python
  • Ruby
  • ... (more)

WebAssembly Core Specification

Release 2.0 + tail calls + function references + gc
(Draft 2023-08-28)

Thank You! ย ๐Ÿ˜ป

@imikushin ย โ€” ย on ๐• (Twitter), GitHub, LinkedIn

Special Thanks to:

  • Nick Fitzgerald (Fastly, Bytecode Alliance)
  • Daniel Lopez Ridruejo (ex VMware, Bytecode Alliance)
  • David Wong (zkSecurity)

Wasm GC: what exactly is it

By Ivan Mikushin

Wasm GC: what exactly is it

  • 75