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

Selected apphost is not a valid PE file on s390x #92344

Closed
omajid opened this issue Sep 20, 2023 · 7 comments
Closed

Selected apphost is not a valid PE file on s390x #92344

omajid opened this issue Sep 20, 2023 · 7 comments
Labels
arch-s390x Related to s390x architecture (unsupported) area-HostModel Microsoft.NET.HostModel issues
Milestone

Comments

@omajid
Copy link
Member

omajid commented Sep 20, 2023

Using a .NET 8 RC 1 SDK compiled on s390x with #90292 applied locally, dotnet tool install -g dotnet-ef fails:

    $ dotnet tool install -g dotnet-ef
    Unhandled exception: Microsoft.NET.HostModel.AppHost.AppHostNotPEFileException: Selected apphost is not a valid PE file. Subsystem offset out of file range.
       at Microsoft.NET.HostModel.AppHost.PEUtils.GetWindowsGraphicalUserInterfaceBit(MemoryMappedViewAccessor accessor)
       at Microsoft.NET.HostModel.AppHost.PEUtils.GetWindowsGraphicalUserInterfaceBit(String filePath)
       at Microsoft.DotNet.ShellShim.AppHostShellShimMaker.CreateApphostShellShim(FilePath entryPoint, FilePath shimPath)
       at Microsoft.DotNet.ShellShim.ShellShimRepository.<>c__DisplayClass5_0.<CreateShim>b__0()
       at Microsoft.DotNet.Cli.TransactionalAction.<>c__DisplayClass5_0.<Run>b__0()
       at Microsoft.DotNet.Cli.TransactionalAction.Run[Object](Func`1 action, Action commit, Action rollback)
       at Microsoft.DotNet.Cli.TransactionalAction.Run(Action action, Action commit, Action rollback)
       at Microsoft.DotNet.ShellShim.ShellShimRepository.CreateShim(FilePath targetExecutablePath, ToolCommandName commandName, IReadOnlyList`1 packagedShims)
       at Microsoft.DotNet.Tools.Tool.Install.ToolInstallGlobalOrToolPathCommand.Execute()
       at Microsoft.DotNet.Tools.Tool.Install.ToolInstallCommand.Execute()
       at Microsoft.DotNet.Cli.ToolInstallCommandParser.<>c.<ConstructCommand>b__17_0(ParseResult parseResult)
       at System.CommandLine.Invocation.AnonymousCliAction.Invoke(ParseResult parseResult)
       at System.CommandLine.Invocation.InvocationPipeline.Invoke(ParseResult parseResult)
       at System.CommandLine.ParseResult.Invoke()
       at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, TimeSpan startupTime, ITelemetry telemetryClient)

The stack trace suggests this is the culprit:

internal static unsafe ushort GetWindowsGraphicalUserInterfaceBit(MemoryMappedViewAccessor accessor)
{
byte* pointer = null;
try
{
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pointer);
byte* bytes = pointer + accessor.PointerOffset;
// https://en.wikipedia.org/wiki/Portable_Executable
uint peHeaderOffset = ((uint*)(bytes + PEOffsets.DosStub.PESignatureOffset))[0];
if (accessor.Capacity < peHeaderOffset + PEOffsets.PEHeader.Subsystem + sizeof(ushort))
{
throw new AppHostNotPEFileException("Subsystem offset out of file range.");
}
ushort* subsystem = ((ushort*)(bytes + peHeaderOffset + PEOffsets.PEHeader.Subsystem));
return subsystem[0];
}
finally
{
if (pointer != null)
{
accessor.SafeMemoryMappedViewHandle.ReleasePointer();
}
}
}

Probably an endianness issue?

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Sep 20, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 20, 2023
@omajid
Copy link
Member Author

omajid commented Sep 20, 2023

cc @uweigand @Vishwanatha-HD

@jkotas jkotas changed the title Selected apphsot is not a valid PE file on s390x Selected apphost is not a valid PE file on s390x Sep 20, 2023
@jkotas jkotas added arch-s390x Related to s390x architecture (unsupported) area-HostModel Microsoft.NET.HostModel issues and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Sep 20, 2023
@ghost
Copy link

ghost commented Sep 20, 2023

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

Issue Details

Using a .NET 8 RC 1 SDK compiled on s390x with #90292 applied locally, dotnet tool install -g dotnet-ef fails:

    $ dotnet tool install -g dotnet-ef
    Unhandled exception: Microsoft.NET.HostModel.AppHost.AppHostNotPEFileException: Selected apphost is not a valid PE file. Subsystem offset out of file range.
       at Microsoft.NET.HostModel.AppHost.PEUtils.GetWindowsGraphicalUserInterfaceBit(MemoryMappedViewAccessor accessor)
       at Microsoft.NET.HostModel.AppHost.PEUtils.GetWindowsGraphicalUserInterfaceBit(String filePath)
       at Microsoft.DotNet.ShellShim.AppHostShellShimMaker.CreateApphostShellShim(FilePath entryPoint, FilePath shimPath)
       at Microsoft.DotNet.ShellShim.ShellShimRepository.<>c__DisplayClass5_0.<CreateShim>b__0()
       at Microsoft.DotNet.Cli.TransactionalAction.<>c__DisplayClass5_0.<Run>b__0()
       at Microsoft.DotNet.Cli.TransactionalAction.Run[Object](Func`1 action, Action commit, Action rollback)
       at Microsoft.DotNet.Cli.TransactionalAction.Run(Action action, Action commit, Action rollback)
       at Microsoft.DotNet.ShellShim.ShellShimRepository.CreateShim(FilePath targetExecutablePath, ToolCommandName commandName, IReadOnlyList`1 packagedShims)
       at Microsoft.DotNet.Tools.Tool.Install.ToolInstallGlobalOrToolPathCommand.Execute()
       at Microsoft.DotNet.Tools.Tool.Install.ToolInstallCommand.Execute()
       at Microsoft.DotNet.Cli.ToolInstallCommandParser.<>c.<ConstructCommand>b__17_0(ParseResult parseResult)
       at System.CommandLine.Invocation.AnonymousCliAction.Invoke(ParseResult parseResult)
       at System.CommandLine.Invocation.InvocationPipeline.Invoke(ParseResult parseResult)
       at System.CommandLine.ParseResult.Invoke()
       at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, TimeSpan startupTime, ITelemetry telemetryClient)

The stack trace suggests this is the culprit:

internal static unsafe ushort GetWindowsGraphicalUserInterfaceBit(MemoryMappedViewAccessor accessor)
{
byte* pointer = null;
try
{
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pointer);
byte* bytes = pointer + accessor.PointerOffset;
// https://en.wikipedia.org/wiki/Portable_Executable
uint peHeaderOffset = ((uint*)(bytes + PEOffsets.DosStub.PESignatureOffset))[0];
if (accessor.Capacity < peHeaderOffset + PEOffsets.PEHeader.Subsystem + sizeof(ushort))
{
throw new AppHostNotPEFileException("Subsystem offset out of file range.");
}
ushort* subsystem = ((ushort*)(bytes + peHeaderOffset + PEOffsets.PEHeader.Subsystem));
return subsystem[0];
}
finally
{
if (pointer != null)
{
accessor.SafeMemoryMappedViewHandle.ReleasePointer();
}
}
}

Probably an endianness issue?

Author: omajid
Assignees: -
Labels:

area-HostModel, untriaged, arch-s390x, needs-area-label

Milestone: -

@agocke
Copy link
Member

agocke commented Sep 20, 2023

Cc @elinor-fung . Looks like endianness mismatch in the new resource writing code.

@agocke agocke added this to the 8.0.0 milestone Sep 20, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Sep 20, 2023
@agocke agocke added this to AppModel Sep 20, 2023
@elinor-fung
Copy link
Member

Taking a look. It's just in GetWindowsGraphicalUserInterfaceBit, not at the point of trying to write resources yet - I don't think that was changed (constants were just moved to a shared place).

@agocke
Copy link
Member

agocke commented Sep 20, 2023

Yeah, that uint* cast looks pretty suspicious -- that'll be reversed on big-endian.

@elinor-fung
Copy link
Member

Oh, I think this is because AppHostShimMaker had an implicit assumption that ResourceUpdater.IsSupportedOS() is tied to Windows.
https://github.com/dotnet/sdk/blob/d4f2153fdbdbd5ee6b793c03fc908254fa6b253c/src/Cli/dotnet/ShellShim/AppHostShimMaker.cs#L44-L47

Before, GetWindowsGraphicalUserInterfaceBit was never getting called on non-Windows, but now, ResourceUpdater.IsSupportedOS() is also true on non-Windows.

@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Sep 22, 2023
@omajid
Copy link
Member Author

omajid commented Sep 27, 2023

Shall I close this issue now that all the linked PRs have been merged?

@omajid omajid closed this as completed Oct 4, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-s390x Related to s390x architecture (unsupported) area-HostModel Microsoft.NET.HostModel issues
Projects
Archived in project
Development

No branches or pull requests

4 participants