Files
VCCPlusOSS/platforms/macos/Tests/CMOCVariableDebugTests.swift
T

495 lines
40 KiB
Swift

// SPDX-License-Identifier: GPL-3.0-or-later
import XCTest
final class CMOCVariableDebugTests: XCTestCase {
private let word = CMOCVariableDebug.CType(name: "int", kind: "integer", size: 2, signed: true)
private func fixture(frame: Bool = true, changedCode: Bool = false, io: Bool = false) -> CMOCWatchEvaluator {
let sourceMap = CMOCProjectSourceMap(entryPoint: 0x2800, binarySHA256: "", sourceHashes: [:],
instructions: [.init(sourcePath: "main.c", sourceLine: 5, address: 0x2800, bytes: [0x12], assembly: "NOP")],
objectHashes: [:], listingHashes: [:], linkMapSHA256: "")
let pointer = CMOCVariableDebug.CType(name: "int *", kind: "pointer", size: 2, element: word)
let array = CMOCVariableDebug.CType(name: "int[3]", kind: "array", size: 6, count: 3, element: word)
let variables: [CMOCVariableDebug.Variable] = [
.init(name: "global", sourcePath: "main.c", line: 1, type: word, address: 0x3000, offset: nil, scope: nil, isStatic: false),
.init(name: "local", sourcePath: "main.c", line: 2, type: word, address: nil, offset: -2, scope: 0x2800..<0x2820, isStatic: false),
.init(name: "pointer", sourcePath: "main.c", line: 1, type: pointer, address: 0x3002, offset: nil, scope: nil, isStatic: false),
.init(name: "array", sourcePath: "main.c", line: 1, type: array, address: 0x3010, offset: nil, scope: nil, isStatic: false)
]
let metadata = CMOCVariableDebug(functions: [.init(name: "main", sourcePath: "main.c", entry: 0x2800, body: 0x2800, end: 0x2820, frame: frame)],
variables: variables, sourceMap: sourceMap)
var memory = Data(repeating: 0, count: 65536), available = Data(repeating: 1, count: 65536)
memory[0x2800] = changedCode ? 0x39 : 0x12
memory[0x3000] = 0xff; memory[0x3001] = 0xf9 // -7, big endian
memory[0x3002] = io ? 0xff : 0x30; memory[0x3003] = io ? 0 : 0x12
memory[0x3ffe] = 0; memory[0x3fff] = 42
for i in 0..<3 { memory[0x3011 + 2*i] = UInt8(11 * (i+1)) }
for i in 0xff00..<0x10000 { available[i] = 0 }
return CMOCWatchEvaluator(metadata: metadata, context: .init(pc: 0x2800, u: 0x4000, memory: memory, available: available))
}
func testSignedBigEndianLocalsArrayAndPointerReads() {
let evaluator = fixture()
XCTAssertEqual(evaluator.evaluate("global").value, "-7")
XCTAssertEqual(evaluator.evaluate("local").value, "42")
XCTAssertEqual(evaluator.evaluate("array").value, "[11, 22, 33]")
XCTAssertEqual(evaluator.evaluate("array[1]").value, "22")
XCTAssertEqual(evaluator.evaluate("*pointer").value, "22")
XCTAssertEqual(evaluator.evaluate("*(pointer + 1)").value, "33")
XCTAssertEqual(evaluator.evaluate("&array[2]").value, "$3014")
XCTAssertEqual(evaluator.visibleVariables().count, 4)
}
func testFlatEntriesAndPublicGlobalsUseCompleteVerifiedCodeAndSharedStorage() throws {
let code = Array(repeating: UInt8(0x12), count: 8)
let map = CMOCProjectSourceMap(entryPoint: 0x2800, binarySHA256: "binary", sourceHashes: [:],
instructions: [.init(sourcePath: "main.c", sourceLine: 3, address: 0x2802, bytes: [0x12], assembly: "NOP")],
objectHashes: [:], listingHashes: [:], linkMapSHA256: "map",
flatCodeGuards: [.init(address: 0x2800, bytes: code)])
let function = CMOCVariableDebug.Function(name: "main", sourcePath: "main.c", entry: 0x2800,
body: 0x2802, end: 0x2808, frame: false, sourceLine: 2)
let unavailable = CMOCVariableDebug.Function(name: "outside", sourcePath: "main.c", entry: 0x2800,
body: 0x2800, end: 0x2809, frame: false)
let metadata = CMOCVariableDebug(functions: [function, unavailable], variables: [
.init(name: "known", sourcePath: "main.c", line: 1, type: word, address: 0x3000,
offset: nil, scope: nil, isStatic: false),
.init(name: "hidden", sourcePath: "private.c", line: 1, type: word, address: 0x3002,
offset: nil, scope: nil, isStatic: false),
.init(name: "unknown", sourcePath: "main.c", line: 1, type: word, address: 0x6000,
offset: nil, scope: nil, isStatic: false)
], sourceMap: map)
XCTAssertEqual(metadata.functionEntryNames, ["main"])
XCTAssertEqual(metadata.breakpointInstruction(named: "main")?.address, 0x2802)
XCTAssertEqual(metadata.entryFunction(containing: 0x2800)?.name, "main")
let receipt = CMOCPublicDebugEvidence.fields(map: map, metadata: metadata, variableError: nil, sharedPaths: ["main.c"])
XCTAssertEqual(receipt["function_entries"] as? [String], ["main"])
XCTAssertEqual((receipt["globals"] as? [[String: Any]])?.count, 2)
var memory = Data(repeating: 0, count: 65536), available = Data(repeating: 1, count: 65536)
memory.replaceSubrange(0x2800..<0x2808, with: code)
memory[0x3000] = 0; memory[0x3001] = 42
for i in 0xff00..<0x10000 { available[i] = 0 }
let context = CMOCWatchEvaluator.Context(pc: 0x2800, u: 0x4000, memory: memory, available: available)
let values = CMOCPublicDebugEvidence.globals(metadata: metadata, sharedPaths: ["main.c"], context: context,
permittedRanges: [0x2800..<0x2808, 0x3000..<0x3002], physicalPages: [1: 57])
XCTAssertEqual(values.total, 2)
let known = try XCTUnwrap(values.items.first { $0["name"] as? String == "known" })
XCTAssertEqual(known["value"] as? String, "42")
XCTAssertEqual(known["physical_address"] as? Int, 57 * 8192 + 0x1000)
XCTAssertEqual(values.items.first { $0["name"] as? String == "unknown" }?["status"] as? String, "unavailable")
XCTAssertFalse(values.items.contains { $0["name"] as? String == "hidden" })
memory[0x2800] = 0x39 // Unmapped prologue byte, outside the lone C statement.
XCTAssertFalse(map.verifiesRuntime(memory: memory, available: available))
let stale = CMOCPublicDebugEvidence.globals(metadata: metadata, sharedPaths: ["main.c"],
context: .init(pc: 0x2800, u: 0x4000, memory: memory, available: available),
permittedRanges: [0x3000..<0x3002], physicalPages: [1: 57])
XCTAssertEqual(stale.items.first { $0["name"] as? String == "known" }?["status"] as? String, "unavailable")
}
func testBankedMetadataProjectionHidesWrongBankGlobalsAndLexicalScopes() {
let layout = CMOCBankedDebugLayout(entryPoint: 0x4000, binarySHA256: "", linkMap: Data(), sourceHashes: [:], artifactHashes: [:], modules: [
.init(id: "resident", sections: ["code", "bss"], codeSections: ["code"], logicalAddress: 0x2800, bytes: [0x12], pages: [.init(logicalPage: 1, physicalPage: 0x39)], codeGuards: [.init(address: 0x2800, bytes: [0x12])]),
.init(id: "overlay", sections: ["overlay"], codeSections: ["overlay"], logicalAddress: 0x4000, bytes: [0x12], pages: [.init(logicalPage: 2, physicalPage: 0x20)], codeGuards: [.init(address: 0x4000, bytes: [0x12])])
])
let map = CMOCProjectSourceMap(entryPoint: 0x2800, binarySHA256: "", sourceHashes: [:], instructions: [], objectHashes: [:], listingHashes: [:], linkMapSHA256: "", bankedLayout: layout)
let metadata = CMOCVariableDebug(functions: [
.init(name: "resident", sourcePath: "main.c", entry: 0x2800, body: 0x2800, end: 0x2801, frame: true, moduleID: "resident"),
.init(name: "overlay", sourcePath: "overlay.c", entry: 0x4000, body: 0x4000, end: 0x4001, frame: true, moduleID: "overlay")
], variables: [
.init(name: "global", sourcePath: "main.c", line: 1, type: word, address: 0x3000, offset: nil, scope: nil, isStatic: false, moduleID: "resident"),
.init(name: "overlayGlobal", sourcePath: "overlay.c", line: 1, type: word, address: 0x4100, offset: nil, scope: nil, isStatic: false, moduleID: "overlay"),
.init(name: "staticLocal", sourcePath: "overlay.c", line: 1, type: word, address: 0x3002, offset: nil, scope: 0x4000..<0x4001, isStatic: true, moduleID: "resident", scopeModuleID: "overlay")
], sourceMap: map)
let wrong = metadata.projected(physicalPages: [1: 0x39, 2: 0x21])
XCTAssertEqual(wrong.variables.map(\.name), ["global"])
XCTAssertEqual(wrong.functions.map(\.name), ["resident"])
XCTAssertEqual(metadata.projected(physicalPages: [1: 0x39, 2: 0x20]).variables.count, 3)
XCTAssertTrue(metadata.projected(physicalPages: [1: 0x38, 2: 0x21]).variables.isEmpty)
}
func testBreakpointFunctionCatalogRequiresUniqueMappedBodyInItsModule() {
let map = CMOCProjectSourceMap(entryPoint: 0x4000, binarySHA256: "", sourceHashes: [:], instructions: [
.init(sourcePath: "main.c", sourceLine: 1, address: 0x4001, bytes: [0x12], assembly: "NOP", moduleID: "resident"),
.init(sourcePath: "main.c", sourceLine: 2, address: 0x4004, bytes: [0x12], assembly: "NOP", moduleID: "resident"),
.init(sourcePath: "main.c", sourceLine: 2, address: 0x4002, bytes: [0x12], assembly: "NOP", moduleID: "resident"),
.init(sourcePath: "main.c", sourceLine: 3, address: 0x4008, bytes: [0x12], assembly: "NOP", moduleID: "resident"),
.init(sourcePath: "overlay.c", sourceLine: 1, address: 0x5000, bytes: [0x12], assembly: "NOP", moduleID: "overlay")
], objectHashes: [:], listingHashes: [:], linkMapSHA256: "")
let main = CMOCVariableDebug.Function(name: "main", sourcePath: "main.c", entry: 0x4000, body: 0x4002,
end: 0x4008, frame: false, moduleID: "resident")
let assembly = CMOCVariableDebug.Function(name: "raw", sourcePath: "main.c", entry: 0x5000, body: 0x5000,
end: 0x5002, frame: false, moduleID: "resident")
let metadata = CMOCVariableDebug(functions: [assembly, main], variables: [], sourceMap: map)
XCTAssertEqual(metadata.breakpointInstruction(named: "main")?.address, 0x4002)
XCTAssertEqual(metadata.breakpointInstruction(named: "main")?.moduleID, "resident")
XCTAssertNil(metadata.breakpointInstruction(named: "raw"), "An instruction at the same address in another module is not a valid body")
XCTAssertNil(metadata.breakpointInstruction(named: "missing"))
XCTAssertEqual(metadata.breakpointFunctionNames, ["main"])
let duplicate = CMOCVariableDebug(functions: [main, main], variables: [], sourceMap: map)
XCTAssertNil(duplicate.breakpointInstruction(named: "main"))
XCTAssertTrue(duplicate.breakpointFunctionNames.isEmpty)
}
func testFunctionEntryStopsRequireExecutableBankedRangesAndUnambiguousActivePC() {
let executable = CMOCBankedDebugLayout.Module(id: "assembly", sections: ["code"], codeSections: ["code"],
logicalAddress: 0x4000, bytes: Array(repeating: 0x12, count: 8), pages: [.init(logicalPage: 2, physicalPage: 4)],
codeGuards: [.init(address: 0x4000, bytes: [0x12, 0x12]), .init(address: 0x4002, bytes: Array(repeating: 0x12, count: 6))])
let bss = CMOCBankedDebugLayout.Module(id: "state", sections: ["bss"], codeSections: [], logicalAddress: 0x6000,
bytes: [], pages: [.init(logicalPage: 3, physicalPage: 5)], codeGuards: [])
let function = CMOCVariableDebug.Function(name: "raw", sourcePath: "main.c", entry: 0x4000, body: 0x4002,
end: 0x4008, frame: false, moduleID: "assembly", sourceLine: 12)
let invalid = [
CMOCVariableDebug.Function(name: "data", sourcePath: "main.c", entry: 0x6000, body: 0x6000, end: 0x6002, frame: false, moduleID: "state"),
.init(name: "gap", sourcePath: "main.c", entry: 0x4000, body: 0x4000, end: 0x4009, frame: false, moduleID: "assembly"),
.init(name: "io", sourcePath: "main.c", entry: 0xfdff, body: 0xfdff, end: 0xfe01, frame: false, moduleID: "assembly"),
.init(name: "empty", sourcePath: "main.c", entry: 0x4000, body: 0x4000, end: 0x4000, frame: false, moduleID: "assembly"),
.init(name: "negative", sourcePath: "main.c", entry: -1, body: 0, end: 1, frame: false, moduleID: "assembly"),
.init(name: "unknown", sourcePath: "main.c", entry: 0x4000, body: 0x4000, end: 0x4001, frame: false, moduleID: "absent")
]
func metadata(_ functions: [CMOCVariableDebug.Function], executable override: CMOCBankedDebugLayout.Module? = nil) -> CMOCVariableDebug {
let layout = CMOCBankedDebugLayout(entryPoint: 0x4000, binarySHA256: "", linkMap: Data(), sourceHashes: [:],
artifactHashes: [:], modules: [override ?? executable, bss])
let map = CMOCProjectSourceMap(entryPoint: 0x4000, binarySHA256: "", sourceHashes: [:], instructions: [],
objectHashes: [:], listingHashes: [:], linkMapSHA256: "", bankedLayout: layout)
return CMOCVariableDebug(functions: functions, variables: [
.init(name: "fileState", sourcePath: "main.c", line: 1, type: word, address: 0x6000,
offset: nil, scope: nil, isStatic: true, moduleID: "state"),
.init(name: "otherState", sourcePath: "other.c", line: 1, type: word, address: 0x6002,
offset: nil, scope: nil, isStatic: true, moduleID: "state"),
.init(name: "local", sourcePath: "main.c", line: 12, type: word, address: nil,
offset: -2, scope: 0x4000..<0x4008, isStatic: false, moduleID: "assembly", scopeModuleID: "assembly")
], sourceMap: map)
}
let complete = metadata([function] + invalid)
XCTAssertEqual(complete.entryFunction(named: "raw")?.entry, 0x4000)
XCTAssertEqual(complete.entryFunction(named: "raw")?.sourceLine, 12)
XCTAssertEqual(complete.functionEntryNames, ["raw"])
XCTAssertNil(complete.breakpointInstruction(named: "raw"), "An entry stop is not an invented C statement")
XCTAssertNil(complete.entryFunction(containing: 0x4000), "A live function claim requires a physical projection")
var memory = Data(repeating: 0, count: 65536)
memory.replaceSubrange(0x4000..<0x4008, with: repeatElement(UInt8(0x12), count: 8))
let available = Data(repeating: 1, count: 65536)
let active = complete.projected(physicalPages: [2: 4, 3: 5], memory: memory, available: available)
XCTAssertEqual(active.entryFunction(containing: 0x4000)?.name, "raw", "Prologue bytes precede the C body but belong to the executable function")
XCTAssertEqual(active.entryFunction(containing: 0x4007)?.name, "raw")
XCTAssertNil(active.entryFunction(containing: 0x4008))
let entryWatch = CMOCWatchEvaluator(metadata: active,
context: .init(pc: 0x4000, u: 0x7000, memory: memory, available: available), frameLocalsAvailable: false)
XCTAssertEqual(entryWatch.evaluate("fileState").value, "0", "A verified prologue retains file-static global scope")
XCTAssertNotNil(entryWatch.evaluate("otherState").error)
XCTAssertNotNil(entryWatch.evaluate("local").error, "A function entry does not establish stack-local storage")
let wrong = complete.projected(physicalPages: [2: 6, 3: 5], memory: memory, available: available)
XCTAssertNil(wrong.entryFunction(named: "raw")); XCTAssertNil(wrong.entryFunction(containing: 0x4000))
XCTAssertTrue(wrong.functionEntryNames.isEmpty)
let overlap = CMOCVariableDebug.Function(name: "overlap", sourcePath: "main.c", entry: 0x4002, body: 0x4002,
end: 0x4004, frame: false, moduleID: "assembly", sourceLine: 18)
let overlapping = metadata([function, overlap]).projected(physicalPages: [2: 4], memory: memory, available: available)
XCTAssertNil(overlapping.entryFunction(containing: 0x4002), "Overlapping records cannot choose an arbitrary function")
XCTAssertEqual(overlapping.entryFunction(containing: 0x4000)?.name, "raw")
let duplicate = metadata([function, function]).projected(physicalPages: [2: 4], memory: memory, available: available)
XCTAssertNil(duplicate.entryFunction(named: "raw")); XCTAssertNil(duplicate.entryFunction(containing: 0x4000))
XCTAssertTrue(duplicate.functionEntryNames.isEmpty)
let missingPage = CMOCBankedDebugLayout.Module(id: executable.id, sections: executable.sections, codeSections: executable.codeSections,
logicalAddress: executable.logicalAddress, bytes: executable.bytes, pages: [.init(logicalPage: 3, physicalPage: 4)], codeGuards: executable.codeGuards)
XCTAssertNil(metadata([function], executable: missingPage).entryFunction(named: "raw"))
let invalidPage = CMOCBankedDebugLayout.Module(id: executable.id, sections: executable.sections, codeSections: executable.codeSections,
logicalAddress: executable.logicalAddress, bytes: executable.bytes, pages: [.init(logicalPage: 2, physicalPage: -1)], codeGuards: executable.codeGuards)
XCTAssertNil(metadata([function], executable: invalidPage).entryFunction(named: "raw"))
}
func testReadOnlyExpressionsAndCheckedWideArithmetic() {
let evaluator = fixture()
XCTAssertEqual(evaluator.evaluate("(local + global) * 2").value, "70")
XCTAssertEqual(evaluator.evaluate("32767 + 1").typeName, "watch integer (64-bit)")
XCTAssertEqual(evaluator.evaluate("32767 + 1").value, "32768")
XCTAssertEqual(evaluator.evaluate("0 && (1 / 0)").value, "0")
XCTAssertEqual(evaluator.evaluate("1 || missing").value, "1")
for expression in ["++local", "--local", "local++", "global=3", "main()", "1/0", "array[3]", "array[-1]", "1 << 40", "9223372036854775807+1", "9223372036854775807 << 1"] {
XCTAssertNotNil(evaluator.evaluate(expression).error, expression)
}
}
func testUnavailableIOStaleCodeAndMissingFramesFailExplicitly() {
XCTAssertNotNil(fixture(io: true).evaluate("*pointer").error)
XCTAssertNotNil(fixture(changedCode: true).evaluate("global").error)
XCTAssertNotNil(fixture(frame: false).evaluate("local").error)
XCTAssertNil(fixture(frame: false).evaluate("global").error)
let empty = CMOCWatchEvaluator(metadata: fixture().metadata, context: .init(pc: 0x2800, u: 0, memory: Data(), available: Data()))
XCTAssertNotNil(empty.evaluate("1").error)
XCTAssertNotNil(fixture().evaluate(String(repeating: "(", count: 40) + "1" + String(repeating: ")", count: 40)).error)
}
private func parse(_ type: [String: Any], mutateListing: Bool = false, tamperSymbol: Bool = false) throws -> CMOCVariableDebug {
let directory = FileManager.default.temporaryDirectory.appendingPathComponent("VCCVariableMetadata-" + UUID().uuidString)
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: directory) }
let source = Data("int main(void) { return 0; }\n".utf8), binary = Data([1,2,3])
func c(_ value: String) -> [UInt8] { Array(value.utf8) + [0] }
let code = c("code") + [0] + c("_main") + [0,0] + c("body") + [0,0] + c("end") + [0,32] + [0,0,0,0,32] + Array(repeating: UInt8(0x12), count: 32)
let bss = c("bss") + [1,0] + c("_data") + [0,0] + [0,0,0,0,2]
let object = Data(c("LWOBJ16") + code + bss + [0])
let records: [[String: Any]] = [
["kind":"function", "name":"main", "location":"main.c:1", "symbol":"_main", "begin":"body", "end":"end", "frame":true],
["kind":"variable", "name":"data", "location":"main.c:1", "symbol":"_data", "begin":"", "end":"", "static":false, "type":type]
]
let listing = try Data(records.map { "* VCCDBG1 " + String(decoding: try JSONSerialization.data(withJSONObject: $0), as: UTF8.self) }.joined(separator: "\n").utf8)
let map = Data(("Section: code (main.o) load at 2800, length 0020\nSection: bss (main.o) load at 3000, length 0002\nSymbol: _main (main.o) = 2800\nSymbol: body (main.o) = 2800\nSymbol: end (main.o) = 2820\nSymbol: _data (main.o) = " + (tamperSymbol ? "3001" : "3000") + "\n").utf8)
try listing.write(to: directory.appendingPathComponent("main.lst")); try object.write(to: directory.appendingPathComponent("main.o")); try map.write(to: directory.appendingPathComponent("main.map"))
let artifact = CMOCBuildArtifact(id: UUID(), directory: directory, source: "main.c", sourceData: source,
sourceSHA256: CMOCSourceMap.sha256(source), binaryData: binary, buildLog: "")
let verified = CMOCProjectSourceMap(entryPoint: 0x2800, binarySHA256: CMOCSourceMap.sha256(binary), sourceHashes: ["main.c":CMOCSourceMap.sha256(source)],
instructions: [], objectHashes: ["main.o":CMOCSourceMap.sha256(object)], listingHashes: ["main.o":CMOCSourceMap.sha256(listing)], linkMapSHA256: CMOCSourceMap.sha256(map))
if mutateListing { try Data((String(decoding: listing, as: UTF8.self) + "\n").utf8).write(to: directory.appendingPathComponent("main.lst")) }
return try CMOCVariableDebug.parse(artifact: artifact, verifiedMap: verified)
}
func testMetadataBindsToArtifactsAndRejectsOverflowingTypeLayouts() throws {
let integer: [String: Any] = ["name":"int", "kind":"integer", "size":2, "signed":true]
XCTAssertEqual(try parse(integer).variables[0].address, 0x3000)
XCTAssertThrowsError(try parse(integer, mutateListing: true))
XCTAssertThrowsError(try parse(integer, tamperSymbol: true), "A forged unused map symbol must fail even when its hash agrees")
XCTAssertThrowsError(try parse(["name":"bad", "kind":"array", "size":2, "count":65535,
"element":["name":"bad", "kind":"integer", "size":Int.max, "signed":true]]))
XCTAssertThrowsError(try parse(["name":"bad", "kind":"aggregate", "size":2,
"members":[["name":"x", "offset":Int.max, "type":integer]]]))
var nested = integer
for _ in 0..<30 { nested = ["name":"pointer", "kind":"pointer", "size":2, "element":nested] }
XCTAssertThrowsError(try parse(nested))
}
func testFloatingMetadataRequiresExplicitEncodingAndConsistentStorage() throws {
func floating(_ size: Int, _ format: String? = nil) -> [String: Any] {
var type: [String: Any] = ["name": "float", "kind": "floating", "size": size]
if let format { type["floatingFormat"] = format }
return type
}
XCTAssertNil(try parse(floating(5)).variables[0].type.floatingFormat)
XCTAssertEqual(try parse(floating(5, "coco-basic-40")).variables[0].type.floatingFormat, "coco-basic-40")
XCTAssertEqual(try parse(floating(4, "ieee754-binary32-be")).variables[0].type.floatingFormat, "ieee754-binary32-be")
XCTAssertEqual(try parse(floating(8, "unsupported")).variables[0].type.floatingFormat, "unsupported")
XCTAssertEqual(try parse(floating(5, "future-format")).variables[0].type.floatingFormat, "future-format")
XCTAssertThrowsError(try parse(floating(4, "coco-basic-40")))
XCTAssertThrowsError(try parse(floating(5, "ieee754-binary32-be")))
XCTAssertThrowsError(try parse(floating(5, String(repeating: "x", count: 129))))
}
func testCompilerTagsActualFloatingFormatsIncludingNestedTypes() throws {
let runtime = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent()
.appendingPathComponent("LocalResources/ToolchainBundle")
let compiler = runtime.appendingPathComponent("bin/cmoc")
try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: compiler.path), "Staged compiler required")
let root = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCFloatFormats-" + UUID().uuidString)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: root) }
let source = """
float positive = 1.5f;
float negative = -2.25f;
double alias = 3.5f;
struct Values { float item; float samples[2]; } values;
int main(void) { float local = 0.5f; return local > 0; }
"""
for (mode, flags, format, size, positive, negative) in [
("basic", [String](), "coco-basic-40", 5, "$81,$40,$00,$00,$00", "$82,$90,$00,$00,$00"),
("native", ["--native-float"], "coco-basic-40", 5, "$81,$40,$00,$00,$00", "$82,$90,$00,$00,$00"),
("mc6839", ["--mc6839"], "ieee754-binary32-be", 4, "$3F,$C0,$00,$00", "$C0,$10,$00,$00")
] {
let directory = root.appendingPathComponent(mode)
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
try Data(source.utf8).write(to: directory.appendingPathComponent("probe.c"))
let process = Process(), output = Pipe()
process.executableURL = compiler; process.currentDirectoryURL = directory
process.arguments = flags + ["-O0", "-fno-omit-frame-pointer", "--intermediate", "-c", "probe.c"]
process.standardOutput = output; process.standardError = output
try process.run()
let log = String(decoding: output.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self)
process.waitUntilExit()
XCTAssertEqual(process.terminationStatus, 0, log)
let assembly = try String(contentsOf: directory.appendingPathComponent("probe.s"), encoding: .utf8)
var types: [String: CMOCVariableDebug.CType] = [:]
for line in assembly.components(separatedBy: .newlines) {
guard let marker = line.range(of: "* VCCDBG1 ") else { continue }
let payload = Data(line[marker.upperBound...].utf8)
let record = try XCTUnwrap(JSONSerialization.jsonObject(with: payload) as? [String: Any])
guard let name = record["name"] as? String, let rawType = record["type"] else { continue }
types[name] = try JSONDecoder().decode(CMOCVariableDebug.CType.self,
from: JSONSerialization.data(withJSONObject: rawType))
}
for name in ["positive", "negative", "local", "alias"] {
let type = try XCTUnwrap(types[name], name)
XCTAssertEqual(type.kind, "floating"); XCTAssertEqual(type.floatingFormat, format)
XCTAssertEqual(type.size, size)
}
// CMOC 0.1.100 lowers source-level double to float, rather than
// providing an independent 64-bit target type.
XCTAssertEqual(types["alias"]?.name, "float")
XCTAssertEqual(types["values"]?.members?[0].type.floatingFormat, format)
XCTAssertEqual(types["values"]?.members?[1].type.element?.floatingFormat, format)
XCTAssertTrue(assembly.contains(positive)); XCTAssertTrue(assembly.contains(negative))
}
}
func testMetadataPathsDoNotDependOnLiveIncrementalWorkspaceLifetime() async throws {
let resources = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("LocalResources")
let toolchain = resources.appendingPathComponent("ToolchainBundle")
try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: toolchain.appendingPathComponent("bin/cmoc").path), "Staged compiler required")
let root = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCMetadataLive-" + UUID().uuidString)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: root) }
let project = try CMOCProject.create(at: root.appendingPathComponent("Project"))
let main = "#include <cmoc.h>\n#include <coco.h>\nunsigned int score = 5;\nunsigned int transform(unsigned int);\nint main(void) { unsigned int round = 2; initCoCoSupport(); score = transform(round); return score; }\n"
let helper = "extern unsigned int score;\nunsigned int transform(unsigned int value) { unsigned int local = value + 5; score = local; return local * 2; }\n"
try Data(main.utf8).write(to: project.sourceURL)
try Data(helper.utf8).write(to: project.directory.appendingPathComponent("helper.c"))
let makefile = "CMOC ?= cmoc\nbuild: main.bin\nmain.bin: main.o helper.o\n\t$(CMOC) --intermediate -o $@ $^\n%.o: %.c\n\t$(CMOC) -O0 -fno-omit-frame-pointer --intermediate -c -o $@ $<\n"
try Data(makefile.utf8).write(to: project.makefileURL)
let service = CMOCBuildService(toolchain: toolchain, buildRoot: root.appendingPathComponent("Builds"))
let artifact = try await service.build(project: project, overlays: [:])
try withExtendedLifetime(service) {
XCTAssertTrue(FileManager.default.fileExists(atPath: artifact.compilationDirectory.appendingPathComponent("helper.c").path))
let map = try CMOCProjectSourceMap.parse(artifact: artifact)
let metadata = try CMOCVariableDebug.parse(artifact: artifact, verifiedMap: map)
XCTAssertEqual(Set(metadata.functions.map(\.name)), ["main", "transform"])
XCTAssertTrue(metadata.variables.contains { $0.name == "local" && $0.sourcePath == "helper.c" })
// A later incremental build may remove a source. Frozen debug
// artifacts remain valid even when the original physical path goes away.
try FileManager.default.removeItem(at: artifact.compilationDirectory.appendingPathComponent("helper.c"))
XCTAssertEqual(try CMOCVariableDebug.parse(artifact: artifact, verifiedMap: map).variables.count, metadata.variables.count)
}
}
func testLongCompilerTypeRecordsUseOnlyVerifiedMatchingAssembly() async throws {
let resources = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent()
.appendingPathComponent("LocalResources")
let toolchain = resources.appendingPathComponent("ToolchainBundle")
try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: toolchain.appendingPathComponent("bin/cmoc").path), "Staged compiler required")
let root = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCLongMetadata-" + UUID().uuidString)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: root) }
let project = try CMOCProject.create(at: root.appendingPathComponent("Project"))
let members = (0..<80).map { "unsigned char member\($0);" }.joined(separator: "\n")
let source = "struct Pilot {\n" + members + "\n};\nstruct Pilot pilot;\nstatic asm unsigned raw_probe(void) { asm { ldd #7 } }\nint main(void) { pilot.member79 = raw_probe(); return pilot.member79; }\n"
try Data(source.utf8).write(to: project.sourceURL)
let service = CMOCBuildService(toolchain: toolchain, buildRoot: root.appendingPathComponent("Builds"))
let artifact = try await service.build(project: project, overlays: [:])
let map = try CMOCProjectSourceMap.parse(artifact: artifact)
let assemblyPath = "main.s", listingPath = "main.lst"
let original = try XCTUnwrap(artifact.sources[assemblyPath], "Compiler assembly must be part of the frozen build source set")
let assembly = String(decoding: original, as: UTF8.self)
let listing = try String(contentsOf: artifact.fileURL(listingPath), encoding: .utf8)
func records(_ text: String) -> [String] {
text.components(separatedBy: .newlines).compactMap { row in
row.range(of: "* VCCDBG1 ").map { String(row[$0.upperBound...]) }
}
}
XCTAssertTrue(records(assembly).contains { $0.utf8.count > 2048 })
XCTAssertTrue(records(listing).contains { $0.utf8.count == 2038 }, "The real assembler truncates long comment rows at 2,048 input bytes")
let metadata = try CMOCVariableDebug.parse(artifact: artifact, verifiedMap: map)
let pilot = try XCTUnwrap(metadata.variables.first { $0.name == "pilot" })
XCTAssertEqual(pilot.type.size, 80)
XCTAssertEqual(pilot.type.members?.count, 80)
XCTAssertEqual(pilot.type.members?.last?.name, "member79")
XCTAssertTrue(metadata.functions.contains { $0.name == "raw_probe" }, "Pure assembly still has honest compiler function metadata")
XCTAssertNil(metadata.breakpointInstruction(named: "raw_probe"))
XCTAssertFalse(metadata.breakpointFunctionNames.contains("raw_probe"))
XCTAssertNotNil(metadata.breakpointInstruction(named: "main"))
XCTAssertTrue(metadata.breakpointFunctionNames.contains("main"))
let mainFunction = try XCTUnwrap(metadata.functions.first { $0.name == "main" })
let duplicate = CMOCVariableDebug(functions: metadata.functions + [mainFunction], variables: metadata.variables, sourceMap: map)
XCTAssertNil(duplicate.breakpointInstruction(named: "main"))
XCTAssertFalse(duplicate.breakpointFunctionNames.contains("main"))
XCTAssertNotNil(metadata.entryFunction(named: "raw_probe"), "Complete verified flat executable guards establish honest entry provenance")
var declarationOnly = map; declarationOnly.flatCodeGuards = []
let unguarded = CMOCVariableDebug(functions: metadata.functions, variables: metadata.variables, sourceMap: declarationOnly)
XCTAssertNil(unguarded.entryFunction(named: "raw_probe"), "Compiler symbols alone do not establish executable entry provenance")
// Reuse the real compiler result to qualify assembly entries against
// executable guards derived from the independently verified image.
var binaryBytes: [Int: UInt8] = [:], cursor = 0
while cursor + 5 <= artifact.binaryData.count {
let type = artifact.binaryData[cursor]
let length = Int(artifact.binaryData[cursor + 1]) * 256 + Int(artifact.binaryData[cursor + 2])
let address = Int(artifact.binaryData[cursor + 3]) * 256 + Int(artifact.binaryData[cursor + 4])
cursor += 5
if type == 255 { break }
guard type == 0, cursor + length <= artifact.binaryData.count else { return XCTFail("Unexpected compiler DECB result") }
for offset in 0..<length { binaryBytes[address + offset] = artifact.binaryData[cursor + offset] }
cursor += length
}
let guards = try metadata.functions.map { function -> CMOCBankedDebugLayout.CodeGuard in
XCTAssertGreaterThan(function.end, function.entry)
let code = try (function.entry..<function.end).map { try XCTUnwrap(binaryBytes[$0]) }
return .init(address: UInt16(function.entry), bytes: code)
}
let logicalPages = Set(metadata.functions.flatMap { Array($0.entry / 8192...($0.end - 1) / 8192) }).sorted()
let pages = logicalPages.map { CMOCBankedDebugLayout.Page(logicalPage: $0, physicalPage: 0x38 + $0) }
let module = CMOCBankedDebugLayout.Module(id: "compiled", sections: ["code"], codeSections: ["code"],
logicalAddress: Int(guards.first?.address ?? 0), bytes: [], pages: pages, codeGuards: guards)
let layout = CMOCBankedDebugLayout(entryPoint: map.entryPoint, binarySHA256: map.binarySHA256, linkMap: Data(),
sourceHashes: map.sourceHashes, artifactHashes: [:], modules: [module])
let bankedMap = CMOCProjectSourceMap(entryPoint: map.entryPoint, binarySHA256: map.binarySHA256, sourceHashes: map.sourceHashes,
instructions: map.instructions.map { item in var item = item; item.moduleID = "compiled"; return item },
objectHashes: map.objectHashes, listingHashes: map.listingHashes, linkMapSHA256: map.linkMapSHA256, bankedLayout: layout)
let banked = CMOCVariableDebug(functions: metadata.functions.map { function in var function = function; function.moduleID = "compiled"; return function },
variables: metadata.variables, sourceMap: bankedMap)
let assemblyFunction = try XCTUnwrap(banked.entryFunction(named: "raw_probe"))
XCTAssertNotNil(assemblyFunction.sourceLine)
XCTAssertEqual(assemblyFunction.sourceLine, source.components(separatedBy: .newlines).firstIndex(where: { $0.contains("static asm unsigned raw_probe") }).map { $0 + 1 })
XCTAssertTrue(banked.functionEntryNames.contains("raw_probe"))
XCTAssertNil(banked.breakpointInstruction(named: "raw_probe"))
var runtimeMemory = Data(repeating: 0, count: 65536)
for (address, byte) in binaryBytes { runtimeMemory[address] = byte }
let runtimeAvailable = Data(repeating: 1, count: 65536)
let physicalPages = Dictionary(uniqueKeysWithValues: pages.map { ($0.logicalPage, $0.physicalPage) })
let live = banked.projected(physicalPages: physicalPages, memory: runtimeMemory, available: runtimeAvailable)
XCTAssertEqual(live.entryFunction(containing: UInt16(assemblyFunction.entry))?.name, "raw_probe")
var wrongPages = physicalPages
let firstPage = try XCTUnwrap(logicalPages.first)
wrongPages[firstPage] = (physicalPages[firstPage] ?? 0) + 1
XCTAssertNil(banked.projected(physicalPages: wrongPages, memory: runtimeMemory, available: runtimeAvailable).entryFunction(named: "raw_probe"))
// A changed retained file cannot supply metadata, even if its only
// change leaves all parsed records and machine code unchanged.
try Data((assembly + "\n").utf8).write(to: artifact.fileURL(assemblyPath))
XCTAssertThrowsError(try CMOCVariableDebug.parse(artifact: artifact, verifiedMap: map)) { error in
XCTAssertTrue(error.localizedDescription.contains("assembly changed"))
}
try original.write(to: artifact.fileURL(assemblyPath))
func evidence(_ sources: [String: Data]) -> (CMOCBuildArtifact, CMOCProjectSourceMap) {
let copy = CMOCBuildArtifact(id: artifact.id, directory: artifact.directory, source: artifact.source,
sourceData: artifact.sourceData, sourceSHA256: artifact.sourceSHA256, binaryData: artifact.binaryData,
buildLog: artifact.buildLog, sources: sources, fileHashes: artifact.fileHashes,
compilationDirectory: artifact.compilationDirectory, entrySourcePath: artifact.entrySourcePath,
binaryRelativePath: artifact.binaryRelativePath, diskRelativePath: artifact.diskRelativePath,
outputPaths: artifact.outputPaths, configurationName: artifact.configurationName, action: artifact.action)
let verified = CMOCProjectSourceMap(entryPoint: map.entryPoint, binarySHA256: map.binarySHA256,
sourceHashes: sources.mapValues(CMOCSourceMap.sha256), instructions: map.instructions,
objectHashes: map.objectHashes, listingHashes: map.listingHashes, linkMapSHA256: map.linkMapSHA256)
return (copy, verified)
}
// A stale immutable source set is rejected before assembly recovery.
var forged = artifact.sources
forged[assemblyPath] = Data((assembly + "\n").utf8)
XCTAssertThrowsError(try CMOCVariableDebug.parse(artifact: evidence(forged).0, verifiedMap: map))
// Matching hashes alone are insufficient: records must still correspond
// to the independently verified listing in count and order.
var rows = assembly.components(separatedBy: .newlines)
let positions = rows.indices.filter { rows[$0].contains("* VCCDBG1 ") }
let first = try XCTUnwrap(positions.first), second = try XCTUnwrap(positions.dropFirst().first)
rows.swapAt(first, second)
let reordered = Data(rows.joined(separator: "\n").utf8)
forged[assemblyPath] = reordered
try reordered.write(to: artifact.fileURL(assemblyPath))
var candidate = evidence(forged)
XCTAssertThrowsError(try CMOCVariableDebug.parse(artifact: candidate.0, verifiedMap: candidate.1)) { error in
XCTAssertTrue(error.localizedDescription.contains("differs from the verified listing"))
}
rows.remove(at: first)
let removed = Data(rows.joined(separator: "\n").utf8)
forged[assemblyPath] = removed
try removed.write(to: artifact.fileURL(assemblyPath))
candidate = evidence(forged)
XCTAssertThrowsError(try CMOCVariableDebug.parse(artifact: candidate.0, verifiedMap: candidate.1)) { error in
XCTAssertTrue(error.localizedDescription.contains("record count differs"))
}
try original.write(to: artifact.fileURL(assemblyPath))
var unhashed = artifact.sources
unhashed.removeValue(forKey: assemblyPath)
candidate = evidence(unhashed)
XCTAssertThrowsError(try CMOCVariableDebug.parse(artifact: candidate.0, verifiedMap: candidate.1)) { error in
XCTAssertTrue(error.localizedDescription.contains("invalid or truncated JSON"))
XCTAssertTrue(error.localizedDescription.contains(listingPath))
}
}
}