Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/tests/all/code_too_large.rs
1690 views
1
#![cfg(not(miri))]
2
3
use wasm_encoder::Instruction as I;
4
use wasmtime::*;
5
6
#[test]
7
fn code_too_large_without_panic() -> Result<()> {
8
// This takes 1m+ in ASAN and isn't too useful to test in ASAN.
9
if cfg!(asan) {
10
return Ok(());
11
}
12
13
const N: usize = 80000;
14
15
// Build a module with a function whose body will allocate too many
16
// temporaries for our current (Cranelift-based) compiler backend to
17
// handle. This test ensures that we propagate the failure upward
18
// and return it programmatically, rather than panic'ing. If we ever
19
// improve our compiler backend to actually handle such a large
20
// function body, we'll need to increase the limits here too!
21
let mut module = wasm_encoder::Module::default();
22
23
let mut types = wasm_encoder::TypeSection::new();
24
types.ty().function([], [wasm_encoder::ValType::I32]);
25
module.section(&types);
26
27
let mut funcs = wasm_encoder::FunctionSection::new();
28
funcs.function(0);
29
module.section(&funcs);
30
31
let mut tables = wasm_encoder::TableSection::new();
32
tables.table(wasm_encoder::TableType {
33
element_type: wasm_encoder::RefType::FUNCREF,
34
table64: false,
35
minimum: 1,
36
maximum: Some(1),
37
shared: false,
38
});
39
module.section(&tables);
40
41
let mut exports = wasm_encoder::ExportSection::new();
42
exports.export("", wasm_encoder::ExportKind::Func, 0);
43
module.section(&exports);
44
45
let mut func = wasm_encoder::Function::new([]);
46
func.instruction(&I::I32Const(0));
47
for _ in 0..N {
48
func.instruction(&I::TableGet(0));
49
func.instruction(&I::RefIsNull);
50
}
51
func.instruction(&I::End);
52
let mut code = wasm_encoder::CodeSection::new();
53
code.function(&func);
54
module.section(&code);
55
56
let mut config = Config::new();
57
config.cranelift_opt_level(OptLevel::None);
58
let engine = Engine::new(&config)?;
59
60
let store = Store::new(&engine, ());
61
let result = Module::new(store.engine(), &module.finish());
62
match result {
63
Err(e) => {
64
assert!(format!("{e:?}").contains("Compilation error: Code for function is too large"))
65
}
66
Ok(_) => panic!("Please adjust limits to make the module too large to compile!"),
67
}
68
Ok(())
69
}
70
71