Skip to content

Commit fb6948c

Browse files
committed
Fix DebuggerMemoryCacheTests
1 parent cbee2bd commit fb6948c

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

[email protected]

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
195195
name: "WasmKitGDBHandlerTests",
196196
dependencies: [
197197
"WasmKitGDBHandler",
198+
"WAT",
198199
],
199200
),
200201
])

Sources/WasmKit/Execution/Debugger.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,14 @@
150150
return wasm
151151
}
152152

153-
package mutating func enableBreakpoint(module: Module, function: Int) throws -> Int {
154-
try self.enableBreakpoint(address: module.functions[function].code.originalAddress)
153+
package mutating func enableBreakpoint(
154+
module: Module,
155+
function: Int,
156+
offsetWithinFunction: Int = 0
157+
) throws -> Int {
158+
try self.enableBreakpoint(address: module.functions[function].code.originalAddress + offsetWithinFunction)
155159
}
160+
156161
/// Disables a breakpoint at a given Wasm address. If no breakpoint at a given address was previously set with
157162
/// `self.enableBreakpoint(address:), this function immediately returns.
158163
/// - Parameter address: byte offset of the Wasm instruction that was replaced with a breakpoint. The original

Sources/WasmKit/Execution/DebuggerStackFrame.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@
5151

5252
for (i, type) in functionType.parameters.enumerated() {
5353
// See ``FrameHeaderLayout`` documentation for offset calculation details.
54-
type.append(to: &span, frame, offset: i - max(functionType.parameters.count, functionType.results.count))
5554
layout.localOffsets.append(span.byteCount)
55+
type.append(to: &span, frame, offset: i - 3 - max(functionType.parameters.count, functionType.results.count))
5656
}
5757

5858
for (i, type) in wasm.locals.enumerated() {
59-
type.append(to: &span, frame, offset: i)
6059
layout.localOffsets.append(span.byteCount)
60+
type.append(to: &span, frame, offset: i)
6161
}
6262

6363
// FIXME: copy over actual stack values

Sources/WasmKitGDBHandler/DebuggerMemoryCache.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
let (base, layout) = try debugger.packedStackFrame(frameIndex: frameIndex) { span, layout in
4848
let baseAddress = self.stackFrames.writerIndex
4949
self.stackFrames.writeBytes(span)
50+
print("written to stackFrames: \(self.stackFrames.hexDump(format: .plain))")
5051
return (baseAddress, layout)
5152
}
5253

@@ -67,9 +68,7 @@
6768
var length = length
6869

6970
if addressInProtocolSpace >= self.stackOffsetInProtocolSpace {
70-
print("stackMemory")
7171
let stackAddress = Int(addressInProtocolSpace - self.stackOffsetInProtocolSpace)
72-
print("stackAddress is \(stackAddress)")
7372
if stackAddress + length > self.stackFrames.readableBytes {
7473
length = self.stackFrames.readableBytes - stackAddress
7574
}
@@ -88,7 +87,7 @@
8887
}
8988
}
9089

91-
mutating func invalidate() {
90+
package mutating func invalidate() {
9291
self.stackFrames = self.allocator.buffer(capacity: 0)
9392
self.stackFrameLayouts = [:]
9493
}

Tests/WasmKitGDBHandlerTests/DebuggerMemoryCacheTests.swift

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@
1616
(local.set $x)
1717
(local.get $x)
1818
(call $f)
19+
(call $g)
1920
)
2021
2122
(func $f (param $a i32) (result i32)
2223
(local.get $a)
2324
)
25+
26+
(func $g (param $a i32) (result i32) (local $x i32)
27+
(i32.const 24)
28+
(local.set $x)
29+
(local.get $a)
30+
)
2431
)
2532
"""
2633

2734
@Suite
2835
struct DebuggerMemoryCacheTests {
29-
3036
@Test
3137
func localAddress() throws {
3238
let store = Store(engine: Engine())
@@ -36,13 +42,29 @@
3642

3743
var memoryCache = DebuggerMemoryCache(allocator: .init(), wasmBinary: .init(bytes: bytes))
3844

39-
let breakpointAddress = try debugger.enableBreakpoint(module: module, function: 1)
45+
var breakpointAddress = try debugger.enableBreakpoint(module: module, function: 1)
46+
try debugger.enableBreakpoint(address: breakpointAddress)
4047
try debugger.run()
4148

42-
let localAddress = try memoryCache.getAddressOfLocal(debugger: &debugger, frameIndex: 0, localIndex: 0)
49+
var localAddress = try memoryCache.getAddressOfLocal(debugger: &debugger, frameIndex: 0, localIndex: 0)
4350
var buffer = ByteBuffer(memoryCache.readMemory(debugger: debugger, addressInProtocolSpace: localAddress, length: 4))
44-
let value = buffer.readInteger(endianness: .little, as: UInt32.self)
51+
var value = buffer.readInteger(endianness: .little, as: UInt32.self)
4552
#expect(value == 42)
53+
54+
breakpointAddress = try debugger.enableBreakpoint(
55+
module: module,
56+
function: 2,
57+
// i32.const 2 bytes + local.set 4 bytes
58+
offsetWithinFunction: 6
59+
)
60+
try debugger.enableBreakpoint(address: breakpointAddress)
61+
try debugger.run()
62+
memoryCache.invalidate()
63+
64+
localAddress = try memoryCache.getAddressOfLocal(debugger: &debugger, frameIndex: 0, localIndex: 1)
65+
buffer = ByteBuffer(memoryCache.readMemory(debugger: debugger, addressInProtocolSpace: localAddress, length: 4))
66+
value = buffer.readInteger(endianness: .little, as: UInt32.self)
67+
#expect(value == 24)
4668
}
4769
}
4870
#endif

0 commit comments

Comments
 (0)