Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7.3.0 x86 crashes on startup on Windows 11 22H2 #18545

Closed
5 tasks done
chrullrich opened this issue Nov 13, 2022 · 20 comments · Fixed by #18547
Closed
5 tasks done

7.3.0 x86 crashes on startup on Windows 11 22H2 #18545

chrullrich opened this issue Nov 13, 2022 · 20 comments · Fixed by #18547
Labels

Comments

@chrullrich
Copy link
Contributor

chrullrich commented Nov 13, 2022

Prerequisites

Steps to reproduce

  1. Install PowerShell 7.3.0 x86 on a Windows x64 system.
  2. Start pwsh.exe.

No previous version of PowerShell 7 behaved in this way.

Expected behavior

PowerShell 7.3.0
PS C:\Users\Me>

Actual behavior

PowerShell 7.3.0
Fatal error. 0xC0000005
   at System.Management.Automation.Security.SystemPolicy+WldpNativeMethods.WldpCanExecuteFile(System.Guid, WLDP_EXECUTION_EVALUATION_OPTIONS, IntPtr, System.String, WLDP_EXECUTION_POLICY ByRef)
   at System.Management.Automation.Security.SystemPolicy.GetFilePolicyEnforcement(System.String, System.IO.FileStream)
   at System.Management.Automation.ExternalScriptInfo.ReadScriptContents()
   at System.Management.Automation.ExternalScriptInfo.get_ScriptBlock()
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifestData(System.Management.Automation.ExternalScriptInfo, System.String[], ManifestProcessingFlags, Boolean ByRef)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifestData(System.Management.Automation.ExternalScriptInfo, ManifestProcessingFlags, System.Collections.Hashtable ByRef, System.Collections.Hashtable ByRef, Boolean ByRef)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(System.Management.Automation.PSModuleInfo, System.String, System.String, System.String, System.Management.Automation.SessionState, System.Object, ImportModuleOptions ByRef, ManifestProcessingFlags, Boolean ByRef, Boolean ByRef)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingExtensions(System.Management.Automation.PSModuleInfo, System.String, System.String, System.String, System.String, System.String, System.Management.Automation.SessionState, ImportModuleOptions, ManifestProcessingFlags, Boolean ByRef, Boolean ByRef)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingModulePath(System.Management.Automation.PSModuleInfo, System.Collections.Generic.IEnumerable`1<System.String>, System.String, System.Management.Automation.SessionState, ImportModuleOptions, ManifestProcessingFlags, System.Management.Automation.PSModuleInfo ByRef)
   at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName(ImportModuleOptions, System.String)
   at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName_WithTelemetry(ImportModuleOptions, System.String)
   at Microsoft.PowerShell.Commands.ImportModuleCommand.ProcessRecord()
   at System.Management.Automation.Cmdlet.DoProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
   at System.Management.Automation.CommandProcessorBase.DoExecute()
   at System.Management.Automation.Internal.PipelineProcessor.Inject(System.Object, Boolean)
   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(System.Object)
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProcImpersonate()
   at System.Management.Automation.Runspaces.PipelineThread.WorkerProc()
   at System.Threading.Thread+StartHelper.Callback(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.Thread.StartCallback()

Error details

No response

Environment data

Not available because PowerShell crashes on startup.

Visuals

No response

@chrullrich chrullrich added the Needs-Triage The issue is new and needs to be triaged by a work group. label Nov 13, 2022
@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

I have looked for a statement on whether using the x86 version of PS on an x64 platform is supported, but could not find any.

@rhubarb-geek-nz
Copy link

rhubarb-geek-nz commented Nov 13, 2022

A few questions..

  • What operating system did you install it on?
  • How did you install it, ZIP, MSI or some other way?
  • If using the MSI did you add it to the global PATH?
  • Do you also have x64 version installed?
  • How did you start pwsh.exe, from old powershell, from start menu or from 32bit or 64bit cmd.exe?

I have just installed the PowerShell-7.3.0-win-x86.msi on Windows 10 Pro 22H2 and that installs in "C:\\Program Files (x86)\\PowerShell\7". That system also has the 64 bit version installed.

I did not check the box to add to the global PATH, or any other option.

I ran it from 64 bit cmd.exe after cd into "C:\\Program Files (x86)\\PowerShell\7" , and it started cleanly and now sits waiting at the prompt.

@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

Sometimes I'm too impatient when writing bug reports.

  • Windows 11 22H2 x64, current updates
  • PowerShell-7.3.0-win-x86.zip
  • N/A
  • No
  • It crashes from 64-bit and 32-bit cmd, from 64-bit and 32-bit WPS 5.1.22621.608, and when run from Explorer, both by double-click and Run.

Reproduction:

  1. Have no version of PowerShell 7 installed (may not be necessary).
  2. Extract PowerShell-7.3.0-win-x86.zip .
  3. Open a command prompt window (i.e. tab in Terminal in 11 22H2 with October updates).
  4. cd to the directory just extracted.
  5. Run pwsh.exe.

The crash happens both with my regular cluttered PATH and with PATH=C:\windows\system32;C:\windows, and when starting it from another directory with an absolute path on the command line. It also happens with the MSI installed the same way you describe (same path, same no options), both from command prompt and start menu. Installing the MSI had no effect on the behavior of the copy I unzipped earlier. It also happens when run from an elevated "real" command prompt window.

I noticed that the delay, both before the first line of output and between the last line of the stack trace and the return of the prompt, is much shorter with the MSI (<< 1 s start, 1.5 s finish) than with the Zip (~2.5 s start, ~6 s finish).

Hope that's better :-)

@rhubarb-geek-nz
Copy link

rhubarb-geek-nz commented Nov 13, 2022

Does the 64-bit version from a zip work on your system? Given the CPU requirements for Windows 11, are there any x86 only Windows 11 installations?

@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

  • 64-bit from Zip works.
  • No idea, but since I'm not aware of any x86 Windows 11 release, I rather doubt it.

@rhubarb-geek-nz
Copy link

rhubarb-geek-nz commented Nov 13, 2022

I just tried the 7.3.0 x86 zip on my Windows 11 Pro 22H2 and that throws exactly the same exception as you describe.

@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

The function it crashes in only exists in 11 22H2. That at least explains why it works on 10. Procmon shows no obvious smoking gun, not that I expected one.

@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

If I had to guess, I would guess that https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/security/wldpNativeMethods.cs#L699 should be "ref Guid". But what do I know?

GitHub
PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.

@rhubarb-geek-nz
Copy link

rhubarb-geek-nz commented Nov 13, 2022

https://learn.microsoft.com/en-us/previous-versions/troubleshoot/visualstudio/foxpro/c0000005-error-troubleshoot

https://learn.microsoft.com/en-us/windows/win32/api/wldp/nf-wldp-wldpcanexecutefile

Does the export WldpCanExecuteFile exist in the x86 wldp.dll?

This article defines a C0000005 error and offers troubleshooting suggestions.
Queries whether the execution policy allows execution of the code in the supplied file.

@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

C:\Windows\SysWOW64>dumpbin /exports wldp.dll | findstr WldpCanExecuteFile
         17    2 000237A0 WldpCanExecuteFile

The code at the call site (line 120 of the same file I linked above; I hope it's the right one) catches EntryPointNotFoundException and continues with "legacy system checks".

@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

Adding [MarshalAs(UnmanagedType.LPStruct)] to the Guid argument (ref does not work here) fixes the crash when building the v7.3.0 tag. I'm getting unrelated compiler errors from the master branch. Whether this is the correct fix is beyond me.

I can create a PR for adding the attribute, but I cannot be held responsible for it ...

@rhubarb-geek-nz
Copy link

rhubarb-geek-nz commented Nov 13, 2022

So how does the 64 bit code work if it goes through the same code path?

@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

Calling conventions.

Per wldp.h the first argument is a REFGUID, also known as a GUID*. The Windows x64 calling convention says that values larger than 64 bits (that is, larger than a register) are passed by address (i.e. as pointers). This means that in C# terms, Guid and ref Guid look the same to the called native function (but I think in the non-ref case the Guid is probably copied somewhere in case the native function changes it).

The x86 calling conventions, in this case __stdcall, are more complicated. Here, structures (such as Guid) are passed on the stack, that is, the compiler copies the structure to the stack instead of just pushing a pointer to it. This also means that the function will mistake the last four bytes of the GUID as a pointer to one, dereference it, and ... boom. I tried it from C, and this is exactly what happens.

My fix tells .NET to pass the Guid as a pointer to a structure, and the fact that this changes the behavior at all is the best proof that it is correct: If it works when passing a pointer, then it must not have been passing one before, and that is definitely wrong because we know the function expects a REFGUID.

Also note that a few lines down in the file, the declaration of SHGetKnownFolderPath() already uses the attribute, and that function's first argument is a REFKNOWNFOLDERID, another alternative spelling of GUID*.

Now that I have convinced myself of that (and you, I hope), my next problem is how to create a PR for the fix if I cannot get master to build for testing.

For illustration, this is a call to WldpCanExecuteFile(REFGUID, int, int, int, int) (the exact types don't matter):

push	0
push	0
push	0
push	0
push	OFFSET ?guid@?1??main@@9@9
call	_WldpCanExecuteFile

This is WldpCanExecuteFile(GUID, 0, 0, 0, 0):

push	0
push	0
push	0
push	0
sub	esp, 16
mov	eax, esp
mov	ecx, DWORD PTR ?guid@?1??main@@9@9
mov	DWORD PTR [eax], ecx
mov	edx, DWORD PTR ?guid@?1??main@@9@9+4
mov	DWORD PTR [eax+4], edx
mov	ecx, DWORD PTR ?guid@?1??main@@9@9+8
mov	DWORD PTR [eax+8], ecx
mov	edx, DWORD PTR ?guid@?1??main@@9@9+12
mov	DWORD PTR [eax+12], edx
call	_WldpCanExecuteFile

@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

I just found that master works on another system (no idea why). There, Start-PSBuild -Runtime win7-x86 -Configuration Debug itself fails with the same error when it calls its own just-built pwsh.exe somewhere along the way. This probably just means that the "official" release was created on something other than 11 22H2.

chrullrich pushed a commit to chrullrich/PowerShell that referenced this issue Nov 13, 2022
The error occurs only when running x86 pwsh on x64 Windows 11 22H2, the
first Windows version that has WldpCanExecuteFile().
@chrullrich chrullrich changed the title 7.3.0 x86 crashes on startup on Windows x64 7.3.0 x86 crashes on startup on Windows 11 22H2 Nov 13, 2022
@sergeevabc
Copy link

sergeevabc commented Nov 13, 2022

@chrullrich, my condolences. 7.3.0 crashes upon launch on your end, whereas its x64 zip bundle hangs my Windows 7 x64 system completely, until it is rebooted via hard reset (literally holding that reset button for a few seconds). I was advised to try -NoProfile switch, no luck there. No such thing happens with 7.2.5, so I rolled back.

@rhubarb-geek-nz
Copy link

rhubarb-geek-nz commented Nov 13, 2022

@chrullrich, No such thing happens with 7.2.5, so I rolled back.

Does 7.2.7 work?

@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

Yes, 7.2.7 works. It does not contain this particular bug anyway, that was introduced between v7.3.0-preview.3 and .4 according to git.

@sergeevabc
Copy link

sergeevabc commented Nov 13, 2022

@rhubarb-geek-nz, not sure if you asked me or Christian, so let me reply as well.
Yes, 7.2.7 launches fine. Upgraded from 7.2.5 successfully. Thanks for mentioning it.

Edit.
However I am not happy as it advises me to upgrade to 7.3.0 on every launch.
Too intrusive, not to mention that very build is not that stable after all.

C:\APPS\System\PowerShell727>pwsh
PowerShell 7.2.7
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

   A new PowerShell stable release is available: v7.3.0
   Upgrade now, or check out the release page at:
     https://aka.ms/PowerShell-Release?tag=v7.3.0

PS C:\APPS\System\PowerShell727>

@chrullrich
Copy link
Contributor Author

chrullrich commented Nov 13, 2022

@sergeevabc It can be turned off by setting an environment variable: POWERSHELL_UPDATECHECK=Off

Source: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_update_notifications?view=powershell-7.2

Notifies users on startup of PowerShell that a new version of PowerShell has been released.

@iSazonov
Copy link
Collaborator

iSazonov commented Nov 14, 2022

This probably just means that the "official" release was created on something other than 11 22H2.

It is Windows 10.

@msftbot msftbot bot added the In-PR Indicates that a PR is out for the issue label Nov 14, 2022
@msftbot msftbot bot added Resolution-Fixed The issue is fixed. and removed In-PR Indicates that a PR is out for the issue labels Nov 14, 2022
@msftbot msftbot bot removed the Needs-Triage The issue is new and needs to be triaged by a work group. label Nov 14, 2022
PaulHigin pushed a commit that referenced this issue Nov 14, 2022
The error occurs only when running x86 pwsh on x64 Windows 11 22H2, the
first Windows version that has WldpCanExecuteFile().

Co-authored-by: Christian Ullrich <christian.ullrich@traditionsa.lu>
github-actions bot pushed a commit that referenced this issue Nov 15, 2022
The error occurs only when running x86 pwsh on x64 Windows 11 22H2, the
first Windows version that has WldpCanExecuteFile().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants