Skip to content
This repository has been archived by the owner on Aug 26, 2023. It is now read-only.

Commit

Permalink
Make number of allocated globals for saved parameters adaptive
Browse files Browse the repository at this point in the history
  • Loading branch information
lwagner94 committed Mar 10, 2022
1 parent b26afdc commit 8701315
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 50 deletions.
39 changes: 0 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,21 @@ clap = {version = "3.0.0", features=["cargo", "derive"]}
object = { version = "0.28.1", features = ["read", "wasm"]}
addr2line = "0.17.0"
gimli = "0.26.1"

toml = "0.5.0"
serde = { version = "1.0", features = ["derive"] }

colored = "2.0.0"
log = "0.4.0"
env_logger = "0.9.0"
num_cpus = "1.13.1"
indicatif = {version = "0.16.2", features = ["rayon"]}
rand = "0.8.4"

syntect = "4.6.0"

handlebars = "4.2.0"
md5 = "0.7.0"
concat-idents = "1.1.3"
chrono = "0.4.19"
atomic-counter = "1.0.1"
dyn-clone = "1.0.4"
wabt = "0.10.0"

[dev-dependencies]
tempfile = "3.3.0"
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ fn main() {
Builder::new()
.filter_level(LevelFilter::Info)
.format_timestamp(None)
// .format_target(false)
.format_target(false)
.filter_module("wasmer_wasi", LevelFilter::Warn)
.filter_module("regalloc", LevelFilter::Warn)
.filter_module("cranelift_codegen", LevelFilter::Warn)
Expand Down
67 changes: 63 additions & 4 deletions src/wasmmodule.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::HashSet};
use std::{borrow::Cow, collections::HashSet, path::Path};

use crate::{
addressresolver::AddressResolver,
Expand Down Expand Up @@ -171,15 +171,17 @@ impl<'a> WasmModule<'a> {
self.fix_tables();
self.fix_exports();

// binary operators have two params, so we need to save at least two parameters
let number_of_saved_params = self.max_number_of_params_of_same_type().max(2);
let global_section = self.get_or_create_global_section();

// TODO: Make number_of_saved_params adaptive
let parameter_saver = ParameterSaver::new(30, global_section.entries_mut());
let parameter_saver =
ParameterSaver::new(number_of_saved_params, global_section.entries_mut());

let bodies = self
.module
.code_section_mut()
.context("Module does not have a code section")? // TODO: Error handling?
.context("Module does not have a code section")?
.bodies_mut();

let mut locations = locations.to_vec();
Expand Down Expand Up @@ -484,6 +486,40 @@ impl<'a> WasmModule<'a> {
}
}

/// Goes through the type signatures and get the maximum number of params of the same type
fn max_number_of_params_of_same_type(&self) -> usize {
let type_section = self
.module
.type_section()
.expect("module does not have a type section");

let mut max = 0;

for Type::Function(t) in type_section.types() {
let mut i32_params = 0;
let mut i64_params = 0;
let mut f32_params = 0;
let mut f64_params = 0;

for param in t.params() {
match param {
ValueType::I32 => i32_params += 1,
ValueType::I64 => i64_params += 1,
ValueType::F32 => f32_params += 1,
ValueType::F64 => f64_params += 1,
}
}

max = max
.max(i32_params)
.max(i64_params)
.max(f32_params)
.max(f64_params);
}

max
}

/// Serialize module
///
/// Debug information that may have been present in the original module
Expand All @@ -509,6 +545,15 @@ impl<'a> WasmModule<'a> {
pub fn path(&self) -> &str {
&self.path
}

#[allow(dead_code)]
pub fn dump<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let bytes = self.to_bytes()?;

std::fs::write(path, bytes)?;

Ok(())
}
}

fn generate_mutant_sequence(
Expand Down Expand Up @@ -869,4 +914,18 @@ mod tests {
assert_eq!(function_index, 0);
Ok(())
}

#[test]
fn max_number_of_params_of_same_type() -> Result<()> {
let module = WasmModule::from_file("testdata/factorial/test.wasm")?;
assert_eq!(module.max_number_of_params_of_same_type(), 1);

let module = WasmModule::from_file("testdata/simple_add/test.wasm")?;
assert_eq!(module.max_number_of_params_of_same_type(), 2);

let module = WasmModule::from_file("testdata/simple_add64/test.wasm")?;
assert_eq!(module.max_number_of_params_of_same_type(), 2);

Ok(())
}
}

0 comments on commit 8701315

Please sign in to comment.