Skip to content

Commit

Permalink
Merge branch 'mk/wasm64_bulk_instrumentation' into 'master'
Browse files Browse the repository at this point in the history
Feat: RUN-986: Instrumentation for wasm64 bulk memory ops

Metering for bulk memory operations needs to be updated to support wasm64 

See merge request dfinity-lab/public/ic!19630
  • Loading branch information
Maciej Kot committed Jun 5, 2024
2 parents c717533 + aa96f73 commit 8311999
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
37 changes: 26 additions & 11 deletions rs/embedders/src/wasm_utils/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,12 @@ pub(super) fn instrument(

// inject instructions counter decrementation
for func_body in &mut module.code_sections {
inject_metering(&mut func_body.instructions, &special_indices, metering_type);
inject_metering(
&mut func_body.instructions,
&special_indices,
metering_type,
main_memory_type,
);
}

// Collect all the function types of the locally defined functions inside the
Expand Down Expand Up @@ -1425,6 +1430,7 @@ fn inject_metering(
code: &mut Vec<Operator>,
export_data_module: &SpecialIndices,
metering_type: MeteringType,
mem_type: WasmMemoryType,
) {
let points = match metering_type {
MeteringType::None => Vec::new(),
Expand Down Expand Up @@ -1476,16 +1482,25 @@ fn inject_metering(
}
}
InjectionPointCostDetail::DynamicCost => {
elems.extend_from_slice(&[
I64ExtendI32U,
Call {
function_index: export_data_module.decr_instruction_counter_fn,
},
// decr_instruction_counter returns it's argument unchanged,
// so we can convert back to I32 without worrying about
// overflows.
I32WrapI64,
]);
match mem_type {
WasmMemoryType::Wasm32 => {
elems.extend_from_slice(&[
I64ExtendI32U,
Call {
function_index: export_data_module.decr_instruction_counter_fn,
},
// decr_instruction_counter returns it's argument unchanged,
// so we can convert back to I32 without worrying about
// overflows.
I32WrapI64,
]);
}
WasmMemoryType::Wasm64 => {
elems.extend_from_slice(&[Call {
function_index: export_data_module.decr_instruction_counter_fn,
}]);
}
}
}
}
last_injection_position = point.position;
Expand Down
53 changes: 53 additions & 0 deletions rs/embedders/tests/wasmtime_embedder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,59 @@ fn wasm64_basic_test() {
assert_eq!(res.exported_globals[0], Global::I64(137));
}

#[test]
// Verify that we can create 64 bit memory and write to it
fn memory_copy_test() {
let wat = r#"
(module
(global $g1 (export "g1") (mut i64) (i64.const 0))
(func $test (export "canister_update test")
(i64.store (i32.const 20) (i64.const 137))
(memory.copy (i32.const 50) (i32.const 20) (i32.const 8))
(i64.load (i32.const 50))
global.set $g1
)
(memory (export "memory") 10)
)"#;

let mut config = ic_config::embedders::Config::default();
config.feature_flags.wasm64 = FlagStatus::Enabled;
let mut instance = WasmtimeInstanceBuilder::new()
.with_config(config)
.with_wat(wat)
.build();
let res = instance
.run(FuncRef::Method(WasmMethod::Update("test".to_string())))
.unwrap();
assert_eq!(res.exported_globals[0], Global::I64(137));
}

#[test]
fn wasm64_memory_copy_test() {
let wat = r#"
(module
(global $g1 (export "g1") (mut i64) (i64.const 0))
(func $test (export "canister_update test")
(i64.store (i64.const 20) (i64.const 137))
(memory.copy (i64.const 50) (i64.const 20) (i64.const 8))
(i64.load (i64.const 50))
global.set $g1
)
(memory (export "memory") i64 10)
)"#;

let mut config = ic_config::embedders::Config::default();
config.feature_flags.wasm64 = FlagStatus::Enabled;
let mut instance = WasmtimeInstanceBuilder::new()
.with_config(config)
.with_wat(wat)
.build();
let res = instance
.run(FuncRef::Method(WasmMethod::Update("test".to_string())))
.unwrap();
assert_eq!(res.exported_globals[0], Global::I64(137));
}

#[test]
// Verify behavior of failed memory grow in wasm64 mode
fn wasm64_handles_memory_grow_failure_test() {
Expand Down

0 comments on commit 8311999

Please sign in to comment.