Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit 4c75de4

Browse files
authored
Merge pull request #366 from data-pup/that-which-we-call-a-rose
Look at wasm name section
2 parents ad1e6a4 + 5298634 commit 4c75de4

File tree

4 files changed

+71
-19
lines changed

4 files changed

+71
-19
lines changed

lucetc/src/decls.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,30 @@ impl<'a> ModuleDecls<'a> {
117117
for ix in 0..decls.info.functions.len() {
118118
let func_index = UniqueFuncIndex::new(ix);
119119

120+
// Get the name for this function from the module names section, if it exists.
121+
// Because names have to be unique, we append the index value (ix) to the name.
122+
fn custom_name_for<'a>(
123+
ix: usize,
124+
func_index: UniqueFuncIndex,
125+
decls: &mut ModuleDecls<'a>,
126+
) -> Option<String> {
127+
decls
128+
.info
129+
.function_names
130+
.get(func_index)
131+
.map(|s| format!("{}_{}", s, ix))
132+
}
133+
120134
fn export_name_for<'a>(
121135
func_ix: UniqueFuncIndex,
122136
decls: &mut ModuleDecls<'a>,
123137
) -> Option<String> {
124138
let export = decls.info.functions.get(func_ix).unwrap();
125-
126139
if !export.export_names.is_empty() {
127140
decls.exports.push(ExportFunction {
128141
fn_idx: LucetFunctionIndex::from_u32(decls.function_names.len() as u32),
129142
names: export.export_names.clone(),
130143
});
131-
132144
Some(format!("guest_func_{}", export.export_names[0]))
133145
} else {
134146
None
@@ -164,7 +176,6 @@ impl<'a> ModuleDecls<'a> {
164176
// if a function is an export and import, it will not have a real function body
165177
// in this program, and we must not declare it with Linkage::Export (there will
166178
// never be a define to satisfy the symbol!)
167-
168179
decls.declare_function(clif_module, import_sym, Linkage::Import, func_index)?;
169180
}
170181
(None, Some(export_sym)) => {
@@ -173,14 +184,12 @@ impl<'a> ModuleDecls<'a> {
173184
decls.declare_function(clif_module, export_sym, Linkage::Export, func_index)?;
174185
}
175186
(None, None) => {
176-
// No import or export for this function. It's local, and we have to make up a
177-
// name.
178-
decls.declare_function(
179-
clif_module,
180-
format!("guest_func_{}", ix),
181-
Linkage::Local,
182-
func_index,
183-
)?;
187+
// No import or export for this function, which means that it is local. We can
188+
// look for a name provided in the custom names section, otherwise we have to
189+
// make up a placeholder name for it using its index.
190+
let local_sym = custom_name_for(ix, func_index, decls)
191+
.unwrap_or_else(|| format!("guest_func_{}", ix));
192+
decls.declare_function(clif_module, local_sym, Linkage::Local, func_index)?;
184193
}
185194
}
186195
}
@@ -524,9 +533,6 @@ impl<'a> ModuleDecls<'a> {
524533

525534
functions.push(FunctionMetadata {
526535
signature: decl.signature_index,
527-
// TODO: this is a best-effort attempt to figure out a useful name.
528-
// in the future, we should use names from the module names section
529-
// and maybe use export names as a fallback.
530536
name: Some(name.symbol()),
531537
});
532538
}

lucetc/src/module.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Implements ModuleEnvironment for cranelift-wasm. Code derived from cranelift-wasm/environ/dummy.rs
22
use crate::error::{LucetcError, LucetcErrorKind};
33
use crate::pointer::NATIVE_POINTER;
4-
use cranelift_codegen::entity::{entity_impl, EntityRef, PrimaryMap};
4+
use cranelift_codegen::entity::{entity_impl, EntityRef, PrimaryMap, SecondaryMap};
55
use cranelift_codegen::ir;
66
use cranelift_codegen::isa::TargetFrontendConfig;
77
use cranelift_wasm::{
@@ -72,6 +72,8 @@ pub struct ModuleInfo<'a> {
7272
pub function_mapping: PrimaryMap<FuncIndex, UniqueFuncIndex>,
7373
/// Function signatures: imported and local
7474
pub functions: PrimaryMap<UniqueFuncIndex, Exportable<'a, SignatureIndex>>,
75+
/// Function names.
76+
pub function_names: SecondaryMap<UniqueFuncIndex, &'a str>,
7577
/// Provided by `declare_table`
7678
pub tables: PrimaryMap<TableIndex, Exportable<'a, Table>>,
7779
/// Provided by `declare_memory`
@@ -103,6 +105,7 @@ impl<'a> ModuleInfo<'a> {
103105
imported_memories: PrimaryMap::new(),
104106
function_mapping: PrimaryMap::new(),
105107
functions: PrimaryMap::new(),
108+
function_names: SecondaryMap::new(),
106109
tables: PrimaryMap::new(),
107110
memories: PrimaryMap::new(),
108111
globals: PrimaryMap::new(),
@@ -401,4 +404,13 @@ impl<'a> ModuleEnvironment<'a> for ModuleInfo<'a> {
401404
}
402405
Ok(())
403406
}
407+
408+
fn declare_func_name(&mut self, func_index: FuncIndex, name: &'a str) -> WasmResult<()> {
409+
let unique_func_index = *self
410+
.function_mapping
411+
.get(func_index)
412+
.expect("function indices are valid");
413+
self.function_names[unique_func_index] = name;
414+
Ok(())
415+
}
404416
}

lucetc/tests/wasm.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ use lucet_module::bindings::Bindings;
22
use std::collections::HashMap;
33
use std::path::PathBuf;
44

5-
pub fn load_wat_module(name: &str) -> Vec<u8> {
5+
fn load_wat_module(name: &str) -> Vec<u8> {
66
use std::fs::File;
77
use std::io::Read;
8-
use wabt::wat2wasm;
8+
use wabt::Wat2Wasm;
99
let watfile = PathBuf::from(&format!("tests/wasm/{}.wat", name));
1010
let mut contents = Vec::new();
1111
let mut file = File::open(&watfile).expect("open module file");
1212
file.read_to_end(&mut contents).expect("read module file");
13-
wat2wasm(contents).expect("convert module to wasm binary format")
13+
Wat2Wasm::new()
14+
.write_debug_names(true)
15+
.convert(contents)
16+
.expect("convert module to wasm binary format")
17+
.as_ref()
18+
.to_owned()
1419
}
1520

1621
pub fn test_bindings() -> Bindings {
@@ -508,6 +513,32 @@ mod module_data {
508513
);
509514
*/
510515
}
516+
517+
#[test]
518+
fn names_local() {
519+
let m = load_wat_module("names_local");
520+
let b = super::test_bindings();
521+
let h = HeapSettings::default();
522+
let c = Compiler::new(
523+
&m,
524+
OptLevel::default(),
525+
CpuFeatures::default(),
526+
&b,
527+
h,
528+
false,
529+
&None,
530+
)
531+
.expect("compile names_local");
532+
let mdata = c.module_data().unwrap();
533+
534+
assert_eq!(mdata.import_functions().len(), 0);
535+
assert_eq!(mdata.export_functions().len(), 0);
536+
assert_eq!(mdata.function_info().len(), 3);
537+
assert_eq!(
538+
mdata.function_info().get(0).unwrap().name,
539+
Some("func_name_0")
540+
)
541+
}
511542
}
512543

513544
mod compile {
@@ -561,7 +592,6 @@ mod compile {
561592
}
562593

563594
mod validate {
564-
565595
use super::load_wat_module;
566596
use lucet_validate::Validator;
567597
use lucetc::{Compiler, CpuFeatures, HeapSettings, OptLevel};

lucetc/tests/wasm/names_local.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(module
2+
(func $func_name
3+
)
4+
)

0 commit comments

Comments
 (0)