Skip to content

Commit c953a49

Browse files
committed
address suggestion, add a test
1 parent 60e9900 commit c953a49

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

emsymbolizer.py

100644100755
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class Error(BaseException):
2828
def get_codesec_offset(module):
2929
for sec in module.sections():
3030
if sec.type == webassembly.SecType.CODE:
31-
print(sec)
3231
return sec.offset
3332
raise Error(f'No code section found in {module.filename}')
3433

@@ -44,13 +43,12 @@ def symbolize_address_dwarf(module, address):
4443
vma_adjust = get_codesec_offset(module)
4544
cmd = [LLVM_SYMBOLIZER, '-e', module.filename, f'--adjust-vma={vma_adjust}',
4645
str(address)]
47-
#print(cmd)
4846
check_call(cmd)
4947

5048
def main(argv):
5149
wasm_file = argv[1]
52-
print('Warning: the command-line and output format of this file are not'
53-
'finalized yet')
50+
print('Warning: the command-line and output format of this file are not '
51+
'finalized yet', file=sys.stderr)
5452
module = webassembly.Module(wasm_file)
5553

5654
if not has_debug_line_section(module):

tests/core/test_dwarf.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
EM_JS(int, out_to_js, (int x), {})
44

5-
void foo() {
5+
void __attribute__((noinline)) foo() {
66
out_to_js(0); // line 5
77
out_to_js(1); // line 6
88
out_to_js(2); // line 7
99
// A silly possible recursion to avoid binaryen doing any inlining.
1010
if (out_to_js(3)) foo();
1111
}
1212

13+
void __attribute__((always_inline)) bar() {
14+
out_to_js(3);
15+
__builtin_trap();
16+
}
17+
1318
int main() {
1419
foo();
20+
bar();
1521
}

tests/test_other.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8219,6 +8219,27 @@ def test(infile, source_map_added_dir=''):
82198219
ensure_dir('inner')
82208220
test('inner/a.cpp', 'inner')
82218221

8222+
def test_emsymbolizer(self):
8223+
# Test DWARF output
8224+
self.run_process([EMCC, test_file('core/test_dwarf.c'),
8225+
'-g', '-O1', '-o', 'test_dwarf.js'])
8226+
# Use hard-coded addresses. This is potentially brittle, but LLVM's
8227+
# O1 output is pretty minimal so hopefully it won't break too much?
8228+
# Another option would be to disassemble the binary to look for certain
8229+
# instructions or code sequences.
8230+
def get_addr(address):
8231+
return self.run_process(
8232+
[PYTHON, path_from_root('emsymbolizer.py'), 'test_dwarf.wasm', address],
8233+
stdout=PIPE).stdout
8234+
# Check a location in foo(), not inlined.
8235+
self.assertIn('test_dwarf.c:6:3', get_addr('0x101'))
8236+
# Check that both bar (inlined) and main (inlinee) are in the output,
8237+
# as described by the DWARF.
8238+
# TODO: consider also checking the function names once the output format
8239+
# stabilizes more
8240+
self.assertRegex(get_addr('0x124').replace('\n', ''),
8241+
'test_dwarf.c:15:3.*test_dwarf.c:20:3')
8242+
82228243
def test_separate_dwarf(self):
82238244
self.run_process([EMCC, test_file('hello_world.c'), '-g'])
82248245
self.assertExists('a.out.wasm')

tools/webassembly.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class Module:
125125
"""Extremely minimal wasm module reader. Currently only used
126126
for parsing the dylink section."""
127127
def __init__(self, filename):
128+
self.buf = None # Set this before FS calls below in case they throw.
128129
self.filename = filename
129130
self.size = os.path.getsize(filename)
130131
self.buf = open(filename, 'rb')
@@ -134,7 +135,7 @@ def __init__(self, filename):
134135
raise InvalidWasmError(f'{filename} is not a valid wasm file')
135136

136137
def __del__(self):
137-
if hasattr(self, 'buf'):
138+
if self.buf:
138139
self.buf.close()
139140

140141
def readAt(self, offset, count):

0 commit comments

Comments
 (0)