master
  1//! wasi_snapshot_preview1 spec available (in witx format) here:
  2//! * typenames -- https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/witx/typenames.witx
  3//! * module -- https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/witx/wasi_snapshot_preview1.witx
  4//! Note that libc API does *not* go in this file. wasi libc API goes into std/c.zig instead.
  5const builtin = @import("builtin");
  6const std = @import("std");
  7const assert = std.debug.assert;
  8
  9comptime {
 10    if (builtin.os.tag == .wasi) {
 11        assert(@alignOf(i8) == 1);
 12        assert(@alignOf(u8) == 1);
 13        assert(@alignOf(i16) == 2);
 14        assert(@alignOf(u16) == 2);
 15        assert(@alignOf(i32) == 4);
 16        assert(@alignOf(u32) == 4);
 17        assert(@alignOf(i64) == 8);
 18        assert(@alignOf(u64) == 8);
 19    }
 20}
 21
 22pub const iovec_t = std.posix.iovec;
 23pub const ciovec_t = std.posix.iovec_const;
 24
 25pub extern "wasi_snapshot_preview1" fn args_get(argv: [*][*:0]u8, argv_buf: [*]u8) errno_t;
 26pub extern "wasi_snapshot_preview1" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t;
 27
 28pub extern "wasi_snapshot_preview1" fn clock_res_get(clock_id: clockid_t, resolution: *timestamp_t) errno_t;
 29pub extern "wasi_snapshot_preview1" fn clock_time_get(clock_id: clockid_t, precision: timestamp_t, timestamp: *timestamp_t) errno_t;
 30
 31pub extern "wasi_snapshot_preview1" fn environ_get(environ: [*][*:0]u8, environ_buf: [*]u8) errno_t;
 32pub extern "wasi_snapshot_preview1" fn environ_sizes_get(environ_count: *usize, environ_buf_size: *usize) errno_t;
 33
 34pub extern "wasi_snapshot_preview1" fn fd_advise(fd: fd_t, offset: filesize_t, len: filesize_t, advice: advice_t) errno_t;
 35pub extern "wasi_snapshot_preview1" fn fd_allocate(fd: fd_t, offset: filesize_t, len: filesize_t) errno_t;
 36pub extern "wasi_snapshot_preview1" fn fd_close(fd: fd_t) errno_t;
 37pub extern "wasi_snapshot_preview1" fn fd_datasync(fd: fd_t) errno_t;
 38pub extern "wasi_snapshot_preview1" fn fd_pread(fd: fd_t, iovs: [*]const iovec_t, iovs_len: usize, offset: filesize_t, nread: *usize) errno_t;
 39pub extern "wasi_snapshot_preview1" fn fd_pwrite(fd: fd_t, iovs: [*]const ciovec_t, iovs_len: usize, offset: filesize_t, nwritten: *usize) errno_t;
 40pub extern "wasi_snapshot_preview1" fn fd_read(fd: fd_t, iovs: [*]const iovec_t, iovs_len: usize, nread: *usize) errno_t;
 41pub extern "wasi_snapshot_preview1" fn fd_readdir(fd: fd_t, buf: [*]u8, buf_len: usize, cookie: dircookie_t, bufused: *usize) errno_t;
 42pub extern "wasi_snapshot_preview1" fn fd_renumber(from: fd_t, to: fd_t) errno_t;
 43pub extern "wasi_snapshot_preview1" fn fd_seek(fd: fd_t, offset: filedelta_t, whence: whence_t, newoffset: *filesize_t) errno_t;
 44pub extern "wasi_snapshot_preview1" fn fd_sync(fd: fd_t) errno_t;
 45pub extern "wasi_snapshot_preview1" fn fd_tell(fd: fd_t, newoffset: *filesize_t) errno_t;
 46pub extern "wasi_snapshot_preview1" fn fd_write(fd: fd_t, iovs: [*]const ciovec_t, iovs_len: usize, nwritten: *usize) errno_t;
 47
 48pub extern "wasi_snapshot_preview1" fn fd_fdstat_get(fd: fd_t, buf: *fdstat_t) errno_t;
 49pub extern "wasi_snapshot_preview1" fn fd_fdstat_set_flags(fd: fd_t, flags: fdflags_t) errno_t;
 50pub extern "wasi_snapshot_preview1" fn fd_fdstat_set_rights(fd: fd_t, fs_rights_base: rights_t, fs_rights_inheriting: rights_t) errno_t;
 51
 52pub extern "wasi_snapshot_preview1" fn fd_filestat_get(fd: fd_t, buf: *filestat_t) errno_t;
 53pub extern "wasi_snapshot_preview1" fn fd_filestat_set_size(fd: fd_t, st_size: filesize_t) errno_t;
 54pub extern "wasi_snapshot_preview1" fn fd_filestat_set_times(fd: fd_t, st_atim: timestamp_t, st_mtim: timestamp_t, fstflags: fstflags_t) errno_t;
 55
 56pub extern "wasi_snapshot_preview1" fn fd_prestat_get(fd: fd_t, buf: *prestat_t) errno_t;
 57pub extern "wasi_snapshot_preview1" fn fd_prestat_dir_name(fd: fd_t, path: [*]u8, path_len: usize) errno_t;
 58
 59pub extern "wasi_snapshot_preview1" fn path_create_directory(fd: fd_t, path: [*]const u8, path_len: usize) errno_t;
 60pub extern "wasi_snapshot_preview1" fn path_filestat_get(fd: fd_t, flags: lookupflags_t, path: [*]const u8, path_len: usize, buf: *filestat_t) errno_t;
 61pub extern "wasi_snapshot_preview1" fn path_filestat_set_times(fd: fd_t, flags: lookupflags_t, path: [*]const u8, path_len: usize, st_atim: timestamp_t, st_mtim: timestamp_t, fstflags: fstflags_t) errno_t;
 62pub extern "wasi_snapshot_preview1" fn path_link(old_fd: fd_t, old_flags: lookupflags_t, old_path: [*]const u8, old_path_len: usize, new_fd: fd_t, new_path: [*]const u8, new_path_len: usize) errno_t;
 63pub extern "wasi_snapshot_preview1" fn path_open(dirfd: fd_t, dirflags: lookupflags_t, path: [*]const u8, path_len: usize, oflags: oflags_t, fs_rights_base: rights_t, fs_rights_inheriting: rights_t, fs_flags: fdflags_t, fd: *fd_t) errno_t;
 64pub extern "wasi_snapshot_preview1" fn path_readlink(fd: fd_t, path: [*]const u8, path_len: usize, buf: [*]u8, buf_len: usize, bufused: *usize) errno_t;
 65pub extern "wasi_snapshot_preview1" fn path_remove_directory(fd: fd_t, path: [*]const u8, path_len: usize) errno_t;
 66pub extern "wasi_snapshot_preview1" fn path_rename(old_fd: fd_t, old_path: [*]const u8, old_path_len: usize, new_fd: fd_t, new_path: [*]const u8, new_path_len: usize) errno_t;
 67pub extern "wasi_snapshot_preview1" fn path_symlink(old_path: [*]const u8, old_path_len: usize, fd: fd_t, new_path: [*]const u8, new_path_len: usize) errno_t;
 68pub extern "wasi_snapshot_preview1" fn path_unlink_file(fd: fd_t, path: [*]const u8, path_len: usize) errno_t;
 69
 70pub extern "wasi_snapshot_preview1" fn poll_oneoff(in: *const subscription_t, out: *event_t, nsubscriptions: usize, nevents: *usize) errno_t;
 71
 72pub extern "wasi_snapshot_preview1" fn proc_exit(rval: exitcode_t) noreturn;
 73
 74pub extern "wasi_snapshot_preview1" fn random_get(buf: [*]u8, buf_len: usize) errno_t;
 75
 76pub extern "wasi_snapshot_preview1" fn sched_yield() errno_t;
 77
 78pub extern "wasi_snapshot_preview1" fn sock_accept(sock: fd_t, flags: fdflags_t, result_fd: *fd_t) errno_t;
 79pub extern "wasi_snapshot_preview1" fn sock_recv(sock: fd_t, ri_data: [*]iovec_t, ri_data_len: usize, ri_flags: riflags_t, ro_datalen: *usize, ro_flags: *roflags_t) errno_t;
 80pub extern "wasi_snapshot_preview1" fn sock_send(sock: fd_t, si_data: [*]const ciovec_t, si_data_len: usize, si_flags: siflags_t, so_datalen: *usize) errno_t;
 81pub extern "wasi_snapshot_preview1" fn sock_shutdown(sock: fd_t, how: sdflags_t) errno_t;
 82
 83// As defined in the wasi_snapshot_preview1 spec file:
 84// https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/witx/typenames.witx
 85pub const advice_t = enum(u8) {
 86    NORMAL = 0,
 87    SEQUENTIAL = 1,
 88    RANDOM = 2,
 89    WILLNEED = 3,
 90    DONTNEED = 4,
 91    NOREUSE = 5,
 92};
 93
 94pub const clockid_t = enum(u32) {
 95    REALTIME = 0,
 96    MONOTONIC = 1,
 97    PROCESS_CPUTIME_ID = 2,
 98    THREAD_CPUTIME_ID = 3,
 99};
100
101pub const device_t = u64;
102
103pub const dircookie_t = u64;
104pub const DIRCOOKIE_START: dircookie_t = 0;
105
106pub const dirnamlen_t = u32;
107
108pub const dirent_t = extern struct {
109    next: dircookie_t,
110    ino: inode_t,
111    namlen: dirnamlen_t,
112    type: filetype_t,
113};
114
115pub const errno_t = enum(u16) {
116    SUCCESS = 0,
117    @"2BIG" = 1,
118    ACCES = 2,
119    ADDRINUSE = 3,
120    ADDRNOTAVAIL = 4,
121    AFNOSUPPORT = 5,
122    /// This is also the error code used for `WOULDBLOCK`.
123    AGAIN = 6,
124    ALREADY = 7,
125    BADF = 8,
126    BADMSG = 9,
127    BUSY = 10,
128    CANCELED = 11,
129    CHILD = 12,
130    CONNABORTED = 13,
131    CONNREFUSED = 14,
132    CONNRESET = 15,
133    DEADLK = 16,
134    DESTADDRREQ = 17,
135    DOM = 18,
136    DQUOT = 19,
137    EXIST = 20,
138    FAULT = 21,
139    FBIG = 22,
140    HOSTUNREACH = 23,
141    IDRM = 24,
142    ILSEQ = 25,
143    INPROGRESS = 26,
144    INTR = 27,
145    INVAL = 28,
146    IO = 29,
147    ISCONN = 30,
148    ISDIR = 31,
149    LOOP = 32,
150    MFILE = 33,
151    MLINK = 34,
152    MSGSIZE = 35,
153    MULTIHOP = 36,
154    NAMETOOLONG = 37,
155    NETDOWN = 38,
156    NETRESET = 39,
157    NETUNREACH = 40,
158    NFILE = 41,
159    NOBUFS = 42,
160    NODEV = 43,
161    NOENT = 44,
162    NOEXEC = 45,
163    NOLCK = 46,
164    NOLINK = 47,
165    NOMEM = 48,
166    NOMSG = 49,
167    NOPROTOOPT = 50,
168    NOSPC = 51,
169    NOSYS = 52,
170    NOTCONN = 53,
171    NOTDIR = 54,
172    NOTEMPTY = 55,
173    NOTRECOVERABLE = 56,
174    NOTSOCK = 57,
175    /// This is also the code used for `NOTSUP`.
176    OPNOTSUPP = 58,
177    NOTTY = 59,
178    NXIO = 60,
179    OVERFLOW = 61,
180    OWNERDEAD = 62,
181    PERM = 63,
182    PIPE = 64,
183    PROTO = 65,
184    PROTONOSUPPORT = 66,
185    PROTOTYPE = 67,
186    RANGE = 68,
187    ROFS = 69,
188    SPIPE = 70,
189    SRCH = 71,
190    STALE = 72,
191    TIMEDOUT = 73,
192    TXTBSY = 74,
193    XDEV = 75,
194    NOTCAPABLE = 76,
195    _,
196};
197
198pub const event_t = extern struct {
199    userdata: userdata_t,
200    @"error": errno_t,
201    type: eventtype_t,
202    fd_readwrite: eventfdreadwrite_t,
203};
204
205pub const eventfdreadwrite_t = extern struct {
206    nbytes: filesize_t,
207    flags: eventrwflags_t,
208};
209
210pub const eventrwflags_t = u16;
211pub const EVENT_FD_READWRITE_HANGUP: eventrwflags_t = 0x0001;
212
213pub const eventtype_t = enum(u8) {
214    CLOCK = 0,
215    FD_READ = 1,
216    FD_WRITE = 2,
217};
218
219pub const exitcode_t = u32;
220
221pub const fd_t = i32;
222
223pub const fdflags_t = packed struct(u16) {
224    APPEND: bool = false,
225    DSYNC: bool = false,
226    NONBLOCK: bool = false,
227    RSYNC: bool = false,
228    SYNC: bool = false,
229    _: u11 = 0,
230};
231
232pub const fdstat_t = extern struct {
233    fs_filetype: filetype_t,
234    fs_flags: fdflags_t,
235    fs_rights_base: rights_t,
236    fs_rights_inheriting: rights_t,
237};
238
239pub const filedelta_t = i64;
240
241pub const filesize_t = u64;
242
243pub const filestat_t = extern struct {
244    dev: device_t,
245    ino: inode_t,
246    filetype: filetype_t,
247    nlink: linkcount_t,
248    size: filesize_t,
249    atim: timestamp_t,
250    mtim: timestamp_t,
251    ctim: timestamp_t,
252};
253
254pub const filetype_t = enum(u8) {
255    UNKNOWN,
256    BLOCK_DEVICE,
257    CHARACTER_DEVICE,
258    DIRECTORY,
259    REGULAR_FILE,
260    SOCKET_DGRAM,
261    SOCKET_STREAM,
262    SYMBOLIC_LINK,
263    _,
264};
265
266pub const fstflags_t = packed struct(u16) {
267    ATIM: bool = false,
268    ATIM_NOW: bool = false,
269    MTIM: bool = false,
270    MTIM_NOW: bool = false,
271    _: u12 = 0,
272};
273
274pub const inode_t = u64;
275
276pub const linkcount_t = u64;
277
278pub const lookupflags_t = packed struct(u32) {
279    SYMLINK_FOLLOW: bool = false,
280    _: u31 = 0,
281};
282
283pub const oflags_t = packed struct(u16) {
284    CREAT: bool = false,
285    DIRECTORY: bool = false,
286    EXCL: bool = false,
287    TRUNC: bool = false,
288    _: u12 = 0,
289};
290
291pub const preopentype_t = u8;
292pub const PREOPENTYPE_DIR: preopentype_t = 0;
293
294pub const prestat_t = extern struct {
295    pr_type: preopentype_t,
296    u: prestat_u_t,
297};
298
299pub const prestat_dir_t = extern struct {
300    pr_name_len: usize,
301};
302
303pub const prestat_u_t = extern union {
304    dir: prestat_dir_t,
305};
306
307pub const riflags_t = u16;
308pub const roflags_t = u16;
309
310pub const SOCK = struct {
311    pub const RECV_PEEK: riflags_t = 0x0001;
312    pub const RECV_WAITALL: riflags_t = 0x0002;
313
314    pub const RECV_DATA_TRUNCATED: roflags_t = 0x0001;
315};
316
317pub const rights_t = packed struct(u64) {
318    /// The right to invoke fd_datasync. If PATH_OPEN is set, includes the right to invoke
319    /// path_open with fdflags_t.dsync.
320    FD_DATASYNC: bool = false,
321    /// The right to invoke fd_read and sock_recv. If FD_SEEK is set, includes the right to invoke
322    /// fd_pread.
323    FD_READ: bool = false,
324    /// The right to invoke fd_seek. This flag implies FD_TELL.
325    FD_SEEK: bool = false,
326    /// The right to invoke fd_fdstat_set_flags.
327    FD_FDSTAT_SET_FLAGS: bool = false,
328    /// The right to invoke fd_sync. If PATH_OPEN is set, includes the right to invoke path_open
329    /// with fdflags_t.RSYNC and fdflags_t.DSYNC.
330    FD_SYNC: bool = false,
331    /// The right to invoke fd_seek in such a way that the file offset remains unaltered (i.e.
332    /// whence_t.CUR with offset zero), or to invoke fd_tell.
333    FD_TELL: bool = false,
334    /// The right to invoke fd_write and sock_send. If FD_SEEK is set, includes the right to invoke
335    /// fd_pwrite.
336    FD_WRITE: bool = false,
337    /// The right to invoke fd_advise.
338    FD_ADVISE: bool = false,
339    /// The right to invoke fd_allocate.
340    FD_ALLOCATE: bool = false,
341    /// The right to invoke path_create_directory.
342    PATH_CREATE_DIRECTORY: bool = false,
343    /// If PATH_OPEN is set, the right to invoke path_open with oflags_t.CREAT.
344    PATH_CREATE_FILE: bool = false,
345    /// The right to invoke path_link with the file descriptor as the source directory.
346    PATH_LINK_SOURCE: bool = false,
347    /// The right to invoke path_link with the file descriptor as the target directory.
348    PATH_LINK_TARGET: bool = false,
349    /// The right to invoke path_open.
350    PATH_OPEN: bool = false,
351    /// The right to invoke fd_readdir.
352    FD_READDIR: bool = false,
353    /// The right to invoke path_readlink.
354    PATH_READLINK: bool = false,
355    /// The right to invoke path_rename with the file descriptor as the source directory.
356    PATH_RENAME_SOURCE: bool = false,
357    /// The right to invoke path_rename with the file descriptor as the target directory.
358    PATH_RENAME_TARGET: bool = false,
359    /// The right to invoke path_filestat_get.
360    PATH_FILESTAT_GET: bool = false,
361    /// The right to change a file's size. If PATH_OPEN is set, includes the right to invoke
362    /// path_open with oflags_t.TRUNC. Note: there is no function named path_filestat_set_size.
363    /// This follows POSIX design, which only has ftruncate and does not provide ftruncateat. While
364    /// such function would be desirable from the API design perspective, there are virtually no
365    /// use cases for it since no code written for POSIX systems would use it. Moreover,
366    /// implementing it would require multiple syscalls, leading to inferior performance.
367    PATH_FILESTAT_SET_SIZE: bool = false,
368    /// The right to invoke path_filestat_set_times.
369    PATH_FILESTAT_SET_TIMES: bool = false,
370    /// The right to invoke fd_filestat_get.
371    FD_FILESTAT_GET: bool = false,
372    /// The right to invoke fd_filestat_set_size.
373    FD_FILESTAT_SET_SIZE: bool = false,
374    /// The right to invoke fd_filestat_set_times.
375    FD_FILESTAT_SET_TIMES: bool = false,
376    /// The right to invoke path_symlink.
377    PATH_SYMLINK: bool = false,
378    /// The right to invoke path_remove_directory.
379    PATH_REMOVE_DIRECTORY: bool = false,
380    /// The right to invoke path_unlink_file.
381    PATH_UNLINK_FILE: bool = false,
382    /// If FD_READ is set, includes the right to invoke poll_oneoff to subscribe to
383    /// eventtype_t.FD_READ. If FD_WRITE is set, includes the right to invoke poll_oneoff to
384    /// subscribe to eventtype_t.FD_WRITE.
385    POLL_FD_READWRITE: bool = false,
386    /// The right to invoke sock_shutdown.
387    SOCK_SHUTDOWN: bool = false,
388    /// The right to invoke sock_accept.
389    SOCK_ACCEPT: bool = false,
390    _: u34 = 0,
391};
392
393pub const sdflags_t = packed struct(u8) {
394    RD: bool = false,
395    WR: bool = false,
396    _: u6 = 0,
397};
398
399pub const siflags_t = u16;
400
401pub const signal_t = enum(u8) {
402    NONE = 0,
403    HUP = 1,
404    INT = 2,
405    QUIT = 3,
406    ILL = 4,
407    TRAP = 5,
408    ABRT = 6,
409    BUS = 7,
410    FPE = 8,
411    KILL = 9,
412    USR1 = 10,
413    SEGV = 11,
414    USR2 = 12,
415    PIPE = 13,
416    ALRM = 14,
417    TERM = 15,
418    CHLD = 16,
419    CONT = 17,
420    STOP = 18,
421    TSTP = 19,
422    TTIN = 20,
423    TTOU = 21,
424    URG = 22,
425    XCPU = 23,
426    XFSZ = 24,
427    VTALRM = 25,
428    PROF = 26,
429    WINCH = 27,
430    POLL = 28,
431    PWR = 29,
432    SYS = 30,
433};
434
435pub const subclockflags_t = u16;
436pub const SUBSCRIPTION_CLOCK_ABSTIME: subclockflags_t = 0x0001;
437
438pub const subscription_t = extern struct {
439    userdata: userdata_t,
440    u: subscription_u_t,
441};
442
443pub const subscription_clock_t = extern struct {
444    id: clockid_t,
445    timeout: timestamp_t,
446    precision: timestamp_t,
447    flags: subclockflags_t,
448};
449
450pub const subscription_fd_readwrite_t = extern struct {
451    fd: fd_t,
452};
453
454pub const subscription_u_t = extern struct {
455    tag: eventtype_t,
456    u: subscription_u_u_t,
457};
458
459pub const subscription_u_u_t = extern union {
460    clock: subscription_clock_t,
461    fd_read: subscription_fd_readwrite_t,
462    fd_write: subscription_fd_readwrite_t,
463};
464
465/// Nanoseconds.
466pub const timestamp_t = u64;
467
468pub const userdata_t = u64;
469
470pub const whence_t = enum(u8) { SET, CUR, END };