Skip to content

Commit ccafc5b

Browse files
authored
Merge pull request #85 from s1na/deployer-mem
Grow deployer's memory to size
2 parents 1960f58 + 03f1db8 commit ccafc5b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

libchisel/src/deployer.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ fn create_custom_deployer(payload: &[u8]) -> Module {
7575
// This is the pre-written deployer code.
7676
let mut module: Module = parity_wasm::deserialize_buffer(&code).expect("Failed to load module");
7777

78+
// Re-write memory to pre-allocate enough for code size
79+
let memory_initial = (payload.len() as u32 / 65536) + 1;
80+
let mem_type = parity_wasm::elements::MemoryType::new(memory_initial, None, false);
81+
module.memory_section_mut().unwrap().entries_mut()[0] = mem_type;
82+
7883
// Prepare payload (append length).
7984
let mut custom_payload = payload.to_vec();
8085
custom_payload
@@ -102,6 +107,8 @@ fn create_memory_deployer(payload: &[u8]) -> Module {
102107
parity_wasm::elements::Instruction::End,
103108
];
104109

110+
let memory_initial = (payload.len() as u32 / 65536) + 1;
111+
105112
let module = builder::module()
106113
// Create a func/type for the ethereum::finish
107114
.function()
@@ -132,6 +139,7 @@ fn create_memory_deployer(payload: &[u8]) -> Module {
132139
.build()
133140
// Add default memory section
134141
.memory()
142+
.with_min(memory_initial)
135143
.build()
136144
// Export memory
137145
.export()
@@ -214,6 +222,19 @@ mod tests {
214222
assert_eq!(output, expected);
215223
}
216224

225+
#[test]
226+
fn big_payload() {
227+
let payload = [0; 632232];
228+
let module = Deployer::with_preset("customsection", &payload)
229+
.unwrap()
230+
.create()
231+
.unwrap();
232+
let memory_initial = module.memory_section().unwrap().entries()[0]
233+
.limits()
234+
.initial();
235+
assert_eq!(memory_initial, 10);
236+
}
237+
217238
#[test]
218239
fn memory_zero_payload() {
219240
let payload = vec![];
@@ -252,4 +273,17 @@ mod tests {
252273
let output = parity_wasm::serialize(module).expect("Failed to serialize");
253274
assert_eq!(output, expected);
254275
}
276+
277+
#[test]
278+
fn memory_big_payload() {
279+
let payload = [0; 632232];
280+
let module = Deployer::with_preset("memory", &payload)
281+
.unwrap()
282+
.create()
283+
.unwrap();
284+
let memory_initial = module.memory_section().unwrap().entries()[0]
285+
.limits()
286+
.initial();
287+
assert_eq!(memory_initial, 10);
288+
}
255289
}

0 commit comments

Comments
 (0)