|
|
|
@@ -14,8 +14,9 @@ return await NativeDiagnostic.Execute(args);
|
|
|
|
|
static class NativeDiagnostic
|
|
|
|
|
{
|
|
|
|
|
const string DockurCommit = "16a5b470cdd601bae8b05b02d748d7edfb36c12e";
|
|
|
|
|
const string CryptexUrl = "https://github.com/acidanthera/CryptexFixup/releases/download/1.0.5/CryptexFixup-1.0.5-RELEASE.zip";
|
|
|
|
|
const string CryptexHash = "25041d94a0fe9a0261caf0ba89b36dfcb21682bf3c697a34bcaddc839576ab30";
|
|
|
|
|
const string Profile = "tcg-haswell-sonoma";
|
|
|
|
|
const string CpuModel = "Haswell-noTSX";
|
|
|
|
|
const string CpuFlags = "Haswell-noTSX,l3-cache=on,+hypervisor,vendor=GenuineIntel,vmx=off,vmware-cpuid-freq=on,-pdpe1gb,-pcid,-invpcid,-tsc-deadline,-xsavec,-xsaves,+ssse3,+sse4.2,+popcnt,+avx,+avx2,+aes,+fma,+bmi1,+bmi2,+smep,+xsave,+xsaveopt,+xgetbv1,+movbe,+rdrand,enforce=on";
|
|
|
|
|
const string OpenCoreTemplateHash = "287328995d4198f1b05166f087d85bf7ef66bedafe150d17ad112ac8de60051d";
|
|
|
|
|
const string UdifChecksumBindingHash = "6109d04619e800c483fdac363d593cd1cd69f34131d2521417334e11d41c8bfa";
|
|
|
|
|
const string OwnerLabel = "org.meeting-assistant.native-diagnostic";
|
|
|
|
@@ -57,7 +58,7 @@ static class NativeDiagnostic
|
|
|
|
|
{
|
|
|
|
|
if (args.Length == 0 || args.Contains("--help"))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("dotnet run --file tools/ci/MacOsNativeDiagnostic.cs -- --run|--cleanup|--validate [--output artifacts/native-macos] [--source existing-dockur-clone] [--cryptex-archive verified-release.zip]");
|
|
|
|
|
Console.WriteLine("dotnet run --file tools/ci/MacOsNativeDiagnostic.cs -- --run|--cleanup|--validate [--output artifacts/native-macos] [--source existing-dockur-clone]");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
var output = Path.GetFullPath(Option(args, "--output") ?? "artifacts/native-macos");
|
|
|
|
@@ -66,10 +67,23 @@ static class NativeDiagnostic
|
|
|
|
|
ValidateContracts();
|
|
|
|
|
if (Option(args, "--source") is { } source)
|
|
|
|
|
{
|
|
|
|
|
await PrepareSource(Path.GetFullPath(source), output, "validation", false, Option(args, "--cryptex-archive"), CancellationToken.None);
|
|
|
|
|
await PrepareSource(Path.GetFullPath(source), output, "validation", false, CancellationToken.None);
|
|
|
|
|
await ValidateResourceRetention(output);
|
|
|
|
|
await ValidateRecoveryPatch(output);
|
|
|
|
|
Save(Path.Combine(output, "validation.json"), new { success = true, profile = "kvm-host-ventura-cryptex", helperSha256 = Hash(File.ReadAllBytes(Path.Combine("tools", "ci", "MacOsNativeDiagnostic.cs"))), udifChecksumBindingSha256 = Hash(File.ReadAllBytes(Path.Combine("tools", "ci", "macos-native-udif-checksums.py"))), baselineReadinessNormalized = true, resultNegativeCases = 6, containerNegativeCases = 11, recoveryPositiveCases = 4, recoveryNegativeCases = 12, independentFixtureCrc32Readback = true, resourceSnapshotRetention = true, cryptexArchiveVerified = true, configurationAndStagingContractsVerified = true, templateIsoDownloaded = false, activeLiluRuntimeChecked = false, sourceModified = false, dockerExecuted = false, guestExecuted = false, completedUtc = DateTimeOffset.UtcNow });
|
|
|
|
|
await ValidateTcgPreflight(output, CancellationToken.None);
|
|
|
|
|
Save(Path.Combine(output, "validation.json"), new
|
|
|
|
|
{
|
|
|
|
|
success = true, profile = Profile,
|
|
|
|
|
helperSha256 = Hash(File.ReadAllBytes(Path.Combine("tools", "ci", "MacOsNativeDiagnostic.cs"))),
|
|
|
|
|
udifChecksumBindingSha256 = Hash(File.ReadAllBytes(Path.Combine("tools", "ci", "macos-native-udif-checksums.py"))),
|
|
|
|
|
baselineReadinessNormalized = true, readinessDiagnosticBlocksExcluded = 7,
|
|
|
|
|
productVersionParserBlockExcluded = true, productVersionSequenceRestored = true, productVersionMaximumBytes = 1024, nativeProductVersionCommandRemoved = true,
|
|
|
|
|
diskReadinessAttemptLimit = 1, diskCommandLimitSeconds = 120, diskThreadObservationLimitSeconds = 60, stackSamplingUsed = false,
|
|
|
|
|
resultNegativeCases = 6, containerNegativeCases = 11, recoveryPositiveCases = 4, recoveryNegativeCases = 12,
|
|
|
|
|
independentFixtureCrc32Readback = true, resourceSnapshotRetention = true, cpuProfileSourceContractsVerified = true,
|
|
|
|
|
preflightGateFixtureCases = 8, qemuRuntimePreflightExecuted = false, templateIsoDownloaded = false,
|
|
|
|
|
sourceModified = false, dockerExecuted = false, guestExecuted = false, completedUtc = DateTimeOffset.UtcNow
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("Source patch contracts and diagnostic result validation passed; no Docker or guest execution occurred.");
|
|
|
|
|
return 0;
|
|
|
|
@@ -86,7 +100,7 @@ static class NativeDiagnostic
|
|
|
|
|
Save(statePath, state);
|
|
|
|
|
Directory.CreateDirectory(work);
|
|
|
|
|
File.WriteAllText(Path.Combine(work, "run.owner"), token);
|
|
|
|
|
using var deadline = new CancellationTokenSource(TimeSpan.FromMinutes(40));
|
|
|
|
|
using var deadline = new CancellationTokenSource(TimeSpan.FromMinutes(90));
|
|
|
|
|
using var signal = OperatingSystem.IsLinux() ? PosixSignalRegistration.Create(PosixSignal.SIGTERM, context => { context.Cancel = true; deadline.Cancel(); }) : null;
|
|
|
|
|
ConsoleCancelEventHandler cancelHandler = (_, context) => { context.Cancel = true; deadline.Cancel(); };
|
|
|
|
|
Console.CancelKeyPress += cancelHandler;
|
|
|
|
@@ -98,7 +112,7 @@ static class NativeDiagnostic
|
|
|
|
|
throw new InvalidOperationException("This diagnostic runs on the existing Linux/x64 runner only.");
|
|
|
|
|
ValidateContracts();
|
|
|
|
|
var sourceCommit = (await Command("git", ["rev-parse", "HEAD"], output, "candidate-commit", deadline.Token)).Output.Trim();
|
|
|
|
|
Save(Path.Combine(output, "run-metadata.json"), new { token, startedUtc = DateTimeOffset.UtcNow, sourceCommit, dockurCommit = DockurCommit, profile = "kvm-host-ventura-cryptex", causalSingleVariableTest = false, kvm = true, cpuModel = "host", recoveryMajor = 13, cryptexVersion = "1.0.5", liluVersion = "1.7.1", runId = Environment.GetEnvironmentVariable("GITHUB_RUN_ID"), server = Environment.GetEnvironmentVariable("GITHUB_SERVER_URL"), architecture = RuntimeInformation.ProcessArchitecture.ToString(), deadlineMinutes = 40 });
|
|
|
|
|
Save(Path.Combine(output, "run-metadata.json"), new { token, startedUtc = DateTimeOffset.UtcNow, sourceCommit, dockurCommit = DockurCommit, profile = Profile, causalSingleVariableTest = false, kvm = false, cpuModel = CpuModel, recoveryMajor = 14, cpuFlags = CpuFlags, runId = Environment.GetEnvironmentVariable("GITHUB_RUN_ID"), server = Environment.GetEnvironmentVariable("GITHUB_SERVER_URL"), architecture = RuntimeInformation.ProcessArchitecture.ToString(), deadlineMinutes = 90 });
|
|
|
|
|
var info = await Command("docker", ["info", "--format", "{{json .}}"], output, "docker-info", deadline.Token);
|
|
|
|
|
using (var document = JsonDocument.Parse(info.Output))
|
|
|
|
|
{
|
|
|
|
@@ -117,13 +131,13 @@ static class NativeDiagnostic
|
|
|
|
|
await Command("git", ["-C", source, "checkout", "--detach", DockurCommit], output, "dockur-checkout", deadline.Token);
|
|
|
|
|
var actualCommit = (await Command("git", ["-C", source, "rev-parse", "HEAD"], output, "dockur-commit", deadline.Token)).Output.Trim();
|
|
|
|
|
if (actualCommit != DockurCommit) throw new InvalidOperationException("Dockur source pin mismatch.");
|
|
|
|
|
await PrepareSource(source, output, token, true, Option(args, "--cryptex-archive"), deadline.Token);
|
|
|
|
|
await PrepareSource(source, output, token, true, deadline.Token);
|
|
|
|
|
await Command("docker", ["build", "--platform", "linux/amd64", "--label", OwnerLabel + "=" + token, "--tag", state.ImageTag, source], output, "docker-build", deadline.Token, echo: true);
|
|
|
|
|
var imageInspect = await Command("docker", ["image", "inspect", state.ImageTag], output, "image-inspect", deadline.Token);
|
|
|
|
|
using (var image = JsonDocument.Parse(imageInspect.Output))
|
|
|
|
|
state = state with { ImageId = image.RootElement[0].GetProperty("Id").GetString() };
|
|
|
|
|
Save(statePath, state);
|
|
|
|
|
var create = await Command("docker", ["create", "--name", state.ContainerName, "--label", OwnerLabel + "=" + token, "--memory", "6g", "--memory-swap", "6g", "--cpus", "2", "--shm-size", "512m", "--log-opt", "max-size=8m", "--log-opt", "max-file=1", "--device", "/dev/kvm:/dev/kvm:rw", "--env", "KVM=Y", "--env", "CPU_MODEL=host", "--env", "NETWORK=slirp", "--env", "DISPLAY=web", "--env", "MANUAL=N", "--env", "VERSION=13", "--env", "RAM_SIZE=4G", "--env", "CPU_CORES=2", "--env", "DISK_SIZE=64G", "--env", "DISK_TYPE=sata", "--env", "ARGUMENTS=-object iothread,id=io2", state.ImageTag], output, "docker-create", deadline.Token);
|
|
|
|
|
var create = await Command("docker", ["create", "--name", state.ContainerName, "--label", OwnerLabel + "=" + token, "--memory", "6g", "--memory-swap", "6g", "--cpus", "2", "--shm-size", "512m", "--log-opt", "max-size=8m", "--log-opt", "max-file=1", "--env", "KVM=N", "--env", "CPU_MODEL=" + CpuModel, "--env", "NETWORK=slirp", "--env", "DISPLAY=web", "--env", "MANUAL=N", "--env", "VERSION=14", "--env", "RAM_SIZE=4G", "--env", "CPU_CORES=2", "--env", "DISK_SIZE=64G", "--env", "DISK_TYPE=sata", "--env", "ARGUMENTS=-object iothread,id=io2", state.ImageTag], output, "docker-create", deadline.Token);
|
|
|
|
|
var id = create.Output.Trim();
|
|
|
|
|
if (!System.Text.RegularExpressions.Regex.IsMatch(id, "^[0-9a-f]{64}$")) throw new InvalidOperationException("Docker did not return a container identity.");
|
|
|
|
|
state = state with { ContainerId = id };
|
|
|
|
@@ -131,13 +145,21 @@ static class NativeDiagnostic
|
|
|
|
|
await Command("docker", ["inspect", id], output, "container-created", deadline.Token);
|
|
|
|
|
AssertContainer(File.ReadAllText(Path.Combine(output, "container-created.stdout.log")), token);
|
|
|
|
|
await Command("docker", ["start", id], output, "docker-start", deadline.Token);
|
|
|
|
|
Console.WriteLine("The owned restricted KVM/host-CPU macOS 13 compatibility guest is starting. Success requires native macOS 13+/x86_64 and a writable 64-GiB disk; no installer will run. This is not a single-variable causal test.");
|
|
|
|
|
await CapturePressure(id, output, "before", deadline.Token);
|
|
|
|
|
Console.WriteLine("The owned unprivileged TCG/Haswell macOS 14 guest is starting. Success requires native macOS 14+/x86_64 and a writable 64-GiB disk; no installer will run. This is not a single-variable causal test.");
|
|
|
|
|
var recoveryStarted = Stopwatch.StartNew();
|
|
|
|
|
var heartbeat = Stopwatch.StartNew();
|
|
|
|
|
var diskPressureCaptured = false;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
deadline.Token.ThrowIfCancellationRequested();
|
|
|
|
|
await CaptureGuest(id, output, deadline.Token);
|
|
|
|
|
var proofPath = Path.Combine(output, "guest-proof.log");
|
|
|
|
|
if (!diskPressureCaptured && File.Exists(proofPath) && File.ReadAllText(proofPath).Contains("[proof-start] disks", StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
diskPressureCaptured = true;
|
|
|
|
|
await CapturePressure(id, output, "during", deadline.Token);
|
|
|
|
|
}
|
|
|
|
|
var resultPath = Path.Combine(output, "guest-result.json");
|
|
|
|
|
if (File.Exists(resultPath))
|
|
|
|
|
{
|
|
|
|
@@ -159,7 +181,7 @@ static class NativeDiagnostic
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
error = exception is OperationCanceledException ? "The explicit 40-minute diagnostic deadline or cancellation was reached." : exception.Message;
|
|
|
|
|
error = exception is OperationCanceledException ? "The explicit 90-minute diagnostic deadline or cancellation was reached." : exception.Message;
|
|
|
|
|
Console.Error.WriteLine(error);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
@@ -167,6 +189,7 @@ static class NativeDiagnostic
|
|
|
|
|
Console.CancelKeyPress -= cancelHandler;
|
|
|
|
|
using var captureDeadline = new CancellationTokenSource(TimeSpan.FromSeconds(45));
|
|
|
|
|
try { await CaptureGuest(state.ContainerId ?? state.ContainerName, output, captureDeadline.Token, true, state.Token); } catch (Exception exception) { Console.Error.WriteLine("Final evidence capture: " + exception.Message); }
|
|
|
|
|
await CapturePressure(state.ContainerId ?? state.ContainerName, output, "after", captureDeadline.Token);
|
|
|
|
|
try { PrintGuestProof(output, state.Token); } catch (Exception exception) { Console.Error.WriteLine("Native proof output: " + exception.Message); }
|
|
|
|
|
var clean = await Cleanup(output);
|
|
|
|
|
if (!clean) { outcome = "failed"; error = (error ?? "") + " Owned-resource cleanup failed; inspect cleanup evidence."; }
|
|
|
|
@@ -184,9 +207,10 @@ static class NativeDiagnostic
|
|
|
|
|
static void ValidateContracts()
|
|
|
|
|
{
|
|
|
|
|
var readiness = File.ReadAllText(Path.Combine("tools", "ci", "macos-native-readiness.sh"));
|
|
|
|
|
var baseline = ReplaceOnce(readiness, "(( ${os_version%%.*} >= 13 ))", "(( ${os_version%%.*} >= 14 ))");
|
|
|
|
|
var baseline = NormalizeReadinessDiagnostics(readiness);
|
|
|
|
|
baseline = ReplaceOnce(baseline, "while (( attempt < 1 && SECONDS - readiness_start < 600 )); do", "while (( SECONDS - readiness_start < 600 )); do");
|
|
|
|
|
if (Hash(Encoding.UTF8.GetBytes(baseline)) != "4d428f594dac14eff64ed87b172c81ecf85ac91da8c5460cd6ec4b1d310800c3")
|
|
|
|
|
throw new InvalidOperationException("Compatibility readiness may change only the baseline's macOS minimum to 13; identity, services, disk and limits must remain identical.");
|
|
|
|
|
throw new InvalidOperationException("Outside seven explicit diagnostic blocks, the successful sw_vers version parser/sequence and one-attempt limit, baseline identity/service/disk gates and watchdogs must remain identical.");
|
|
|
|
|
if (Hash(File.ReadAllBytes(Path.Combine("tools", "ci", "macos-native-bootstrap.sh"))) != "94f069e116fdc7685a4d233cab6fa50df9f39274386bb82157674061e74fadb5")
|
|
|
|
|
throw new InvalidOperationException("Compatibility profile must preserve the baseline Apple recoveryosd wrapper.");
|
|
|
|
|
if (Hash(Encoding.UTF8.GetBytes(OriginalDaemon13)) != "af9d7f6c1948079bd4384d27b6882678d6fb4e338fcf6a8be8f84fceef174ad6") throw new InvalidOperationException("macOS 13 allowlist bytes differ from the independently read comparison plist.");
|
|
|
|
@@ -196,25 +220,57 @@ static class NativeDiagnostic
|
|
|
|
|
ValidateDaemon(variant.Item2, variant.Item3, true);
|
|
|
|
|
if (Encoding.UTF8.GetByteCount(variant.Item2) > Encoding.UTF8.GetByteCount(variant.Item1)) throw new InvalidOperationException("Daemon replacement exceeds original file.");
|
|
|
|
|
}
|
|
|
|
|
var good = JsonSerializer.Serialize(new { token = "validation", success = true, osVersion = "13.6.1", architecture = "x86_64", uid = 0, disk = "/dev/disk1", diskBytes = GuestDiskBytes, readOnly = false, systemExit = 0, diskArbitrationExit = 0, recoveryExit = 0, diskListExit = 0 });
|
|
|
|
|
var good = JsonSerializer.Serialize(new { token = "validation", success = true, osVersion = "14.6.1", architecture = "x86_64", uid = 0, disk = "/dev/disk1", diskBytes = GuestDiskBytes, readOnly = false, systemExit = 0, diskArbitrationExit = 0, recoveryExit = 0, diskListExit = 0 });
|
|
|
|
|
ValidateResult(good, "validation");
|
|
|
|
|
foreach (var invalid in new[] { good.Replace("13.6.1", "12.6.1"), good.Replace("x86_64", "arm64"), good.Replace("\"readOnly\":false", "\"readOnly\":true"), good.Replace("\"success\":true", "\"success\":false"), good.Replace("68719476736", "17179869184"), good.Replace("validation", "stale") })
|
|
|
|
|
foreach (var invalid in new[] { good.Replace("14.6.1", "13.6.1"), good.Replace("x86_64", "arm64"), good.Replace("\"readOnly\":false", "\"readOnly\":true"), good.Replace("\"success\":true", "\"success\":false"), good.Replace("68719476736", "17179869184"), good.Replace("validation", "stale") })
|
|
|
|
|
{
|
|
|
|
|
try { ValidateResult(invalid, "validation"); } catch (InvalidOperationException) { continue; }
|
|
|
|
|
throw new InvalidOperationException("Diagnostic validator accepted an invalid/stale result.");
|
|
|
|
|
}
|
|
|
|
|
var boundary = """
|
|
|
|
|
[{"Config":{"Labels":{"org.meeting-assistant.native-diagnostic":"validation"},"Env":["KVM=Y","CPU_MODEL=host","VERSION=13"]},"HostConfig":{"Privileged":false,"NetworkMode":"default","Memory":6442450944,"MemorySwap":6442450944,"NanoCpus":2000000000,"ShmSize":536870912,"CapAdd":null,"DeviceRequests":null,"Binds":null,"PortBindings":{},"DeviceCgroupRules":null,"Tmpfs":null,"Devices":[{"PathOnHost":"/dev/kvm","PathInContainer":"/dev/kvm","CgroupPermissions":"rw"}]},"Mounts":[{"Type":"volume","Destination":"/storage","RW":true}]}]
|
|
|
|
|
[{"Config":{"Labels":{"org.meeting-assistant.native-diagnostic":"validation"},"Env":["KVM=N","CPU_MODEL=Haswell-noTSX","VERSION=14"]},"HostConfig":{"Privileged":false,"NetworkMode":"default","Memory":6442450944,"MemorySwap":6442450944,"NanoCpus":2000000000,"ShmSize":536870912,"CapAdd":null,"DeviceRequests":null,"Binds":null,"PortBindings":{},"DeviceCgroupRules":null,"Tmpfs":null,"Devices":[]},"Mounts":[{"Type":"volume","Destination":"/storage","RW":true}]}]
|
|
|
|
|
""";
|
|
|
|
|
AssertContainer(boundary, "validation");
|
|
|
|
|
foreach (var invalid in new[] { boundary.Replace("\"Privileged\":false", "\"Privileged\":true"), boundary.Replace("\"CgroupPermissions\":\"rw\"", "\"CgroupPermissions\":\"rwm\""), boundary.Replace("/dev/kvm", "/dev/other"), boundary.Replace("KVM=Y", "KVM=N"), boundary.Replace("CPU_MODEL=host", "CPU_MODEL=Skylake-Client-v4"), boundary.Replace("VERSION=13", "VERSION=14"), boundary.Replace("6442450944", "8589934592"), boundary.Replace("\"NetworkMode\":\"default\"", "\"NetworkMode\":\"host\""), boundary.Replace("\"CapAdd\":null", "\"CapAdd\":[\"NET_ADMIN\"]"), boundary.Replace("\"Type\":\"volume\"", "\"Type\":\"bind\""), boundary.Replace("/storage", "/host") })
|
|
|
|
|
foreach (var invalid in new[] { boundary.Replace("\"Privileged\":false", "\"Privileged\":true"), boundary.Replace("\"Devices\":[]", "\"Devices\":[{\"PathOnHost\":\"/dev/kvm\",\"PathInContainer\":\"/dev/kvm\",\"CgroupPermissions\":\"rw\"}]"), boundary.Replace("KVM=N", "KVM=Y"), boundary.Replace("CPU_MODEL=Haswell-noTSX", "CPU_MODEL=host"), boundary.Replace("VERSION=14", "VERSION=13"), boundary.Replace("6442450944", "8589934592"), boundary.Replace("\"NetworkMode\":\"default\"", "\"NetworkMode\":\"host\""), boundary.Replace("\"CapAdd\":null", "\"CapAdd\":[\"NET_ADMIN\"]"), boundary.Replace("\"Type\":\"volume\"", "\"Type\":\"bind\""), boundary.Replace("/storage", "/host"), boundary.Replace("\"NanoCpus\":2000000000", "\"NanoCpus\":4000000000") })
|
|
|
|
|
{
|
|
|
|
|
try { AssertContainer(invalid, "validation"); } catch (InvalidOperationException) { continue; }
|
|
|
|
|
throw new InvalidOperationException("Diagnostic validator accepted an excessive/wrong-profile container boundary.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task PrepareSource(string source, string output, string token, bool writeSource, string? cryptexArchive, CancellationToken cancellation)
|
|
|
|
|
static string NormalizeReadinessDiagnostics(string source)
|
|
|
|
|
{
|
|
|
|
|
const string start = "# BEGIN disk IPC diagnostic\n";
|
|
|
|
|
const string end = "# END disk IPC diagnostic\n";
|
|
|
|
|
var blocks = 0;
|
|
|
|
|
while (source.IndexOf(start, StringComparison.Ordinal) is var from && from >= 0)
|
|
|
|
|
{
|
|
|
|
|
var to = source.IndexOf(end, from + start.Length, StringComparison.Ordinal);
|
|
|
|
|
if (to < 0 || source.IndexOf(start, from + start.Length, to - from - start.Length, StringComparison.Ordinal) >= 0)
|
|
|
|
|
throw new InvalidOperationException("Readiness diagnostic blocks are unbalanced or nested.");
|
|
|
|
|
source = source.Remove(from, to + end.Length - from);
|
|
|
|
|
blocks++;
|
|
|
|
|
}
|
|
|
|
|
if (blocks != 7 || source.Contains(end, StringComparison.Ordinal))
|
|
|
|
|
throw new InvalidOperationException("Readiness must contain exactly seven explicit disk IPC diagnostic blocks.");
|
|
|
|
|
source = RestoreVersionBlock(source, "successful sw_vers version parser", "");
|
|
|
|
|
source = RestoreVersionBlock(source, "successful sw_vers version extraction", "run_command version /usr/bin/sw_vers -productVersion\n(( LAST_EXIT == 0 )) || fail_probe product_version_failed\nread_scalar || fail_probe product_version_invalid\n");
|
|
|
|
|
return source;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string RestoreVersionBlock(string source, string name, string originalSequence)
|
|
|
|
|
{
|
|
|
|
|
var start = "# BEGIN " + name + "\n";
|
|
|
|
|
var end = "# END " + name + "\n";
|
|
|
|
|
if (source.Split(start, StringSplitOptions.None).Length != 2 || source.Split(end, StringSplitOptions.None).Length != 2)
|
|
|
|
|
throw new InvalidOperationException("Readiness requires exactly one named version marker pair: " + name);
|
|
|
|
|
var from = source.IndexOf(start, StringComparison.Ordinal);
|
|
|
|
|
var to = source.IndexOf(end, StringComparison.Ordinal);
|
|
|
|
|
if (to < from + start.Length) throw new InvalidOperationException("Readiness version markers are reversed: " + name);
|
|
|
|
|
return source.Remove(from, to + end.Length - from).Insert(from, originalSequence);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task PrepareSource(string source, string output, string token, bool writeSource, CancellationToken cancellation)
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(output);
|
|
|
|
|
var patchPath = Path.Combine(source, "src/install/recovery/patch.py");
|
|
|
|
@@ -232,11 +288,20 @@ static class NativeDiagnostic
|
|
|
|
|
var dockerfile = ReplaceOnce(File.ReadAllText(dockerPath), "FROM scratch AS base\nCOPY --from=qemux/qemu:7.50 --exclude=usr/bin/qemu-system-x86_64 / /\n", "FROM qemux/qemu:7.50@sha256:e7f6fda52503a546fd649670ba46e4bc23dc6dcef275bc3fac48877fbbc430df AS base\n");
|
|
|
|
|
dockerfile = ReplaceAllExact(dockerfile, "--from=qemux/qemu-macos:latest ", "--from=qemux/qemu-macos:latest@sha256:af64297171228f27d5f616249e18f6ad5e2fbc79c1cc517252521e8bcd8eadaa ", 2);
|
|
|
|
|
dockerfile = ReplaceOnce(dockerfile, "ADD $REPO_KVM_OPENCORE/releases/download/v$VERSION_KVM_OPENCORE/LongQT-OpenCore-v$VERSION_KVM_OPENCORE.iso /opencore.iso", "ADD --checksum=sha256:" + OpenCoreTemplateHash + " $REPO_KVM_OPENCORE/releases/download/v$VERSION_KVM_OPENCORE/LongQT-OpenCore-v$VERSION_KVM_OPENCORE.iso /opencore.iso");
|
|
|
|
|
var compatibility = await PrepareCompatibility(source, output, cryptexArchive, cancellation);
|
|
|
|
|
dockerfile = ReplaceOnce(dockerfile, " gzip \\\n", " gzip \\\n nasm \\\n");
|
|
|
|
|
dockerfile = ReplaceOnce(dockerfile, "COPY --chmod=755 ./assets /assets/\n", "COPY --chmod=755 ./assets /assets/\nRUN nasm -f bin /assets/ci-cpu-preflight.asm -o /assets/ci-cpu-preflight.bin && test \"$(stat -c%s /assets/ci-cpu-preflight.bin)\" = 65536\n");
|
|
|
|
|
var boot = PrepareTcgBoot(source);
|
|
|
|
|
var cpuPath = Path.Combine(source, "src/cpu.sh");
|
|
|
|
|
var cpu = File.ReadAllText(cpuPath);
|
|
|
|
|
if (Hash(Encoding.UTF8.GetBytes(cpu)) != "0f3e4b4e1c3e17743d3a8d27b77a76424ceebb576b269283d612c489bc70993e") throw new InvalidOperationException("Pinned CPU composition script hash mismatch.");
|
|
|
|
|
cpu = ReplaceOnce(cpu, ",+movbe,+rdrand,check\"", ",+movbe,+rdrand,enforce=on\"");
|
|
|
|
|
var preflight = File.ReadAllText(Path.Combine("tools", "ci", "macos-tcg-cpu-preflight.asm"));
|
|
|
|
|
var entryPath = Path.Combine(source, "src/entry.sh");
|
|
|
|
|
var entry = ReplaceOnce(File.ReadAllText(entryPath), "set -Eeuo pipefail\n", "set -Eeuo pipefail\n\n# Diagnostic budget: inspect existing Docker storage before Recovery download/boot.\ndf -Pk /storage\nfree_kib=$(df -Pk /storage | awk 'NR==2 {print $4}')\n[[ \"$free_kib\" =~ ^[0-9]+$ ]] && (( free_kib >= 8 * 1024 * 1024 )) || { echo 'Existing Docker storage has less than the 8-GiB diagnostic budget.' >&2; exit 1; }\n");
|
|
|
|
|
entry = ReplaceOnce(entry, ". init.sh # Initialize system\n", ". init.sh # Initialize system\n# Fail before Apple downloads if the existing daemon cannot retain this profile.\nenabled \"$KVM\" && [[ \"$CPU_MODEL\" == host && \"$VERSION\" == 13 ]] && grep -Eq '^vendor_id[[:space:]]*:[[:space:]]*GenuineIntel$' /proc/cpuinfo || { error 'Compatibility probe requires existing Intel KVM and the exact host/13 profile.'; exit 1; }\n");
|
|
|
|
|
entry = ReplaceOnce(entry, "trap - ERR\n", "[[ \"$KVM_OPTS\" == *'accel=kvm'* || \"$KVM_OPTS\" == *'-accel kvm'* ]] && [[ \"$KVM_OPTS\" != *tcg* && \"$CPU_MODEL\" == host ]] || { error 'Compatibility profile refuses a TCG/CPU fallback.'; exit 1; }\ninfo '[compatibility-profile] accelerator=kvm cpu=host recovery=13; actual guest gates still pending'\n\ntrap - ERR\n");
|
|
|
|
|
entry = ReplaceOnce(entry, ". cpu.sh # Configure CPU model\n", "");
|
|
|
|
|
entry = ReplaceOnce(entry, ". proc.sh # Initialize processor\n", "");
|
|
|
|
|
entry = ReplaceOnce(entry, ". init.sh # Initialize system\n", ". init.sh # Initialize system\n. cpu.sh # Compose the exact guest CPU before any Apple download\n. proc.sh # Compose the actual accelerator/CPU_FLAGS once\n" + TcgPreflight + "\n");
|
|
|
|
|
entry = ReplaceOnce(entry, "trap - ERR\n", "[[ \"$KVM_OPTS\" == ' -accel tcg,thread=multi' && \"$CPU_FLAGS\" == '" + CpuFlags + "' && \"$CPU_OPTS\" == \"-cpu $CPU_FLAGS -smp $SMP\" ]] || { error 'Supported profile refuses a CPU/accelerator fallback.'; exit 1; }\ninfo '[supported-profile] accelerator=tcg cpu=Haswell-noTSX recovery=14; AVX/AVX2 preflight passed; native guest gates still pending'\n\ntrap - ERR\n");
|
|
|
|
|
var hookPath = Path.Combine("tools", "ci", "macos-native-readiness.sh");
|
|
|
|
|
var hook = ReplaceOnce(File.ReadAllText(hookPath), "@@PROOF_TOKEN@@", token);
|
|
|
|
|
var wrapper = File.ReadAllText(Path.Combine("tools", "ci", "macos-native-bootstrap.sh"));
|
|
|
|
@@ -246,14 +311,16 @@ static class NativeDiagnostic
|
|
|
|
|
var image = ReplaceOnce(originalImage, " if ! cp -f \"$IMAGE_TOOLS/recovery/launch.sh\" \"$script\"; then\n", " if ! cp -f \"$IMAGE_TOOLS/recovery/launch.sh\" \"$script\" ||\n ! cp -f \"$IMAGE_TOOLS/recovery/readiness.sh\" \"${script%/*}/readiness.sh\"; then\n");
|
|
|
|
|
image = ReplaceOnce(image, " if ! cmp -s \"$IMAGE_TOOLS/recovery/launch.sh\" \"$script\" ||\n", " if ! cmp -s \"$IMAGE_TOOLS/recovery/launch.sh\" \"$script\" ||\n ! cmp -s \"$IMAGE_TOOLS/recovery/readiness.sh\" \"$state/readiness.sh\" ||\n");
|
|
|
|
|
image = ReplaceOnce(image, " if ! result=$(python3 \"$IMAGE_TOOLS/recovery/patch.py\" \"$image\"); then\n", " info \"[recovery-original] bytes=$(stat -c%s -- \"$image\") sha256=$(sha256sum \"$image\" | awk '{print $1}')\"\n if ! result=$(python3 \"$IMAGE_TOOLS/recovery/patch.py\" \"$image\"); then\n");
|
|
|
|
|
foreach (var pair in new[] { ("recovery-patch.py", patch), ("Dockerfile.patched", dockerfile), ("container-entry.sh", entry), ("guest-launch.sh", wrapper), ("guest-readiness.sh", hook), ("image.sh.patched", image), ("recoveryosd-original.plist", daemon), ("recoveryosd-diagnostic.plist", DiagnosticDaemon), ("recoveryosd-13-original.plist", OriginalDaemon13), ("recoveryosd-13-diagnostic.plist", DiagnosticDaemon13), ("early-bootstrap.sh", MountOnlyBootstrap), ("boot.sh.patched", compatibility.Boot), ("opencore-config.plist", compatibility.Config) })
|
|
|
|
|
foreach (var pair in new[] { ("recovery-patch.py", patch), ("Dockerfile.patched", dockerfile), ("container-entry.sh", entry), ("guest-launch.sh", wrapper), ("guest-readiness.sh", hook), ("image.sh.patched", image), ("recoveryosd-original.plist", daemon), ("recoveryosd-diagnostic.plist", DiagnosticDaemon), ("recoveryosd-13-original.plist", OriginalDaemon13), ("recoveryosd-13-diagnostic.plist", DiagnosticDaemon13), ("early-bootstrap.sh", MountOnlyBootstrap), ("boot.sh.patched", boot), ("opencore-config.plist", File.ReadAllText(Path.Combine(source, "assets/config.plist"))), ("cpu.sh.patched", cpu), ("ci-cpu-preflight.asm", preflight) })
|
|
|
|
|
File.WriteAllText(Path.Combine(output, pair.Item1), pair.Item2, new UTF8Encoding(false));
|
|
|
|
|
Save(Path.Combine(output, "source-hashes.json"), Directory.GetFiles(output).Where(path => Path.GetFileName(path) is "recovery-patch.py" or "udif_checksums.py" or "Dockerfile.patched" or "container-entry.sh" or "guest-launch.sh" or "guest-readiness.sh" or "image.sh.patched" or "recoveryosd-original.plist" or "recoveryosd-diagnostic.plist" or "recoveryosd-13-original.plist" or "recoveryosd-13-diagnostic.plist" or "early-bootstrap.sh" or "boot.sh.patched" or "opencore-config.plist" or "compatibility-boot-assets.json").ToDictionary(path => Path.GetFileName(path)!, path => Hash(File.ReadAllBytes(path))));
|
|
|
|
|
Save(Path.Combine(output, "source-hashes.json"), Directory.GetFiles(output).Where(path => Path.GetFileName(path) is "recovery-patch.py" or "udif_checksums.py" or "Dockerfile.patched" or "container-entry.sh" or "guest-launch.sh" or "guest-readiness.sh" or "image.sh.patched" or "recoveryosd-original.plist" or "recoveryosd-diagnostic.plist" or "recoveryosd-13-original.plist" or "recoveryosd-13-diagnostic.plist" or "early-bootstrap.sh" or "boot.sh.patched" or "opencore-config.plist" or "cpu.sh.patched" or "ci-cpu-preflight.asm").ToDictionary(path => Path.GetFileName(path)!, path => Hash(File.ReadAllBytes(path))));
|
|
|
|
|
Save(Path.Combine(output, "cpu-preflight-source.json"), new { profile = Profile, cpuModel = CpuModel, cpuFlags = CpuFlags, expectedExitCode = 33, instructionProbeExecuted = false, qemuBinaryExecuted = false, sourceSha256 = Hash(Encoding.UTF8.GetBytes(preflight)) });
|
|
|
|
|
await Command("bash", ["-n", Path.Combine(output, "guest-launch.sh")], output, "guest-hook-syntax", cancellation);
|
|
|
|
|
await Command("bash", ["-n", Path.Combine(output, "guest-readiness.sh")], output, "guest-readiness-syntax", cancellation);
|
|
|
|
|
await Command("bash", ["-n", Path.Combine(output, "image.sh.patched")], output, "guest-staging-syntax", cancellation);
|
|
|
|
|
await Command("bash", ["-n", Path.Combine(output, "container-entry.sh")], output, "entry-syntax", cancellation);
|
|
|
|
|
await Command("bash", ["-n", Path.Combine(output, "boot.sh.patched")], output, "boot-staging-syntax", cancellation);
|
|
|
|
|
await Command("bash", ["-n", Path.Combine(output, "cpu.sh.patched")], output, "cpu-composition-syntax", cancellation);
|
|
|
|
|
if (!writeSource) return;
|
|
|
|
|
File.WriteAllText(patchPath, patch, new UTF8Encoding(false));
|
|
|
|
|
File.WriteAllText(Path.Combine(source, "src/install/recovery/udif_checksums.py"), checksumBinding, new UTF8Encoding(false));
|
|
|
|
@@ -262,16 +329,9 @@ static class NativeDiagnostic
|
|
|
|
|
File.WriteAllText(imagePath, image, new UTF8Encoding(false));
|
|
|
|
|
File.WriteAllText(Path.Combine(source, "src/install/recovery/launch.sh"), wrapper, new UTF8Encoding(false));
|
|
|
|
|
File.WriteAllText(Path.Combine(source, "src/install/recovery/readiness.sh"), hook, new UTF8Encoding(false));
|
|
|
|
|
File.WriteAllText(Path.Combine(source, "src/boot.sh"), compatibility.Boot, new UTF8Encoding(false));
|
|
|
|
|
File.WriteAllText(Path.Combine(source, "assets/config.plist"), compatibility.Config, new UTF8Encoding(false));
|
|
|
|
|
var target = Path.Combine(source, "assets", "native-compatibility");
|
|
|
|
|
if (Directory.Exists(target)) throw new InvalidOperationException("Refusing an existing compatibility asset overlay.");
|
|
|
|
|
foreach (var file in Directory.GetFiles(compatibility.Assets, "*", SearchOption.AllDirectories))
|
|
|
|
|
{
|
|
|
|
|
var destination = Path.Combine(target, Path.GetRelativePath(compatibility.Assets, file));
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(destination)!);
|
|
|
|
|
File.Copy(file, destination, false);
|
|
|
|
|
}
|
|
|
|
|
File.WriteAllText(Path.Combine(source, "src/boot.sh"), boot, new UTF8Encoding(false));
|
|
|
|
|
File.WriteAllText(cpuPath, cpu, new UTF8Encoding(false));
|
|
|
|
|
File.WriteAllText(Path.Combine(source, "assets/ci-cpu-preflight.asm"), preflight, new UTF8Encoding(false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ValidateDaemon(string xml, string processType, bool patched)
|
|
|
|
@@ -317,53 +377,19 @@ static class NativeDiagnostic
|
|
|
|
|
return string.Join("\n", lines.Select(line => new string(' ', spaces) + (line.Length > 0 ? line[common..] : "")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task<(string Boot, string Config, string Assets)> PrepareCompatibility(string source, string output, string? archivePath, CancellationToken cancellation)
|
|
|
|
|
static string PrepareTcgBoot(string source)
|
|
|
|
|
{
|
|
|
|
|
var boot = File.ReadAllText(Path.Combine(source, "src", "boot.sh"));
|
|
|
|
|
var config = File.ReadAllText(Path.Combine(source, "assets", "config.plist"));
|
|
|
|
|
if (Hash(Encoding.UTF8.GetBytes(boot)) != "82b56525707a8f586e040f56108b5034c02e7fecfea071f1857e596cba10cbed" || Hash(Encoding.UTF8.GetBytes(config)) != "3b0ec58b693cfa0fadf3e3f952486e87af8c27d504f9545d1e90ae2dc3777096") throw new InvalidOperationException("Pinned OpenCore staging/config hashes mismatch.");
|
|
|
|
|
byte[] bytes;
|
|
|
|
|
if (archivePath is not null) bytes = await File.ReadAllBytesAsync(archivePath, cancellation);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
using var client = new HttpClient { Timeout = TimeSpan.FromSeconds(30), MaxResponseContentBufferSize = 2 * 1024 * 1024 };
|
|
|
|
|
bytes = await client.GetByteArrayAsync(CryptexUrl, cancellation);
|
|
|
|
|
}
|
|
|
|
|
if (bytes.Length != 69703 || Hash(bytes) != CryptexHash) throw new InvalidOperationException("Official CryptexFixup release size/hash mismatch.");
|
|
|
|
|
File.WriteAllBytes(Path.Combine(output, "CryptexFixup-1.0.5-RELEASE.zip"), bytes);
|
|
|
|
|
var assets = Path.Combine(output, "compatibility-assets");
|
|
|
|
|
if (Directory.Exists(assets)) throw new InvalidOperationException("Compatibility validation requires a fresh output directory.");
|
|
|
|
|
Directory.CreateDirectory(assets);
|
|
|
|
|
using var archive = new ZipArchive(new MemoryStream(bytes), ZipArchiveMode.Read);
|
|
|
|
|
var required = new[] { "CryptexFixup.kext/Contents/Info.plist", "CryptexFixup.kext/Contents/MacOS/CryptexFixup" };
|
|
|
|
|
var entries = archive.Entries.Where(entry => entry.FullName.StartsWith("CryptexFixup.kext/", StringComparison.Ordinal) && !entry.FullName.EndsWith('/')).ToArray();
|
|
|
|
|
if (entries.Length != 2 || required.Any(name => entries.Count(entry => entry.FullName == name) != 1)) throw new InvalidOperationException("Cryptex bundle has an unexpected file layout.");
|
|
|
|
|
foreach (var entry in entries)
|
|
|
|
|
{
|
|
|
|
|
if (entry.Length <= 0 || entry.Length > 1024 * 1024) throw new InvalidOperationException("Cryptex bundle file exceeded the staging bound.");
|
|
|
|
|
var destination = Path.Combine(assets, entry.FullName);
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(destination)!);
|
|
|
|
|
entry.ExtractToFile(destination, false);
|
|
|
|
|
}
|
|
|
|
|
var info = XDocument.Load(Path.Combine(assets, required[0])).Root!.Element("dict")!;
|
|
|
|
|
if (PlistValue(info, "CFBundleIdentifier").Value != "com.khronokernel.CryptexFixup" || PlistValue(info, "CFBundleVersion").Value != "1.0.5" || PlistValue(info, "CFBundleExecutable").Value != "CryptexFixup" || PlistValue(PlistValue(info, "OSBundleLibraries"), "as.vit9696.Lilu").Value != "1.4.7") throw new InvalidOperationException("Cryptex bundle identity/version/Lilu dependency mismatch.");
|
|
|
|
|
var fileHashes = required.ToDictionary(name => name, name => Hash(File.ReadAllBytes(Path.Combine(assets, name))));
|
|
|
|
|
File.WriteAllText(Path.Combine(assets, "SHA256SUMS"), string.Concat(fileHashes.Select(pair => pair.Value + " " + pair.Key + "\n")), new UTF8Encoding(false));
|
|
|
|
|
Save(Path.Combine(output, "compatibility-boot-assets.json"), new { cryptexUrl = CryptexUrl, cryptexSha256 = CryptexHash, cryptexBytes = bytes.Length, cryptexFiles = fileHashes, templateUrl = "https://github.com/LongQT-sea/OpenCore-ISO/releases/download/v0.7/LongQT-OpenCore-v0.7.iso", templateSha256 = OpenCoreTemplateHash, templateBytes = 15884288, liluVersion = "1.7.1", liluBinarySha256 = "0c016d93cfe40c7fa3965813175c1b991a76f3d295efd5be66ae712b4a3ffb52", liluBinaryBytes = 526984, liluInfoSha256 = "fc885f3319f326e3af60e7965a5216b671772d39d40993ec695758bb43d6ea3a", causalSingleVariableTest = false });
|
|
|
|
|
var document = XDocument.Parse(config, LoadOptions.PreserveWhitespace);
|
|
|
|
|
var add = PlistValue(PlistValue(document.Root!.Element("dict")!, "Kernel"), "Add");
|
|
|
|
|
var expected = new[] { "Lilu.kext", "VMHide.kext", "VirtualSMC.kext", "WhateverGreen.kext", "VoodooPS2Controller.kext", "VoodooPS2Controller.kext/Contents/PlugIns/VoodooPS2Keyboard.kext", "AppleMCEReporterDisabler.kext" };
|
|
|
|
|
if (!add.Elements("dict").Select(dict => PlistValue(dict, "BundlePath").Value).SequenceEqual(expected) || add.Elements("dict").Any(dict => PlistValue(dict, "Enabled").Name != "true")) throw new InvalidOperationException("Pinned Kernel.Add order/enabled contract mismatch.");
|
|
|
|
|
var cryptex = XElement.Parse("<dict><key>Arch</key><string>x86_64</string><key>BundlePath</key><string>CryptexFixup.kext</string><key>Comment</key><string>Official CryptexFixup 1.0.5; owned compatibility guest only</string><key>Enabled</key><true/><key>ExecutablePath</key><string>Contents/MacOS/CryptexFixup</string><key>MaxKernel</key><string></string><key>MinKernel</key><string>22.0.0</string><key>PlistPath</key><string>Contents/Info.plist</string></dict>");
|
|
|
|
|
add.Elements("dict").First().AddAfterSelf(cryptex);
|
|
|
|
|
var bootArguments = PlistValue(PlistValue(PlistValue(PlistValue(document.Root.Element("dict")!, "NVRAM"), "Add"), "7C436110-AB2A-4BBB-A880-FE41995C9F82"), "boot-args").Value.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (bootArguments.Intersect(new[] { "-cryptoff", "-liluoff", "-crypt_allow_hash_validation", "-crypt_force_avx", "-cryptbeta", "-lilubetaall" }).Any()) throw new InvalidOperationException("Unexpected Cryptex/Lilu disabling or forcing boot argument.");
|
|
|
|
|
boot = ReplaceOnce(boot, " cp -a \"$template/OC/Resources\" \"$EFI_DIR/OC/\"\n", " cp -a \"$template/OC/Resources\" \"$EFI_DIR/OC/\"\n" + CompatibilityStaging + "\n");
|
|
|
|
|
boot = ReplaceOnce(boot, " PLIST=\"/assets/config.plist\"\n", " [ ! -e /custom.plist ] || { error 'Compatibility profile refuses an unverified custom OpenCore config!'; exit 12; }\n PLIST=\"/assets/config.plist\"\n");
|
|
|
|
|
boot = ReplaceOnce(boot, " checkOpenCoreConfig\n addVmHideKext\n", " checkOpenCoreConfig\n" + CompatibilityConfigCheck + "\n addVmHideKext\n");
|
|
|
|
|
boot = ReplaceOnce(boot, " PLIST=\"/assets/config.plist\"\n", " [ ! -e /custom.plist ] || { error 'Supported profile refuses an unverified custom OpenCore config!'; exit 12; }\n PLIST=\"/assets/config.plist\"\n");
|
|
|
|
|
boot = ReplaceOnce(boot, " if [ -s \"$target\" ] && [ \"$previous\" = \"$current\" ]; then\n IMG=\"$target\"\n return 0\n fi\n", " # This owned compatibility probe always rebuilds; never trust a cached boot.img.\n");
|
|
|
|
|
boot = ReplaceOnce(boot, " echo \"VMHIDE=$vmhide\"\n", " echo \"VMHIDE=$vmhide\"\n echo \"COMPATIBILITY=kvm-host-ventura-cryptex\"\n sha256sum /assets/native-compatibility/SHA256SUMS\n");
|
|
|
|
|
return (boot, document.ToString(), assets);
|
|
|
|
|
boot = ReplaceOnce(boot, " echo \"VMHIDE=$vmhide\"\n", " echo \"VMHIDE=$vmhide\"\n echo \"PROFILE=tcg-haswell-sonoma\"\n");
|
|
|
|
|
return boot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static XElement PlistValue(XElement dictionary, string key)
|
|
|
|
@@ -373,32 +399,80 @@ static class NativeDiagnostic
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string CompatibilityStaging = """
|
|
|
|
|
# Only the freshly extracted, owned guest EFI is changed; never the host.
|
|
|
|
|
local lilu="$EFI_DIR/OC/Kexts/Lilu.kext/Contents"
|
|
|
|
|
printf '%s %s\n' \
|
|
|
|
|
fc885f3319f326e3af60e7965a5216b671772d39d40993ec695758bb43d6ea3a "$lilu/Info.plist" \
|
|
|
|
|
0c016d93cfe40c7fa3965813175c1b991a76f3d295efd5be66ae712b4a3ffb52 "$lilu/MacOS/Lilu" | sha256sum -c - || { error "Pinned active Lilu files mismatch!"; exit 12; }
|
|
|
|
|
[ "$(xmlstarlet sel -T -t -v '/plist/dict/key[.="CFBundleVersion"]/following-sibling::string[1]' "$lilu/Info.plist")" = 1.7.1 ] || { error "Active Lilu version mismatch!"; exit 12; }
|
|
|
|
|
[ ! -e "$EFI_DIR/OC/Kexts/CryptexFixup.kext" ] || { error "Unexpected pre-existing Cryptex kext!"; exit 12; }
|
|
|
|
|
(cd /assets/native-compatibility && sha256sum -c SHA256SUMS) || { error "Pinned Cryptex staging files mismatch!"; exit 12; }
|
|
|
|
|
cp -a /assets/native-compatibility/CryptexFixup.kext "$EFI_DIR/OC/Kexts/"
|
|
|
|
|
(cd "$EFI_DIR/OC/Kexts" && sha256sum -c /assets/native-compatibility/SHA256SUMS) || { error "Active Cryptex copy mismatch!"; exit 12; }
|
|
|
|
|
info "[compatibility-boot] Lilu=1.7.1 CryptexFixup=1.0.5 files=verified; guest injection and Recovery readiness remain unproved"
|
|
|
|
|
""";
|
|
|
|
|
static readonly string TcgPreflight = """
|
|
|
|
|
# Realize this exact TCG model and execute AVX/AVX2 before Apple downloads.
|
|
|
|
|
disabled "$KVM" && [[ "$ARCH" == amd64 && "$CPU_MODEL" == Haswell-noTSX && "$VERSION" == 14 && "$CPU_FLAGS" == '@@CPU_FLAGS@@' ]] || { error 'Supported probe requires the exact TCG/Haswell/macOS 14 profile.'; exit 1; }
|
|
|
|
|
[[ "$(qemu-system-x86_64 --version | head -n 1)" == 'QEMU emulator version 11.1.1 (Reims 11.1.3)' ]] || { error 'Pinned QEMU runtime version mismatch.'; exit 1; }
|
|
|
|
|
printf '%s %s\n' c32746122cc68f3ed642aa46c21b677f803c58f0d4ff665841723fcc5625f549 /assets/ci-cpu-preflight.bin | sha256sum -c - || { error 'Compiled AVX/AVX2 preflight ROM hash mismatch.'; exit 1; }
|
|
|
|
|
probeTcgInstructions() {
|
|
|
|
|
/usr/bin/timeout --signal=TERM --kill-after=2 10 qemu-system-x86_64 \
|
|
|
|
|
-machine q35 -accel tcg,thread=multi -cpu "$1" -smp 2 -m 64 -bios /assets/ci-cpu-preflight.bin \
|
|
|
|
|
-nodefaults -display none -serial none -monitor none -nographic -no-reboot \
|
|
|
|
|
-device isa-debug-exit,iobase=0xf4,iosize=0x04
|
|
|
|
|
}
|
|
|
|
|
if probeTcgInstructions "$CPU_FLAGS" > "$QEMU_DIR/cpu-preflight-positive.log" 2>&1; then positive=0; else positive=$?; fi
|
|
|
|
|
cat "$QEMU_DIR/cpu-preflight-positive.log"
|
|
|
|
|
(( positive == 33 )) || { error "Actual TCG AVX/AVX2 instruction preflight failed: exit=$positive"; exit 1; }
|
|
|
|
|
if probeTcgInstructions "$CPU_FLAGS,-avx2" > "$QEMU_DIR/cpu-preflight-negative.log" 2>&1; then negative=0; else negative=$?; fi
|
|
|
|
|
cat "$QEMU_DIR/cpu-preflight-negative.log"
|
|
|
|
|
(( negative != 33 )) || { error 'AVX2-disabled negative control unexpectedly passed.'; exit 1; }
|
|
|
|
|
info "[cpu-preflight] positive=$positive negative=$negative cpu=$CPU_FLAGS; actual AVX/AVX2 executed before Apple download"
|
|
|
|
|
""".Replace("@@CPU_FLAGS@@", CpuFlags, StringComparison.Ordinal);
|
|
|
|
|
|
|
|
|
|
const string CompatibilityConfigCheck = """
|
|
|
|
|
local kernel='/plist/dict/key[.="Kernel"]/following-sibling::dict[1]/key[.="Add"]/following-sibling::array[1]'
|
|
|
|
|
local actual expected
|
|
|
|
|
actual=$(xmlstarlet sel -T -t -m "$kernel/dict" -v 'key[.="BundlePath"]/following-sibling::string[1]' -n "$CFG") || exit 12
|
|
|
|
|
expected=$(printf '%s\n' Lilu.kext CryptexFixup.kext VMHide.kext VirtualSMC.kext WhateverGreen.kext VoodooPS2Controller.kext VoodooPS2Controller.kext/Contents/PlugIns/VoodooPS2Keyboard.kext AppleMCEReporterDisabler.kext)
|
|
|
|
|
[ "$actual" = "$expected" ] || { error "Active Kernel.Add order mismatch!"; exit 12; }
|
|
|
|
|
[ "$(xmlstarlet sel -T -t -v "name($kernel/dict[1]/key[.='Enabled']/following-sibling::*[1])" -v "name($kernel/dict[2]/key[.='Enabled']/following-sibling::*[1])" "$CFG")" = truetrue ] || { error "Active Lilu/Cryptex must both be enabled!"; exit 12; }
|
|
|
|
|
actual=$(xmlstarlet sel -T -t -m "$kernel/dict[2]" -v 'key[.="Arch"]/following-sibling::string[1]' -n -v 'key[.="ExecutablePath"]/following-sibling::string[1]' -n -v 'key[.="PlistPath"]/following-sibling::string[1]' -n -v 'key[.="MinKernel"]/following-sibling::string[1]' -n -v 'key[.="MaxKernel"]/following-sibling::string[1]' "$CFG") || exit 12
|
|
|
|
|
expected=$(printf '%s\n' x86_64 Contents/MacOS/CryptexFixup Contents/Info.plist 22.0.0 '')
|
|
|
|
|
[ "$actual" = "$expected" ] || { error "Active Cryptex Kernel.Add paths/architecture/Darwin bounds mismatch!"; exit 12; }
|
|
|
|
|
info "[compatibility-config] Kernel.Add=Lilu,CryptexFixup before remaining baseline kexts; MinKernel=22.0.0 MaxKernel=empty"
|
|
|
|
|
""";
|
|
|
|
|
static async Task ValidateTcgPreflight(string output, CancellationToken cancellation)
|
|
|
|
|
{
|
|
|
|
|
var fixture = Path.Combine(output, "validation-cpu-preflight-gate");
|
|
|
|
|
Directory.CreateDirectory(fixture);
|
|
|
|
|
var entry = File.ReadAllText(Path.Combine(output, "container-entry.sh"));
|
|
|
|
|
if (entry.IndexOf(TcgPreflight, StringComparison.Ordinal) >= entry.IndexOf(". download.sh", StringComparison.Ordinal)
|
|
|
|
|
|| entry.Split(". cpu.sh", StringSplitOptions.None).Length != 2
|
|
|
|
|
|| entry.Split(". proc.sh", StringSplitOptions.None).Length != 2)
|
|
|
|
|
throw new InvalidOperationException("Actual composed CPU preflight must execute once before any Apple download.");
|
|
|
|
|
var cases = new[]
|
|
|
|
|
{
|
|
|
|
|
("positive-negative-exit", 33, 0, "14", CpuFlags, true, true),
|
|
|
|
|
("positive-negative-timeout", 33, 124, "14", CpuFlags, true, true),
|
|
|
|
|
("positive-normal-exit", 0, 0, "14", CpuFlags, true, false),
|
|
|
|
|
("positive-timeout", 124, 0, "14", CpuFlags, true, false),
|
|
|
|
|
("negative-passed", 33, 33, "14", CpuFlags, true, false),
|
|
|
|
|
("wrong-recovery", 33, 0, "13", CpuFlags, true, false),
|
|
|
|
|
("wrong-cpu-flags", 33, 0, "14", CpuFlags.Replace("enforce=on", "check"), true, false),
|
|
|
|
|
("wrong-rom-hash", 33, 0, "14", CpuFlags, false, false)
|
|
|
|
|
};
|
|
|
|
|
foreach (var item in cases)
|
|
|
|
|
{
|
|
|
|
|
var work = Path.Combine(fixture, item.Item1);
|
|
|
|
|
Directory.CreateDirectory(work);
|
|
|
|
|
var script = """
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
disabled() { [ "$1" = N ]; }
|
|
|
|
|
error() { printf '%s\n' "$*" >&2; }
|
|
|
|
|
info() { printf '%s\n' "$*"; }
|
|
|
|
|
qemu-system-x86_64() { printf '%s\n' 'QEMU emulator version 11.1.1 (Reims 11.1.3)'; }
|
|
|
|
|
sha256sum() { cat >/dev/null; return "@@HASH_EXIT@@"; }
|
|
|
|
|
mock_timeout() {
|
|
|
|
|
printf '[fixture-command] %s\n' "$*"
|
|
|
|
|
case "$*" in *,-avx2*) return @@NEGATIVE@@ ;; *) return @@POSITIVE@@ ;; esac
|
|
|
|
|
}
|
|
|
|
|
KVM=N; ARCH=amd64; CPU_MODEL=Haswell-noTSX; VERSION='@@VERSION@@'
|
|
|
|
|
CPU_FLAGS='@@FLAGS@@'; QEMU_DIR='@@WORK@@'
|
|
|
|
|
""".Replace("@@HASH_EXIT@@", item.Item6 ? "0" : "1", StringComparison.Ordinal)
|
|
|
|
|
.Replace("@@NEGATIVE@@", item.Item3.ToString(), StringComparison.Ordinal)
|
|
|
|
|
.Replace("@@POSITIVE@@", item.Item2.ToString(), StringComparison.Ordinal)
|
|
|
|
|
.Replace("@@VERSION@@", item.Item4, StringComparison.Ordinal)
|
|
|
|
|
.Replace("@@FLAGS@@", item.Item5, StringComparison.Ordinal)
|
|
|
|
|
.Replace("@@WORK@@", work.Replace("'", "'\\''", StringComparison.Ordinal), StringComparison.Ordinal)
|
|
|
|
|
+ "\n" + TcgPreflight.Replace("/usr/bin/timeout", "mock_timeout", StringComparison.Ordinal)
|
|
|
|
|
+ "\nprintf '[fixture] Apple download reached after gate\\n'\n";
|
|
|
|
|
var path = Path.Combine(work, "fixture.sh");
|
|
|
|
|
File.WriteAllText(path, script, new UTF8Encoding(false));
|
|
|
|
|
var result = await Command("bash", [path], work, "gate", cancellation, requireSuccess: false);
|
|
|
|
|
var reached = result.Output.Contains("Apple download reached after gate", StringComparison.Ordinal);
|
|
|
|
|
Save(Path.Combine(work, "receipt.json"), new { success = (result.ExitCode == 0) == item.Item7 && reached == item.Item7, result.ExitCode, reachedAppleDownloadSeam = reached, qemuExecuted = false });
|
|
|
|
|
if ((result.ExitCode == 0) != item.Item7 || reached != item.Item7)
|
|
|
|
|
throw new InvalidOperationException("Actual TCG preflight gate fixture failed: " + item.Item1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string ReplaceOnce(string text, string oldValue, string newValue) => ReplaceAllExact(text, oldValue, newValue, 1);
|
|
|
|
|
static string ReplaceAllExact(string text, string oldValue, string newValue, int expected)
|
|
|
|
@@ -412,8 +486,8 @@ static class NativeDiagnostic
|
|
|
|
|
{
|
|
|
|
|
using var document = JsonDocument.Parse(json);
|
|
|
|
|
var result = document.RootElement;
|
|
|
|
|
if (result.GetProperty("token").GetString() != token || !result.GetProperty("success").GetBoolean() || !Version.TryParse(result.GetProperty("osVersion").GetString(), out var version) || version.Major < 13 || result.GetProperty("architecture").GetString() != "x86_64" || result.GetProperty("uid").GetInt32() != 0 || !System.Text.RegularExpressions.Regex.IsMatch(result.GetProperty("disk").GetString() ?? "", "^/dev/disk[0-9]+$") || result.GetProperty("diskBytes").GetInt64() != GuestDiskBytes || result.GetProperty("readOnly").GetBoolean() || new[] { "systemExit", "diskArbitrationExit", "recoveryExit", "diskListExit" }.Any(key => result.GetProperty(key).GetInt32() != 0))
|
|
|
|
|
throw new InvalidOperationException("The fresh guest receipt did not prove native macOS 13+/x86_64, service readiness and the writable 64-GiB disk.");
|
|
|
|
|
if (result.GetProperty("token").GetString() != token || !result.GetProperty("success").GetBoolean() || !Version.TryParse(result.GetProperty("osVersion").GetString(), out var version) || version.Major < 14 || result.GetProperty("architecture").GetString() != "x86_64" || result.GetProperty("uid").GetInt32() != 0 || !System.Text.RegularExpressions.Regex.IsMatch(result.GetProperty("disk").GetString() ?? "", "^/dev/disk[0-9]+$") || result.GetProperty("diskBytes").GetInt64() != GuestDiskBytes || result.GetProperty("readOnly").GetBoolean() || new[] { "systemExit", "diskArbitrationExit", "recoveryExit", "diskListExit" }.Any(key => result.GetProperty(key).GetInt32() != 0))
|
|
|
|
|
throw new InvalidOperationException("The fresh guest receipt did not prove native macOS 14+/x86_64, service readiness and the writable 64-GiB disk.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void AssertContainer(string json, string token)
|
|
|
|
@@ -434,18 +508,15 @@ static class NativeDiagnostic
|
|
|
|
|
|| new[] { "CapAdd", "DeviceRequests", "Binds", "PortBindings", "DeviceCgroupRules", "Tmpfs" }.Any(key =>
|
|
|
|
|
config.TryGetProperty(key, out var value) && value.ValueKind != JsonValueKind.Null
|
|
|
|
|
&& (value.ValueKind == JsonValueKind.Array ? value.GetArrayLength() != 0 : value.EnumerateObject().Any()))
|
|
|
|
|
|| devices.GetArrayLength() != 1
|
|
|
|
|
|| devices[0].GetProperty("PathOnHost").GetString() != "/dev/kvm"
|
|
|
|
|
|| devices[0].GetProperty("PathInContainer").GetString() != "/dev/kvm"
|
|
|
|
|
|| devices[0].GetProperty("CgroupPermissions").GetString() != "rw"
|
|
|
|
|
|| devices.GetArrayLength() != 0
|
|
|
|
|
|| mounts.GetArrayLength() != 1
|
|
|
|
|
|| mounts[0].GetProperty("Type").GetString() != "volume"
|
|
|
|
|
|| mounts[0].GetProperty("Destination").GetString() != "/storage"
|
|
|
|
|
|| !mounts[0].GetProperty("RW").GetBoolean()
|
|
|
|
|
|| new[] { "KVM=Y", "CPU_MODEL=host", "VERSION=13" }.Any(expected =>
|
|
|
|
|
|| new[] { "KVM=N", "CPU_MODEL=" + CpuModel, "VERSION=14" }.Any(expected =>
|
|
|
|
|
environment.Count(value => value is not null && value.StartsWith(expected.Split('=')[0] + "=", StringComparison.Ordinal)) != 1
|
|
|
|
|
|| !environment.Contains(expected)))
|
|
|
|
|
throw new InvalidOperationException("Created container exceeds the owned restricted KVM/host-CPU compatibility boundary.");
|
|
|
|
|
throw new InvalidOperationException("Created container exceeds the owned unprivileged TCG/Haswell/macOS 14 boundary.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task CaptureGuest(string id, string output, CancellationToken cancellation, bool final = false, string? token = null)
|
|
|
|
@@ -457,7 +528,40 @@ static class NativeDiagnostic
|
|
|
|
|
var result = await Command("docker", ["exec", id, "cat", "/dev/shm/installstate/" + file.Item1], output, "capture-" + file.Item1, cancellation, requireSuccess: false);
|
|
|
|
|
if (result.ExitCode == 0 && !string.IsNullOrWhiteSpace(result.Output)) File.WriteAllText(Path.Combine(output, file.Item2), result.Output);
|
|
|
|
|
}
|
|
|
|
|
await Command("docker", ["exec", id, "sh", "-c", "printf '[qemu]\n'; qemu-system-x86_64 --version | head -n 1; printf '[Recovery hash]\n'; test ! -f /storage/13/setup.dmg || sha256sum /storage/13/setup.dmg; printf '[resources]\n'; df -Pk /storage; cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/cpu.max 2>/dev/null || true"], output, "guest-container-resources", cancellation, requireSuccess: false, retainSuccessful: true);
|
|
|
|
|
// The immutable Recovery image is complete only after this staging marker.
|
|
|
|
|
// Hash it once instead of rereading the image on every twenty-second poll.
|
|
|
|
|
if (logs.Output.Contains("[supported-profile] accelerator=tcg", StringComparison.Ordinal)
|
|
|
|
|
&& !File.Exists(Path.Combine(output, "guest-container-resources.last-success.json")))
|
|
|
|
|
await Command("docker", ["exec", id, "sh", "-c", "printf '[qemu]\n'; qemu-system-x86_64 --version | head -n 1; printf '[Recovery hash]\n'; test -f /storage/14/setup.dmg && sha256sum /storage/14/setup.dmg || exit 1; printf '[resources]\n'; df -Pk /storage; cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/cpu.max 2>/dev/null || true"], output, "guest-container-resources", cancellation, requireSuccess: false, retainSuccessful: true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task CapturePressure(string id, string output, string phase, CancellationToken cancellation)
|
|
|
|
|
{
|
|
|
|
|
using var snapshotDeadline = CancellationTokenSource.CreateLinkedTokenSource(cancellation);
|
|
|
|
|
snapshotDeadline.CancelAfter(TimeSpan.FromSeconds(20));
|
|
|
|
|
const string snapshot = """
|
|
|
|
|
printf '[snapshot UTC]\n'; date -u '+%Y-%m-%dT%H:%M:%SZ'
|
|
|
|
|
for path in /proc/meminfo /proc/pressure/cpu /proc/pressure/memory /proc/pressure/io \
|
|
|
|
|
/sys/fs/cgroup/cpu.max /sys/fs/cgroup/cpu.stat /sys/fs/cgroup/cpu.pressure \
|
|
|
|
|
/sys/fs/cgroup/memory.max /sys/fs/cgroup/memory.current /sys/fs/cgroup/memory.peak \
|
|
|
|
|
/sys/fs/cgroup/memory.events /sys/fs/cgroup/memory.stat /sys/fs/cgroup/memory.pressure \
|
|
|
|
|
/sys/fs/cgroup/memory.swap.current; do
|
|
|
|
|
printf '\n[%s]\n' "$path"
|
|
|
|
|
if [ -r "$path" ]; then cat "$path"; else printf 'unavailable\n'; fi
|
|
|
|
|
done
|
|
|
|
|
printf '\n[host paging counters]\n'
|
|
|
|
|
awk '/^(pgmajfault|pswpin|pswpout) / {print}' /proc/vmstat
|
|
|
|
|
""";
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Command("docker", ["exec", id, "sh", "-c", snapshot], output, "capture-pressure-" + phase, snapshotDeadline.Token, requireSuccess: false, retainSuccessful: true);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
// Optional evidence must not replace the guest outcome or prevent cleanup.
|
|
|
|
|
try { Save(Path.Combine(output, "capture-pressure-" + phase + ".unavailable.json"), new { phase, error = exception.Message, capturedUtc = DateTimeOffset.UtcNow }); }
|
|
|
|
|
catch (Exception evidenceError) { Console.Error.WriteLine("Optional pressure evidence: " + evidenceError.Message); }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task CaptureMonitor(string id, string output, string token, CancellationToken cancellation)
|
|
|
|
|