// SPDX-License-Identifier: GPL-3.0-or-later import XCTest final class CMOCSourceMapTests: XCTestCase { /// Explicit opt-in diagnostic for any retained custom project's immutable /// build result. No project Makefile is executed and no emulator is loaded. func testExplicitlySuppliedBankedBuildEvidence() throws { guard let path = ProcessInfo.processInfo.environment["VCC_TEST_BANKED_ARTIFACT"] else { throw XCTSkip("Supply a retained banked artifact directory for project qualification.") } let directory = URL(fileURLWithPath: path).resolvingSymlinksInPath() let project = try CMOCProject.open(at: directory) let files = try CMOCProject.files(in: directory) let sources = files.filter { CMOCProject.isSource($0.key) && String(data: $0.value, encoding: .utf8) != nil && !$0.value.contains(0) } let settings = project.configuration let entry = try XCTUnwrap(sources[settings.entrySource]) let binaryPath = try XCTUnwrap(settings.binary) let binary = try XCTUnwrap(files[binaryPath]) // Compiler source markers retain the original isolated working folder. // Recover it from a full, retained metadata record rather than assuming // it is the current output folder or a historical machine path. var compilationDirectory: URL? for (name, data) in sources where name.hasSuffix(".s") { guard let text = String(data: data, encoding: .utf8) else { continue } for row in text.components(separatedBy: .newlines) { guard let marker = row.range(of: "* VCCDBG1 "), let record = try? JSONSerialization.jsonObject(with: Data(row[marker.upperBound...].utf8)) as? [String: Any], let location = record["location"] as? String, let colon = location.lastIndex(of: ":") else { continue } let sourcePath = String(location[.. CMOCSourceMap { try CMOCSourceMap.parse(source: source, sourceName: sourceName, listing: listing, object: object, objectName: "main.o", linkMap: map, binary: binary) } } private func word(_ n: Int) -> [UInt8] { [UInt8(truncatingIfNeeded: n >> 8), UInt8(truncatingIfNeeded: n)] } private func cString(_ text: String) -> [UInt8] { Array(text.utf8) + [0] } private func object(_ sections: [Section]) -> Data { var result = cString("LWOBJ16") for section in sections { result += cString(section.name) result += section.bssLength == nil ? [0] : [1, 0] for (name, offset) in section.locals { result += cString(name) + word(offset) } result += [0] for (name, offset) in section.exports { result += cString(name) + word(offset) } result += [0] // export terminator for (offset, expression) in section.relocations { result += expression + [0] + word(offset) } result += [0] + word(section.bssLength ?? section.bytes.count) if section.bssLength == nil { result += section.bytes } } return Data(result + [0]) } private func binary(_ blocks: [(Int, [UInt8])], entry: Int = 0x2800) -> Data { var result: [UInt8] = [] for (address, bytes) in blocks { result += [0] + word(bytes.count) + word(address) + bytes } return Data(result + [255, 0, 0] + word(entry)) } private func row(_ assembly: String, offset: Int? = nil, bytes: [UInt8] = []) -> String { (offset.map { String(format: "%04X", $0) } ?? " ") + " " + bytes.map { String(format: "%02X", $0) }.joined().padding(toLength: 16, withPad: " ", startingAt: 0) + " ( main.s):00123 " + assembly } private func fixture() -> Fixture { // main.o code is at $4200, not the first 'code' section ($2800). // rodata appears first in the object but after code in final memory. let relativePrint: [UInt8] = [2] + cString("_print") + [5, 4, 2, 1] + word(3) + [4, 2] let localString: [UInt8] = [3] + cString("message") let object = object([ Section(name: "rodata", bytes: [72, 73, 0], locals: [("message", 0)]), Section(name: "code", bytes: [0x17, 0, 0, 0x8e, 0, 0, 0x4f, 0x5f, 0x39], relocations: [(1, relativePrint), (4, localString)]), Section(name: "bss", bytes: [], bssLength: 4) ]) let listing = [row("SECTION rodata"), row("FCB 72,73,0", offset: 0, bytes: [72,73,0]), row("ENDSECTION"), row("SECTION code"), row("* Line main.c:2: function call: print()"), row("LBSR _print", offset: 0, bytes: [0x17, 0, 0]), row("LDX #message", offset: 3, bytes: [0x8e, 0, 0]), row("* Line main.c:3: return with value"), row("CLRA", offset: 6, bytes: [0x4f]), row("CLRB", offset: 7, bytes: [0x5f]), row("RTS", offset: 8, bytes: [0x39]), row("ENDSECTION")] return Fixture(listing: Data(listing.joined(separator: "\n").utf8), object: object, map: Data(""" Section: code (crt.o) load at 2800, length 0001 Section: code (main.o) load at 4200, length 0009 Section: bss (main.o) load at 4400, length 0004 Section: rodata (main.o) load at 4300, length 0003 Symbol: _print (crt.o) = 2800 """.utf8), binary: binary([(0x2800, [0x39]), (0x4200, [0x17, 0xe5, 0xfd, 0x8e, 0x43, 0, 0x4f, 0x5f, 0x39]), (0x4300, [72,73,0])])) } func testObjectAndSectionAwareMappingUsesFinalRelocatedBytes() throws { let result = try fixture().parse() XCTAssertEqual(result.executableLines, [2, 3]) XCTAssertEqual(result.addresses(forLine: 2), [0x4200, 0x4203]) XCTAssertEqual(result.addresses(forLine: 3), [0x4206, 0x4207, 0x4208]) XCTAssertEqual(result.instructions[0].bytes, [0x17, 0xe5, 0xfd]) XCTAssertEqual(result.instructions[1].bytes, [0x8e, 0x43, 0x00]) XCTAssertEqual(result.line(at: 0x4200), 2) XCTAssertNil(result.line(at: 0x4201), "An operand byte is never a source breakpoint") XCTAssertNil(result.line(at: 0x2800), "Runtime objects never acquire the user's source location") XCTAssertNil(result.line(at: 0x4300), "Read-only data is not executable C") XCTAssertEqual(result.entryPoint, 0x2800) } func testBankedManifestVerifiesRelocatedImagesAndKeepsOverlappingPCsDistinct() throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: directory) } let source = Data("void first(void) {}\nvoid second(void) {}\n".utf8) let object = object([ Section(name: "first", bytes: [0x8e, 0, 0, 0x12], relocations: [(1, [2] + cString("_target"))]), Section(name: "second", bytes: [0x39]) ]) let listing = Data([ row("SECTION first"), row("* Line main.c:1"), row("LDX #_target", offset: 0, bytes: [0x8e, 0, 0]), row("ENDSECTION"), row("SECTION second"), row("* Line main.c:2"), row("RTS", offset: 0, bytes: [0x39]), row("ENDSECTION") ].joined(separator: "\n").utf8) let map = Data("Section: first (main.o) load at 4000, length 0004\nSection: second (main.o) load at 4000, length 0001\nSymbol: _target (runtime.o) = 1234\n".utf8) let packed = binary([(0x0e00, [0x12])], entry: 0x0e00) var files = ["game.bin": packed, "game.map": map, "main.o": object, "main.lst": listing, "first.raw": Data([0x8e, 0x12, 0x34, 0x12, 0]), "second.raw": Data([0x39])] func manifest(secondPage: Int = 5) throws -> Data { let value: [String: Any] = [ "version": 1, "entryPoint": 0x0e00, "binary": ["path": "game.bin", "sha256": CMOCSourceMap.sha256(packed)], "linkMap": ["path": "game.map", "sha256": CMOCSourceMap.sha256(map)], "sources": ["main.c": CMOCSourceMap.sha256(source)], "artifacts": files.filter { $0.key != "game.bin" && $0.key != "game.map" }.mapValues(CMOCSourceMap.sha256), "modules": [ ["id": "first", "sections": ["first"], "codeSections": ["first"], "image": ["path": "first.raw", "logicalAddress": 0x4000], "pages": [["logicalPage": 2, "physicalPage": 4]]], ["id": "second", "sections": ["second"], "codeSections": ["second"], "image": ["path": "second.raw", "logicalAddress": 0x4000], "pages": [["logicalPage": 2, "physicalPage": secondPage]]] ] ] return try JSONSerialization.data(withJSONObject: value) } func layout(_ data: Data) throws -> CMOCBankedDebugLayout { for (path, data) in files { try data.write(to: directory.appendingPathComponent(path)) } return try CMOCBankedDebugLayout.parse(data: data, directory: directory, sources: ["main.c": source], binary: packed, binaryPath: "game.bin", allowedOutputs: Array(files.keys)) } let verified = try layout(manifest()) XCTAssertEqual(verified.modules[0].codeGuards, [.init(address: 0x4000, bytes: [0x8e, 0x12, 0x34, 0x12])]) let units = [CMOCProjectSourceMap.TranslationUnit(sourcePath: "main.c", sourceName: "main.c", objectName: "main.o", listing: listing, object: object)] let result = try CMOCProjectSourceMap.parse(sources: ["main.c": source], compilationDirectory: directory, units: units, linkMap: map, binary: packed, bankedLayout: verified) XCTAssertEqual(result.instructions.count, 2) XCTAssertEqual(Set(result.instructions.compactMap(\.moduleID)), ["first", "second"]) XCTAssertEqual(Set(result.instructions.compactMap(\.physicalAddress)), [UInt32(4 * 8192), UInt32(5 * 8192)]) XCTAssertNil(result.location(at: 0x4000), "An unselected overlay PC must remain ambiguous") XCTAssertEqual(result.projected(physicalPages: [2: 4]).location(at: 0x4000)?.line, 1) XCTAssertEqual(result.projected(physicalPages: [2: 5]).location(at: 0x4000)?.line, 2) XCTAssertTrue(result.projected(physicalPages: [2: 6]).instructions.isEmpty) let activeMap = result.projected(physicalPages: [2: 4]) var memory = Data(repeating: 0, count: 65536), available = Data(repeating: 1, count: 65536) memory.replaceSubrange(0x4000..<0x4004, with: [0x8e,0x12,0x34,0x12]); memory[0x5001] = 7 XCTAssertTrue(activeMap.verifiesRuntime(memory: memory, available: available)) XCTAssertFalse(result.verifiesRuntime(memory: memory, available: available), "Banked runtime checks require an explicit physical projection") let metadata = CMOCVariableDebug(functions: [.init(name: "first", sourcePath: "main.c", entry: 0x4000, body: 0x4000, end: 0x4004, frame: false, moduleID: "first")], variables: [ .init(name: "score", sourcePath: "main.c", line: 1, type: .integer, address: 0x5000, offset: nil, scope: nil, isStatic: false, moduleID: "first")], sourceMap: activeMap) func watch(_ data: Data) -> CMOCWatchEvaluator.Value { CMOCWatchEvaluator(metadata: metadata, context: .init(pc: 0x4000, u: 0x6000, memory: data, available: available)).evaluate("score") } XCTAssertEqual(watch(memory).value, "7") memory[0x4003] = 0x39 // Assembly-only tail, outside the sole C instruction. XCTAssertFalse(activeMap.verifiesRuntime(memory: memory, available: available)) XCTAssertNotNil(watch(memory).error, "A matching C opcode cannot conceal changed unmapped assembly") let stack = CMOCCallStack.capture(metadata: metadata, pc: 0x4000, u: 0x6000, s: 0x6000, memory: memory, available: available) XCTAssertTrue(stack.frames.isEmpty); XCTAssertTrue(stack.termination.contains("Executable bytes changed")) memory[0x4003] = 0x12; available[0x4003] = 0 XCTAssertFalse(activeMap.verifiesRuntime(memory: memory, available: available), "Unavailable assembly bytes cannot establish source identity") let extended = try layout(manifest(secondPage: 300)) var extendedPages = Array(repeating: 0, count: 8); extendedPages[2] = 300 XCTAssertTrue(extended.modules[1].isMapped(physicalPages: extendedPages)) XCTAssertEqual(extended.modules[1].physicalAddress(0x4000), UInt32(300 * 8192), "8 MiB RAM pages must retain identity above the 8-bit page range") XCTAssertThrowsError(try layout(manifest(secondPage: 1024)), "Pages outside the largest supported RAM capacity are rejected") let emptySections = self.object([ Section(name: "first", bytes: [0x39], locals: [("_first", 0)]), Section(name: "bss", bytes: [], bssLength: 0), Section(name: "data", bytes: []) ]) XCTAssertEqual(try CMOCSourceMap.resolvedObjectSymbolModules(object: emptySections, layout: verified), ["_first": "first"], "Empty default sections without debug symbols must not suppress all metadata") for missingBSS in [Section(name: "bss", bytes: [], bssLength: 2), Section(name: "bss", bytes: [], locals: [("_end", 0)], bssLength: 0)] { XCTAssertThrowsError(try CMOCSourceMap.resolvedObjectSymbolModules( object: self.object([missingBSS]), layout: verified), "Real storage and debug labels still require explicit module ownership") } let validManifest = try manifest() func changed(_ mutation: (inout [String: Any]) -> Void) throws -> Data { var json = try XCTUnwrap(JSONSerialization.jsonObject(with: validManifest) as? [String: Any]) mutation(&json) return try JSONSerialization.data(withJSONObject: json) } func bssManifest(address: Int, extraPage: Int? = nil) throws -> Data { let bssMap = Data((String(data: map, encoding: .utf8)! + String(format: "Section: bss (main.o) load at %04X, length 0001\n", address)).utf8) files["game.map"] = bssMap return try changed { value in value["linkMap"] = ["path": "game.map", "sha256": CMOCSourceMap.sha256(bssMap)] var modules = value["modules"] as! [[String: Any]] modules[0]["sections"] = ["first", "bss"] if let extraPage { modules[0]["pages"] = [["logicalPage": 2, "physicalPage": 4], ["logicalPage": 3, "physicalPage": extraPage]] } value["modules"] = modules } } XCTAssertNoThrow(try layout(bssManifest(address: 0x4004)), "An image's zero padding may cover its own BSS") XCTAssertThrowsError(try layout(bssManifest(address: 0x6000, extraPage: 5)), "BSS must not alias another module's physical image") files["game.map"] = map XCTAssertThrowsError(try layout(changed { $0["sources"] = ["main.c": String(repeating: "0", count: 64)] })) XCTAssertThrowsError(try layout(changed { $0["binary"] = ["path": "game.bin", "sha256": String(repeating: "0", count: 64)] })) XCTAssertThrowsError(try layout(changed { $0["artifacts"] = ["../outside.o": CMOCSourceMap.sha256(object)] })) XCTAssertThrowsError(try layout(changed { $0["disk"] = ["path": "unrelated.dsk", "sha256": CMOCSourceMap.sha256(packed)] })) XCTAssertThrowsError(try layout(changed { $0["version"] = 2 })) XCTAssertThrowsError(try layout(Data(repeating: 32, count: 1024 * 1024 + 1))) try FileManager.default.removeItem(at: directory.appendingPathComponent("second.raw")) try FileManager.default.createSymbolicLink(at: directory.appendingPathComponent("second.raw"), withDestinationURL: directory.appendingPathComponent("first.raw")) XCTAssertThrowsError(try CMOCBankedDebugLayout.parse(data: validManifest, directory: directory, sources: ["main.c": source], binary: packed, binaryPath: "game.bin", allowedOutputs: Array(files.keys))) try FileManager.default.removeItem(at: directory.appendingPathComponent("second.raw")) XCTAssertThrowsError(try layout(manifest(secondPage: 4)), "Logical overlap requires different physical banks") files["first.raw"] = Data([0x8e, 0x12, 0x35, 0x12, 0]) let stale = try layout(manifest()) // A new hash alone cannot bless a wrong relocation operand. XCTAssertThrowsError(try CMOCProjectSourceMap.parse(sources: ["main.c": source], compilationDirectory: directory, units: units, linkMap: map, binary: packed, bankedLayout: stale)) } func testPureBSSModuleNeedsNoInventedImageAndKeepsVerifiedGlobalPlacement() throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCPureBSS-" + UUID().uuidString) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: directory) } let source = Data("int pilot;\nvoid main(void) {}\n".utf8) let code = Section(name: "code", bytes: [0x12], locals: [("_main", 0), ("body", 0), ("end", 1)]) let bss = Section(name: "bss", bytes: [], locals: [("_pilot", 0)], bssLength: 2) var currentObject = object([code, bss]) let records: [[String: Any]] = [ ["kind": "function", "name": "main", "location": "main.c:2", "symbol": "_main", "begin": "body", "end": "end", "frame": false], ["kind": "variable", "name": "pilot", "location": "main.c:1", "symbol": "_pilot", "begin": "", "end": "", "static": false, "type": ["name": "int", "kind": "integer", "size": 2, "signed": true]] ] let listing = try Data(([ row("SECTION code"), row("* Line main.c:2"), row("NOP", offset: 0, bytes: [0x12]), row("ENDSECTION") ] + records.map { row("* VCCDBG1 " + String(decoding: try JSONSerialization.data(withJSONObject: $0), as: UTF8.self)) }).joined(separator: "\n").utf8) let map = Data("Section: code (main.o) load at 4000, length 0001\nSection: bss (main.o) load at 8000, length 0002\nSymbol: _main (main.o) = 4000\nSymbol: body (main.o) = 4000\nSymbol: end (main.o) = 4001\nSymbol: _pilot (main.o) = 8000\n".utf8) let packed = binary([(0x0e00, [0x12])], entry: 0x0e00) let image = Data([0x12]) let defaultModules: [[String: Any]] = [ ["id": "resident", "sections": ["code"], "codeSections": ["code"], "image": ["path": "code.raw", "logicalAddress": 0x4000], "pages": [["logicalPage": 2, "physicalPage": 4]]], ["id": "state", "sections": ["bss"], "codeSections": [], "pages": [["logicalPage": 4, "physicalPage": 10]]] ] func layout(modules: [[String: Any]]? = nil) throws -> CMOCBankedDebugLayout { let files = ["game.bin": packed, "game.map": map, "main.o": currentObject, "main.lst": listing, "code.raw": image] for (name, data) in files { try data.write(to: directory.appendingPathComponent(name)) } let manifest: [String: Any] = [ "version": 1, "entryPoint": 0x0e00, "binary": ["path": "game.bin", "sha256": CMOCSourceMap.sha256(packed)], "linkMap": ["path": "game.map", "sha256": CMOCSourceMap.sha256(map)], "sources": ["main.c": CMOCSourceMap.sha256(source)], "artifacts": files.filter { !["game.bin", "game.map"].contains($0.key) }.mapValues(CMOCSourceMap.sha256), "modules": modules ?? defaultModules ] return try CMOCBankedDebugLayout.parse(data: JSONSerialization.data(withJSONObject: manifest), directory: directory, sources: ["main.c": source], binary: packed, binaryPath: "game.bin", allowedOutputs: Array(files.keys)) } func sourceMap(_ layout: CMOCBankedDebugLayout) throws -> CMOCProjectSourceMap { try CMOCProjectSourceMap.parse(sources: ["main.c": source], compilationDirectory: directory, units: [.init(sourcePath: "main.c", sourceName: "main.c", objectName: "main.o", listing: listing, object: currentObject)], linkMap: map, binary: packed, bankedLayout: layout) } let verified = try layout() let state = try XCTUnwrap(verified.module(id: "state")) XCTAssertTrue(state.bytes.isEmpty) XCTAssertTrue(state.codeGuards.isEmpty) XCTAssertEqual(state.logicalAddress, 0x8000) XCTAssertEqual(state.physicalAddress(0x8001), UInt32(10 * 8192 + 1)) let mapped = try sourceMap(verified) let artifact = CMOCBuildArtifact(id: UUID(), directory: directory, source: String(decoding: source, as: UTF8.self), sourceData: source, sourceSHA256: CMOCSourceMap.sha256(source), binaryData: packed, buildLog: "", binaryRelativePath: "game.bin") let metadata = try CMOCVariableDebug.parse(artifact: artifact, verifiedMap: mapped) var memory = Data(repeating: 0, count: 65536) let available = Data(repeating: 1, count: 65536) memory[0x4000] = 0x12; memory[0x8000] = 0x12; memory[0x8001] = 0x34 let projected = metadata.projected(physicalPages: [2: 4, 4: 10], memory: memory, available: available) XCTAssertEqual(projected.variables.map(\.name), ["pilot"]) let evaluator = CMOCWatchEvaluator(metadata: projected, context: .init(pc: 0x4000, u: 0x7000, memory: memory, available: available)) XCTAssertEqual(evaluator.evaluate("pilot").value, "4660") XCTAssertTrue(metadata.projected(physicalPages: [2: 4, 4: 11], memory: memory, available: available).variables.isEmpty, "Uninitialized storage still requires its exact physical bank") var missingCodeImage = defaultModules missingCodeImage[0].removeValue(forKey: "image") XCTAssertThrowsError(try layout(modules: missingCodeImage)) { error in XCTAssertTrue(error.localizedDescription.contains("codeSections require a module image")) } var overflowingOffset = defaultModules overflowingOffset[0]["image"] = ["path": "code.raw", "logicalAddress": 0x4000, "offset": Int.min] XCTAssertThrowsError(try layout(modules: overflowingOffset), "Validate offsets before computing an inferred image length") var alias = defaultModules alias[1]["pages"] = [["logicalPage": 4, "physicalPage": 4]] XCTAssertThrowsError(try layout(modules: alias), "Pure BSS must not alias another module's physical code") var missingPage = defaultModules missingPage[1]["pages"] = [["logicalPage": 5, "physicalPage": 10]] XCTAssertThrowsError(try layout(modules: missingPage), "Omitting the real storage page cannot invent BSS identity") currentObject = object([code, Section(name: "bss", bytes: [0, 7], locals: [("_pilot", 0)])]) let undeclaredInitializedImage = try layout() XCTAssertThrowsError(try sourceMap(undeclaredInitializedImage), "An initialized object section cannot borrow the no-image BSS declaration") { error in XCTAssertTrue(error.localizedDescription.contains("bytes do not match")) } } private func sourceOnlySymbols(aliasCollision: Bool = false, duplicateName: Bool = false, forgedMapAddress: Bool = false) throws -> CMOCProjectSourceMap { let source = Data("/* Assembly-backed build without compiler type comments. */\n".utf8) let residentBytes: [UInt8] = [0x12, 0x12, 0x39], overlayBytes: [UInt8] = [0x86, 9, 0x39] var residentNames = [("_sample", 0), ("resident_after", 1), ("L0001", 1), (".vcc.debug.L0003", 1), ("L0002", 2), ("_code_end", 3), ("_missing_map", 0), ("funcend_sample", 1), ("funcsize_sample", 2), ("funcsize_main", 1), ("funcend_explicit_asm", 1), ("funcsize_explicit_asm", 2)] if aliasCollision { residentNames.append(("sample", 1)) } if duplicateName { residentNames.append(("_shared", 0)) } var overlayNames = [("_other", 0)] if duplicateName { overlayNames.append(("_shared", 0)) } let resident = object([ Section(name: "resident", bytes: residentBytes, locals: residentNames, exports: [("L0002", 2), ("funcend_explicit_asm", 1), ("funcsize_explicit_asm", 2)]), Section(name: "bss", bytes: [], locals: [("_counter", 0)], bssLength: 2), Section(name: "rodata", bytes: [5, 7], locals: [("_data_entry", 0)]) ]) let overlay = object([Section(name: "overlay", bytes: overlayBytes, locals: overlayNames)]) var mapRows = ["Section: resident (resident.o) load at 4000, length 0003", "Section: bss (resident.o) load at 6000, length 0002", "Section: rodata (resident.o) load at 6002, length 0002", "Section: overlay (overlay.o) load at 4000, length 0003"] for (name, offset) in residentNames where name != "_missing_map" { let address = forgedMapAddress && name == "_sample" ? 0x4001 : 0x4000 + offset mapRows.append(String(format: "Symbol: %@ (resident.o) = %04X", name, address)) } for (name, offset) in overlayNames { mapRows.append(String(format: "Symbol: %@ (overlay.o) = %04X", name, 0x4000 + offset)) } mapRows += ["Symbol: _counter (resident.o) = 6000", "Symbol: _data_entry (resident.o) = 6002", "Symbol: _forged (resident.o) = 4000"] let mapData = Data(mapRows.joined(separator: "\n").utf8) let packed = binary([(0x0e00, [0x12])], entry: 0x0e00) let layout = CMOCBankedDebugLayout(entryPoint: 0x0e00, binarySHA256: CMOCSourceMap.sha256(packed), linkMap: mapData, sourceHashes: ["main.c": CMOCSourceMap.sha256(source)], artifactHashes: ["resident.o": CMOCSourceMap.sha256(resident), "overlay.o": CMOCSourceMap.sha256(overlay)], modules: [ .init(id: "resident", sections: ["resident"], codeSections: ["resident"], logicalAddress: 0x4000, bytes: residentBytes, pages: [.init(logicalPage: 2, physicalPage: 4)], codeGuards: [.init(address: 0x4000, bytes: residentBytes)]), .init(id: "overlay", sections: ["overlay"], codeSections: ["overlay"], logicalAddress: 0x4000, bytes: overlayBytes, pages: [.init(logicalPage: 2, physicalPage: 5)], codeGuards: [.init(address: 0x4000, bytes: overlayBytes)]), .init(id: "state", sections: ["bss", "rodata"], codeSections: [], logicalAddress: 0x6002, bytes: [5, 7], pages: [.init(logicalPage: 3, physicalPage: 6)], codeGuards: []) ]) return try CMOCProjectSourceMap.parse(sources: ["main.c": source], compilationDirectory: FileManager.default.temporaryDirectory, units: [.init(sourcePath: "main.c", sourceName: "main.c", objectName: "resident.o", listing: Data(), object: resident), .init(sourcePath: "main.c", sourceName: "main.c", objectName: "overlay.o", listing: Data(), object: overlay)], linkMap: mapData, binary: packed, bankedLayout: layout) } func testSourceOnlyExecutableSymbolsDoNotRequireOrInventTypedMetadata() throws { let map = try sourceOnlySymbols() XCTAssertTrue(map.instructions.isEmpty, "Executable symbols do not create C source locations") let sample = try XCTUnwrap(map.executableSymbol(named: "sample")) XCTAssertEqual(sample.name, "_sample") XCTAssertEqual(sample.address, 0x4000); XCTAssertEqual(sample.moduleID, "resident") XCTAssertEqual(sample.objectPath, "resident.o"); XCTAssertEqual(sample.sectionName, "resident") XCTAssertEqual(sample.physicalAddress, UInt32(4 * 8192)) XCTAssertTrue(sample.sourcePaths.isEmpty, "An assembler object without C markers does not acquire a fabricated source declaration") XCTAssertEqual(map.executableSymbol(named: "_sample"), sample) XCTAssertTrue(map.executableSymbolNames.contains("sample")) XCTAssertTrue(map.executableSymbolNames.contains("L0002"), "Explicitly exported assembly labels remain selectable") XCTAssertNotNil(map.executableSymbol(named: "funcend_explicit_asm")) XCTAssertNotNil(map.executableSymbol(named: "funcsize_explicit_asm")) for unavailable in ["_counter", "counter", "_data_entry", "_code_end", "_forged", "_missing_map", "L0001", ".vcc.debug.L0003", "funcend_sample", "funcsize_sample", "funcsize_main"] { XCTAssertNil(map.executableSymbol(named: unavailable), unavailable) } let source = Data("/* Assembly-backed build without compiler type comments. */\n".utf8) let artifact = CMOCBuildArtifact(id: UUID(), directory: FileManager.default.temporaryDirectory, source: String(decoding: source, as: UTF8.self), sourceData: source, sourceSHA256: CMOCSourceMap.sha256(source), binaryData: binary([(0x0e00, [0x12])], entry: 0x0e00), buildLog: "") XCTAssertThrowsError(try CMOCVariableDebug.parse(artifact: artifact, verifiedMap: map), "Unavailable VCCDBG1 types must not erase the separately verified executable map") var memory = Data(repeating: 0, count: 65536), available = Data(repeating: 1, count: 65536) memory.replaceSubrange(0x4000..<0x4003, with: [0x12, 0x12, 0x39]) XCTAssertNil(map.executableLocation(at: 0x4000, memory: memory, available: available), "An unprojected PC is not a runtime claim") let active = map.projected(physicalPages: [2: 4, 3: 6], memory: memory, available: available) XCTAssertEqual(active.executableSymbol(named: "sample"), sample) XCTAssertNil(active.executableSymbol(named: "other")) let location = try XCTUnwrap(active.executableLocation(at: 0x4001, memory: memory, available: available)) XCTAssertEqual(location.moduleID, "resident"); XCTAssertEqual(location.physicalAddress, UInt32(4 * 8192 + 1)) XCTAssertEqual(location.codeRange, 0x4000..<0x4003) XCTAssertNil(active.instruction(at: 0x4001)); XCTAssertNil(active.location(at: 0x4001)) XCTAssertEqual(active.executableCodeRanges, [0x4000..<0x4003]) XCTAssertNil(active.executableLocation(at: 0x6000, memory: memory, available: available), "BSS is not executable") XCTAssertNil(active.executableLocation(at: 0xfe00, memory: memory, available: available)) let wrong = map.projected(physicalPages: [2: 7, 3: 6], memory: memory, available: available) XCTAssertTrue(wrong.executableSymbols.isEmpty) XCTAssertNil(wrong.executableLocation(at: 0x4001, memory: memory, available: available)) memory[0x4002] = 0x12 XCTAssertNil(active.executableLocation(at: 0x4000, memory: memory, available: available), "A changed assembly tail invalidates a previously selected module") XCTAssertTrue(map.projected(physicalPages: [2: 4, 3: 6], memory: memory, available: available).executableSymbols.isEmpty) memory[0x4002] = 0x39; available[0x4002] = 0 XCTAssertNil(active.executableLocation(at: 0x4000, memory: memory, available: available)) } func testPublicSymbolCatalogUsesEncodedJSONBudgetWithoutTruncatingPrivateSelection() throws { var map = try sourceOnlySymbols() let original = try XCTUnwrap(map.executableSymbol(named: "sample")) map.executableSymbols = (0..<200).map { index in .init(name: "entry_" + String(index) + String(repeating: "x", count: 1000), objectPath: original.objectPath, sectionName: original.sectionName, address: original.address, moduleID: original.moduleID, physicalAddress: original.physicalAddress) } let names = map.executableSymbolNames let publicNames = CMOCSourceMap.boundedJSONCatalog(names) XCTAssertEqual(publicNames.total, 200); XCTAssertTrue(publicNames.truncated) XCTAssertGreaterThan(publicNames.items.count, 0) XCTAssertLessThanOrEqual(try JSONSerialization.data(withJSONObject: publicNames.items).count, 32 * 1024) let last = try XCTUnwrap(names.last) XCTAssertFalse(publicNames.items.contains(last)) XCTAssertNotNil(map.executableSymbol(named: last), "Wire limits must never silently remove private verified selectors") // Quoting, backslashes and control characters must count as encoded // bytes, including when advertising dictionaries at a shared PC. let rows = (0..<100).map { ["name": String($0) + String(repeating: "\"\\\n", count: 30), "object_path": "obj/main.o"] } let bounded = CMOCSourceMap.boundedJSONCatalog(rows, maximumBytes: 2048) XCTAssertEqual(bounded.total, rows.count); XCTAssertTrue(bounded.truncated) XCTAssertLessThanOrEqual(try JSONSerialization.data(withJSONObject: bounded.items).count, 2048) XCTAssertEqual(bounded.items, Array(rows.prefix(bounded.items.count))) let tiny = CMOCSourceMap.boundedJSONCatalog(["oversized"], maximumBytes: 2) XCTAssertTrue(tiny.items.isEmpty); XCTAssertTrue(tiny.truncated) let complete = CMOCSourceMap.boundedJSONCatalog(["one", "two"], maximumBytes: 100) XCTAssertFalse(complete.truncated); XCTAssertEqual(complete.total, 2) } func testSourceOnlyMemoryAndStopRequireKnownSharedContributingSources() throws { var map = try sourceOnlySymbols() var memory = Data(repeating: 0, count: 65536) let available = Data(repeating: 1, count: 65536) memory.replaceSubrange(0x4000..<0x4003, with: [0x12, 0x12, 0x39]) let unknown = map.projected(physicalPages: [2: 4, 3: 6], memory: memory, available: available) XCTAssertTrue(unknown.executableCodeRanges(sharedPaths: ["main.c"]).isEmpty) XCTAssertNil(unknown.executableLocation(at: 0x4001, memory: memory, available: available, sharedPaths: ["main.c"]), "An unknown source association must not become shared executable memory") let sampleIndex = try XCTUnwrap(map.executableSymbols.firstIndex(where: { $0.name == "_sample" })) map.executableSymbols[sampleIndex].sourcePaths = ["main.c"] let active = map.projected(physicalPages: [2: 4, 3: 6], memory: memory, available: available) XCTAssertEqual(active.sharedExecutableModuleIDs(paths: ["main.c"]), ["resident"]) XCTAssertEqual(active.executableCodeRanges(sharedPaths: ["main.c"]), [0x4000..<0x4003]) XCTAssertNotNil(active.executableLocation(at: 0x4001, memory: memory, available: available, sharedPaths: ["main.c"])) XCTAssertTrue(active.executableCodeRanges(sharedPaths: ["unshared.c"]).isEmpty) XCTAssertNil(active.executableLocation(at: 0x4001, memory: memory, available: available, sharedPaths: ["unshared.c"])) memory.replaceSubrange(0x4000..<0x4003, with: [0x86, 9, 0x39]) let overlay = map.projected(physicalPages: [2: 5, 3: 6], memory: memory, available: available) XCTAssertTrue(overlay.executableCodeRanges(sharedPaths: ["main.c"]).isEmpty, "Resident source evidence does not grant an overlapping unknown overlay") XCTAssertNil(overlay.executableLocation(at: 0x4001, memory: memory, available: available, sharedPaths: ["main.c"])) } func testSourceOnlySymbolAliasesAmbiguityAndMapAddressMustBeHonest() throws { let aliases = try sourceOnlySymbols(aliasCollision: true) XCTAssertNil(aliases.executableSymbol(named: "sample"), "A raw name and another symbol's C alias cannot select an arbitrary address") XCTAssertFalse(aliases.executableSymbolNames.contains("sample")) XCTAssertNotNil(aliases.executableSymbol(named: "_sample")) let duplicate = try sourceOnlySymbols(duplicateName: true) XCTAssertNil(duplicate.executableSymbol(named: "shared")); XCTAssertNil(duplicate.executableSymbol(named: "_shared")) var memory = Data(repeating: 0, count: 65536) memory.replaceSubrange(0x4000..<0x4003, with: [0x12, 0x12, 0x39]) let active = duplicate.projected(physicalPages: [2: 4, 3: 6], memory: memory, available: Data(repeating: 1, count: 65536)) XCTAssertEqual(active.executableSymbol(named: "shared")?.moduleID, "resident") XCTAssertNil(try sourceOnlySymbols(forgedMapAddress: true).executableSymbol(named: "sample"), "An object symbol must agree with its own map record") let flat = CMOCProjectSourceMap(entryPoint: 0x4000, binarySHA256: "", sourceHashes: [:], instructions: [.init(sourcePath: "main.c", sourceLine: 1, address: 0x4000, bytes: [0x12], assembly: "NOP")], objectHashes: [:], listingHashes: [:], linkMapSHA256: "") XCTAssertTrue(flat.executableSymbolNames.isEmpty) XCTAssertNil(flat.executableLocation(at: 0x4000, memory: memory, available: Data(repeating: 1, count: 65536))) XCTAssertEqual(flat.instruction(at: 0x4000)?.sourceLine, 1, "Ordinary flat C stops retain their prior behavior") } func testRuntimeBankProjectionWaitsForCompleteModuleInstallation() { let first = CMOCBankedDebugLayout.Module(id: "first", sections: ["first"], codeSections: ["first"], logicalAddress: 0x4000, bytes: [0x12,0x12], pages: [.init(logicalPage: 2, physicalPage: 4)], codeGuards: [.init(address: 0x4000, bytes: [0x12,0x12])]) let pending = CMOCBankedDebugLayout.Module(id: "pending", sections: ["pending"], codeSections: ["pending"], logicalAddress: 0x6000, bytes: [0x39], pages: [.init(logicalPage: 3, physicalPage: 6)], codeGuards: [.init(address: 0x6000, bytes: [0x39])]) let data = CMOCBankedDebugLayout.Module(id: "data", sections: ["bss"], codeSections: [], logicalAddress: 0x5000, bytes: [0], pages: [.init(logicalPage: 2, physicalPage: 4)], codeGuards: []) let layout = CMOCBankedDebugLayout(entryPoint: 0x4000, binarySHA256: "", linkMap: Data(), sourceHashes: [:], artifactHashes: [:], modules: [first,pending,data]) let map = CMOCProjectSourceMap(entryPoint: 0x4000, binarySHA256: "", sourceHashes: [:], instructions: [ .init(sourcePath: "first.c", sourceLine: 1, address: 0x4000, bytes: [0x12], assembly: "NOP", moduleID: "first"), .init(sourcePath: "pending.c", sourceLine: 1, address: 0x6000, bytes: [0x39], assembly: "RTS", moduleID: "pending") ], objectHashes: [:], listingHashes: [:], linkMapSHA256: "", bankedLayout: layout) let metadata = CMOCVariableDebug(functions: [], variables: [ .init(name: "score", sourcePath: "first.c", line: 1, type: .integer, address: 0x5000, offset: nil, scope: nil, isStatic: false, moduleID: "data"), .init(name: "pendingLocal", sourcePath: "pending.c", line: 1, type: .integer, address: nil, offset: -2, scope: 0x6000..<0x6001, isStatic: false, moduleID: "pending", scopeModuleID: "pending") ], sourceMap: map) var memory = Data(repeating: 0, count: 65536) let available = Data(repeating: 1, count: 65536), pages = [2: 4, 3: 6] memory[0x4000] = 0x12; memory[0x4001] = 0x12 let active = map.projected(physicalPages: pages, memory: memory, available: available) XCTAssertEqual(active.activeModuleIDs, Set(["first","data"])) XCTAssertEqual(active.location(at: 0x4000)?.path, "first.c"); XCTAssertNil(active.location(at: 0x6000)) XCTAssertTrue(active.verifiesRuntime(memory: memory, available: available), "A mapped but not installed overlay must not block a valid current module") XCTAssertEqual(metadata.projected(physicalPages: pages, memory: memory, available: available).variables.map(\.name), ["score"], "Mapped data remains inspectable while not-yet-installed code contributes no locals") memory[0x6000] = 0x39 XCTAssertEqual(map.projected(physicalPages: pages, memory: memory, available: available).location(at: 0x6000)?.path, "pending.c") memory[0x4001] = 0x39 let changed = map.projected(physicalPages: pages, memory: memory, available: available) XCTAssertNil(changed.location(at: 0x4000), "Changing an unmapped assembly tail removes the current module's C claim") XCTAssertEqual(changed.location(at: 0x6000)?.path, "pending.c", "Other complete installed modules retain their identity") } func testStaleExecutableAndRelocationOperandsAreRejected() { for byteIndex in [11, 12, 15, 25] { // opcode, relocated call, relocated data, literal data var input = fixture() input.binary[byteIndex] ^= 1 XCTAssertThrowsError(try input.parse()) } } func testListingMustMatchOriginalObjectBeforeRelocation() { var input = fixture() input.listing = Data(String(decoding: input.listing, as: UTF8.self).replacingOccurrences(of: "170000", with: "170001").utf8) XCTAssertThrowsError(try input.parse()) } func testMissingAndAmbiguousObjectsAndSectionsAreRejected() { let replacements = [ ("(main.o)", "(missing.o)"), ("Section: bss (main.o)", "Section: bss (other/main.o)"), ("load at 4200, length 0009", "load at 4200, length 0008"), ("Section: bss (main.o)", "Section: code (main.o)") ] for (before, after) in replacements { var input = fixture() input.map = Data(String(decoding: input.map, as: UTF8.self).replacingOccurrences(of: before, with: after).utf8) XCTAssertThrowsError(try input.parse(), before) } } func testUnresolvedAndAmbiguousExternalSymbolsAreRejected() { var input = fixture() input.map = Data(String(decoding: input.map, as: UTF8.self).replacingOccurrences(of: "Symbol: _print", with: "Symbol: _other").utf8) XCTAssertThrowsError(try input.parse()) input = fixture() input.map.append(Data("\nSymbol: _print (other.o) = 2800\n".utf8)) XCTAssertThrowsError(try input.parse(), "Even duplicate symbols with equal values are ambiguous") } func testOnlyExactSourceIdentityAndValidLinesAreMapped() throws { for replacement in ["other/main.c:2", "main.c:999"] { var input = fixture() input.listing = Data(String(decoding: input.listing, as: UTF8.self).replacingOccurrences(of: "main.c:2", with: replacement).replacingOccurrences(of: "main.c:3", with: replacement).utf8) XCTAssertThrowsError(try input.parse()) } var input = fixture() input.listing = Data(String(decoding: input.listing, as: UTF8.self).replacingOccurrences(of: "main.c:", with: "./main.c:").utf8) XCTAssertEqual(try input.parse().addresses(forLine: 2), [0x4200, 0x4203]) } func testEquatesAndIncludedHeaderLinesDoNotBecomeInstructions() throws { var input = fixture() let addition = row("local EQU 0", bytes: [0, 0]) + "\n" + row("* Line cmoc.h:2: inline helper") + "\n" let text = String(decoding: input.listing, as: UTF8.self) .replacingOccurrences(of: row("LDX #message", offset: 3, bytes: [0x8e,0,0]), with: addition + row("LDX #message", offset: 3, bytes: [0x8e,0,0])) input.listing = Data(text.utf8) XCTAssertEqual(try input.parse().addresses(forLine: 2), [0x4200]) } func testAbsoluteSnapshotIdentityAndInlineAssemblyFailClosed() throws { var input = fixture() XCTAssertEqual(try input.parse(sourceName: "/build/main.c").executableLines, [2,3]) input.listing = Data(String(decoding: input.listing, as: UTF8.self).replacingOccurrences(of: "main.c:", with: "/build/main.c:").utf8) XCTAssertEqual(try input.parse(sourceName: "/build/main.c").executableLines, [2,3]) XCTAssertThrowsError(try input.parse(sourceName: "/other/main.c")) input = fixture() input.listing = Data(String(decoding: input.listing, as: UTF8.self).replacingOccurrences(of: "function call: print()", with: "inline assembly").utf8) XCTAssertEqual(try input.parse().executableLines, [3], "Inline assembler may contain arbitrary data and has no C breakpoint in this prototype") } func testProvenanceRejectsEditedSourceOrExecutable() throws { let input = fixture(), mapping = try input.parse() XCTAssertTrue(mapping.matches(source: input.source, binary: input.binary)) XCTAssertFalse(mapping.matches(source: input.source + Data("\n".utf8), binary: input.binary)) XCTAssertFalse(mapping.matches(source: input.source, binary: input.binary + Data([0]))) XCTAssertEqual(mapping.provenance.objectSHA256, CMOCSourceMap.sha256(input.object)) XCTAssertEqual(mapping.provenance.listingSHA256, CMOCSourceMap.sha256(input.listing)) XCTAssertEqual(mapping.provenance.linkMapSHA256, CMOCSourceMap.sha256(input.map)) } func testByteRelocationAndMalformedExpressions() throws { var input = fixture() input.object = object([Section(name: "code", bytes: [0x86, 0, 0x39], relocations: [(1, [255, 1, 1, 0, 65])])]) input.map = Data("Section: code (main.o) load at 2800, length 0003".utf8) input.binary = binary([(0x2800, [0x86, 65, 0x39])]) input.listing = Data([row("SECTION code"), row("* Line main.c:2: assignment"), row("LDA #65", offset: 0, bytes: [0x86, 0]), row("RTS", offset: 2, bytes: [0x39]), row("ENDSECTION")].joined(separator: "\n").utf8) XCTAssertEqual(try input.parse().instructions[0].bytes, [0x86, 65]) for expression: [UInt8] in [[4, 1], [1, 0, 1, 1, 0, 0, 4, 4], [3] + cString("missing"), [99]] { input.object = object([Section(name: "code", bytes: [0x86, 0, 0x39], relocations: [(1, expression)])]) XCTAssertThrowsError(try input.parse()) } } func testMalformedTruncatedAndOverlappingArtifactsAreRejected() { var input = fixture() input.object.removeLast(3) XCTAssertThrowsError(try input.parse()) input = fixture(); input.binary.removeLast() XCTAssertThrowsError(try input.parse()) input = fixture(); input.binary = binary([(0x2800, [0x39]), (0x2800, [0x39])]) XCTAssertThrowsError(try input.parse()) input = fixture(); input.listing = Data([0xff]) XCTAssertThrowsError(try input.parse()) } private func projectFixture() -> (sources: [String: Data], units: [CMOCProjectSourceMap.TranslationUnit], map: Data, binary: Data) { let source = Data("int work(void) {\n return 7;\n}\n".utf8) let header = Data("static int helper(void) {\n return 8;\n}\n".utf8) let paths = ["left/main.c", "right/main.c"] let instructions: [[UInt8]] = [[0x4f, 0x5f, 0x39], [0x4f, 0x5c, 0x39]] let units = paths.enumerated().map { index, path in let code = instructions[index] let listing = [row("SECTION code"), row("* Line /build/" + path + ":2: return"), row("CLRA", offset: 0, bytes: [code[0]]), row("CLRB", offset: 1, bytes: [code[1]]), row("* Line include/common.h:2: return"), row("RTS", offset: 2, bytes: [code[2]]), row("ENDSECTION")] return CMOCProjectSourceMap.TranslationUnit(sourcePath: path, sourceName: "/build/" + path, objectName: "obj/" + path.replacingOccurrences(of: ".c", with: ".o"), listing: Data(listing.joined(separator: "\n").utf8), object: object([Section(name: "code", bytes: code)])) } return ([paths[0]: source, paths[1]: source, "include/common.h": header], units, Data("Section: code (obj/left/main.o) load at 2800, length 0003\nSection: code (obj/right/main.o) load at 2900, length 0003".utf8), binary([(0x2800, instructions[0]), (0x2900, instructions[1])])) } func testProjectMapsDuplicateBasenamesAndHeaderInstructionsByExactIdentity() throws { let fixture = projectFixture() let map = try CMOCProjectSourceMap.parse(sources: fixture.sources, compilationDirectory: URL(fileURLWithPath: "/build"), units: fixture.units, linkMap: fixture.map, binary: fixture.binary) XCTAssertEqual(map.sourcePaths, ["include/common.h", "left/main.c", "right/main.c"]) XCTAssertEqual(map.addresses(file: "left/main.c", line: 2), [0x2800, 0x2801]) XCTAssertEqual(map.addresses(file: "right/main.c", line: 2), [0x2900, 0x2901]) XCTAssertEqual(map.addresses(file: "include/common.h", line: 2), [0x2802, 0x2902]) XCTAssertEqual(map.location(at: 0x2900), .init(path: "right/main.c", line: 2)) XCTAssertEqual(map.instruction(at: 0x2901)?.bytes, [0x5c]) XCTAssertTrue(map.matches(sources: fixture.sources, binary: fixture.binary)) var stale = fixture.sources; stale["include/common.h"]!.append(Data("\n".utf8)) XCTAssertFalse(map.matches(sources: stale, binary: fixture.binary), "Header edits detach source provenance too") XCTAssertEqual(map.map(forFile: "left/main.c")?.executableLines, [2]) } func testProjectRejectsWrongObjectIdentityDespiteMatchingBasename() { let fixture = projectFixture(), unit = fixture.units[0] let wrong = CMOCProjectSourceMap.TranslationUnit(sourcePath: unit.sourcePath, sourceName: unit.sourceName, objectName: "obj/wrong/main.o", listing: unit.listing, object: unit.object) XCTAssertThrowsError(try CMOCProjectSourceMap.parse(sources: fixture.sources, compilationDirectory: URL(fileURLWithPath: "/build"), units: [wrong], linkMap: fixture.map, binary: fixture.binary)) } func testProjectDoesNotGuessNestedSourcesFromBareBasenames() throws { let fixture = projectFixture() let units = fixture.units.map { unit in CMOCProjectSourceMap.TranslationUnit(sourcePath: unit.sourcePath, sourceName: unit.sourceName, objectName: unit.objectName, listing: Data(String(decoding: unit.listing, as: UTF8.self) .replacingOccurrences(of: "/build/" + unit.sourcePath, with: "main.c").utf8), object: unit.object) } let map = try CMOCProjectSourceMap.parse(sources: fixture.sources, compilationDirectory: URL(fileURLWithPath: "/build"), units: units, linkMap: fixture.map, binary: fixture.binary) XCTAssertEqual(map.sourcePaths, ["include/common.h"], "Ambiguous bare nested filenames never claim executable lines") } func testProjectRejectsStaleSecondObjectAndUnsafeSourcePaths() { let fixture = projectFixture(); var bytes = fixture.binary; bytes[15] ^= 1 XCTAssertThrowsError(try CMOCProjectSourceMap.parse(sources: fixture.sources, compilationDirectory: URL(fileURLWithPath: "/build"), units: fixture.units, linkMap: fixture.map, binary: bytes)) var sources = fixture.sources; sources["../elsewhere.c"] = Data("int x;".utf8) XCTAssertThrowsError(try CMOCProjectSourceMap.parse(sources: sources, compilationDirectory: URL(fileURLWithPath: "/build"), units: fixture.units, linkMap: fixture.map, binary: fixture.binary)) } func testArtifactDiscoveryHandlesGeneratedSourceAndDifferentObjectDirectories() throws { let fixture = projectFixture(), root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: root) } var paths: [String] = [] for unit in fixture.units { let objectURL = root.appendingPathComponent(unit.objectName) try FileManager.default.createDirectory(at: objectURL.deletingLastPathComponent(), withIntermediateDirectories: true) try unit.object.write(to: objectURL) let listingPath = String(unit.objectName.dropLast(2)) + ".lst" try unit.listing.write(to: root.appendingPathComponent(listingPath)); paths += [unit.objectName, listingPath] } try fixture.map.write(to: root.appendingPathComponent("program.map")) try fixture.binary.write(to: root.appendingPathComponent("program.bin")) let data = fixture.sources["left/main.c"]! let artifact = CMOCBuildArtifact(id: UUID(), directory: root, source: String(decoding: data, as: UTF8.self), sourceData: data, sourceSHA256: CMOCSourceMap.sha256(data), binaryData: fixture.binary, buildLog: "", sources: fixture.sources, compilationDirectory: URL(fileURLWithPath: "/build"), entrySourcePath: "left/main.c", binaryRelativePath: "program.bin", outputPaths: paths) let map = try CMOCProjectSourceMap.parse(artifact: artifact) XCTAssertEqual(map.instructions.count, 6) XCTAssertEqual(map.location(at: 0x2901)?.path, "right/main.c") XCTAssertEqual(map.flatCodeGuards.map(\.address), [0x2800, 0x2900]) let receipt = CMOCPublicDebugEvidence.fields(artifact: artifact, sharedPaths: ["left/main.c"]) XCTAssertEqual(receipt["status"] as? String, "source_only", "Ordinary custom builds expose verified map evidence without a bank manifest") XCTAssertEqual(receipt["banked"] as? Bool, false) XCTAssertEqual(receipt["owned_stop_evidence"] as? String, "verified_flat_code") let stops = try XCTUnwrap(receipt["c_line_stops"] as? [[String: Any]]) XCTAssertFalse(stops.isEmpty) XCTAssertTrue(stops.allSatisfy { $0["path"] as? String == "left/main.c" }) XCTAssertEqual(receipt["reason_code"] as? String, "typed_metadata_unavailable") var changed = fixture.binary; changed[5] ^= 1 try changed.write(to: root.appendingPathComponent("program.bin")) let mismatch = CMOCPublicDebugEvidence.fields(artifact: artifact, sharedPaths: Set(fixture.sources.keys)) XCTAssertEqual(mismatch["status"] as? String, "unavailable") XCTAssertTrue((mismatch["reason"] as? String ?? "").contains("retained executable bytes differ")) try fixture.binary.write(to: root.appendingPathComponent("program.bin")) try Data("Section: code (wrong.o) load at 2800, length 0003".utf8).write(to: root.appendingPathComponent("program.map")) XCTAssertEqual(CMOCPublicDebugEvidence.fields(artifact: artifact, sharedPaths: Set(fixture.sources.keys))["status"] as? String, "unavailable") } } /// Real-toolchain checks are optional only when licensed local ROMs/tool staging /// are absent. The sources and disks here are authored test inputs, never games. final class CMOCMultiFileExecutionTests: XCTestCase { @MainActor private func command(_ body: (@escaping @Sendable (VCCMachineSnapshot) -> Void) -> Void) async -> VCCMachineSnapshot { await withCheckedContinuation { continuation in body { continuation.resume(returning: $0) } } } @MainActor private func stop(_ controller: VCCMachineController, timeout: TimeInterval = 15) async -> VCCMachineSnapshot { let deadline = Date().addingTimeInterval(timeout) var snapshot = await command(controller.snapshot) while snapshot.state == .running && Date() < deadline { try? await Task.sleep(nanoseconds: 20_000_000) snapshot = await command(controller.snapshot) } XCTAssertNil(snapshot.errorMessage) XCTAssertEqual(snapshot.state, .paused, "Expected a verified source breakpoint before the deadline") return snapshot } @MainActor private func boot(_ controller: VCCMachineController) async throws { _ = await command(controller.resume) let deadline = Date().addingTimeInterval(10) while Date() < deadline { let text: String? = await withCheckedContinuation { continuation in controller.copyScreenText { text, _ in continuation.resume(returning: text) } } if text?.contains("OK") == true { // The ROM prints OK before finishing keyboard initialization. // Wait for guest frames, not a guessed host sleep, before typing. let visible = await command(controller.snapshot) while Date() < deadline { let ready = await command(controller.snapshot) if ready.frames >= visible.frames + 18 { return } try await Task.sleep(nanoseconds: 20_000_000) } } try await Task.sleep(nanoseconds: 40_000_000) } XCTFail("Disk BASIC did not reach its prompt") throw CMOCBuildError.message("Disk BASIC boot timed out") } @MainActor func testHelloDiskDOSAndLOADMRunTheSameProgramOnBothCPUs() async throws { let mac = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent() let resources = mac.appendingPathComponent("LocalResources") let runtime = resources.appendingPathComponent("ToolchainBundle") let romURL = resources.appendingPathComponent("ROMs/coco3.rom"), diskROMURL = resources.appendingPathComponent("ROMs/disk11.rom") try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: runtime.appendingPathComponent("bin/cmoc").path) && FileManager.default.fileExists(atPath: romURL.path) && FileManager.default.fileExists(atPath: diskROMURL.path), "Stage local tools and ROMs") let root = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCHelloDOS-" + UUID().uuidString) try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: root) } let template = mac.deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("examples/cmoc/HelloDisk") let project = try CMOCProject.createHelloDisk(at: root.appendingPathComponent("Hello Disk"), templateDirectory: template) let build = try await CMOCBuildService(toolchain: runtime, buildRoot: root.appendingPathComponent("Builds")).build(project: project, overlays: [:], action: .disk) let map = try CMOCProjectSourceMap.parse(artifact: build) let point = try XCTUnwrap(map.instructions.first { $0.sourcePath == "main.c" && $0.sourceLine == 13 }) let disk = try Data(contentsOf: XCTUnwrap(build.diskURL)) let rom = try Data(contentsOf: romURL), diskROM = try Data(contentsOf: diskROMURL) for cpu in [false, true] { for text in ["DOS", "LOADM\"HELLO\":EXEC"] { let controller = VCCMachineController() _ = await command { controller.setAudioEnabled(false, volume: 0, completion: $0) } _ = await command { controller.loadDiskROM(diskROM, name: "Disk BASIC", completion: $0) } _ = await command { controller.configureCartridges([2,0,0,0,2], roms: Array(repeating: Data(), count: 5), names: Array(repeating: "", count: 5), multiPak: true, physicalSlot: 4, completion: $0) } _ = await command { controller.mountDisk(disk, drive: 0, name: "HELLO.dsk", readOnly: true, completion: $0) } _ = await command { controller.loadROM(rom, use6309: cpu, completion: $0) } try await boot(controller) let before = await command(controller.pause) let armed = await command { controller.debuggerArmDevelopmentBreakpoints([NSNumber(value: point.address)], guardAddresses: map.instructions.map { NSNumber(value: $0.address) }, guardBytes: map.instructions.map { Data($0.bytes) }, completion: $0) } XCTAssertNil(armed.errorMessage); XCTAssertFalse(armed.debuggerDevelopmentAttached) _ = await command(controller.resume) let paste = try CocoPaste(text + "\n", mode: .text) _ = await command { controller.pasteChords(paste.chords.map { NSNumber(value: $0) }, completion: $0) } let stopped = await stop(controller) XCTAssertEqual(stopped.programCounter, point.address, "\(text), 6309=\(cpu)") XCTAssertTrue(stopped.debuggerDevelopmentAttached) XCTAssertEqual(stopped.generation, before.generation) XCTAssertEqual(stopped.debuggerProgramRevision, before.debuggerProgramRevision) XCTAssertGreaterThan(stopped.disks[0].readEvents, before.disks[0].readEvents) _ = await command(controller.resume) var screen = "" for _ in 0..<200 { screen = await withCheckedContinuation { continuation in controller.copyScreenText { value, _ in continuation.resume(returning: value ?? "") } } if screen.contains("GENERATED WORD COUNT: 5") { break } try await Task.sleep(nanoseconds: 20_000_000) } XCTAssertTrue(screen.contains("DISK RESULT: 0"), screen) XCTAssertTrue(screen.contains("GENERATED WORD COUNT: 5"), screen) let after = await command(controller.pause) XCTAssertFalse(after.disks[0].dirty) let exported: Data? = await withCheckedContinuation { continuation in controller.exportDisk(inDrive: 0) { data, _, _ in continuation.resume(returning: data) } } XCTAssertEqual(exported, disk, "Boot and reading must preserve every disk byte") _ = await command(controller.shutdown) } } } @MainActor func testMultiFileQuickloadAndDiskBootStopAtVerifiedSourceOnBothCPUs() async throws { let mac = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent() let resources = mac.appendingPathComponent("LocalResources"), toolchain = resources.appendingPathComponent("ToolchainBundle") let romURL = resources.appendingPathComponent("ROMs/coco3.rom"), diskROMURL = resources.appendingPathComponent("ROMs/disk11.rom") try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: toolchain.appendingPathComponent("bin/cmoc").path) && FileManager.default.fileExists(atPath: romURL.path) && FileManager.default.fileExists(atPath: diskROMURL.path), "The staged standalone tools and licensed local ROMs are required") let folder = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCMultiFileExecution-" + UUID().uuidString) try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: folder) } let projectRoot = folder.appendingPathComponent("Project") _ = try CMOCProject.create(at: projectRoot) try FileManager.default.createDirectory(at: projectRoot.appendingPathComponent("src"), withIntermediateDirectories: true) let main = """ #include #include "src/helper.h" int main(void) { unsigned answer = calculate(); printf("MULTI FILE RESULT %u\\n", answer); return 0; } """ let helper = """ #include "helper.h" unsigned testResult; unsigned calculate(void) { testResult = 40; testResult += 2; return testResult; } """ try Data(main.utf8).write(to: projectRoot.appendingPathComponent("main.c")) try Data(helper.utf8).write(to: projectRoot.appendingPathComponent("src/helper.c")) try Data("unsigned calculate(void);\n".utf8).write(to: projectRoot.appendingPathComponent("src/helper.h")) var settings = CMOCMakefileSettings() settings.sources = ["main.c", "src/helper.c"]; settings.diskImage = "program.dsk"; settings.diskBootMode = .program try Data(settings.generateMakefile().utf8).write(to: projectRoot.appendingPathComponent("Makefile")) var configuration = CMOCProjectConfiguration() configuration.diskTarget = "disk"; configuration.diskImage = "program.dsk"; configuration.jobs = 2 try JSONEncoder().encode(configuration).write(to: projectRoot.appendingPathComponent(CMOCProject.configurationFilename)) let project = try CMOCProject.open(at: projectRoot) let build = try await CMOCBuildService(toolchain: toolchain, buildRoot: folder.appendingPathComponent("Builds")) .build(project: project, overlays: [:], action: .disk) let mapping = try CMOCProjectSourceMap.parse(artifact: build) let first = try XCTUnwrap(mapping.instructions.first { $0.sourcePath == "src/helper.c" && $0.sourceLine == 6 }) let second = try XCTUnwrap(mapping.instructions.first { $0.sourcePath == "main.c" && $0.sourceLine == 6 }) let symbol = try XCTUnwrap(String(contentsOf: build.mapURL, encoding: .utf8).components(separatedBy: .newlines) .first { $0.hasPrefix("Symbol: _testResult (obj/src/helper.o) = ") }) let resultAddress = try XCTUnwrap(UInt16(try XCTUnwrap(symbol.split(separator: " ").last), radix: 16)) let disk = try Data(contentsOf: XCTUnwrap(build.diskURL)) XCTAssertEqual(disk.count, 35 * 18 * 256) let rom = try Data(contentsOf: romURL), diskROM = try Data(contentsOf: diskROMURL) for use6309 in [false, true] { for launchCommand in ["", "LOADM\"MAIN\":EXEC", "DOS"] { let diskLaunch = !launchCommand.isEmpty let controller = VCCMachineController() do { _ = await command { controller.setAudioEnabled(false, volume: 0, completion: $0) } let diskConfigured = await command { controller.loadDiskROM(diskROM, name: "Test disk ROM", completion: $0) } XCTAssertNil(diskConfigured.errorMessage) let layout = await command { controller.configureCartridges([2,0,0,0,2], roms: Array(repeating: Data(), count: 5), names: Array(repeating: "", count: 5), multiPak: true, physicalSlot: 4, completion: $0) } XCTAssertNil(layout.errorMessage) let mounted = await command { controller.mountDisk(disk, drive: 0, name: "program.dsk", readOnly: true, completion: $0) } XCTAssertNil(mounted.errorMessage) let loaded = await command { controller.loadROM(rom, use6309: use6309, completion: $0) } XCTAssertNil(loaded.errorMessage) try await boot(controller) let before = await command(controller.pause) let points = [NSNumber(value: first.address), NSNumber(value: second.address)] if diskLaunch { let armed = await command { controller.debuggerArmDevelopmentBreakpoints(points, guardAddresses: mapping.instructions.map { NSNumber(value: $0.address) }, guardBytes: mapping.instructions.map { Data($0.bytes) }, completion: $0) } XCTAssertNil(armed.errorMessage); XCTAssertFalse(armed.debuggerDevelopmentAttached) XCTAssertEqual(armed.programCounter, before.programCounter, "Arming disk debugging must not quickload or change PC") _ = await command(controller.resume) let paste = try CocoPaste(launchCommand + "\n", mode: .text) let typed = await command { controller.pasteChords(paste.chords.map { NSNumber(value: $0) }, completion: $0) } XCTAssertNil(typed.errorMessage) } else { let loaded = await command { controller.debuggerLoadBinary(build.binaryData, breakpoints: points, completion: $0) } XCTAssertNil(loaded.errorMessage) } let stopped = await stop(controller) if stopped.state != .paused { let screen: String? = await withCheckedContinuation { continuation in controller.copyScreenText { text, _ in continuation.resume(returning: text) } } print("CMOC FAILED LAUNCH cpu6309=\(use6309) disk=\(diskLaunch) remaining=\(stopped.pasteRemaining) screen=\(screen ?? "unavailable")") throw CMOCBuildError.message("Source breakpoint not reached") } XCTAssertEqual(stopped.programCounter, first.address) XCTAssertEqual(mapping.location(at: stopped.programCounter), .init(path: "src/helper.c", line: 6)) XCTAssertTrue(stopped.debuggerDevelopmentAttached) XCTAssertEqual(stopped.generation, before.generation) if diskLaunch { XCTAssertEqual(stopped.debuggerProgramRevision, before.debuggerProgramRevision, "Disk loading is guest execution, not host quickload") XCTAssertGreaterThan(stopped.disks[0].readEvents, before.disks[0].readEvents) } let detail: VCCDebuggerSnapshot = await withCheckedContinuation { continuation in controller.debuggerSnapshot(address: first.address, followPC: true, memoryAddress: resultAddress) { continuation.resume(returning: $0) } } XCTAssertEqual(detail.instructions.first?.bytes, Data(first.bytes)) XCTAssertEqual(detail.memory.prefix(2), Data([0,40]), "The helper executes until immediately before the selected C statement") _ = await command(controller.resume) let next = await stop(controller) XCTAssertEqual(next.programCounter, second.address) XCTAssertEqual(mapping.location(at: next.programCounter), .init(path: "main.c", line: 6)) let secondDetail: VCCDebuggerSnapshot = await withCheckedContinuation { continuation in controller.debuggerSnapshot(address: second.address, followPC: true, memoryAddress: resultAddress) { continuation.resume(returning: $0) } } XCTAssertEqual(secondDetail.memory.prefix(2), Data([0,42])) XCTAssertEqual(secondDetail.instructions.first?.bytes, Data(second.bytes)) XCTAssertFalse(next.disks[0].dirty) _ = await command(controller.shutdown) } catch { _ = await command(controller.shutdown) throw error } } } } } final class CMOCLiveBreakpointModelTests: XCTestCase { @MainActor private func waitUntil(_ condition: () -> Bool, timeout: TimeInterval = 10, action: () -> Void = {}) async throws { let deadline = Date().addingTimeInterval(timeout) while !condition(), Date() < deadline { action(); try await Task.sleep(nanoseconds: 20_000_000) } XCTAssertTrue(condition(), "Model operation did not finish before its deadline") } @MainActor private func detail(_ machine: MachineModel) async -> VCCDebuggerSnapshot? { await withCheckedContinuation { continuation in machine.readDebugger(address: 0, followPC: true, memoryAddress: 0) { continuation.resume(returning: $0) } } } @MainActor func testPausedLiveToggleKeepsInstructionUpdatesAndRejectsUnobservedMemoryEdits() async throws { let resources = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("LocalResources") let toolchain = resources.appendingPathComponent("ToolchainBundle"), romURL = resources.appendingPathComponent("ROMs/coco3.rom") try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: toolchain.appendingPathComponent("bin/cmoc").path) && FileManager.default.fileExists(atPath: romURL.path), "Staged tools and a licensed local CoCo ROM are required") let folder = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCLiveBreakpoints-" + UUID().uuidString) try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) let suite = "CMOCLiveBreakpoints." + UUID().uuidString let defaults = UserDefaults(suiteName: suite)! defer { defaults.removePersistentDomain(forName: suite); try? FileManager.default.removeItem(at: folder) } let machine = MachineModel(diskStore: .init(url: folder.appendingPathComponent("disks.json")), displayDefaults: defaults, inputStore: .init(url: folder.appendingPathComponent("input.json"))) let model = CMOCDevelopmentModel(machine: machine, toolchain: toolchain, defaults: defaults) do { try model.adopt(CMOCProject.create(at: folder.appendingPathComponent("Project"))) model.build(); try await waitUntil({ !model.building }, timeout: 30) XCTAssertNil(model.message); XCTAssertTrue(model.buildMatchesEditor, model.log) try await waitUntil({ !machine.busy }) machine.installROM(try Data(contentsOf: romURL), url: romURL) try await waitUntil({ !machine.busy }) try await Task.sleep(nanoseconds: 1_500_000_000) await withCheckedContinuation { continuation in machine.debuggerConfigureBreakpoints([0x4500], disabled: []) { continuation.resume() } } let lines = model.source.components(separatedBy: "\n") let startLine = try XCTUnwrap(lines.firstIndex { $0.contains("width(40)") }) + 1 let menuLine = try XCTUnwrap(lines.firstIndex { $0.contains("What planet do you live on?") }) + 1 model.breakpointLine = startLine; model.run() try await waitUntil({ model.currentLine == startLine && !model.commanding }, action: { model.refresh() }) XCTAssertTrue(model.loadedMatchesEditor) let before = try XCTUnwrap(model.pc) // Add a later point while paused at an earlier source statement. // Conditional guards have not hit again; stepping must still update. model.toggleBreakpoint(menuLine) try await waitUntil({ !model.commanding }) XCTAssertEqual(machine.state, .paused) model.stepInstruction() try await waitUntil({ model.pc != before && !model.commanding }, action: { model.refresh() }) XCTAssertTrue(model.loadedMatchesEditor) let stepped = try XCTUnwrap(model.pc) XCTAssertEqual(model.currentLine, model.projectMapping?.location(at: stepped)?.line) XCTAssertEqual(model.currentFile, model.projectMapping?.location(at: stepped)?.path) model.toggleBreakpoint(menuLine) // Remove the final explicit point. try await waitUntil({ !model.commanding }) model.stepInstruction() try await waitUntil({ model.pc != stepped && !model.commanding }, action: { model.refresh() }) XCTAssertTrue(model.loadedMatchesEditor, "Removing points must not discard an already verified code connection") let clearedDetail = await detail(machine) let cleared = try XCTUnwrap(clearedDetail) XCTAssertEqual(cleared.breakpoints, [0x4500], "Only the development feature's breakpoints are removed") // Modify RAM without letting model.refresh observe the changed epoch. // A live toggle must inspect a new snapshot before rearming, rather // than blessing the new revision using its old source connection. await withCheckedContinuation { continuation in machine.debuggerWriteMemory(address: 0x4000, data: Data([0x55])) { continuation.resume() } } model.toggleBreakpoint(menuLine) try await waitUntil({ !model.commanding }) XCTAssertFalse(model.loadedMatchesEditor) XCTAssertNil(model.currentLine); XCTAssertNil(model.currentFile) XCTAssertEqual(machine.state, .paused) let changedDetail = await detail(machine) let changed = try XCTUnwrap(changedDetail) XCTAssertEqual(changed.breakpoints, [0x4500]) await withCheckedContinuation { continuation in machine.shutdown { continuation.resume() } } } catch { model.cancelBuild() await withCheckedContinuation { continuation in machine.shutdown { continuation.resume() } } throw error } } }