dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.91k stars 4.63k forks source link

JIT_PInvoke asm stubs missing for i386 #68411

Open ta264 opened 2 years ago

ta264 commented 2 years ago

Description

The JIT_PInvokeBegin and JIT_PInvokeEnd stubs are missing for i386.

Currently you can compile a linux-x86 runtime and it seems functional. However if you allow the build process to run crossgen2 on the output, the resulting product fails and throws SIGTRAP at runtime.

The missing stubs should be here: https://github.com/dotnet/runtime/blob/0e18cfda13a8136103c83442e1a649cdecc237b0/src/coreclr/vm/i386/pinvokestubs.S#L14-L30

Reproduction Steps

See the pipeline here: https://github.com/Servarr/dotnet-linux-x86/blob/4b0474c30e5ea2900e14fbe94831d64d7f44b318/azure-pipelines.yml#L73

Omit the following two lines to allow crossgen2 to run: https://github.com/Servarr/dotnet-linux-x86/blob/4b0474c30e5ea2900e14fbe94831d64d7f44b318/azure-pipelines.yml#L82-L83

Expected behavior

Resulting product can run a hello world

Actual behavior

Starting program: /git_working/dotnet/dotnet ./bin/Debug/net7.0/helloworld2.dll
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0xf735eb40 (LWP 362)]
[New Thread 0xf69ffb40 (LWP 363)]
[New Thread 0xf5fffb40 (LWP 364)]
[New Thread 0xf55ffb40 (LWP 365)]
[New Thread 0xf1bfeb40 (LWP 366)]
[New Thread 0xefbd6b40 (LWP 367)]

Thread 1 "dotnet" received signal SIGTRAP, Trace/breakpoint trap.
JIT_PInvokeBegin () at /runtime/src/coreclr/vm/i386/pinvokestubs.S:17
17              ret
(gdb) bt
#0  JIT_PInvokeBegin () at /runtime/src/coreclr/vm/i386/pinvokestubs.S:17
#1  0xeff0e799 in ?? ()
#2  0xeff0e75e in ?? ()
#3  0xeff2c86e in ?? ()
#4  0xeff3884d in ?? ()
#5  0xf76a48aa in CallDescrWorkerInternal () at /runtime/src/coreclr/vm/i386/asmhelpers.S:303
#6  0xf74dff13 in CallDescrWorkerWithHandler (pCallDescrData=0xffffd038, fCriticalCall=0) at /runtime/src/coreclr/vm/callhelpers.cpp:67
#7  MethodDescCallSite::CallTargetWorker (this=0xffffd0d0, pArguments=0xffffd140, pReturnValue=0x0, cbReturnValue=0) at /runtime/src/coreclr/vm/callhelpers.cpp:538
#8  0xf73e45b1 in MethodDescCallSite::Call (this=0xffffd0d0, pArguments=0xffffd140) at /runtime/src/coreclr/vm/callhelpers.h:458
#9  CorHost2::CreateAppDomainWithManager (this=0x5657aa90, wszFriendlyName=<optimized out>, dwFlags=0, wszAppDomainManagerAssemblyName=<optimized out>, wszAppDomainManagerTypeName=<optimized out>, nProperties=9, pPropertyNames=0x56576960, pPropertyValues=0x565813a0, pAppDomainID=0xffffd260) at /runtime/src/coreclr/vm/corhost.cpp:625
#10 0xf7398325 in coreclr_initialize (exePath=0x56570190 "/git_working/dotnet/dotnet", appDomainFriendlyName=0xf7a47ae4 "clrhost", propertyCount=9, propertyKeys=0x56582900, propertyValues=0x56582a70, hostHandle=0xffffd264, domainId=0xffffd260) at /runtime/src/coreclr/dlls/mscoree/exports.cpp:254
#11 0xf7a1346f in ?? () from /git_working/dotnet/shared/Microsoft.NETCore.App/7.0.0-preview.3.22175.4/libhostpolicy.so
#12 0xf7a21209 in ?? () from /git_working/dotnet/shared/Microsoft.NETCore.App/7.0.0-preview.3.22175.4/libhostpolicy.so
#13 0xf7a20aed in corehost_main () from /git_working/dotnet/shared/Microsoft.NETCore.App/7.0.0-preview.3.22175.4/libhostpolicy.so
#14 0xf7a66f62 in ?? () from /git_working/dotnet/host/fxr/7.0.0-preview.3.22175.4/libhostfxr.so
#15 0xf7a65a4f in ?? () from /git_working/dotnet/host/fxr/7.0.0-preview.3.22175.4/libhostfxr.so
#16 0xf7a624f6 in hostfxr_main_startupinfo () from /git_working/dotnet/host/fxr/7.0.0-preview.3.22175.4/libhostfxr.so
#17 0x56562ca2 in ?? ()
#18 0x56562f18 in ?? ()
#19 0xf7ac7ee5 in __libc_start_main () from /lib/i386-linux-gnu/libc.so.6
#20 0x565588df in ?? ()

### Regression?

I'm not sure

### Known Workarounds

Replacing `EmitIL` here:
https://github.com/dotnet/runtime/blob/0e18cfda13a8136103c83442e1a649cdecc237b0/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IL/Stubs/PInvokeILEmitter.cs#L82-L129

with:
```csharp
private MethodIL EmitIL()
{
    throw new NotSupportedException();
}

seems to help.

Alternatively disable running crossgen completely.

Configuration

.NET 7 Preview 3 Cross compiling on Ubuntu 20.04 for linux-x86 Running the output in an Ubuntu 20.04 docker with multilib support enabled

dpkg --add-architecture i386
apt-get update
apt-get install libc6:i386 libgcc1:i386 libgssapi-krb5-2:i386 libicu66:i386 libssl1.1:i386 libstdc++6:i386 zlib1g:i386

Other information

Would be happy to help to try to implement this but I'm totally out of my depth so would need some pointers

dotnet-issue-labeler[bot] commented 2 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

jkotas commented 2 years ago

This should be straightforward to add: Copy over Windows version of these asm stubs https://github.com/dotnet/runtime/blob/main/src/coreclr/vm/i386/PInvokeStubs.asm#L32-L108 and adjust them for Linux asm syntax.