Skip to content

Commit 586361d

Browse files
authored
fix(cranelift): fix get_function_name reference lookup (#5888)
* fix(cranelift): fix get_function_name reference lookup Use sparse index for ir::ExternalName::user as UserExternalNameRef are references to the user_named_funcs vector. We can then get to the original FunctionIndex by a proper lookup to the user_named_funcs.
1 parent 80ccb04 commit 586361d

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

lib/compiler-cranelift/src/compiler.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ impl CraneliftCompiler {
173173

174174
let mut code_buf: Vec<u8> = Vec::new();
175175
let mut ctrl_plane = Default::default();
176+
let func_name_map = context.func.params.user_named_funcs().clone();
176177
let result = context
177178
.compile(&*isa, &mut ctrl_plane)
178179
.map_err(|error| CompileError::Codegen(error.inner.to_string()))?;
@@ -182,7 +183,7 @@ impl CraneliftCompiler {
182183
.buffer
183184
.relocs()
184185
.into_iter()
185-
.map(|r| mach_reloc_to_reloc(module, r))
186+
.map(|r| mach_reloc_to_reloc(module, &func_name_map, r))
186187
.collect::<Vec<_>>();
187188

188189
let traps = result
@@ -297,6 +298,7 @@ impl CraneliftCompiler {
297298

298299
let mut code_buf: Vec<u8> = Vec::new();
299300
let mut ctrl_plane = Default::default();
301+
let func_name_map = context.func.params.user_named_funcs().clone();
300302
let result = context
301303
.compile(&*isa, &mut ctrl_plane)
302304
.map_err(|error| CompileError::Codegen(format!("{error:#?}")))?;
@@ -315,7 +317,7 @@ impl CraneliftCompiler {
315317
.buffer
316318
.relocs()
317319
.iter()
318-
.map(|r| mach_reloc_to_reloc(module, r))
320+
.map(|r| mach_reloc_to_reloc(module, &func_name_map, r))
319321
.collect::<Vec<_>>();
320322

321323
let traps = result
@@ -524,7 +526,11 @@ impl Compiler for CraneliftCompiler {
524526
}
525527
}
526528

527-
fn mach_reloc_to_reloc(module: &ModuleInfo, reloc: &FinalizedMachReloc) -> Relocation {
529+
fn mach_reloc_to_reloc(
530+
module: &ModuleInfo,
531+
func_index_map: &cranelift_entity::PrimaryMap<ir::UserExternalNameRef, ir::UserExternalName>,
532+
reloc: &FinalizedMachReloc,
533+
) -> Relocation {
528534
let FinalizedMachReloc {
529535
offset,
530536
kind,
@@ -538,10 +544,11 @@ fn mach_reloc_to_reloc(module: &ModuleInfo, reloc: &FinalizedMachReloc) -> Reloc
538544
}
539545
};
540546
let reloc_target: RelocationTarget = if let ExternalName::User(extname_ref) = name {
547+
let func_index = func_index_map[*extname_ref].index;
541548
//debug_assert_eq!(namespace, 0);
542549
RelocationTarget::LocalFunc(
543550
module
544-
.local_func_index(FunctionIndex::from_u32(extname_ref.as_u32()))
551+
.local_func_index(FunctionIndex::from_u32(func_index))
545552
.expect("The provided function should be local"),
546553
)
547554
} else if let ExternalName::LibCall(libcall) = name {

lib/compiler-cranelift/src/func_environ.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ use wasmer_types::{
2929

3030
/// Compute an `ir::ExternalName` for a given wasm function index.
3131
pub fn get_function_name(func: &mut Function, func_index: FunctionIndex) -> ir::ExternalName {
32-
// Called as the function name offset is used for debugging purpose.
33-
func.params
34-
.ensure_user_func_name(UserExternalName::new(0, func_index.as_u32()));
35-
ir::ExternalName::user(ir::UserExternalNameRef::from_u32(func_index.as_u32()))
32+
ir::ExternalName::user(
33+
func.params
34+
.ensure_user_func_name(UserExternalName::new(0, func_index.as_u32())),
35+
)
3636
}
3737

3838
/// The type of the `current_elements` field.

tests/compilers/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ impl Config {
7070
&self,
7171
#[allow(unused_variables)] canonicalize_nans: bool,
7272
) -> Box<dyn CompilerConfig> {
73-
let debug_dir = std::env::var("WASMER_DEBUG_DIR").ok().map(PathBuf::from);
73+
let debug_dir = std::env::var("WASMER_COMPILER_DEBUG_DIR")
74+
.ok()
75+
.map(PathBuf::from);
7476

7577
match &self.compiler {
7678
#[cfg(feature = "cranelift")]

tests/compilers/issues.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! This file is mainly to assure specific issues are working well
2+
use std::env;
3+
24
use anyhow::{Context, Result};
35
use itertools::Itertools;
6+
use tempfile::TempDir;
47
use wasmer::FunctionEnv;
58
use wasmer::*;
69

@@ -587,3 +590,26 @@ fn huge_number_of_arguments_fn(
587590

588591
Ok(())
589592
}
593+
594+
#[compiler_test(issues)]
595+
fn compiler_debug_dir_test(mut config: crate::Config) {
596+
let temp = TempDir::new().expect("temp folder creation failed");
597+
unsafe {
598+
env::set_var(
599+
"WASMER_COMPILER_DEBUG_DIR",
600+
temp.path()
601+
.as_os_str()
602+
.to_str()
603+
.expect("path must be valid"),
604+
);
605+
}
606+
let store = config.store();
607+
608+
let mut wat = include_str!("../wast/spec/fac.wast").to_string();
609+
wat.truncate(
610+
wat.find("(assert_return")
611+
.expect("assert expected in the test"),
612+
);
613+
614+
assert!(Module::new(&store, wat).is_ok());
615+
}

0 commit comments

Comments
 (0)