···11+---
22+title: "WebAssembly on the Server: How System Calls Work"
33+date: 2019-05-31
44+basename: ../webassembly-on-the-server-system-calls-2019-05-31
55+slides_link: https://cdn.xeiaso.net/file/christine-static/static/talks/wasm-on-the-server-system-calls.pdf
66+---
77+88+[Video](https://www.youtube.com/watch?v=G4l8RX0tA3E)
99+1010+## My Speaker Notes
1111+1212+- Hi, my name is Christine. I work as a senior SRE for Lightspeed. Today I'm gonna talk about something I've been researching and learning a lot about: WebAssembly on the server.
1313+- Something a lot of you might be asking: what is WebAssembly?
1414+ - WebAssembly is very new and there's a lot of confusing and overly vague coverage on it.
1515+ - In this talk, I will explain WebAssembly at a high level and show how to start solving one of the hardest problems in it: how to communicate with the outside world.
1616+ - When I say the "outside world" I mean anything that is not literally one of these 5 basic things:
1717+ - Externally imported functions, defined by the user
1818+ - The dynamic dispatch function table
1919+ - Global variables
2020+ - Linear memory, or basically ram
2121+ - Compiled functions, or your code that runs in the virtual machine
2222+- WebAssembly is a Virtual Machine format for the Web
2323+ - The closest analogue to WASM in its current form is a CPU and supporting hardware
2424+ - However, because it's a virtual machine, the hardware is irrelevant
2525+ - Though it was intended for browsers, the implementation of it is really generic.
2626+ - WebAssembly provides:
2727+ - External functions
2828+ - A function table for dynamic dispatch
2929+ - Immutable globals (as of the MVP)
3030+ - Linear memory
3131+ - Compiled functions (these exist outside of linear memory like an AVR chip)
3232+- Why WebAssembly on the Server?
3333+ - It makes hardware less relevant.
3434+ - Most of our industry targets a single vendor in basic configurations: Intel amd64 processors running Linux
3535+ - Intel has had many security bugs and it may not be a good idea to fundamentally design our architecture to rely on them.
3636+ - This also removes the OS from the equation for most compute tasks.
3737+- What are system calls and why do they matter?
3838+- System calls enforce abstractions to the outside world.
3939+ - Your code goes through system calls to reach things from the outside world, eg:
4040+ - Randomness
4141+ - Network sockets
4242+ - The filesystem
4343+ - Etc
4444+- How are they implemented?
4545+ - The platform your program runs on exposes those system calls
4646+ - Programs pass pointers into linear memory (this will be shown later in the slides)
4747+- Why is this relevant to WebAssembly?
4848+ - The WebAssembly Minimum Viable Product doesn't define any system calls
4949+- WebAssembly System Calls Out of The Box
5050+ - Yeah, nothing. You're on your own. This is both very good and very very bad.
5151+- So what's a pointer in WebAssembly?
5252+ - Simplified, a WebAssembly virtual machine is some structure that has a reference to a byte slice. That byte slice is treated as the linear memory of that VM.
5353+ - A pointer is just an offset into this slice
5454+ - Showing the WebAssembly world diagram from earlier: pointers apply to only this part of it. Function pointers _do_ exist in WebAssembly, just by the dynamic dispatch table from earlier.
5555+- So what can we do about it?
5656+- Let's introduce a pet project of mine for a few years. It's called Dagger, and it has been a fantastic stepping stone while other solutions are being invented.
5757+ - Dagger is a proof of concept system call API that I'll be walking through the high level implementation of
5858+ - It's got a very simple implementation (500-ish lines)
5959+ - It's intended for teaching and learning about the low levels of WebAssembly.
6060+ - It's based on a very very very simplistic view of the unix philosophy. In unix, everything is a file. With Dagger, everything is a stream, even HTTP.
6161+ - As such, there's no magic in Dagger.
6262+ - And even though it's so simple, it's still usable for more than just basic/trivial things.
6363+ - A dagger process has a bunch of streams in a slice.
6464+ - The API gives out and uses stream descriptors, or offsets into this slice.
6565+- Dagger's API is really really simple, it's only got 5 calls:
6666+ - Opening a stream
6767+ - Closing a stream
6868+ - Reading from a stream
6969+ - Writing to a stream
7070+ - Flushing intermediately buffered data from a stream to its remote (or local) target
7171+- Open
7272+ - Open opens a stream by URL, then returns its descriptor. It can also return an error instead.
7373+ - It's got 5 basic stream types:
7474+ - Logging
7575+ - Jailed filesystem access
7676+ - HTTP/S
7777+ - 5 system calls is all you need for HTTP!
7878+ - Randomness
7979+ - Standard input/output
8080+ - Let's walk through the code that implements it
8181+ - Here's a simplified view of the open function in a Dagger process.
8282+ - The system call arguments are here
8383+ - And the stream URL gets read from the VM memory here
8484+ - Remember that pointers are just integer offsets into memory
8585+ - Then this gets passed to the rest of the open file logic that isn't shown here
8686+- Close
8787+ - Closes a stream by its descriptor.
8888+ - It returns a negative error if anything goes wrong, which is unlikely.
8989+ - Let's walk through its code:
9090+ - It grabs the arguments from the VM
9191+ - Then it passes that to the rest of the logic that isn't shown here
9292+- Read
9393+ - Reads a limited amount of bytes from a stream
9494+ - Returns a negative error if things go wrong
9595+ - Let's walk through its code:
9696+ - This is a bigger function, so I've broken it up into a few slides.
9797+ - First it gets the arguments from the VM
9898+ - Then it creates the intermediate buffer to copy things into from the stream
9999+ - Then it does the reading into that buffer
100100+ - Then it copies the buffer into the VM ram
101101+- Write
102102+ - Write is very similar to read, except it just copies the ram out of the VM and into the stream
103103+ - It returns the number of bytes written, which SHOULD equal the data length argument
104104+ - Let's walk through the code:
105105+ - Again, this function is bigger so I
106106+- Flush
107107+ - Flush does just about what you'd think, it flushes intermediate buffers to the actual stream targets.
108108+ - This blocks until the flushing is complete
109109+ - Mostly used for the HTTP client
110110+ - Let's walk through its code:
111111+ - It gets the descriptor from the VM
112112+ - It runs the flush operation and returns the result
113113+- So, with all this covered, let's talk about usage. Here's the famous "Hello, world" example:
114114+ - This is in Zig, mainly because Zig allows me to be really concise. Things work just about as you'd expect so it's less of a logical jump than you'd think.
115115+ - First we try to open the stream. Dagger doesn't have any streams open in its environment by default, so we open standard output.
116116+ - Then we try to write the message to the stream. The interface in Zig is a bit rough right now, but it takes the pointer to the message and how long the message is. Zig doesn't let us implicitly ignore the return value of this function, so we just explicitly ignore it instead.
117117+ - Finally we try to close the output stream.
118118+ - The beauty of zig is that if any of these things we try to do fails, the entire function will fail.
119119+ - However none of this fails so we can just run it with the dagger tool and get this output:
120120+- What this can build to
121121+ - This basic idea can be used to build up to any of the following things:
122122+ - A functions as a service backend (See Olin)
123123+ - Generic event handlers
124124+ - Distributed computing
125125+ - Transactional computing
126126+- What you can do
127127+ - Play with the code (link at the end)
128128+ - Implement this API from scratch
129129+ - It's really not that hard
130130+ - A possible project idea I was going to do but ran out of time (moving internationally sucks) is to make a Gopher server with every route powered by WebAssembly
131131+- Got questions?
132132+ - Tweet or email me if you really want to make sure your questions get answered. That is one of the best ways to ensure I actually see it.
133133+ - I'm happy to go into detail, I can pull out code examples too.
134134+- Thanks to all of these people who have given help, ideas and inspiration. Without them I would never have been able to get this far.
135135+- Follow my progress on GitHub!
136136+ - I hope that QR code is big enough. If it's not let me know and I can make things like that bigger in the future somehow, hopefully.
···22title: "Making NixOS modules for fun and (hopefully) profit"
33date: 2023-09-15
44slides_link: https://drive.google.com/file/d/1h0Y8PUREuSrrSStmJMJ4MFHm0h53tf5a/view?usp=sharing
55+basename: ../asg-2023-nixos
56tags:
66- - nix
77- - nixos
88- - tailscale
77+ - nix
88+ - nixos
99+ - tailscale
910---
10111111-<XeblogConv name="Cadey" mood="coffee" standalone>There was an A/V glitch with the recording, my slides were intended to be black and white, but they somehow came out as purple and green. This couldn't be fixed even when trying several HDMI cables. If this becomes an issue I may re-record this talk in my home studio.</XeblogConv>
1212+<XeblogConv name="Cadey" mood="coffee" standalone>
1313+ There was an A/V glitch with the recording, my slides were intended to be
1414+ black and white, but they somehow came out as purple and green. This couldn't
1515+ be fixed even when trying several HDMI cables. If this becomes an issue I may
1616+ re-record this talk in my home studio.
1717+</XeblogConv>
12181319<XeblogVideo path="talks/2023/asg-nixos/video/proper"></XeblogVideo>
1420···297303};
298304```
299305300300-We defined the devShell to build the program development. We defined the package to build the software, and now we'll define the module to tell NixOS how to run the software. This is a basic NixOS module. It's defined inline to the flake for now, moving it to its own file is an exercise for the reader.
306306+We defined the devShell to build the program development. We defined the package to build the software, and now we'll define the module to tell NixOS how to run the software. This is a basic NixOS module. It's defined inline to the flake for now, moving it to its own file is an exercise for the reader.
301307302308Like I said before, a NixOS module is a function that takes in the state of the world and returns new additions to the state of the world. This NixOS module provides some options under `xe.services.douglas-adams-quotes` and then if the module is enabled, it creates a new systemd service to run it in. We're in the future, so we can use fancy things like DynamicUser to avoid having to run this service as root.
303309···360366}
361367```
362368363363-To make it import this, first we add a new input that points to the Douglas Adams quotes flake. This then gets threaded into the outputs function, we import the module, and finally enable it on the system.
369369+To make it import this, first we add a new input that points to the Douglas Adams quotes flake. This then gets threaded into the outputs function, we import the module, and finally enable it on the system.
364370365371(Pause)
366372···389395390396Get your phones out, I'm gonna be showing a QR Code:
391397392392-<XeblogConv name="Mara" mood="hacker" standalone>For those playing the Xe Iaso blog extended universe home game, visit [m85-kongir.shark-harmonic.ts.net](https://m85-kongir.shark-harmonic.ts.net) instead.</XeblogConv>
398398+<XeblogConv name="Mara" mood="hacker" standalone>
399399+ For those playing the Xe Iaso blog extended universe home game, visit
400400+ [m85-kongir.shark-harmonic.ts.net](https://m85-kongir.shark-harmonic.ts.net)
401401+ instead.
402402+</XeblogConv>
393403394404Scan this QR code. You can trust me, right? It's not gonna be a Rick Roll. I'm not that mean. When you do, you'll connect to my VM on my laptop on the conference wifi, yet still exposed to the public internet.
395405···421431422432There was a question about encrypted secrets in NixOS. I suggest using [agenix](https://github.com/ryantm/agenix) to have age-encrypted secrets in your NixOS configs. It has you encrypt things against SSH host public keys for your machines. It's a bit of a hack, but it works well enough that it's what I use in prod for my own stuff. This really needs to be solved upstream with proper handling of secret values at the Nix level.
423433424424-<XeblogConv name="Aoi" mood="wut">Why do you need to use something like agenix at all?</XeblogConv>
425425-<XeblogConv name="Mara" mood="hacker">Every file in `/nix/store` is world-readable. Depending on your threat model and if your NixOS configs are open source, this can be fine. If your threat model includes public NixOS configs, this becomes less fine; especially when CI is brought into the mix. You wouldn't want someone to figure out what your secrets are in your CI flow and then exfiltrate [Tailscale authkeys](https://tailscale.com/kb/1085/auth-keys/) or something, that could be bad!</XeblogConv>
434434+<XeblogConv name="Aoi" mood="wut">
435435+ Why do you need to use something like agenix at all?
436436+</XeblogConv>
437437+<XeblogConv name="Mara" mood="hacker">
438438+ Every file in `/nix/store` is world-readable. Depending on your threat model
439439+ and if your NixOS configs are open source, this can be fine. If your threat
440440+ model includes public NixOS configs, this becomes less fine; especially when
441441+ CI is brought into the mix. You wouldn't want someone to figure out what your
442442+ secrets are in your CI flow and then exfiltrate [Tailscale
443443+ authkeys](https://tailscale.com/kb/1085/auth-keys/) or something, that could
444444+ be bad!
445445+</XeblogConv>
426446427447---
428448
···11---
22title: How Static Code Analysis Prevents You From Waking Up at 3AM With Production on Fire
33date: 2022-06-09
44+basename: ../conf42-static-analysis
45slides_link: https://cdn.xeiaso.net/file/christine-static/talks/Conf42+SRE+2022.pdf
56---
67
···22title: |
33 My Blog is Hilariously Overengineered to the Point People Think it's a Static Site
44date: 2022-09-12
55+basename: ../how-my-website-works
56slides_link: https://drive.google.com/file/d/1B6dOK-nJbu30oT8fyNwBdvoOhHOJmreI/view
67tags:
78 - rust
···22title: "IRC: Why it Failed"
33date: 2018-05-17
44slides_link: https://cdn.xeiaso.net/file/christine-static/static/talks/irc-why-it-failed.pdf
55+basename: ../irc-why-it-failed-2018-05-17
56---
6778A brief discussion of the IRC protocol and why it has failed in today's internet.
···22title: How Nix and NixOS Get So Close to Perfect
33date: 2021-11-10
44slides_link: https://cdn.xeiaso.net/file/christine-static/static/talks/nixos-pain.pdf
55+basename: ../nixos-pain-2021-11-10
56tags:
66- - nix
77- - nixos
88- - docker
99- - packagingcon
77+ - nix
88+ - nixos
99+ - docker
1010+ - packagingcon
1011---
11121213## Author's Note
···319320documentation on every single thing that ships with NixOS by default. There
320321should be no module in the library of modules without documentation on how to
321322use it and an example or two of where you'd use some of the weirder options.
322322-323323324324<center>
325325 <picture>
···11---
22title: The Sheer Terror of PAM
33date: 2022-09-05
44+basename: ../rustconf-2022-sheer-terror-pam
45slides_link: https://cdn.xeiaso.net/file/christine-static/blog/rc2022/slides.pdf
56---
67
···11---
22title: The Subtle Magic of tsnet
33date: 2023-07-07
44+basename: ../subtle-magic-tsnet
45tags:
56 - tsnet
67 - go
···89---
9101011<div className="text-xl">
1111- This post was written while I worked for Tailscale. It is archived here for posterity.
1212+ This post was written while I worked for Tailscale. It is archived here for
1313+ posterity.
1214</div>
13151416<XeblogConv name="Cadey" mood="enby">
···22title: The Surreal Horror of PAM
33date: 2021-11-09
44slides_link: https://cdn.xeiaso.net/file/christine-static/static/talks/surreal-horror-pam.pdf
55+basename: ../surreal-horror-pam-2021-11-09
56tags:
66- - alpinelinux
77- - pam
88- - satire
77+ - alpinelinux
88+ - pam
99+ - satire
910---
10111112<iframe width="1043" height="587" src="https://www.youtube.com/embed/INjCiHUIjgg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
···137138really do not want to spend all day debugging PAM with gdb and strace on Ubuntu
138139to demonstrate it with that. PAM has a few kinds of modules:
139140140140-* authentication, this is not just checking your password, but also making sure
141141+- authentication, this is not just checking your password, but also making sure
141142 that your account is allowed to be logged into and setting up things like your
142143 preferred login shell
143143-* account, the things that assign a user an account based on the circumstances
144144+- account, the things that assign a user an account based on the circumstances
144145 of their authentication or validate that somehow (this is also where an LDAP
145146 server would get thrown into the mix if you really hate yourself)
146146-* password, the things that check passwords or do other kinds of validation like
147147+- password, the things that check passwords or do other kinds of validation like
147148 that (if you want to use Google Authenticator TOTP codes, you’d do that here)
148148-* session, these things handle other system errata like making sure the
149149+- session, these things handle other system errata like making sure the
149150 message-of-the-day (MOTD) is shown when you log in or letting logind know
150151 about the session so it can make a cgroup for you
151152
···11---
22title: "systemd: The Good Parts"
33date: 2021-05-16
44+basename: ../systemd-the-good-parts-2021-05-16
45slides_link: https://docs.google.com/presentation/d/1a0XaGu87xUcpQQVLkrnXKoKrdpN1ObiPrG9aGYVMw7k/edit?usp=sharing
56---
67···1617Alpine user for almost a decade and it's one of my favorite linux distributions.
17181819The best things in life come with disclaimers and here are the disclaimers for this talk:
2020+1921- This talk may contain opinions. These opinions are my own and not necessarily
2022 the opinions of my employer.
2123- This talk is not evangelism. This talk is intended to show how green the grass
···7274- What are the last few log lines?
7375- If you need to reboot the server right now for some reason, will that service
7476 come back up on reboot?
7575-7777+7678
77797880systemd includes a tool called systemctl that allows you to query the status of
···9395 those numbers aren't reflected here but it's actually much higher.
9496- At the bottom we can see the last few log lines. These are just random
9597 requests that people make to my blog.
9696-9898+9799If you haven't seen all of this in action before you might be wondering
98100something like "Wait, where did it get those logs from?"
99101···126128127129Now there's at least four classes of benefits for systemd and I'm going to break
128130them down into the following groups:
131131+129132- developers
130133- packagers
131134- system administrators
132135- users
133133-136136+134137In general people that are developing services that run on systemd get the following benefits:
135138136139- Predictability. systemd configuration files are declarative rather than
137140 imperative. You declare units instead of imperatively building up init
138141 scripts. Options are declared and enforced by the service manager. This makes
139139- it a lot easier to review changes for correctness.
142142+ it a lot easier to review changes for correctness.
140143- Portability. when setting up a service with systemd there's only one syntax to
141144 learn across 15 plus different distributions. This means that you don't have
142145 to maintain a giant pile of hacks to make the program just start consistently
···188191 operator. systemd is set up so that it's hard to do the wrong thing. It is
189192 hard to have logs go anywhere but the system journal. It is hard to write a
190193 unit that doesn't tell you if the service is actually running or not. And it
191191- makes it so that the path of least resistance will do most of what you want.
194194+ makes it so that the path of least resistance will do most of what you want.
192195- Sometimes system administrators have opinions that are different than the
193196 opinions of the packager. Sometimes you need to change environment variables
194197 for http proxies or something and sometimes you believe the packager has
···219222 incident response, so it is more difficult to have hidden dependencies.
220223221224As far as users go:
225225+222226- systemd is not limited to just managing system level services systemd can also
223227 manage user services with systemd user mode. I use this on my Linux system in
224228 order to have a couple services running in the background querying for weather
···11---
22title: Building Virtual Networks with Pulumi and Tailscale
33date: 2023-01-11
44+basename: ../virtual-networks-pulumi-tailscale
45tags:
55- - pulumi
66- - tailscale
66+ - pulumi
77+ - tailscale
78skip_ads: true
89---
9101010-<XeblogConv name="Cadey" mood="enby">This was a
1111-[workshop](https://www.pulumi.com/resources/building-virtual-networks-with-pulumi-and-tailscale/)
1212-that I helped with so that people could learn how to glue Tailscale and
1313-[Pulumi](https://www.pulumi.com/) (think Terraform but you can declare resources
1414-in programming languages such as TypeScript instead of HCL) together by creating
1515-a Tailscale subnet router to connect you to a VPC in AWS. I'm including the
1616-speaking bits that I did for the talk, but most of what I was there for was to
1717-help field questions about Tailscale. Internet streamer brain is a useful tool
1818-when properly harnessed.</XeblogConv>
1111+<XeblogConv name="Cadey" mood="enby">
1212+ This was a
1313+ [workshop](https://www.pulumi.com/resources/building-virtual-networks-with-pulumi-and-tailscale/)
1414+ that I helped with so that people could learn how to glue Tailscale and
1515+ [Pulumi](https://www.pulumi.com/) (think Terraform but you can declare
1616+ resources in programming languages such as TypeScript instead of HCL) together
1717+ by creating a Tailscale subnet router to connect you to a VPC in AWS. I'm
1818+ including the speaking bits that I did for the talk, but most of what I was
1919+ there for was to help field questions about Tailscale. Internet streamer brain
2020+ is a useful tool when properly harnessed.
2121+</XeblogConv>
19222023<XeblogVideo path="talks/pulumi-workshop-2023"></XeblogVideo>
2124···65686669Tailscale doesn't stop there, there's SSH management, file sharing, an
6770ngrok-like tunnelling solution, and so much more.
6868-7171+6972I'll hand things back over to Josh so we can learn more about Pulumi.
···11---
22title: The Go WebAssembly ABI at a Low Level
33date: 2022-10-17
44+basename: ../wasm-abi
45slides_link: "https://drive.google.com/file/d/1RKitNYC77AYnsstNsYvJcBNnaKT06stb/view?usp=sharing"
56tags:
67 - wasm
···11---
22title: "[talk] The carcinization of Go programs"
33date: 2023-03-24
44+basename: ../wazero-lightning-2023
45slides_link: https://drive.google.com/file/d/1ANxRJPzNeKbLogZz0wCH_o9E9jNLypja/view?usp=sharing
56tags:
66- - wasm
77- - wazero
88- - rust
99- - golang
77+ - wasm
88+ - wazero
99+ - rust
1010+ - golang
1011skip_ads: true
1112---
1213···16171718## Transcript
18191919-<XeblogConv standalone name="Cadey" mood="enby">This is a lightning
2020-talk version of [this
2121-post](https://xeiaso.net/blog/carcinization-golang).</XeblogConv>
2020+<XeblogConv standalone name="Cadey" mood="enby">
2121+ This is a lightning talk version of [this
2222+ post](https://xeiaso.net/blog/carcinization-golang).
2323+</XeblogConv>
22242325<XeblogSlide name="2023/wazero-lightning/01" essential></XeblogSlide>
2426···6062carcinization of Go programs via WebAssembly. This is how I snuck Rust
6163into a Go shop.
62646363-<XeblogConv name="Mara" mood="hacker">The "carcinization" refers to
6464-the evolutionary tendency of programs becoming either crabs or trees
6565-when time stretches to infinity. If you can imagine library use as
6666-evolution, then this joke makes more sense.</XeblogConv>
6565+<XeblogConv name="Mara" mood="hacker">
6666+ The "carcinization" refers to the evolutionary tendency of programs becoming
6767+ either crabs or trees when time stretches to infinity. If you can imagine
6868+ library use as evolution, then this joke makes more sense.
6969+</XeblogConv>
67706871<XeblogSlide name="2023/wazero-lightning/06"></XeblogSlide>
6972···8386this problem is fairly novel. Anything that was close to this just
8487made atrocities out of the text in ways that I couldn't customize.
85888686-<XeblogConv name="Aoi" mood="wut">I guess some solutions could be out
8787-there, but just locked in closed-source repos.</XeblogConv>
8989+<XeblogConv name="Aoi" mood="wut">
9090+ I guess some solutions could be out there, but just locked in closed-source
9191+ repos.
9292+</XeblogConv>
88938994<XeblogSlide name="2023/wazero-lightning/09"></XeblogSlide>
9095···100105101106When I wrote this program, I made it in a fairly naiive way. I took
102107HTML over standard input and had it spit out slackdown on standard
103103-output.
108108+output.
104109105110This idea just so happens to be one of the core parts of the Unix
106111philosophy: programs should be filters that take input in one form and
···143148And now this atrocity is shipped to production and holds together the
144149Mastodon post announcement service. Most people aren't aware that it's
145150a thing, and it runs fast enough that nobody really cares that it's a
146146-programming turducken of Go, Rust and WebAssembly. It works perfectly
151151+programming turducken of Go, Rust and WebAssembly. It works perfectly
147152and it wouldn't be possible without the efforts of the Wazero team.
148153149154<XeblogSlide name="2023/wazero-lightning/16" essential></XeblogSlide>
···11----
22-title: "WebAssembly on the Server: How System Calls Work"
33-date: 2019-05-31
44-slides_link: https://cdn.xeiaso.net/file/christine-static/static/talks/wasm-on-the-server-system-calls.pdf
55----
66-77-[Video](https://www.youtube.com/watch?v=G4l8RX0tA3E)
88-99-## My Speaker Notes
1010-1111-* Hi, my name is Christine. I work as a senior SRE for Lightspeed. Today I'm gonna talk about something I've been researching and learning a lot about: WebAssembly on the server.
1212-* Something a lot of you might be asking: what is WebAssembly?
1313- * WebAssembly is very new and there's a lot of confusing and overly vague coverage on it.
1414- * In this talk, I will explain WebAssembly at a high level and show how to start solving one of the hardest problems in it: how to communicate with the outside world.
1515- * When I say the "outside world" I mean anything that is not literally one of these 5 basic things:
1616- * Externally imported functions, defined by the user
1717- * The dynamic dispatch function table
1818- * Global variables
1919- * Linear memory, or basically ram
2020- * Compiled functions, or your code that runs in the virtual machine
2121-* WebAssembly is a Virtual Machine format for the Web
2222- * The closest analogue to WASM in its current form is a CPU and supporting hardware
2323- * However, because it's a virtual machine, the hardware is irrelevant
2424- * Though it was intended for browsers, the implementation of it is really generic.
2525- * WebAssembly provides:
2626- * External functions
2727- * A function table for dynamic dispatch
2828- * Immutable globals (as of the MVP)
2929- * Linear memory
3030- * Compiled functions (these exist outside of linear memory like an AVR chip)
3131-* Why WebAssembly on the Server?
3232- * It makes hardware less relevant.
3333- * Most of our industry targets a single vendor in basic configurations: Intel amd64 processors running Linux
3434- * Intel has had many security bugs and it may not be a good idea to fundamentally design our architecture to rely on them.
3535- * This also removes the OS from the equation for most compute tasks.
3636-* What are system calls and why do they matter?
3737-* System calls enforce abstractions to the outside world.
3838- * Your code goes through system calls to reach things from the outside world, eg:
3939- * Randomness
4040- * Network sockets
4141- * The filesystem
4242- * Etc
4343-* How are they implemented?
4444- * The platform your program runs on exposes those system calls
4545- * Programs pass pointers into linear memory (this will be shown later in the slides)
4646-* Why is this relevant to WebAssembly?
4747- * The WebAssembly Minimum Viable Product doesn't define any system calls
4848-* WebAssembly System Calls Out of The Box
4949- * Yeah, nothing. You're on your own. This is both very good and very very bad.
5050-* So what's a pointer in WebAssembly?
5151- * Simplified, a WebAssembly virtual machine is some structure that has a reference to a byte slice. That byte slice is treated as the linear memory of that VM.
5252- * A pointer is just an offset into this slice
5353- * Showing the WebAssembly world diagram from earlier: pointers apply to only this part of it. Function pointers _do_ exist in WebAssembly, just by the dynamic dispatch table from earlier.
5454-* So what can we do about it?
5555-* Let's introduce a pet project of mine for a few years. It's called Dagger, and it has been a fantastic stepping stone while other solutions are being invented.
5656- * Dagger is a proof of concept system call API that I'll be walking through the high level implementation of
5757- * It's got a very simple implementation (500-ish lines)
5858- * It's intended for teaching and learning about the low levels of WebAssembly.
5959- * It's based on a very very very simplistic view of the unix philosophy. In unix, everything is a file. With Dagger, everything is a stream, even HTTP.
6060- * As such, there's no magic in Dagger.
6161- * And even though it's so simple, it's still usable for more than just basic/trivial things.
6262- * A dagger process has a bunch of streams in a slice.
6363- * The API gives out and uses stream descriptors, or offsets into this slice.
6464-* Dagger's API is really really simple, it's only got 5 calls:
6565- * Opening a stream
6666- * Closing a stream
6767- * Reading from a stream
6868- * Writing to a stream
6969- * Flushing intermediately buffered data from a stream to its remote (or local) target
7070-* Open
7171- * Open opens a stream by URL, then returns its descriptor. It can also return an error instead.
7272- * It's got 5 basic stream types:
7373- * Logging
7474- * Jailed filesystem access
7575- * HTTP/S
7676- * 5 system calls is all you need for HTTP!
7777- * Randomness
7878- * Standard input/output
7979- * Let's walk through the code that implements it
8080- * Here's a simplified view of the open function in a Dagger process.
8181- * The system call arguments are here
8282- * And the stream URL gets read from the VM memory here
8383- * Remember that pointers are just integer offsets into memory
8484- * Then this gets passed to the rest of the open file logic that isn't shown here
8585-* Close
8686- * Closes a stream by its descriptor.
8787- * It returns a negative error if anything goes wrong, which is unlikely.
8888- * Let's walk through its code:
8989- * It grabs the arguments from the VM
9090- * Then it passes that to the rest of the logic that isn't shown here
9191-* Read
9292- * Reads a limited amount of bytes from a stream
9393- * Returns a negative error if things go wrong
9494- * Let's walk through its code:
9595- * This is a bigger function, so I've broken it up into a few slides.
9696- * First it gets the arguments from the VM
9797- * Then it creates the intermediate buffer to copy things into from the stream
9898- * Then it does the reading into that buffer
9999- * Then it copies the buffer into the VM ram
100100-* Write
101101- * Write is very similar to read, except it just copies the ram out of the VM and into the stream
102102- * It returns the number of bytes written, which SHOULD equal the data length argument
103103- * Let's walk through the code:
104104- * Again, this function is bigger so I
105105-* Flush
106106- * Flush does just about what you'd think, it flushes intermediate buffers to the actual stream targets.
107107- * This blocks until the flushing is complete
108108- * Mostly used for the HTTP client
109109- * Let's walk through its code:
110110- * It gets the descriptor from the VM
111111- * It runs the flush operation and returns the result
112112-* So, with all this covered, let's talk about usage. Here's the famous "Hello, world" example:
113113- * This is in Zig, mainly because Zig allows me to be really concise. Things work just about as you'd expect so it's less of a logical jump than you'd think.
114114- * First we try to open the stream. Dagger doesn't have any streams open in its environment by default, so we open standard output.
115115- * Then we try to write the message to the stream. The interface in Zig is a bit rough right now, but it takes the pointer to the message and how long the message is. Zig doesn't let us implicitly ignore the return value of this function, so we just explicitly ignore it instead.
116116- * Finally we try to close the output stream.
117117- * The beauty of zig is that if any of these things we try to do fails, the entire function will fail.
118118- * However none of this fails so we can just run it with the dagger tool and get this output:
119119-* What this can build to
120120- * This basic idea can be used to build up to any of the following things:
121121- * A functions as a service backend (See Olin)
122122- * Generic event handlers
123123- * Distributed computing
124124- * Transactional computing
125125-* What you can do
126126- * Play with the code (link at the end)
127127- * Implement this API from scratch
128128- * It's really not that hard
129129- * A possible project idea I was going to do but ran out of time (moving internationally sucks) is to make a Gopher server with every route powered by WebAssembly
130130-* Got questions?
131131- * Tweet or email me if you really want to make sure your questions get answered. That is one of the best ways to ensure I actually see it.
132132- * I'm happy to go into detail, I can pull out code examples too.
133133-* Thanks to all of these people who have given help, ideas and inspiration. Without them I would never have been able to get this far.
134134-* Follow my progress on GitHub!
135135- * I hope that QR code is big enough. If it's not let me know and I can make things like that bigger in the future somehow, hopefully.