File size: 12,651 Bytes
0cb039c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
param(
    [Parameter(Mandatory = $true)][string]$TargetName,
    [Parameter(Mandatory = $true)]
    [ValidateSet("O0", "O2", "O2-noinline")]
    [string]$Mode,
    [Parameter(Mandatory = $true)][string]$BinaryPath,
    [Parameter(Mandatory = $true)][string]$PdbPath,
    [Parameter(Mandatory = $true)][string[]]$ProjectSourceRoots,
    [Parameter(Mandatory = $true)][string]$EvidenceRoot
)

$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest

$BinaryPath = (Resolve-Path $BinaryPath).Path
$PdbPath = (Resolve-Path $PdbPath).Path
$EvidenceRoot = [System.IO.Path]::GetFullPath($EvidenceRoot)
New-Item -ItemType Directory -Force -Path $EvidenceRoot | Out-Null
$rawDir = Join-Path $EvidenceRoot "raw"
New-Item -ItemType Directory -Force -Path $rawDir | Out-Null

function Get-NativeTool {
    param([Parameter(Mandatory = $true)][string]$Name)
    $cmd = Get-Command $Name -CommandType Application -ErrorAction Stop | Select-Object -First 1
    return $cmd.Source
}

function Normalize-SourcePath {
    param([Parameter(Mandatory = $true)][string]$PathValue)
    return $PathValue.Trim().Replace('/', '\')
}

$llvmPdbutilExe = Get-NativeTool "llvm-pdbutil.exe"
$llvmReadobjExe = Get-NativeTool "llvm-readobj.exe"

$peHeaders = Join-Path $rawDir "pe-headers.txt"
$pdbSummary = Join-Path $rawDir "pdb-summary.txt"
$pdbModules = Join-Path $rawDir "pdb-modules-files.txt"
$pdbSymbols = Join-Path $rawDir "pdb-symbols.txt"

& $llvmReadobjExe --file-headers --coff-debug-directory $BinaryPath 2>&1 | Set-Content -Encoding utf8 $peHeaders
if ($LASTEXITCODE -ne 0) { throw "llvm-readobj failed for $BinaryPath" }
$peText = Get-Content -Raw $peHeaders
if ($peText -notmatch "IMAGE_FILE_MACHINE_AMD64") {
    throw "Linked PE is not AMD64: $BinaryPath"
}

& $llvmPdbutilExe dump -summary $PdbPath 2>&1 | Set-Content -Encoding utf8 $pdbSummary
if ($LASTEXITCODE -ne 0) { throw "llvm-pdbutil summary failed for $PdbPath" }
& $llvmPdbutilExe dump -modules -files $PdbPath 2>&1 | Set-Content -Encoding utf8 $pdbModules
if ($LASTEXITCODE -ne 0) { throw "llvm-pdbutil module/file dump failed for $PdbPath" }
& $llvmPdbutilExe dump -symbols $PdbPath 2>&1 | Set-Content -Encoding utf8 $pdbSymbols
if ($LASTEXITCODE -ne 0) { throw "llvm-pdbutil symbol dump failed for $PdbPath" }

# Build exact project-source and object-name indexes from the selected roots.
# Exact source provenance from `llvm-pdbutil dump -modules -files` is preferred:
# basename-only object matching can otherwise admit an unrelated vendor object when
# a dependency happens to compile a source with the same filename (for example,
# WinSparkle src/settings.cpp vs wxWidgets src/msw/settings.cpp).
$projectSources = [System.Collections.Generic.List[string]]::new()
$projectSourceSet = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
$objectNeedles = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
foreach ($rootInput in $ProjectSourceRoots) {
    $root = (Resolve-Path $rootInput).Path
    Get-ChildItem -Path $root -Recurse -File | Where-Object {
        $_.Extension -in @(".c", ".cc", ".cpp", ".cxx", ".c++")
    } | ForEach-Object {
        $normalized = Normalize-SourcePath -PathValue $_.FullName
        $projectSources.Add($_.FullName)
        [void]$projectSourceSet.Add($normalized)
        [void]$objectNeedles.Add(($_.Name + ".obj"))
        [void]$objectNeedles.Add(($_.BaseName + ".obj"))
    }
}
if ($projectSources.Count -eq 0) {
    throw "No project source files found under: $($ProjectSourceRoots -join ', ')"
}

# Parse the Files section, where llvm-pdbutil groups source/header paths by Mod.
# A module is project-owned when at least one exact compiled source path from the
# selected source roots is present in that module's file provenance.
$provenanceModules = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
$moduleLines = Get-Content $pdbModules
$inFilesSection = $false
$currentFileModule = ""
foreach ($line in $moduleLines) {
    if ($line -match '^\s*Files\s*$') {
        $inFilesSection = $true
        $currentFileModule = ""
        continue
    }
    if (-not $inFilesSection) { continue }

    if ($line -match '^\s*Mod\s+\d+\s+\|\s+`([^`]+)`') {
        $currentFileModule = $Matches[1]
        continue
    }

    if (-not [string]::IsNullOrWhiteSpace($currentFileModule) -and
        $line -match '^\s*-\s+\([^)]*\)\s+(.+)$') {
        $sourcePath = Normalize-SourcePath -PathValue $Matches[1]
        if ($projectSourceSet.Contains($sourcePath)) {
            [void]$provenanceModules.Add($currentFileModule)
        }
    }
}

$useProvenanceOwnership = $provenanceModules.Count -gt 0
$ownershipMethod = if ($useProvenanceOwnership) {
    "pdb-module-file-provenance"
} else {
    "source-derived-object-name-fallback"
}

function Test-ProjectModule {
    param(
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [string]$ModuleName
    )
    if ([string]::IsNullOrWhiteSpace($ModuleName)) { return $false }

    if ($useProvenanceOwnership) {
        return $provenanceModules.Contains($ModuleName)
    }

    # Fallback for PDBs/tool versions that do not expose usable module-file
    # provenance. CMake/Ninja commonly uses foo.cpp.obj while MSBuild commonly
    # uses foo.obj, so retain both spellings.
    $lower = $ModuleName.ToLowerInvariant()
    foreach ($needleValue in $objectNeedles) {
        $needle = $needleValue.ToLowerInvariant()
        if ($lower -eq $needle -or
            $lower.EndsWith("\$needle") -or
            $lower.EndsWith("/$needle") -or
            $lower.Contains("($needle)")) {
            return $true
        }
    }
    return $false
}

$symbolLines = Get-Content $pdbSymbols
$currentModule = ""
$pendingProc = $null
$awaitingCompile3Flags = $false
$procedures = [System.Collections.Generic.List[object]]::new()
$selectedModules = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
$globalCompile3Count = 0
$selectedCompile3Count = 0
$selectedLtcgCompile3Count = 0
$globalOptSpeedFrameCount = 0
$selectedOptSpeedFrameCount = 0

foreach ($line in $symbolLines) {
    if ($line -match '^\s*Mod\s+\d+\s+\|\s+`([^`]+)`') {
        $currentModule = $Matches[1]
        $pendingProc = $null
        $awaitingCompile3Flags = $false
        if (Test-ProjectModule -ModuleName $currentModule) {
            [void]$selectedModules.Add($currentModule)
        }
        continue
    }

    $isProjectModule = Test-ProjectModule -ModuleName $currentModule

    if ($line -match 'S_COMPILE3') {
        $globalCompile3Count++
        if ($isProjectModule) { $selectedCompile3Count++ }
        $awaitingCompile3Flags = $true
        continue
    }

    if ($awaitingCompile3Flags -and $line -match 'flags\s*=\s*(.*)$') {
        if ($isProjectModule -and $Matches[1] -match '(?i)ltcg') {
            $selectedLtcgCompile3Count++
        }
        $awaitingCompile3Flags = $false
    }

    if ($line -match 'flags\s*=.*opt speed') {
        $globalOptSpeedFrameCount++
        if ($isProjectModule) { $selectedOptSpeedFrameCount++ }
    }

    if ($line -match 'S_[A-Z0-9_]*PROC32(?:_ID)?\s+\[.*\]\s+`([^`]+)`') {
        $pendingProc = $Matches[1]
        continue
    }

    if ($null -ne $pendingProc -and $line -match 'addr\s*=\s*([0-9A-Fa-f]+):([0-9A-Fa-f]+),\s*code size\s*=\s*([0-9]+)') {
        $name = [string]$pendingProc
        $leaf = $name
        if ($leaf.Contains("::")) { $leaf = $leaf.Substring($leaf.LastIndexOf("::") + 2) }
        if ($leaf.Contains("(")) { $leaf = $leaf.Substring(0, $leaf.IndexOf("(")) }

        $procedures.Add([pscustomobject]@{
            module = $currentModule
            project_owned = $isProjectModule
            raw_name = $name
            leaf_name = $leaf
            address = ($Matches[1] + ":" + $Matches[2])
            code_size = [int]$Matches[3]
        })
        $pendingProc = $null
    }
}

if ($selectedModules.Count -eq 0) {
    throw "No project-owned PDB modules matched the selected source roots"
}
if ($globalCompile3Count -eq 0) {
    throw "No S_COMPILE3 records found in linked PDB"
}
if ($selectedCompile3Count -eq 0) {
    throw "No S_COMPILE3 records found for project-owned compilands"
}
if ($selectedLtcgCompile3Count -ne 0) {
    throw "Project-owned compilands unexpectedly report LTCG"
}

$projectProcedures = @($procedures | Where-Object { $_.project_owned -and $_.code_size -gt 0 })
if ($projectProcedures.Count -eq 0) {
    throw "No project-owned procedure records were recovered from the PDB"
}

function Get-CollisionStats {
    param(
        [Parameter(Mandatory = $true)][object[]]$Rows,
        [Parameter(Mandatory = $true)][string]$Property
    )
    $groups = @($Rows | Group-Object -Property $Property)
    $collisionGroups = @($groups | Where-Object {
        @($_.Group | Select-Object -ExpandProperty address -Unique).Count -gt 1
    })
    $collisionAddresses = 0
    foreach ($group in $collisionGroups) {
        $collisionAddresses += @($group.Group | Select-Object -ExpandProperty address -Unique).Count
    }
    $allAddresses = @($Rows | Select-Object -ExpandProperty address -Unique).Count
    $rate = if ($allAddresses -gt 0) {
        [math]::Round(100.0 * $collisionAddresses / $allAddresses, 2)
    } else { 0.0 }

    return [ordered]@{
        source_function_addresses = $allAddresses
        unique_names = $groups.Count
        collision_groups = $collisionGroups.Count
        collision_addresses = $collisionAddresses
        collision_rate_pct = $rate
    }
}

$rawStats = Get-CollisionStats -Rows $projectProcedures -Property "raw_name"
$leafStats = Get-CollisionStats -Rows $projectProcedures -Property "leaf_name"

$summary = [ordered]@{
    schema = "decbench-native-msvc-pdb-qualification-v2"
    target = $TargetName
    optimization = $Mode
    architecture = "x86_64"
    linked_image = [System.IO.Path]::GetFileName($BinaryPath)
    pdb = [System.IO.Path]::GetFileName($PdbPath)
    ground_truth = "native MSVC PDB / CodeView"
    project_ownership_method = $ownershipMethod
    project_source_file_count = $projectSources.Count
    project_object_name_candidates = @($objectNeedles | Sort-Object)
    project_provenance_compiland_count = $provenanceModules.Count
    project_compiland_count = $selectedModules.Count
    project_compilands = @($selectedModules | Sort-Object)
    pdb_compile3_records_global = $globalCompile3Count
    pdb_compile3_records_selected = $selectedCompile3Count
    pdb_ltcg_compile3_records_selected = $selectedLtcgCompile3Count
    pdb_optimized_frame_records_global = $globalOptSpeedFrameCount
    pdb_optimized_frame_records_selected = $selectedOptSpeedFrameCount
    project_procedure_records = $projectProcedures.Count
    raw_pdb_name_collision = $rawStats
    leaf_name_collision_heuristic = $leafStats
    caveats = @(
        "The PDB raw-name collision metric is a CodeView diagnostic and is not asserted to be numerically equivalent to DecBench's DWARF DW_AT_name metric.",
        "Project ownership uses exact PDB module/file source provenance when available; source-derived object-name matching is only a fallback for PDBs without usable provenance.",
        "FRAMEPROC OptimizedForSpeed counts are diagnostic only and are not used as the optimization-mode oracle.",
        "This is target/oracle qualification, not an end-to-end DecBench GED/type/byte benchmark run."
    )
}

$summary | ConvertTo-Json -Depth 8 | Set-Content -Encoding utf8 (Join-Path $EvidenceRoot "qualification.json")
$projectProcedures | ConvertTo-Json -Depth 5 | Set-Content -Encoding utf8 (Join-Path $EvidenceRoot "project-procedures.json")

Write-Host "PASS $TargetName $Mode native MSVC/PDB oracle qualification"
Write-Host "  ownership method:          $ownershipMethod"
Write-Host "  project source files:      $($projectSources.Count)"
Write-Host "  project compilands:        $($selectedModules.Count)"
Write-Host "  project procedures:        $($projectProcedures.Count)"
Write-Host "  selected S_COMPILE3:       $selectedCompile3Count"
Write-Host "  selected LTCG S_COMPILE3:  $selectedLtcgCompile3Count"
Write-Host "  raw-name collision:        $($rawStats.collision_rate_pct)%"
Write-Host "  leaf-name heuristic:       $($leafStats.collision_rate_pct)%"