#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Audit a staged runtime and compile through Make without Apple developer tools.""" import argparse import hashlib import json import os from pathlib import Path import shutil import subprocess import tempfile from runtime_inputs import validate_private_inputs TOOLS = ('libexec/cmoc', 'bin/cpp', 'bin/mcpp', 'bin/make', 'bin/lwasm', 'bin/lwlink', 'bin/lwar', 'bin/writecocofile') def check_assembly(runtime, folder): """Both native slices must resolve source identity and reject escaping inputs.""" folder.mkdir() assembler = str(runtime / 'bin/lwasm') (folder / 'part.inc').write_text(' lda #$8f\n fcb $86,$8f\n') source = folder / 'main.asm' source.write_text(' org $3f00\n include "part.inc"\n end $3f00\n') command = [assembler, '--project-inputs', '--project-map=lines.tsv', '--6809', '--decb', '-o', 'first.bin', 'main.asm'] subprocess.run(command, cwd=folder, check=True) if (folder / 'lines.tsv').read_text() != 'part.inc\t1\t\t3F00\t868F\n': raise SystemExit('LWASM physical source map is missing or includes data') if (folder / 'first.bin').read_bytes() != bytes.fromhex('00 0004 3f00 868f 868f ff 0000 3f00'): raise SystemExit('LWASM project image differs') outside = folder.with_suffix('.inc') outside.write_text(' nop\n') (folder / 'linked.inc').symlink_to(outside) for directive, path in [('include', str(outside)), ('include', 'linked.inc'), ('includebin', '../' + outside.name)]: source.write_text(f" org $3f00\n {directive} '{path}'\n end $3f00\n") failure = subprocess.run(command, cwd=folder, capture_output=True, text=True) if failure.returncode == 0 or 'leaves the project snapshot' not in failure.stderr: raise SystemExit('Unexpected LWASM result for escaping ' + directive + ': ' + failure.stderr) def check_disk_allocation(runtime, folder): """The explicit before-dir option must match the existing forward allocator.""" folder.mkdir() tool = str(runtime / 'bin/writecocofile') payload = folder / 'DATA.DAT' payload.write_bytes(bytes(range(256)) * (35 * 9)) # 35 granules: spills above track 17. images = [] for name, options, first in [('default', [], 0), ('before', ['--allocate-before-dir'], 0), ('after', ['--allocate-after-dir'], 34), ('last-option', ['--allocate-after-dir', '--allocate-before-dir'], 0)]: image = folder / (name + '.dsk') image.write_bytes(b'\xff' * (35 * 18 * 256)) subprocess.run([tool, *options, str(image), str(payload)], check=True, stdout=subprocess.DEVNULL) content = image.read_bytes() entry = 17 * 18 * 256 + 2 * 256 if content[entry + 13] != first: raise SystemExit('Incorrect disk allocation start: ' + name) restored = subprocess.check_output([tool, '--stdout', str(image), 'DATA.DAT']) if restored != payload.read_bytes(): raise SystemExit('Disk allocation readback mismatch: ' + name) images.append(content) if images[0] != images[1] or images[0] != images[3]: raise SystemExit('Before-directory option changed default allocation semantics') def check_guest_example(runtime, env, folder, project_name, payloads): """Build an editable example and compare its guest disk files with the build outputs.""" source = Path(__file__).resolve().parents[2] / "examples/cmoc" / project_name shutil.copytree(source, folder, ignore=shutil.ignore_patterns("obj", "generated", "*.bin", "*.BIN", "*.dsk", "*.DSK", "*.map", "*.link")) subprocess.run([runtime / "bin/make", "-B", "disk"], cwd=folder, env=env, check=True) for disk_name, output, guest_name in payloads: expected = (folder / output).read_bytes() extracted = subprocess.check_output([runtime / "bin/writecocofile", "--stdout", disk_name, guest_name], cwd=folder, env=env) if not expected or extracted != expected: raise SystemExit(f"{project_name}: {guest_name} differs from {output}") subprocess.run([runtime / "bin/make", "-q", "build"], cwd=folder, env=env, check=True) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('runtime', type=Path) args = parser.parse_args() runtime = args.runtime.resolve() record = json.loads((runtime / 'manifest.json').read_text()) validate_private_inputs(runtime, record) disk = record.get('decbfile') if disk: if disk.get('profile') != 'rom-backed-core' or disk.get('standaloneDriverIncluded') is not False: raise SystemExit('Unsupported disk library profile') for relative, key in [('share/cmoc/lib/libdecbfile.a', 'archiveSHA256'), ('share/cmoc/include/decbfile.h', 'headerSHA256')]: if hashlib.sha256((runtime / relative).read_bytes()).hexdigest() != disk[key]: raise SystemExit('Disk library manifest hash mismatch: ' + relative) helpers = {relative: record['architectures'] for relative in TOOLS} helpers.update(record.get('nativeHelpers', {})) for library in record.get('libraries', {}).values(): for relative, expected in library['filesSHA256'].items(): if hashlib.sha256((runtime / relative).read_bytes()).hexdigest() != expected: raise SystemExit('Library manifest hash mismatch: ' + relative) for relative, architectures in helpers.items(): binary = runtime / relative info = subprocess.check_output(['/usr/bin/lipo', '-archs', str(binary)], text=True).split() if not set(architectures).issubset(info): raise SystemExit(f'Missing architecture: {relative}') links = subprocess.check_output(['/usr/bin/otool', '-L', str(binary)], text=True) for line in links.splitlines(): if line.startswith('\t') and not line.strip().startswith(('/usr/lib/', '/System/Library/')): raise SystemExit(f'Non-system runtime dependency: {relative}: {line}') with tempfile.TemporaryDirectory(prefix='VCC runtime #$ quote" relocation ') as folder: moved = Path(folder) / 'VCC Plus.app/Contents/Resources/Toolchain' shutil.copytree(runtime, moved) project = Path(folder) / 'C project with spaces'; project.mkdir() (project / 'main.c').write_text('#include \n#define JOIN(a,b) a##b\n#define STRING(x) #x\nint main(void) { unsigned JOIN(co,unt)=7; printf(STRING(HELLO) " %u\\n", count); return 0; }\n') if disk: (project / 'disk.c').write_text('''#include DECBDrive drives[1]; DECBFile file; byte written[256], readback[256]; int main(void) { initCoCoSupport(); if (decb_init(drives, 1) || decb_registerDrive(0)) return 1; memset(written, 42, 256); if (decb_createSectorFile(&file, 0, "ROUND.DAT", DECB_TYPE_BASIC_DATA, DECB_FORMAT_BINARY)) return 2; if (decb_writeSector(&file, written, 0) || decb_setNumBytesUsedInLastSector(&file, 256) || decb_closeSectorFile(&file)) return 3; if (decb_openSectorFile(&file, 0, "ROUND.DAT") || decb_readSector(&file, readback, 0) || decb_closeSectorFile(&file)) return 4; if (decb_rename(0, "ROUND.DAT", "RESULT.DAT") || decb_kill(0, "RESULT.DAT") || decb_shutdown()) return 5; return memcmp(written, readback, 256) != 0; } ''') def compile_disk(toolchain, tool_env): if disk: subprocess.run([str(toolchain / 'bin/cmoc'), '--coco', '--intermediate', '-O0', '-o', 'disk.bin', 'disk.c', '-ldecbfile'], cwd=project, env=tool_env, check=True) image = (project / 'disk.bin').read_bytes() if len(image) < 10 or image[0] != 0 or image[-5:-2] != b'\xff\0\0': raise SystemExit('Invalid linked disk example binary') if record.get('standaloneDisk'): (project / 'standalone.c').write_text('#include \nint main(void) { unsigned long saved = dskcon_init(dskcon_nmiService); DCOPC=2; DCDRV=0; DCTRK=17; DCSEC=3; DCBPT=(void *)0x500; dskcon_processSector(); dskcon_irqService(); dskcon_shutdown(saved); return DCSTA; }\n') subprocess.run([str(toolchain / 'bin/cmoc'), '--coco', '-o', 'standalone.bin', 'standalone.c', '-ldskcon-standalone'], cwd=project, env=tool_env, check=True) (project / 'Makefile').write_text('all: main.bin\nmain.bin: main.c\n\tcmoc --coco --intermediate -O0 -o main.bin main.c\n') env = dict(os.environ, PATH=str(moved / 'bin') + ':/usr/bin:/bin', DEVELOPER_DIR='/VCC-validation-no-developer-tools', SDKROOT='/VCC-validation-no-sdk', LC_ALL='C') for key in ('PERL5LIB', 'PERLLIB', 'PERL5OPT'): env.pop(key, None) def check_perl(toolchain, tool_env): if not record.get('perl'): return result = subprocess.check_output([str(toolchain / 'bin/perl'), '-MGetopt::Long', '-MDigest::SHA', '-e', 'use strict; use warnings; print $^X, \"\\n\", Digest::SHA::sha256_hex(\"VCC\");'], cwd=project, env=tool_env, text=True) if str(toolchain) not in result or hashlib.sha256(b'VCC').hexdigest() not in result: raise SystemExit('Perl did not use the relocated bundled interpreter and modules') subprocess.run([str(toolchain / 'bin/install-coco-boot-loader'), '--help'], cwd=project, env=tool_env, check=True, stdout=subprocess.DEVNULL) def check_dependencies(toolchain, tool_env, name): dependency_project = Path(folder) / name dependency_project.mkdir() header = dependency_project / 'headers #$ spaces' / 'value.h' header.parent.mkdir() header.write_text('#define VALUE 42\n') (dependency_project / 'main.c').write_text('#include \n#include "headers #$ spaces/value.h"\nint main(void) { return VALUE; }\n') # Include before the .d exists: Make must generate/reparse it, not # merely tolerate a dependency file that has never been consumed. # --deps-only and --deps use separate CMOC marker readers. (dependency_project / 'child').mkdir() (dependency_project / 'child/Makefile').write_text('stamp:\n\tprintf child > stamp\n') (dependency_project / 'Makefile').write_text( 'all: main.bin child/stamp\n' 'child/stamp:\n\t$(MAKE) -C child\n' 'main.bin: main.o\n\tcmoc --coco -o $@ $<\n' 'main.o: main.c\n\tcmoc --coco --deps -c -o $@ $<\n' 'main.d: main.c\n\tcmoc --coco --deps-only main.c\n' 'include main.d\n') # Same recursion-safe override used by the app: Make's default MAKE # is its absolute executable path, unsafe unquoted inside recipes. command = [str(toolchain / 'bin/make'), 'MAKE=make', '--no-builtin-rules'] def make(*options): subprocess.run(command + list(options), cwd=dependency_project, env=tool_env, check=True) make() make('-q') first = (dependency_project / 'main.bin').read_bytes() # A changed header must invalidate both the generated dependency # file and compiled object; no-op rebuilds must remain no-ops. header.write_text('#define VALUE 43\n') make() make('-q') if first == (dependency_project / 'main.bin').read_bytes(): raise SystemExit('Changed dependency did not rebuild the executable') dependency_text = (dependency_project / 'main.d').read_text() if 'VCC\\ Plus.app' not in dependency_text and 'Toolchain' not in dependency_text: raise SystemExit('Dependency test did not exercise relocated SDK headers') if '\\#$$' not in dependency_text: raise SystemExit('Make dependency special characters were not escaped') check_disk_allocation(moved, Path(folder) / "Disk allocation") check_dependencies(moved, env, 'Dependency project') check_perl(moved, env) # A deliberately unusable developer directory catches accidental xcrun # or Apple compiler use even on the development machine. subprocess.run([str(moved / 'bin/make'), '--no-builtin-rules'], cwd=project, env=env, check=True) compile_disk(moved, env) for name in ('main.bin', 'main.s', 'main.lst', 'main.map', 'main.link'): if not (project / name).is_file(): raise SystemExit('Missing build product: ' + name) binary = (project / 'main.bin').read_bytes() if len(binary) < 10 or binary[0] != 0 or binary[-5:-2] != b'\xff\0\0': raise SystemExit('Invalid DECB binary') # Thin copies force the entire compiler/preprocessor/assembler/linker # chain onto each architecture, not just a --version smoke test. for architecture in record['architectures']: thin = Path(folder) / (architecture + ' Toolchain') shutil.copytree(runtime, thin) for relative, architectures in helpers.items(): if architecture not in architectures: continue binary = thin / relative temporary = binary.with_name(binary.name + '.thin') slices = subprocess.check_output(['/usr/bin/lipo', '-archs', str(binary)], text=True).split() if len(slices) > 1: subprocess.run(['/usr/bin/lipo', str(binary), '-thin', architecture, '-output', str(temporary)], check=True) temporary.replace(binary) binary.chmod(0o755) subprocess.run(['/usr/bin/codesign', '--force', '--sign', '-', '--timestamp=none', str(binary)], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) arch_env = dict(env, PATH=str(thin / 'bin') + ':/usr/bin:/bin') subprocess.run([str(thin / 'bin/make'), '--no-builtin-rules', '-B'], cwd=project, env=arch_env, check=True) if {"ssc", "orch90", "vcccompat"}.issubset(record.get("libraries", {})): check_guest_example(thin, arch_env, Path(folder) / (architecture + " audio SDK"), "TalkingPlanetLander", [("LANDER.dsk", "LANDER.bin", "LANDER.BIN")]) if record["libraries"]["vcccompat"].get("reentrantTokenizer"): check_guest_example(thin, arch_env, Path(folder) / (architecture + " streams SDK"), "PlanetSurvey", [("SURVEY.dsk", "SURVEY.bin", "SURVEY.BIN"), ("SURVEY.dsk", "generated/PLANETS.TXT", "PLANETS.TXT")]) if "sdc" in record.get("libraries", {}): check_guest_example(thin, arch_env, Path(folder) / (architecture + " SDC SDK"), "PlanetExpedition", [ ("EXPED/EXPLORE.DSK", "EXPLORE.BIN", "EXPLORE.BIN"), ("EXPED/EARTH.DSK", "generated/earth/LANDING.TXT", "LANDING.TXT"), ("EXPED/MARS.DSK", "generated/mars/LANDING.TXT", "LANDING.TXT")]) libraries = record.get("libraries", {}) if (record.get("decbfile") and {"bank", "gime", "input", "bcontrol", "hirestxt"} <= libraries.keys() and libraries.get("orch90", {}).get("backgroundPlayback") and libraries.get("ssc", {}).get("storedSpeechAndEffects") and libraries.get("vcccompat", {}).get("diskFileManagement")): check_guest_example(thin, arch_env, Path(folder) / (architecture + " mission SDK"), "MissionRecorder", [("RECORDER.dsk", "RECORDER.bin", "RECORDER.BIN")]) manual_examples = ( ("vcccompat", "diskWriteStreams", "fieldlog.c", ["decbfile"]), ("vcccompat", "integerConversions", "atlas.c", ["decbfile"]), ("vcccompat", "diskStreamClearError", "returntrip.c", ["decbfile"]), ("vcccompat", "printerFormattedOutput", "flightreport.c", []), ("vcccompat", "cassetteTextFiles", "flightrecorder.c", []), ("vcccompat", "diskFileManagement", "atlasbackup.c", ["decbfile"]), ("vcccompat", "fixedDecimal", "descent.c", []), ("orch90", "backgroundPlayback", "flyby.c", ["input", "bcontrol"]), ("ssc", "storedSpeechAndEffects", "announcer.c", ["input", "bcontrol"]), ) for library, feature, source, dependencies in manual_examples: if record.get("libraries", {}).get(library, {}).get(feature): example = Path(__file__).resolve().parents[2] / "examples/cmoc/manual" / source shutil.copyfile(example, project / source) link_flags = ["-l" + name for name in [library, *dependencies]] subprocess.run([thin / "bin/cmoc", "--coco", "-O2", "-Werror", "-o", example.stem + ".bin", source, *link_flags], cwd=project, env=dict(arch_env, VCC_CMOC_COMPAT="1" if library == "vcccompat" else "0"), check=True) if record.get("libraries", {}).get("vcccompat", {}).get("boundedHeap"): example = Path(__file__).resolve().parents[2] / "examples/cmoc/manual/crew.c" shutil.copyfile(example, project / "crew.c") subprocess.run([thin / "bin/cmoc", "--coco", "-O2", "-Werror", "-o", "crew.bin", "crew.c", "-lvcccompat", "-lwydealloc"], cwd=project, env=dict(arch_env, VCC_CMOC_COMPAT="1"), check=True) if "gime" in record.get("libraries", {}): example = Path(__file__).resolve().parents[2] / "examples/cmoc/manual/lander.c" shutil.copyfile(example, project / "lander.c") subprocess.run([thin / "bin/cmoc", "--coco", "-O2", "-Werror", "-o", "lander.bin", "lander.c", "-lgime", "-lbgraph", "-lbcontrol"], cwd=project, env=arch_env, check=True) if "input" in record.get("libraries", {}): example = Path(__file__).resolve().parents[2] / "examples/cmoc/manual/probe.c" shutil.copyfile(example, project / "probe.c") subprocess.run([thin / "bin/cmoc", "--coco", "-O2", "-Werror", "-o", "probe.bin", "probe.c", "-lgime", "-linput", "-lbcontrol"], cwd=project, env=arch_env, check=True) if "bank" in record.get("libraries", {}): example = Path(__file__).resolve().parents[2] / "examples/cmoc/manual/ridge.c" shutil.copyfile(example, project / "ridge.c") subprocess.run([thin / "bin/cmoc", "--coco", "-O2", "-Werror", "-o", "ridge.bin", "ridge.c", "-lbank", "-lgime", "-lhirestxt"], cwd=project, env=arch_env, check=True) compile_disk(thin, arch_env) check_perl(thin, arch_env) check_assembly(thin, Path(folder) / (architecture + " assembly")) check_disk_allocation(thin, Path(folder) / (architecture + " disk allocation")) check_dependencies(thin, arch_env, architecture + " dependency project") # Preprocessor errors must stop Make and retain source-line diagnostics. (project / 'main.c').write_text('#line 81 "mapped.c"\n#error VCC_EXPECTED_PREPROCESSOR_FAILURE\n') failure = subprocess.run([str(moved / 'bin/make'), '-B'], cwd=project, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) if failure.returncode == 0 or 'VCC_EXPECTED_PREPROCESSOR_FAILURE' not in failure.stdout or '81' not in failure.stdout: raise SystemExit('Preprocessor errors/line directives were not preserved: ' + failure.stdout) print('PASS: architectures, system-only libraries, relocated space-containing bundle, both complete architecture build chains, preprocessor errors, Make dependency regeneration with spaces/quotes/#/$, optional ROM-backed/standalone disk API linking, bundled Perl/core modules, disk allocation and BIN without developer tools') if __name__ == '__main__': main()