1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!("wasi_host.rs");
pub type void = ::std::os::raw::c_void;
pub unsafe fn ciovec_to_nix<'a>(ciovec: &'a __wasi_ciovec_t) -> nix::sys::uio::IoVec<&'a [u8]> {
let slice = std::slice::from_raw_parts(ciovec.buf as *const u8, ciovec.buf_len);
nix::sys::uio::IoVec::from_slice(slice)
}
pub unsafe fn ciovec_to_nix_mut<'a>(
ciovec: &'a mut __wasi_ciovec_t,
) -> nix::sys::uio::IoVec<&'a mut [u8]> {
let slice = std::slice::from_raw_parts_mut(ciovec.buf as *mut u8, ciovec.buf_len);
nix::sys::uio::IoVec::from_mut_slice(slice)
}
pub unsafe fn iovec_to_nix<'a>(iovec: &'a __wasi_iovec_t) -> nix::sys::uio::IoVec<&'a [u8]> {
let slice = std::slice::from_raw_parts(iovec.buf as *const u8, iovec.buf_len);
nix::sys::uio::IoVec::from_slice(slice)
}
pub unsafe fn iovec_to_nix_mut<'a>(
iovec: &'a mut __wasi_iovec_t,
) -> nix::sys::uio::IoVec<&'a mut [u8]> {
let slice = std::slice::from_raw_parts_mut(iovec.buf as *mut u8, iovec.buf_len);
nix::sys::uio::IoVec::from_mut_slice(slice)
}
pub fn errno_from_nix(errno: nix::errno::Errno) -> __wasi_errno_t {
let e = match errno {
nix::errno::Errno::EPERM => __WASI_EPERM,
nix::errno::Errno::ENOENT => __WASI_ENOENT,
nix::errno::Errno::ESRCH => __WASI_ESRCH,
nix::errno::Errno::EINTR => __WASI_EINTR,
nix::errno::Errno::EIO => __WASI_EIO,
nix::errno::Errno::ENXIO => __WASI_ENXIO,
nix::errno::Errno::E2BIG => __WASI_E2BIG,
nix::errno::Errno::ENOEXEC => __WASI_ENOEXEC,
nix::errno::Errno::EBADF => __WASI_EBADF,
nix::errno::Errno::ECHILD => __WASI_ECHILD,
nix::errno::Errno::EAGAIN => __WASI_EAGAIN,
nix::errno::Errno::ENOMEM => __WASI_ENOMEM,
nix::errno::Errno::EACCES => __WASI_EACCES,
nix::errno::Errno::EFAULT => __WASI_EFAULT,
nix::errno::Errno::EBUSY => __WASI_EBUSY,
nix::errno::Errno::EEXIST => __WASI_EEXIST,
nix::errno::Errno::EXDEV => __WASI_EXDEV,
nix::errno::Errno::ENODEV => __WASI_ENODEV,
nix::errno::Errno::ENOTDIR => __WASI_ENOTDIR,
nix::errno::Errno::EISDIR => __WASI_EISDIR,
nix::errno::Errno::EINVAL => __WASI_EINVAL,
nix::errno::Errno::ENFILE => __WASI_ENFILE,
nix::errno::Errno::EMFILE => __WASI_EMFILE,
nix::errno::Errno::ENOTTY => __WASI_ENOTTY,
nix::errno::Errno::ETXTBSY => __WASI_ETXTBSY,
nix::errno::Errno::EFBIG => __WASI_EFBIG,
nix::errno::Errno::ENOSPC => __WASI_ENOSPC,
nix::errno::Errno::ESPIPE => __WASI_ESPIPE,
nix::errno::Errno::EROFS => __WASI_EROFS,
nix::errno::Errno::EMLINK => __WASI_EMLINK,
nix::errno::Errno::EPIPE => __WASI_EPIPE,
nix::errno::Errno::EDOM => __WASI_EDOM,
nix::errno::Errno::ERANGE => __WASI_ERANGE,
nix::errno::Errno::EDEADLK => __WASI_EDEADLK,
nix::errno::Errno::ENAMETOOLONG => __WASI_ENAMETOOLONG,
nix::errno::Errno::ENOLCK => __WASI_ENOLCK,
nix::errno::Errno::ENOSYS => __WASI_ENOSYS,
nix::errno::Errno::ENOTEMPTY => __WASI_ENOTEMPTY,
nix::errno::Errno::ELOOP => __WASI_ELOOP,
nix::errno::Errno::ENOMSG => __WASI_ENOMSG,
nix::errno::Errno::EIDRM => __WASI_EIDRM,
nix::errno::Errno::ENOLINK => __WASI_ENOLINK,
nix::errno::Errno::EPROTO => __WASI_EPROTO,
nix::errno::Errno::EMULTIHOP => __WASI_EMULTIHOP,
nix::errno::Errno::EBADMSG => __WASI_EBADMSG,
nix::errno::Errno::EOVERFLOW => __WASI_EOVERFLOW,
nix::errno::Errno::EILSEQ => __WASI_EILSEQ,
nix::errno::Errno::ENOTSOCK => __WASI_ENOTSOCK,
nix::errno::Errno::EDESTADDRREQ => __WASI_EDESTADDRREQ,
nix::errno::Errno::EMSGSIZE => __WASI_EMSGSIZE,
nix::errno::Errno::EPROTOTYPE => __WASI_EPROTOTYPE,
nix::errno::Errno::ENOPROTOOPT => __WASI_ENOPROTOOPT,
nix::errno::Errno::EPROTONOSUPPORT => __WASI_EPROTONOSUPPORT,
nix::errno::Errno::EAFNOSUPPORT => __WASI_EAFNOSUPPORT,
nix::errno::Errno::EADDRINUSE => __WASI_EADDRINUSE,
nix::errno::Errno::EADDRNOTAVAIL => __WASI_EADDRNOTAVAIL,
nix::errno::Errno::ENETDOWN => __WASI_ENETDOWN,
nix::errno::Errno::ENETUNREACH => __WASI_ENETUNREACH,
nix::errno::Errno::ENETRESET => __WASI_ENETRESET,
nix::errno::Errno::ECONNABORTED => __WASI_ECONNABORTED,
nix::errno::Errno::ECONNRESET => __WASI_ECONNRESET,
nix::errno::Errno::ENOBUFS => __WASI_ENOBUFS,
nix::errno::Errno::EISCONN => __WASI_EISCONN,
nix::errno::Errno::ENOTCONN => __WASI_ENOTCONN,
nix::errno::Errno::ETIMEDOUT => __WASI_ETIMEDOUT,
nix::errno::Errno::ECONNREFUSED => __WASI_ECONNREFUSED,
nix::errno::Errno::EHOSTUNREACH => __WASI_EHOSTUNREACH,
nix::errno::Errno::EALREADY => __WASI_EALREADY,
nix::errno::Errno::EINPROGRESS => __WASI_EINPROGRESS,
nix::errno::Errno::ESTALE => __WASI_ESTALE,
nix::errno::Errno::EDQUOT => __WASI_EDQUOT,
nix::errno::Errno::ECANCELED => __WASI_ECANCELED,
nix::errno::Errno::EOWNERDEAD => __WASI_EOWNERDEAD,
nix::errno::Errno::ENOTRECOVERABLE => __WASI_ENOTRECOVERABLE,
_ => __WASI_ENOSYS,
};
e as __wasi_errno_t
}
#[cfg(target_os = "linux")]
const O_RSYNC: nix::fcntl::OFlag = nix::fcntl::OFlag::O_RSYNC;
#[cfg(not(target_os = "linux"))]
const O_RSYNC: nix::fcntl::OFlag = nix::fcntl::OFlag::O_SYNC;
pub fn nix_from_fdflags(fdflags: __wasi_fdflags_t) -> nix::fcntl::OFlag {
use nix::fcntl::OFlag;
let mut nix_flags = OFlag::empty();
if fdflags & (__WASI_FDFLAG_APPEND as __wasi_fdflags_t) != 0 {
nix_flags.insert(OFlag::O_APPEND);
}
if fdflags & (__WASI_FDFLAG_DSYNC as __wasi_fdflags_t) != 0 {
nix_flags.insert(OFlag::O_DSYNC);
}
if fdflags & (__WASI_FDFLAG_NONBLOCK as __wasi_fdflags_t) != 0 {
nix_flags.insert(OFlag::O_NONBLOCK);
}
if fdflags & (__WASI_FDFLAG_RSYNC as __wasi_fdflags_t) != 0 {
nix_flags.insert(O_RSYNC);
}
if fdflags & (__WASI_FDFLAG_SYNC as __wasi_fdflags_t) != 0 {
nix_flags.insert(OFlag::O_SYNC);
}
nix_flags
}
pub fn fdflags_from_nix(oflags: nix::fcntl::OFlag) -> __wasi_fdflags_t {
use nix::fcntl::OFlag;
let mut fdflags = 0;
if oflags.contains(OFlag::O_APPEND) {
fdflags |= __WASI_FDFLAG_APPEND;
}
if oflags.contains(OFlag::O_DSYNC) {
fdflags |= __WASI_FDFLAG_DSYNC;
}
if oflags.contains(OFlag::O_NONBLOCK) {
fdflags |= __WASI_FDFLAG_NONBLOCK;
}
if oflags.contains(O_RSYNC) {
fdflags |= __WASI_FDFLAG_RSYNC;
}
if oflags.contains(OFlag::O_SYNC) {
fdflags |= __WASI_FDFLAG_SYNC;
}
fdflags as __wasi_fdflags_t
}
pub fn nix_from_oflags(oflags: __wasi_oflags_t) -> nix::fcntl::OFlag {
use nix::fcntl::OFlag;
let mut nix_flags = OFlag::empty();
if oflags & (__WASI_O_CREAT as __wasi_oflags_t) != 0 {
nix_flags.insert(OFlag::O_CREAT);
}
if oflags & (__WASI_O_DIRECTORY as __wasi_oflags_t) != 0 {
nix_flags.insert(OFlag::O_DIRECTORY);
}
if oflags & (__WASI_O_EXCL as __wasi_oflags_t) != 0 {
nix_flags.insert(OFlag::O_EXCL);
}
if oflags & (__WASI_O_TRUNC as __wasi_oflags_t) != 0 {
nix_flags.insert(OFlag::O_TRUNC);
}
nix_flags
}
pub fn filetype_from_nix(sflags: nix::sys::stat::SFlag) -> __wasi_filetype_t {
use nix::sys::stat::SFlag;
if sflags.contains(SFlag::S_IFCHR) {
__WASI_FILETYPE_CHARACTER_DEVICE as __wasi_filetype_t
} else if sflags.contains(SFlag::S_IFBLK) {
__WASI_FILETYPE_BLOCK_DEVICE as __wasi_filetype_t
} else if sflags.contains(SFlag::S_IFIFO) | sflags.contains(SFlag::S_IFSOCK) {
__WASI_FILETYPE_SOCKET_STREAM as __wasi_filetype_t
} else if sflags.contains(SFlag::S_IFDIR) {
__WASI_FILETYPE_DIRECTORY as __wasi_filetype_t
} else if sflags.contains(SFlag::S_IFREG) {
__WASI_FILETYPE_REGULAR_FILE as __wasi_filetype_t
} else if sflags.contains(SFlag::S_IFLNK) {
__WASI_FILETYPE_SYMBOLIC_LINK as __wasi_filetype_t
} else {
__WASI_FILETYPE_UNKNOWN as __wasi_filetype_t
}
}
pub fn nix_from_filetype(sflags: __wasi_filetype_t) -> nix::sys::stat::SFlag {
use nix::sys::stat::SFlag;
let mut nix_sflags = SFlag::empty();
if sflags & (__WASI_FILETYPE_CHARACTER_DEVICE as __wasi_filetype_t) != 0 {
nix_sflags.insert(SFlag::S_IFCHR);
}
if sflags & (__WASI_FILETYPE_BLOCK_DEVICE as __wasi_filetype_t) != 0 {
nix_sflags.insert(SFlag::S_IFBLK);
}
if sflags & (__WASI_FILETYPE_SOCKET_STREAM as __wasi_filetype_t) != 0 {
nix_sflags.insert(SFlag::S_IFIFO);
nix_sflags.insert(SFlag::S_IFSOCK);
}
if sflags & (__WASI_FILETYPE_DIRECTORY as __wasi_filetype_t) != 0 {
nix_sflags.insert(SFlag::S_IFDIR);
}
if sflags & (__WASI_FILETYPE_REGULAR_FILE as __wasi_filetype_t) != 0 {
nix_sflags.insert(SFlag::S_IFREG);
}
if sflags & (__WASI_FILETYPE_SYMBOLIC_LINK as __wasi_filetype_t) != 0 {
nix_sflags.insert(SFlag::S_IFLNK);
}
nix_sflags
}
pub fn filestat_from_nix(filestat: nix::sys::stat::FileStat) -> __wasi_filestat_t {
let filetype = nix::sys::stat::SFlag::from_bits_truncate(filestat.st_mode);
__wasi_filestat_t {
st_dev: filestat.st_dev as __wasi_device_t,
st_ino: filestat.st_ino as __wasi_inode_t,
st_nlink: filestat.st_nlink as __wasi_linkcount_t,
st_size: filestat.st_size as __wasi_filesize_t,
st_atim: filestat.st_atime as __wasi_timestamp_t * 1_000_000_000,
st_ctim: filestat.st_ctime as __wasi_timestamp_t * 1_000_000_000,
st_mtim: filestat.st_mtime as __wasi_timestamp_t * 1_000_000_000,
st_filetype: filetype_from_nix(filetype),
}
}
// Rights sets from wasmtime-wasi sandboxed system primitives. Transcribed because bindgen can't
// parse the #defines.
pub const RIGHTS_ALL: __wasi_rights_t = (__WASI_RIGHT_FD_DATASYNC
| __WASI_RIGHT_FD_READ
| __WASI_RIGHT_FD_SEEK
| __WASI_RIGHT_FD_FDSTAT_SET_FLAGS
| __WASI_RIGHT_FD_SYNC
| __WASI_RIGHT_FD_TELL
| __WASI_RIGHT_FD_WRITE
| __WASI_RIGHT_FD_ADVISE
| __WASI_RIGHT_FD_ALLOCATE
| __WASI_RIGHT_PATH_CREATE_DIRECTORY
| __WASI_RIGHT_PATH_CREATE_FILE
| __WASI_RIGHT_PATH_LINK_SOURCE
| __WASI_RIGHT_PATH_LINK_TARGET
| __WASI_RIGHT_PATH_OPEN
| __WASI_RIGHT_FD_READDIR
| __WASI_RIGHT_PATH_READLINK
| __WASI_RIGHT_PATH_RENAME_SOURCE
| __WASI_RIGHT_PATH_RENAME_TARGET
| __WASI_RIGHT_PATH_FILESTAT_GET
| __WASI_RIGHT_PATH_FILESTAT_SET_SIZE
| __WASI_RIGHT_PATH_FILESTAT_SET_TIMES
| __WASI_RIGHT_FD_FILESTAT_GET
| __WASI_RIGHT_FD_FILESTAT_SET_SIZE
| __WASI_RIGHT_FD_FILESTAT_SET_TIMES
| __WASI_RIGHT_PATH_SYMLINK
| __WASI_RIGHT_PATH_UNLINK_FILE
| __WASI_RIGHT_PATH_REMOVE_DIRECTORY
| __WASI_RIGHT_POLL_FD_READWRITE
| __WASI_RIGHT_SOCK_SHUTDOWN) as __wasi_rights_t;
// Block and character device interaction is outside the scope of
// CloudABI. Simply allow everything.
pub const RIGHTS_BLOCK_DEVICE_BASE: __wasi_rights_t = RIGHTS_ALL;
pub const RIGHTS_BLOCK_DEVICE_INHERITING: __wasi_rights_t = RIGHTS_ALL;
pub const RIGHTS_CHARACTER_DEVICE_BASE: __wasi_rights_t = RIGHTS_ALL;
pub const RIGHTS_CHARACTER_DEVICE_INHERITING: __wasi_rights_t = RIGHTS_ALL;
// Only allow directory operations on directories. Directories can only
// yield file descriptors to other directories and files.
pub const RIGHTS_DIRECTORY_BASE: __wasi_rights_t = (__WASI_RIGHT_FD_FDSTAT_SET_FLAGS
| __WASI_RIGHT_FD_SYNC
| __WASI_RIGHT_FD_ADVISE
| __WASI_RIGHT_PATH_CREATE_DIRECTORY
| __WASI_RIGHT_PATH_CREATE_FILE
| __WASI_RIGHT_PATH_LINK_SOURCE
| __WASI_RIGHT_PATH_LINK_TARGET
| __WASI_RIGHT_PATH_OPEN
| __WASI_RIGHT_FD_READDIR
| __WASI_RIGHT_PATH_READLINK
| __WASI_RIGHT_PATH_RENAME_SOURCE
| __WASI_RIGHT_PATH_RENAME_TARGET
| __WASI_RIGHT_PATH_FILESTAT_GET
| __WASI_RIGHT_PATH_FILESTAT_SET_SIZE
| __WASI_RIGHT_PATH_FILESTAT_SET_TIMES
| __WASI_RIGHT_FD_FILESTAT_GET
| __WASI_RIGHT_FD_FILESTAT_SET_TIMES
| __WASI_RIGHT_PATH_SYMLINK
| __WASI_RIGHT_PATH_UNLINK_FILE
| __WASI_RIGHT_PATH_REMOVE_DIRECTORY
| __WASI_RIGHT_POLL_FD_READWRITE)
as __wasi_rights_t;
pub const RIGHTS_DIRECTORY_INHERITING: __wasi_rights_t =
(RIGHTS_DIRECTORY_BASE | RIGHTS_REGULAR_FILE_BASE);
// Operations that apply to regular files.
pub const RIGHTS_REGULAR_FILE_BASE: __wasi_rights_t = (__WASI_RIGHT_FD_DATASYNC
| __WASI_RIGHT_FD_READ
| __WASI_RIGHT_FD_SEEK
| __WASI_RIGHT_FD_FDSTAT_SET_FLAGS
| __WASI_RIGHT_FD_SYNC
| __WASI_RIGHT_FD_TELL
| __WASI_RIGHT_FD_WRITE
| __WASI_RIGHT_FD_ADVISE
| __WASI_RIGHT_FD_ALLOCATE
| __WASI_RIGHT_FD_FILESTAT_GET
| __WASI_RIGHT_FD_FILESTAT_SET_SIZE
| __WASI_RIGHT_FD_FILESTAT_SET_TIMES
| __WASI_RIGHT_POLL_FD_READWRITE)
as __wasi_rights_t;
pub const RIGHTS_REGULAR_FILE_INHERITING: __wasi_rights_t = 0;
// Operations that apply to shared memory objects.
pub const RIGHTS_SHARED_MEMORY_BASE: __wasi_rights_t = (__WASI_RIGHT_FD_READ
| __WASI_RIGHT_FD_WRITE
| __WASI_RIGHT_FD_FILESTAT_GET
| __WASI_RIGHT_FD_FILESTAT_SET_SIZE)
as __wasi_rights_t;
pub const RIGHTS_SHARED_MEMORY_INHERITING: __wasi_rights_t = 0;
// Operations that apply to sockets and socket pairs.
pub const RIGHTS_SOCKET_BASE: __wasi_rights_t = (__WASI_RIGHT_FD_READ
| __WASI_RIGHT_FD_FDSTAT_SET_FLAGS
| __WASI_RIGHT_FD_WRITE
| __WASI_RIGHT_FD_FILESTAT_GET
| __WASI_RIGHT_POLL_FD_READWRITE
| __WASI_RIGHT_SOCK_SHUTDOWN)
as __wasi_rights_t;
pub const RIGHTS_SOCKET_INHERITING: __wasi_rights_t = RIGHTS_ALL;
// Operations that apply to TTYs.
pub const RIGHTS_TTY_BASE: __wasi_rights_t = (__WASI_RIGHT_FD_READ
| __WASI_RIGHT_FD_FDSTAT_SET_FLAGS
| __WASI_RIGHT_FD_WRITE
| __WASI_RIGHT_FD_FILESTAT_GET
| __WASI_RIGHT_POLL_FD_READWRITE)
as __wasi_rights_t;
pub const RIGHTS_TTY_INHERITING: __wasi_rights_t = 0;
|