From 7f5d310e58248e2196d48008bdef6f570b3a0b22 Mon Sep 17 00:00:00 2001 From: L <11664880+silver886@users.noreply.github.com> Date: Sat, 14 Mar 2026 04:45:58 -0700 Subject: [PATCH 1/8] update core.ps1 fix x64 system program path resolving --- lib/core.ps1 | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 7ca7d121e2..f54421f581 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -21,6 +21,26 @@ function Get-PESubsystem($filePath) { } } +function Get-PEMachine($filePath) { + try { + $fileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read) + $binaryReader = [System.IO.BinaryReader]::new($fileStream) + + $fileStream.Seek(0x3C, [System.IO.SeekOrigin]::Begin) | Out-Null + $peOffset = $binaryReader.ReadInt32() + + # Machine field is at PE signature (4 bytes) + offset 0 of COFF header + $fileStream.Seek($peOffset + 4, [System.IO.SeekOrigin]::Begin) | Out-Null + + return $binaryReader.ReadUInt16() + } catch { + return 0 + } finally { + $binaryReader.Close() + $fileStream.Close() + } +} + function Set-PESubsystem($filePath, $targetSubsystem) { try { $fileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite) @@ -902,7 +922,27 @@ function shim($path, $global, $name, $arg) { # for programs with no awareness of any shell warn_on_overwrite "$shim.shim" $path Copy-Item (get_shim_path) "$shim.exe" -Force - Write-Output "path = `"$resolved_path`"" | Out-UTF8File "$shim.shim" + + $rewrote_path = $resolved_path + + # If the shim exe is x86 on a x64 OS, rewrite paths so the shim resolves correctly: + # System32 -> Sysnative (x64 resolve x64 program in System32, and it should be Sysnative in x86 program) + # SysWOW64 -> System32 (x64 resolve x86 program in SysWOW64, and it should be System32 in x86 program) + $shim_machine = Get-PEMachine "$shim.exe" + # 0x014c is IMAGE_FILE_MACHINE_I386 + # https://learn.microsoft.com/en-us/windows/win32/sysinfo/image-file-machine-constants + if ($shim_machine -eq 0x014c -and [System.Environment]::Is64BitOperatingSystem) { + $sysdir = [System.IO.Path]::Combine($env:SystemRoot, 'System32') + $sysnative = [System.IO.Path]::Combine($env:SystemRoot, 'Sysnative') + $syswow = [System.IO.Path]::Combine($env:SystemRoot, 'SysWOW64') + if ($rewrote_path -like "$sysdir\*") { + $rewrote_path = $rewrote_path -replace [regex]::Escape($sysdir), $sysnative + } elseif ($rewrote_path -like "$syswow\*") { + $rewrote_path = $rewrote_path -replace [regex]::Escape($syswow), $sysdir + } + } + + Write-Output "path = `"$rewrote_path`"" | Out-UTF8File "$shim.shim" if ($arg) { Write-Output "args = $arg" | Out-UTF8File "$shim.shim" -Append } From bf7787266029495ee266fb1aea10b92f1fc515aa Mon Sep 17 00:00:00 2001 From: L <11664880+silver886@users.noreply.github.com> Date: Sat, 14 Mar 2026 04:57:24 -0700 Subject: [PATCH 2/8] add test --- test/Scoop-Core.Tests.ps1 | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/Scoop-Core.Tests.ps1 b/test/Scoop-Core.Tests.ps1 index d1cc9defe3..6f966ce49a 100644 --- a/test/Scoop-Core.Tests.ps1 +++ b/test/Scoop-Core.Tests.ps1 @@ -357,6 +357,71 @@ Describe 'app' -Tag 'Scoop' { } } +Describe 'Get-PEMachine' -Tag 'Scoop' { + It 'returns machine type for a valid PE file' { + $shim_path = get_shim_path + if ($shim_path -and (Test-Path $shim_path)) { + $machine = Get-PEMachine $shim_path + # Should be a known machine type (I386 (x86): 0x014c, amd64 (x64): 0x8664, arm64: 0xAA64) + # https://learn.microsoft.com/en-us/windows/win32/sysinfo/image-file-machine-constants + $machine | Should -BeIn @(0x014c, 0x8664, 0xAA64) + } else { + Set-ItResult -Skipped -Because 'shim exe not found' + } + } + + It 'returns 0 for a non-existent file' { + Get-PEMachine 'C:\nonexistent\fake.exe' | Should -Be 0 + } + + It 'returns 0 for a non-PE file' { + $working_dir = setup_working 'shim' + Get-PEMachine "$working_dir\shim-test.ps1" | Should -Be 0 + } +} + +Describe 'WoW64 path rewriting in shim' -Tag 'Scoop' { + It 'rewrites System32 to Sysnative for x86 shim on x64 OS' { + $sysdir = [System.IO.Path]::Combine($env:SystemRoot, 'System32') + $sysnative = [System.IO.Path]::Combine($env:SystemRoot, 'Sysnative') + $testPath = "$sysdir\notepad.exe" + + if ([System.Environment]::Is64BitOperatingSystem) { + $result = $testPath -replace [regex]::Escape($sysdir), $sysnative + $result | Should -Be "$sysnative\notepad.exe" + } else { + Set-ItResult -Skipped -Because 'not a x64 OS' + } + } + + It 'rewrites SysWOW64 to System32 for x86 shim on x64 OS' { + $sysdir = [System.IO.Path]::Combine($env:SystemRoot, 'System32') + $syswow = [System.IO.Path]::Combine($env:SystemRoot, 'SysWOW64') + $testPath = "$syswow\notepad.exe" + + if ([System.Environment]::Is64BitOperatingSystem) { + $result = $testPath -replace [regex]::Escape($syswow), $sysdir + $result | Should -Be "$sysdir\notepad.exe" + } else { + Set-ItResult -Skipped -Because 'not a x64 OS' + } + } + + It 'does not rewrite paths outside System32 and SysWOW64' { + $sysdir = [System.IO.Path]::Combine($env:SystemRoot, 'System32') + $syswow = [System.IO.Path]::Combine($env:SystemRoot, 'SysWOW64') + $testPath = 'C:\Program Files\test\app.exe' + + $result = $testPath + if ($result -like "$sysdir\*") { + $result = $result -replace [regex]::Escape($sysdir), 'Sysnative' + } elseif ($result -like "$syswow\*") { + $result = $result -replace [regex]::Escape($syswow), $sysdir + } + $result | Should -Be $testPath + } +} + Describe 'Format Architecture String' -Tag 'Scoop' { It 'should keep correct architectures' { Format-ArchitectureString '32bit' | Should -Be '32bit' From 279f7003d86b69ba9f02debfec1f7749cff9c865 Mon Sep 17 00:00:00 2001 From: L <11664880+silver886@users.noreply.github.com> Date: Sat, 14 Mar 2026 04:58:31 -0700 Subject: [PATCH 3/8] add changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0c15ec326..f648795dc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [Unreleased] + +### Bug Fixes + +- **shim:** Fix WoW64 file system redirection for x86 shim executables on x64 OS by rewriting System32/SysWOW64 paths in `.shim` config + ## [v0.5.3](https://github.com/ScoopInstaller/Scoop/compare/v0.5.2...v0.5.3) - 2025-08-11 ### Features From ec1643d664ca1df95f8fc4ce10f83a41b2b42891 Mon Sep 17 00:00:00 2001 From: L <11664880+silver886@users.noreply.github.com> Date: Sat, 14 Mar 2026 05:14:22 -0700 Subject: [PATCH 4/8] Update Scoop-Core.Tests.ps1 --- test/Scoop-Core.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Scoop-Core.Tests.ps1 b/test/Scoop-Core.Tests.ps1 index b3ff7ce3bc..c10b4ffee0 100644 --- a/test/Scoop-Core.Tests.ps1 +++ b/test/Scoop-Core.Tests.ps1 @@ -357,7 +357,7 @@ Describe 'app' -Tag 'Scoop' { } } -Describe 'Get-PEMachine' -Tag 'Scoop' { +Describe 'Get-PEMachine' -Tag 'Scoop', 'Windows' { It 'returns machine type for a valid PE file' { $shim_path = get_shim_path if ($shim_path -and (Test-Path $shim_path)) { From 1f808240a46eca6c57df053bf55fffd7f5551bf9 Mon Sep 17 00:00:00 2001 From: L <11664880+silver886@users.noreply.github.com> Date: Tue, 24 Mar 2026 09:01:54 -0700 Subject: [PATCH 5/8] Fix possible null variable --- lib/core.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 4be5d4d85c..7981089006 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -36,8 +36,8 @@ function Get-PEMachine($filePath) { } catch { return 0 } finally { - $binaryReader.Close() - $fileStream.Close() + if ($null -ne $binaryReader) { $binaryReader.Close() } + if ($null -ne $fileStream) { $fileStream.Close() } } } From a72c1930764af14bf1ec6f69be5286e32d48dc9b Mon Sep 17 00:00:00 2001 From: L <11664880+silver886@users.noreply.github.com> Date: Tue, 24 Mar 2026 09:12:44 -0700 Subject: [PATCH 6/8] Add tag for unit test --- test/Scoop-Core.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Scoop-Core.Tests.ps1 b/test/Scoop-Core.Tests.ps1 index c10b4ffee0..a973221418 100644 --- a/test/Scoop-Core.Tests.ps1 +++ b/test/Scoop-Core.Tests.ps1 @@ -380,7 +380,7 @@ Describe 'Get-PEMachine' -Tag 'Scoop', 'Windows' { } } -Describe 'WoW64 path rewriting in shim' -Tag 'Scoop' { +Describe 'WoW64 path rewriting in shim' -Tag 'Scoop', 'Windows' { It 'rewrites System32 to Sysnative for x86 shim on x64 OS' { $sysdir = [System.IO.Path]::Combine($env:SystemRoot, 'System32') $sysnative = [System.IO.Path]::Combine($env:SystemRoot, 'Sysnative') From bcf137ab902899cd82146bb7bf3c2ee7f291a101 Mon Sep 17 00:00:00 2001 From: L <11664880+silver886@users.noreply.github.com> Date: Tue, 24 Mar 2026 09:29:51 -0700 Subject: [PATCH 7/8] Fix resolving 32-bit path on 64-bit system --- CHANGELOG.md | 3 +-- lib/core.ps1 | 7 ++++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8f1fe6587..09c7a4ee76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,8 +31,7 @@ - **core:** Skip NO_JUNCTION logic when $app is 'scoop' in `currentdir` function ([#6541](https://github.com/ScoopInstaller/Scoop/issues/6541)) - **core:** Fix substitute handling of substring keys ([#6561](https://github.com/ScoopInstaller/Scoop/issues/6561)) - **core:** Check `$deprecated_dir` exists before accessing it ([#6574](https://github.com/ScoopInstaller/Scoop/issues/6574)) -- **checkver:** Remove redundant always-true condition in GitHub checkver logic ([#6571](https://github.com/ScoopInstaller/Scoop/issues/6571)) -- **shim:** Fix WoW64 file system redirection for x86 shim executables on x64 OS by rewriting System32/SysWOW64 paths in `.shim` config ([#6619](https://github.com/ScoopInstaller/Scoop/issues/6619)) +- **checkver:** Remove redundant always-true condition in GitHub checkver logic ([#6571](https://github.com/ScoopInstaller/Scoop/issue- **shim:** Fix WoW64 file system redirection for x86 shim executables on x64 OS by rewriting System32/SysWOW64 paths in `.shim` config ([#6619](https://github.com/ScoopInstaller/Scoop/issues/6619)) ### Code Refactoring diff --git a/lib/core.ps1 b/lib/core.ps1 index 7981089006..b8915e6914 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -887,7 +887,12 @@ function Get-ShimTarget($ShimPath) { if (!$shimTarget) { $shimTarget = ((Select-String -Path $ShimPath -Pattern '[''"]([^@&]*?)[''"]' -AllMatches).Matches.Groups | Select-Object -Last 1).Value } - $shimTarget | Convert-Path -ErrorAction SilentlyContinue + $shimTargetConverted = $shimTarget | Convert-Path -ErrorAction SilentlyContinue + if (!$shimTargetConverted -and $shimTarget.Contains('\Sysnative\')) { + $shimTarget.Replace('\Sysnative\', '\System32\') | Convert-Path -ErrorAction SilentlyContinue + } else { + return $shimTargetConverted + } } } From 61dc3b92575125528f2ce06622c8c3dc413f1c7b Mon Sep 17 00:00:00 2001 From: L <11664880+silver886@users.noreply.github.com> Date: Tue, 24 Mar 2026 09:32:08 -0700 Subject: [PATCH 8/8] Revert changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09c7a4ee76..b8f1fe6587 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,8 @@ - **core:** Skip NO_JUNCTION logic when $app is 'scoop' in `currentdir` function ([#6541](https://github.com/ScoopInstaller/Scoop/issues/6541)) - **core:** Fix substitute handling of substring keys ([#6561](https://github.com/ScoopInstaller/Scoop/issues/6561)) - **core:** Check `$deprecated_dir` exists before accessing it ([#6574](https://github.com/ScoopInstaller/Scoop/issues/6574)) -- **checkver:** Remove redundant always-true condition in GitHub checkver logic ([#6571](https://github.com/ScoopInstaller/Scoop/issue- **shim:** Fix WoW64 file system redirection for x86 shim executables on x64 OS by rewriting System32/SysWOW64 paths in `.shim` config ([#6619](https://github.com/ScoopInstaller/Scoop/issues/6619)) +- **checkver:** Remove redundant always-true condition in GitHub checkver logic ([#6571](https://github.com/ScoopInstaller/Scoop/issues/6571)) +- **shim:** Fix WoW64 file system redirection for x86 shim executables on x64 OS by rewriting System32/SysWOW64 paths in `.shim` config ([#6619](https://github.com/ScoopInstaller/Scoop/issues/6619)) ### Code Refactoring