Skip to content

Commit 9962a18

Browse files
Fix function index in backtrace with imported functions
1 parent 2ce00c8 commit 9962a18

File tree

2 files changed

+72
-30
lines changed

2 files changed

+72
-30
lines changed

Sources/WasmKit/Execution/StoreAllocator.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,10 @@ extension StoreAllocator {
315315
for (index, importedEntity) in imports.enumerated() {
316316
buffer.initializeElement(at: index, to: importedEntity)
317317
}
318-
for (index, internalEntity) in internals.enumerated() {
318+
for (internalIndex, internalEntity) in internals.enumerated() {
319+
let index = imports.count + internalIndex
319320
let allocated = try allocateHandle(internalEntity, index)
320-
buffer.initializeElement(at: imports.count + index, to: allocated)
321+
buffer.initializeElement(at: index, to: allocated)
321322
}
322323
}
323324
}
@@ -372,7 +373,7 @@ extension StoreAllocator {
372373
let globals = try allocateEntities(
373374
imports: importedGlobals,
374375
internals: module.globals,
375-
allocateHandle: { global, i in
376+
allocateHandle: { global, _ in
376377
let initialValue = try global.initializer.evaluate(
377378
context: constEvalContext, expectedType: global.type.valueType
378379
)

Tests/WasmKitTests/ExecutionTests.swift

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,26 @@ final class ExecutionTests: XCTestCase {
5252
XCTAssertEqual(results, [.i32(42)])
5353
}
5454

55-
func testBacktrace() throws {
55+
func expectTrap(_ wat: String, assertTrap: (Trap) throws -> Void) throws {
5656
let module = try parseWasm(
57-
bytes: wat2wasm(
58-
"""
59-
(module
60-
(memory (export "memory") 1)
61-
(func $foo (result i32)
62-
unreachable
63-
)
64-
(func $bar (result i32)
65-
(call $foo)
66-
)
67-
(func (export "_start")
68-
(call $bar)
69-
(drop)
70-
)
71-
)
72-
""",
73-
options: EncodeOptions(nameSection: true)
74-
)
57+
bytes: wat2wasm(wat, options: EncodeOptions(nameSection: true))
7558
)
7659

7760
let engine = Engine()
7861
let store = Store(engine: engine)
79-
let instance = try module.instantiate(store: store)
62+
var imports = Imports()
63+
for importEntry in module.imports {
64+
guard case .function(let type) = importEntry.descriptor else { continue }
65+
let function = try Function(
66+
store: store,
67+
type: module.resolveFunctionType(type),
68+
body: { _, _ in
69+
return []
70+
}
71+
)
72+
imports.define(importEntry, .function(function))
73+
}
74+
let instance = try module.instantiate(store: store, imports: imports)
8075
let _start = try XCTUnwrap(instance.exports[function: "_start"])
8176

8277
let trap: Trap
@@ -87,13 +82,59 @@ final class ExecutionTests: XCTestCase {
8782
} catch let error {
8883
trap = try XCTUnwrap(error as? Trap)
8984
}
85+
try assertTrap(trap)
86+
}
9087

91-
XCTAssertEqual(
92-
trap.backtrace?.symbols.compactMap(\.?.name),
93-
[
94-
"foo",
95-
"bar",
96-
"_start",
97-
])
88+
func testBacktraceBasic() throws {
89+
try expectTrap(
90+
"""
91+
(module
92+
(func $foo
93+
unreachable
94+
)
95+
(func $bar
96+
(call $foo)
97+
)
98+
(func (export "_start")
99+
(call $bar)
100+
)
101+
)
102+
"""
103+
) { trap in
104+
XCTAssertEqual(
105+
trap.backtrace?.symbols.compactMap(\.?.name),
106+
[
107+
"foo",
108+
"bar",
109+
"_start",
110+
])
111+
}
112+
}
113+
114+
func testBacktraceWithImports() throws {
115+
try expectTrap(
116+
"""
117+
(module
118+
(func (import "env" "bar"))
119+
(func
120+
unreachable
121+
)
122+
(func $bar
123+
(call 1)
124+
)
125+
(func (export "_start")
126+
(call $bar)
127+
)
128+
)
129+
"""
130+
) { trap in
131+
XCTAssertEqual(
132+
trap.backtrace?.symbols.compactMap(\.?.name),
133+
[
134+
"wasm function[1]",
135+
"bar",
136+
"_start",
137+
])
138+
}
98139
}
99140
}

0 commit comments

Comments
 (0)