Skip to content

Commit 5a8e46e

Browse files
authored
Take typed Wasm indices in FuncTranslationState::get_* methods (#11355)
These methods get-or-create various entities given some kind of Wasm index. Those Wasm indices were previously passed as `u32`s, but are now typed indices (e.g. `MemoryIndex` instead of `u32`). This makes it more difficult to pass the wrong index to these methods. I also removed some unused arguments from a few `FuncEnvironment` methods that the results of `FuncTranslationState::get_*` calls were feeding into. These were left over from when `FuncEnvironment` was a trait and not a single type.
1 parent 587ca6f commit 5a8e46e

File tree

3 files changed

+58
-79
lines changed

3 files changed

+58
-79
lines changed

crates/cranelift/src/func_environ.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,7 +2823,6 @@ impl FuncEnvironment<'_> {
28232823
&mut self,
28242824
builder: &mut FunctionBuilder<'_>,
28252825
index: MemoryIndex,
2826-
_heap: Heap,
28272826
val: ir::Value,
28282827
) -> WasmResult<ir::Value> {
28292828
let mut pos = builder.cursor();
@@ -2855,7 +2854,6 @@ impl FuncEnvironment<'_> {
28552854
&mut self,
28562855
mut pos: FuncCursor<'_>,
28572856
index: MemoryIndex,
2858-
_heap: Heap,
28592857
) -> WasmResult<ir::Value> {
28602858
let pointer_type = self.pointer_type();
28612859
let vmctx = self.vmctx(&mut pos.func);
@@ -2940,9 +2938,7 @@ impl FuncEnvironment<'_> {
29402938
&mut self,
29412939
builder: &mut FunctionBuilder<'_>,
29422940
src_index: MemoryIndex,
2943-
_src_heap: Heap,
29442941
dst_index: MemoryIndex,
2945-
_dst_heap: Heap,
29462942
dst: ir::Value,
29472943
src: ir::Value,
29482944
len: ir::Value,
@@ -2977,7 +2973,6 @@ impl FuncEnvironment<'_> {
29772973
&mut self,
29782974
builder: &mut FunctionBuilder<'_>,
29792975
memory_index: MemoryIndex,
2980-
_heap: Heap,
29812976
dst: ir::Value,
29822977
val: ir::Value,
29832978
len: ir::Value,
@@ -3001,7 +2996,6 @@ impl FuncEnvironment<'_> {
30012996
&mut self,
30022997
builder: &mut FunctionBuilder<'_>,
30032998
memory_index: MemoryIndex,
3004-
_heap: Heap,
30052999
seg_index: u32,
30063000
dst: ir::Value,
30073001
src: ir::Value,
@@ -3366,12 +3360,12 @@ impl FuncEnvironment<'_> {
33663360
pub fn update_global(
33673361
&mut self,
33683362
builder: &mut FunctionBuilder,
3369-
global_index: u32,
3363+
global_index: GlobalIndex,
33703364
value: ir::Value,
33713365
) {
33723366
#[cfg(feature = "wmemcheck")]
33733367
if self.compiler.wmemcheck {
3374-
if global_index == 0 {
3368+
if global_index.index() == 0 {
33753369
// We are making the assumption that global 0 is the auxiliary stack pointer.
33763370
let update_stack_pointer =
33773371
self.builtin_functions.update_stack_pointer(builder.func);

crates/cranelift/src/translate/code_translator.rs

Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ pub fn translate_operator(
180180
* `get_global` and `set_global` are handled by the environment.
181181
***********************************************************************************/
182182
Operator::GlobalGet { global_index } => {
183-
let val = match state.get_global(builder.func, *global_index, environ)? {
183+
let global_index = GlobalIndex::from_u32(*global_index);
184+
let val = match state.get_global(builder.func, global_index, environ)? {
184185
GlobalVariable::Memory { gv, offset, ty } => {
185186
let addr = builder.ins().global_value(environ.pointer_type(), gv);
186187
let mut flags = ir::MemFlags::trusted();
@@ -194,13 +195,15 @@ pub fn translate_operator(
194195
flags.set_alias_region(Some(ir::AliasRegion::Table));
195196
builder.ins().load(ty, flags, addr, offset)
196197
}
197-
GlobalVariable::Custom => environ
198-
.translate_custom_global_get(builder, GlobalIndex::from_u32(*global_index))?,
198+
GlobalVariable::Custom => {
199+
environ.translate_custom_global_get(builder, global_index)?
200+
}
199201
};
200202
state.push1(val);
201203
}
202204
Operator::GlobalSet { global_index } => {
203-
match state.get_global(builder.func, *global_index, environ)? {
205+
let global_index = GlobalIndex::from_u32(*global_index);
206+
match state.get_global(builder.func, global_index, environ)? {
204207
GlobalVariable::Memory { gv, offset, ty } => {
205208
let addr = builder.ins().global_value(environ.pointer_type(), gv);
206209
let mut flags = ir::MemFlags::trusted();
@@ -217,15 +220,11 @@ pub fn translate_operator(
217220
}
218221
debug_assert_eq!(ty, builder.func.dfg.value_type(val));
219222
builder.ins().store(flags, val, addr, offset);
220-
environ.update_global(builder, *global_index, val);
223+
environ.update_global(builder, global_index, val);
221224
}
222225
GlobalVariable::Custom => {
223226
let val = state.pop1();
224-
environ.translate_custom_global_set(
225-
builder,
226-
GlobalIndex::from_u32(*global_index),
227-
val,
228-
)?;
227+
environ.translate_custom_global_set(builder, global_index, val)?;
229228
}
230229
}
231230
}
@@ -623,7 +622,8 @@ pub fn translate_operator(
623622
* argument referring to an index in the external functions table of the module.
624623
************************************************************************************/
625624
Operator::Call { function_index } => {
626-
let (fref, num_args) = state.get_direct_func(builder.func, *function_index, environ)?;
625+
let function_index = FuncIndex::from_u32(*function_index);
626+
let (fref, num_args) = state.get_direct_func(builder.func, function_index, environ)?;
627627

628628
// Bitcast any vector arguments to their default type, I8X16, before calling.
629629
let args = state.peekn_mut(num_args);
@@ -634,12 +634,7 @@ pub fn translate_operator(
634634
builder,
635635
);
636636

637-
let call = environ.translate_call(
638-
builder,
639-
FuncIndex::from_u32(*function_index),
640-
fref,
641-
args,
642-
)?;
637+
let call = environ.translate_call(builder, function_index, fref, args)?;
643638
let inst_results = builder.inst_results(call);
644639
debug_assert_eq!(
645640
inst_results.len(),
@@ -658,7 +653,8 @@ pub fn translate_operator(
658653
// `type_index` is the index of the function's signature and
659654
// `table_index` is the index of the table to search the function
660655
// in.
661-
let (sigref, num_args) = state.get_indirect_sig(builder.func, *type_index, environ)?;
656+
let type_index = TypeIndex::from_u32(*type_index);
657+
let (sigref, num_args) = state.get_indirect_sig(builder.func, type_index, environ)?;
662658
let callee = state.pop1();
663659

664660
// Bitcast any vector arguments to their default type, I8X16, before calling.
@@ -669,7 +665,7 @@ pub fn translate_operator(
669665
builder,
670666
validator.features(),
671667
TableIndex::from_u32(*table_index),
672-
TypeIndex::from_u32(*type_index),
668+
type_index,
673669
sigref,
674670
callee,
675671
state.peekn(num_args),
@@ -698,7 +694,8 @@ pub fn translate_operator(
698694
* VM's runtime state via tables.
699695
************************************************************************************/
700696
Operator::ReturnCall { function_index } => {
701-
let (fref, num_args) = state.get_direct_func(builder.func, *function_index, environ)?;
697+
let function_index = FuncIndex::from_u32(*function_index);
698+
let (fref, num_args) = state.get_direct_func(builder.func, function_index, environ)?;
702699

703700
// Bitcast any vector arguments to their default type, I8X16, before calling.
704701
let args = state.peekn_mut(num_args);
@@ -709,12 +706,7 @@ pub fn translate_operator(
709706
builder,
710707
);
711708

712-
environ.translate_return_call(
713-
builder,
714-
FuncIndex::from_u32(*function_index),
715-
fref,
716-
args,
717-
)?;
709+
environ.translate_return_call(builder, function_index, fref, args)?;
718710

719711
state.popn(num_args);
720712
state.reachable = false;
@@ -726,7 +718,8 @@ pub fn translate_operator(
726718
// `type_index` is the index of the function's signature and
727719
// `table_index` is the index of the table to search the function
728720
// in.
729-
let (sigref, num_args) = state.get_indirect_sig(builder.func, *type_index, environ)?;
721+
let type_index = TypeIndex::from_u32(*type_index);
722+
let (sigref, num_args) = state.get_indirect_sig(builder.func, type_index, environ)?;
730723
let callee = state.pop1();
731724

732725
// Bitcast any vector arguments to their default type, I8X16, before calling.
@@ -737,7 +730,7 @@ pub fn translate_operator(
737730
builder,
738731
validator.features(),
739732
TableIndex::from_u32(*table_index),
740-
TypeIndex::from_u32(*type_index),
733+
type_index,
741734
sigref,
742735
callee,
743736
state.peekn(num_args),
@@ -750,7 +743,8 @@ pub fn translate_operator(
750743
// Get function signature
751744
// `index` is the index of the function's signature and `table_index` is the index of
752745
// the table to search the function in.
753-
let (sigref, num_args) = state.get_indirect_sig(builder.func, *type_index, environ)?;
746+
let type_index = TypeIndex::from_u32(*type_index);
747+
let (sigref, num_args) = state.get_indirect_sig(builder.func, type_index, environ)?;
754748
let callee = state.pop1();
755749

756750
// Bitcast any vector arguments to their default type, I8X16, before calling.
@@ -769,16 +763,16 @@ pub fn translate_operator(
769763
Operator::MemoryGrow { mem } => {
770764
// The WebAssembly MVP only supports one linear memory, but we expect the reserved
771765
// argument to be a memory index.
772-
let heap_index = MemoryIndex::from_u32(*mem);
773-
let heap = state.get_heap(builder.func, *mem, environ)?;
766+
let mem = MemoryIndex::from_u32(*mem);
767+
let _heap = state.get_heap(builder.func, mem, environ)?;
774768
let val = state.pop1();
775-
environ.before_memory_grow(builder, val, heap_index);
776-
state.push1(environ.translate_memory_grow(builder, heap_index, heap, val)?)
769+
environ.before_memory_grow(builder, val, mem);
770+
state.push1(environ.translate_memory_grow(builder, mem, val)?)
777771
}
778772
Operator::MemorySize { mem } => {
779-
let heap_index = MemoryIndex::from_u32(*mem);
780-
let heap = state.get_heap(builder.func, *mem, environ)?;
781-
state.push1(environ.translate_memory_size(builder.cursor(), heap_index, heap)?);
773+
let mem = MemoryIndex::from_u32(*mem);
774+
let _heap = state.get_heap(builder.func, mem, environ)?;
775+
state.push1(environ.translate_memory_size(builder.cursor(), mem)?);
782776
}
783777
/******************************* Load instructions ***********************************
784778
* Wasm specifies an integer alignment flag but we drop it in Cranelift.
@@ -1282,8 +1276,8 @@ pub fn translate_operator(
12821276
Operator::MemoryAtomicWait32 { .. } => I32,
12831277
_ => unreachable!(),
12841278
};
1285-
let heap_index = MemoryIndex::from_u32(memarg.memory);
1286-
let heap = state.get_heap(builder.func, memarg.memory, environ)?;
1279+
let memory_index = MemoryIndex::from_u32(memarg.memory);
1280+
let heap = state.get_heap(builder.func, memory_index, environ)?;
12871281
let timeout = state.pop1(); // 64 (fixed)
12881282
let expected = state.pop1(); // 32 or 64 (per the `Ixx` in `IxxAtomicWait`)
12891283
assert!(builder.func.dfg.value_type(expected) == implied_ty);
@@ -1299,7 +1293,7 @@ pub fn translate_operator(
12991293
// code it needs to generate, if it wants.
13001294
let res = environ.translate_atomic_wait(
13011295
builder,
1302-
heap_index,
1296+
memory_index,
13031297
heap,
13041298
effective_addr,
13051299
expected,
@@ -1308,8 +1302,8 @@ pub fn translate_operator(
13081302
state.push1(res);
13091303
}
13101304
Operator::MemoryAtomicNotify { memarg } => {
1311-
let heap_index = MemoryIndex::from_u32(memarg.memory);
1312-
let heap = state.get_heap(builder.func, memarg.memory, environ)?;
1305+
let memory_index = MemoryIndex::from_u32(memarg.memory);
1306+
let heap = state.get_heap(builder.func, memory_index, environ)?;
13131307
let count = state.pop1(); // 32 (fixed)
13141308
let addr = state.pop1();
13151309
let effective_addr = if memarg.offset == 0 {
@@ -1321,7 +1315,7 @@ pub fn translate_operator(
13211315
};
13221316
let res = environ.translate_atomic_notify(
13231317
builder,
1324-
heap_index,
1318+
memory_index,
13251319
heap,
13261320
effective_addr,
13271321
count,
@@ -1531,39 +1525,31 @@ pub fn translate_operator(
15311525
}
15321526
Operator::MemoryCopy { src_mem, dst_mem } => {
15331527
let src_index = MemoryIndex::from_u32(*src_mem);
1528+
let _src_heap = state.get_heap(builder.func, src_index, environ)?;
1529+
15341530
let dst_index = MemoryIndex::from_u32(*dst_mem);
1535-
let src_heap = state.get_heap(builder.func, *src_mem, environ)?;
1536-
let dst_heap = state.get_heap(builder.func, *dst_mem, environ)?;
1531+
let _dst_heap = state.get_heap(builder.func, dst_index, environ)?;
1532+
15371533
let len = state.pop1();
15381534
let src_pos = state.pop1();
15391535
let dst_pos = state.pop1();
1540-
environ.translate_memory_copy(
1541-
builder, src_index, src_heap, dst_index, dst_heap, dst_pos, src_pos, len,
1542-
)?;
1536+
environ.translate_memory_copy(builder, src_index, dst_index, dst_pos, src_pos, len)?;
15431537
}
15441538
Operator::MemoryFill { mem } => {
1545-
let heap_index = MemoryIndex::from_u32(*mem);
1546-
let heap = state.get_heap(builder.func, *mem, environ)?;
1539+
let mem = MemoryIndex::from_u32(*mem);
1540+
let _heap = state.get_heap(builder.func, mem, environ)?;
15471541
let len = state.pop1();
15481542
let val = state.pop1();
15491543
let dest = state.pop1();
1550-
environ.translate_memory_fill(builder, heap_index, heap, dest, val, len)?;
1544+
environ.translate_memory_fill(builder, mem, dest, val, len)?;
15511545
}
15521546
Operator::MemoryInit { data_index, mem } => {
1553-
let heap_index = MemoryIndex::from_u32(*mem);
1554-
let heap = state.get_heap(builder.func, *mem, environ)?;
1547+
let mem = MemoryIndex::from_u32(*mem);
1548+
let _heap = state.get_heap(builder.func, mem, environ)?;
15551549
let len = state.pop1();
15561550
let src = state.pop1();
15571551
let dest = state.pop1();
1558-
environ.translate_memory_init(
1559-
builder,
1560-
heap_index,
1561-
heap,
1562-
*data_index,
1563-
dest,
1564-
src,
1565-
len,
1566-
)?;
1552+
environ.translate_memory_init(builder, mem, *data_index, dest, src, len)?;
15671553
}
15681554
Operator::DataDrop { data_index } => {
15691555
environ.translate_data_drop(builder.cursor(), *data_index)?;
@@ -2498,7 +2484,8 @@ pub fn translate_operator(
24982484
// Get function signature
24992485
// `index` is the index of the function's signature and `table_index` is the index of
25002486
// the table to search the function in.
2501-
let (sigref, num_args) = state.get_indirect_sig(builder.func, *type_index, environ)?;
2487+
let type_index = TypeIndex::from_u32(*type_index);
2488+
let (sigref, num_args) = state.get_indirect_sig(builder.func, type_index, environ)?;
25022489
let callee = state.pop1();
25032490

25042491
// Bitcast any vector arguments to their default type, I8X16, before calling.
@@ -3224,7 +3211,9 @@ fn prepare_addr(
32243211
environ: &mut FuncEnvironment<'_>,
32253212
) -> WasmResult<Reachability<(MemFlags, Value, Value)>> {
32263213
let index = state.pop1();
3227-
let heap = state.get_heap(builder.func, memarg.memory, environ)?;
3214+
3215+
let memory_index = MemoryIndex::from_u32(memarg.memory);
3216+
let heap = state.get_heap(builder.func, memory_index, environ)?;
32283217

32293218
// How exactly the bounds check is performed here and what it's performed
32303219
// on is a bit tricky. Generally we want to rely on access violations (e.g.

crates/cranelift/src/translate/state.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,9 @@ impl FuncTranslationState {
470470
pub(crate) fn get_global(
471471
&mut self,
472472
func: &mut ir::Function,
473-
index: u32,
473+
index: GlobalIndex,
474474
environ: &mut FuncEnvironment<'_>,
475475
) -> WasmResult<GlobalVariable> {
476-
let index = GlobalIndex::from_u32(index);
477476
match self.globals[index] {
478477
Some(g) => Ok(g),
479478
None => {
@@ -489,10 +488,9 @@ impl FuncTranslationState {
489488
pub(crate) fn get_heap(
490489
&mut self,
491490
func: &mut ir::Function,
492-
index: u32,
491+
index: MemoryIndex,
493492
environ: &mut FuncEnvironment<'_>,
494493
) -> WasmResult<Heap> {
495-
let index = MemoryIndex::from_u32(index);
496494
match self.memory_to_heap[index].expand() {
497495
Some(heap) => Ok(heap),
498496
None => {
@@ -510,10 +508,9 @@ impl FuncTranslationState {
510508
pub(crate) fn get_indirect_sig(
511509
&mut self,
512510
func: &mut ir::Function,
513-
index: u32,
511+
index: TypeIndex,
514512
environ: &mut FuncEnvironment<'_>,
515513
) -> WasmResult<(ir::SigRef, usize)> {
516-
let index = TypeIndex::from_u32(index);
517514
match self.signatures[index] {
518515
Some((sig, num_params)) => Ok((sig, num_params)),
519516
None => {
@@ -532,10 +529,9 @@ impl FuncTranslationState {
532529
pub(crate) fn get_direct_func(
533530
&mut self,
534531
func: &mut ir::Function,
535-
index: u32,
532+
index: FuncIndex,
536533
environ: &mut FuncEnvironment<'_>,
537534
) -> WasmResult<(ir::FuncRef, usize)> {
538-
let index = FuncIndex::from_u32(index);
539535
match self.functions[index] {
540536
Some((fref, num_args)) => Ok((fref, num_args)),
541537
None => {

0 commit comments

Comments
 (0)