Skip to content

Commit

Permalink
Use WASM function names in compiled objects (#8627)
Browse files Browse the repository at this point in the history
* Use WASM function names in compiled objects

Instead of generating symbol names in the format
"wasm[$MODULE_ID]::function[$FUNCTION_INDEX]", generate (if possible)
something more readable, such as "wasm[$MODULE_ID]::$FUNCTION_NAME".
This helps when debugging or profiling the generated code.

Co-authored-by: Jamey Sharp <jsharp@fastly.com>

* Ensure symbol names are cleaned up and have function indexes

Filter symbol names to include only characters that are usually used
for function names, and that might be produced by name mangling.
Replace everything else with a question mark (and all repeated question
marks by a single one), and then truncate to a length of 96 characters.

This should be enough to not only avoid passing user-controlled strings
to tools such as "perf" and "objdump", and make it easier to
disambiguate symbols that might have the same name but different
indices.

* Make symbol cleaning slightly more efficient

* Update symbol names to be closer to what tests expect

* Ensure only alphanumeric ASCII characters are allowed in a symbol name

* Ensure sliced symbol name is within its bounds

* Update test expectations after adding function name to symbol name

---------

Co-authored-by: Jamey Sharp <jsharp@fastly.com>
  • Loading branch information
lpereira and jameysharp authored May 16, 2024
1 parent d18477f commit cd8bd0d
Show file tree
Hide file tree
Showing 83 changed files with 134 additions and 89 deletions.
51 changes: 48 additions & 3 deletions crates/wasmtime/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::Engine;
use anyhow::{Context, Result};
use std::{
any::Any,
borrow::Cow,
collections::{btree_map, BTreeMap, BTreeSet, HashMap, HashSet},
mem,
};
Expand Down Expand Up @@ -419,6 +420,35 @@ impl<'a> CompileInputs<'a> {
ret
}

fn clean_symbol(name: &str) -> Cow<str> {
/// Maximum length of symbols generated in objects.
const MAX_SYMBOL_LEN: usize = 96;

// Just to be on the safe side, avoid passing user-provided data to tools
// like "perf" or "objdump", and filter the name. Let only characters usually
// used for function names, plus some characters that might be used in name
// mangling.
let bad_char = |c: char| !c.is_ascii_alphanumeric() && !r"<>[]_-:@$".contains(c);
if name.chars().any(bad_char) {
let mut last_char_seen = '\u{0000}';
Cow::Owned(
name.chars()
.map(|c| if bad_char(c) { '?' } else { c })
.filter(|c| {
let skip = last_char_seen == '?' && *c == '?';
last_char_seen = *c;
!skip
})
.take(MAX_SYMBOL_LEN)
.collect::<String>(),
)
} else if name.len() <= MAX_SYMBOL_LEN {
Cow::Borrowed(&name[..])
} else {
Cow::Borrowed(&name[..MAX_SYMBOL_LEN])
}
}

fn collect_inputs_in_translations(
&mut self,
types: &'a ModuleTypesBuilder,
Expand All @@ -436,13 +466,28 @@ impl<'a> CompileInputs<'a> {
let func_index = translation.module.func_index(def_func_index);
let (info, function) =
compiler.compile_function(translation, def_func_index, func_body, types)?;
Ok(CompileOutput {
key: CompileKey::wasm_function(module, def_func_index),
symbol: format!(
let symbol = match translation
.debuginfo
.name_section
.func_names
.get(&func_index)
{
Some(name) => format!(
"wasm[{}]::function[{}]::{}",
module.as_u32(),
func_index.as_u32(),
Self::clean_symbol(&name)
),
None => format!(
"wasm[{}]::function[{}]",
module.as_u32(),
func_index.as_u32()
),
};

Ok(CompileOutput {
key: CompileKey::wasm_function(module, def_func_index),
symbol,
function: CompiledFunction::Function(function),
info: Some(info),
})
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/block/as_if_cond.wat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)
)

;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/block/deep.wat
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
))
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/block/empty.wat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(block $l)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/block/nested.wat
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br/as_block_first.wat
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(block (br 0) (call $dummy))
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br/as_block_last.wat
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(block (nop) (call $dummy) (br 0))
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br/as_block_mid.wat
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(block (call $dummy) (br 0) (call $dummy))
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br/as_block_value.wat
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(block (result i32) (nop) (call $dummy) (br 0 (i32.const 2)))
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br/as_call_all.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(block (result i32) (call $f (br 0 (i32.const 15))))
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::f:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br/as_call_first.wat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::f:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br/as_call_last.wat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::f:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br/as_call_mid.wat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::f:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br/as_loop_last.wat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br/as_loop_mid.wat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br_if/as_block_last.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(block (call $dummy) (call $dummy) (br_if 0 (local.get 0)))
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br_if/as_block_last_value.wat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br_if/as_call_first.wat
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::f:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br_if/as_call_last.wat
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::f:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br_if/as_call_mid.wat
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::f:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br_if/as_if_else.wat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)
)

;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br_if/as_if_then.wat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/br_if/as_loop_last.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(loop (call $dummy) (br_if 1 (local.get 0)))
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/call/params.wat
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
;; retq
;; 152: ud2
;;
;; wasm[0]::function[1]:
;; wasm[0]::function[1]::add:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/call/recursive.wat
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)
(export "fib" (func $fibonacci8))
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::fibonacci8:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
4 changes: 2 additions & 2 deletions tests/disas/winch/x64/call/simple.wat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(local.get 1)
(i32.mul))
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::main:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down Expand Up @@ -53,7 +53,7 @@
;; retq
;; 8b: ud2
;;
;; wasm[0]::function[1]:
;; wasm[0]::function[1]::product:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/f32_const/call_id.wat
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(func $id-f32 (param f32) (result f32) (local.get 0))
(func (export "type-first-f32") (result f32) (call $id-f32 (f32.const 1.32)))
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::id-f32:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/f64_const/call_id.wat
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(func $id-f64 (param f64) (result f64) (local.get 0))
(func (export "type-first-f64") (result f64) (call $id-f64 (f64.const 1.32)))
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::id-f64:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/if/as_binop.wat
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/if/as_br_if_last.wat
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/if/as_if_cond.wat
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/if/as_testop.wat
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/if/nested.wat
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/if/singular.wat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(if (result i32) (local.get 0) (then (i32.const 7)) (else (i32.const 8)))
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/loop/as_binary_operand.wat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::dummy:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/winch/x64/loop/as_call_value.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(call $f (loop (result i32) (i32.const 1)))
)
)
;; wasm[0]::function[0]:
;; wasm[0]::function[0]::f:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
Expand Down
Loading

0 comments on commit cd8bd0d

Please sign in to comment.