forked from bytecodealliance/wasm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanonicals.rs
395 lines (388 loc) · 15.2 KB
/
canonicals.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
use crate::limits::MAX_WASM_CANONICAL_OPTIONS;
use crate::prelude::*;
use crate::{BinaryReader, ComponentValType, FromReader, Result, SectionLimited};
/// Represents options for component functions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CanonicalOption {
/// The string types in the function signature are UTF-8 encoded.
UTF8,
/// The string types in the function signature are UTF-16 encoded.
UTF16,
/// The string types in the function signature are compact UTF-16 encoded.
CompactUTF16,
/// The memory to use if the lifting or lowering of a function requires memory access.
///
/// The value is an index to a core memory.
Memory(u32),
/// The realloc function to use if the lifting or lowering of a function requires memory
/// allocation.
///
/// The value is an index to a core function of type `(func (param i32 i32 i32 i32) (result i32))`.
Realloc(u32),
/// The post-return function to use if the lifting of a function requires
/// cleanup after the function returns.
PostReturn(u32),
/// Indicates that specified function should be lifted or lowered using the `async` ABI.
Async,
/// The function to use if the async lifting of a function should receive task/stream/future progress events
/// using a callback.
Callback(u32),
}
/// Represents a canonical function in a WebAssembly component.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum CanonicalFunction {
/// The function lifts a core WebAssembly function to the canonical ABI.
Lift {
/// The index of the core WebAssembly function to lift.
core_func_index: u32,
/// The index of the lifted function's type.
type_index: u32,
/// The canonical options for the function.
options: Box<[CanonicalOption]>,
},
/// The function lowers a canonical ABI function to a core WebAssembly function.
Lower {
/// The index of the function to lower.
func_index: u32,
/// The canonical options for the function.
options: Box<[CanonicalOption]>,
},
/// A function which creates a new owned handle to a resource.
ResourceNew {
/// The type index of the resource that's being created.
resource: u32,
},
/// A function which is used to drop resource handles of the specified type.
ResourceDrop {
/// The type index of the resource that's being dropped.
resource: u32,
},
/// Same as `ResourceDrop`, but implements the `async` ABI.
ResourceDropAsync {
/// The type index of the resource that's being dropped.
resource: u32,
},
/// A function which returns the underlying i32-based representation of the
/// specified resource.
ResourceRep {
/// The type index of the resource that's being accessed.
resource: u32,
},
/// A function which spawns a new thread by invoking the shared function.
ThreadSpawnRef {
/// The index of the function type to spawn.
func_ty_index: u32,
},
/// A function which spawns a new thread by invoking the shared function
/// passed as an index into a `funcref` table.
ThreadSpawnIndirect {
/// The index of the function type to spawn.
func_ty_index: u32,
/// The index of the table to use for the indirect spawn.
table_index: u32,
},
/// A function which returns the number of threads that can be expected to
/// execute concurrently
ThreadAvailableParallelism,
/// A function which tells the host to enable or disable backpressure for
/// the caller's instance.
BackpressureSet,
/// A function which returns a result to the caller of a lifted export
/// function. This allows the callee to continue executing after returning
/// a result.
TaskReturn {
/// The result type, if any.
result: Option<ComponentValType>,
/// The canonical options for the function.
options: Box<[CanonicalOption]>,
},
/// A `context.get` intrinsic for the `i`th slot of task-local storage.
ContextGet(u32),
/// A `context.set` intrinsic for the `i`th slot of task-local storage.
ContextSet(u32),
/// A function which yields control to the host so that other tasks are able
/// to make progress, if any.
Yield {
/// If `true`, indicates the caller instance maybe reentered.
async_: bool,
},
/// A function to drop a specified task which has completed.
SubtaskDrop,
/// A function to create a new `stream` handle of the specified type.
StreamNew {
/// The `stream` type to instantiate.
ty: u32,
},
/// A function to read from a `stream` of the specified type.
StreamRead {
/// The `stream` type to expect.
ty: u32,
/// Any options (e.g. string encoding) to use when storing values to
/// memory.
options: Box<[CanonicalOption]>,
},
/// A function to write to a `stream` of the specified type.
StreamWrite {
/// The `stream` type to expect.
ty: u32,
/// Any options (e.g. string encoding) to use when loading values from
/// memory.
options: Box<[CanonicalOption]>,
},
/// A function to cancel an in-progress read from a `stream` of the
/// specified type.
StreamCancelRead {
/// The `stream` type to expect.
ty: u32,
/// If `false`, block until cancellation completes rather than return
/// `BLOCKED`.
async_: bool,
},
/// A function to cancel an in-progress write to a `stream` of the specified
/// type.
StreamCancelWrite {
/// The `stream` type to expect.
ty: u32,
/// If `false`, block until cancellation completes rather than return
/// `BLOCKED`.
async_: bool,
},
/// A function to close the readable end of a `stream` of the specified
/// type.
StreamCloseReadable {
/// The `stream` type to expect.
ty: u32,
},
/// A function to close the writable end of a `stream` of the specified
/// type.
StreamCloseWritable {
/// The `stream` type to expect.
ty: u32,
},
/// A function to create a new `future` handle of the specified type.
FutureNew {
/// The `future` type to instantiate.
ty: u32,
},
/// A function to read from a `future` of the specified type.
FutureRead {
/// The `future` type to expect.
ty: u32,
/// Any options (e.g. string encoding) to use when storing values to
/// memory.
options: Box<[CanonicalOption]>,
},
/// A function to write to a `future` of the specified type.
FutureWrite {
/// The `future` type to expect.
ty: u32,
/// Any options (e.g. string encoding) to use when loading values from
/// memory.
options: Box<[CanonicalOption]>,
},
/// A function to cancel an in-progress read from a `future` of the
/// specified type.
FutureCancelRead {
/// The `future` type to expect.
ty: u32,
/// If `false`, block until cancellation completes rather than return
/// `BLOCKED`.
async_: bool,
},
/// A function to cancel an in-progress write to a `future` of the specified
/// type.
FutureCancelWrite {
/// The `future` type to expect.
ty: u32,
/// If `false`, block until cancellation completes rather than return
/// `BLOCKED`.
async_: bool,
},
/// A function to close the readable end of a `future` of the specified
/// type.
FutureCloseReadable {
/// The `future` type to expect.
ty: u32,
},
/// A function to close the writable end of a `future` of the specified
/// type.
FutureCloseWritable {
/// The `future` type to expect.
ty: u32,
},
/// A function to create a new `error-context` with a specified debug
/// message.
ErrorContextNew {
/// String encoding, memory, etc. to use when loading debug message.
options: Box<[CanonicalOption]>,
},
/// A function to get the debug message for a specified `error-context`.
///
/// Note that the debug message might not necessarily match what was passed
/// to `error.new`.
ErrorContextDebugMessage {
/// String encoding, memory, etc. to use when storing debug message.
options: Box<[CanonicalOption]>,
},
/// A function to drop a specified `error-context`.
ErrorContextDrop,
/// A function to create a new `waitable-set`.
WaitableSetNew,
/// A function to block on the next item within a `waitable-set`.
WaitableSetWait {
/// Whether or not the guest can be reentered while calling this
/// function.
async_: bool,
/// Which memory the results of this operation are stored in.
memory: u32,
},
/// A function to check if any items are ready within a `waitable-set`.
WaitableSetPoll {
/// Whether or not the guest can be reentered while calling this
/// function.
async_: bool,
/// Which memory the results of this operation are stored in.
memory: u32,
},
/// A function to drop a `waitable-set`.
WaitableSetDrop,
/// A function to add an item to a `waitable-set`.
WaitableJoin,
}
/// A reader for the canonical section of a WebAssembly component.
pub type ComponentCanonicalSectionReader<'a> = SectionLimited<'a, CanonicalFunction>;
impl<'a> FromReader<'a> for CanonicalFunction {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<CanonicalFunction> {
Ok(match reader.read_u8()? {
0x00 => match reader.read_u8()? {
0x00 => CanonicalFunction::Lift {
core_func_index: reader.read_var_u32()?,
options: read_opts(reader)?,
type_index: reader.read_var_u32()?,
},
x => return reader.invalid_leading_byte(x, "canonical function lift"),
},
0x01 => match reader.read_u8()? {
0x00 => CanonicalFunction::Lower {
func_index: reader.read_var_u32()?,
options: read_opts(reader)?,
},
x => return reader.invalid_leading_byte(x, "canonical function lower"),
},
0x02 => CanonicalFunction::ResourceNew {
resource: reader.read()?,
},
0x03 => CanonicalFunction::ResourceDrop {
resource: reader.read()?,
},
0x07 => CanonicalFunction::ResourceDropAsync {
resource: reader.read()?,
},
0x04 => CanonicalFunction::ResourceRep {
resource: reader.read()?,
},
0x08 => CanonicalFunction::BackpressureSet,
0x09 => CanonicalFunction::TaskReturn {
result: crate::read_resultlist(reader)?,
options: read_opts(reader)?,
},
0x0a => match reader.read_u8()? {
0x7f => CanonicalFunction::ContextGet(reader.read_var_u32()?),
x => return reader.invalid_leading_byte(x, "context.get intrinsic type"),
},
0x0b => match reader.read_u8()? {
0x7f => CanonicalFunction::ContextSet(reader.read_var_u32()?),
x => return reader.invalid_leading_byte(x, "context.set intrinsic type"),
},
0x0c => CanonicalFunction::Yield {
async_: reader.read()?,
},
0x0d => CanonicalFunction::SubtaskDrop,
0x0e => CanonicalFunction::StreamNew { ty: reader.read()? },
0x0f => CanonicalFunction::StreamRead {
ty: reader.read()?,
options: read_opts(reader)?,
},
0x10 => CanonicalFunction::StreamWrite {
ty: reader.read()?,
options: read_opts(reader)?,
},
0x11 => CanonicalFunction::StreamCancelRead {
ty: reader.read()?,
async_: reader.read()?,
},
0x12 => CanonicalFunction::StreamCancelWrite {
ty: reader.read()?,
async_: reader.read()?,
},
0x13 => CanonicalFunction::StreamCloseReadable { ty: reader.read()? },
0x14 => CanonicalFunction::StreamCloseWritable { ty: reader.read()? },
0x15 => CanonicalFunction::FutureNew { ty: reader.read()? },
0x16 => CanonicalFunction::FutureRead {
ty: reader.read()?,
options: read_opts(reader)?,
},
0x17 => CanonicalFunction::FutureWrite {
ty: reader.read()?,
options: read_opts(reader)?,
},
0x18 => CanonicalFunction::FutureCancelRead {
ty: reader.read()?,
async_: reader.read()?,
},
0x19 => CanonicalFunction::FutureCancelWrite {
ty: reader.read()?,
async_: reader.read()?,
},
0x1a => CanonicalFunction::FutureCloseReadable { ty: reader.read()? },
0x1b => CanonicalFunction::FutureCloseWritable { ty: reader.read()? },
0x1c => CanonicalFunction::ErrorContextNew {
options: read_opts(reader)?,
},
0x1d => CanonicalFunction::ErrorContextDebugMessage {
options: read_opts(reader)?,
},
0x1e => CanonicalFunction::ErrorContextDrop,
0x1f => CanonicalFunction::WaitableSetNew,
0x20 => CanonicalFunction::WaitableSetWait {
async_: reader.read()?,
memory: reader.read()?,
},
0x21 => CanonicalFunction::WaitableSetPoll {
async_: reader.read()?,
memory: reader.read()?,
},
0x22 => CanonicalFunction::WaitableSetDrop,
0x23 => CanonicalFunction::WaitableJoin,
0x40 => CanonicalFunction::ThreadSpawnRef {
func_ty_index: reader.read()?,
},
0x41 => CanonicalFunction::ThreadSpawnIndirect {
func_ty_index: reader.read()?,
table_index: reader.read()?,
},
0x42 => CanonicalFunction::ThreadAvailableParallelism,
x => return reader.invalid_leading_byte(x, "canonical function"),
})
}
}
fn read_opts(reader: &mut BinaryReader<'_>) -> Result<Box<[CanonicalOption]>> {
reader
.read_iter(MAX_WASM_CANONICAL_OPTIONS, "canonical options")?
.collect::<Result<_>>()
}
impl<'a> FromReader<'a> for CanonicalOption {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
Ok(match reader.read_u8()? {
0x00 => CanonicalOption::UTF8,
0x01 => CanonicalOption::UTF16,
0x02 => CanonicalOption::CompactUTF16,
0x03 => CanonicalOption::Memory(reader.read_var_u32()?),
0x04 => CanonicalOption::Realloc(reader.read_var_u32()?),
0x05 => CanonicalOption::PostReturn(reader.read_var_u32()?),
0x06 => CanonicalOption::Async,
0x07 => CanonicalOption::Callback(reader.read_var_u32()?),
x => return reader.invalid_leading_byte(x, "canonical option"),
})
}
}