Skip to content

Commit ab1d874

Browse files
committed
fix: account for global offset in import calculations
1 parent d057c14 commit ab1d874

File tree

6 files changed

+53
-13
lines changed

6 files changed

+53
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exclude = ["StarlingMonkey/crates/rust-url"]
44
resolver = "2"
55

66
[workspace.package]
7-
edition = "2021"
7+
edition = "2024"
88
version = "0.1.0"
99

1010
[workspace.lints.clippy]

crates/spidermonkey-embedding-splicer/src/bindgen.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use heck::*;
66
use js_component_bindgen::function_bindgen::{
77
ErrHandling, FunctionBindgen, ResourceData, ResourceMap, ResourceTable,
88
};
9-
use js_component_bindgen::intrinsics::{render_intrinsics, Intrinsic};
9+
use js_component_bindgen::intrinsics::{Intrinsic, render_intrinsics};
1010
use js_component_bindgen::names::LocalNames;
1111
use js_component_bindgen::source::Source;
1212
use wit_bindgen_core::abi::{self, LiftLower};
@@ -1238,9 +1238,15 @@ impl EsmBindgen {
12381238
"verifyInterfaceFn"
12391239
};
12401240
if let Some(alias) = self.export_aliases.get(export_name) {
1241-
uwriteln!(bind_exports, "{verify_name}({local_name}, '{export_name}', '{external_name}', '{alias}');");
1241+
uwriteln!(
1242+
bind_exports,
1243+
"{verify_name}({local_name}, '{export_name}', '{external_name}', '{alias}');"
1244+
);
12421245
} else {
1243-
uwriteln!(bind_exports, "{verify_name}({local_name}, '{export_name}', '{external_name}', null);");
1246+
uwriteln!(
1247+
bind_exports,
1248+
"{verify_name}({local_name}, '{export_name}', '{external_name}', null);"
1249+
);
12441250
};
12451251
}
12461252
}

crates/spidermonkey-embedding-splicer/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::Path;
22

3-
use anyhow::{bail, Context, Result};
3+
use anyhow::{Context, Result, bail};
44
use wit_parser::{PackageId, Resolve};
55

66
pub mod bindgen;

crates/spidermonkey-embedding-splicer/src/splice.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ use wasmparser::ExternalKind;
66
use wasmparser::MemArg;
77
use wasmparser::Operator;
88
use wirm::ir::function::{FunctionBuilder, FunctionModifier};
9-
use wirm::ir::id::{ExportsID, FunctionID, LocalID};
9+
use wirm::ir::id::{ExportsID, FunctionID, GlobalID, LocalID};
1010
use wirm::ir::module::Module;
11-
use wirm::ir::types::{BlockType, ElementItems, InstrumentationMode};
11+
use wirm::ir::module::module_globals::GlobalKind;
12+
use wirm::ir::types::{BlockType, ElementItems, InitInstr, InstrumentationMode, Value};
1213
use wirm::module_builder::AddLocal;
1314
use wirm::opcode::{Inject, InjectAt};
1415
use wirm::{DataType, Opcode};
15-
use wit_component::metadata::{decode, Bindgen};
1616
use wit_component::StringEncoding;
17+
use wit_component::metadata::{Bindgen, decode};
1718
use wit_parser::Resolve;
1819

1920
use crate::bindgen::BindingItem;
@@ -757,7 +758,27 @@ fn synthesize_import_functions(
757758
.get_fn_modifier(coreabi_get_import_fid)
758759
.unwrap();
759760

760-
// Find the I32Const base index and compute the delta to new base
761+
// Save the idx parameter to local at the start of the function,
762+
// so that orignal code does not oeverwrit it.
763+
let idx_local = builder.add_local(DataType::I32);
764+
builder.inject_at(
765+
0,
766+
InstrumentationMode::Before,
767+
Operator::LocalGet {
768+
local_index: *arg_idx,
769+
},
770+
);
771+
builder.inject_at(
772+
0,
773+
InstrumentationMode::Before,
774+
Operator::LocalSet {
775+
local_index: *idx_local,
776+
},
777+
);
778+
779+
// Find the I32Const base index and compute the delta to new base.
780+
// The codegen may split the base index into:
781+
// `global.get N` + `i32.const X` where the global is a constant.
761782
let mut table_instr_idx = 0usize;
762783
let mut delta: i32 = 0;
763784
{
@@ -769,7 +790,20 @@ fn synthesize_import_functions(
769790
if *value < 1000 || *value > 5000 {
770791
continue;
771792
}
772-
delta = import_fn_table_start_idx - *value;
793+
// Check if instruction just before is a global.get with a
794+
// constant value and if so, include the global's init value
795+
// in the base computation.
796+
let mut base = *value;
797+
if idx > 0
798+
&& let Operator::GlobalGet { global_index } = &ops_ro[idx - 1]
799+
&& let GlobalKind::Local(local_global) =
800+
module.globals.get_kind(GlobalID(*global_index))
801+
&& let [InitInstr::Value(Value::I32(v))] =
802+
local_global.init_expr.instructions()
803+
{
804+
base += v;
805+
}
806+
delta = import_fn_table_start_idx - base;
773807
table_instr_idx = idx;
774808
break;
775809
}
@@ -780,7 +814,7 @@ fn synthesize_import_functions(
780814
table_instr_idx,
781815
InstrumentationMode::Before,
782816
Operator::LocalGet {
783-
local_index: *arg_idx,
817+
local_index: *idx_local,
784818
},
785819
);
786820
builder.inject_at(

crates/spidermonkey-embedding-splicer/src/stub_wasi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashSet;
22
use std::path::PathBuf;
33
use std::time::{SystemTime, UNIX_EPOCH};
44

5-
use anyhow::{bail, Result};
5+
use anyhow::{Result, bail};
66
use wasmparser::{MemArg, TypeRef};
77
use wirm::ir::function::FunctionBuilder;
88
use wirm::ir::id::{FunctionID, LocalID};

crates/spidermonkey-embedding-splicer/src/wit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{bail, Result};
1+
use anyhow::{Result, bail};
22

33
wit_bindgen::generate!({
44
world: "spidermonkey-embedding-splicer",

0 commit comments

Comments
 (0)