forked from Manuel/meeting-assistant
753 lines
65 KiB
C#
753 lines
65 KiB
C#
#:property PublishAot=false
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Xml.Linq;
|
|
|
|
// .NET 10 file-based CI diagnostic. See docs/macos-native-diagnostic.md.
|
|
return await NativeDiagnostic.Execute(args);
|
|
|
|
static class NativeDiagnostic
|
|
{
|
|
const string DockurCommit = "16a5b470cdd601bae8b05b02d748d7edfb36c12e";
|
|
const string OwnerLabel = "org.meeting-assistant.native-diagnostic";
|
|
const long GuestDiskBytes = 64L * 1024 * 1024 * 1024;
|
|
const long ContainerMemoryBytes = 6L * 1024 * 1024 * 1024;
|
|
const int MaximumCapturedCharacters = 8 * 1024 * 1024;
|
|
const string SdkVersion = "10.0.401";
|
|
const string SdkSha512 = "33401b4a2da8554e3306db6072ea8569d9fcc608509c271e0aa4b39e7cc432da3631f14e7e1e2445d67d72550d18ce44a8bbd2382a756867ad2edab6b1c963c0";
|
|
const string FullState = "/storage/14/ci-state";
|
|
static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true };
|
|
const string OriginalBootstrap = "[ ! -e /tmp/m ]&&{ /sbin/mount_9p installstate >/dev/null 2>&1;exec /Volumes/installstate/launch.sh;};: >/tmp/m\n";
|
|
const string MountOnlyBootstrap = "[ ! -e /tmp/m ]&& /sbin/mount_9p installstate >/dev/null 2>&1; : >/tmp/m\n";
|
|
static readonly string OriginalDaemon = """
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
<plist version="1.0">
|
|
<dict>
|
|
\t<key>Label</key>
|
|
\t<string>com.apple.recoveryosd</string>
|
|
\t<key>OnDemand</key>
|
|
\t<false/>
|
|
\t<key>ProcessType</key>
|
|
\t<string>App</string>
|
|
\t<key>EnablePressuredExit</key>
|
|
\t<false/>
|
|
\t<key>ProgramArguments</key>
|
|
\t<array>
|
|
\t\t<string>/usr/libexec/recoveryosd</string>
|
|
\t</array>
|
|
</dict>
|
|
</plist>
|
|
""".Replace("\\t", "\t", StringComparison.Ordinal);
|
|
static readonly string DiagnosticDaemon = (OriginalDaemon + "\n")
|
|
.Replace("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n", "", StringComparison.Ordinal)
|
|
.Replace("\t\t<string>/usr/libexec/recoveryosd</string>", "\t\t<string>/bin/bash</string>\n\t\t<string>/Volumes/installstate/launch.sh</string>", StringComparison.Ordinal);
|
|
|
|
public static async Task<int> Execute(string[] args)
|
|
{
|
|
if (args.Length == 0 || args.Contains("--help"))
|
|
{
|
|
Console.WriteLine("dotnet run --file tools/ci/MacOsNativeDiagnostic.cs -- --run|--cleanup|--validate [--full] [--output artifacts/native-macos] [--source existing-dockur-clone] [--compression-chunk readonly-qualified-chunk]");
|
|
return 0;
|
|
}
|
|
var output = Path.GetFullPath(Option(args, "--output") ?? "artifacts/native-macos");
|
|
var full = args.Contains("--full");
|
|
if (args.Contains("--validate"))
|
|
{
|
|
ValidateContracts();
|
|
if (full) ValidateFullContracts();
|
|
if (Option(args, "--source") is { } source)
|
|
await PrepareSource(Path.GetFullPath(source), output, full ? new string('0', 32) : "validation", false, CancellationToken.None, full);
|
|
if (full) await ValidateDiskSerialParser(output);
|
|
if (Option(args, "--compression-chunk") is { } chunk)
|
|
await ValidateCompression(Path.GetFullPath(chunk), output);
|
|
Console.WriteLine("Source patch contracts and diagnostic result validation passed; no Docker or guest execution occurred.");
|
|
return 0;
|
|
}
|
|
if (args.Contains("--cleanup"))
|
|
return await Cleanup(output) ? 0 : 1;
|
|
if (!args.Contains("--run")) throw new ArgumentException("Choose --run, --cleanup or --validate.");
|
|
Directory.CreateDirectory(output);
|
|
var statePath = Path.Combine(output, "owned-resources.json");
|
|
if (File.Exists(statePath)) throw new InvalidOperationException("Output already contains a run identity; choose a fresh directory or clean up its run first.");
|
|
var token = Guid.NewGuid().ToString("N");
|
|
var work = Path.Combine(Environment.GetEnvironmentVariable("RUNNER_TEMP") ?? Path.GetTempPath(), "meeting-assistant-native-" + token);
|
|
var state = new OwnedResources(token, "meeting-assistant-native-" + token, "meeting-assistant-native-diagnostic:" + token, work);
|
|
Save(statePath, state);
|
|
Directory.CreateDirectory(work);
|
|
File.WriteAllText(Path.Combine(work, "run.owner"), token);
|
|
// Full execution leaves eight minutes within the existing 180-minute job for evidence/cleanup.
|
|
var deadlineMinutes = full ? 172 : 40;
|
|
using var deadline = new CancellationTokenSource(TimeSpan.FromMinutes(deadlineMinutes));
|
|
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;
|
|
var outcome = "failed";
|
|
string? error = null;
|
|
try
|
|
{
|
|
if (!OperatingSystem.IsLinux() || RuntimeInformation.ProcessArchitecture != Architecture.X64)
|
|
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, runId = Environment.GetEnvironmentVariable("GITHUB_RUN_ID"), server = Environment.GetEnvironmentVariable("GITHUB_SERVER_URL"), architecture = RuntimeInformation.ProcessArchitecture.ToString(), deadlineMinutes, mode = full ? "full" : "readiness" });
|
|
var info = await Command("docker", ["info", "--format", "{{json .}}"], output, "docker-info", deadline.Token);
|
|
using (var document = JsonDocument.Parse(info.Output))
|
|
{
|
|
var data = document.RootElement;
|
|
if (data.GetProperty("OSType").GetString() != "linux" || data.GetProperty("Architecture").GetString() is not ("x86_64" or "amd64"))
|
|
throw new InvalidOperationException("The existing Docker daemon is not Linux/x64; this diagnostic does not reconfigure it.");
|
|
if (data.GetProperty("NCPU").GetInt32() < 2 || data.GetProperty("MemTotal").GetInt64() < ContainerMemoryBytes)
|
|
throw new InvalidOperationException("Existing Docker resources cannot fit this bounded 2-CPU/6-GiB diagnostic; no infrastructure change was requested.");
|
|
}
|
|
await Command("sh", ["-c", "cat /proc/meminfo; printf '\n[cgroup]\n'; cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/cpu.max 2>/dev/null || true; printf '\n[workspace disk]\n'; df -Pk ."], output, "runner-resources", deadline.Token);
|
|
var available = System.Text.RegularExpressions.Regex.Match(File.ReadAllText("/proc/meminfo"), @"(?m)^MemAvailable:\s+(\d+) kB$");
|
|
if (!available.Success || long.Parse(available.Groups[1].Value) < 5L * 1024 * 1024)
|
|
throw new InvalidOperationException("Existing runner memory has less than the 5-GiB available diagnostic budget; no infrastructure change was requested.");
|
|
var source = Path.Combine(work, "dockur");
|
|
await Command("git", ["clone", "--no-checkout", "https://github.com/dockur/macos.git", source], output, "dockur-clone", deadline.Token);
|
|
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.");
|
|
if (full) await PreparePayload(source, output, token, sourceCommit, deadline.Token);
|
|
await PrepareSource(source, output, token, true, deadline.Token, full);
|
|
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);
|
|
List<string> createArguments = ["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", "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"];
|
|
if (full) createArguments.AddRange(["--env", "ALLOCATE=N", "--env", "DISK_OPTIONS=serial=" + DiskSerial(token)]);
|
|
createArguments.Add(state.ImageTag);
|
|
var create = await Command("docker", createArguments.ToArray(), 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 };
|
|
Save(statePath, state);
|
|
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(full ? "The owned unprivileged TCG guest is starting. Installation requires a fresh native readiness receipt and an explicit owned-disk permit; success requires all 577 tests with zero skips." : "The owned unprivileged TCG guest is starting. Success requires native macOS 14+/x86_64 and a writable 64-GiB disk; no installer will run.");
|
|
var phaseStarted = Stopwatch.StartNew();
|
|
var phase = "recovery";
|
|
var phaseBudget = TimeSpan.FromMinutes(40);
|
|
var heartbeat = Stopwatch.StartNew();
|
|
var permitted = false;
|
|
while (true)
|
|
{
|
|
deadline.Token.ThrowIfCancellationRequested();
|
|
await CaptureGuest(id, output, deadline.Token, full);
|
|
if (full && phaseStarted.Elapsed > phaseBudget)
|
|
throw new InvalidOperationException("The bounded native " + phase + " phase exceeded " + phaseBudget.TotalMinutes + " minutes.");
|
|
var resultPath = Path.Combine(output, "guest-result.json");
|
|
if (File.Exists(resultPath) && !permitted)
|
|
{
|
|
var result = File.ReadAllText(resultPath);
|
|
ValidateResult(result, token);
|
|
if (full)
|
|
{
|
|
await PermitInstallation(id, output, token, sourceCommit, result, deadline.Token);
|
|
permitted = true;
|
|
phase = "installation";
|
|
phaseBudget = TimeSpan.FromMinutes(80);
|
|
phaseStarted.Restart();
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Native Recovery readiness passed. This run has not installed macOS, .NET, CLT, or run Meeting Assistant tests.");
|
|
outcome = "readiness-passed";
|
|
break;
|
|
}
|
|
}
|
|
if (full && permitted)
|
|
{
|
|
var phasePath = Path.Combine(output, "guest-phase.json");
|
|
if (File.Exists(phasePath))
|
|
{
|
|
using var nativePhase = JsonDocument.Parse(File.ReadAllText(phasePath));
|
|
if (nativePhase.RootElement.GetProperty("token").GetString() != token) throw new InvalidOperationException("Stale native phase receipt.");
|
|
var current = nativePhase.RootElement.GetProperty("phase").GetString();
|
|
var next = current == "toolchain-installing" ? "toolchain" : current is "tests-running" or "tests-passed" ? "tests" : phase;
|
|
if (next != phase) { phase = next; phaseBudget = TimeSpan.FromMinutes(next == "toolchain" ? 30 : 25); phaseStarted.Restart(); Console.WriteLine("[native-diagnostic] phase: " + phase); }
|
|
if (current is "tests-failed" or "bootstrap-failed" or "installation-failed") throw new InvalidOperationException("Guest phase failed: " + current);
|
|
}
|
|
var fullResult = Path.Combine(output, "full-result.json");
|
|
if (File.Exists(fullResult))
|
|
{
|
|
ValidateFullResult(File.ReadAllText(fullResult), token, sourceCommit, File.ReadAllText(Path.Combine(output, "archive.sha256")).Trim());
|
|
ValidateTrx(File.ReadAllBytes(Path.Combine(output, "native.trx")), File.ReadAllText(fullResult));
|
|
outcome = "native-tests-passed";
|
|
Console.WriteLine("Native macOS 577/577 tests passed, including all five native tests, with fresh Mach-O/x86_64 and codesign evidence.");
|
|
break;
|
|
}
|
|
}
|
|
var running = await Command("docker", ["inspect", "--format", "{{.State.Running}}", id], output, "container-running", deadline.Token);
|
|
if (running.Output.Trim() != "true") throw new InvalidOperationException("Guest container exited before a native readiness result.");
|
|
if (heartbeat.Elapsed >= TimeSpan.FromSeconds(60))
|
|
{
|
|
Console.WriteLine($"[native-diagnostic] phase={phase}; elapsed={phaseStarted.Elapsed.TotalMinutes:F1}/{phaseBudget.TotalMinutes:F0} minutes; container=running; readiness={(permitted ? "passed" : "pending")}");
|
|
heartbeat.Restart();
|
|
}
|
|
await Task.Delay(TimeSpan.FromSeconds(20), deadline.Token);
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
error = exception is OperationCanceledException ? $"The explicit {deadlineMinutes}-minute diagnostic deadline or cancellation was reached." : exception.Message;
|
|
Console.Error.WriteLine(error);
|
|
}
|
|
finally
|
|
{
|
|
Console.CancelKeyPress -= cancelHandler;
|
|
using var captureDeadline = new CancellationTokenSource(TimeSpan.FromSeconds(45));
|
|
try { await CaptureGuest(state.ContainerId ?? state.ContainerName, output, captureDeadline.Token, full, true, state.Token); } catch (Exception exception) { Console.Error.WriteLine("Final evidence capture: " + exception.Message); }
|
|
try { PrintGuestProof(output, state.Token); } catch (Exception exception) { Console.Error.WriteLine("Native proof output: " + exception.Message); }
|
|
if (full) try { PrintFullProof(output, state.Token); } catch (Exception exception) { Console.Error.WriteLine("Full native proof output: " + exception.Message); }
|
|
var clean = await Cleanup(output);
|
|
if (!clean) { outcome = "failed"; error = (error ?? "") + " Owned-resource cleanup failed; inspect cleanup evidence."; }
|
|
Save(Path.Combine(output, "outcome.json"), new { token, outcome, error, completedUtc = DateTimeOffset.UtcNow });
|
|
}
|
|
return outcome is "readiness-passed" or "native-tests-passed" ? 0 : 1;
|
|
}
|
|
|
|
static string? Option(string[] args, string name)
|
|
{
|
|
var index = Array.IndexOf(args, name);
|
|
return index < 0 ? null : index + 1 < args.Length ? args[index + 1] : throw new ArgumentException("Missing value for " + name);
|
|
}
|
|
|
|
static void ValidateContracts()
|
|
{
|
|
XDocument.Parse(DiagnosticDaemon);
|
|
if (Encoding.UTF8.GetByteCount(DiagnosticDaemon) > Encoding.UTF8.GetByteCount(OriginalDaemon + "\n")) throw new InvalidOperationException("Daemon replacement exceeds original file.");
|
|
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("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.");
|
|
}
|
|
}
|
|
|
|
static async Task PrepareSource(string source, string output, string token, bool writeSource, CancellationToken cancellation, bool full = false)
|
|
{
|
|
Directory.CreateDirectory(output);
|
|
var patchPath = Path.Combine(source, "src/install/recovery/patch.py");
|
|
var originalPatch = File.ReadAllText(patchPath);
|
|
if (Hash(Encoding.UTF8.GetBytes(originalPatch)) != "84f13db88c02edbf5ce21a39571fe58f12bebf5b0886c2d012f16ddbaed45323") throw new InvalidOperationException("Pinned Recovery patcher hash mismatch.");
|
|
var patch = ReplaceOnce(originalPatch, OriginalBootstrap, MountOnlyBootstrap);
|
|
var oldConstants = "RECOVERY_ORIGINAL = b\"/usr/libexec/recoveryosd\"\nRECOVERY_REPLACEMENT = b\"/private/etc/rc.cdrom.sh\"";
|
|
var daemon = OriginalDaemon + "\n";
|
|
var constants = "RECOVERY_ORIGINAL = b'''" + daemon + "'''\nRECOVERY_REPLACEMENT = b'''" + DiagnosticDaemon + "'''.ljust(len(RECOVERY_ORIGINAL), b\" \")";
|
|
patch = ReplaceOnce(patch, oldConstants, constants);
|
|
var dockerPath = Path.Combine(source, "Dockerfile");
|
|
// The actual remote BuildKit cannot checksum a dangling symlink during root COPY.
|
|
// Start directly from the same immutable image filesystem; its inspected Config is empty.
|
|
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);
|
|
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");
|
|
var hookPath = Path.Combine("tools", "ci", "macos-native-readiness.sh");
|
|
var hook = ReplaceOnce(File.ReadAllText(hookPath), "@@PROOF_TOKEN@@", token);
|
|
if (full)
|
|
{
|
|
await PrepareFullSource(source, output, token, writeSource, cancellation);
|
|
hook = CreateFullReadiness(hook);
|
|
dockerfile += "\n# Payload stays inside this image and its owned anonymous storage volume.\nCOPY ci-payload/ /assets/ci-payload/\n";
|
|
entry = ReplaceOnce(entry, "free_kib >= 8 * 1024 * 1024", "free_kib >= 32 * 1024 * 1024").Replace("8-GiB diagnostic budget", "32-GiB full-run budget", StringComparison.Ordinal);
|
|
}
|
|
foreach (var pair in new[] { ("recovery-patch.py", patch), ("Dockerfile.patched", dockerfile), ("container-entry.sh", entry), ("guest-launch.sh", hook), ("recoveryosd-original.plist", daemon), ("recoveryosd-diagnostic.plist", DiagnosticDaemon), ("early-bootstrap.sh", MountOnlyBootstrap) })
|
|
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 "Dockerfile.patched" or "container-entry.sh" or "guest-launch.sh" or "recoveryosd-original.plist" or "recoveryosd-diagnostic.plist" or "early-bootstrap.sh").ToDictionary(path => Path.GetFileName(path)!, path => Hash(File.ReadAllBytes(path))));
|
|
await Command("bash", ["-n", Path.Combine(output, "guest-launch.sh")], output, "guest-hook-syntax", cancellation);
|
|
await Command("bash", ["-n", Path.Combine(output, "container-entry.sh")], output, "entry-syntax", cancellation);
|
|
if (!writeSource) return;
|
|
File.WriteAllText(patchPath, patch, new UTF8Encoding(false));
|
|
File.WriteAllText(dockerPath, dockerfile, new UTF8Encoding(false));
|
|
File.WriteAllText(entryPath, entry, new UTF8Encoding(false));
|
|
File.WriteAllText(Path.Combine(source, "src/install/recovery/launch.sh"), hook, new UTF8Encoding(false));
|
|
}
|
|
|
|
static string ReplaceOnce(string text, string oldValue, string newValue) => ReplaceAllExact(text, oldValue, newValue, 1);
|
|
|
|
static string DiskSerial(string token) => token[..20];
|
|
|
|
static async Task PreparePayload(string source, string output, string token, string commit, CancellationToken cancellation)
|
|
{
|
|
if (!System.Text.RegularExpressions.Regex.IsMatch(commit, "^[0-9a-f]{40}$")) throw new InvalidOperationException("Candidate commit is not an exact Git SHA.");
|
|
var status = await Command("git", ["status", "--porcelain", "--untracked-files=all"], output, "source-cleanliness", cancellation);
|
|
if (!string.IsNullOrEmpty(status.Output)) throw new InvalidOperationException("Full CI requires a clean exact HEAD; commit the reviewable candidate before running it.");
|
|
var payload = Path.Combine(source, "ci-payload");
|
|
Directory.CreateDirectory(payload);
|
|
var archive = Path.Combine(payload, "source.tar");
|
|
await Command("git", ["archive", "--format=tar", "--output", archive, commit], output, "source-archive", cancellation);
|
|
var archiveHash = Hash(File.ReadAllBytes(archive));
|
|
File.WriteAllText(Path.Combine(output, "archive.sha256"), archiveHash + "\n");
|
|
File.WriteAllText(Path.Combine(payload, "source.commit"), commit + "\n");
|
|
Save(Path.Combine(payload, "payload.json"), new { runToken = token, sourceCommit = commit, archiveSha256 = archiveHash, sdkVersion = SdkVersion, sdkSha512 = SdkSha512, expectedTests = 577 });
|
|
File.Copy(Path.Combine(payload, "payload.json"), Path.Combine(output, "payload.json"));
|
|
foreach (var pair in new[] { ("MacOsNativeGuest.cs", "MacOsNativeGuest.cs"), ("macos-native-firstboot.sh", "native-firstboot-bootstrap.sh"), ("macos-native-disk-guard.sh", "macos-native-disk-guard.sh") })
|
|
File.Copy(Path.Combine("tools", "ci", pair.Item1), Path.Combine(payload, pair.Item2));
|
|
Console.WriteLine("[native-diagnostic] pinned-sdk-download");
|
|
using var downloadDeadline = CancellationTokenSource.CreateLinkedTokenSource(cancellation);
|
|
downloadDeadline.CancelAfter(TimeSpan.FromMinutes(15));
|
|
using var client = new HttpClient { Timeout = Timeout.InfiniteTimeSpan };
|
|
using var response = await client.GetAsync($"https://builds.dotnet.microsoft.com/dotnet/Sdk/{SdkVersion}/dotnet-sdk-{SdkVersion}-osx-x64.tar.gz", HttpCompletionOption.ResponseHeadersRead, downloadDeadline.Token);
|
|
response.EnsureSuccessStatusCode();
|
|
var sdkPath = Path.Combine(payload, "sdk.tar.gz");
|
|
await using (var sdk = File.Create(sdkPath))
|
|
await using (var stream = await response.Content.ReadAsStreamAsync(downloadDeadline.Token))
|
|
await stream.CopyToAsync(sdk, downloadDeadline.Token);
|
|
await using (var sdk = File.OpenRead(sdkPath))
|
|
if (Convert.ToHexStringLower(await SHA512.HashDataAsync(sdk, downloadDeadline.Token)) != SdkSha512) throw new InvalidOperationException("Official macOS/x64 SDK SHA-512 mismatch.");
|
|
Save(Path.Combine(output, "payload-hashes.json"), Directory.GetFiles(payload).ToDictionary(path => Path.GetFileName(path)!, path => Hash(File.ReadAllBytes(path))));
|
|
}
|
|
|
|
static string ReadPinned(string source, string path, string expected)
|
|
{
|
|
var bytes = File.ReadAllBytes(Path.Combine(source, path));
|
|
if (Hash(bytes) != expected) throw new InvalidOperationException("Pinned full-install source hash mismatch: " + path);
|
|
return Encoding.UTF8.GetString(bytes);
|
|
}
|
|
|
|
static string CreateFullReadiness(string hook) => ReplaceOnce(hook,
|
|
" # Keep the service alive for the bounded host diagnostic to capture evidence.\n while :; do sleep 60; done",
|
|
"""
|
|
# Full mode waits for the host's independently validated fresh owned-disk permit.
|
|
if [ "$success" = true ]; then
|
|
permit_start=$SECONDS
|
|
while (( SECONDS - permit_start < 300 )); do
|
|
if [ -s "$STATE_DIR/install.permit" ]; then
|
|
exec /bin/bash "$STATE_DIR/full-install.sh"
|
|
fi
|
|
sleep 1
|
|
done
|
|
printf '[full-install] host permit was not received in five minutes\n' >> "$PROOF_LOG"
|
|
fi
|
|
while :; do sleep 60; done
|
|
""");
|
|
|
|
static async Task PrepareFullSource(string source, string output, string token, bool writeSource, CancellationToken cancellation)
|
|
{
|
|
var installer = ReadPinned(source, "src/install/recovery/launch.sh", "b44309d1056bbd0321251cc9f04a52cf6ad68209586f263b9619339bf7146b6c");
|
|
var selectorStart = installer.IndexOf("select_target_disk() {", StringComparison.Ordinal);
|
|
var selectorEnd = installer.IndexOf("find_startosinstall() {", StringComparison.Ordinal);
|
|
if (selectorStart < 0 || selectorEnd <= selectorStart) throw new InvalidOperationException("Pinned installer selector boundaries changed.");
|
|
var selector = """
|
|
select_target_disk() {
|
|
local permit_token permit_disk permit_commit extra
|
|
{ IFS= read -r permit_token; IFS= read -r permit_disk; IFS= read -r permit_commit; IFS= read -r extra || :; } < "$STATE_DIR/install.permit" || return 1
|
|
[ "$permit_token" = "$PROOF_TOKEN" ] && [ -z "${extra:-}" ] || return 1
|
|
[ "$permit_commit" = "$(cat "$STATE_DIR/source.commit")" ] || return 1
|
|
[[ "$permit_commit" =~ ^[0-9a-f]{40}$ ]] || return 1
|
|
. "$STATE_DIR/macos-native-disk-guard.sh"
|
|
verify_owned_disk "$permit_disk" "$STATE_DIR" "$PROOF_TOKEN" >&2 || return 1
|
|
printf '%s\n' "$permit_disk"
|
|
}
|
|
|
|
""" + "\n";
|
|
installer = ReplaceOnce(installer, installer[selectorStart..selectorEnd], selector);
|
|
installer = ReplaceOnce(installer, "MIN_TARGET_SIZE=$((16 * 1024 * 1024 * 1024))", "# Target policy is exclusively the own writable 64-GiB emulated disk.");
|
|
installer = ReplaceOnce(installer, "no writable installation disk of at least 16 GiB was found", "the run-owned writable 64-GiB installation disk was not proved");
|
|
installer = ReplaceOnce(installer, " local message=\"$1\"\n\n echo \"[log] ERROR: $message\"", " local message=\"$1\"\n\n mark_installation_failed || :\n echo \"[log] ERROR: $message\"");
|
|
installer = ReplaceOnce(installer, "if (( rc != 0 )); then\n", "if (( rc != 0 )); then\n mark_installation_failed || :\n");
|
|
installer = ReplaceAllExact(installer, "rm -f \"$STARTED\"", ": # Keep the owned erase guard on failure; never erase again.", 2);
|
|
installer = ReplaceOnce(installer, ": > \"$STARTED\" || fail \"failed to create installation guard\"", "( set -o noclobber; printf '%s:%s:%s\\n' \"$PROOF_TOKEN\" \"$(cat \"$STATE_DIR/source.commit\")\" \"$TARGET_DISK\" > \"$STARTED\" ) || fail \"failed to create the exclusive owned installation guard\"");
|
|
installer = ReplaceOnce(installer, "set -u\n", "set -u\nPROOF_TOKEN=\"" + token + "\"\n" + """
|
|
mark_installation_failed() {
|
|
local state="${STATE_DIR:-/Volumes/installstate}" temporary
|
|
[ "$(cat "$state/run.owner" 2>/dev/null)" = "$PROOF_TOKEN" ] || return 1
|
|
temporary="$state/guest-phase.install.$$.tmp"
|
|
printf '{"token":"%s","phase":"installation-failed"}\n' "$PROOF_TOKEN" > "$temporary" &&
|
|
/bin/mv -f "$temporary" "$state/guest-phase.json"
|
|
}
|
|
installer_parent=$$
|
|
# Installer watchdog: 80 minutes, also bounded by the host's 172-minute total.
|
|
(
|
|
trap 'kill "$sleeper" 2>/dev/null || :; exit 0' TERM INT
|
|
sleep 4800 & sleeper=$!; wait "$sleeper"; kill -TERM "$installer_parent" 2>/dev/null || :
|
|
) & install_watchdog=$!
|
|
trap 'kill -TERM "$install_watchdog" 2>/dev/null || :; wait "$install_watchdog" 2>/dev/null || :' EXIT
|
|
trap 'kill "${STARTOSINSTALL_PID:-}" "${BOOTSTRAPPER_PID:-}" 2>/dev/null || :; mark_installation_failed || :; exit 1' TERM INT
|
|
""" + "\n");
|
|
var firstboot = ReadPinned(source, "src/install/firstboot/launch.sh", "d6b29bb42ffe99edda6b3be3faf6009c4e0b34e5b8bba6eb0855cf24a0c24307");
|
|
firstboot = ReplaceOnce(firstboot, "log \"prebuilt account package installed successfully\"\n", "log \"prebuilt account package installed successfully\"\n" + """
|
|
count=0
|
|
while [ ! -d /Volumes/installstate ] && (( count < 120 )); do
|
|
/sbin/mount_9p installstate >/dev/null 2>&1 || :
|
|
count=$((count + 1)); sleep 1
|
|
done
|
|
[ -d /Volumes/installstate ] || fail "native CI state share did not mount"
|
|
""" + "\n/bin/bash /Volumes/installstate/native-firstboot-bootstrap.sh \"" + token + "\" || fail \"native CI bootstrap or tests failed\"\n");
|
|
ReadPinned(source, "src/install/firstboot/com.dockur.macos.firstboot.plist", "29ef05388d962c236bdb87b2911e3b42e08c600035cfccf440d35ba64011c3d3");
|
|
ReadPinned(source, "src/image.sh", "c08bf9436fb8b72ea82fdf0e677641ab2fc42a0a59e2cf0309c00df519884c5c");
|
|
var initialize = ReadPinned(source, "src/install.sh", "19b4b27187de85148ad1de1c40d75a54738eb9890f02dbb9445155bb5ec44f90");
|
|
initialize = ReplaceOnce(initialize, "INSTALL_STATE_DIR=\"$QEMU_DIR/installstate\"\nrm -rf \"$INSTALL_STATE_DIR\"", "INSTALL_STATE_DIR=\"$STORAGE/ci-state\"\n# Full CI preserves the own-volume erase guard and evidence across starts.");
|
|
initialize = ReplaceOnce(initialize, " if ! prepareInstallationState \"$INSTALL_STATE_DIR\"; then\n exit 34\n fi", """
|
|
if [ -e "$INSTALL_STATE_DIR/run.owner" ]; then
|
|
[ "$(cat "$INSTALL_STATE_DIR/run.owner")" = "@@OWNER@@" ] || { error "CI storage belongs to another run."; exit 34; }
|
|
else
|
|
prepareInstallationState "$INSTALL_STATE_DIR" || exit 34
|
|
cp -f /assets/ci-payload/* "$INSTALL_STATE_DIR/" || exit 34
|
|
cp -f "$IMAGE_TOOLS/recovery/full-install.sh" "$INSTALL_STATE_DIR/full-install.sh" || exit 34
|
|
cmp -s "$IMAGE_TOOLS/recovery/full-install.sh" "$INSTALL_STATE_DIR/full-install.sh" || exit 34
|
|
for file in /assets/ci-payload/*; do cmp -s "$file" "$INSTALL_STATE_DIR/${file##*/}" || exit 34; done
|
|
chmod 0755 "$INSTALL_STATE_DIR/full-install.sh" "$INSTALL_STATE_DIR/native-firstboot-bootstrap.sh" || exit 34
|
|
printf '%s\n' '@@OWNER@@' > "$INSTALL_STATE_DIR/run.owner" || exit 34
|
|
fi
|
|
""".Replace("@@OWNER@@", token, StringComparison.Ordinal));
|
|
foreach (var pair in new[] { ("full-install.sh", installer), ("full-firstboot.sh", firstboot), ("full-state-source.sh", initialize) })
|
|
{
|
|
File.WriteAllText(Path.Combine(output, pair.Item1), pair.Item2, new UTF8Encoding(false));
|
|
await Command("bash", ["-n", Path.Combine(output, pair.Item1)], output, pair.Item1 + "-syntax", cancellation);
|
|
}
|
|
foreach (var name in new[] { "macos-native-firstboot.sh", "macos-native-disk-guard.sh" })
|
|
await Command("bash", ["-n", Path.Combine("tools", "ci", name)], output, name + "-syntax", cancellation);
|
|
Save(Path.Combine(output, "full-source-hashes.json"), new Dictionary<string, string> { ["full-install.sh"] = Hash(Encoding.UTF8.GetBytes(installer)), ["full-firstboot.sh"] = Hash(Encoding.UTF8.GetBytes(firstboot)), ["full-state-source.sh"] = Hash(Encoding.UTF8.GetBytes(initialize)), ["MacOsNativeGuest.cs"] = Hash(File.ReadAllBytes("tools/ci/MacOsNativeGuest.cs")), ["macos-native-firstboot.sh"] = Hash(File.ReadAllBytes("tools/ci/macos-native-firstboot.sh")), ["macos-native-disk-guard.sh"] = Hash(File.ReadAllBytes("tools/ci/macos-native-disk-guard.sh")) });
|
|
if (!writeSource)
|
|
{
|
|
await ValidateInstallerFailureReceipt(installer, output, token, cancellation);
|
|
return;
|
|
}
|
|
File.WriteAllText(Path.Combine(source, "src/install/recovery/full-install.sh"), installer, new UTF8Encoding(false));
|
|
File.WriteAllText(Path.Combine(source, "src/install/firstboot/launch.sh"), firstboot, new UTF8Encoding(false));
|
|
File.WriteAllText(Path.Combine(source, "src/install.sh"), initialize, new UTF8Encoding(false));
|
|
}
|
|
|
|
static async Task ValidateInstallerFailureReceipt(string installer, string output, string token, CancellationToken cancellation)
|
|
{
|
|
const string start = "mark_installation_failed() {";
|
|
const string end = "\n}\ninstaller_parent=$$";
|
|
var begin = installer.IndexOf(start, StringComparison.Ordinal);
|
|
var finish = begin < 0 ? -1 : installer.IndexOf(end, begin, StringComparison.Ordinal);
|
|
if (begin < 0 || finish < begin || installer.Split("mark_installation_failed || :", StringSplitOptions.None).Length != 4)
|
|
throw new InvalidOperationException("Pinned installer must publish its terminal failure phase from fail(), nonzero startosinstall and TERM/INT.");
|
|
var function = installer[begin..(finish + 2)];
|
|
var state = Path.Combine(output, "installer-failure-fixture");
|
|
Directory.CreateDirectory(state);
|
|
File.WriteAllText(Path.Combine(state, "run.owner"), token);
|
|
var command = "set -u\n" + function + "\nPROOF_TOKEN=\"$1\"; STATE_DIR=\"$2\"; mark_installation_failed";
|
|
await Command("bash", ["-c", command, "installer-failure-validation", token, state], output, "installer-terminal-failure", cancellation);
|
|
var receipt = File.ReadAllText(Path.Combine(state, "guest-phase.json"));
|
|
using var json = JsonDocument.Parse(receipt);
|
|
if (json.RootElement.GetProperty("token").GetString() != token || json.RootElement.GetProperty("phase").GetString() != "installation-failed" || Directory.GetFiles(state, "*.tmp").Length != 0)
|
|
throw new InvalidOperationException("Installer failed to publish a complete atomic terminal phase.");
|
|
File.WriteAllText(Path.Combine(state, "run.owner"), "foreign");
|
|
var foreign = await Command("bash", ["-c", command, "installer-failure-validation", token, state], output, "installer-foreign-failure", cancellation, requireSuccess: false);
|
|
if (foreign.ExitCode == 0 || File.ReadAllText(Path.Combine(state, "guest-phase.json")) != receipt)
|
|
throw new InvalidOperationException("Installer terminal phase overwrote foreign run-owned state.");
|
|
}
|
|
|
|
static async Task PermitInstallation(string id, string output, string token, string commit, string readiness, CancellationToken cancellation)
|
|
{
|
|
await Command("docker", ["inspect", id], output, "full-container-boundary", cancellation);
|
|
AssertContainer(File.ReadAllText(Path.Combine(output, "full-container-boundary.stdout.log")), token);
|
|
using (var document = JsonDocument.Parse(File.ReadAllText(Path.Combine(output, "full-container-boundary.stdout.log"))))
|
|
{
|
|
var mounts = document.RootElement[0].GetProperty("Mounts").EnumerateArray().ToArray();
|
|
if (mounts.Length != 1 || mounts[0].GetProperty("Type").GetString() != "volume" || mounts[0].GetProperty("Destination").GetString() != "/storage" || !mounts[0].GetProperty("RW").GetBoolean()) throw new InvalidOperationException("Full installer requires only the newly owned anonymous /storage volume.");
|
|
}
|
|
var drive = await Command("docker", ["exec", id, "qemu-img", "info", "--force-share", "--output=json", "/storage/14/data.img"], output, "owned-raw-disk", cancellation);
|
|
using (var document = JsonDocument.Parse(drive.Output))
|
|
if (document.RootElement.GetProperty("format").GetString() != "raw" || document.RootElement.GetProperty("virtual-size").GetInt64() != GuestDiskBytes) throw new InvalidOperationException("Owned VM raw-disk capacity/format mismatch.");
|
|
var attachment = await Command("docker", ["exec", id, "sh", "-c", "qemu_pid=$(cat /dev/shm/qemu.pid); tr '\\0' '\\n' < /proc/\"$qemu_pid\"/cmdline | sed -n '/^file=\\/storage\\/14\\/data\\.img,/p; /^ide-hd,drive=data3,/p; /^local,id=installstatefs,/p'"], output, "owned-disk-attachment", cancellation);
|
|
var lines = attachment.Output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
|
if (lines.Length != 3 || !lines.Any(line => line.StartsWith("file=/storage/14/data.img,id=data3,format=raw,", StringComparison.Ordinal) && !line.Contains("readonly=on", StringComparison.Ordinal)) || !lines.Any(line => line.StartsWith("ide-hd,drive=data3,", StringComparison.Ordinal) && line.Contains("serial=" + DiskSerial(token), StringComparison.Ordinal)) || !lines.Contains("local,id=installstatefs,path=" + FullState + ",security_model=none")) throw new InvalidOperationException("QEMU did not attach the exact owned raw disk/serial and persistent state share.");
|
|
var owner = await Command("docker", ["exec", id, "cat", FullState + "/run.owner"], output, "full-share-owner", cancellation);
|
|
if (owner.Output.Trim() != token) throw new InvalidOperationException("Native state owner mismatch.");
|
|
using var receipt = JsonDocument.Parse(readiness);
|
|
var disk = receipt.RootElement.GetProperty("disk").GetString();
|
|
var permit = Path.Combine(output, "install.permit");
|
|
File.WriteAllText(permit, token + "\n" + disk + "\n" + commit + "\n");
|
|
await Command("docker", ["cp", permit, id + ":" + FullState + "/install.permit.tmp"], output, "stage-owned-install-permit", cancellation);
|
|
await Command("docker", ["exec", id, "mv", FullState + "/install.permit.tmp", FullState + "/install.permit"], output, "authorize-owned-guest-installation", cancellation);
|
|
}
|
|
|
|
static readonly string[] NativeFacts = [
|
|
"MeetingAssistant.Tests.MacOsMeetingAudioSourceTests.NativeAudioCaptureIsPackagedAsSignedMacOsAppForPersistentPrivacyGrant",
|
|
"MeetingAssistant.Tests.MacOsMeetingIntegrationTests.MacOsCapabilityEndpointReportsEnabledRealProviders",
|
|
"MeetingAssistant.Tests.MacOsMeetingIntegrationTests.NativeHelperAdvertisesCalendarPromptAndScreenshotFeatures",
|
|
"MeetingAssistant.Tests.MacOsMeetingIntegrationTests.NativeHelperCropsPngUsingOcrPixelCoordinates",
|
|
"MeetingAssistant.Tests.MacOsMeetingIntegrationTests.CalendarClientFallsBackToCalendarAutomationWhenEventKitIsDenied"];
|
|
static readonly string[] NativeArtifacts = ["MeetingAssistantAudioCapture.app/Contents/MacOS/macos-meeting-audio-capture", "macos-desktop-controls", "macos-meeting-integrations", "macos-meeting-assistant-launcher"];
|
|
|
|
static void ValidateFullResult(string json, string token, string commit, string archiveHash)
|
|
{
|
|
using var document = JsonDocument.Parse(json);
|
|
var result = document.RootElement;
|
|
if (result.GetProperty("token").GetString() != token || !result.GetProperty("success").GetBoolean() || result.GetProperty("sourceCommit").GetString() != commit || result.GetProperty("archiveSha256").GetString() != archiveHash || result.GetProperty("sdkVersion").GetString() != SdkVersion || !Version.TryParse(result.GetProperty("osVersion").GetString(), out var version) || version.Major < 14 || result.GetProperty("architecture").GetString() != "x86_64" || new[] { "expectedTests", "total", "executed", "passed" }.Any(key => result.GetProperty(key).GetInt32() != 577) || new[] { "failed", "notExecuted", "audioCodeSignExit" }.Any(key => result.GetProperty(key).GetInt32() != 0) || !result.GetProperty("nativeTests").EnumerateArray().Select(value => value.GetString()).Order().SequenceEqual(NativeFacts.Order())) throw new InvalidOperationException("Native full receipt did not prove exact-source 577/577 with all five native tests and zero skips.");
|
|
var artifacts = result.GetProperty("nativeArtifacts").EnumerateArray().ToArray();
|
|
if (artifacts.Length != 4 || !artifacts.Select(item => item.GetProperty("name").GetString()).Order().SequenceEqual(NativeArtifacts.Order()) || artifacts.Any(item => item.GetProperty("architecture").GetString() != "x86_64" || !System.Text.RegularExpressions.Regex.IsMatch(item.GetProperty("sha256").GetString() ?? "", "^[0-9a-f]{64}$"))) throw new InvalidOperationException("Native Mach-O/x86_64 helper manifest is incomplete.");
|
|
}
|
|
|
|
static void ValidateTrx(byte[] bytes, string json)
|
|
{
|
|
using var receipt = JsonDocument.Parse(json);
|
|
if (Hash(bytes) != receipt.RootElement.GetProperty("trxSha256").GetString()) throw new InvalidOperationException("Returned native TRX SHA-256 mismatch.");
|
|
var document = XDocument.Load(new MemoryStream(bytes));
|
|
var counters = document.Descendants().Single(item => item.Name.LocalName == "Counters");
|
|
foreach (var key in new[] { "total", "executed", "passed" }) if ((int?)counters.Attribute(key) != 577) throw new InvalidOperationException("Native TRX count mismatch: " + key);
|
|
foreach (var key in new[] { "failed", "notExecuted" }) if ((int?)counters.Attribute(key) != 0) throw new InvalidOperationException("Native TRX failure/skip counter: " + key);
|
|
var results = document.Descendants().Where(item => item.Name.LocalName == "UnitTestResult").ToArray();
|
|
if (results.Length != 577 || results.Any(item => (string?)item.Attribute("outcome") != "Passed")) throw new InvalidOperationException("Native TRX contains missing, failed or skipped results.");
|
|
var identities = document.Descendants().Where(item => item.Name.LocalName == "UnitTest").ToDictionary(item => (string)item.Attribute("id")!, item => { var method = item.Elements().Single(child => child.Name.LocalName == "TestMethod"); var className = ((string?)method.Attribute("className"))?.Split(',')[0].Trim() ?? ""; var name = (string?)method.Attribute("name") ?? ""; return name.StartsWith(className + ".", StringComparison.Ordinal) ? name : className + "." + name; });
|
|
var passed = results.Select(item => identities[(string)item.Attribute("testId")!]).ToHashSet();
|
|
if (NativeFacts.Any(name => !passed.Contains(name))) throw new InvalidOperationException("Native TRX does not explicitly pass every required macOS fact.");
|
|
}
|
|
|
|
static void ValidateFullContracts()
|
|
{
|
|
var token = new string('a', 32); var commit = new string('b', 40); var hash = new string('c', 64);
|
|
var trx = "<TestRun><TestDefinitions>" + string.Concat(Enumerable.Range(0, 577).Select(index => { var identity = index < 5 ? NativeFacts[index] : "MeetingAssistant.Tests.Validation.Test" + index; var split = identity.LastIndexOf('.'); return $"<UnitTest id='t{index}'><TestMethod className='{identity[..split]}' name='{identity[(split + 1)..]}' /></UnitTest>"; })) + "</TestDefinitions><Results>" + string.Concat(Enumerable.Range(0, 577).Select(index => $"<UnitTestResult testId='t{index}' outcome='Passed' />")) + "</Results><ResultSummary><Counters total='577' executed='577' passed='577' failed='0' notExecuted='0' /></ResultSummary></TestRun>";
|
|
var good = JsonSerializer.Serialize(new { token, success = true, sourceCommit = commit, archiveSha256 = hash, sdkVersion = SdkVersion, osVersion = "14.6.1", architecture = "x86_64", expectedTests = 577, total = 577, executed = 577, passed = 577, failed = 0, notExecuted = 0, nativeTests = NativeFacts, nativeArtifacts = NativeArtifacts.Select(name => new { name, sha256 = hash, architecture = "x86_64" }), audioCodeSignExit = 0, trxSha256 = Hash(Encoding.UTF8.GetBytes(trx)) });
|
|
ValidateFullResult(good, token, commit, hash); ValidateTrx(Encoding.UTF8.GetBytes(trx), good);
|
|
byte[] bomTrx = [0xef, 0xbb, 0xbf, .. Encoding.UTF8.GetBytes(trx)];
|
|
ValidateTrx(bomTrx, good.Replace(Hash(Encoding.UTF8.GetBytes(trx)), Hash(bomTrx), StringComparison.Ordinal));
|
|
var qualifiedTrx = trx;
|
|
foreach (var name in NativeFacts) qualifiedTrx = qualifiedTrx.Replace("name='" + name[(name.LastIndexOf('.') + 1)..] + "'", "name='" + name + "'", StringComparison.Ordinal);
|
|
ValidateTrx(Encoding.UTF8.GetBytes(qualifiedTrx), good.Replace(Hash(Encoding.UTF8.GetBytes(trx)), Hash(Encoding.UTF8.GetBytes(qualifiedTrx)), StringComparison.Ordinal));
|
|
foreach (var invalid in new[] { good.Replace("\"success\":true", "\"success\":false"), good.Replace("\"passed\":577", "\"passed\":572"), good.Replace("\"notExecuted\":0", "\"notExecuted\":5"), good.Replace("\"audioCodeSignExit\":0", "\"audioCodeSignExit\":1"), good.Replace("x86_64", "arm64"), good.Replace(token, new string('d', 32)), good.Replace(commit, new string('e', 40)), good.Replace("NativeHelperAdvertisesCalendarPromptAndScreenshotFeatures", "UnrelatedTest") })
|
|
{
|
|
try { ValidateFullResult(invalid, token, commit, hash); } catch (InvalidOperationException) { continue; }
|
|
throw new InvalidOperationException("Full native validator accepted an incomplete or stale proof.");
|
|
}
|
|
try { ValidateTrx(Encoding.UTF8.GetBytes(trx.Replace("outcome='Passed'", "outcome='NotExecuted'")), good); } catch (InvalidOperationException) { return; }
|
|
throw new InvalidOperationException("Full native validator accepted changed TRX bytes.");
|
|
}
|
|
|
|
static async Task ValidateCompression(string chunk, string output)
|
|
{
|
|
Directory.CreateDirectory(output);
|
|
var bytes = File.ReadAllBytes(chunk);
|
|
if (Hash(bytes) != "2770f06fe51f19ddc040cfad4ab870d7227f540d1ba4190a9ce631145c12fae0") throw new InvalidOperationException("Readonly retained Recovery qualification chunk hash mismatch.");
|
|
var text = Encoding.Latin1.GetString(bytes);
|
|
if (text.Split(DiagnosticDaemon, StringSplitOptions.None).Length != 2 || text.Split(MountOnlyBootstrap, StringSplitOptions.None).Length != 2) throw new InvalidOperationException("Qualification chunk does not contain this exact Recovery LaunchDaemon and mount-only patch.");
|
|
// Upstream UDIF recompression uses Python zlib; .NET's compressor differs even on baseline.
|
|
await Command("python3", ["-c", "import json,pathlib,sys,zlib; b=pathlib.Path(sys.argv[1]).read_bytes(); n=len(zlib.compress(b,9)); print(json.dumps({'runtime':zlib.ZLIB_RUNTIME_VERSION,'compressedBytes':n,'slotBytes':43266,'fits':n<=43266})); sys.exit(0 if n<=43266 else 1)", chunk], output, "readonly-recovery-compression", CancellationToken.None);
|
|
}
|
|
|
|
static async Task ValidateDiskSerialParser(string output)
|
|
{
|
|
Directory.CreateDirectory(output);
|
|
var guard = File.ReadAllText("tools/ci/macos-native-disk-guard.sh");
|
|
const string start = "-v disk=\"${disk#/dev/}\" '\n";
|
|
const string end = " ' \"$state/disk-ownership-ioreg.log\")";
|
|
var program = guard[(guard.IndexOf(start, StringComparison.Ordinal) + start.Length)..guard.IndexOf(end, StringComparison.Ordinal)];
|
|
var serial = new string('0', 20);
|
|
var own = "+-o QEMU HARDDISK <class IOAHCIBlockStorageDevice>\n | \"Device Characteristics\" = {\"Serial Number\"=\"" + serial + "\"}\n +-o Media <class IOMedia>\n \"BSD Name\" = \"disk1\"\n";
|
|
var reversed = "+-o QEMU HARDDISK <class IOAHCIBlockStorageDevice>\n +-o Media <class IOMedia>\n \"BSD Name\" = \"disk1\"\n | \"Device Characteristics\" = {\"Serial Number\"=\"" + serial + "\"}\n";
|
|
var splitRoots = "+-o QEMU HARDDISK <class IOAHCIBlockStorageDevice>\n | \"Device Characteristics\" = {\"Serial Number\"=\"" + serial + "\"}\n+-o Other <class IOAHCIBlockStorageDevice>\n +-o Media <class IOMedia>\n \"BSD Name\" = \"disk1\"\n";
|
|
foreach (var test in new[] { ("own", own, "1"), ("reversed-properties", reversed, "1"), ("split-roots", splitRoots, "0"), ("foreign-serial", own.Replace(serial, new string('a', 20)), "0"), ("foreign-disk", own.Replace("disk1", "disk2"), "0"), ("ambiguous", own + own, "2") })
|
|
{
|
|
var path = Path.Combine(output, "ioreg-" + test.Item1 + ".fixture");
|
|
File.WriteAllText(path, test.Item2);
|
|
var result = await Command("awk", ["-v", "expected=" + serial, "-v", "disk=disk1", program, path], output, "disk-serial-" + test.Item1, CancellationToken.None);
|
|
if (result.Output.Trim() != test.Item3) throw new InvalidOperationException("Actual boot-seam IORegistry parser fixture failed: " + test.Item1);
|
|
}
|
|
}
|
|
static string ReplaceAllExact(string text, string oldValue, string newValue, int expected)
|
|
{
|
|
var count = text.Split(oldValue, StringSplitOptions.None).Length - 1;
|
|
if (count != expected) throw new InvalidOperationException($"Pinned source contract expected {expected} match(es), found {count}: {oldValue.Split('\n')[0]}");
|
|
return text.Replace(oldValue, newValue, StringComparison.Ordinal);
|
|
}
|
|
|
|
static void ValidateResult(string json, string token)
|
|
{
|
|
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 < 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)
|
|
{
|
|
using var document = JsonDocument.Parse(json);
|
|
var container = document.RootElement[0];
|
|
var config = container.GetProperty("HostConfig");
|
|
if (container.GetProperty("Config").GetProperty("Labels").GetProperty(OwnerLabel).GetString() != token || config.GetProperty("Privileged").GetBoolean() || config.GetProperty("NetworkMode").GetString() != "default" && config.GetProperty("NetworkMode").GetString() != "bridge" || config.GetProperty("Memory").GetInt64() != ContainerMemoryBytes || new[] { "CapAdd", "Devices", "DeviceRequests", "Binds", "PortBindings" }.Any(key => config.TryGetProperty(key, out var value) && value.ValueKind != JsonValueKind.Null && (value.ValueKind == JsonValueKind.Array ? value.GetArrayLength() != 0 : value.EnumerateObject().Any())))
|
|
throw new InvalidOperationException("Created container exceeds the owned/unprivileged diagnostic boundary.");
|
|
}
|
|
|
|
static async Task CaptureGuest(string id, string output, CancellationToken cancellation, bool full = false, bool final = false, string? token = null)
|
|
{
|
|
if (final && token is not null) await CaptureMonitor(id, output, token, cancellation);
|
|
var logs = await Command("docker", ["logs", "--tail", "3000", id], output, "container", cancellation, requireSuccess: false);
|
|
var files = new List<(string, string)> { ("proof.log", "guest-proof.log"), ("result.json", "guest-result.json") };
|
|
if (full) files.AddRange([("guest-phase.json", "guest-phase.json"), ("full-result.json", "full-result.json"), ("firstboot.log", "firstboot.log"), ("install.log", "install.log"), ("apple.log", "apple.log"), ("disk-ownership-ioreg.log", "disk-ownership-ioreg.log"), ("installed-root.plist", "installed-root.plist"), ("apfs-containers.plist", "apfs-containers.plist"), ("physical-store.plist", "physical-store.plist"), ("clt-catalog.log", "clt-catalog.log"), ("clt-install.log", "clt-install.log")]);
|
|
foreach (var file in files)
|
|
{
|
|
var result = await Command("docker", ["exec", id, "cat", (full ? FullState : "/dev/shm/installstate") + "/" + file.Item1], output, "capture-" + file.Item1.Replace('/', '-'), cancellation, requireSuccess: false);
|
|
if (result.ExitCode == 0 && !string.IsNullOrWhiteSpace(result.Output)) File.WriteAllText(Path.Combine(output, file.Item2), result.Output);
|
|
}
|
|
if (full)
|
|
{
|
|
// Copy only this owned guest's diagnostic logs; no host paths or credentials.
|
|
var trx = await Command("docker", ["cp", id + ":" + FullState + "/test-results/native.trx", Path.Combine(output, "native.trx.tmp")], output, "capture-native-trx", cancellation, requireSuccess: false);
|
|
if (trx.ExitCode == 0) File.Move(Path.Combine(output, "native.trx.tmp"), Path.Combine(output, "native.trx"), overwrite: true);
|
|
if (final || File.Exists(Path.Combine(output, "full-result.json")))
|
|
{
|
|
Directory.CreateDirectory(Path.Combine(output, "guest-logs"));
|
|
await Command("docker", ["cp", id + ":" + FullState + "/guest-logs/.", Path.Combine(output, "guest-logs")], output, "capture-guest-logs", cancellation, requireSuccess: false);
|
|
}
|
|
}
|
|
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; 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);
|
|
}
|
|
|
|
static async Task CaptureMonitor(string id, string output, string token, CancellationToken cancellation)
|
|
{
|
|
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(cancellation);
|
|
deadline.CancelAfter(TimeSpan.FromSeconds(10));
|
|
int? monitorExit = null, copyExit = null;
|
|
string? error = null;
|
|
try
|
|
{
|
|
if (!System.Text.RegularExpressions.Regex.IsMatch(id, "^[0-9a-f]{64}$") || !System.Text.RegularExpressions.Regex.IsMatch(token, "^[0-9a-f]{32}$")) throw new InvalidOperationException("No saved owned container identity for the optional monitor capture.");
|
|
var inspection = await Command("docker", ["inspect", id], output, "capture-monitor-container", deadline.Token);
|
|
AssertContainer(inspection.Output, token);
|
|
using (var document = JsonDocument.Parse(inspection.Output))
|
|
if (document.RootElement[0].GetProperty("Id").GetString() != id || !document.RootElement[0].GetProperty("State").GetProperty("Running").GetBoolean()) throw new InvalidOperationException("Owned guest container is no longer running for the optional monitor capture.");
|
|
var screen = "/tmp/native-diagnostic-screen-" + token + ".ppm";
|
|
var monitor = await Command("docker", ["exec", id, "sh", "-c", """
|
|
test -S /run/shm/monitor.sock || exit 1
|
|
rm -f -- "$1" || exit 1
|
|
printf 'info status\nscreendump %s\n' "$1" | /usr/bin/timeout -s KILL 5 /usr/bin/nc.openbsd -q 1 -w 2 -U /run/shm/monitor.sock
|
|
monitor_exit=$?
|
|
printf '\n[monitor-exit] %s\n' "$monitor_exit"
|
|
[ "$monitor_exit" -eq 0 ] || exit "$monitor_exit"
|
|
bytes=$(stat -c%s "$1") || exit 1
|
|
[ "$bytes" -gt 0 ] && [ "$bytes" -le 8388608 ] || exit 1
|
|
printf '[screen-bytes] %s\n' "$bytes"
|
|
""", "native-monitor", screen], output, "capture-monitor", deadline.Token, requireSuccess: false);
|
|
monitorExit = monitor.ExitCode;
|
|
if (monitorExit != 0) throw new InvalidOperationException("Optional monitor status/screenshot command exited " + monitorExit + ".");
|
|
var copy = await Command("docker", ["cp", id + ":" + screen, Path.Combine(output, "guest-screen-" + token + ".ppm")], output, "capture-monitor-screen", deadline.Token, requireSuccess: false);
|
|
copyExit = copy.ExitCode;
|
|
if (copyExit != 0) throw new InvalidOperationException("Optional monitor screenshot copy exited " + copyExit + ".");
|
|
}
|
|
catch (Exception exception) { error = exception.Message; Console.Error.WriteLine("Optional final monitor capture: " + error); }
|
|
finally { Save(Path.Combine(output, "monitor-capture.json"), new { token, monitorExit, copyExit, success = error is null, error, capturedUtc = DateTimeOffset.UtcNow }); }
|
|
}
|
|
|
|
static async Task<bool> Cleanup(string output)
|
|
{
|
|
var path = Path.Combine(output, "owned-resources.json");
|
|
if (!File.Exists(path)) return true;
|
|
var state = JsonSerializer.Deserialize<OwnedResources>(File.ReadAllText(path), JsonOptions) ?? throw new InvalidOperationException("Invalid owned-resource receipt.");
|
|
if (!System.Text.RegularExpressions.Regex.IsMatch(state.Token, "^[0-9a-f]{32}$") || state.ContainerName != "meeting-assistant-native-" + state.Token || state.ImageTag != "meeting-assistant-native-diagnostic:" + state.Token) throw new InvalidOperationException("Invalid cleanup ownership identity.");
|
|
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(90));
|
|
try
|
|
{
|
|
foreach (var kind in new[] { "container", "image" })
|
|
{
|
|
var name = kind == "container" ? state.ContainerName : state.ImageTag;
|
|
var inspect = await Command("docker", [kind, "inspect", name], output, "cleanup-" + kind + "-inspect", deadline.Token, requireSuccess: false);
|
|
if (inspect.ExitCode != 0)
|
|
{
|
|
if (inspect.Error.Contains("No such object", StringComparison.Ordinal) || inspect.Error.Contains("No such container", StringComparison.Ordinal) || inspect.Error.Contains("No such image", StringComparison.Ordinal)) continue;
|
|
throw new InvalidOperationException("Cannot establish owned " + kind + " absence: " + inspect.Error);
|
|
}
|
|
using var document = JsonDocument.Parse(inspect.Output);
|
|
var resource = document.RootElement[0];
|
|
if (resource.GetProperty("Config").GetProperty("Labels").GetProperty(OwnerLabel).GetString() != state.Token) throw new InvalidOperationException("Cleanup refuses a resource without this run's exact ownership label.");
|
|
var id = resource.GetProperty("Id").GetString()!;
|
|
var expectedId = kind == "container" ? state.ContainerId : state.ImageId;
|
|
if (expectedId is not null && expectedId != id) throw new InvalidOperationException("Cleanup refuses a resource whose ID changed after creation.");
|
|
await Command("docker", kind == "container" ? ["rm", "--force", "--volumes", id] : ["image", "rm", id], output, "cleanup-" + kind + "-remove", deadline.Token);
|
|
}
|
|
if (Path.GetFileName(state.WorkDirectory) == "meeting-assistant-native-" + state.Token && File.Exists(Path.Combine(state.WorkDirectory, "run.owner")) && File.ReadAllText(Path.Combine(state.WorkDirectory, "run.owner")) == state.Token) Directory.Delete(state.WorkDirectory, true);
|
|
Save(Path.Combine(output, "cleanup.json"), new { state.Token, success = true, completedUtc = DateTimeOffset.UtcNow });
|
|
return true;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Save(Path.Combine(output, "cleanup.json"), new { state.Token, success = false, error = exception.Message, completedUtc = DateTimeOffset.UtcNow });
|
|
Console.Error.WriteLine("Owned diagnostic cleanup failed: " + exception.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static async Task<CommandResult> Command(string executable, string[] arguments, string output, string label, CancellationToken cancellation, bool requireSuccess = true, bool echo = false)
|
|
{
|
|
if (!label.StartsWith("capture-", StringComparison.Ordinal) && label is not "container" and not "container-running" and not "guest-container-resources")
|
|
Console.WriteLine("[native-diagnostic] " + label);
|
|
using var commandCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellation);
|
|
var commandToken = commandCancellation.Token;
|
|
var start = new ProcessStartInfo(executable) { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false };
|
|
foreach (var argument in arguments) start.ArgumentList.Add(argument);
|
|
start.Environment["GIT_TERMINAL_PROMPT"] = "0";
|
|
using var process = Process.Start(start) ?? throw new InvalidOperationException("Cannot start " + executable);
|
|
async Task<string> Read(StreamReader reader, string stream)
|
|
{
|
|
var captured = new StringBuilder();
|
|
var buffer = new char[8192];
|
|
using var log = new StreamWriter(Path.Combine(output, label + "." + stream + ".log"), false, new UTF8Encoding(false));
|
|
while (true)
|
|
{
|
|
var count = await reader.ReadAsync(buffer.AsMemory(), commandToken);
|
|
if (count == 0) break;
|
|
if (captured.Length + count > MaximumCapturedCharacters)
|
|
{
|
|
commandCancellation.Cancel();
|
|
throw new InvalidOperationException(label + " exceeded its bounded diagnostic log size.");
|
|
}
|
|
captured.Append(buffer, 0, count);
|
|
await log.WriteAsync(buffer.AsMemory(0, count), commandToken);
|
|
await log.FlushAsync(commandToken);
|
|
if (echo) Console.Write(new string(buffer, 0, count));
|
|
}
|
|
return captured.ToString();
|
|
}
|
|
var stdout = Read(process.StandardOutput, "stdout");
|
|
var stderr = Read(process.StandardError, "stderr");
|
|
try
|
|
{
|
|
await Task.WhenAll(stdout, stderr, process.WaitForExitAsync(commandToken));
|
|
var result = new CommandResult(process.ExitCode, await stdout, await stderr);
|
|
if (requireSuccess && result.ExitCode != 0) throw new InvalidOperationException($"{label} exited {result.ExitCode}: {result.Error[..Math.Min(result.Error.Length, 1500)]}");
|
|
return result;
|
|
}
|
|
catch
|
|
{
|
|
try { if (!process.HasExited) process.Kill(entireProcessTree: true); } catch (InvalidOperationException) { }
|
|
throw;
|
|
}
|
|
}
|
|
|
|
static string Hash(byte[] bytes) => Convert.ToHexStringLower(SHA256.HashData(bytes));
|
|
static void PrintGuestProof(string output, string token)
|
|
{
|
|
var path = Path.Combine(output, "guest-proof.log");
|
|
if (!File.Exists(path)) { Console.WriteLine("[native-diagnostic] No native guest proof was captured."); return; }
|
|
var proof = File.ReadAllText(path).Replace(token, "<run-id>", StringComparison.Ordinal);
|
|
const int budget = 512 * 1024;
|
|
if (proof.Length > budget)
|
|
proof = proof[..(64 * 1024)] + "\n[native-diagnostic] Middle of proof omitted from CI stdout; complete bounded proof is retained in the artifact.\n" + proof[^((budget - 64 * 1024))..];
|
|
Console.WriteLine("[native-diagnostic] Final native guest proof:");
|
|
Console.Write(proof);
|
|
}
|
|
static void PrintFullProof(string output, string token)
|
|
{
|
|
foreach (var name in new[] { "full-result.json", "firstboot.log", "guest-logs/build.stdout.log", "guest-logs/build.stderr.log", "guest-logs/test.stdout.log", "guest-logs/test.stderr.log" })
|
|
{
|
|
var path = Path.Combine(output, name);
|
|
if (!File.Exists(path)) continue;
|
|
var proof = File.ReadAllText(path).Replace(token, "<run-id>", StringComparison.Ordinal);
|
|
if (proof.Length > 64 * 1024) proof = "[earlier output retained in artifact]\n" + proof[^(64 * 1024)..];
|
|
Console.WriteLine("[native-diagnostic] Final native evidence: " + name);
|
|
Console.WriteLine(proof);
|
|
}
|
|
}
|
|
static void Save(string path, object value)
|
|
{
|
|
var temporary = path + ".tmp";
|
|
File.WriteAllText(temporary, JsonSerializer.Serialize(value, JsonOptions), new UTF8Encoding(false));
|
|
File.Move(temporary, path, overwrite: true);
|
|
}
|
|
sealed record OwnedResources(string Token, string ContainerName, string ImageTag, string WorkDirectory, string? ContainerId = null, string? ImageId = null);
|
|
sealed record CommandResult(int ExitCode, string Output, string Error);
|
|
}
|