251 lines
15 KiB
Swift
251 lines
15 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import XCTest
|
|
|
|
final class CMOCOwnedCallContextTests: XCTestCase {
|
|
func testEntryExposesExactlyActualSWindowAndProvesSevenIntegerArguments() throws {
|
|
let fixture = Fixture()
|
|
let before = fixture.memory
|
|
let result = fixture.capture()
|
|
XCTAssertEqual(result["status"] as? String, "available")
|
|
let window = try XCTUnwrap(result["stack_window"] as? [String: Any])
|
|
XCTAssertEqual(window["address"] as? Int, 0x4000)
|
|
XCTAssertEqual(window["count"] as? Int, 64)
|
|
XCTAssertEqual(window["interpretation"] as? String, "uninterpreted")
|
|
XCTAssertEqual(window["bytes"] as? [UInt8], Array(before[0x4000..<0x4040]))
|
|
let words = try XCTUnwrap(window["words_be"] as? [[String: Any]])
|
|
XCTAssertEqual(words.count, 32)
|
|
XCTAssertEqual(words.first?["value"] as? Int, 0x280b)
|
|
XCTAssertEqual(words.last?["address"] as? Int, 0x403e)
|
|
let caller = try XCTUnwrap(result["caller"] as? [String: Any])
|
|
XCTAssertEqual(caller["status"] as? String, "available")
|
|
XCTAssertEqual(caller["call_address"] as? Int, 0x2808)
|
|
XCTAssertEqual(caller["callee_phase"] as? String, "function_entry")
|
|
let arguments = try XCTUnwrap(result["arguments"] as? [String: Any])
|
|
let items = try XCTUnwrap(arguments["items"] as? [[String: Any]])
|
|
XCTAssertEqual(items.compactMap { $0["value"] as? Int64 }, Fixture.arguments.map(Int64.init))
|
|
XCTAssertEqual(items.compactMap { $0["address"] as? Int }, stride(from: 0x4002, through: 0x400e, by: 2).map { $0 })
|
|
XCTAssertEqual(arguments["all_recorded_parameters_available"] as? Bool, true)
|
|
XCTAssertEqual(fixture.memory, before, "Inspection has no mutable guest state.")
|
|
}
|
|
|
|
func testBodyUsesOnlyBoundedActualUFrameAndCompilerParameterLocations() throws {
|
|
var fixture = Fixture()
|
|
fixture.pc = 0x2904; fixture.s = 0x3fee; fixture.u = 0x3ffe
|
|
fixture.word(0x3ffe, 0x6a5a) // Saved U need not itself be readable or inspected.
|
|
let result = fixture.capture()
|
|
let caller = try XCTUnwrap(result["caller"] as? [String: Any])
|
|
XCTAssertEqual(caller["status"] as? String, "available")
|
|
XCTAssertEqual(caller["callee_phase"] as? String, "mapped_body")
|
|
let args = try XCTUnwrap(result["arguments"] as? [String: Any])
|
|
XCTAssertEqual((args["items"] as? [[String: Any]])?.count, 7)
|
|
let window = try XCTUnwrap(result["stack_window"] as? [String: Any])
|
|
XCTAssertEqual((window["physical_spans"] as? [[String: Any]])?.count, 2)
|
|
fixture.u = 0x6000
|
|
XCTAssertEqual((fixture.capture()["caller"] as? [String: Any])?["status"] as? String, "unavailable")
|
|
XCTAssertEqual(fixture.capture()["status"] as? String, "available", "Raw evidence does not require a usable U frame.")
|
|
}
|
|
|
|
func testRejectsMalformedUnavailableROMAndWrappingWindows() {
|
|
var fixture = Fixture()
|
|
fixture.memory.removeLast()
|
|
XCTAssertEqual(fixture.capture()["status"] as? String, "unavailable")
|
|
fixture = Fixture(); fixture.available.removeLast()
|
|
XCTAssertEqual(fixture.capture()["status"] as? String, "unavailable")
|
|
fixture = Fixture(); fixture.available[0x403f] = 0
|
|
XCTAssertNil(fixture.capture()["stack_window"])
|
|
fixture = Fixture(); fixture.pages.removeValue(forKey: 2)
|
|
XCTAssertNil(fixture.capture()["stack_window"], "Readable ROM is not RAM.")
|
|
fixture = Fixture(); fixture.pages[2] = 1024
|
|
XCTAssertNil(fixture.capture()["stack_window"])
|
|
fixture = Fixture(); fixture.s = 0xfdc1
|
|
XCTAssertNil(fixture.capture()["stack_window"])
|
|
fixture = Fixture(); fixture.s = 0xfff0
|
|
XCTAssertNil(fixture.capture()["stack_window"])
|
|
fixture = Fixture(); fixture.s = 0x3ff0; fixture.pages.removeValue(forKey: 1)
|
|
XCTAssertNil(fixture.capture()["stack_window"], "Both physical pages of a split window are required.")
|
|
}
|
|
|
|
func testRejectsChangedExecutableROMPCAndUnsharedPC() {
|
|
var fixture = Fixture()
|
|
fixture.memory[0x2900] ^= 1
|
|
XCTAssertEqual(fixture.capture()["status"] as? String, "unavailable")
|
|
fixture = Fixture(); fixture.pages.removeValue(forKey: 1)
|
|
XCTAssertEqual(fixture.capture()["status"] as? String, "unavailable")
|
|
fixture = Fixture()
|
|
XCTAssertEqual(fixture.capture(shared: ["other.c"])["status"] as? String, "unavailable")
|
|
fixture.pc = 0x5000
|
|
XCTAssertEqual(fixture.capture()["status"] as? String, "unavailable")
|
|
}
|
|
|
|
func testUnknownOptimizedIndirectAndInterruptFramesStayUninterpreted() {
|
|
var fixture = Fixture()
|
|
var result = fixture.capture(metadata: false)
|
|
XCTAssertEqual(result["status"] as? String, "available")
|
|
XCTAssertEqual((result["caller"] as? [String: Any])?["status"] as? String, "unavailable")
|
|
fixture.framed = false
|
|
result = fixture.capture()
|
|
XCTAssertEqual((result["arguments"] as? [String: Any])?["status"] as? String, "unavailable")
|
|
fixture = Fixture(); fixture.callBytes = [0xad, 0x84, 0x12] // JSR ,X plus unrelated NOP is not a direct call.
|
|
result = fixture.capture()
|
|
XCTAssertEqual(result["status"] as? String, "available")
|
|
XCTAssertEqual((result["caller"] as? [String: Any])?["status"] as? String, "unavailable")
|
|
fixture = Fixture(); fixture.epilogue = [0x32, 0xc4, 0x35, 0x40, 0x3b]
|
|
result = fixture.capture()
|
|
XCTAssertEqual(result["status"] as? String, "available")
|
|
XCTAssertEqual((result["caller"] as? [String: Any])?["status"] as? String, "unavailable")
|
|
}
|
|
|
|
func testDoesNotScanOtherWordsOrDiscloseUnsharedCaller() {
|
|
var fixture = Fixture()
|
|
fixture.word(0x4000, 0x9999); fixture.word(0x4010, 0x280b)
|
|
XCTAssertEqual((fixture.capture()["caller"] as? [String: Any])?["status"] as? String, "unavailable")
|
|
fixture = Fixture(); fixture.callerPath = "private.c"
|
|
let result = fixture.capture()
|
|
XCTAssertEqual(result["status"] as? String, "available")
|
|
XCTAssertEqual((result["caller"] as? [String: Any])?["status"] as? String, "unavailable")
|
|
XCTAssertFalse(String(describing: result).contains("private.c"))
|
|
fixture = Fixture(); fixture.duplicateCall = true
|
|
XCTAssertEqual((fixture.capture()["caller"] as? [String: Any])?["status"] as? String, "unavailable",
|
|
"Ambiguous call-site evidence must not choose a caller.")
|
|
}
|
|
|
|
func testExcludesLocalsAggregatesOutOfWindowAndMismatchedMetadata() throws {
|
|
var fixture = Fixture()
|
|
fixture.extraVariables = [
|
|
.init(name: "local", sourcePath: "fixture.c", line: 3, type: .integer,
|
|
address: nil, offset: -2, scope: 0x2904..<0x2920, isStatic: false),
|
|
.init(name: "aggregate", sourcePath: "fixture.c", line: 3,
|
|
type: .init(name: "structure", kind: "struct", size: 4),
|
|
address: nil, offset: 30, scope: 0x2904..<0x2920, isStatic: false),
|
|
.init(name: "far", sourcePath: "fixture.c", line: 3, type: .integer,
|
|
address: nil, offset: 100, scope: 0x2904..<0x2920, isStatic: false)
|
|
]
|
|
let args = try XCTUnwrap(fixture.capture()["arguments"] as? [String: Any])
|
|
XCTAssertEqual((args["items"] as? [[String: Any]])?.count, 7)
|
|
XCTAssertEqual(args["all_recorded_parameters_available"] as? Bool, false)
|
|
fixture.metadataHash = "other-build"
|
|
let result = fixture.capture()
|
|
XCTAssertEqual(result["status"] as? String, "available")
|
|
XCTAssertEqual((result["arguments"] as? [String: Any])?["status"] as? String, "unavailable")
|
|
}
|
|
|
|
func testBankMismatchRejectsEvenIdenticalLogicalBytes() {
|
|
var fixture = Fixture()
|
|
fixture.banked = true
|
|
XCTAssertEqual(fixture.capture()["status"] as? String, "available")
|
|
fixture.pages[1] = 58
|
|
XCTAssertEqual(fixture.capture()["status"] as? String, "unavailable")
|
|
}
|
|
|
|
func testSignedByteUsesCompilerByteOffsetAndNeverDereferencesPointers() throws {
|
|
var fixture = Fixture()
|
|
fixture.memory[0x4011] = 0x80
|
|
fixture.extraVariables = [
|
|
.init(name: "byteArgument", sourcePath: "fixture.c", line: 3,
|
|
type: .init(name: "signed char", kind: "integer", size: 1, signed: true),
|
|
address: nil, offset: 19, scope: 0x2904..<0x2920, isStatic: false),
|
|
.init(name: "pointerArgument", sourcePath: "fixture.c", line: 3,
|
|
type: .init(name: "int *", kind: "pointer", size: 2),
|
|
address: nil, offset: 20, scope: 0x2904..<0x2920, isStatic: false)
|
|
]
|
|
let args = try XCTUnwrap(fixture.capture()["arguments"] as? [String: Any])
|
|
let items = try XCTUnwrap(args["items"] as? [[String: Any]])
|
|
XCTAssertEqual(items.last?["name"] as? String, "byteArgument")
|
|
XCTAssertEqual(items.last?["address"] as? Int, 0x4011)
|
|
XCTAssertEqual(items.last?["value"] as? Int64, -128)
|
|
XCTAssertEqual(items.count, 8)
|
|
XCTAssertEqual(args["all_recorded_parameters_available"] as? Bool, false)
|
|
}
|
|
|
|
func testOverlappingParameterLocationsAreNotInterpreted() throws {
|
|
var fixture = Fixture()
|
|
fixture.extraVariables = [
|
|
.init(name: "alias", sourcePath: "fixture.c", line: 3, type: .integer,
|
|
address: nil, offset: 4, scope: 0x2904..<0x2920, isStatic: false)
|
|
]
|
|
let args = try XCTUnwrap(fixture.capture()["arguments"] as? [String: Any])
|
|
let items = try XCTUnwrap(args["items"] as? [[String: Any]])
|
|
XCTAssertEqual(items.count, 6)
|
|
XCTAssertFalse(items.contains { ($0["name"] as? String) == "arg0" || ($0["name"] as? String) == "alias" })
|
|
XCTAssertEqual(args["all_recorded_parameters_available"] as? Bool, false)
|
|
}
|
|
|
|
private struct Fixture {
|
|
static let arguments: [UInt16] = [0x0102, 0x2030, 0x7f80, 0xa0b0, 0xc0d0, 0xdead, 0xbeef]
|
|
var memory = Data(repeating: 0, count: 65536)
|
|
var available = Data(repeating: 1, count: 65536)
|
|
var pages = [1: 57, 2: 12]
|
|
var pc: UInt16 = 0x2900, s: UInt16 = 0x4000, u: UInt16 = 0x6a5a
|
|
var framed = true, banked = false, duplicateCall = false
|
|
var callBytes: [UInt8] = [0x17, 0x00, 0xf5]
|
|
var epilogue: [UInt8] = [0x32, 0xc4, 0x35, 0xc0]
|
|
var callerPath = "fixture.c", metadataHash = "fixture"
|
|
var extraVariables: [CMOCVariableDebug.Variable] = []
|
|
|
|
init() {
|
|
word(0x4000, 0x280b)
|
|
for (index, value) in Self.arguments.enumerated() { word(0x4002 + index * 2, value) }
|
|
}
|
|
mutating func word(_ address: Int, _ value: UInt16) {
|
|
memory[address] = UInt8(value >> 8); memory[address + 1] = UInt8(value & 255)
|
|
}
|
|
func capture(metadata useMetadata: Bool = true, shared: Set<String> = ["fixture.c"]) -> [String: Any] {
|
|
let module: String? = banked ? "resident" : nil
|
|
var instructions: [CMOCProjectSourceMap.Instruction] = []
|
|
for entry in [0x2800, 0x2900] {
|
|
let path = entry == 0x2800 ? callerPath : "fixture.c"
|
|
let rows: [(Int, [UInt8])] = [(0, [0x34, 0x40]), (2, [0x33, 0xe4]), (4, [0x12]),
|
|
(0x20, Array(epilogue.prefix(2))), (0x22, Array(epilogue.dropFirst(2)))]
|
|
for (offset, bytes) in rows {
|
|
instructions.append(.init(sourcePath: path, sourceLine: 3, address: UInt16(entry + offset),
|
|
bytes: bytes, assembly: "authored", moduleID: module))
|
|
}
|
|
}
|
|
instructions.append(.init(sourcePath: callerPath, sourceLine: 2, address: 0x2808,
|
|
bytes: callBytes, assembly: "authored direct call", moduleID: module))
|
|
if duplicateCall, let call = instructions.last { instructions.append(call) }
|
|
var live = memory
|
|
// Fixture code is prepared independently of capture's immutable snapshot.
|
|
// A caller's deliberate mutation at entry must remain visible to the guard.
|
|
var code = Data(repeating: 0x12, count: 0x125)
|
|
for instruction in instructions {
|
|
let begin = Int(instruction.address)
|
|
code.replaceSubrange((begin - 0x2800)..<(begin - 0x2800 + instruction.bytes.count), with: instruction.bytes)
|
|
}
|
|
for offset in 0..<code.count { live[0x2800 + offset] = code[offset] }
|
|
if memory[0x2900] != 0 { live[0x2900] ^= memory[0x2900] }
|
|
var map = CMOCProjectSourceMap(entryPoint: 0x2800, binarySHA256: "fixture", sourceHashes: [:],
|
|
instructions: instructions, objectHashes: [:], listingHashes: [:], linkMapSHA256: "fixture")
|
|
map.flatCodeGuards = [.init(address: 0x2800, bytes: Array(code))]
|
|
if banked {
|
|
map.bankedLayout = .init(entryPoint: 0x2800, binarySHA256: "fixture", linkMap: Data(),
|
|
sourceHashes: [:], artifactHashes: [:], modules: [
|
|
.init(id: "resident", sections: ["code"], codeSections: ["code"],
|
|
logicalAddress: 0x2800, bytes: Array(code),
|
|
pages: [.init(logicalPage: 1, physicalPage: 57)],
|
|
codeGuards: [.init(address: 0x2800, bytes: Array(code))])])
|
|
map.flatCodeGuards = []
|
|
}
|
|
var metadataMap = map
|
|
// Identity mismatch is deliberately independent of live executable bytes.
|
|
if metadataHash != "fixture" {
|
|
metadataMap = .init(entryPoint: map.entryPoint, binarySHA256: metadataHash, sourceHashes: [:],
|
|
instructions: map.instructions, objectHashes: [:], listingHashes: [:], linkMapSHA256: "fixture")
|
|
}
|
|
let functions: [CMOCVariableDebug.Function] = [
|
|
.init(name: "parent", sourcePath: callerPath, entry: 0x2800, body: 0x2804, end: 0x2820, frame: true, moduleID: module),
|
|
.init(name: "child", sourcePath: "fixture.c", entry: 0x2900, body: 0x2904, end: 0x2920, frame: framed, moduleID: module)
|
|
]
|
|
let variables: [CMOCVariableDebug.Variable] = (0..<7).map {
|
|
.init(name: "arg\($0)", sourcePath: "fixture.c", line: 3,
|
|
type: .init(name: "unsigned int", kind: "integer", size: 2, signed: false),
|
|
address: nil, offset: 4 + $0 * 2, scope: 0x2904..<0x2920, isStatic: false, moduleID: module)
|
|
} + extraVariables
|
|
let metadata = CMOCVariableDebug(functions: functions, variables: variables, sourceMap: metadataMap)
|
|
return CMOCOwnedCallContext.capture(map: map, metadata: useMetadata ? metadata : nil,
|
|
pc: pc, s: s, u: u, memory: live, available: available,
|
|
physicalPages: pages, sharedPaths: shared)
|
|
}
|
|
}
|
|
}
|