Sync source edition with VCC Plus 3.7.0 build 63
This commit is contained in:
1 parent
c652a5276d
commit
b2ecec4d87
86 files changed
+10519
-472
No files matched your search
@@ -5,6 +5,14 @@ import Foundation
|
||||
/// declaration to one retained build; object relocation replay and live physical
|
||||
/// RAM comparisons provide the independent byte checks before debugging.
|
||||
struct CMOCBankedDebugLayout: Sendable {
|
||||
struct OverlayIdentity: Codable, Equatable, Hashable, Sendable {
|
||||
let group: String
|
||||
let owner: String
|
||||
}
|
||||
struct OverlayGroup: Codable, Equatable, Sendable {
|
||||
let id: String
|
||||
let owners: [String]
|
||||
}
|
||||
struct File: Decodable, Sendable { let path: String; let sha256: String }
|
||||
struct Page: Decodable, Equatable, Sendable { let logicalPage: Int; let physicalPage: Int }
|
||||
struct CodeGuard: Equatable, Sendable { let address: UInt16; let bytes: [UInt8] }
|
||||
@@ -33,6 +41,7 @@ struct CMOCBankedDebugLayout: Sendable {
|
||||
let bytes: [UInt8]
|
||||
let pages: [Page]
|
||||
let codeGuards: [CodeGuard]
|
||||
var overlay: OverlayIdentity? = nil
|
||||
func physicalAddress(_ logical: Int) -> UInt32? {
|
||||
guard (0..<65536).contains(logical),
|
||||
let page = pages.first(where: { $0.logicalPage == logical / 8192 }) else { return nil }
|
||||
@@ -44,6 +53,7 @@ struct CMOCBankedDebugLayout: Sendable {
|
||||
}
|
||||
private struct ModuleRecord: Decodable {
|
||||
let id: String; let sections: [String]; let codeSections: [String]; let image: Image?; let pages: [Page]
|
||||
let overlay: OverlayIdentity?
|
||||
}
|
||||
private struct Manifest: Decodable {
|
||||
let version: Int; let entryPoint: Int
|
||||
@@ -51,6 +61,7 @@ struct CMOCBankedDebugLayout: Sendable {
|
||||
let sources: [String: String]; let artifacts: [String: String]
|
||||
let modules: [ModuleRecord]
|
||||
let framebuffers: [Framebuffer]?
|
||||
let overlayGroups: [OverlayGroup]?
|
||||
}
|
||||
let entryPoint: UInt16
|
||||
let binarySHA256: String
|
||||
@@ -59,15 +70,21 @@ struct CMOCBankedDebugLayout: Sendable {
|
||||
let artifactHashes: [String: String]
|
||||
let modules: [Module]
|
||||
var framebuffers: [Framebuffer] = []
|
||||
var overlayGroups: [OverlayGroup] = []
|
||||
func module(forSection name: String) -> Module? { modules.first { $0.sections.contains(name) } }
|
||||
func module(id: String) -> Module? { modules.first { $0.id == id } }
|
||||
func permitsPhysicalOverlap(between first: String, and second: String) -> Bool {
|
||||
guard first != second, let a = module(id: first)?.overlay, let b = module(id: second)?.overlay else { return false }
|
||||
return a.group == b.group && a.owner != b.owner
|
||||
}
|
||||
|
||||
static func parse(data: Data, directory: URL, sources: [String: Data], binary: Data,
|
||||
binaryPath: String, diskPath: String? = nil, allowedOutputs: [String]) throws -> Self {
|
||||
func fail(_ text: String) -> CMOCSourceMap.Failure { .invalid("banked manifest: " + text) }
|
||||
guard data.count <= 1024 * 1024 else { throw fail("manifest exceeds 1 MB.") }
|
||||
// Bound recursive decoding before handing untrusted JSON to Decodable.
|
||||
var pending: [(Any, Int)] = [(try JSONSerialization.jsonObject(with: data), 0)], nodes = 0
|
||||
let raw = try JSONSerialization.jsonObject(with: data)
|
||||
var pending: [(Any, Int)] = [(raw, 0)], nodes = 0
|
||||
while let (value, depth) = pending.popLast() {
|
||||
nodes += 1
|
||||
guard depth <= 16, nodes <= 30000 else { throw fail("manifest nesting or record limit exceeded.") }
|
||||
@@ -75,9 +92,31 @@ struct CMOCBankedDebugLayout: Sendable {
|
||||
if let values = value as? [Any] { pending += values.map { ($0, depth + 1) } }
|
||||
}
|
||||
let manifest = try JSONDecoder().decode(Manifest.self, from: data)
|
||||
guard manifest.version == 1, (0..<65536).contains(manifest.entryPoint),
|
||||
guard [1, 2].contains(manifest.version), (0..<65536).contains(manifest.entryPoint),
|
||||
!manifest.modules.isEmpty, manifest.modules.count <= 256,
|
||||
manifest.artifacts.count <= 8192, sources.count <= 4096 else { throw fail("unsupported version or record count.") }
|
||||
let groups = manifest.overlayGroups ?? []
|
||||
if manifest.version == 1 {
|
||||
let object = raw as? [String: Any]
|
||||
let records = object?["modules"] as? [[String: Any]] ?? []
|
||||
guard object?["overlayGroups"] == nil, records.allSatisfy({ $0["overlay"] == nil }) else {
|
||||
throw fail("overlay declarations require manifest version 2.")
|
||||
}
|
||||
} else if manifest.overlayGroups == nil {
|
||||
throw fail("version 2 requires an explicit overlayGroups array.")
|
||||
}
|
||||
guard groups.count <= 64, groups.reduce(0, { $0 + $1.owners.count }) <= 256 else {
|
||||
throw fail("overlay group or owner count exceeds limits.")
|
||||
}
|
||||
func validIdentity(_ value: String) -> Bool { !value.isEmpty && value.utf8.count <= 128 && !value.contains("\0") }
|
||||
var declaredOwners: [String: Set<String>] = [:]
|
||||
for group in groups {
|
||||
guard validIdentity(group.id), declaredOwners[group.id] == nil, group.owners.count >= 2,
|
||||
group.owners.allSatisfy(validIdentity), Set(group.owners).count == group.owners.count else {
|
||||
throw fail("overlay groups require unique bounded identities and at least two distinct owners.")
|
||||
}
|
||||
declaredOwners[group.id] = Set(group.owners)
|
||||
}
|
||||
func path(_ value: String) throws {
|
||||
guard !value.isEmpty, value.utf8.count <= 4096, !value.hasPrefix("/"), !value.contains("\\"),
|
||||
!value.contains("\0"), !value.split(separator: "/", omittingEmptySubsequences: false).contains(where: { $0.isEmpty || $0 == "." || $0 == ".." }),
|
||||
@@ -96,14 +135,23 @@ struct CMOCBankedDebugLayout: Sendable {
|
||||
guard CMOCSourceMap.sha256(data) == digest else { throw fail("conflicting artifact hashes.") }; return data
|
||||
}
|
||||
let url = root.appendingPathComponent(name)
|
||||
guard url.resolvingSymlinksInPath().path == url.path else { throw fail("artifact paths must not traverse symbolic links.") }
|
||||
guard url.resolvingSymlinksInPath().path == url.path else { throw fail("artifact \(name) must not traverse symbolic links.") }
|
||||
let info = try url.resourceValues(forKeys: [.isRegularFileKey, .isSymbolicLinkKey, .fileSizeKey])
|
||||
guard info.isRegularFile == true, info.isSymbolicLink != true, (info.fileSize ?? Int.max) <= 8 * 1024 * 1024 else { throw fail("artifact is not a bounded regular file.") }
|
||||
guard info.isRegularFile == true, info.isSymbolicLink != true, let size = info.fileSize else {
|
||||
throw fail("artifact \(name) is not a regular file with a known size.")
|
||||
}
|
||||
let limit = CMOCSourceMap.evidenceByteLimit(for: name)
|
||||
guard size <= limit else { throw fail("artifact \(name) is \(size) bytes; its verification limit is \(limit) bytes.") }
|
||||
guard size <= CMOCSourceMap.maximumVerificationBytes - total else {
|
||||
throw fail("artifact \(name) would bring retained evidence to \(total + size) bytes; the aggregate limit is \(CMOCSourceMap.maximumVerificationBytes) bytes.")
|
||||
}
|
||||
let handle = try FileHandle(forReadingFrom: url); defer { try? handle.close() }
|
||||
let value = try handle.read(upToCount: 8 * 1024 * 1024 + 1) ?? Data()
|
||||
let value = try handle.read(upToCount: limit + 1) ?? Data()
|
||||
guard value.count == size, value.count <= limit else {
|
||||
throw fail("artifact \(name) changed size while reading (observed \(value.count) bytes; expected \(size), limit \(limit)).")
|
||||
}
|
||||
guard CMOCSourceMap.sha256(value) == digest else { throw fail("artifact \(name) SHA-256 differs from the retained build manifest.") }
|
||||
total += value.count
|
||||
guard value.count <= 8 * 1024 * 1024, total <= 128 * 1024 * 1024,
|
||||
CMOCSourceMap.sha256(value) == digest else { throw fail("artifact changed or exceeds verification limits.") }
|
||||
loaded[name] = value; return value
|
||||
}
|
||||
guard manifest.sources == sources.mapValues(CMOCSourceMap.sha256) else { throw fail("source snapshot differs from the manifest.") }
|
||||
@@ -130,13 +178,27 @@ struct CMOCBankedDebugLayout: Sendable {
|
||||
}
|
||||
for (name, digest) in manifest.artifacts { _ = try read(name, digest: digest) }
|
||||
var modules: [Module] = [], ids = Set<String>(), sections = Set<String>()
|
||||
var physical: [UInt32: String] = [:]
|
||||
var physical: [UInt32: Set<String>] = [:]
|
||||
var overlayByModule: [String: OverlayIdentity] = [:]
|
||||
func mayShare(_ address: UInt32, module: String, overlay: OverlayIdentity?) -> Bool {
|
||||
(physical[address] ?? []).allSatisfy { previous in
|
||||
if previous == module { return true }
|
||||
guard let overlay, let other = overlayByModule[previous] else { return false }
|
||||
return overlay.group == other.group && overlay.owner != other.owner
|
||||
}
|
||||
}
|
||||
for record in manifest.modules {
|
||||
guard !record.id.isEmpty, record.id.count <= 128, ids.insert(record.id).inserted,
|
||||
!record.sections.isEmpty, record.sections.count <= 256,
|
||||
Set(record.codeSections).count == record.codeSections.count,
|
||||
Set(record.codeSections).isSubset(of: Set(record.sections)),
|
||||
!record.pages.isEmpty, record.pages.count <= 8 else { throw fail("invalid or repeated module identity.") }
|
||||
if let overlay = record.overlay {
|
||||
guard declaredOwners[overlay.group]?.contains(overlay.owner) == true else {
|
||||
throw fail("module \(record.id) names an undeclared overlay group or owner.")
|
||||
}
|
||||
overlayByModule[record.id] = overlay
|
||||
}
|
||||
for section in record.sections {
|
||||
guard !section.isEmpty, section.count <= 256, !section.contains(where: \.isWhitespace), sections.insert(section).inserted else { throw fail("section has ambiguous module ownership.") }
|
||||
}
|
||||
@@ -181,14 +243,14 @@ struct CMOCBankedDebugLayout: Sendable {
|
||||
}
|
||||
let module = Module(id: record.id, sections: record.sections, codeSections: record.codeSections,
|
||||
logicalAddress: logicalAddress, bytes: moduleBytes, pages: record.pages,
|
||||
codeGuards: guards.sorted { $0.address < $1.address })
|
||||
codeGuards: guards.sorted { $0.address < $1.address }, overlay: record.overlay)
|
||||
var imagePhysical = Set<UInt32>()
|
||||
for logical in module.logicalAddress..<(module.logicalAddress + moduleBytes.count) {
|
||||
guard let address = module.physicalAddress(logical), imagePhysical.insert(address).inserted,
|
||||
physical[address] == nil || physical[address] == module.id else {
|
||||
mayShare(address, module: module.id, overlay: module.overlay) else {
|
||||
throw fail("module images overlap physical RAM or lack a page identity.")
|
||||
}
|
||||
physical[address] = module.id
|
||||
physical[address, default: []].insert(module.id)
|
||||
}
|
||||
// BSS has no image bytes, but its storage must still have one
|
||||
// physical owner. Initialized non-code data follows the same rule.
|
||||
@@ -198,15 +260,23 @@ struct CMOCBankedDebugLayout: Sendable {
|
||||
for placement in matching where placement.length > 0 {
|
||||
for logical in placement.address..<(placement.address + placement.length) {
|
||||
guard let address = module.physicalAddress(logical),
|
||||
physical[address] == nil || physical[address] == module.id else {
|
||||
mayShare(address, module: module.id, overlay: module.overlay) else {
|
||||
throw fail("module storage overlaps another module's physical RAM or lacks a page identity.")
|
||||
}
|
||||
physical[address] = module.id
|
||||
physical[address, default: []].insert(module.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
modules.append(module)
|
||||
}
|
||||
for group in groups {
|
||||
for owner in group.owners {
|
||||
let members = modules.filter { $0.overlay == OverlayIdentity(group: group.id, owner: owner) }
|
||||
guard !members.isEmpty, members.contains(where: { !$0.codeGuards.isEmpty }) else {
|
||||
throw fail("overlay owner \(group.id)/\(owner) needs at least one code-bearing module.")
|
||||
}
|
||||
}
|
||||
}
|
||||
// A compressed transport can enter through its loader, outside every
|
||||
// eventual source module. Bind this entry to the delivered DECB trailer.
|
||||
guard try CMOCSourceMap.decbEntryPoint(binary) == UInt16(manifest.entryPoint) else { throw fail("entry point differs from the delivered executable.") }
|
||||
@@ -235,6 +305,6 @@ struct CMOCBankedDebugLayout: Sendable {
|
||||
}
|
||||
return Self(entryPoint: UInt16(manifest.entryPoint), binarySHA256: manifest.binary.sha256,
|
||||
linkMap: linkMap, sourceHashes: manifest.sources, artifactHashes: manifest.artifacts,
|
||||
modules: modules, framebuffers: buffers)
|
||||
modules: modules, framebuffers: buffers, overlayGroups: groups)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user