67 lines
4.0 KiB
PowerShell
67 lines
4.0 KiB
PowerShell
|
|
param([string]$RuntimeDirectory = (Join-Path $PSScriptRoot '..\bin\release'), [switch]$IfPresent)
|
||
|
|
$ErrorActionPreference = 'Stop'
|
||
|
|
function FileHash([string]$path) { $algorithm = [Security.Cryptography.SHA256]::Create(); try { return [BitConverter]::ToString($algorithm.ComputeHash([IO.File]::ReadAllBytes($path))).Replace('-', '') } finally { $algorithm.Dispose() } }
|
||
|
|
$dll = Join-Path ([IO.Path]::GetFullPath($RuntimeDirectory)) 'Qt53DRender.dll'
|
||
|
|
if (!(Test-Path -LiteralPath $dll)) { if ($IfPresent) { Write-Output "Qt3D runtime not deployed yet; repair will run during packaging."; exit 0 }; throw "Qt3D runtime not found: $dll" }
|
||
|
|
$originalHash = '40FD189F1E233FCDCC07A8BC7E226DB7FBF86DBC4A6480C70E31FE1A185936F7'
|
||
|
|
$repairedHash = 'AABB4931C7F7145205F3DEA5DAA1F096600EDD16258FC0A67A5C4F203C5A5DE0'
|
||
|
|
$actual = (FileHash $dll)
|
||
|
|
if ($actual -eq $repairedHash) { Write-Output 'Qt3D Release register-preservation repair already installed.'; exit 0 }
|
||
|
|
if ($actual -ne $originalHash) { throw "Unrecognized Qt3D runtime; refusing a version-specific repair: $actual" }
|
||
|
|
|
||
|
|
# This exact Qt 5.12.4 MinGW x64 build's QVector<QNodeId>::contains helper calls
|
||
|
|
# a find helper that clobbers R11. Other optimized callers retain their container
|
||
|
|
# pointer in R11 across contains(), subsequently passing null to QVector::append.
|
||
|
|
# Preserve R11 in this function's own first parameter home slot. Stack allocation
|
||
|
|
# and nonvolatile saves stay unchanged; extend its x64 runtime-function extent.
|
||
|
|
[byte[]]$bytes = [IO.File]::ReadAllBytes($dll)
|
||
|
|
$pe = [BitConverter]::ToInt32($bytes, 0x3c)
|
||
|
|
$optional = $pe + 24
|
||
|
|
if ([BitConverter]::ToUInt16($bytes, $optional) -ne 0x20b) { throw 'Expected PE32+ image' }
|
||
|
|
$sections = $optional + [BitConverter]::ToUInt16($bytes, $pe + 20)
|
||
|
|
function Offset([int]$rva) {
|
||
|
|
for ($i = 0; $i -lt [BitConverter]::ToUInt16($bytes, $pe + 6); $i++) {
|
||
|
|
$s = $sections + $i * 40
|
||
|
|
$va = [BitConverter]::ToInt32($bytes, $s + 12)
|
||
|
|
$size = [BitConverter]::ToInt32($bytes, $s + 16)
|
||
|
|
if ($rva -ge $va -and $rva -lt ($va + $size)) { return [BitConverter]::ToInt32($bytes, $s + 20) + $rva - $va }
|
||
|
|
}
|
||
|
|
throw ('Unmapped RVA: 0x{0:x}' -f $rva)
|
||
|
|
}
|
||
|
|
$rva = 0x1c2690
|
||
|
|
$start = Offset $rva
|
||
|
|
$code = [Array]::CreateInstance([byte], 62)
|
||
|
|
[Array]::Copy($bytes, $start, $code, 0, 5)
|
||
|
|
[Array]::Copy([byte[]](0x4c,0x89,0x5c,0x24,0x30), 0, $code, 5, 5) # save R11
|
||
|
|
[Array]::Copy($bytes, $start + 5, $code, 10, 41)
|
||
|
|
[Array]::Copy([byte[]](0x4c,0x8b,0x5c,0x24,0x30), 0, $code, 51, 5) # restore R11
|
||
|
|
[Array]::Copy($bytes, $start + 46, $code, 56, 6)
|
||
|
|
[BitConverter]::GetBytes([int](0x1c3bc0 - ($rva + 45))).CopyTo($code, 41)
|
||
|
|
$code.CopyTo($bytes, $start)
|
||
|
|
$bytes[$start + 62] = 0x90
|
||
|
|
$bytes[$start + 63] = 0x90
|
||
|
|
$directory = $optional + 112 + 3 * 8
|
||
|
|
$pdata = [BitConverter]::ToInt32($bytes, $directory)
|
||
|
|
$size = [BitConverter]::ToInt32($bytes, $directory + 4)
|
||
|
|
$found = $false
|
||
|
|
for ($i = 0; $i -lt $size; $i += 12) {
|
||
|
|
$entry = Offset ($pdata + $i)
|
||
|
|
if ([BitConverter]::ToInt32($bytes, $entry) -eq $rva) {
|
||
|
|
if ([BitConverter]::ToInt32($bytes, $entry + 4) -ne ($rva + 52)) { throw 'Unexpected unwind extent' }
|
||
|
|
[BitConverter]::GetBytes([int]($rva + 62)).CopyTo($bytes, $entry + 4)
|
||
|
|
$found = $true
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (!$found) { throw 'Missing x64 runtime-function entry' }
|
||
|
|
$sha = [Security.Cryptography.SHA256]::Create()
|
||
|
|
try { $after = [BitConverter]::ToString($sha.ComputeHash($bytes)).Replace('-', '') } finally { $sha.Dispose() }
|
||
|
|
if ($after -ne $repairedHash) { throw "Unexpected repaired image hash: $after" }
|
||
|
|
$backupDir = Join-Path $RuntimeDirectory 'crash_dumps\dependency_backups'
|
||
|
|
New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
|
||
|
|
$backup = Join-Path $backupDir 'Qt53DRender.original.dll'
|
||
|
|
if (!(Test-Path -LiteralPath $backup)) { Copy-Item -LiteralPath $dll -Destination $backup }
|
||
|
|
elseif ((FileHash $backup) -ne $originalHash) { throw 'Backup differs from the expected original; preserving it' }
|
||
|
|
[IO.File]::WriteAllBytes($dll, $bytes)
|
||
|
|
Write-Output "Repaired Qt3D Release runtime: $dll; original preserved at $backup"
|