|
1 | 1 | #![cfg(not(miri))]
|
2 | 2 |
|
| 3 | +use wasm_encoder::Instruction as I; |
3 | 4 | use wasmtime::*;
|
4 | 5 |
|
5 | 6 | #[test]
|
6 | 7 | fn code_too_large_without_panic() -> Result<()> {
|
7 |
| - const N: usize = 120000; |
| 8 | + const N: usize = 80000; |
8 | 9 |
|
9 | 10 | // Build a module with a function whose body will allocate too many
|
10 | 11 | // temporaries for our current (Cranelift-based) compiler backend to
|
11 | 12 | // handle. This test ensures that we propagate the failure upward
|
12 | 13 | // and return it programmatically, rather than panic'ing. If we ever
|
13 | 14 | // improve our compiler backend to actually handle such a large
|
14 | 15 | // function body, we'll need to increase the limits here too!
|
15 |
| - let mut s = String::new(); |
16 |
| - s.push_str("(module\n"); |
17 |
| - s.push_str("(table 1 1 funcref)\n"); |
18 |
| - s.push_str("(func (export \"\") (result i32)\n"); |
19 |
| - s.push_str("i32.const 0\n"); |
| 16 | + let mut module = wasm_encoder::Module::default(); |
| 17 | + |
| 18 | + let mut types = wasm_encoder::TypeSection::new(); |
| 19 | + types.ty().function([], [wasm_encoder::ValType::I32]); |
| 20 | + module.section(&types); |
| 21 | + |
| 22 | + let mut funcs = wasm_encoder::FunctionSection::new(); |
| 23 | + funcs.function(0); |
| 24 | + module.section(&funcs); |
| 25 | + |
| 26 | + let mut tables = wasm_encoder::TableSection::new(); |
| 27 | + tables.table(wasm_encoder::TableType { |
| 28 | + element_type: wasm_encoder::RefType::FUNCREF, |
| 29 | + table64: false, |
| 30 | + minimum: 1, |
| 31 | + maximum: Some(1), |
| 32 | + shared: false, |
| 33 | + }); |
| 34 | + module.section(&tables); |
| 35 | + |
| 36 | + let mut exports = wasm_encoder::ExportSection::new(); |
| 37 | + exports.export("", wasm_encoder::ExportKind::Func, 0); |
| 38 | + module.section(&exports); |
| 39 | + |
| 40 | + let mut func = wasm_encoder::Function::new([]); |
| 41 | + func.instruction(&I::I32Const(0)); |
20 | 42 | for _ in 0..N {
|
21 |
| - s.push_str("table.get 0\n"); |
22 |
| - s.push_str("ref.is_null\n"); |
| 43 | + func.instruction(&I::TableGet(0)); |
| 44 | + func.instruction(&I::RefIsNull); |
23 | 45 | }
|
24 |
| - s.push_str("))\n"); |
| 46 | + func.instruction(&I::End); |
| 47 | + let mut code = wasm_encoder::CodeSection::new(); |
| 48 | + code.function(&func); |
| 49 | + module.section(&code); |
| 50 | + |
| 51 | + let mut config = Config::new(); |
| 52 | + config.cranelift_opt_level(OptLevel::None); |
| 53 | + let engine = Engine::new(&config)?; |
25 | 54 |
|
26 |
| - let store = Store::<()>::default(); |
27 |
| - let result = Module::new(store.engine(), &s); |
| 55 | + let store = Store::new(&engine, ()); |
| 56 | + let result = Module::new(store.engine(), &module.finish()); |
28 | 57 | match result {
|
29 | 58 | Err(e) => assert!(e
|
30 | 59 | .to_string()
|
|
0 commit comments