Skip to content

[dotnet-sdk-7.0.100-alpha.1.22057.7]MaterialDesignInXamlToolkit failed to launch with BadImageFormat exception #64255

Closed

Description

@Junjun-zhao

Application Name: MaterialDesignInXamlToolkit
OS: Windows RS5
​CPU: X64
.NET Build Number: dotnet-sdk-7.0.100-alpha.1.22057.7

Verify Scenarios:
1). Windows10 RS5 x64+dotnet-sdk-7.0.100-alpha.1.22057.7:Fail
2). Windows10 RS5 x64+dotnet-sdk-7.0-alpha.1.21568.2:Pass
3). Windows10 RS5 x64+dotnet-sdk-3.1.121-win-x64:Pass
4). Windows10 RS5 x64+dotnet-sdk-5.0.404-win-x64:Pass
5). Windows10 RS5 x64+dotnet-sdk-7.0.100-alpha.1.22060.9:Fail

Github Repo link:
https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit

App Source and App Checking at: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1460702

Repro steps:
This is .NET Core 3.1 app,but we are running it on 7.0, machine only have 7.0 installed, and DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX=2, so it runs on 7.0

  1. Copy all files from \MaterialDesignInXamlToolkitApp
  2. to C:\Appcompat\applications\MaterialDesignInXamlToolKit folder.
  3. Launch MaterialDesignDemo.exe.

Expected Result:
MaterialDesignDemo.exe launched successfully.

Actual Result:
Nothing happens, app can't launch.

Exception Details:
Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'C:\Users\v-zhiyang\Desktop\Debug\netcoreapp3.1\MaterialDesignDemo.dll'. Format of the executable (.exe) or library (.dll) is invalid.
File name: 'C:\Users\v-zhiyang\Desktop\Debug\netcoreapp3.1\MaterialDesignDemo.dll'

Minimal Repro steps:
1.Create a .Net Core 3.1 WPF application named Test.
2.set Prefer32Bit to true in csproj, such as :
Prefer32Bit

3.Build the project.
4.Change the Test.runtimeconfig.json under Test\bin\Debug\netcoreapp3.1 as follow, to let it run with sdk-7.0.100-alpha.1.22057.7.

{
  "runtimeOptions": {
    "tfm": "netcoreapp3.1",
    "framework": {
      "name": "Microsoft.WindowsDesktop.App",
      "version": "7.0.0-alpha.1.22055.1"
    }
  }
}

5.Run the Test.exe from VS with F5.

Expected Result:
Test.exe launch successfully.

Actual Result:
Error shown in the figure:
actual result

@dotnet-actwx-bot @dotnet/compat

Activity

  1. Repository owner unassigned on Jan 24, 2022
  2. transferred this issue fromon Jan 24, 2022
  3. transferred this issue fromon Jan 24, 2022
  4. ThomasGoulet73 commented on Jan 25, 2022

    @ThomasGoulet73
    Contributor

    @Junjun-zhao This seems to be unrelated to WPF. I am able to reproduce this in a console application. This issue is only reproducible when Prefer32Bit is set to true.

    Content of ConsoleApp1.csproj:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <Prefer32Bit>true</Prefer32Bit>
      </PropertyGroup>
    
    </Project>
    

    Content of ConsoleApp1.runtimeconfig.json:

    {
      "rollForwardOnNoCandidateFx" : 2,
      "runtimeOptions": {
        "tfm": "netcoreapp3.1",
        "framework": {
          "name": "Microsoft.NETCore.App",
          "version": "7.0.0-alpha.1.22055.1"
        }
      }
    }
    

    This issue should probably be transfered to dotnet/runtime.

  5. Junjun-zhao commented on Jan 25, 2022

    @Junjun-zhao
    MemberAuthor

    @Junjun-zhao This seems to be unrelated to WPF. I am able to reproduce this in a console application. This issue is only reproducible when Prefer32Bit is set to true.
    This issue should probably be transfered to dotnet/runtime.

    @ThomasGoulet73 Thanks for helping look into this issue. Could you please help move it to dotnet/runtime as I don't have the permission for this operation. Thanks.

  6. ThomasGoulet73 commented on Jan 25, 2022

    @ThomasGoulet73
    Contributor

    I don't have the permission to transfer an issue either. Tagging @gurpreet-wpf who transfered a similar issue to dotnet/runtime a couple of days ago.

  7. transferred this issue fromdotnet/wpfon Jan 25, 2022
  8. ghost added
    untriaged
    on Jan 25, 2022
  9. vitek-karas commented on Jan 25, 2022

    @vitek-karas
    Member

    First I wanted to note that Prefer32Bit has no material effect on .NET Core applications. The bitness of the process is not determined by this, it's determined by the host which is used to launch the app. If it's dotnet.exe, then it's up to the caller to pick the right one. If it's the app.exe then it depends on the RID used to build the app (if none is specified, SDK will use the RID of the SDK itself). The managed assembly will carry the Prefer32Bit setting in it, but the runtime doesn't really pay attention to it.

    I verified that this works on .NET 6 just fine, but on 7 it's broken.

    I debugged into this a little bit:

    • Resetting the Prefer32Bit to 0 via corflags fixes the problem (it doesn't fail anymore)
    • The failure is coming from LoadLibrary directly - we simply propagate it.
    • The assembly is an x86 PE image, so normally it should not load into a 64bit process. Interestingly if the Prefer32Bit is not set, LoadLibrary works on it (I assume this is something from .NET Framework interfering). If the Prefer32Bit is set, LoadLibrary fails.
    • We go through the PEImage::GetOrCreateLayoutInternal twice in the failure case.
      • First time it's called from the binder - in this case the layout mask is LAYOUT_ANY. In this case it first calls CreateLoadedLayout which fails (due to LoadLibrary), but since any layout is permissible we go and try again with CreateFlatLayout which succeeds.
      • Second time it's called from PEAssembly::EnsureLoaded and this time it's explicitly with LAYOUT_LOADED. Which fails again.

    This was almost certainly introduced by #61938. Before this change this assembly returned GetPEImage()->IsFile() == true and it's also ILOnly so it ended up with LoadFromMapped(). After this change we force load it via LAYOUT_LOADED. This should work for all assemblies for which LoadLibrary works, but apparently that's not the case for Prefer32Bit assemblies.

    Given that Prefer32Bit is basically useless for .NET Core there's also the question if we should fix this at all. It IS a regression though.

  10. vitek-karas commented on Feb 15, 2022

    @vitek-karas
    Member

    @VSadov @agocke - any opinions on the necessity to fix this? I think it's worth a couple of hours to see if this is easy to fix. If the fix is really complicated (given the new way we load assemblies) then maybe we could reconsider. Technically this has the potential of breaking apps which use .NET Framework assemblies - not fully supported, but they exist.

  11. agocke commented on Feb 15, 2022

    @agocke
    Member

    Fascinating bug -- what's the cost for the fix? I'm not interested in heroics, but I would generally say that I would expect Prefer32Bit to have no effect on loading an ILONLY binary, so I'd re-enable it if it's cheap.

  12. vitek-karas commented on Feb 16, 2022

    @vitek-karas
    Member

    I strongly suspect this to be something with .NET Framework OS loader being involved - LoadLibrary itself reacts to this flag. The runtime behavior difference is that previously we tried LoadLibrary and if it failed, we mapped it ourselves, now we don't have a fallback, if LoadLibrary fails, we fail as well.

  13. VSadov commented on Feb 16, 2022

    @VSadov
    Member

    There was a concern that having LAYOUT_LOADED fallback after OS failed to load may create a potential support burden for ever increasing set of PE files broken from the OS point of view, but loadable via fallback if it happens to be less strict about the file format.
    It looks like the set is not empty already.

    This can be fixed by reintroducing fallback.

  14. Junjun-zhao commented on Mar 30, 2022

    @Junjun-zhao
    MemberAuthor

    @vitek-karas, @agocke, @VSadov Is there any update for this bug? Will it be fixed? We checked this issue and it still repro on dotnet-sdk-7.0.100-preview.3.22179.4

  15. vitek-karas commented on Mar 30, 2022

    @vitek-karas
    Member

    Personally I'm inclined to not fix this. Setting the Prefer32Bit when targeting .NET Core or .NET 5+ is something we should probably disable in the SDK (maybe generate a warning), but in any case it should not be used as it doesn't have the desired effect. If the assembly is targeting .NET Framework such bit is only effective on .exe, and trying to load .NET Framework .exe as an assembly into .NET Core/.NET 5+ is very rare and still effectively unsupported (there are many other things which will not work).
    If this is a .dll targeting .NET Framework, the bit is ineffective even in .NET Framework (or rather, .NET Framework would have the same behavior as .NET 7 has now) - it won't "change" the bitness of the process anyway - so there's no point in using it.

    As @VSadov mentioned above, supporting this adds non-trivial burned on the runtime going forward - it just doesn't seem worth the cost in my opinion.

  16. agocke commented on Mar 30, 2022

    @agocke
    Member

    I agree with Vitek and Vlad. Won't fix.

  17. ghost locked as resolved and limited conversation to collaborators on Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Hostuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions