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
-
Encode algebraic constraints of arithmetic circuit implementing the computation
-
Hard to use
-
Easy to create bugs: https://www.zksecurity.xyz/blog/posts/boomerang/
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)
-
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
(from Chromium's
Already existing heap types
-
externandfuncโ from reference-types, -
(ref null? $t)โ from function-references
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 nullnone) -
nullexternref==(ref nullnoextern) -
nullfuncref==(ref nullnofunc) -
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.*andstruct.*-- 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