• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    Rust
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

WASI API proposal for managing sockets

WASI Sockets

A proposed WebAssembly System Interface API.

Current Phase

Phase 2

Champions

  • Dave Bakker (@badeend)

Phase 4 Advancement Criteria

  • At least two independent production implementations.
  • Implementations available for at least Windows, Linux & MacOS.
  • A testsuite that passes on the platforms and implementations mentioned above.

Table of Contents

Introduction

This proposal adds TCP & UDP sockets and domain name lookup to WASI. It adds the basic BSD socket interface with the intent to enable server and client networking software running on WebAssembly.

Unlike BSD sockets, WASI sockets require capability handles to create sockets and perform domain name lookups. On top of capability handles, WASI Socket implementations should implement deny-by-default firewalling.

The socket APIs have been split up into standalone protocol-specific WASI modules. Both current and future socket modules can then be tailored to the needs of that specific protocol and progress the standardization process independently.

This proposal introduces 4 new WASI modules:

Goals

  • Start out as an MVP; add the bare minimum amount of APIs required to create a basic functioning TCP/UDP application.
  • Toolchains must be able to provide a POSIX compatible interface on top of the functions introduced in this proposal.

Non-goals

  • SSL/TLS support
  • HTTP(S) support
  • Retrieving network-related information of the executing machine, like: installed network interfaces and the computer hostname.

API walk-through

[Walk through of how someone would use this API.]

Asynchronous APIs

At the moment, WIT has no built-in way of expressing asynchronous operations. To work around this limitation, we split up async functions into two parts: start-* and finish-*.

Desired signature:

operation: func(this, the-inputs...) -> future<result<the-outputs..., error-code>>

Temporary workaround:

start-operation: func(this, the-inputs...) -> result<_, error-code>
finish-operation: func(this) -> result<the-outputs..., error-code>

The semantics are as follows:

  • When start-* completes successfully:
    • The operation should be considered "in progress".
    • This is the POSIX equivalent of EINPROGRESS.
    • The socket can be polled for completion of the just started operation, using wasi-poll.
    • Its corresponding finish-* function can be called until it returns something other than the would-block error code.
  • When finish-* returns anything other than would-block:
    • The asynchronous operation should be considered "finished" (either successful or failed)
    • Future calls to finish-* return the not-in-progress error code.

Runtimes that don't need asynchrony, can simply validate the arguments provided to the start function and stash them on their internal socket instance and perform the actual syscall in the finish function. Conveniently, sockets only allow one of these start/finish asynchronous operation to be active at a time.

Example of how to recover blocking semantics in guest code:

// Pseudo code:
fn blocking-connect(sock: tcp-socket, addr: ip-socket-address) -> result<tuple<input-stream, output-stream>, error-code> {
	
	let pollable = tcp::subscribe(tcp-socket);

	let start-result = tcp::start-connect(sock, addr);
	if (start-result is error) {
		return error;
	}

	while (true) {
		poll::poll-oneoff([ pollable ]);

		let finish-result = tcp::finish-connect(sock);
		if (finish-result is NOT error(would-block)) {
			return finish-result;
		}
	}
}

Use case: Wasm module per connection

Thanks to the low startup cost of Wasm modules, its feasible for server software with Wasm integration to spawn a Wasm module for each inbound connection. Each module instance is passed only the accepted client socket. This way, all connection handlers are completely isolated from each other. This resembles PHP's "shared nothing" architecture.

[Use case 2]

[Provide example code snippets and diagrams explaining how the API would be used to solve the given problem]

Detailed design discussion

[This section should mostly refer to the .wit.md file that specifies the API. This section is for any discussion of the choices made in the API which don't make sense to document in the spec file itself.]

Dualstack sockets

IPv6 sockets returned by this proposal are never dualstack because that can't easily be implemented in a cross platform manner. If an application wants to serve both IPv4 and IPv6 traffic, it should create two sockets; one for IPv4 traffic and one for IPv6 traffic.

This behaviour is deemed acceptable because all existing applications that are truly cross-platform must already handle this scenario. Dualstack support can be part of a future proposal adding it as an opt-in feature.

Related issue: Emulate dualstack sockets in userspace

Modularity

This proposal is not POSIX compatible by itself. The BSD sockets interface is highly generic. The same functions have different semantics depending on which kind of socket they're called on. The man-pages are riddled with conditional documentation. If this had been translated 1:1 into a WASI API using Interface Types, this would have resulted in a proliferation of optional parameters and result types.

Instead, the sockets API has been split up into protocol-specific modules. All BSD socket functions have been pushed into these protocol-specific modules and tailored to their specific needs. Functions, parameters and flags that did not apply within a specific context have been dropped.

A downside of this approach is that functions that do not differ per protocol (bind, local_address, connect, shutdown, ...) are duplicated as well.

POSIX compatibility

See Posix-compatibility.md.

Why not getaddrinfo?

The proposed wasi-ip-name-lookup module focuses strictly on translating internet domain names to ip addresses and nothing else.

Like BSD sockets, getaddrinfo is very generic and multipurpose by design. The proposed WASI API is not. This eliminates many of the other "hats" getaddrinfo has (and potential security holes), like:

  • Mapping service names to port numbers ("https" -> 443)
  • Mapping service names/ports to socket types ("https" -> SOCK_STREAM)
  • Network interface name translation (%eth0 -> 1)
  • IP address deserialization ("127.0.0.1" -> Ipv4Address(127, 0, 0, 1))
  • IP address string canonicalization ("0:0:0:0:0:0:0:1" -> "::1")
  • Constants lookup for INADDR_ANY, INADDR_LOOPBACK, IN6ADDR_ANY_INIT and IN6ADDR_LOOPBACK_INIT.

Many of these functionalities can be shimmed in the libc implementation. Though some require future WASI additions. An example is network interface name translation. That requires a future if_nametoindex-like syscall.

Security

Wasm modules can not open sockets by themselves without a network capability handle. Even with capability handles, WASI implementations should deny all network access by default. Access should be granted at the most granular level possible. See Granting Access for examples. Whenever access is denied, the implementation should return EACCES.

This means Wasm modules will get a lot more EACCES errors compared to when running unsandboxed. This might break existing applications that, for example, don't expect creating a TCP client to require special permissions.

At the moment there is no way for a Wasm modules to query which network access permissions it has. The only thing it can do, is to just call the WASI functions it needs and see if they fail.

Deferred permission requests

This proposal does not specify how wasm runtimes should handle network permissions. One method could be to let end users declare on the command line which endpoints a wasm component may connect to. Another method could be to somehow let component authors distribute a manifest alongside the component itself, containing the set of permissions that it requires.

Both of these examples depend on the network permissions being known and granted upfront. This is not always feasible and that's usually where dynamic permission requests come into play.

The most likely contenders for permission prompt interception are:

  • TCP: connect
  • TCP: bind
  • TCP: listen
  • UDP: bind
  • UDP: connect

Now, again, this proposal does not specify if/how permission prompts should be implemented. However, it does at least facilitate the ability for runtimes to do so. Since waiting for user input takes an unknowable amount of time, the operations listed above have been made asynchronous. POSIX-compatibility layers can simply synchronously block on the returned futures.

Considered alternatives

[This section is not required if you already covered considered alternatives in the design discussion above.]

[Alternative 1]

[Describe an alternative which was considered, and why you decided against it.]

[Alternative 2]

[etc.]

Stakeholder Interest & Feedback

TODO before entering Phase 3.

[This should include a list of implementers who have expressed interest in implementing the proposal]

References & acknowledgements

Many thanks for valuable feedback and advice from:

  • [Person 1]
  • [Person 2]
  • [etc.]

More Repositories

1

design

WebAssembly Design Documents
11,261
star
2

binaryen

Optimizer and compiler/toolchain library for WebAssembly
WebAssembly
7,376
star
3

wabt

The WebAssembly Binary Toolkit
C++
6,778
star
4

WASI

WebAssembly System Interface
Rust
4,810
star
5

spec

WebAssembly specification, reference interpreter, and test suite.
WebAssembly
3,148
star
6

wasi-sdk

WASI-enabled WebAssembly C/C++ toolchain
CMake
1,224
star
7

gc

Branch of the spec repo scoped to discussion of GC integration in WebAssembly
WebAssembly
982
star
8

component-model

Repository for design and specification of the Component Model
Python
945
star
9

proposals

Tracking WebAssembly proposals
849
star
10

wasi-libc

WASI libc implementation for WebAssembly
C
836
star
11

interface-types

WebAssembly
641
star
12

threads

Threads and Atomics in WebAssembly
WebAssembly
577
star
13

wasm-c-api

Wasm C API prototype
C++
539
star
14

simd

Branch of the spec repo scoped to discussion of SIMD in WebAssembly
WebAssembly
531
star
15

meetings

WebAssembly meetings (VC or in-person), agendas, and notes
HTML
468
star
16

wasi-nn

Neural Network proposal for WASI
449
star
17

esm-integration

ECMAScript module integration
WebAssembly
370
star
18

wasm-jit-prototype

Standalone VM using LLVM JIT
C++
309
star
19

tool-conventions

Conventions supporting interoperatibility between tools working with WebAssembly.
297
star
20

website

WebAssembly website
CSS
270
star
21

memory64

Memory with 64-bit indexes
WebAssembly
210
star
22

testsuite

Mirror of the spec testsuite
WebAssembly
191
star
23

wasi-filesystem

Filesystem API for WASI
167
star
24

reference-types

Proposal for adding basic reference types (anyref)
WebAssembly
163
star
25

wasi-crypto

WASI Cryptography API Proposal
Makefile
162
star
26

exception-handling

Proposal to add exception handling to WebAssembly
WebAssembly
156
star
27

wasi-http

142
star
28

wasi-threads

WebAssembly
136
star
29

wasi-io

I/O Types proposal for WASI
132
star
30

multi-memory

Multiple per-module memories for Wasm
WebAssembly
127
star
31

stack-switching

A repository for the stack switching proposal.
WebAssembly
124
star
32

module-linking

Proposal for allowing modules to define, import and export modules and instances
WebAssembly
120
star
33

tail-call

Proposal to add tail calls to WebAssembly
WebAssembly
110
star
34

wasp

WebAssembly module decoder in C++
C++
106
star
35

function-references

Proposal for Typed Function References
WebAssembly
99
star
36

debugging

Design documents and discussions about debug support in WebAssembly
98
star
37

wasmint

Library for interpreting / debugging wasm code
C++
94
star
38

bulk-memory-operations

Bulk memory operations
WebAssembly
74
star
39

wasi-webgpu

72
star
40

js-promise-integration

JavaScript Promise Integration
WebAssembly
69
star
41

multi-value

Proposal to add multi-values to WebAssembly
WebAssembly
66
star
42

wasi-testsuite

WASI Testsuite
Rust
51
star
43

wasi-cloud-core

Rust
50
star
44

flexible-vectors

Vector operations for WebAssembly
WebAssembly
48
star
45

waterfall

Build and test bots
45
star
46

js-types

Proposal for adding type reflection to the JS API
WebAssembly
44
star
47

relaxed-simd

Relax the strict determinism requirements of SIMD operations.
WebAssembly
43
star
48

content-security-policy

WebAssembly
38
star
49

shared-everything-threads

A draft proposal for spawning threads in WebAssembly
WebAssembly
38
star
50

stringref

WebAssembly
37
star
51

JS-BigInt-integration

JavaScript BigInt to WebAssembly i64 integration
WebAssembly
36
star
52

wasi-keyvalue

35
star
53

wasi-libc-old

Precursor to WASI libc.
C
35
star
54

wasi-clocks

Clocks API for WASI
35
star
55

wasi-random

Entropy source API for WASI
29
star
56

benchmarks

Resources for collaborative benchmarking
JavaScript
27
star
57

wasi-sql

25
star
58

wasi-sql-embed

23
star
59

memory-control

A proposal to introduce finer grained control of WebAssembly memory.
WebAssembly
21
star
60

annotations

Proposal for Custom Annotation Syntax in the Text Format
WebAssembly
20
star
61

proposal-type-imports

Proposal for Type Imports & Exports
WebAssembly
20
star
62

constant-time

Constant-time WebAssembly
WebAssembly
20
star
63

wasi-proposal-template

Starter template for proposing a new WASI API
19
star
64

wasi-messaging

Messaging proposal for WASI
18
star
65

wasi-tools

WASI tools
Rust
17
star
66

funclets

Proposal for adding funclets - flexible intraprocedural control flow
WebAssembly
17
star
67

wasi-parallel

wasi-parallel is a proposal to add a parallel for construct to WASI.
Shell
17
star
68

sign-extension-ops

Sign-extension opcodes
WebAssembly
17
star
69

mutable-global

Import & export of mutable globals
WebAssembly
16
star
70

wasi-poll

16
star
71

wasi-cli

Command-Line Interface (CLI) World for WASI
15
star
72

extended-const

Proposal for extended constant expressions
WebAssembly
15
star
73

wasi-observe

Observability World for WASI
Just
15
star
74

instrument-tracing

Proposal to add instrumentation and tracing instructions to WebAssembly
WebAssembly
14
star
75

nontrapping-float-to-int-conversions

Proposal to add non-trapping float-to-int conversions to WebAssembly
WebAssembly
14
star
76

wasi-blobstore

14
star
77

conditional-sections

WebAssembly
13
star
78

wasi-grpc

13
star
79

profiles

Profiles proposal
WebAssembly
12
star
80

wasi-logging

WASI logging API
11
star
81

wasi-i2c

I2C API for WASI
Rust
11
star
82

extended-name-section

WebAssembly
11
star
83

feature-detection

WebAssembly
10
star
84

wasi-config

10
star
85

webassembly.github.io

Redirect to webassembly.org
HTML
10
star
86

testsuite-js

WebAssembly testsuite tests converted to single-file JavaScript tests
JavaScript
9
star
87

lld

Staging repository for upstreaming WebAssembly support into lld
C++
9
star
88

branch-hinting

Proposal to add branch hinting functionality to WebAssembly
WebAssembly
9
star
89

decompressor-prototype

C++
8
star
90

wat-numeric-values

Proposal to add numeric values to data segment definitions in the text format
WebAssembly
7
star
91

gc-js-customization

WebAssembly
7
star
92

js-string-builtins

JS String Builtins
WebAssembly
6
star
93

wasi-classic-command

6
star
94

wasi-url

6
star
95

half-precision

Proposal to introduce half precision operations
WebAssembly
6
star
96

wit-abi-up-to-date

5
star
97

wasi-spi

Rust
5
star
98

wg-charter

Proposed WebAssembly Working Group charter
HTML
5
star
99

call-tags

WebAssembly
4
star
100

cg-charter

Proposed WebAssembly Community Group charter
HTML
4
star