1param([string] $MSBuild, [string] $Project)
2$ErrorActionPreference = 'Stop'
3
4if (-not $Project) { $Project = Join-Path $PSScriptRoot '..\CrashReporting\CrashHandler.vcxproj' }
5$projectPath = (Resolve-Path -LiteralPath $Project).Path
6if (-not $MSBuild) {
7 $vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe'
8 $installation = & $vswhere -latest -products '*' -requires Microsoft.Component.MSBuild -property installationPath
9 if ($LASTEXITCODE -or -not $installation) { throw 'Visual Studio with MSBuild is required' }
10 $MSBuild = Join-Path $installation 'MSBuild\Current\Bin\amd64\MSBuild.exe'
11}
12
13# Keep rebuilds and the intentional source mutation away from the shipping helper.
14$root = Join-Path $PSScriptRoot ('..\.codex-build\crash-handler-repro-' + [Guid]::NewGuid().ToString('N'))
15$root = [IO.Path]::GetFullPath($root)
16$source = Join-Path $root 'source'
17$output = Join-Path $root 'bin'
18[void][IO.Directory]::CreateDirectory($source)
19foreach ($name in 'CrashHandler.cpp', 'CrashUpload.cpp', 'CrashShared.h', 'CrashUpload.h') {
20 Copy-Item -LiteralPath (Join-Path (Split-Path $projectPath) $name) -Destination $source
21}
22$stagedProject = Join-Path $source 'CrashHandler.vcxproj'
23Copy-Item -LiteralPath $projectPath -Destination $stagedProject
24
25function Build-Helper([string] $Label, [string] $Target = 'Build') {
26 $log = Join-Path $root ($Label + '.log')
27 $arguments = @($stagedProject, "/t:$Target", '/p:Configuration=Release', '/p:Platform=x64',
28 '/p:PreferredToolArchitecture=x64', "/p:OutDir=$output\", "/p:IntDir=$output\obj\",
29 '/m', '/nologo', '/v:normal', '/nr:false')
30 & $MSBuild @arguments *> $log
31 if ($LASTEXITCODE) {
32 Get-Content -LiteralPath $log -Tail 40 | Write-Host
33 throw "Build failed: $log"
34 }
35 # Validate the EXE/PDB identity and repeated archival, not PDB byte layout:
36 # MSVC may reorder PDB records while keeping their matching GUID and age.
37 $archiveLog = Join-Path $root ($Label + '-symbols.log')
38 $exitCode = Invoke-Archive $archiveLog
39 if ($exitCode) {
40 Get-Content -LiteralPath $archiveLog -Tail 20 | Write-Host
41 throw "Symbol validation failed: $archiveLog"
42 }
43}
44
45function Invoke-Archive([string] $Log) {
46 # Windows PowerShell treats redirected native stderr as an error record;
47 # use the child process exit code, including for intentional rejection tests.
48 $savedPreference = $ErrorActionPreference
49 try {
50 $ErrorActionPreference = 'Continue'
51 & powershell.exe -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot '..\CrashReporting\Archive-Symbols.ps1') `
52 -ExecutablePath (Join-Path $output 'CrashHandler.exe') -ArchiveRoot (Join-Path $root 'symbols') *> $Log
53 return $LASTEXITCODE
54 } finally { $ErrorActionPreference = $savedPreference }
55}
56
57function Read-Artifacts {
58 $result = @{}
59 foreach ($name in 'CrashHandler.exe', 'CrashHandler.pdb') {
60 $path = Join-Path $output $name
61 $result[$name] = @{ Hash = (Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash }
62 }
63 return $result
64}
65
66function Assert-Same($Expected, [string] $Description) {
67 $actual = Read-Artifacts
68 foreach ($name in 'CrashHandler.exe') {
69 if ($Expected[$name].Hash -ne $actual[$name].Hash) { throw "FAIL ${Description}: $name changed" }
70 }
71 Write-Host "PASS $Description (identical EXE; matching symbols archived)"
72}
73
74Build-Helper '01-clean' 'Rebuild'
75$baseline = Read-Artifacts
76$archiveDirectory = Get-ChildItem -LiteralPath (Join-Path $root 'symbols') -Directory | Select-Object -First 1
77$archivedPdb = Join-Path $archiveDirectory.FullName 'CrashHandler.pdb'
78$archivedManifest = Join-Path $archiveDirectory.FullName 'manifest.json'
79$archivePdbHash = (Get-FileHash -LiteralPath $archivedPdb).Hash
80$archiveManifestHash = (Get-FileHash -LiteralPath $archivedManifest).Hash
81Build-Helper '02-noop'
82Assert-Same $baseline 'unchanged incremental build'
83
84# A forced link must not advance an embedded timestamp or PDB age.
85Start-Sleep -Milliseconds 1100
86Get-ChildItem -LiteralPath (Join-Path $output 'obj') -Filter '*.obj' |
87 ForEach-Object { $_.LastWriteTimeUtc = [DateTime]::UtcNow }
88Build-Helper '03-relink'
89Assert-Same $baseline 'forced relink'
90
91Build-Helper '04-clean-repeat' 'Rebuild'
92Assert-Same $baseline 'second clean rebuild'
93
94Start-Sleep -Milliseconds 1100
95(Get-Item -LiteralPath (Join-Path $source 'CrashHandler.cpp')).LastWriteTimeUtc = [DateTime]::UtcNow
96Build-Helper '05-touch-source'
97Assert-Same $baseline 'source timestamp change with identical content'
98
99# Prove that reproducibility has not hidden a real behavior change from patching.
100$header = Join-Path $source 'CrashShared.h'
101$original = [IO.File]::ReadAllBytes($header)
102$text = [Text.Encoding]::UTF8.GetString($original)
103$changed = $text.Replace('MaxDumpBytes = 64ull', 'MaxDumpBytes = 63ull')
104if ($changed -eq $text) { throw 'Cannot locate the isolated source mutation' }
105try {
106 [IO.File]::WriteAllBytes($header, [Text.Encoding]::UTF8.GetBytes($changed))
107 Build-Helper '06-changed-source'
108 $modified = Read-Artifacts
109 if ($modified['CrashHandler.exe'].Hash -eq $baseline['CrashHandler.exe'].Hash) {
110 throw 'FAIL a real source change did not change the executable hash'
111 }
112 Write-Host 'PASS real source change changes the executable hash'
113 Copy-Item -LiteralPath (Join-Path $output 'CrashHandler.pdb') -Destination (Join-Path $root 'changed-source.pdb')
114} finally {
115 [IO.File]::WriteAllBytes($header, $original)
116}
117Build-Helper '07-restored-source'
118Assert-Same $baseline 'restored source restores the original hashes'
119if ((Get-FileHash -LiteralPath $archivedPdb).Hash -ne $archivePdbHash -or
120 (Get-FileHash -LiteralPath $archivedManifest).Hash -ne $archiveManifestHash) {
121 throw 'FAIL repeated archival replaced the original symbols or manifest'
122}
123Write-Host 'PASS original archived symbols and manifest retained'
124
125$currentPdb = Join-Path $output 'CrashHandler.pdb'
126$originalPdb = [IO.File]::ReadAllBytes($currentPdb)
127try {
128 Copy-Item -LiteralPath (Join-Path $root 'changed-source.pdb') -Destination $currentPdb
129 $log = Join-Path $root '08-mismatched-symbols.log'
130 if ((Invoke-Archive $log) -eq 0 -or -not (Select-String -LiteralPath $log -SimpleMatch 'PDB does not match')) {
131 throw 'FAIL mismatched input symbols were not rejected'
132 }
133 Write-Host 'PASS mismatched input symbols rejected'
134} finally { [IO.File]::WriteAllBytes($currentPdb, $originalPdb) }
135
136$originalArchive = [IO.File]::ReadAllBytes($archivedPdb)
137try {
138 $corrupted = [byte[]]$originalArchive.Clone()
139 $corrupted[$corrupted.Length - 1] = $corrupted[$corrupted.Length - 1] -bxor 1
140 [IO.File]::WriteAllBytes($archivedPdb, $corrupted)
141 $log = Join-Path $root '09-damaged-archive.log'
142 if ((Invoke-Archive $log) -eq 0 -or -not (Select-String -LiteralPath $log -SimpleMatch 'Refusing to overwrite')) {
143 throw 'FAIL damaged archived symbols were not rejected'
144 }
145 Write-Host 'PASS damaged archived symbols rejected even with unchanged identity'
146} finally { [IO.File]::WriteAllBytes($archivedPdb, $originalArchive) }
147$baseline | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath (Join-Path $root 'baseline-hashes.json') -Encoding UTF8
148Write-Host "Reproducibility tests passed: $root"