Exploit database

GH-945bd5267b0d1b94c3f74faf5cda7e5a

blkph0x/cve-2026-83844_poc

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-83844

5 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files579
blkph0x/cve-2026-83844_pocRevision 193ed44859da
Next files →

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

lab/trace/stage_bfdb_authoring_trial.ps1

PowerShell · 31,509 bytes
param(
    [Parameter(Mandatory = $true)][ValidateSet('old','new')][string]$Build,
    [Parameter(Mandatory = $true)][ValidateSet('control','trigger')][string]$CaseId,
    [Parameter(Mandatory = $true)]
    [ValidatePattern('^[a-z0-9][a-z0-9-]{0,39}$')][string]$TrialLabel,
    [ValidateSet('licensed','external','geometry','crossslot','r32','r48')][string]$Source = 'licensed',
    [ValidateSet('','control-20','trigger-20','unterminated-19','control-21','trigger-21')]
    [string]$GeometryCase = '',
    [ValidateSet('ExistingPid','PlmDebug','IfeoDebugger')][string]$HandoffMode = 'ExistingPid',
    [ValidateSet('PageHeap','Normal')][string]$HeapMode = 'PageHeap',
    [ValidateSet('Default','NewReader','NormalRecord','NormalExtent','CrossslotNormal','CrossslotGuard','R32Normal','R48Normal')][string]$TraceMode = 'Default',
    [ValidatePattern('^[a-z0-9][a-z0-9-]{0,39}$')][string]$ControlTrialLabel = '',
    [switch]$ValidateOnly
)

# Host staging only. Default licensed JPEGs regenerate from tracked source;
# the cited author's separate JPEGs are an optional ignored local source.
# No external script is executed and no guest is launched here.
$ErrorActionPreference = 'Stop'
if (($Source -eq 'geometry') -ne [bool]$GeometryCase) {
    throw 'GeometryCase is required only for the geometry source.'
}
if ($Source -eq 'geometry' -and
    @('control-20','trigger-20','unterminated-19','control-21','trigger-21') -cnotcontains $GeometryCase) {
    throw 'GeometryCase must use the exact lowercase pinned name.'
}
if ($Source -eq 'geometry') {
    $expectedCaseId = if ($GeometryCase.StartsWith('control-')) { 'control' } else { 'trigger' }
    if ($CaseId -cne $expectedCaseId -or $HandoffMode -cne 'PlmDebug' -or
        (($Build -eq 'old') -and ($HeapMode -cne 'Normal' -or $TraceMode -cne 'NormalExtent')) -or
        (($Build -eq 'new') -and ($HeapMode -cne 'PageHeap' -or $TraceMode -cne 'NewReader'))) {
        throw 'Geometry trial requires matched case, PLMDebug, old NormalExtent or new NewReader.'
    }
}
if ($Source -eq 'crossslot' -and
    ($HandoffMode -cne 'PlmDebug' -or
     (($Build -eq 'old') -and (
         ($HeapMode -eq 'Normal' -and $TraceMode -cne 'CrossslotNormal') -or
         ($HeapMode -eq 'PageHeap' -and $TraceMode -cne 'CrossslotGuard'))) -or
     (($Build -eq 'new') -and ($HeapMode -cne 'PageHeap' -or $TraceMode -cne 'NewReader')))) {
    throw 'Crossslot requires PLMDebug, old CrossslotNormal/Guard or new PageHeap NewReader.'
}
if (($Source -eq 'r32') -ne ($TraceMode -eq 'R32Normal') -or
    ($Source -eq 'r32' -and
     ($Build -cne 'old' -or $HeapMode -cne 'Normal' -or $HandoffMode -cne 'PlmDebug'))) {
    throw 'R32 requires signed old Paint, normal heap, PLMDebug and R32Normal.'
}
if (($Source -eq 'r48') -ne ($TraceMode -eq 'R48Normal') -or
    ($Source -eq 'r48' -and
     ($Build -cne 'old' -or $HeapMode -cne 'Normal' -or $HandoffMode -cne 'PlmDebug'))) {
    throw 'R48 requires signed old Paint, normal heap, PLMDebug and R48Normal.'
}
if ($HeapMode -eq 'Normal' -and ($Build -cne 'old' -or $HandoffMode -cne 'PlmDebug')) {
    throw 'Normal-heap leverage follow-up requires old signed Paint and PLMDebug.'
}
if ($TraceMode -eq 'NewReader' -and
    ($Build -cne 'new' -or $HeapMode -cne 'PageHeap' -or
     $Source -notin @('licensed','geometry','crossslot') -or $HandoffMode -cne 'PlmDebug')) {
    throw 'NewReader requires signed new Paint, licensed JPEG, PageHeap and PLMDebug.'
}
if ($TraceMode -eq 'NormalRecord' -and
    ($Build -cne 'old' -or $HeapMode -cne 'Normal' -or
     $Source -notin @('licensed','geometry') -or $HandoffMode -cne 'PlmDebug')) {
    throw 'NormalRecord requires signed old Paint, licensed JPEG, normal heap and PLMDebug.'
}
if ($TraceMode -eq 'NormalExtent' -and
    ($Build -cne 'old' -or $HeapMode -cne 'Normal' -or
     $Source -notin @('licensed','geometry') -or $HandoffMode -cne 'PlmDebug')) {
    throw 'NormalExtent requires signed old Paint, licensed JPEG, normal heap and PLMDebug.'
}
$labRoot = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '..')).Path
$sources = @{
    licensed = @{
        root = (Join-Path $labRoot 'output\provenance-bfdb')
        builder = (Join-Path $labRoot 'corpus\provenance_bfdb\build_fixtures.py')
        verifier = (Join-Path $labRoot 'corpus\provenance_bfdb\verify_fixtures.py')
        cases = @{
            control = @{ file = 'control.jpg'; sha256 = '75a8da33f6eaf1e16bf3b42cd78913b22b2e6a671fda217a508b1ba4230ce864' }
            trigger = @{ file = 'trigger-0120-01.jpg'; sha256 = '82e1ad0b1be3183b0a6075ce53a44a888f149765f24cd2a681b0656b348a8f9a' }
        }
    }
    external = @{
        root = (Join-Path $labRoot 'output\external-bfdb-hackinglz')
        builder = $null
        verifier = (Join-Path $labRoot 'output\external-bfdb-hackinglz\verify_local.py')
        cases = @{
            control = @{ file = 'reproduction__c2pa-control.jpg'; sha256 = 'a2d14755db55de67a47c04090340d8266e892367be4104a45626d7a6fa6e9ffd' }
            trigger = @{ file = 'reproduction__c2pa-0120-01.jpg'; sha256 = 'd743b6c36b47ba6ed86d6614380daa5c5d56d49d681f53e4bad4a1acce9d4757' }
        }
    }
    geometry = @{
        root = (Join-Path $labRoot 'output\provenance-bfdb-geometry')
        builder = (Join-Path $labRoot 'corpus\provenance_bfdb_geometry\build_fixtures.py')
        verifier = (Join-Path $labRoot 'corpus\provenance_bfdb_geometry\verify_fixtures.py')
        manifest = (Join-Path $labRoot 'corpus\provenance_bfdb_geometry\manifest.json')
        cases = @{
            'control-20' = @{ file = 'control-20.jpg'; sha256 = '75a8da33f6eaf1e16bf3b42cd78913b22b2e6a671fda217a508b1ba4230ce864' }
            'trigger-20' = @{ file = 'trigger-20.jpg'; sha256 = '82e1ad0b1be3183b0a6075ce53a44a888f149765f24cd2a681b0656b348a8f9a' }
            'unterminated-19' = @{ file = 'unterminated-19.jpg'; sha256 = 'c45669deac5488374e304292c3359608ebea3aacb3fb8cf6a62c89858121f9ab' }
            'control-21' = @{ file = 'control-21.jpg'; sha256 = 'f38e99936debb9ca123d664079a21f8129bd67a3f2e6ef1f715109e56427cf64' }
            'trigger-21' = @{ file = 'trigger-21.jpg'; sha256 = '7a7008dfa25b0799f025e7765878b7bcb2abea37da3783247075bff767032466' }
        }
    }
    crossslot = @{
        root = (Join-Path $labRoot 'output\provenance-bfdb-crossslot')
        builder = (Join-Path $labRoot 'corpus\provenance_bfdb_crossslot\build_fixtures.py')
        verifier = (Join-Path $labRoot 'corpus\provenance_bfdb_crossslot\verify_fixtures.py')
        manifest = (Join-Path $labRoot 'corpus\provenance_bfdb_crossslot\manifest.json')
        cases = @{
            control = @{ file = 'control-24.jpg'; sha256 = 'a2bdfd294fab4aec5a5df086cde7303f217f4d29031b19a36c0db4dc97bd678b' }
            trigger = @{ file = 'trigger-24.jpg'; sha256 = 'e38a4bb5e2aeab332c6682315b94a0ac7423eea206e33b5238dddb836bbcb345' }
        }
    }
    r32 = @{
        root = (Join-Path $labRoot 'output\provenance-bfdb-r32')
        builder = (Join-Path $labRoot 'corpus\provenance_bfdb_r32\build_fixtures.py')
        verifier = (Join-Path $labRoot 'corpus\provenance_bfdb_r32\verify_fixtures.py')
        manifest = (Join-Path $labRoot 'corpus\provenance_bfdb_r32\manifest.json')
        cases = @{
            control = @{ file = 'control-40.jpg'; sha256 = 'c4af8f072877b9a6a0db19f409dc0c56cc88b72962f2ef8c1f3ca49aac7be52d' }
            trigger = @{ file = 'trigger-40.jpg'; sha256 = '75315db4096f7580490dfbfe8beedc17dcc79f1854a50aa571c7327c71852afa' }
        }
    }
    r48 = @{
        root = (Join-Path $labRoot 'output\provenance-bfdb-r48')
        builder = (Join-Path $labRoot 'corpus\provenance_bfdb_r48\build_fixtures.py')
        verifier = (Join-Path $labRoot 'corpus\provenance_bfdb_r48\verify_fixtures.py')
        manifest = (Join-Path $labRoot 'corpus\provenance_bfdb_r48\manifest.json')
        cases = @{
            control = @{ file = 'control-56.jpg'; sha256 = '3efd5b264ce87fe9dcba39853018a9ce4fbe056e12ea901119d7bb5cdffa7e6c' }
            trigger = @{ file = 'trigger-56.jpg'; sha256 = '7249839113ce0453fe1f6a3e7bc86d6493e7766d405c87c745ea31058f418962' }
        }
    }
}
$selectedSource = $sources[$Source]
$selectedCase = $selectedSource.cases[$(if ($Source -eq 'geometry') { $GeometryCase } else { $CaseId })]
$gflagsSource = 'C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\gflags.exe'
$gflagsHash = '3b4501db349dcd49214c5e4fc57a886458dc013dcee498b2722146fc8d451f45'
$plmdebugSource = Join-Path $labRoot 'isolation\dependencies\debugger\plmdebug.exe'
$plmdebugHash = 'df595e3ab812fabe89dca86fa8526a4ef91c670a71a42c0f2a380c4cd8a65dac'
$guestScript = if ($Source -eq 'r48') {
    Join-Path $PSScriptRoot 'guest_bfdb_r48_entry.ps1'
} else { Join-Path $PSScriptRoot 'guest_bfdb_authoring_entry.ps1' }
$normalReviewer = Join-Path $PSScriptRoot 'export_bfdb_normal_heap_trial.py'
$newReaderReviewer = Join-Path $PSScriptRoot 'export_bfdb_new_reader_trial.py'
$normalRecordReviewer = Join-Path $PSScriptRoot 'export_bfdb_normal_record_trial.py'
$normalExtentReviewer = Join-Path $PSScriptRoot 'export_bfdb_normal_extent_trial.py'
$geometryReviewer = Join-Path $PSScriptRoot 'export_bfdb_geometry_trial.py'
$crossslotReviewer = Join-Path $PSScriptRoot 'export_bfdb_crossslot_trial.py'
$r32Reviewer = Join-Path $PSScriptRoot 'review_bfdb_r32_trial.py'
$r48Reviewer = Join-Path $PSScriptRoot 'review_bfdb_r48_trial.py'
$builder = if ($TraceMode -eq 'NormalExtent') {
    Join-Path $PSScriptRoot 'build_bfdb_normal_extent_trace.py'
} elseif ($TraceMode -eq 'NormalRecord') {
    Join-Path $PSScriptRoot 'build_bfdb_normal_record_trace.py'
} elseif ($TraceMode -in @('CrossslotNormal','CrossslotGuard')) {
    Join-Path $PSScriptRoot 'build_bfdb_crossslot_trace.py'
} elseif ($TraceMode -eq 'R32Normal') {
    Join-Path $PSScriptRoot 'build_bfdb_r32_trace.py'
} elseif ($TraceMode -eq 'R48Normal') {
    Join-Path $PSScriptRoot 'build_bfdb_r48_trace.py'
} elseif ($HeapMode -eq 'Normal') {
    Join-Path $PSScriptRoot 'build_bfdb_normal_heap_trace.py'
} elseif ($TraceMode -eq 'NewReader') {
    Join-Path $PSScriptRoot 'build_bfdb_new_reader_trace.py'
} else { Join-Path $PSScriptRoot 'build_bfdb_authoring_trace.py' }
$profile = if ($TraceMode -eq 'NormalExtent') {
    Join-Path $labRoot 'output\bfdb-normal-extent\old.auto.cdb.txt'
} elseif ($TraceMode -eq 'NormalRecord') {
    Join-Path $labRoot 'output\bfdb-normal-record\old.auto.cdb.txt'
} elseif ($TraceMode -in @('CrossslotNormal','CrossslotGuard')) {
    Join-Path $labRoot "output\bfdb-crossslot\$($HeapMode.ToLowerInvariant())\old.auto.cdb.txt"
} elseif ($TraceMode -eq 'R32Normal') {
    Join-Path $labRoot 'output\bfdb-r32\old.auto.cdb.txt'
} elseif ($TraceMode -eq 'R48Normal') {
    Join-Path $labRoot 'output\bfdb-r48\old.auto.cdb.txt'
} elseif ($HeapMode -eq 'Normal') {
    Join-Path $labRoot 'output\bfdb-normal-heap\old.auto.cdb.txt'
} elseif ($TraceMode -eq 'NewReader') {
    Join-Path $labRoot 'output\bfdb-new-reader\new.auto.cdb.txt'
} else { Join-Path $labRoot "output\bfdb-authoring\$Build.auto.cdb.txt" }
$fixture = Join-Path $selectedSource.root $selectedCase.file

if ($selectedSource.builder) {
    if (-not (Test-Path -LiteralPath $selectedSource.builder -PathType Leaf)) {
        throw 'Tracked licensed bfdb builder is missing.'
    }
    & python $selectedSource.builder | Out-Null
    if ($LASTEXITCODE -ne 0) { throw 'Licensed bfdb pair generation failed.' }
    & python $selectedSource.builder --check | Out-Null
    if ($LASTEXITCODE -ne 0) { throw 'Licensed bfdb pair regeneration check failed.' }
}
if ($Source -eq 'geometry') {
    $geometryManifest = Get-Content -LiteralPath $selectedSource.manifest -Raw | ConvertFrom-Json
    $manifestRows = @($geometryManifest.outputs | Where-Object { $_.file -ceq $selectedCase.file })
    if ($geometryManifest.schema -cne 'paint-provenance-bfdb-geometry-v1' -or
        $manifestRows.Count -ne 1 -or $manifestRows[0].sha256 -cne $selectedCase.sha256) {
        throw 'Pinned geometry manifest does not match selected case.'
    }
}
if ($Source -eq 'crossslot') {
    $crossslotManifest = Get-Content -LiteralPath $selectedSource.manifest -Raw | ConvertFrom-Json
    $manifestRows = @($crossslotManifest.outputs | Where-Object { $_.file -ceq $selectedCase.file })
    if ($crossslotManifest.schema -cne 'paint-provenance-bfdb-crossslot-v1' -or
        $manifestRows.Count -ne 1 -or $manifestRows[0].sha256 -cne $selectedCase.sha256) {
        throw 'Pinned crossslot manifest does not match selected case.'
    }
}
if ($Source -eq 'r32') {
    $r32Manifest = Get-Content -LiteralPath $selectedSource.manifest -Raw | ConvertFrom-Json
    $manifestRows = @($r32Manifest.outputs | Where-Object { $_.file -ceq $selectedCase.file })
    if ($r32Manifest.schema -cne 'paint-provenance-bfdb-r32-v1' -or
        $manifestRows.Count -ne 1 -or $manifestRows[0].sha256 -cne $selectedCase.sha256) {
        throw 'Pinned R32 manifest does not match selected case.'
    }
}
if ($Source -eq 'r48') {
    $r48Manifest = Get-Content -LiteralPath $selectedSource.manifest -Raw | ConvertFrom-Json
    $manifestRows = @($r48Manifest.outputs | Where-Object { $_.file -ceq $selectedCase.file })
    if ($r48Manifest.schema -cne 'paint-provenance-bfdb-r48-v1' -or
        $manifestRows.Count -ne 1 -or $manifestRows[0].sha256 -cne $selectedCase.sha256) {
        throw 'Pinned R48 manifest does not match selected case.'
    }
}
foreach ($path in @($selectedSource.verifier,$gflagsSource,$guestScript,$builder,$fixture)) {
    if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { throw "Required pinned file is missing: $path" }
}
if ($HeapMode -eq 'Normal' -and -not (Test-Path -LiteralPath $normalReviewer -PathType Leaf)) {
    throw 'Normal-heap categorical control reviewer is missing.'
}
if ($TraceMode -eq 'NewReader' -and -not (Test-Path -LiteralPath $newReaderReviewer -PathType Leaf)) {
    throw 'New-reader categorical control reviewer is missing.'
}
if ($TraceMode -eq 'NormalRecord' -and -not (Test-Path -LiteralPath $normalRecordReviewer -PathType Leaf)) {
    throw 'Normal-record categorical control reviewer is missing.'
}
if ($TraceMode -eq 'NormalExtent' -and -not (Test-Path -LiteralPath $normalExtentReviewer -PathType Leaf)) {
    throw 'Normal-extent categorical control reviewer is missing.'
}
if ($Source -eq 'geometry' -and -not (Test-Path -LiteralPath $geometryReviewer -PathType Leaf)) {
    throw 'Geometry categorical control reviewer is missing.'
}
if ($Source -eq 'crossslot' -and -not (Test-Path -LiteralPath $crossslotReviewer -PathType Leaf)) {
    throw 'Crossslot categorical control reviewer is missing.'
}
if ($Source -eq 'r32' -and -not (Test-Path -LiteralPath $r32Reviewer -PathType Leaf)) {
    throw 'R32 categorical control reviewer is missing.'
}
if ($Source -eq 'r48' -and -not (Test-Path -LiteralPath $r48Reviewer -PathType Leaf)) {
    throw 'R48 categorical control reviewer is missing.'
}
& python $selectedSource.verifier | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'Independent JPEG pair verification failed.' }
if ((Get-FileHash -LiteralPath $fixture -Algorithm SHA256).Hash.ToLowerInvariant() -cne $selectedCase.sha256) {
    throw 'Selected fixture digest differs.'
}
if ((Get-FileHash -LiteralPath $gflagsSource -Algorithm SHA256).Hash.ToLowerInvariant() -cne $gflagsHash) {
    throw 'Pinned gflags digest differs.'
}
if ($HandoffMode -eq 'PlmDebug' -and (
    -not (Test-Path -LiteralPath $plmdebugSource -PathType Leaf) -or
    (Get-FileHash -LiteralPath $plmdebugSource -Algorithm SHA256).Hash.ToLowerInvariant() -cne $plmdebugHash)) {
    throw 'Pinned PLMDebug prerequisite differs.'
}
if ($TraceMode -in @('CrossslotNormal','CrossslotGuard')) {
    & python $builder --heap-mode $HeapMode --output $profile | Out-Null
} elseif ($HeapMode -eq 'Normal' -or $TraceMode -eq 'NewReader') {
    & python $builder --output $profile | Out-Null
} else {
    & python $builder --profile $Build --output $profile | Out-Null
}
if ($LASTEXITCODE -ne 0 -or -not (Test-Path -LiteralPath $profile -PathType Leaf) -or
    -not (Test-Path -LiteralPath "$profile.json" -PathType Leaf)) {
    throw 'Signed x64 authoring profile generation failed.'
}
$profileData = Get-Content -LiteralPath "$profile.json" -Raw | ConvertFrom-Json
$expectedProfileSchema = if ($TraceMode -eq 'NormalExtent') {
    'paint-bfdb-normal-extent-trace-v1'
} elseif ($TraceMode -eq 'NormalRecord') {
    'paint-bfdb-normal-record-trace-v1'
} elseif ($TraceMode -in @('CrossslotNormal','CrossslotGuard')) {
    'paint-bfdb-crossslot-trace-v1'
} elseif ($TraceMode -eq 'R32Normal') {
    'paint-bfdb-r32-trace-v1'
} elseif ($TraceMode -eq 'R48Normal') {
    'paint-bfdb-r48-trace-v1'
} elseif ($HeapMode -eq 'Normal') {
    'paint-bfdb-normal-heap-trace-v1'
} elseif ($TraceMode -eq 'NewReader') {
    'paint-bfdb-new-reader-trace-v1'
} else { 'paint-bfdb-authoring-trace-v1' }
if ($profileData.schema -cne $expectedProfileSchema -or
    $profileData.profile -cne $Build -or -not [bool]$profileData.auto_continue) {
    throw 'Generated authoring profile identity differs.'
}
$profileHash = (Get-FileHash -LiteralPath $profile -Algorithm SHA256).Hash.ToLowerInvariant()
$profileManifestHash = (Get-FileHash -LiteralPath "$profile.json" -Algorithm SHA256).Hash.ToLowerInvariant()
$guestEntryHash = (Get-FileHash -LiteralPath $guestScript -Algorithm SHA256).Hash.ToLowerInvariant()
$controlReceiptHash = $null
if ($CaseId -eq 'trigger' -and $HandoffMode -ne 'ExistingPid' -and -not $ValidateOnly) {
    if (-not $ControlTrialLabel) {
        throw 'Debugger-owned trigger requires a completed same-mode control trial label.'
    }
    $experimentSubdir = if ($Source -eq 'r48') { 'bfdb-r48' }
        elseif ($Source -eq 'r32') { 'bfdb-r32' }
        elseif ($Source -eq 'crossslot') { 'bfdb-crossslot' }
        elseif ($TraceMode -eq 'NormalExtent') { 'bfdb-normal-extent' }
        elseif ($TraceMode -eq 'NormalRecord') { 'bfdb-normal-record' }
        elseif ($HeapMode -eq 'Normal') { 'bfdb-normal-heap' }
        elseif ($TraceMode -eq 'NewReader') { 'bfdb-new-reader' }
        else { 'bfdb-authoring' }
    $controlRoot = Join-Path $labRoot "results\$ControlTrialLabel\$experimentSubdir\$Build\control"
    $controlFiles = @(Get-ChildItem -LiteralPath $controlRoot -Recurse -Filter result.json -File -ErrorAction SilentlyContinue)
    if ($controlFiles.Count -ne 1) { throw 'Expected exactly one private control result for debugger-owned trigger.' }
    $control = Get-Content -LiteralPath $controlFiles[0].FullName -Raw | ConvertFrom-Json
    $controlTrialRoot = Join-Path $labRoot "results\$ControlTrialLabel"
    $teardownFile = Join-Path $controlTrialRoot 'teardown-result.json'
    $postShutdownDumps = @(Get-ChildItem -LiteralPath (Join-Path $controlTrialRoot 'dumps') `
        -Filter 'mspaint*.dmp' -File -ErrorAction SilentlyContinue)
    $teardown = if (Test-Path -LiteralPath $teardownFile -PathType Leaf) {
        Get-Content -LiteralPath $teardownFile -Raw | ConvertFrom-Json
    } else { $null }
    if ($control.schema -cne 'paint-bfdb-authoring-guest-v1' -or
        $control.build -cne $Build -or $control.source -cne $Source -or
        $control.case_id -cne 'control' -or $control.handoff_mode -cne $HandoffMode -or
        ($Source -eq 'geometry' -and $control.geometry_case -cne $(if ($GeometryCase -eq 'trigger-21') { 'control-21' } else { 'control-20' })) -or
        ($Source -in @('crossslot','r32','r48') -and $control.trace_mode -cne $TraceMode) -or
        ($HeapMode -eq 'Normal' -and $control.heap_mode -cne 'Normal') -or
        ($TraceMode -eq 'NewReader' -and $control.trace_mode -cne 'NewReader') -or
        ($TraceMode -eq 'NormalRecord' -and $control.trace_mode -cne 'NormalRecord') -or
        ($TraceMode -eq 'NormalExtent' -and $control.trace_mode -cne 'NormalExtent') -or
        $control.status -notin @('observed','review_required') -or
        $control.route_status_before_cleanup -cne 'observed' -or
        -not [bool]$control.debugger_ready -or
        ($HeapMode -eq 'PageHeap' -and -not [bool]$control.pageheap_listing_verified) -or
        ($HeapMode -eq 'Normal' -and
            (-not [bool]$control.normal_heap_verified -or
             [bool]$control.pageheap_listing_verified)) -or
        [bool]$control.observation_window_exhausted -or
        -not [bool]$control.all_marker_pids_match -or
        -not [bool]$control.required_backend_markers_seen -or
        $control.markers -notcontains 'author_uri_dispatch' -or
        $control.profile_sha256 -cne $profileHash -or
        $control.profile_manifest_sha256 -cne $profileManifestHash -or
        $control.guest_entry_sha256 -cne $guestEntryHash -or
        [bool]$control.cdb_access_violation_text_seen -or
        @($control.application_error_events).Count -ne 0 -or
        @($control.dump_files).Count -ne 0 -or
        $postShutdownDumps.Count -ne 0 -or
        @($control.extra_paint_pids).Count -ne 0 -or
        @($control.cleanup.paint_remaining_pids).Count -ne 0 -or
        @($control.cleanup.debugger_remaining_pids).Count -ne 0 -or
        ($HandoffMode -eq 'PlmDebug' -and
            ($control.auto_debugger_disable.exit_code -ne 0 -or
             $control.auto_debugger_disable.registration_pair_completed -ne $true)) -or
        ($HandoffMode -eq 'IfeoDebugger' -and
            ($control.auto_debugger_residue -ne $false -or
             $control.auto_debugger_disable.debugger_value_removed -ne $true)) -or
        ($HeapMode -eq 'PageHeap' -and $control.pageheap_disable_exit_code -ne 0) -or
        ($HeapMode -eq 'Normal' -and $null -ne $control.pageheap_disable_exit_code) -or
        -not $teardown -or
        $teardown.schema -cne 'paint-ab-guest-shutdown-cleanup-v1' -or
        $teardown.trial_label -cne $ControlTrialLabel -or
        $teardown.state -cne 'closed' -or
        $teardown.guest_host_exited -ne $true -or
        $teardown.sandbox_process_count_after -ne 0) {
        throw 'Debugger-owned control did not attest a complete same-PID URI route and closed guest.'
    }
    if ($Source -notin @('geometry','crossslot','r32','r48') -and $HeapMode -eq 'Normal' -and $TraceMode -eq 'Default') {
        $controlStage = Join-Path $labRoot "isolation\generated\$ControlTrialLabel.bfdb-authoring-stage.json"
        if (-not (Test-Path -LiteralPath $controlStage -PathType Leaf)) {
            throw 'Normal-heap control stage record is missing.'
        }
        & python $normalReviewer --stage $controlStage --result $controlFiles[0].FullName `
            --check-control | Out-Null
        if ($LASTEXITCODE -ne 0) {
            throw 'Normal-heap control did not pass categorical strlen/copy review.'
        }
    }
    if ($Source -notin @('geometry','crossslot','r32','r48') -and $TraceMode -eq 'NewReader') {
        $controlStage = Join-Path $labRoot "isolation\generated\$ControlTrialLabel.bfdb-authoring-stage.json"
        if (-not (Test-Path -LiteralPath $controlStage -PathType Leaf)) {
            throw 'New-reader control stage record is missing.'
        }
        & python $newReaderReviewer --stage $controlStage --result $controlFiles[0].FullName `
            --check-control | Out-Null
        if ($LASTEXITCODE -ne 0) {
            throw 'New-reader control did not pass categorical field review.'
        }
    }
    if ($Source -notin @('geometry','crossslot','r32','r48') -and $TraceMode -eq 'NormalRecord') {
        $controlStage = Join-Path $labRoot "isolation\generated\$ControlTrialLabel.bfdb-authoring-stage.json"
        if (-not (Test-Path -LiteralPath $controlStage -PathType Leaf)) {
            throw 'Normal-record control stage record is missing.'
        }
        & python $normalRecordReviewer --stage $controlStage --result $controlFiles[0].FullName `
            --check-control | Out-Null
        if ($LASTEXITCODE -ne 0) {
            throw 'Normal-record control did not pass post-copy and parser-route review.'
        }
    }
    if ($Source -notin @('geometry','crossslot','r32','r48') -and $TraceMode -eq 'NormalExtent') {
        $controlStage = Join-Path $labRoot "isolation\generated\$ControlTrialLabel.bfdb-authoring-stage.json"
        if (-not (Test-Path -LiteralPath $controlStage -PathType Leaf)) {
            throw 'Normal-extent control stage record is missing.'
        }
        & python $normalExtentReviewer --stage $controlStage --result $controlFiles[0].FullName `
            --check-control | Out-Null
        if ($LASTEXITCODE -ne 0) {
            throw 'Normal-extent control did not prove one parseable containing block.'
        }
    }
    if ($Source -eq 'geometry') {
        $controlStage = Join-Path $labRoot "isolation\generated\$ControlTrialLabel.bfdb-authoring-stage.json"
        if (-not (Test-Path -LiteralPath $controlStage -PathType Leaf)) {
            throw 'Geometry control stage record is missing.'
        }
        & python $geometryReviewer --stage $controlStage --result $controlFiles[0].FullName `
            --check-control | Out-Null
        if ($LASTEXITCODE -ne 0) {
            throw 'Geometry control did not pass signed-route and field review.'
        }
    }
    if ($Source -eq 'crossslot') {
        $controlStage = Join-Path $labRoot "isolation\generated\$ControlTrialLabel.bfdb-authoring-stage.json"
        if (-not (Test-Path -LiteralPath $controlStage -PathType Leaf)) {
            throw 'Crossslot control stage record is missing.'
        }
        & python $crossslotReviewer --stage $controlStage --result $controlFiles[0].FullName `
            --check-control | Out-Null
        if ($LASTEXITCODE -ne 0) {
            throw 'Crossslot control did not pass signed-route and field review.'
        }
    }
    if ($Source -eq 'r32') {
        $controlStage = Join-Path $labRoot "isolation\generated\$ControlTrialLabel.bfdb-authoring-stage.json"
        if (-not (Test-Path -LiteralPath $controlStage -PathType Leaf)) {
            throw 'R32 control stage record is missing.'
        }
        & python $r32Reviewer --stage $controlStage --result $controlFiles[0].FullName `
            --check-control | Out-Null
        if ($LASTEXITCODE -ne 0) {
            throw 'R32 control did not prove the pinned 32-byte request and rounded source slot.'
        }
    }
    if ($Source -eq 'r48') {
        $controlStage = Join-Path $labRoot "isolation\generated\$ControlTrialLabel.bfdb-authoring-stage.json"
        if (-not (Test-Path -LiteralPath $controlStage -PathType Leaf)) {
            throw 'R48 control stage record is missing.'
        }
        & python $r48Reviewer --stage $controlStage --result $controlFiles[0].FullName `
            --check-control | Out-Null
        if ($LASTEXITCODE -ne 0) {
            throw 'R48 control did not prove the pinned 48-byte request and rounded source slot.'
        }
    }
    $controlReceiptHash = (Get-FileHash -LiteralPath $controlFiles[0].FullName -Algorithm SHA256).Hash.ToLowerInvariant()
}
if ($ValidateOnly) {
    Write-Output "Verified $Build/$CaseId $Source fixture, $HandoffMode route, $HeapMode heap and signed x64 profile; no guest staged."
    return
}
if (@(Get-Process WindowsSandbox,WindowsSandboxClient -ErrorAction SilentlyContinue).Count -ne 0) {
    throw 'A guest is active; shared mapped input cannot be replaced.'
}

$wsb = (& (Join-Path $labRoot 'isolation\prepare-sandbox.ps1') `
    -Build $Build -CorpusMode None -FocusedTrace -DebuggerHeapExtensions `
    -Networking Enable -VGpu Disable -TrialLabel $TrialLabel | Select-Object -Last 1)
if (-not (Test-Path -LiteralPath $wsb -PathType Leaf)) { throw 'Shared preparer did not produce a guest configuration.' }
$inputRoot = Join-Path $labRoot "isolation\input\$Build"
$traceDest = Join-Path $inputRoot 'trace'
$fixtureDest = Join-Path $inputRoot 'bfdb_authoring'
$debuggerDest = Join-Path $inputRoot 'debugger'
New-Item -ItemType Directory -Force -Path $traceDest,$fixtureDest | Out-Null
Copy-Item -LiteralPath $guestScript -Destination (Join-Path $traceDest 'guest_bfdb_authoring_entry.ps1') -Force
Copy-Item -LiteralPath $profile -Destination (Join-Path $traceDest 'bfdb.auto.cdb.txt') -Force
Copy-Item -LiteralPath "$profile.json" -Destination (Join-Path $traceDest 'bfdb.auto.cdb.txt.json') -Force
Copy-Item -LiteralPath $fixture -Destination (Join-Path $fixtureDest $selectedCase.file) -Force
Copy-Item -LiteralPath $gflagsSource -Destination (Join-Path $debuggerDest 'gflags.exe') -Force
foreach ($row in @(
    @{source=$guestScript; destination=(Join-Path $traceDest 'guest_bfdb_authoring_entry.ps1')},
    @{source=$profile; destination=(Join-Path $traceDest 'bfdb.auto.cdb.txt')},
    @{source="$profile.json"; destination=(Join-Path $traceDest 'bfdb.auto.cdb.txt.json')},
    @{source=$fixture; destination=(Join-Path $fixtureDest $selectedCase.file)},
    @{source=$gflagsSource; destination=(Join-Path $debuggerDest 'gflags.exe')}
)) {
    if ((Get-FileHash -LiteralPath $row.source -Algorithm SHA256).Hash.ToLowerInvariant() -cne
        (Get-FileHash -LiteralPath $row.destination -Algorithm SHA256).Hash.ToLowerInvariant()) {
        throw 'Staged authoring input differs from verified source.'
    }
}
[xml]$config = Get-Content -LiteralPath $wsb -Raw
$command = $config.SelectSingleNode('/Configuration/LogonCommand/Command')
if (-not $command -or $command.InnerText -notlike '*guest_focus_entry.ps1*') {
    throw 'Focused guest entry replacement is not unique.'
}
$command.InnerText = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\PaintLab\Input\trace\guest_bfdb_authoring_entry.ps1 -Build $Build -CaseId $CaseId -Source $Source -HandoffMode $HandoffMode"
if ($Source -eq 'geometry') { $command.InnerText += " -GeometryCase $GeometryCase" }
if ($HeapMode -eq 'Normal') { $command.InnerText += ' -HeapMode Normal' }
if ($TraceMode -eq 'NewReader') { $command.InnerText += ' -TraceMode NewReader' }
if ($TraceMode -eq 'NormalRecord') { $command.InnerText += ' -TraceMode NormalRecord' }
if ($TraceMode -eq 'NormalExtent') { $command.InnerText += ' -TraceMode NormalExtent' }
if ($TraceMode -eq 'CrossslotNormal') { $command.InnerText += ' -TraceMode CrossslotNormal' }
if ($TraceMode -eq 'CrossslotGuard') { $command.InnerText += ' -TraceMode CrossslotGuard' }
if ($TraceMode -eq 'R32Normal') { $command.InnerText += ' -TraceMode R32Normal' }
if ($TraceMode -eq 'R48Normal') { $command.InnerText += ' -TraceMode R48Normal' }
$config.Save($wsb)
[xml]$check = Get-Content -LiteralPath $wsb -Raw
if ($check.Configuration.LogonCommand.Command -cne $command.InnerText) {
    throw 'Guest command did not retain the pinned authoring entry.'
}
[ordered]@{
    schema = 'paint-bfdb-authoring-stage-v1'; status = 'staged_not_run'
    build = $Build; case_id = $CaseId; source = $Source; trial_label = $TrialLabel
    geometry_case = $GeometryCase
    handoff_mode = $HandoffMode; control_trial_label = $ControlTrialLabel
    heap_mode = $HeapMode; trace_mode = $TraceMode
    control_result_sha256 = $controlReceiptHash
    networking = 'Enable'; fixture_sha256 = $selectedCase.sha256
    gflags_sha256 = $gflagsHash
    plmdebug_sha256 = $(if ($HandoffMode -eq 'PlmDebug') { $plmdebugHash } else { $null })
    profile_sha256 = $profileHash
    profile_manifest_sha256 = $profileManifestHash
    guest_entry_sha256 = $guestEntryHash
    wsb_sha256 = (Get-FileHash -LiteralPath $wsb -Algorithm SHA256).Hash.ToLowerInvariant()
} | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath (
    Join-Path $labRoot "isolation\generated\$TrialLabel.bfdb-authoring-stage.json") -Encoding UTF8
Write-Output $wsb