webassembly on the server: how system calls work...greetz brian ketelsen andrew kelly eric mcclure...

52
WebAssembly on the Server: How System Calls Work Christine Dodrill https://github.com/Xe https://christine.website @theprincessxena

Upload: others

Post on 26-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

WebAssembly on the Server: How System Calls Work

Christine Dodrill https://github.com/Xe https://christine.website @theprincessxena

Page 2: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

What is WebAssembly?

Page 3: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

WebAssembly is a Virtual Machine format for the WebLike a CPU and supporting hardware Hardware independent Initially made for browsers Generic implementation

Page 4: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin
Page 5: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Why WebAssembly on the Server?It makes hardware less relevant No single vendor reliance Removes the OS

Page 6: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

What are System Calls and why do they matter?

Page 7: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

System Calls Enforce Abstractions To The Outside World

Page 8: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

How are they implemented?The platform knows all Programs pass pointers to them

Page 9: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Why is This Relevant to WebAssembly?

Page 10: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

WebAssembly System Calls Out of The Box

Page 11: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

What's a pointer in WebAssembly?type wasm.VirtualMachine struct { // ... Memory []byte // ... }

Page 12: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

What's a pointer in WebAssembly?

Page 13: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Dagger: A Stepping Stone

Page 14: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

DaggerA proof of concept system call API Simple implementation Intended for learning/teaching

Page 15: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Dagger ConceptsStreams of data are the only interface NO MAGIC Simple doesn't have to mean useless

Page 16: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Dagger ProcessHas a slice of streams Stream descriptors are offsets into that slice

Page 17: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Open Close Read Write Flush

Dagger API

Page 18: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 open(const char *surl, int32 surl_len);Opens a stream descriptor or errors URL scheme determines the stream target

Page 19: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 open(const char *surl, int32 surl_len);Stream kinds: log:// file:// http:// or https:// random:// stdin:// or stdout:// or stderr://

Page 20: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 open(const char *surl, int32 surl_len);func (p *Process) open(vm *exec.VirtualMachine) int32 { args := vm.GetCurrentFrame().Locals var ( furlPtr = uint32(args[0]) fLen = uint32(args[1]) furl = string(vm.Memory[furlPtr : furlPtr+fLen]) )

return p.OpenFile(furl) }

Page 21: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 open(const char *surl, int32 surl_len);func (p *Process) open(vm *exec.VirtualMachine) int32 { args := vm.GetCurrentFrame().Locals var ( furlPtr = uint32(args[0]) fLen = uint32(args[1]) furl = string(vm.Memory[furlPtr : furlPtr+fLen]) )

return p.OpenFile(furl) }

Page 22: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 open(const char *surl, int32 surl_len);func (p *Process) open(vm *exec.VirtualMachine) int32 { args := vm.GetCurrentFrame().Locals var ( furlPtr = uint32(args[0]) fLen = uint32(args[1]) furl = string(vm.Memory[furlPtr : furlPtr+fLen]) )

return p.OpenFile(furl) }

Page 23: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 open(const char *surl, int32 surl_len);func (p *Process) open(vm *exec.VirtualMachine) int32 { args := vm.GetCurrentFrame().Locals var ( furlPtr = uint32(args[0]) fLen = uint32(args[1]) furl = string(vm.Memory[furlPtr : furlPtr+fLen]) )

return p.OpenFile(furl) }

Page 24: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 close(int32 sd);Closes a stream Errors should not normally happen

Page 25: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 close(int32 sd);func (p *Process) open(vm *exec.VirtualMachine) int32 { args := vm.GetCurrentFrame().Locals var desc = uint32(args[0])

return p.CloseFile(desc) }

Page 26: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 close(int32 sd);func (p *Process) open(vm *exec.VirtualMachine) int32 { args := vm.GetCurrentFrame().Locals var desc = uint32(args[0])

return p.CloseFile(desc) }

Page 27: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 close(int32 sd);func (p *Process) open(vm *exec.VirtualMachine) int32 { args := vm.GetCurrentFrame().Locals var desc = uint32(args[0])

return p.CloseFile(desc) }

Page 28: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 read(int32 sd, void *data, int32 data_len); Reads up to data_len bytes Returns the number of bytes read

Page 29: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 read(int32 sd, void *data, int32 data_len); func (p *P) read(vm *exec.VirtualMachine) int32 { args := vm.GetCurrentFrame().Locals var ( desc = uint32(args[0]) ptr = uint32(args[1]) len = uint32(args[2]) buf = make([]byte, len) ret = p.ReadFile(desc, buf) ) p.CopyRam(ptr, buf) return ret }

Page 30: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 read(int32 sd, void *data, int32 data_len); // in read function ... var ( desc = uint32(args[0]) ptr = uint32(args[1]) len = uint32(args[2]) buf = make([]byte, len) ) // ...

Page 31: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 read(int32 sd, void *data, int32 data_len); // in read function ... var ( desc = uint32(args[0]) ptr = uint32(args[1]) len = uint32(args[2]) buf = make([]byte, len) ) // ...

Page 32: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 read(int32 sd, void *data, int32 data_len); // in read function ... var ( ret = p.ReadFile(desc, buf) ) p.CopyRam(ptr, buf) return ret }

Page 33: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 read(int32 sd, void *data, int32 data_len); // in read function ... var ( ret = p.ReadFile(desc, buf) ) p.CopyRam(ptr, buf) return ret }

Page 34: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 write(int32 sd, void *data, int32 data_len); Copies data-data+data_len to the stream Returns number of bytes written

Page 35: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 write(int32 sd, void *data, int32 data_len); func (p *P) Write(vm *exec.VirtualMachine) int32 { var ( args = vm.GetCurrentFrame().Locals fd = uint32(args[0]) ptr = f.Locals[1] len = f.Locals[2] mem = vm.Memory[int(ptr):int(ptr+len)] )

return p.WriteFD(fd, mem) }

Page 36: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 write(int32 sd, void *data, int32 data_len); func (p *P) Write(vm *exec.VirtualMachine) int32 { var ( args = vm.GetCurrentFrame().Locals fd = uint32(args[0]) ptr = f.Locals[1] len = f.Locals[2] ) // ...

Page 37: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 write(int32 sd, void *data, int32 data_len); func (p *P) Write(vm *exec.VirtualMachine) int32 { var ( // ... mem = vm.Memory[int(ptr):int(ptr+len)] )

return p.WriteFD(fd, mem) }

Page 38: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 write(int32 sd, void *data, int32 data_len); func (p *P) Write(vm *exec.VirtualMachine) int32 { var ( // ... mem = vm.Memory[int(ptr):int(ptr+len)] )

return p.WriteFD(fd, mem) }

Page 39: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 flush(int32 sd);Flushes any intermediately unwritten bytes Mostly used for the HTTP client

Page 40: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 flush(int32 sd);func (p *P) flush(vm *wasm.VirtualMachine) int32 { args := vm.GetCurrentFrame().Locals var desc = uint32(args[0]) return p.Flush(desc) }

Page 41: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

int32 flush(int32 sd);func (p *P) flush(vm *wasm.VirtualMachine) int32 { args := vm.GetCurrentFrame().Locals var desc = uint32(args[0]) return p.Flush(desc) }

Page 42: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Hello World (Zig)fn main() !void { const out = try Stream.open("stdout://"); const msg = "Hello, world!\n"; const ign = try out.write(&msg, msg.len); try out.close(); }

Page 43: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Hello World (Zig)fn main() !void { const out = try Stream.open("stdout://"); const msg = "Hello, world!\n"; const ign = try out.write(&msg, msg.len); try out.close(); }

Page 44: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Hello World (Zig)fn main() !void { const out = try Stream.open("stdout://"); const msg = "Hello, world!\n"; const ign = try out.write(&msg, msg.len); try out.close(); }

Page 45: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Hello World (Zig)fn main() !void { const out = try Stream.open("stdout://"); const msg = "Hello, world!\n"; const ign = try out.write(&msg, msg.len); try out.close(); }

Page 46: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Hello World (Zig)

$ dagger dagger_hello.wasm Hello, world! $

Page 47: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Future Plans for Dagger"Control streams" for meta-operations More stream kinds

Page 48: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

What This Can Build ToFunctions as a service backend Handling events Distributed computing Transactional computing

Page 49: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

What You Can DoPlay with the code Implement this from scratch CGI-Gopher?

Page 50: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Got Questions?Time is limited on stage Please email, tweet or ask me in person after the talk I'm happy to go into detail

Page 51: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

GReeTZBrian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck

Justin Clift Terry A. Davis Ayke van Laethem The people of #zig Vladimir Pouzanov https://pony.dev on Discord The WebAssembly Team You, for coming to/watching this talk

Page 52: WebAssembly on the Server: How System Calls Work...GReeTZ Brian Ketelsen Andrew Kelly Eric McClure Faith Alderson My coworkers at Lightspeed Andrei Tudor Călin Elliot Speck Justin

Follow my progress on GitHubhttps://github.com/Xe/olin