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
#![deny(missing_docs, missing_debug_implementations)]
use anyhow::{anyhow, bail, Error};
use walrus::{GlobalId, GlobalKind, MemoryId, Module, ValType, InitExpr, ir::Value};
pub fn get_memory(module: &Module) -> Result<MemoryId, Error> {
let mut memories = module.memories.iter().map(|m| m.id());
let memory = memories.next();
if memories.next().is_some() {
bail!(
"expected a single memory, found multiple; multiple memories \
currently not supported"
);
}
memory.ok_or_else(|| {
anyhow!(
"module does not have a memory; must have a memory \
to transform return pointers into Wasm multi-value"
)
})
}
pub fn get_shadow_stack_pointer(module: &Module) -> Option<GlobalId> {
let candidates = module
.globals
.iter()
.filter(|g| g.ty == ValType::I32)
.filter(|g| g.mutable)
.filter(|g| match g.kind {
GlobalKind::Local(InitExpr::Value(Value::I32(n))) => n != 0,
_ => false,
})
.collect::<Vec<_>>();
match candidates.len() {
0 => None,
1 => Some(candidates[0].id()),
_ => None,
}
}