Skip to content

Commit

Permalink
Add check to prevent size increase in embedded actors from corrupting…
Browse files Browse the repository at this point in the history
… data section (#43)
  • Loading branch information
kentosugama authored Sep 6, 2023
1 parent 0cc218f commit 733e09d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ pub fn optimize(
if is_motoko_canister(m) {
let data = get_motoko_wasm_data_sections(m);
for (id, mut module) in data.into_iter() {
let old_size = module.emit_wasm().len();
optimize(
&mut module,
level,
inline_functions_with_loops,
always_inline_max_function_size,
keep_name_section,
)?;
let blob = encode_module_as_data_section(module);
m.data.get_mut(id).value = blob;
let new_size = module.emit_wasm().len();
// Guard against embedded actor class overriding the parent module
if new_size <= old_size {
let blob = encode_module_as_data_section(module);
m.data.get_mut(id).value = blob;
} else {
eprintln!("Warning: embedded actor class module was not optimized because the optimized module is larger than the original module");
}
}
}

Expand Down
Binary file modified tests/ok/classes-optimize-names.wasm
Binary file not shown.
Binary file modified tests/ok/classes-optimize.wasm
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ icp:private motoko:compiler
wasm_input("classes.wasm", true)
.arg("optimize")
.arg("O3")
.arg("--inline-functions-with-loops")
.arg("--always-inline-max-function-size")
.arg("100")
.assert()
.success();
assert_wasm("classes-optimize.wasm");
Expand Down

0 comments on commit 733e09d

Please sign in to comment.