Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/tests/pcc_memory.rs
1685 views
1
//! Tests for proof-carrying-code-based validation of memory accesses
2
//! in Wasmtime/Cranelift-compiled Wasm, with various combinations of
3
//! memory settings.
4
5
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
6
mod pcc_memory_tests {
7
use wasmtime::*;
8
9
const TESTS: &'static [&'static str] = &[
10
r#"
11
local.get 0
12
i32.load8_u
13
drop
14
"#,
15
r#"
16
local.get 0
17
i32.load8_u offset=0x10000
18
drop
19
"#,
20
r#"
21
local.get 0
22
i32.load16_u
23
drop
24
"#,
25
r#"
26
local.get 0
27
i32.load16_u offset=0x10000
28
drop
29
"#,
30
r#"
31
local.get 0
32
i32.load
33
drop
34
"#,
35
r#"
36
local.get 0
37
i32.load offset=0x10000
38
drop
39
"#,
40
r#"
41
local.get 0
42
i64.load
43
drop
44
"#,
45
r#"
46
local.get 0
47
i64.load offset=0x10000
48
drop
49
"#,
50
];
51
52
#[test]
53
#[cfg_attr(miri, ignore)]
54
fn test_build() {
55
let _ = env_logger::try_init();
56
const KIB: u64 = 1024;
57
const MIB: u64 = 1024 * KIB;
58
const GIB: u64 = 1024 * MIB;
59
60
let mut bodies = vec![];
61
for (mem_min, mem_max) in [(1, 1), (10, 20)] {
62
for &snippet in TESTS {
63
bodies.push(format!(
64
"(module (memory {mem_min} {mem_max}) (func (param i32) {snippet}))"
65
));
66
}
67
let all_snippets = TESTS
68
.iter()
69
.map(|s| s.to_owned())
70
.collect::<Vec<_>>()
71
.join("\n");
72
bodies.push(format!(
73
"(module (memory {mem_min} {mem_max}) (func (param i32) {all_snippets}))"
74
));
75
}
76
77
for test in &bodies {
78
for memory_reservation in [4 * GIB] {
79
for guard_size in [2 * GIB] {
80
for enable_spectre in [true /* not yet supported by PCC: false */] {
81
for _memory_bits in [32 /* not yet supported by PCC: 64 */] {
82
log::trace!("test:\n{test}\n");
83
log::trace!("static {memory_reservation:x} guard {guard_size:x}");
84
let mut cfg = Config::new();
85
cfg.memory_reservation(memory_reservation);
86
cfg.memory_guard_size(guard_size);
87
cfg.cranelift_pcc(true);
88
unsafe {
89
cfg.cranelift_flag_set(
90
"enable_heap_access_spectre_mitigation",
91
&enable_spectre.to_string(),
92
);
93
}
94
// TODO: substitute memory32/memory64 into
95
// test module.
96
97
let engine = Engine::new(&cfg).unwrap();
98
99
let _module = Module::new(&engine, test)
100
.expect("compilation with PCC should succeed");
101
}
102
}
103
}
104
}
105
}
106
}
107
}
108
109