166 lines
9.7 KiB
Swift
166 lines
9.7 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import XCTest
|
|
|
|
final class CMOCCallStackTests: XCTestCase {
|
|
private struct Fixture {
|
|
var metadata: CMOCVariableDebug
|
|
var memory: Data
|
|
var available: Data
|
|
var pc: UInt16 = 0x2904
|
|
var u: UInt16 = 0x4001 // Byte arguments/locals need not leave an even U.
|
|
var s: UInt16 = 0x3ff0
|
|
mutating func word(_ at: Int, _ value: Int) {
|
|
memory[at] = UInt8(value >> 8); memory[at + 1] = UInt8(value & 255)
|
|
}
|
|
func capture(limit: Int = 64) -> CMOCCallStack {
|
|
CMOCCallStack.capture(metadata: metadata, pc: pc, u: u, s: s,
|
|
memory: memory, available: available, maximumFrames: limit)
|
|
}
|
|
}
|
|
private func fixture(optimized: Bool = false, interrupt: Bool = false, wrongTarget: Bool = false) -> Fixture {
|
|
var instructions: [CMOCProjectSourceMap.Instruction] = []
|
|
func instruction(_ pc: UInt16, _ bytes: [UInt8], _ file: String, _ line: Int) {
|
|
instructions.append(.init(sourcePath: file, sourceLine: line, address: pc, bytes: bytes, assembly: ""))
|
|
}
|
|
for (entry, file) in [(UInt16(0x2800), "main.c"), (UInt16(0x2900), "helper.c")] {
|
|
instruction(entry, [0x34, 0x40], file, 2)
|
|
instruction(entry + 2, [0x33, 0xe4], file, 2)
|
|
instruction(entry + 4, [0x12], file, 5)
|
|
instruction(entry + 0x20, [0x32, 0xc4], file, 15)
|
|
if interrupt && entry == 0x2900 {
|
|
instruction(entry + 0x22, [0x35, 0x40], file, 15)
|
|
instruction(entry + 0x24, [0x3b], file, 15)
|
|
} else { instruction(entry + 0x22, [0x35, 0xc0], file, 15) }
|
|
}
|
|
instruction(0x2808, [0x17, 0x00, wrongTarget ? 0xf6 : 0xf5], "main.c", 12)
|
|
let map = CMOCProjectSourceMap(entryPoint: 0x2800, binarySHA256: "", sourceHashes: [:], instructions: instructions,
|
|
objectHashes: [:], listingHashes: [:], linkMapSHA256: "")
|
|
let integer = CMOCVariableDebug.CType.integer
|
|
let metadata = CMOCVariableDebug(functions: [
|
|
.init(name: "parent", sourcePath: "main.c", entry: 0x2800, body: 0x2804, end: 0x2820, frame: true),
|
|
.init(name: "child", sourcePath: "helper.c", entry: 0x2900, body: 0x2904, end: 0x2920, frame: !optimized)
|
|
], variables: [
|
|
.init(name: "argument", sourcePath: "main.c", line: 2, type: integer, address: nil, offset: 4, scope: 0x2804..<0x2820, isStatic: false),
|
|
.init(name: "local", sourcePath: "main.c", line: 3, type: integer, address: nil, offset: -2, scope: 0x2804..<0x2820, isStatic: false),
|
|
.init(name: "argument", sourcePath: "helper.c", line: 2, type: integer, address: nil, offset: 4, scope: 0x2904..<0x2920, isStatic: false),
|
|
.init(name: "local", sourcePath: "helper.c", line: 3, type: integer, address: nil, offset: -2, scope: 0x2904..<0x2920, isStatic: false)
|
|
], sourceMap: map)
|
|
var fixture = Fixture(metadata: metadata, memory: Data(repeating: 0, count: 65536), available: Data(repeating: 1, count: 65536))
|
|
for item in instructions { for (index, byte) in item.bytes.enumerated() { fixture.memory[Int(item.address) + index] = byte } }
|
|
fixture.word(0x4001, 0x4021); fixture.word(0x4003, 0x280b)
|
|
fixture.word(0x4021, 0); fixture.word(0x4023, 0x1000) // Unmapped runtime caller.
|
|
fixture.word(0x4005, 9); fixture.word(0x3fff, 3)
|
|
fixture.word(0x4025, 7); fixture.word(0x401f, 5)
|
|
return fixture
|
|
}
|
|
func testCallerUsesCallSiteScopeAndRecoveredOddFramePointer() {
|
|
let fixture = fixture(), result = fixture.capture()
|
|
XCTAssertEqual(result.frames.map(\.functionName), ["child", "parent"])
|
|
XCTAssertTrue(result.frames.allSatisfy(\.localsInspectable))
|
|
XCTAssertEqual(result.frames[1].pc, 0x2808)
|
|
XCTAssertEqual(result.frames[1].sourceLine, 12)
|
|
XCTAssertEqual(result.frames[1].u, 0x4021)
|
|
for (index, expected) in [(0, ["argument":"9", "local":"3"]), (1, ["argument":"7", "local":"5"])] {
|
|
let frame = result.frames[index]
|
|
let evaluator = CMOCWatchEvaluator(metadata: fixture.metadata, context: .init(pc: frame.pc, u: frame.u,
|
|
memory: fixture.memory, available: fixture.available))
|
|
for (name, value) in expected {
|
|
XCTAssertNil(evaluator.evaluate(name).error)
|
|
XCTAssertEqual(evaluator.evaluate(name).value, value)
|
|
}
|
|
}
|
|
XCTAssertTrue(result.termination.contains("caller"))
|
|
}
|
|
func testInvalidUnreadableRepeatedAndWrongCallLinksStopWithoutScanning() {
|
|
var fixture = fixture()
|
|
fixture.word(0x4001, 0x4001)
|
|
XCTAssertEqual(fixture.capture().frames.count, 1)
|
|
fixture.word(0x4001, 0x4003)
|
|
XCTAssertEqual(fixture.capture().frames.count, 1)
|
|
fixture.word(0x4001, 0xff00)
|
|
XCTAssertEqual(fixture.capture().frames.count, 1)
|
|
fixture.word(0x4001, 0x4021); fixture.word(0x4003, 0x280a)
|
|
XCTAssertEqual(fixture.capture().frames.count, 1)
|
|
fixture.word(0x4003, 0x280b); fixture.available[0x4021] = 0
|
|
XCTAssertEqual(fixture.capture().frames.count, 1)
|
|
XCTAssertEqual(self.fixture(wrongTarget: true).capture().frames.count, 1)
|
|
fixture.available[0x4001] = 0
|
|
XCTAssertFalse(fixture.capture().frames[0].localsInspectable)
|
|
fixture = self.fixture(); fixture.s = 0x4002
|
|
XCTAssertFalse(fixture.capture().frames[0].localsInspectable)
|
|
// A plausible U and compiler metadata alone must not bypass the
|
|
// unwinder's rejection. Verified globals still need no stack frame.
|
|
let valid = self.fixture()
|
|
let metadata = CMOCVariableDebug(functions: valid.metadata.functions,
|
|
variables: valid.metadata.variables + [.init(name: "global", sourcePath: "helper.c", line: 1,
|
|
type: .integer, address: 0x4005, offset: nil, scope: nil, isStatic: false)],
|
|
sourceMap: valid.metadata.sourceMap)
|
|
let gated = CMOCWatchEvaluator(metadata: metadata,
|
|
context: .init(pc: valid.pc, u: valid.u, memory: valid.memory, available: valid.available),
|
|
frameLocalsAvailable: false)
|
|
XCTAssertNotNil(gated.evaluate("argument").error)
|
|
XCTAssertNotNil(gated.evaluate("local").error)
|
|
XCTAssertNotNil(gated.evaluate("&local").error)
|
|
XCTAssertNil(gated.evaluate("global").error)
|
|
XCTAssertEqual(gated.evaluate("global").value, "9")
|
|
}
|
|
func testOptimizedInterruptPrologueAndEpilogueDoNotInventCallerFrames() {
|
|
for fixture in [fixture(optimized: true), fixture(interrupt: true)] {
|
|
let result = fixture.capture()
|
|
XCTAssertEqual(result.frames.count, 1)
|
|
XCTAssertFalse(result.frames[0].localsInspectable)
|
|
XCTAssertTrue(result.termination.contains("unsupported"))
|
|
}
|
|
for pc: UInt16 in [0x2900, 0x2902, 0x2920, 0x2922] {
|
|
var fixture = fixture(); fixture.pc = pc
|
|
let result = fixture.capture()
|
|
XCTAssertEqual(result.frames.count, 1)
|
|
XCTAssertFalse(result.frames[0].localsInspectable)
|
|
XCTAssertTrue(result.termination.contains("prologue"))
|
|
}
|
|
// A plausible U and compiler frame flag do not override the caller's
|
|
// explicit determination that this paused frame cannot be inspected.
|
|
var fixture = fixture()
|
|
fixture.word(0x5000, 1234)
|
|
let metadata = CMOCVariableDebug(functions: fixture.metadata.functions,
|
|
variables: fixture.metadata.variables + [.init(name: "global", sourcePath: "helper.c", line: 1,
|
|
type: .integer, address: 0x5000, offset: nil, scope: nil, isStatic: false)],
|
|
sourceMap: fixture.metadata.sourceMap)
|
|
var evaluator = CMOCWatchEvaluator(metadata: metadata, context: .init(pc: fixture.pc, u: fixture.u,
|
|
memory: fixture.memory, available: fixture.available))
|
|
XCTAssertEqual(evaluator.evaluate("argument").value, "9")
|
|
XCTAssertEqual(evaluator.evaluate("local").value, "3")
|
|
evaluator.frameLocalsAvailable = false
|
|
XCTAssertNotNil(evaluator.evaluate("argument").error)
|
|
XCTAssertNotNil(evaluator.evaluate("local").error)
|
|
XCTAssertNotNil(evaluator.visibleVariables().first { $0.name == "local" }?.error)
|
|
XCTAssertNil(evaluator.evaluate("global").error)
|
|
XCTAssertEqual(evaluator.evaluate("global").value, "1234")
|
|
XCTAssertEqual(evaluator.evaluate("1 + 2").value, "3")
|
|
}
|
|
func testUnverifiedCanonicalPrologueIsNotAcceptedFromLiveMemoryAlone() {
|
|
var fixture = fixture()
|
|
let original = fixture.metadata.sourceMap
|
|
let map = CMOCProjectSourceMap(entryPoint: original.entryPoint, binarySHA256: "", sourceHashes: [:],
|
|
instructions: original.instructions.filter { $0.address != 0x2900 }, objectHashes: [:], listingHashes: [:], linkMapSHA256: "")
|
|
fixture.metadata = CMOCVariableDebug(functions: fixture.metadata.functions, variables: fixture.metadata.variables, sourceMap: map)
|
|
let result = fixture.capture()
|
|
XCTAssertEqual(result.frames.count, 1)
|
|
XCTAssertFalse(result.frames[0].localsInspectable)
|
|
XCTAssertTrue(result.termination.contains("unsupported"))
|
|
}
|
|
func testSnapshotIdentityAndDepthLimits() {
|
|
var fixture = fixture()
|
|
fixture.memory[0x2904] = 0x39
|
|
XCTAssertTrue(fixture.capture().frames.isEmpty)
|
|
fixture = self.fixture(); fixture.available[0x2904] = 0
|
|
XCTAssertTrue(fixture.capture().frames.isEmpty)
|
|
fixture = self.fixture(); fixture.memory = Data()
|
|
XCTAssertTrue(fixture.capture().frames.isEmpty)
|
|
fixture = self.fixture()
|
|
let bounded = fixture.capture(limit: 1)
|
|
XCTAssertEqual(bounded.frames.count, 1)
|
|
XCTAssertTrue(bounded.termination.contains("1-frame"))
|
|
}
|
|
}
|