Slim (@sliminality)
Notion (notion.so)
Fast (successor to asm.js)
Many use cases
Formally specified, verified
Might put me out of a job
I’m an academic first, software engineer second
Have you encountered a nifty-sounding technology and thought to yourself:
This seems nifty!
Too bad I am a JavaScript developer and don’t actually understand this lol ¯\_(ツ)_/¯
Those are all actual quotes from me
WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine.
Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust.
(Haas et al. 2017)
To our knowledge, WebAssembly is the first industrial-strength language or VM that has been designed with a formal semantics from the start.
This not only demonstrates the “real world” feasibility of such an approach, but also that it leads to a notably clean design. (Haas et al. 2017)
Types: i32
, i64
, f32
, f64
, functions
Linear memory: each module has its own sandboxed memory space separate from code space and execution stack
Structured control flow: block
and loop
instead of arbitrary jumps
For “high-level” languages like C, C++, Rust
WebAssembly
(module
(type $type0 (func (param i32) (result i32)))
(table 0 anyfunc)
(memory 1)
(export "memory" memory)
(export "_Z6addOnei" $func0)
(export "main" $func1)
(func $func0 (param $var0 i32) (result i32)
get_local $var0
i32.const 1
i32.add
)
(func $func1 (param $var0 i32) (result i32)
get_local $var0
call $func0
)
)
(module
(type $type0 (func (param i32) (result i32)))
(table 0 anyfunc)
(memory 1)
(export "memory" memory)
(export "_Z6addOnei" $func0)
(export "main" $func1)
(func $func0 (param $var0 i32) (result i32)
get_local $var0
i32.const 1
i32.add
)
(func $func1 (param $var0 i32) (result i32)
get_local $var0
call $func0
)
)
How does it run?
get_local $var0
call $func0
$var0
get_local $var0
call $func0
$var0
get_local $var0
call $func0
$var0
get_local $var0
call $func0
$var0
get_local $var0
call $func0
get_local $var0
i32.const 1
i32.add
$var0
get_local $var0
call $func0
get_local $var0
i32.const 1
i32.add
1
$var0
get_local $var0
call $func0
get_local $var0
i32.const 1
i32.add
1
$var0
get_local $var0
call $func0
get_local $var0
i32.const 1
i32.add
get_local $var0
call $func0
get_local $var0
i32.const 1
i32.add
$var0 + 1
get_local $var0
call $func0
get_local $var0
i32.const 1
i32.add
$var0 + 1
“There has been no JavaScript in this talk so far”
Loading a .wasm
file in three lines of JavaScript
wasm-trace
Don’t play with matches, play with binaries!
This is my roommate Meg
We like static analysis, systems programming, Rust, compilers
Needed a cool Rust project
a tool that would take a wasm module and modify its code to inject tracing calls, so that you could get an trace of the wasm’s execution in the console
Nick Fitzgerald, Mozilla
Jim Blandy, Mozilla
.wasm
moduleThen transform each function in module.wasm
!
(module
(type $type0 (func (param i32) (result i32)))
(table 0 anyfunc)
(memory 1)
(export "memory" memory)
(export "_Z6addOnei" $func0)
(export "main" $func1)
(func $func0 (param $var0 i32) (result i32)
get_local $var0
i32.const 1
i32.add
)
(func $func1 (param $var0 i32) (result i32)
get_local $var0
call $func0
)
)
logFunctionReturn
pops an argument off the stack!THIS IS AN INVALID STATE!!!!!!
$func0
must return a value, but the stack is now empty!
tee_local
“Copies the top of the stack” to a local variable
get_local
…call $logFunctionReturn
Looks scary!
But compare to JVM bytecode verification: 150 pages of the current spec!
Validation ensures that the module is well-defined and that its code cannot exhibit any undefined behavior. In particular, along with some runtime checks, this ensures that no program can access or corrupt memory it does not own.
GitHub: sliminality/wasm-trace
Notion is hiring! notion.so/jobs