Skip to content

Commit 08d2c83

Browse files
authored
Merge pull request #78 from Shopify/ap.no-op-on-no-memory
No-op for modules without memory in trampoline
2 parents 6c08aa4 + cb74cc6 commit 08d2c83

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

trampoline/src/lib.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn trampoline_existing_module(
9898

9999
pub struct TrampolineCodegen {
100100
module: Module,
101-
guest_memory_id: MemoryId,
101+
guest_memory_id: Option<MemoryId>,
102102
provider_memory_id: OnceCell<MemoryId>,
103103
memcpy_to_guest: OnceCell<FunctionId>,
104104
memcpy_to_provider: OnceCell<FunctionId>,
@@ -136,18 +136,18 @@ impl TrampolineCodegen {
136136
})
137137
}
138138

139-
fn guest_memory_id(module: &Module) -> walrus::Result<MemoryId> {
139+
fn guest_memory_id(module: &Module) -> walrus::Result<Option<MemoryId>> {
140140
let non_imported_memories = module
141141
.memories
142142
.iter()
143143
.filter(|&memory| memory.import.is_none())
144144
.map(|memory| memory.id())
145145
.collect::<Vec<_>>();
146146

147-
if let Some((memory_id, [])) = non_imported_memories.split_first() {
148-
Ok(*memory_id)
149-
} else {
150-
anyhow::bail!("expected exactly one non-imported guest memory")
147+
match non_imported_memories.split_first() {
148+
Some((memory_id, [])) => Ok(Some(*memory_id)),
149+
Some(_) => anyhow::bail!("multiple non-imported memories are not supported"),
150+
None => Ok(None),
151151
}
152152
}
153153

@@ -170,7 +170,10 @@ impl TrampolineCodegen {
170170
.local_get(dst)
171171
.local_get(src)
172172
.local_get(size)
173-
.memory_copy(provider_memory_id, self.guest_memory_id);
173+
.memory_copy(
174+
provider_memory_id,
175+
self.guest_memory_id.expect("no guest memory"),
176+
);
174177

175178
memcpy_to_guest.finish(vec![dst, src, size], &mut self.module.funcs)
176179
})
@@ -195,7 +198,10 @@ impl TrampolineCodegen {
195198
.local_get(dst)
196199
.local_get(src)
197200
.local_get(size)
198-
.memory_copy(self.guest_memory_id, provider_memory_id);
201+
.memory_copy(
202+
self.guest_memory_id.expect("no guest memory"),
203+
provider_memory_id,
204+
);
199205

200206
memcpy_to_provider.finish(vec![dst, src, size], &mut self.module.funcs)
201207
})
@@ -458,6 +464,11 @@ impl TrampolineCodegen {
458464
}
459465

460466
pub fn apply(mut self) -> walrus::Result<Module> {
467+
// If the module does not have a memory, we should no-op
468+
if self.guest_memory_id.is_none() {
469+
return Ok(self.module);
470+
}
471+
461472
for (original, new) in IMPORTS {
462473
match *original {
463474
"shopify_function_input_read_utf8_str" => {
@@ -542,7 +553,7 @@ mod test {
542553
let err = result.unwrap_err();
543554
assert_eq!(
544555
err.to_string(),
545-
"expected exactly one non-imported guest memory"
556+
"multiple non-imported memories are not supported"
546557
);
547558
}
548559
}

trampoline/src/snapshots/shopify_function_trampoline__test__disassemble_trampoline@empty.wat.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ expression: actual
44
input_file: trampoline/src/test_data/empty.wat
55
---
66
(module
7-
(memory (;0;) 1)
8-
(export "memory" (memory 0))
97
(@producers
108
(processed-by "walrus" "0.23.3")
119
)

trampoline/src/test_data/empty.wat

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
(module
2-
(memory 1)
3-
(export "memory" (memory 0))
4-
)
1+
(module)

0 commit comments

Comments
 (0)