/__w/smoldot/smoldot/repo/lib/src/executor/host/tests/run.rs
Line | Count | Source |
1 | | // Smoldot |
2 | | // Copyright (C) 2023 Pierre Krieger |
3 | | // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
4 | | |
5 | | // This program is free software: you can redistribute it and/or modify |
6 | | // it under the terms of the GNU General Public License as published by |
7 | | // the Free Software Foundation, either version 3 of the License, or |
8 | | // (at your option) any later version. |
9 | | |
10 | | // This program is distributed in the hope that it will be useful, |
11 | | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | // GNU General Public License for more details. |
14 | | |
15 | | // You should have received a copy of the GNU General Public License |
16 | | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | | |
18 | | use super::super::{ |
19 | | Config, Error, HeapPages, HostVm, HostVmPrototype, NewErr, StartErr, StorageProofSizeBehavior, |
20 | | vm, vm::ExecHint, |
21 | | }; |
22 | | use super::with_core_version_custom_sections; |
23 | | |
24 | | #[test] |
25 | 1 | fn function_to_run_doesnt_exist() { |
26 | 1 | let module_bytes = with_core_version_custom_sections( |
27 | 1 | wat::parse_str( |
28 | | r#" |
29 | | (module |
30 | | (import "env" "memory" (memory 0)) |
31 | | (global (export "__heap_base") i32 (i32.const 0)) |
32 | | ) |
33 | | "#, |
34 | | ) |
35 | 1 | .unwrap(), |
36 | | ); |
37 | | |
38 | 2 | for exec_hint in ExecHint::available_engines1 () { |
39 | 2 | let host_vm = HostVmPrototype::new(Config { |
40 | 2 | allow_unresolved_imports: false, |
41 | 2 | exec_hint, |
42 | 2 | heap_pages: HeapPages::new(1024), |
43 | 2 | module: &module_bytes, |
44 | 2 | }) |
45 | 2 | .unwrap(); |
46 | | |
47 | 2 | match host_vm.run_no_param( |
48 | 2 | "functiondoesntexist", |
49 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
50 | | ) { |
51 | 2 | Err((StartErr::VirtualMachine(vm::StartErr::FunctionNotFound), _)) => {} |
52 | 0 | _ => unreachable!(), |
53 | | } |
54 | | } |
55 | 1 | } |
56 | | |
57 | | #[test] |
58 | 1 | fn function_to_run_invalid_params() { |
59 | 1 | let module_bytes = with_core_version_custom_sections( |
60 | 1 | wat::parse_str( |
61 | | r#" |
62 | | (module |
63 | | (import "env" "memory" (memory 0)) |
64 | | (global (export "__heap_base") i32 (i32.const 0)) |
65 | | (func (export "hello") (param i32) (result i32) i32.const 0) |
66 | | ) |
67 | | "#, |
68 | | ) |
69 | 1 | .unwrap(), |
70 | | ); |
71 | | |
72 | 2 | for exec_hint in ExecHint::available_engines1 () { |
73 | 2 | let host_vm = HostVmPrototype::new(Config { |
74 | 2 | allow_unresolved_imports: false, |
75 | 2 | exec_hint, |
76 | 2 | heap_pages: HeapPages::new(1024), |
77 | 2 | module: &module_bytes, |
78 | 2 | }) |
79 | 2 | .unwrap(); |
80 | | |
81 | 2 | match host_vm.run_no_param( |
82 | 2 | "hello", |
83 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
84 | | ) { |
85 | 2 | Err((StartErr::VirtualMachine(vm::StartErr::InvalidParameters), _)) => {} |
86 | 0 | _ => unreachable!(), |
87 | | } |
88 | | } |
89 | 1 | } |
90 | | |
91 | | #[test] |
92 | 1 | fn input_provided_correctly() { |
93 | | /* Source code: |
94 | | |
95 | | #[unsafe(no_mangle)] |
96 | | extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 { |
97 | | let inparam: &[u8] = unsafe { |
98 | | core::slice::from_raw_parts( |
99 | | u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8, |
100 | | u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize, |
101 | | ) |
102 | | }; |
103 | | |
104 | | if inparam |
105 | | != b"hello world" |
106 | | { |
107 | | core::arch::wasm32::unreachable() |
108 | | } |
109 | | |
110 | | 0 |
111 | | } |
112 | | */ |
113 | 1 | let module_bytes = with_core_version_custom_sections( |
114 | 1 | wat::parse_str( |
115 | | r#" |
116 | | (module |
117 | | (type (;0;) (func (param i32 i32) (result i32))) |
118 | | (type (;1;) (func (param i32 i32) (result i64))) |
119 | | (type (;2;) (func (param i32 i32 i32) (result i32))) |
120 | | (func (;0;) (type 0) (param i32 i32) (result i32) |
121 | | local.get 0 |
122 | | local.get 1 |
123 | | i32.const 11 |
124 | | call 3 |
125 | | i32.const 0 |
126 | | i32.ne) |
127 | | (func (;1;) (type 1) (param i32 i32) (result i64) |
128 | | block ;; label = @1 |
129 | | local.get 1 |
130 | | i32.const 11 |
131 | | i32.ne |
132 | | br_if 0 (;@1;) |
133 | | local.get 0 |
134 | | i32.const 1048576 |
135 | | call 0 |
136 | | br_if 0 (;@1;) |
137 | | i64.const 0 |
138 | | return |
139 | | end |
140 | | unreachable |
141 | | unreachable) |
142 | | (func (;2;) (type 2) (param i32 i32 i32) (result i32) |
143 | | (local i32 i32 i32) |
144 | | i32.const 0 |
145 | | local.set 3 |
146 | | block ;; label = @1 |
147 | | local.get 2 |
148 | | i32.eqz |
149 | | br_if 0 (;@1;) |
150 | | block ;; label = @2 |
151 | | loop ;; label = @3 |
152 | | local.get 0 |
153 | | i32.load8_u |
154 | | local.tee 4 |
155 | | local.get 1 |
156 | | i32.load8_u |
157 | | local.tee 5 |
158 | | i32.ne |
159 | | br_if 1 (;@2;) |
160 | | local.get 0 |
161 | | i32.const 1 |
162 | | i32.add |
163 | | local.set 0 |
164 | | local.get 1 |
165 | | i32.const 1 |
166 | | i32.add |
167 | | local.set 1 |
168 | | local.get 2 |
169 | | i32.const -1 |
170 | | i32.add |
171 | | local.tee 2 |
172 | | i32.eqz |
173 | | br_if 2 (;@1;) |
174 | | br 0 (;@3;) |
175 | | end |
176 | | end |
177 | | local.get 4 |
178 | | local.get 5 |
179 | | i32.sub |
180 | | local.set 3 |
181 | | end |
182 | | local.get 3) |
183 | | (func (;3;) (type 2) (param i32 i32 i32) (result i32) |
184 | | local.get 0 |
185 | | local.get 1 |
186 | | local.get 2 |
187 | | call 2) |
188 | | (table (;0;) 1 1 funcref) |
189 | | (memory (;0;) 17) |
190 | | (global (;0;) (mut i32) (i32.const 1048576)) |
191 | | (global (;1;) i32 (i32.const 1048587)) |
192 | | (global (;2;) i32 (i32.const 1048592)) |
193 | | (export "memory" (memory 0)) |
194 | | (export "test" (func 1)) |
195 | | (export "__data_end" (global 1)) |
196 | | (export "__heap_base" (global 2)) |
197 | | (data (;0;) (i32.const 1048576) "hello world") |
198 | | ) |
199 | | "#, |
200 | | ) |
201 | 1 | .unwrap(), |
202 | | ); |
203 | | |
204 | 2 | for exec_hint in ExecHint::available_engines1 () { |
205 | 2 | let proto = HostVmPrototype::new(Config { |
206 | 2 | allow_unresolved_imports: false, |
207 | 2 | exec_hint, |
208 | 2 | heap_pages: HeapPages::new(1024), |
209 | 2 | module: &module_bytes, |
210 | 2 | }) |
211 | 2 | .unwrap(); |
212 | | |
213 | 2 | let mut vm = HostVm::from( |
214 | 2 | proto |
215 | 2 | .run( |
216 | 2 | "test", |
217 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
218 | 2 | b"hello world", |
219 | | ) |
220 | 2 | .unwrap(), |
221 | | ); |
222 | | loop { |
223 | 4 | match vm { |
224 | 2 | HostVm::ReadyToRun(r) => vm = r.run(), |
225 | 2 | HostVm::Finished(v) => { |
226 | 2 | assert_eq!(v.value().as_ref(), b""); |
227 | 2 | break; |
228 | | } |
229 | 0 | _ => unreachable!(), |
230 | | } |
231 | | } |
232 | | } |
233 | | |
234 | 2 | for exec_hint in ExecHint::available_engines1 () { |
235 | 2 | let proto = HostVmPrototype::new(Config { |
236 | 2 | allow_unresolved_imports: false, |
237 | 2 | exec_hint, |
238 | 2 | heap_pages: HeapPages::new(1024), |
239 | 2 | module: &module_bytes, |
240 | 2 | }) |
241 | 2 | .unwrap(); |
242 | | |
243 | 2 | let mut vm = HostVm::from( |
244 | 2 | proto |
245 | 2 | .run_vectored( |
246 | 2 | "test", |
247 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
248 | 2 | [&b"hello "[..], &b"world"[..]].into_iter(), |
249 | | ) |
250 | 2 | .unwrap(), |
251 | | ); |
252 | | |
253 | | loop { |
254 | 4 | match vm { |
255 | 2 | HostVm::ReadyToRun(r) => vm = r.run(), |
256 | 2 | HostVm::Finished(v) => { |
257 | 2 | assert_eq!(v.value().as_ref(), b""); |
258 | 2 | break; |
259 | | } |
260 | 0 | _ => unreachable!(), |
261 | | } |
262 | | } |
263 | | } |
264 | | |
265 | 2 | for exec_hint in ExecHint::available_engines1 () { |
266 | 2 | let proto = HostVmPrototype::new(Config { |
267 | 2 | allow_unresolved_imports: false, |
268 | 2 | exec_hint, |
269 | 2 | heap_pages: HeapPages::new(1024), |
270 | 2 | module: &module_bytes, |
271 | 2 | }) |
272 | 2 | .unwrap(); |
273 | | |
274 | 2 | let mut vm = HostVm::from( |
275 | 2 | proto |
276 | 2 | .run( |
277 | 2 | "test", |
278 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
279 | 2 | b"unexpected input", |
280 | | ) |
281 | 2 | .unwrap(), |
282 | | ); |
283 | | loop { |
284 | 2 | match vm { |
285 | 2 | HostVm::ReadyToRun(r) => vm = r.run(), |
286 | | HostVm::Error { |
287 | | error: Error::Trap(_), |
288 | | .. |
289 | | } => { |
290 | 2 | break; |
291 | | } |
292 | 0 | _ => unreachable!(), |
293 | | } |
294 | | } |
295 | | } |
296 | 1 | } |
297 | | |
298 | | #[test] |
299 | 1 | fn large_input_provided_correctly() { |
300 | | /* Source code: |
301 | | |
302 | | #[unsafe(no_mangle)] |
303 | | extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 { |
304 | | let inparam: &[u8] = unsafe { |
305 | | core::slice::from_raw_parts( |
306 | | u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8, |
307 | | u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize, |
308 | | ) |
309 | | }; |
310 | | |
311 | | if inparam.len() != 395718 { |
312 | | core::arch::wasm32::unreachable() |
313 | | } |
314 | | |
315 | | for byte in inparam { |
316 | | if *byte != 0x7a { |
317 | | core::arch::wasm32::unreachable() |
318 | | } |
319 | | } |
320 | | |
321 | | 0 |
322 | | } |
323 | | */ |
324 | 1 | let module_bytes = with_core_version_custom_sections( |
325 | 1 | wat::parse_str( |
326 | | r#"(module |
327 | | (type (;0;) (func (param i32 i32) (result i64))) |
328 | | (func (;0;) (type 0) (param i32 i32) (result i64) |
329 | | (local i32) |
330 | | block ;; label = @1 |
331 | | block ;; label = @2 |
332 | | local.get 1 |
333 | | i32.const 395718 |
334 | | i32.ne |
335 | | br_if 0 (;@2;) |
336 | | i32.const 0 |
337 | | local.set 1 |
338 | | loop ;; label = @3 |
339 | | local.get 1 |
340 | | i32.const 395718 |
341 | | i32.eq |
342 | | br_if 2 (;@1;) |
343 | | local.get 0 |
344 | | local.get 1 |
345 | | i32.add |
346 | | local.set 2 |
347 | | local.get 1 |
348 | | i32.const 1 |
349 | | i32.add |
350 | | local.set 1 |
351 | | local.get 2 |
352 | | i32.load8_u |
353 | | i32.const 122 |
354 | | i32.eq |
355 | | br_if 0 (;@3;) |
356 | | end |
357 | | end |
358 | | unreachable |
359 | | unreachable |
360 | | end |
361 | | i64.const 0) |
362 | | (table (;0;) 1 1 funcref) |
363 | | (memory (;0;) 16) |
364 | | (global (;0;) (mut i32) (i32.const 1048576)) |
365 | | (global (;1;) i32 (i32.const 1048576)) |
366 | | (global (;2;) i32 (i32.const 1048576)) |
367 | | (export "memory" (memory 0)) |
368 | | (export "test" (func 0)) |
369 | | (export "__data_end" (global 1)) |
370 | | (export "__heap_base" (global 2)) |
371 | | ) |
372 | | "#, |
373 | | ) |
374 | 1 | .unwrap(), |
375 | | ); |
376 | | |
377 | 1 | let input_data = (0..395718).map(|_| 0x7a).collect::<Vec<_>>(); |
378 | | |
379 | 2 | for exec_hint in ExecHint::available_engines1 () { |
380 | 2 | let proto = HostVmPrototype::new(Config { |
381 | 2 | allow_unresolved_imports: false, |
382 | 2 | exec_hint, |
383 | 2 | heap_pages: HeapPages::new(1024), |
384 | 2 | module: &module_bytes, |
385 | 2 | }) |
386 | 2 | .unwrap(); |
387 | | |
388 | 2 | let mut vm = HostVm::from( |
389 | 2 | proto |
390 | 2 | .run( |
391 | 2 | "test", |
392 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
393 | 2 | &input_data, |
394 | | ) |
395 | 2 | .unwrap(), |
396 | | ); |
397 | | loop { |
398 | 4 | match vm { |
399 | 2 | HostVm::ReadyToRun(r) => vm = r.run(), |
400 | 2 | HostVm::Finished(v) => { |
401 | 2 | assert_eq!(v.value().as_ref(), b""); |
402 | 2 | break; |
403 | | } |
404 | 0 | _ => unreachable!(), |
405 | | } |
406 | | } |
407 | | } |
408 | 1 | } |
409 | | |
410 | | #[test] |
411 | 1 | fn return_value_works() { |
412 | | /* Source code: |
413 | | |
414 | | static OUT: &[u8] = b"hello world"; |
415 | | |
416 | | #[unsafe(no_mangle)] |
417 | | extern "C" fn test(_: i32, _: i32) -> i64 { |
418 | | let ptr = OUT.as_ptr() as usize as u32; |
419 | | let sz = OUT.len() as u32; |
420 | | |
421 | | i64::from_ne_bytes((u64::from(sz) << 32 | u64::from(ptr)).to_ne_bytes()) |
422 | | } |
423 | | */ |
424 | 1 | let module_bytes = with_core_version_custom_sections( |
425 | 1 | wat::parse_str( |
426 | | r#" |
427 | | (module |
428 | | (type (;0;) (func (param i32 i32) (result i64))) |
429 | | (func (;0;) (type 0) (param i32 i32) (result i64) |
430 | | i32.const 1048576 |
431 | | i64.extend_i32_u |
432 | | i64.const 47244640256 |
433 | | i64.or) |
434 | | (table (;0;) 1 1 funcref) |
435 | | (memory (;0;) 17) |
436 | | (global (;0;) (mut i32) (i32.const 1048576)) |
437 | | (global (;1;) i32 (i32.const 1048587)) |
438 | | (global (;2;) i32 (i32.const 1048592)) |
439 | | (export "memory" (memory 0)) |
440 | | (export "test" (func 0)) |
441 | | (export "__data_end" (global 1)) |
442 | | (export "__heap_base" (global 2)) |
443 | | (data (;0;) (i32.const 1048576) "hello world") |
444 | | ) |
445 | | "#, |
446 | | ) |
447 | 1 | .unwrap(), |
448 | | ); |
449 | | |
450 | 2 | for exec_hint in ExecHint::available_engines1 () { |
451 | 2 | let proto = HostVmPrototype::new(Config { |
452 | 2 | allow_unresolved_imports: false, |
453 | 2 | exec_hint, |
454 | 2 | heap_pages: HeapPages::new(1024), |
455 | 2 | module: &module_bytes, |
456 | 2 | }) |
457 | 2 | .unwrap(); |
458 | | |
459 | 2 | let mut vm = HostVm::from( |
460 | 2 | proto |
461 | 2 | .run( |
462 | 2 | "test", |
463 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
464 | 2 | &[], |
465 | | ) |
466 | 2 | .unwrap(), |
467 | | ); |
468 | | loop { |
469 | 4 | match vm { |
470 | 2 | HostVm::ReadyToRun(r) => vm = r.run(), |
471 | 2 | HostVm::Finished(out) => { |
472 | 2 | assert_eq!(out.value().as_ref(), b"hello world"); |
473 | 2 | break; |
474 | | } |
475 | 0 | _ => unreachable!(), |
476 | | } |
477 | | } |
478 | | } |
479 | 1 | } |
480 | | |
481 | | #[test] |
482 | 1 | fn bad_return_value() { |
483 | | /* Source code: |
484 | | |
485 | | #[unsafe(no_mangle)] |
486 | | extern "C" fn test(_: i32, _: i32) -> i32 { |
487 | | 0 |
488 | | } |
489 | | */ |
490 | 1 | let module_bytes = with_core_version_custom_sections( |
491 | 1 | wat::parse_str( |
492 | | r#" |
493 | | (module |
494 | | (type (;0;) (func (param i32 i32) (result i32))) |
495 | | (func (;0;) (type 0) (param i32 i32) (result i32) |
496 | | i32.const 0) |
497 | | (table (;0;) 1 1 funcref) |
498 | | (memory (;0;) 16) |
499 | | (global (;0;) (mut i32) (i32.const 1048576)) |
500 | | (global (;1;) i32 (i32.const 1048576)) |
501 | | (global (;2;) i32 (i32.const 1048576)) |
502 | | (export "memory" (memory 0)) |
503 | | (export "test" (func 0)) |
504 | | (export "__data_end" (global 1)) |
505 | | (export "__heap_base" (global 2)) |
506 | | ) |
507 | | "#, |
508 | | ) |
509 | 1 | .unwrap(), |
510 | | ); |
511 | | |
512 | 2 | for exec_hint in ExecHint::available_engines1 () { |
513 | 2 | let proto = HostVmPrototype::new(Config { |
514 | 2 | allow_unresolved_imports: false, |
515 | 2 | exec_hint, |
516 | 2 | heap_pages: HeapPages::new(1024), |
517 | 2 | module: &module_bytes, |
518 | 2 | }) |
519 | 2 | .unwrap(); |
520 | | |
521 | 2 | let mut vm = HostVm::from( |
522 | 2 | proto |
523 | 2 | .run( |
524 | 2 | "test", |
525 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
526 | 2 | &[], |
527 | | ) |
528 | 2 | .unwrap(), |
529 | | ); |
530 | | loop { |
531 | 2 | match vm { |
532 | 2 | HostVm::ReadyToRun(r) => vm = r.run(), |
533 | | HostVm::Error { |
534 | | error: Error::BadReturnValue { .. }, |
535 | | .. |
536 | | } => { |
537 | 2 | break; |
538 | | } |
539 | 0 | _ => unreachable!(), |
540 | | } |
541 | | } |
542 | | } |
543 | 1 | } |
544 | | |
545 | | #[test] |
546 | 1 | fn returned_ptr_out_of_range() { |
547 | | /* Source code: |
548 | | |
549 | | #[unsafe(no_mangle)] |
550 | | extern "C" fn test(_: i32, _: i32) -> i64 { |
551 | | let ptr = 0xffff_fff0usize as u32; |
552 | | let sz = 5 as u32; |
553 | | |
554 | | i64::from_ne_bytes((u64::from(sz) << 32 | u64::from(ptr)).to_ne_bytes()) |
555 | | } |
556 | | */ |
557 | 1 | let module_bytes = with_core_version_custom_sections( |
558 | 1 | wat::parse_str( |
559 | | r#" |
560 | | (module |
561 | | (type (;0;) (func (param i32 i32) (result i64))) |
562 | | (func (;0;) (type 0) (param i32 i32) (result i64) |
563 | | i64.const 25769803760) |
564 | | (table (;0;) 1 1 funcref) |
565 | | (memory (;0;) 16) |
566 | | (global (;0;) (mut i32) (i32.const 1048576)) |
567 | | (global (;1;) i32 (i32.const 1048576)) |
568 | | (global (;2;) i32 (i32.const 1048576)) |
569 | | (export "memory" (memory 0)) |
570 | | (export "test" (func 0)) |
571 | | (export "__data_end" (global 1)) |
572 | | (export "__heap_base" (global 2)) |
573 | | ) |
574 | | "#, |
575 | | ) |
576 | 1 | .unwrap(), |
577 | | ); |
578 | | |
579 | 2 | for exec_hint in ExecHint::available_engines1 () { |
580 | 2 | let proto = HostVmPrototype::new(Config { |
581 | 2 | allow_unresolved_imports: false, |
582 | 2 | exec_hint, |
583 | 2 | heap_pages: HeapPages::new(1024), |
584 | 2 | module: &module_bytes, |
585 | 2 | }) |
586 | 2 | .unwrap(); |
587 | | |
588 | 2 | let mut vm = HostVm::from( |
589 | 2 | proto |
590 | 2 | .run( |
591 | 2 | "test", |
592 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
593 | 2 | &[], |
594 | | ) |
595 | 2 | .unwrap(), |
596 | | ); |
597 | | loop { |
598 | 2 | match vm { |
599 | 2 | HostVm::ReadyToRun(r) => vm = r.run(), |
600 | | HostVm::Error { |
601 | | error: Error::ReturnedPtrOutOfRange { .. }, |
602 | | .. |
603 | | } => { |
604 | 2 | break; |
605 | | } |
606 | 0 | _ => unreachable!(), |
607 | | } |
608 | | } |
609 | | } |
610 | 1 | } |
611 | | |
612 | | #[test] |
613 | 1 | fn returned_size_out_of_range() { |
614 | | /* Source code: |
615 | | |
616 | | #[unsafe(no_mangle)] |
617 | | extern "C" fn test(_: i32, _: i32) -> i64 { |
618 | | let ptr = 5 as u32; |
619 | | let sz = 0xffff_fff0usize as u32; |
620 | | |
621 | | i64::from_ne_bytes((u64::from(sz) << 32 | u64::from(ptr)).to_ne_bytes()) |
622 | | } |
623 | | */ |
624 | 1 | let module_bytes = with_core_version_custom_sections( |
625 | 1 | wat::parse_str( |
626 | | r#" |
627 | | (module |
628 | | (type (;0;) (func (param i32 i32) (result i64))) |
629 | | (func (;0;) (type 0) (param i32 i32) (result i64) |
630 | | i64.const -68719476731) |
631 | | (table (;0;) 1 1 funcref) |
632 | | (memory (;0;) 16) |
633 | | (global (;0;) (mut i32) (i32.const 1048576)) |
634 | | (global (;1;) i32 (i32.const 1048576)) |
635 | | (global (;2;) i32 (i32.const 1048576)) |
636 | | (export "memory" (memory 0)) |
637 | | (export "test" (func 0)) |
638 | | (export "__data_end" (global 1)) |
639 | | (export "__heap_base" (global 2)) |
640 | | ) |
641 | | "#, |
642 | | ) |
643 | 1 | .unwrap(), |
644 | | ); |
645 | | |
646 | 2 | for exec_hint in ExecHint::available_engines1 () { |
647 | 2 | let proto = HostVmPrototype::new(Config { |
648 | 2 | allow_unresolved_imports: false, |
649 | 2 | exec_hint, |
650 | 2 | heap_pages: HeapPages::new(1024), |
651 | 2 | module: &module_bytes, |
652 | 2 | }) |
653 | 2 | .unwrap(); |
654 | | |
655 | 2 | let mut vm = HostVm::from( |
656 | 2 | proto |
657 | 2 | .run( |
658 | 2 | "test", |
659 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
660 | 2 | &[], |
661 | | ) |
662 | 2 | .unwrap(), |
663 | | ); |
664 | | loop { |
665 | 2 | match vm { |
666 | 2 | HostVm::ReadyToRun(r) => vm = r.run(), |
667 | | HostVm::Error { |
668 | | error: Error::ReturnedPtrOutOfRange { .. }, |
669 | | .. |
670 | | } => { |
671 | 2 | break; |
672 | | } |
673 | 0 | _ => unreachable!(), |
674 | | } |
675 | | } |
676 | | } |
677 | 1 | } |
678 | | |
679 | | #[test] |
680 | 1 | fn unresolved_host_function_called() { |
681 | | /* Source code: |
682 | | extern { |
683 | | fn host_function_that_doesnt_exist(); |
684 | | } |
685 | | |
686 | | #[unsafe(no_mangle)] |
687 | | extern "C" fn test(_: i32, _: i32) -> i64 { |
688 | | unsafe { |
689 | | host_function_that_doesnt_exist() |
690 | | } |
691 | | |
692 | | 0 |
693 | | } |
694 | | */ |
695 | 1 | let module_bytes = with_core_version_custom_sections( |
696 | 1 | wat::parse_str( |
697 | | r#" |
698 | | (module |
699 | | (type (;0;) (func)) |
700 | | (type (;1;) (func (param i32 i32) (result i64))) |
701 | | (import "env" "host_function_that_doesnt_exist" (func (;0;) (type 0))) |
702 | | (func (;1;) (type 1) (param i32 i32) (result i64) |
703 | | call 0 |
704 | | i64.const 0) |
705 | | (table (;0;) 1 1 funcref) |
706 | | (memory (;0;) 16) |
707 | | (global (;0;) (mut i32) (i32.const 1048576)) |
708 | | (global (;1;) i32 (i32.const 1048576)) |
709 | | (global (;2;) i32 (i32.const 1048576)) |
710 | | (export "memory" (memory 0)) |
711 | | (export "test" (func 1)) |
712 | | (export "__data_end" (global 1)) |
713 | | (export "__heap_base" (global 2)) |
714 | | ) |
715 | | "#, |
716 | | ) |
717 | 1 | .unwrap(), |
718 | | ); |
719 | | |
720 | 2 | for exec_hint in ExecHint::available_engines1 () { |
721 | 2 | match HostVmPrototype::new(Config { |
722 | 2 | allow_unresolved_imports: false, |
723 | 2 | exec_hint, |
724 | 2 | heap_pages: HeapPages::new(1024), |
725 | 2 | module: &module_bytes, |
726 | 2 | }) { |
727 | 2 | Err(NewErr::VirtualMachine(vm::NewErr::UnresolvedFunctionImport { .. })) => {} |
728 | 0 | _ => panic!(), |
729 | | } |
730 | | |
731 | 2 | let proto = HostVmPrototype::new(Config { |
732 | 2 | allow_unresolved_imports: true, |
733 | 2 | exec_hint, |
734 | 2 | heap_pages: HeapPages::new(1024), |
735 | 2 | module: &module_bytes, |
736 | 2 | }) |
737 | 2 | .unwrap(); |
738 | | |
739 | 2 | let mut vm = HostVm::from( |
740 | 2 | proto |
741 | 2 | .run( |
742 | 2 | "test", |
743 | 2 | StorageProofSizeBehavior::proof_recording_disabled(), |
744 | 2 | &[], |
745 | | ) |
746 | 2 | .unwrap(), |
747 | | ); |
748 | | loop { |
749 | 2 | match vm { |
750 | 2 | HostVm::ReadyToRun(r) => vm = r.run(), |
751 | | HostVm::Error { |
752 | | error: Error::UnresolvedFunctionCalled { .. }, |
753 | | .. |
754 | | } => { |
755 | 2 | break; |
756 | | } |
757 | 0 | _ => unreachable!(), |
758 | | } |
759 | | } |
760 | | } |
761 | 1 | } |
762 | | |
763 | | // TODO: consider more tests for the other errors here, or add them on a host-function case-by-case basis |