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

Prefer32Bit flag produces invalid output assembly in .NET 7 #70776

Closed
mike-ward opened this issue Jun 15, 2022 · 8 comments
Closed

Prefer32Bit flag produces invalid output assembly in .NET 7 #70776

mike-ward opened this issue Jun 15, 2022 · 8 comments
Assignees
Milestone

Comments

@mike-ward
Copy link

mike-ward commented Jun 15, 2022

Description

Project builds and runs using .NET 6.0 Switched to .NET 7 - Preview 5. App builds with no issues but when I run it, it immediately exits.

The debug console has the follow message.

Loaded application domain 'DefaultDomain (id 1)'
Loaded Assembly 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll'
Loading module C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll in application domain 1:DefaultDomain
Pdb file for assembly C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll was not found or failed to read
Started Thread 18556

Reproduction Steps

Expected behavior

Program should display the initial view asking for PIN number.

Actual behavior

Program immediately exits. Debug console has the following message.

Loaded application domain 'DefaultDomain (id 1)'
Loaded Assembly 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll'
Loading module C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll in application domain 1:DefaultDomain
Pdb file for assembly C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll was not found or failed to read
Started Thread 18556

Regression?

Yes, this works in .NET 6.0. The main branch of the project is built using .NET 6.0

Known Workarounds

None

Configuration

  • .NET 7.0
  • Windows 10 [Version 10.0.19043.1165]
  • x86
  • appears not to be specific to debug/release configuration

Other information

This project uses the Avalonia UI framework.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jun 15, 2022
@ghost
Copy link

ghost commented Jun 15, 2022

Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Project builds and runs using .NET 6.0 Switched to .NET 7 - Preview 5. App builds with no issues but when I run it, it immediately exits.

The debug console has the follow message.

Loaded application domain 'DefaultDomain (id 1)'
Loaded Assembly 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll'
Loading module C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll in application domain 1:DefaultDomain
Pdb file for assembly C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll was not found or failed to read
Started Thread 18556

Reproduction Steps

Expected behavior

Program should display the initial view asking for PIN number.

Actual behavior

Program immediately exits. Debug console has the following message.

Loaded application domain 'DefaultDomain (id 1)'
Loaded Assembly 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll'
Loading module C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll in application domain 1:DefaultDomain
Pdb file for assembly C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-preview.4.22229.4\System.Private.CoreLib.dll was not found or failed to read
Started Thread 18556

Regression?

Yes, this works in .NET 6.0. The main branch of the project is built using .NET 6.0

Known Workarounds

None

Configuration

  • .NET 7.0
  • Windows 10 [Version 10.0.19043.1165]
  • x86
  • appears not to be specific to debug/release configuration

Other information

This project uses the Avalonia UI framework.

Author: mike-ward
Assignees: -
Labels:

area-AssemblyLoader-coreclr

Milestone: -

@vitek-karas
Copy link
Member

This has the same root cause as #64255.

How to diagnose:
Since this is a UI app, it won't print out anything to the console (Windows behaves this way, even if the app itself writes to stdout or stderr).
The error is also written to Event Log though, so simply open event log and look for recent ".NET Runtime" events. In this case I got this:

Exception Info: System.BadImageFormatException: Could not load file or assembly 'C:\Repro\Loon\src\Loon.Desktop\bin\Debug\net7.0\Loon.dll'. Format of the executable (.exe) or library (.dll) is invalid.

This typically refers to bitness mismatch - that is trying to load for example 32bit assembly into a 64bit process.

In this case the Loon.Desktop project sets <Prefer32Bit>true</Prefer32Bit>. This marks the produces assembly as 32bit (preferred). You can read more details in #64255 (comment). In short, runtime itself ignores this setting and in .NET it has no meaning really, but Windows still reacts to it and there's an unfortunate behavior which cases the failure.

As noted in #64255 we don't plan to fix he runtime to handle this case (the complexity doesn't seem to be worth the trouble).

So the simple fix is to remove the Prefer32Bit property - then it works (verified). As noted above, it has no meaning for .NET, it only worked on .NET Framework.

@vitek-karas
Copy link
Member

@agocke @VSadov : Since this is a second time it showed up I looked into this a little bit more from an "end user" point of view. A trivial repro is (using .NET 7 Preview 5):

dotnet new console
dotnet build /p:Prefer32Bit=true
.\bin\Debug\net7.0\app.exe

This produces output like:

Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'C:\Repro\p32\bin\Debug\net7.0\p32.dll'. Format of the executable (.exe) or library (.dll) is invalid.

Given that it's really easy to hit this and that it is likely to affect apps migrated from .NET Framework (which might have this property set), I think we should consider doing something about this.

Personally I still prefer to not fix the runtime, but instead make a change in SDK and generate a warning that Prefer32Bit is not supported anymore and may break the app (for TFM 7.0). Thoughts?

@agocke
Copy link
Member

agocke commented Jun 15, 2022

Personally I still prefer to not fix the runtime, but instead make a change in SDK and generate a warning that Prefer32Bit is not supported anymore and may break the app (for TFM 7.0). Thoughts?

Agreed. LGTM

@janvorli janvorli added this to the 7.0.0 milestone Jun 15, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jun 15, 2022
@agocke agocke added this to AppModel Jun 27, 2022
@agocke agocke changed the title System.Private.CoreLib.dll not found in .NET 7 Preview 5 Prefer32Bit flag produces invalid output assembly in .NET 7 Jul 26, 2022
@agocke
Copy link
Member

agocke commented Jul 26, 2022

@VSadov Could you add an error to the SDK for this? And file a breaking change issue?

@VSadov
Copy link
Member

VSadov commented Aug 19, 2022

Right. NT loader does not like it when a Prefer32Bit is applied to a PE32 dll and would not load it in a 64bit process.
We should ignore the setting and produce a build warning.

@hoyosjs
Copy link
Member

hoyosjs commented Sep 12, 2022

@VSadov should this be closed?

@VSadov
Copy link
Member

VSadov commented Sep 12, 2022

yes, dotnet/sdk#27796 has just merged

@VSadov VSadov closed this as completed Sep 12, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Oct 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

No branches or pull requests

6 participants