forked from google/wasefire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.rs
1580 lines (1451 loc) · 56 KB
/
exec.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO: Some toctou could be used instead of panic.
use alloc::vec;
use alloc::vec::Vec;
use crate::error::*;
use crate::module::*;
use crate::side_table::*;
use crate::syntax::*;
use crate::toctou::*;
use crate::util::*;
use crate::*;
pub const MEMORY_ALIGN: usize = 16;
/// Runtime values.
// TODO: Introduce untyped values? (to save the tag)
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Val {
I32(u32),
I64(u64),
#[cfg(feature = "float-types")]
F32(u32),
#[cfg(feature = "float-types")]
F64(u64),
#[cfg(feature = "vector-types")]
V128(u128),
Null(RefType),
Ref(Ptr),
RefExtern(usize),
}
static STORE_ID: id::UniqueId = id::UniqueId::new();
/// Runtime store.
// We cannot GC by design. Vectors can only grow.
#[derive(Debug)]
pub struct Store<'m> {
id: usize,
insts: Vec<Instance<'m>>,
// TODO: This should be an Linker struct that can be shared between stores. The FuncType can be
// reconstructed on demand (only counts can be stored).
funcs: Vec<(HostName<'m>, FuncType<'m>)>,
// When present, contains a module name and a length. In that case, any unresolved imported
// function for that module name is appended to funcs (one per type, so the function name is
// the name of the first unresolved function of that type). The length of resolvable host
// functions in `funcs` is stored to limit normal linking to that part.
func_default: Option<(&'m str, usize)>,
threads: Vec<Continuation<'m>>,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct HostName<'m> {
module: &'m str,
name: &'m str,
}
/// Identifies a store.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct StoreId(usize);
/// Identifies an instance within a store.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct InstId {
store_id: usize,
inst_id: usize,
}
impl InstId {
/// Returns the identifier of the store of this instance.
pub fn store_id(self) -> StoreId {
StoreId(self.store_id)
}
}
/// Store wrapper when calling into the host.
#[derive(Debug)]
// Invariant that there is at least one thread.
pub struct Call<'a, 'm> {
store: &'a mut Store<'m>,
}
impl Default for Store<'_> {
fn default() -> Self {
Self {
id: STORE_ID.next(),
insts: vec![],
funcs: vec![],
func_default: None,
threads: vec![],
}
}
}
impl<'m> Store<'m> {
/// Returns the identifier of this store.
pub fn id(&self) -> StoreId {
StoreId(self.id)
}
/// Instantiates a valid module in this store.
///
/// The memory is not dynamically allocated and must thus be provided. It is not necessary for
/// the memory length to be a multiple of 64kB. Execution will trap if the module tries to
/// access part of the memory that does not exist.
pub fn instantiate(
&mut self, module: Module<'m>, memory: &'m mut [u8],
) -> Result<InstId, Error> {
let inst_id = self.insts.len();
self.insts.push(Instance::default());
self.last_inst().module = module;
for import in self.last_inst().module.imports() {
let type_ = import.type_(&self.last_inst().module);
let id = self.resolve(&import, type_)?;
match import.desc {
ImportDesc::Func(_) => self.last_inst().funcs.ext.push(id),
ImportDesc::Table(_) => self.last_inst().tables.ext.push(id),
ImportDesc::Mem(_) => self.last_inst().mems.ext.push(id),
ImportDesc::Global(_) => self.last_inst().globals.ext.push(id),
}
}
if let Some(mut parser) = self.last_inst().module.section(SectionId::Table) {
for _ in 0 .. parser.parse_vec().into_ok() {
(self.last_inst().tables.int).push(Table::new(parser.parse_tabletype().into_ok()));
}
}
if let Some(mut parser) = self.last_inst().module.section(SectionId::Memory) {
match parser.parse_vec().into_ok() {
0 => (),
1 => {
let limits = parser.parse_memtype().into_ok();
self.last_inst().mems.int.init(memory, limits)?;
}
_ => unimplemented!(),
}
}
if let Some(mut parser) = self.last_inst().module.section(SectionId::Global) {
for _ in 0 .. parser.parse_vec().into_ok() {
parser.parse_globaltype().into_ok();
let value = Thread::const_expr(self, inst_id, &mut parser);
self.last_inst().globals.int.push(Global::new(value));
}
}
if let Some(mut parser) = self.last_inst().module.section(SectionId::Element) {
for _ in 0 .. parser.parse_vec().into_ok() {
// TODO: This is inefficient because we only need init for active segments.
let mut elem = ComputeElem::new(self, inst_id);
parser.parse_elem(&mut elem).into_ok();
let ComputeElem { mode, init, .. } = elem;
let drop = match mode {
ElemMode::Passive => false,
ElemMode::Active { table, offset } => {
let n = init.len();
let table = self.table(inst_id, table);
table_init(offset, 0, n, table, &init)?;
true
}
ElemMode::Declarative => true,
};
self.last_inst().elems.push(drop);
}
}
if let Some(mut parser) = self.last_inst().module.section(SectionId::Data) {
for _ in 0 .. parser.parse_vec().into_ok() {
let mut data = ComputeData::new(self, inst_id);
parser.parse_data(&mut data).into_ok();
let ComputeData { mode, init, .. } = data;
let drop = match mode {
DataMode::Passive => false,
DataMode::Active { memory, offset } => {
let n = init.len();
let memory = self.mem(inst_id, memory);
memory_init(offset, 0, n, memory, init)?;
true
}
};
self.last_inst().datas.push(drop);
}
}
if let Some(mut parser) = self.last_inst().module.section(SectionId::Start) {
let x = parser.parse_funcidx().into_ok();
let ptr = self.func_ptr(inst_id, x);
let inst_id = ptr.instance().unwrap_wasm();
let (mut parser, side_table) = self.insts[inst_id].module.func(ptr.index());
let mut locals = Vec::new();
append_locals(&mut parser, &mut locals);
let thread = Thread::new(parser, Frame::new(inst_id, 0, &[], locals, side_table, 0));
let result = thread.run(self)?;
assert!(matches!(result, RunResult::Done(x) if x.is_empty()));
}
Ok(InstId { store_id: self.id, inst_id })
}
/// Invokes a function in an instance provided its name.
///
/// If a function was already running, it will resume once the function being called terminates.
/// In other words, execution satisfies a stack property. Without this, the stack of the module
/// may be corrupted.
pub fn invoke<'a>(
&'a mut self, inst: InstId, name: &str, args: Vec<Val>,
) -> Result<RunResult<'a, 'm>, Error> {
let inst_id = self.inst_id(inst)?;
let inst = &self.insts[inst_id];
let ptr = match inst.module.export(name).ok_or_else(not_found)? {
ExportDesc::Func(x) => inst.funcs.ptr(inst_id, x),
_ => return Err(Error::Invalid),
};
let inst_id = ptr.instance().unwrap_wasm();
let inst = &self.insts[inst_id];
let x = ptr.index();
let t = inst.module.func_type(x);
let (mut parser, side_table) = inst.module.func(x);
check_types(&t.params, &args)?;
let mut locals = args;
append_locals(&mut parser, &mut locals);
let frame = Frame::new(inst_id, t.results.len(), &[], locals, side_table, 0);
Thread::new(parser, frame).run(self)
}
/// Returns the value of a global of an instance.
pub fn get_global(&mut self, inst: InstId, name: &str) -> Result<Val, Error> {
let inst_id = self.inst_id(inst)?;
let inst = &self.insts[inst_id];
let ptr = match inst.module.export(name).ok_or_else(not_found)? {
ExportDesc::Global(x) => inst.globals.ptr(inst_id, x),
_ => return Err(Error::Invalid),
};
let inst_id = ptr.instance().unwrap_wasm();
let x = ptr.index() as usize;
Ok(self.insts[inst_id].globals.int[x].value)
}
/// Sets the name of an instance.
pub fn set_name(&mut self, inst: InstId, name: &'m str) -> Result<(), Error> {
let inst_id = self.inst_id(inst)?;
self.insts[inst_id].name = name;
Ok(())
}
/// Links a host function provided its signature.
///
/// This is a convenient wrapper around [`Self::link_func_custom()`] for functions which
/// parameter and return types are only `i32`. The `params` and `results` parameters describe
/// how many `i32` are taken as parameters and returned as results respectively.
pub fn link_func(
&mut self, module: &'m str, name: &'m str, params: usize, results: usize,
) -> Result<(), Error> {
static TYPES: &[ValType] = &[ValType::I32; 8];
check(params <= TYPES.len() && results <= TYPES.len())?;
let type_ = FuncType { params: TYPES[.. params].into(), results: TYPES[.. results].into() };
self.link_func_custom(module, name, type_)
}
/// Enables linking unresolved imported function for the given module.
///
/// For each unresolved imported function type that returns exactly one `i32`, a fake host
/// function is created (as if `link_func` was used). As such, out-of-bound indices with respect
/// to real calls to [`Self::link_func()`] represent such fake host functions. Callers must thus
/// expect and support out-of-bound indices returned by [`Call::index()`].
///
/// This function can be called at most once, and once called `link_func` cannot be called.
pub fn link_func_default(&mut self, module: &'m str) -> Result<(), Error> {
check(self.func_default.is_none())?;
self.func_default = Some((module, self.funcs.len()));
Ok(())
}
/// Links a host function provided its signature.
///
/// Note that the order in which functions are linked defines their index. The first linked
/// function has index 0, the second has index 1, etc. This index is used when a module calls in
/// the host to identify the function.
pub fn link_func_custom(
&mut self, module: &'m str, name: &'m str, type_: FuncType<'m>,
) -> Result<(), Error> {
let name = HostName { module, name };
check(self.func_default.is_none())?;
check(self.insts.is_empty())?;
check(self.funcs.last().is_none_or(|x| x.0 < name))?;
self.funcs.push((name, type_));
Ok(())
}
/// Returns the call in the host, if any.
///
/// This function returns `None` if nothing is running.
// NOTE: This is like poll. Could be called next.
pub fn last_call(&mut self) -> Option<Call<'_, 'm>> {
if self.threads.is_empty() { None } else { Some(Call { store: self }) }
}
}
impl<'a, 'm> Call<'a, 'm> {
/// Returns the index of the host function being called.
pub fn index(&self) -> usize {
self.cont().index
}
/// Returns the arguments to the host function being called.
pub fn args(&self) -> &[Val] {
&self.cont().args
}
/// Returns the identifier of the instance calling the host.
pub fn inst(&self) -> InstId {
self.cont().thread.inst(self.store)
}
/// Returns the memory of the instance calling the host.
pub fn mem(self) -> &'a mut [u8] {
self.store.mem(self.inst().inst_id, 0).data
}
/// Returns the memory of the instance calling the host.
pub fn mem_mut(&mut self) -> &mut [u8] {
self.store.mem(self.inst().inst_id, 0).data
}
/// Resumes execution with the results from the host.
pub fn resume(self, results: &[Val]) -> Result<RunResult<'a, 'm>, Error> {
let Continuation { mut thread, arity, .. } = self.store.threads.pop().unwrap();
check(results.len() == arity)?;
thread.push_values(results);
thread.run(self.store)
}
fn cont(&self) -> &Continuation {
self.store.threads.last().unwrap()
}
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Ptr(u32);
impl core::fmt::Debug for Ptr {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Ptr")
.field("instance", &self.instance())
.field("index", &self.index())
.finish()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Side {
Host,
Wasm(usize),
}
impl Side {
fn into_repr(self) -> usize {
match self {
Side::Host => 0,
Side::Wasm(x) => 1 + x,
}
}
fn from_repr(x: usize) -> Self {
match x.checked_sub(1) {
None => Side::Host,
Some(x) => Side::Wasm(x),
}
}
fn unwrap_wasm(self) -> usize {
match self {
Side::Host => unreachable!(),
Side::Wasm(x) => x,
}
}
}
impl Ptr {
// 4k modules with 1M indices (per component)
const INDEX_BITS: usize = 20;
const INDEX_MASK: u32 = (1 << Self::INDEX_BITS) - 1;
const INST_MASK: u32 = (1 << (32 - Self::INDEX_BITS)) - 1;
fn new(inst: Side, idx: u32) -> Self {
let inst = inst.into_repr() as u32;
assert_eq!(inst & !Self::INST_MASK, 0);
assert_eq!(idx & !Self::INDEX_MASK, 0);
Self(inst << Self::INDEX_BITS | idx)
}
fn instance(self) -> Side {
Side::from_repr((self.0 >> Self::INDEX_BITS) as usize)
}
fn index(self) -> u32 {
self.0 & Self::INDEX_MASK
}
}
impl core::fmt::Debug for Val {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {
Val::I32(x) => write!(f, "{x}"),
Val::I64(x) => write!(f, "{x}"),
#[cfg(feature = "float-types")]
Val::F32(x) => write!(f, "{}", f32::from_bits(x)),
#[cfg(feature = "float-types")]
Val::F64(x) => write!(f, "{}", f64::from_bits(x)),
#[cfg(feature = "vector-types")]
Val::V128(_) => todo!(),
Val::Null(_) => write!(f, "null"),
Val::Ref(p) => write!(f, "ref@{:?}:{}", p.instance(), p.index()),
Val::RefExtern(p) => write!(f, "ext@{p}"),
}
}
}
impl Val {
pub fn type_(self) -> ValType {
match self {
Val::I32(_) => ValType::I32,
Val::I64(_) => ValType::I64,
#[cfg(feature = "float-types")]
Val::F32(_) => ValType::F32,
#[cfg(feature = "float-types")]
Val::F64(_) => ValType::F64,
#[cfg(feature = "vector-types")]
Val::V128(_) => ValType::V128,
Val::Null(t) => t.into(),
Val::Ref(_) => ValType::FuncRef,
Val::RefExtern(_) => ValType::ExternRef,
}
}
}
#[derive(Debug, Default)]
struct Instance<'m> {
// TODO: Make sure names are unique. Empty name means exports are not linked.
name: &'m str,
module: Module<'m>,
funcs: Component<()>,
tables: Component<Vec<Table>>,
mems: Component<Memory<'m>>,
globals: Component<Vec<Global>>,
elems: Vec<bool>, // whether the elem segment is dropped
datas: Vec<bool>, // whether the data segment is dropped
}
#[derive(Debug)]
struct Thread<'m> {
parser: Parser<'m>,
frames: Vec<Frame<'m>>,
// TODO: Consider the performance tradeoff between keeping the locals in and out of the value
// stack. See the comments in PR #605 for more details.
values: Vec<Val>,
}
/// Runtime result.
#[derive(Debug)]
pub enum RunResult<'a, 'm> {
/// Execution terminated successfully with those values.
Done(Vec<Val>),
/// Execution is calling into the host.
Host(Call<'a, 'm>),
}
/// Runtime result without host call information.
#[derive(Debug)]
pub enum RunAnswer {
Done(Vec<Val>),
Host,
}
impl RunResult<'_, '_> {
pub fn forget(self) -> RunAnswer {
match self {
RunResult::Done(result) => RunAnswer::Done(result),
RunResult::Host(_) => RunAnswer::Host,
}
}
}
#[derive(Debug)]
struct Continuation<'m> {
thread: Thread<'m>,
index: usize,
args: Vec<Val>,
arity: usize,
}
impl<'m> Store<'m> {
fn last_inst(&mut self) -> &mut Instance<'m> {
self.insts.last_mut().unwrap()
}
fn inst_id(&self, inst: InstId) -> Result<usize, Error> {
check(self.id == inst.store_id)?;
Ok(inst.inst_id)
}
fn resolve_inst(&self, name: &str) -> Result<usize, Error> {
self.insts.iter().position(|x| x.name == name).ok_or_else(not_found)
}
fn func_type(&self, ptr: Ptr) -> FuncType<'m> {
match ptr.instance() {
Side::Host => self.funcs[ptr.index() as usize].1,
Side::Wasm(x) => self.insts[x].module.func_type(ptr.index()),
}
}
fn func_ptr(&self, inst_id: usize, x: FuncIdx) -> Ptr {
self.insts[inst_id].funcs.ptr(inst_id, x)
}
fn table_ptr(&self, inst_id: usize, x: TableIdx) -> Ptr {
self.insts[inst_id].tables.ptr(inst_id, x)
}
fn mem_ptr(&self, inst_id: usize, x: MemIdx) -> Ptr {
self.insts[inst_id].mems.ptr(inst_id, x)
}
fn global_ptr(&self, inst_id: usize, x: GlobalIdx) -> Ptr {
self.insts[inst_id].globals.ptr(inst_id, x)
}
fn table(&mut self, inst_id: usize, x: TableIdx) -> &mut Table {
let ptr = self.table_ptr(inst_id, x);
let x = ptr.instance().unwrap_wasm();
&mut self.insts[x].tables.int[ptr.index() as usize]
}
fn mem(&mut self, inst_id: usize, x: MemIdx) -> &mut Memory<'m> {
let ptr = self.mem_ptr(inst_id, x);
let x = ptr.instance().unwrap_wasm();
assert_eq!(ptr.index(), 0);
&mut self.insts[x].mems.int
}
fn global(&mut self, inst_id: usize, x: GlobalIdx) -> &mut Global {
let ptr = self.global_ptr(inst_id, x);
let x = ptr.instance().unwrap_wasm();
&mut self.insts[x].globals.int[ptr.index() as usize]
}
fn resolve(&mut self, import: &Import<'m>, imp_type_: ExternType<'m>) -> Result<Ptr, Error> {
let host_name = HostName { module: import.module, name: import.name };
let mut found = None;
let funcs_len = match self.func_default {
Some((_, x)) => x,
None => self.funcs.len(),
};
if let Ok(x) = self.funcs[.. funcs_len].binary_search_by(|x| x.0.cmp(&host_name)) {
found = Some((Ptr::new(Side::Host, x as u32), ExternType::Func(self.funcs[x].1)));
} else if matches!(imp_type_, ExternType::Func(x) if *x.results == [ValType::I32])
&& matches!(self.func_default, Some((x, _)) if x == host_name.module)
{
let type_ = match imp_type_ {
ExternType::Func(x) => x,
_ => unreachable!(),
};
let idx = match self.funcs[funcs_len ..].iter().position(|x| x.1 == type_) {
Some(x) => funcs_len + x,
None => {
let idx = self.funcs.len();
self.funcs.push((host_name, type_));
idx
}
};
found = Some((Ptr::new(Side::Host, idx as u32), ExternType::Func(type_)));
} else {
let inst_id = self.resolve_inst(import.module)?;
let inst = &self.insts[inst_id];
if let Some(mut parser) = inst.module.section(SectionId::Export) {
for _ in 0 .. parser.parse_vec().into_ok() {
let name = parser.parse_name().into_ok();
let desc = parser.parse_exportdesc().into_ok();
if name != import.name {
continue;
}
found = Some(match desc {
ExportDesc::Func(x) => {
let ptr = self.func_ptr(inst_id, x);
(ptr, ExternType::Func(self.func_type(ptr)))
}
ExportDesc::Table(x) => {
let ptr = self.table_ptr(inst_id, x);
let inst = &self.insts[ptr.instance().unwrap_wasm()];
let mut t = inst.module.table_type(ptr.index());
t.limits.min = inst.tables.int[ptr.index() as usize].size();
(ptr, ExternType::Table(t))
}
ExportDesc::Mem(x) => {
let ptr = self.mem_ptr(inst_id, x);
let inst = &self.insts[ptr.instance().unwrap_wasm()];
let mut t = inst.module.mem_type(ptr.index());
assert_eq!(ptr.index(), 0);
t.min = inst.mems.int.size();
(ptr, ExternType::Mem(t))
}
ExportDesc::Global(x) => {
let ptr = self.global_ptr(inst_id, x);
let inst = &self.insts[ptr.instance().unwrap_wasm()];
(ptr, ExternType::Global(inst.module.global_type(ptr.index())))
}
});
break;
}
}
}
let (ptr, ext_type_) = found.ok_or_else(not_found)?;
if ext_type_.matches(&imp_type_) { Ok(ptr) } else { Err(not_found()) }
}
}
#[derive(Debug)]
enum ElemMode {
Passive,
Active { table: TableIdx, offset: usize },
Declarative,
}
struct ComputeElem<'a, 'm> {
store: &'a mut Store<'m>,
inst_id: usize,
mode: ElemMode,
init: Vec<Val>,
}
impl<'a, 'm> ComputeElem<'a, 'm> {
fn new(store: &'a mut Store<'m>, inst_id: usize) -> Self {
Self { store, inst_id, mode: ElemMode::Passive, init: Vec::new() }
}
}
impl<'m> parser::ParseElem<'m, Use> for ComputeElem<'_, 'm> {
fn mode(&mut self, mode: parser::ElemMode<'_, 'm, Use>) -> MResult<(), Use> {
let mode = match mode {
parser::ElemMode::Passive => ElemMode::Passive,
parser::ElemMode::Active { table, offset } => ElemMode::Active {
table,
offset: Thread::const_expr(self.store, self.inst_id, offset).unwrap_i32() as usize,
},
parser::ElemMode::Declarative => ElemMode::Declarative,
};
self.mode = mode;
Ok(())
}
fn init_funcidx(&mut self, x: FuncIdx) -> MResult<(), Use> {
self.init.push(Val::Ref(self.store.func_ptr(self.inst_id, x)));
Ok(())
}
fn init_expr(&mut self, parser: &mut Parser<'m>) -> MResult<(), Use> {
self.init.push(Thread::const_expr(self.store, self.inst_id, parser));
Ok(())
}
}
#[derive(Debug)]
enum DataMode {
Passive,
Active { memory: MemIdx, offset: usize },
}
struct ComputeData<'a, 'm> {
store: &'a mut Store<'m>,
inst_id: usize,
mode: DataMode,
init: &'m [u8],
}
impl<'a, 'm> ComputeData<'a, 'm> {
fn new(store: &'a mut Store<'m>, inst_id: usize) -> Self {
Self { store, inst_id, mode: DataMode::Passive, init: &[] }
}
}
impl<'m> parser::ParseData<'m, Use> for ComputeData<'_, 'm> {
fn mode(&mut self, mode: parser::DataMode<'_, 'm, Use>) -> MResult<(), Use> {
let mode = match mode {
parser::DataMode::Passive => DataMode::Passive,
parser::DataMode::Active { memory, offset } => DataMode::Active {
memory,
offset: Thread::const_expr(self.store, self.inst_id, offset).unwrap_i32() as usize,
},
};
self.mode = mode;
Ok(())
}
fn init(&mut self, init: &'m [u8]) -> MResult<(), Use> {
self.init = init;
Ok(())
}
}
#[derive(Debug)]
struct Table {
max: u32,
elems: Vec<Val>,
}
#[derive(Debug)]
struct Global {
value: Val,
}
enum ThreadResult<'m> {
Continue(Thread<'m>),
Done(Vec<Val>),
Host,
}
impl<'m> Thread<'m> {
fn new(parser: Parser<'m>, frame: Frame<'m>) -> Thread<'m> {
Thread { parser, frames: vec![frame], values: vec![] }
}
fn const_expr(store: &mut Store<'m>, inst_id: usize, mut_parser: &mut Parser<'m>) -> Val {
let parser = mut_parser.clone();
let mut thread = Thread::new(parser, Frame::new(inst_id, 1, &[], Vec::new(), &[], 0));
let (parser, results) = loop {
let p = thread.parser.save();
match thread.step(store).unwrap() {
ThreadResult::Continue(x) => thread = x,
ThreadResult::Done(x) => break (p, x),
ThreadResult::Host => unreachable!(),
}
};
unsafe { mut_parser.restore(parser) };
let instr = mut_parser.parse_instr().into_ok();
debug_assert_eq!(instr, Instr::End);
debug_assert_eq!(results.len(), 1);
results[0]
}
fn run<'a>(mut self, store: &'a mut Store<'m>) -> Result<RunResult<'a, 'm>, Error> {
loop {
// TODO: When trapping, we could return some CoreDump<'m> that contains the Thread<'m>.
// This permits to dump the frames.
match self.step(store)? {
ThreadResult::Continue(x) => self = x,
ThreadResult::Done(x) => return Ok(RunResult::Done(x)),
ThreadResult::Host => return Ok(RunResult::Host(Call { store })),
}
}
}
fn step(mut self, store: &mut Store<'m>) -> Result<ThreadResult<'m>, Error> {
use Instr::*;
let inst_id = self.frame().inst_id;
let inst = &mut store.insts[inst_id];
match self.parser.parse_instr().into_ok() {
Unreachable => return Err(trap()),
Nop => (),
Block(_) => self.push_label(),
Loop(_) => self.push_label(),
If(_) => match self.pop_value().unwrap_i32() {
0 => {
self.take_jump(0);
self.push_label();
}
_ => {
self.frame().skip_jump();
self.push_label();
}
},
Else => {
self.take_jump(0);
return Ok(self.exit_label());
}
End => return Ok(self.exit_label()),
Br(l) => return Ok(self.pop_label(l, 0)),
BrIf(l) => {
if self.pop_value().unwrap_i32() != 0 {
return Ok(self.pop_label(l, 0));
}
self.frame().skip_jump();
}
BrTable(ls, ln) => {
// In validation for BrTable, the side table entry for the last label index is
// created at first.
let i = self.pop_value().unwrap_i32() as usize;
return Ok(self
.pop_label(ls.get(i).cloned().unwrap_or(ln), ls.get(i).map_or(0, |_| i + 1)));
}
Return => return Ok(self.exit_frame()),
Call(x) => return self.invoke(store, store.func_ptr(inst_id, x)),
CallIndirect(x, y) => {
let i = self.pop_value().unwrap_i32();
let x = match store.table(inst_id, x).elems.get(i as usize) {
None | Some(Val::Null(_)) => return Err(trap()),
Some(x) => x.unwrap_ref(),
};
if store.func_type(x) != store.insts[inst_id].module.types()[y as usize] {
return Err(trap());
}
return self.invoke(store, x);
}
Drop => drop(self.pop_value()),
Select(_) => {
let c = self.pop_value().unwrap_i32();
let v2 = self.pop_value();
let v1 = self.pop_value();
self.push_value(match c {
0 => v2,
_ => v1,
});
}
LocalGet(x) => {
let v = self.frame().locals[x as usize];
self.push_value(v);
}
LocalSet(x) => {
let v = self.pop_value();
self.frame().locals[x as usize] = v;
}
LocalTee(x) => {
let v = self.peek_value();
self.frame().locals[x as usize] = v;
}
GlobalGet(x) => self.push_value(store.global(inst_id, x).value),
GlobalSet(x) => store.global(inst_id, x).value = self.pop_value(),
TableGet(x) => {
let i = self.pop_value().unwrap_i32();
let v = *store.table(inst_id, x).elems.get(i as usize).ok_or_else(trap)?;
self.push_value(v);
}
TableSet(x) => {
let val = self.pop_value();
let i = self.pop_value().unwrap_i32();
let v = store.table(inst_id, x).elems.get_mut(i as usize).ok_or_else(trap)?;
*v = val;
}
ILoad(n, m) => self.load(store.mem(inst_id, 0), NumType::i(n), n.into(), Sx::U, m)?,
#[cfg(feature = "float-types")]
FLoad(n, m) => self.load(store.mem(inst_id, 0), NumType::f(n), n.into(), Sx::U, m)?,
ILoad_(b, s, m) => {
self.load(store.mem(inst_id, 0), NumType::i(b.into()), b.into(), s, m)?
}
IStore(n, m) => self.store(store.mem(inst_id, 0), NumType::i(n), n.into(), m)?,
#[cfg(feature = "float-types")]
FStore(n, m) => self.store(store.mem(inst_id, 0), NumType::f(n), n.into(), m)?,
IStore_(b, m) => {
self.store(store.mem(inst_id, 0), NumType::i(b.into()), b.into(), m)?
}
MemorySize => self.push_value(Val::I32(store.mem(inst_id, 0).size())),
MemoryGrow => {
let n = self.pop_value().unwrap_i32();
self.push_value(Val::I32(grow(store.mem(inst_id, 0), n, ())));
}
I32Const(c) => self.push_value(Val::I32(c)),
I64Const(c) => self.push_value(Val::I64(c)),
#[cfg(feature = "float-types")]
F32Const(c) => self.push_value(Val::F32(c)),
#[cfg(feature = "float-types")]
F64Const(c) => self.push_value(Val::F64(c)),
ITestOp(n, op) => self.itestop(n, op),
IRelOp(n, op) => self.irelop(n, op),
#[cfg(feature = "float-types")]
FRelOp(n, op) => self.frelop(n, op),
IUnOp(n, op) => self.iunop(n, op)?,
#[cfg(feature = "float-types")]
FUnOp(n, op) => self.funop(n, op),
IBinOp(n, op) => self.ibinop(n, op)?,
#[cfg(feature = "float-types")]
FBinOp(n, op) => self.fbinop(n, op),
CvtOp(op) => self.cvtop(op)?,
IExtend(b) => self.extend(b),
RefNull(t) => self.push_value(Val::Null(t)),
RefIsNull => {
let c = matches!(self.pop_value(), Val::Null(_)) as u32;
self.push_value(Val::I32(c));
}
RefFunc(x) => self.push_value(Val::Ref(store.func_ptr(inst_id, x))),
MemoryInit(x) => {
let n = self.pop_value().unwrap_i32() as usize;
let s = self.pop_value().unwrap_i32() as usize;
let d = self.pop_value().unwrap_i32() as usize;
let data = if inst.datas[x as usize] {
&[]
} else {
let mut parser = inst.module.data(x);
let mut data = ComputeData::new(store, inst_id);
parser.parse_data(&mut data).into_ok();
data.init
};
let mem = store.mem(inst_id, 0);
memory_init(d, s, n, mem, data)?;
}
DataDrop(x) => inst.datas[x as usize] = true,
MemoryCopy => {
let n = self.pop_value().unwrap_i32() as usize;
let s = self.pop_value().unwrap_i32() as usize;
let d = self.pop_value().unwrap_i32() as usize;
let mem = store.mem(inst_id, 0);
if core::cmp::max(s, d).checked_add(n).is_none_or(|x| x > mem.len() as usize) {
return Err(trap());
}
mem.data.copy_within(s .. s + n, d);
}
MemoryFill => {
let n = self.pop_value().unwrap_i32() as usize;
let val = self.pop_value().unwrap_i32() as u8;
let d = self.pop_value().unwrap_i32() as usize;
let mem = store.mem(inst_id, 0);
if d.checked_add(n).is_none_or(|x| x > mem.len() as usize) {
memory_too_small(d, n, mem);
return Err(trap());
}
mem.data[d ..][.. n].fill(val);
}
TableInit(x, y) => {
let n = self.pop_value().unwrap_i32() as usize;
let s = self.pop_value().unwrap_i32() as usize;
let d = self.pop_value().unwrap_i32() as usize;
let elems = if inst.elems[y as usize] {
Vec::new()
} else {
let mut parser = inst.module.elem(y);
let mut elems = ComputeElem::new(store, inst_id);
parser.parse_elem(&mut elems).into_ok();
elems.init
};
let table = store.table(inst_id, x);
table_init(d, s, n, table, &elems)?;
}
ElemDrop(x) => inst.elems[x as usize] = true,
TableCopy(x, y) => {
let n = self.pop_value().unwrap_i32() as usize;
let s = self.pop_value().unwrap_i32() as usize;
let d = self.pop_value().unwrap_i32() as usize;
let sn = s.checked_add(n).ok_or_else(trap)?;
let dn = d.checked_add(n).ok_or_else(trap)?;
// TODO: This is not efficient.
let ys = store.table(inst_id, y).elems.get(s .. sn).ok_or_else(trap)?.to_vec();
let xs = store.table(inst_id, x).elems.get_mut(d .. dn).ok_or_else(trap)?;
xs.copy_from_slice(&ys);
}
TableGrow(x) => {
let n = self.pop_value().unwrap_i32();
let val = self.pop_value();
let table = store.table(inst_id, x);
self.push_value(Val::I32(grow(table, n, val)));
}
TableSize(x) => self.push_value(Val::I32(store.table(inst_id, x).size())),
TableFill(x) => {
let n = self.pop_value().unwrap_i32() as usize;
let val = self.pop_value();
let i = self.pop_value().unwrap_i32() as usize;
let table = store.table(inst_id, x);
if i.checked_add(n).is_none_or(|x| x > table.elems.len()) {
return Err(trap());
}
table.elems[i ..][.. n].fill(val);
}
}
Ok(ThreadResult::Continue(self))
}
fn inst_id(&self) -> usize {
self.frames.last().unwrap().inst_id
}
fn inst(&self, store: &Store<'m>) -> InstId {
InstId { store_id: store.id, inst_id: self.inst_id() }
}
fn frame(&mut self) -> &mut Frame<'m> {
self.frames.last_mut().unwrap()
}
fn values(&mut self) -> &mut Vec<Val> {
&mut self.values
}
fn peek_value(&mut self) -> Val {
*self.values().last().unwrap()
}
fn push_value(&mut self, value: Val) {
self.values().push(value);
}
fn push_value_or_trap(&mut self, value: Option<Val>) -> Result<(), Error> {
if let Some(x) = value {
self.push_value(x);
Ok(())