Skip to content

Commit

Permalink
adapt system api call fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
venkkatesh-sekar committed Jan 11, 2025
1 parent 5717196 commit 2e44482
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
15 changes: 7 additions & 8 deletions rs/embedders/fuzz/src/ic_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl ICWasmModule {
}

pub fn ic_wasm_config(embedder_config: EmbeddersConfig) -> Config {
let memory64_enabled = embedder_config.feature_flags.wasm64 == FlagStatus::Enabled;
Config {
min_funcs: 10,
min_exports: 10,
Expand All @@ -151,20 +152,18 @@ pub fn ic_wasm_config(embedder_config: EmbeddersConfig) -> Config {
bulk_memory_enabled: true,
reference_types_enabled: true,
simd_enabled: true,
memory64_enabled: embedder_config.feature_flags.wasm64 == FlagStatus::Enabled,
memory64_enabled,

threads_enabled: false,
relaxed_simd_enabled: false,
canonicalize_nans: false,
exceptions_enabled: false,

available_imports: Some(
if embedder_config.feature_flags.wasm64 == FlagStatus::Enabled {
SYSTEM_API_IMPORTS_WASM64.to_vec()
} else {
SYSTEM_API_IMPORTS_WASM32.to_vec()
},
),
available_imports: Some(if memory64_enabled {
SYSTEM_API_IMPORTS_WASM64.to_vec()
} else {
SYSTEM_API_IMPORTS_WASM32.to_vec()
}),
..Default::default()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use ic_config::{
embedders::Config as EmbeddersConfig, embedders::FeatureFlags,
execution_environment::Config as ExecutionConfig, flag_status::FlagStatus,
subnet_config::SubnetConfig,
};
use ic_config::{execution_environment::Config as ExecutionConfig, subnet_config::SubnetConfig};
use ic_management_canister_types::{CanisterInstallMode, CanisterSettingsArgsBuilder};
use ic_registry_subnet_type::SubnetType;
use ic_state_machine_tests::{StateMachine, StateMachineBuilder, StateMachineConfig};
use ic_types::{CanisterId, Cycles, NumBytes};

use libfuzzer_sys::fuzz_target;
use std::cell::RefCell;
use wasm_fuzzers::ic_wasm::ICWasmModule;
use wasm_fuzzers::ic_wasm::{ic_embedders_config, ICWasmModule};

thread_local! {
static ENV: RefCell<(StateMachine, CanisterId)> = RefCell::new(setup_env());
static ENV_32: RefCell<(StateMachine, CanisterId)> = RefCell::new(setup_env(false));
static ENV_64: RefCell<(StateMachine, CanisterId)> = RefCell::new(setup_env(true));
}

const HELLO_WORLD_WAT: &str = r#"
Expand All @@ -30,7 +27,7 @@ fn main() {
}

fuzz_target!(|data: ICWasmModule| {
with_env(|env, canister_id| {
with_env(data.config.memory64_enabled, |env, canister_id| {
let wasm = data.module.to_bytes();
if env
.install_wasm_in_mode(*canister_id, CanisterInstallMode::Reinstall, wasm, vec![])
Expand All @@ -44,29 +41,29 @@ fuzz_target!(|data: ICWasmModule| {
});
});

fn with_env<F, R>(f: F) -> R
fn with_env<F, R>(memory64_enabled: bool, f: F) -> R
where
F: FnOnce(&StateMachine, &CanisterId) -> R,
{
ENV.with(|env| {
let env_ref = env.borrow();
f(&env_ref.0, &env_ref.1) // Pass references to the closure
})
if memory64_enabled {
ENV_64.with(|env| {
let env_ref = env.borrow();
f(&env_ref.0, &env_ref.1)
})
} else {
ENV_32.with(|env| {
let env_ref = env.borrow();
f(&env_ref.0, &env_ref.1)
})
}
}

// A setup function to initialize StateMachine with a dummy canister and expose the cansiter_id.
// The same canister_id and StateMachine reference is used in the fuzzing runs, where the
// canister is reinstalled under the same canister_id
fn setup_env() -> (StateMachine, CanisterId) {
fn setup_env(memory64_enabled: bool) -> (StateMachine, CanisterId) {
let exec_config = ExecutionConfig {
embedders_config: EmbeddersConfig {
feature_flags: FeatureFlags {
write_barrier: FlagStatus::Enabled,
wasm64: FlagStatus::Enabled, // Enable wasm64 to match generated ICWasmModule.
..Default::default()
},
..Default::default()
},
embedders_config: ic_embedders_config(memory64_enabled),
max_compilation_cache_size: NumBytes::new(10 * 1024 * 1024), // 10MiB
..Default::default()
};
Expand Down

0 comments on commit 2e44482

Please sign in to comment.