-
Notifications
You must be signed in to change notification settings - Fork 758
/
wasix_manual.rs
495 lines (457 loc) 路 15 KB
/
wasix_manual.rs
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use std::mem::MaybeUninit;
use wasmer::{FromToNativeWasmType, MemorySize, ValueType};
use super::{
Errno, ErrnoSignal, EventFdReadwrite, Eventtype, Fd, JoinStatusType, Signal,
Snapshot0SubscriptionClock, SubscriptionClock, SubscriptionFsReadwrite, Userdata,
};
/// Thread local key
pub type TlKey = u32;
/// Thread local value
pub type TlVal = u64;
/// Thread local user data (associated with the value)
pub type TlUser = u64;
/// Long size used by checkpoints
pub type Longsize = u64;
/// The contents of a `subscription`, snapshot0 version.
#[repr(C)]
#[derive(Clone, Copy)]
pub union Snapshot0SubscriptionUnion {
pub clock: Snapshot0SubscriptionClock,
pub fd_readwrite: SubscriptionFsReadwrite,
}
/// The contents of a `subscription`.
#[repr(C)]
#[derive(Clone, Copy)]
pub union SubscriptionUnion {
pub clock: SubscriptionClock,
pub fd_readwrite: SubscriptionFsReadwrite,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Snapshot0Subscription {
pub userdata: Userdata,
pub type_: Eventtype,
pub u: Snapshot0SubscriptionUnion,
}
impl core::fmt::Debug for Snapshot0Subscription {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Snapshot0Subscription")
.field("userdata", &self.userdata)
.field("type", &self.type_)
.finish()
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Subscription {
pub userdata: Userdata,
pub type_: Eventtype,
pub data: SubscriptionUnion,
}
impl core::fmt::Debug for Subscription {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Subscription")
.field("userdata", &self.userdata)
.field("type", &self.type_)
.finish()
}
}
impl From<Snapshot0Subscription> for Subscription {
fn from(other: Snapshot0Subscription) -> Self {
Self {
userdata: other.userdata,
type_: other.type_,
data: match other.type_ {
Eventtype::Clock => SubscriptionUnion {
clock: unsafe {
SubscriptionClock {
clock_id: other.u.clock.id.into(),
timeout: other.u.clock.timeout,
precision: other.u.clock.precision,
flags: other.u.clock.flags,
}
},
},
Eventtype::FdRead => SubscriptionUnion {
fd_readwrite: unsafe { other.u.fd_readwrite },
},
Eventtype::FdWrite => SubscriptionUnion {
fd_readwrite: unsafe { other.u.fd_readwrite },
},
Eventtype::Unknown => SubscriptionUnion {
fd_readwrite: SubscriptionFsReadwrite {
file_descriptor: u32::MAX,
},
},
},
}
}
}
/// The contents of an `event`.
#[repr(C)]
#[derive(Clone, Copy)]
pub union EventUnion {
pub clock: u8,
pub fd_readwrite: EventFdReadwrite,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct StackSnapshot {
pub user: u64,
pub hash: u128,
}
/// An event that occurred.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Event {
/// User-provided value that got attached to `subscription::userdata`.
pub userdata: Userdata,
/// If non-zero, an error that occurred while processing the subscription request.
pub error: Errno,
/// Type of event that was triggered
pub type_: Eventtype,
/// The type of the event that occurred, and the contents of the event
pub u: EventUnion,
}
impl core::fmt::Debug for Event {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Event")
.field("userdata", &self.userdata)
.field("error", &self.error)
.field("type", &self.type_)
.finish()
}
}
/// An event that occurred.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Snapshot0Event {
/// User-provided value that got attached to `subscription::userdata`.
pub userdata: Userdata,
/// If non-zero, an error that occurred while processing the subscription request.
pub error: Errno,
/// The type of event that occured
pub type_: Eventtype,
/// The contents of the event, if it is an `eventtype::fd_read` or
/// `eventtype::fd_write`. `eventtype::clock` events ignore this field.
pub fd_readwrite: EventFdReadwrite,
}
impl core::fmt::Debug for Snapshot0Event {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Snapshot0Event")
.field("userdata", &self.userdata)
.field("error", &self.error)
.field("type", &self.type_)
.field("fd-readwrite", &self.fd_readwrite)
.finish()
}
}
unsafe impl ValueType for Snapshot0Subscription {
#[inline]
fn zero_padding_bytes(&self, _bytes: &mut [MaybeUninit<u8>]) {}
}
unsafe impl ValueType for Snapshot0Event {
#[inline]
fn zero_padding_bytes(&self, _bytes: &mut [MaybeUninit<u8>]) {}
}
unsafe impl ValueType for Subscription {
#[inline]
fn zero_padding_bytes(&self, _bytes: &mut [MaybeUninit<u8>]) {}
}
unsafe impl ValueType for Event {
#[inline]
fn zero_padding_bytes(&self, _bytes: &mut [MaybeUninit<u8>]) {}
}
unsafe impl ValueType for StackSnapshot {
#[inline]
fn zero_padding_bytes(&self, _bytes: &mut [MaybeUninit<u8>]) {}
}
#[repr(C)]
#[derive(Clone, Copy)]
pub union JoinStatusUnion {
pub nothing: u8,
pub exit_normal: Errno,
pub exit_signal: ErrnoSignal,
pub stopped: Signal,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct JoinStatus {
pub tag: JoinStatusType,
pub u: JoinStatusUnion,
}
impl core::fmt::Debug for JoinStatus {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut binding = f.debug_struct("JoinStatus");
let mut f = binding.field("tag", &self.tag);
f = unsafe {
match self.tag {
JoinStatusType::Nothing => f.field("nothing", &self.u.nothing),
JoinStatusType::ExitNormal => f.field("exit_normal", &self.u.exit_normal),
JoinStatusType::ExitSignal => f.field("exit_signal", &self.u.exit_signal),
JoinStatusType::Stopped => f.field("stopped", &self.u.stopped),
}
};
f.finish()
}
}
unsafe impl ValueType for JoinStatus {
#[inline]
fn zero_padding_bytes(&self, _bytes: &mut [MaybeUninit<u8>]) {}
}
#[doc = " Represents the thread start object"]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct ThreadStart<M: MemorySize> {
pub stack_upper: M::Offset,
pub tls_base: M::Offset,
pub start_funct: M::Offset,
pub start_args: M::Offset,
pub reserved: [M::Offset; 10],
pub stack_size: M::Offset,
pub guard_size: M::Offset,
}
impl<M: MemorySize> core::fmt::Debug for ThreadStart<M> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ThreadStart")
.field("stack_upper", &self.stack_upper)
.field("tls-base", &self.tls_base)
.field("start-funct", &self.start_funct)
.field("start-args", &self.start_args)
.field("stack_size", &self.stack_size)
.field("guard_size", &self.guard_size)
.finish()
}
}
// TODO: if necessary, must be implemented in wit-bindgen
unsafe impl<M: MemorySize> ValueType for ThreadStart<M> {
#[inline]
fn zero_padding_bytes(&self, _bytes: &mut [MaybeUninit<u8>]) {}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub enum ExitCode {
Errno(Errno),
Other(i32),
}
impl ExitCode {
pub fn raw(&self) -> i32 {
match self {
ExitCode::Errno(err) => err.to_native(),
ExitCode::Other(code) => *code,
}
}
pub fn is_success(&self) -> bool {
self.raw() == 0
}
}
impl core::fmt::Debug for ExitCode {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ExitCode::Errno(a) => write!(f, "ExitCode::{}", a),
ExitCode::Other(a) => write!(f, "ExitCode::{}", a),
}
}
}
impl core::fmt::Display for ExitCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
core::fmt::Debug::fmt(&self, f)
}
}
unsafe impl wasmer::FromToNativeWasmType for ExitCode {
type Native = i32;
fn to_native(self) -> Self::Native {
self.into()
}
fn from_native(n: Self::Native) -> Self {
n.into()
}
fn is_from_store(&self, _store: &impl wasmer::AsStoreRef) -> bool {
false
}
}
impl From<Errno> for ExitCode {
fn from(val: Errno) -> Self {
Self::Errno(val)
}
}
impl From<i32> for ExitCode {
fn from(val: i32) -> Self {
let err = Errno::from_native(val);
match err {
Errno::Unknown => Self::Other(val),
err => Self::Errno(err),
}
}
}
impl From<ExitCode> for Errno {
fn from(code: ExitCode) -> Self {
match code {
ExitCode::Errno(err) => err,
ExitCode::Other(code) => Errno::from_native(code),
}
}
}
impl From<ExitCode> for i32 {
fn from(val: ExitCode) -> Self {
match val {
ExitCode::Errno(err) => err.to_native(),
ExitCode::Other(code) => code,
}
}
}
// TODO: if necessary, must be implemented in wit-bindgen
unsafe impl ValueType for EpollType {
#[inline]
fn zero_padding_bytes(&self, _bytes: &mut [MaybeUninit<u8>]) {}
}
wai_bindgen_rust::bitflags::bitflags! {
#[doc = " Epoll available event types."]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct EpollType : u32 {
#[doc = " The associated file is available for read(2) operations."]
const EPOLLIN = 1 << 0;
#[doc = " The associated file is available for write(2) operations."]
const EPOLLOUT = 1 << 1;
#[doc = " Stream socket peer closed connection, or shut down writing"]
#[doc = " half of connection. (This flag is especially useful for"]
#[doc = " writing simple code to detect peer shutdown when using"]
#[doc = " edge-triggered monitoring.)"]
const EPOLLRDHUP = 1 << 2;
#[doc = " There is an exceptional condition on the file descriptor."]
#[doc = " See the discussion of POLLPRI in poll(2)."]
const EPOLLPRI = 1 << 3;
#[doc = " Error condition happened on the associated file"]
#[doc = " descriptor. This event is also reported for the write end"]
#[doc = " of a pipe when the read end has been closed."]
const EPOLLERR = 1 << 4;
#[doc = " Hang up happened on the associated file descriptor."]
const EPOLLHUP = 1 << 5;
#[doc = " Requests edge-triggered notification for the associated"]
#[doc = " file descriptor. The default behavior for epoll is level-"]
#[doc = " triggered. See epoll(7) for more detailed information"]
#[doc = " about edge-triggered and level-triggered notification."]
const EPOLLET = 1 << 6;
#[doc = " Requests one-shot notification for the associated file"]
#[doc = " descriptor. This means that after an event notified for"]
#[doc = " the file descriptor by epoll_wait(2), the file descriptor"]
#[doc = " is disabled in the interest list and no other events will"]
#[doc = " be reported by the epoll interface. The user must call"]
#[doc = " epoll_ctl() with EPOLL_CTL_MOD to rearm the file"]
#[doc = " descriptor with a new event mask."]
const EPOLLONESHOT = 1 << 7;
}
}
#[doc = " Epoll operation."]
#[repr(u32)]
#[derive(Clone, Copy, PartialEq, Eq, num_enum :: TryFromPrimitive, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub enum EpollCtl {
#[doc = " Add an entry to the interest list of the epoll file descriptor, epfd."]
Add,
#[doc = " Change the settings associated with fd in the interest list to the new settings specified in event."]
Mod,
#[doc = " Remove (deregister) the target file descriptor fd from the interest list."]
Del,
#[doc = " Unknown."]
Unknown,
}
impl core::fmt::Debug for EpollCtl {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
EpollCtl::Add => f.debug_tuple("EPOLL_CTL_ADD").finish(),
EpollCtl::Mod => f.debug_tuple("EPOLL_CTL_MOD").finish(),
EpollCtl::Del => f.debug_tuple("EPOLL_CTL_DEL").finish(),
EpollCtl::Unknown => f.debug_tuple("Unknown").finish(),
}
}
}
// TODO: if necessary, must be implemented in wit-bindgen
unsafe impl ValueType for EpollCtl {
#[inline]
fn zero_padding_bytes(&self, _bytes: &mut [MaybeUninit<u8>]) {}
}
unsafe impl wasmer::FromToNativeWasmType for EpollCtl {
type Native = i32;
fn to_native(self) -> Self::Native {
self as i32
}
fn from_native(n: Self::Native) -> Self {
match n {
0 => Self::Add,
1 => Self::Mod,
2 => Self::Del,
q => {
tracing::debug!("could not serialize number {q} to enum EpollCtl");
Self::Unknown
}
}
}
fn is_from_store(&self, _store: &impl wasmer::AsStoreRef) -> bool {
false
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct EpollEventCtl {
pub events: EpollType,
pub ptr: u64,
pub fd: Fd,
pub data1: u32,
pub data2: u64,
}
/// An event that can be triggered
#[repr(C)]
#[derive(Copy, Clone)]
pub struct EpollData<M: MemorySize> {
/// Pointer to the data
pub ptr: M::Offset,
/// File descriptor
pub fd: Fd,
/// Associated user data
pub data1: u32,
/// Associated user data
pub data2: u64,
}
impl<M> core::fmt::Debug for EpollData<M>
where
M: MemorySize,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("EpollData")
.field("ptr", &self.ptr)
.field("fd", &self.fd)
.field("data1", &self.data1)
.field("data2", &self.data2)
.finish()
}
}
/// An event that can be triggered
#[repr(C)]
#[derive(Copy, Clone)]
pub struct EpollEvent<M: MemorySize> {
/// Pointer to the data
pub events: EpollType,
/// File descriptor
pub data: EpollData<M>,
}
impl<M> core::fmt::Debug for EpollEvent<M>
where
M: MemorySize,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("EpollData")
.field("events", &self.events)
.field("data", &self.data)
.finish()
}
}
unsafe impl<M> ValueType for EpollEvent<M>
where
M: MemorySize,
{
#[inline]
fn zero_padding_bytes(&self, _bytes: &mut [MaybeUninit<u8>]) {}
}