Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 38 additions & 24 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ too_many_arguments = 'allow'
anyhow = { version = "1.0.95", default-features = false }
heck = { version = "0.5", default-features = false }
js-component-bindgen = { version = "1.11.0" }
orca-wasm = { version = "0.9.2", default-features = false }
wirm = { version = "2.1.0", default-features = false }
rand = { version = "0.8", default-features = false }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
wasm-encoder = { version = "0.227.1", features = [ "component-model", "std" ] }
wasmparser = { version = "0.227.1", features = ["features",
wasmparser = { version = "0.239.0", features = ["features",
"component-model",
"hash-collections",
"serde",
Expand Down
3 changes: 2 additions & 1 deletion crates/spidermonkey-embedding-splicer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ anyhow = { workspace = true }
clap = { version = "4.5.31", features = ["suggestions", "color", "derive"] }
heck = { workspace = true }
js-component-bindgen = { workspace = true, features = [ "transpile-bindgen" ] }
orca-wasm = { workspace = true }
wirm = { workspace = true }
rand = { workspace = true }
serde_json = { workspace = true }
wasm-encoder = { workspace = true }
wasmparser = { workspace = true }
wit-bindgen = { workspace = true }
Expand Down
54 changes: 35 additions & 19 deletions crates/spidermonkey-embedding-splicer/src/splice.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::path::PathBuf;

use anyhow::Result;
use orca_wasm::ir::function::{FunctionBuilder, FunctionModifier};
use orca_wasm::ir::id::{ExportsID, FunctionID, LocalID};
use orca_wasm::ir::module::Module;
use orca_wasm::ir::types::{BlockType, ElementItems, InstrumentationMode};
use orca_wasm::module_builder::AddLocal;
use orca_wasm::opcode::{Inject, InjectAt};
use orca_wasm::{DataType, Opcode};
use wasm_encoder::{Encode, Section};
use wasmparser::ExternalKind;
use wasmparser::MemArg;
use wasmparser::Operator;
use wirm::ir::function::{FunctionBuilder, FunctionModifier};
use wirm::ir::id::{ExportsID, FunctionID, LocalID};
use wirm::ir::module::Module;
use wirm::ir::types::{BlockType, ElementItems, InstrumentationMode};
use wirm::module_builder::AddLocal;
use wirm::opcode::{Inject, InjectAt};
use wirm::{DataType, Opcode};
use wit_component::metadata::{decode, Bindgen};
use wit_component::StringEncoding;
use wit_parser::Resolve;
Expand Down Expand Up @@ -725,7 +725,7 @@ fn synthesize_import_functions(

// create imported function table
let els = module.elements.iter_mut().next().unwrap();
if let ElementItems::Functions(ref mut funcs) = &mut els.1 {
if let ElementItems::Functions(ref mut funcs) = &mut els.items {
for fid in import_fnids {
funcs.push(fid);
}
Expand Down Expand Up @@ -757,20 +757,25 @@ fn synthesize_import_functions(
.get_fn_modifier(coreabi_get_import_fid)
.unwrap();

// walk until we get to the const representing the table index
let mut table_instr_idx = 0;
for (idx, instr) in builder.body.instructions.iter_mut().enumerate() {
if let Operator::I32Const { value: ref mut v } = instr.op {
// we specifically need the const "around" 3393
// which is the coreabi_sample_i32 table offset
if *v < 1000 || *v > 5000 {
continue;
// Find the I32Const base index and compute the delta to new base
let mut table_instr_idx = 0usize;
let mut delta: i32 = 0;
{
let ops_ro = builder.body.instructions.get_ops();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should be able to revert the changes to this loop and instead change instructions.iter_mut() to instructions.get_ops().iter_mut() in (the original) line 762.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

builder.body.instructions.get_ops() returns an immutable reference so you can't use iter_mut() on it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. There's a get_ops_mut() that should hopefully work, though

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_ops_mut panics if there are flags on the Instructions, and there are flags

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (idx, op) in ops_ro.iter().enumerate() {
if let Operator::I32Const { value } = op {
// we specifically need the const "around" 3393
// which is the coreabi_sample_i32 table offset
if *value < 1000 || *value > 5000 {
continue;
}
delta = import_fn_table_start_idx - *value;
table_instr_idx = idx;
break;
}
*v = import_fn_table_start_idx;
table_instr_idx = idx;
break;
}
}

builder.inject_at(
table_instr_idx,
InstrumentationMode::Before,
Expand All @@ -783,6 +788,17 @@ fn synthesize_import_functions(
InstrumentationMode::Before,
Operator::I32Add,
);

if delta != 0 {
builder
.body
.instructions
.add_instr(table_instr_idx, Operator::I32Const { value: delta });
builder
.body
.instructions
.add_instr(table_instr_idx, Operator::I32Add);
}
}

// remove unnecessary exports
Expand Down
18 changes: 9 additions & 9 deletions crates/spidermonkey-embedding-splicer/src/stub_wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{bail, Result};
use orca_wasm::ir::function::FunctionBuilder;
use orca_wasm::ir::id::{FunctionID, LocalID};
use orca_wasm::ir::module::module_functions::FuncKind;
use orca_wasm::ir::types::{BlockType, InitExpr, Value};
use orca_wasm::module_builder::AddLocal;
use orca_wasm::{DataType, Instructions, Module, Opcode};
use wasmparser::{MemArg, TypeRef};
use wirm::ir::function::FunctionBuilder;
use wirm::ir::id::{FunctionID, LocalID};
use wirm::ir::module::module_functions::FuncKind;
use wirm::ir::types::{BlockType, InitExpr, Value};
use wirm::module_builder::AddLocal;
use wirm::{DataType, InitInstr, Module, Opcode};
use wit_parser::Resolve;

use crate::parse_wit;
Expand Down Expand Up @@ -44,14 +44,14 @@ where
};

let ty = module.types.get(ty_id).unwrap();
let (params, results) = (ty.params().to_vec(), ty.results().to_vec());
let mut builder = FunctionBuilder::new(params.as_slice(), results.as_slice());
let mut builder = FunctionBuilder::new(ty.params().as_slice(), ty.results().as_slice());
let _args = stub(&mut builder)?;

builder.replace_import_in_module(module, iid);

return Ok(Some(fid));
}

Ok(None)
}

Expand Down Expand Up @@ -206,7 +206,7 @@ fn stub_random(module: &mut Module) -> Result<()> {
// create a mutable random seed global
let seed_val: i64 = 0;
let seed_global = module.add_global(
InitExpr::new(vec![Instructions::Value(Value::I64(seed_val))]),
InitExpr::new(vec![InitInstr::Value(Value::I64(seed_val))]),
DataType::I64,
true,
false,
Expand Down
Loading