Skip to content

Commit

Permalink
Improve the api for ModuleInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
kper committed Sep 4, 2021
1 parent fd945f5 commit 664cf5a
Show file tree
Hide file tree
Showing 56 changed files with 503 additions and 439 deletions.
17 changes: 0 additions & 17 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Expand Up @@ -9,7 +9,7 @@ members = [
"wasm_parser",
"validation",
"testrunner2",
"debugger",
#"debugger",
"custom_display"
]
exclude = [
Expand All @@ -27,9 +27,9 @@ name = "funky"
path = "testrunner2/src/main.rs"
name = "testrunner2"

[[bin]]
path = "debugger/src/main.rs"
name = "debugger"
#[[bin]]
#path = "debugger/src/main.rs"
#name = "debugger"

[[bin]]
path = "wasm_parser/src/main.rs"
Expand Down
8 changes: 1 addition & 7 deletions README.md
Expand Up @@ -68,15 +68,9 @@ SUBCOMMANDS:
cargo run --bin funky -- ./testsuite/block.0.wasm "break-bare"
```

## Run the debugger

```
cargo run --bin debugger -- ./testsuite/block.0.wasm "break-bare"
```

## Wait, there is more

You will find a custom debugger in `debugger` and the taint analysis in the `ifds` folder.
You will find a taint analysis in the `ifds` folder.

## Current spec coverage

Expand Down
5 changes: 3 additions & 2 deletions debugger/src/main.rs
Expand Up @@ -81,7 +81,8 @@ fn main() -> Result<()> {

let args_function_cpy = args.arg_function;

let copy = e.lock().unwrap().module.code.clone();
let lock = e.lock().unwrap();
let copy = lock.module.get_code().clone();

let engine = e;

Expand Down Expand Up @@ -117,7 +118,7 @@ fn main() -> Result<()> {

let mut state = None;

let functions: Vec<_> = copy.into_iter().map(|w| w.code).flatten().collect();
let functions: Vec<_> = copy.into_iter().map(|w| w.code.clone()).flatten().collect();
let instructions = get_instructions(&functions); //expands the blocks

let mut stateful = StatefulList::with_items(instructions);
Expand Down
61 changes: 26 additions & 35 deletions src/allocation.rs
Expand Up @@ -32,7 +32,7 @@ pub fn allocate(
.context("Allocating table instances failed")?;

// Step 4a and 8
allocate_memories(m, mod_instance, store);
allocate_memories(m, mod_instance, store)?;

// Step 5a and 9
allocate_globals(m, mod_instance, store, &imports)
Expand All @@ -42,7 +42,7 @@ pub fn allocate(

// Step 14.

allocate_exports(m, mod_instance, store);
allocate_exports(m, mod_instance, store)?;

// Step 15.

Expand Down Expand Up @@ -105,7 +105,7 @@ fn allocate_functions(
// Allocate function

let borrow = &mod_instance;
let fn_sig = match borrow.fn_types.get(*t as usize) {
let fn_sig = match borrow.lookup_func_types(t) {
Some(sig) => sig,
None => {
return Err(anyhow!("{} function type is not defined", t));
Expand All @@ -118,7 +118,7 @@ fn allocate_functions(
if code_index < 0 {
None
} else {
borrow.code.get(code_index as usize)
borrow.lookup_code(code_index as usize)
}
};

Expand Down Expand Up @@ -171,16 +171,10 @@ fn allocate_functions(

store.allocate_func_instance(fn_sig.clone(), fcode);

mod_instance
.funcaddrs
.push(FuncAddr::new(store.count_functions() as u32 - 1));
let addr = FuncAddr::new(store.count_functions() - 1);
mod_instance.store_func_addr(addr)?;
}

/*
for func in imports {
mod_instance.funcaddrs.push(FuncAddr::new(*func));
}*/

Ok(())
}

Expand All @@ -204,7 +198,8 @@ fn allocate_tables(
Limits::One(n, m) => TableInstance::new(n, Some(m)),
};

mod_instance.tableaddrs.push(store.tables.len() as u32);
let addr = TableAddr::new(store.tables.len());
mod_instance.store_table_addr(addr)?;
store.tables.push(instance);
}

Expand All @@ -213,25 +208,25 @@ fn allocate_tables(
let instance = import_resolver.resolve_table(&entry.module_name, &entry.name)?;
debug!("table {:#?}", instance);

mod_instance.tableaddrs.push(store.tables.len() as u32);
let addr = TableAddr::new(store.tables.len());
mod_instance.store_table_addr(addr)?;
store.tables.push(instance.clone());
}
}

debug!("Tables in mod_i {:?}", mod_instance.tableaddrs);
debug!("Tables in store {:#?}", store.tables);

Ok(())
}

fn allocate_memories(m: &Module, mod_instance: &mut ModuleInstance, store: &mut Store) {
fn allocate_memories(m: &Module, mod_instance: &mut ModuleInstance, store: &mut Store) -> Result<()> {
debug!("allocate memories");
// Gets all memories and imports
let ty = validation::extract::get_mems(&m);

for memtype in ty.iter() {
debug!("memtype {:#?}", memtype);
let instance = match memtype.limits {
for mem_type in ty.iter() {
debug!("mem_type {:#?}", mem_type);
let instance = match mem_type.limits {
Limits::Zero(n) => MemoryInstance {
data: vec![0u8; (n * 1024 * 64) as usize],
max: None,
Expand All @@ -242,12 +237,13 @@ fn allocate_memories(m: &Module, mod_instance: &mut ModuleInstance, store: &mut
},
};

mod_instance.memaddrs.push(store.memory.len() as u32);
let addr = MemoryAddr::new(store.memory.len());
mod_instance.store_memory_addr(addr)?;
store.memory.push(instance);
}

debug!("Memories in mod_i {:?}", mod_instance.memaddrs);
debug!("Memories in store {:#?}", store.memory);
Ok(())
}

fn allocate_globals(
Expand All @@ -274,30 +270,27 @@ fn allocate_globals(
val: get_expr_const_ty_global(&gl.init, &mod_instance, store)?,
};

mod_instance
.globaladdrs
.push(GlobalAddr::new(store.globals.len() as u32));
let addr = GlobalAddr::new(store.globals.len());
mod_instance.store_global_addr(addr)?;
store.globals.push(instance);
}

for gl in imported_globals.iter() {
debug!("global {:#?}", gl);

mod_instance
.globaladdrs
.push(GlobalAddr::new(store.globals.len() as u32));
let addr = GlobalAddr::new(store.globals.len());
mod_instance.store_global_addr(addr)?;
store
.globals
.push(imports.resolve_global(&gl.module_name, &gl.name)?);
}

debug!("Globals in mod_i {:?}", mod_instance.globaladdrs);
debug!("Globals in store {:#?}", store.globals);

Ok(())
}

fn allocate_exports(m: &Module, mod_instance: &mut ModuleInstance, _store: &mut Store) {
fn allocate_exports(m: &Module, mod_instance: &mut ModuleInstance, _store: &mut Store) -> Result<()> {
debug!("allocate exports");

// Gets all exports
Expand All @@ -306,10 +299,10 @@ fn allocate_exports(m: &Module, mod_instance: &mut ModuleInstance, _store: &mut
for export in ty.into_iter() {
debug!("Export {:?}", export);

mod_instance.exports.push(export.into());
mod_instance.store_export(export.into())?;
}

debug!("Exports in mod_i {:?}", mod_instance.exports);
Ok(())
}

pub(crate) fn get_expr_const_ty_global(
Expand All @@ -334,10 +327,8 @@ pub(crate) fn get_expr_const_ty_global(
OP_F32_CONST(v) => Ok(Value::F32(*v)),
OP_F64_CONST(v) => Ok(Value::F64(*v)),
OP_GLOBAL_GET(idx) => {
let addr = mod_instance
.globaladdrs
.get(*idx as usize)
.ok_or_else(|| anyhow!("Cannot find global addr by index"))?;
let addr = mod_instance.lookup_global_addr(idx)
.context("Cannot find global addr by index")?;
let global_instance = store.get_global_instance(addr)?;

Ok(global_instance.val)
Expand Down

0 comments on commit 664cf5a

Please sign in to comment.