dotnet / runtime

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

dotNET 9 AOT: Unexpected Behavior with StackTraceSupport and UnmanagedCallersOnly #105582

Closed blackmaple closed 3 months ago

blackmaple commented 3 months ago

Description

In dotNET 9 AOT, when configuring StackTraceSupport to false, only one function is exported successfully during AOT compilation. Exporting other functions results in LNK1120 errors. This issue does not occur in dotNET 8 AOT.

Reproduction Steps

  1. Set StackTraceSupport to false in dotNET 9 AOT configuration.
  2. Attempt to export multiple functions during AOT compilation.
  3. Observe LNK1120 errors for functions other than the first one.
net9.0 enable enable true true True true true false
public class Test
{
    public static void Main()
    {
        Console.WriteLine(Environment.Is64BitProcess);
        Console.ReadLine();

    }
    public static partial class TestDllExport
    {

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)], EntryPoint = nameof(Test))]
        public static nint Test() => nint.Zero;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)], EntryPoint = nameof(Test1))]
        public static nint Test1() => nint.Zero;

    }

}

Expected behavior

Functions should be exported correctly during AOT compilation in dotNET 9, regardless of the StackTraceSupport setting.

Actual behavior

In dotNET 9 AOT, when configuring StackTraceSupport to false, only one function is exported successfully during AOT compilation. Exporting other functions results in LNK1120 errors. This issue does not occur in dotNET 8 AOT.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

dotnet-policy-service[bot] commented 3 months ago

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

hez2010 commented 3 months ago

I'm not able to reproduce it in my local environment:

    ordinal hint RVA      name

          1    0 000F8380 DotNetRuntimeDebugHeader = DotNetRuntimeDebugHeader
          2    1 000675B0 Test = NativeAotExport_Test_TestDllExport__Test
          3    2 000675E0 Test1 = NativeAotExport_Test_TestDllExport__Test1

While I'm using .NET 9 nightly build though.

LuohuaRain commented 3 months ago

@hez2010 Hi, 大佬

Are you suggesting that we use .NET 9 nightly builds instead of Preview 6? We will try that again.

LuohuaRain commented 3 months ago
  1. Sometimes it worked but only output an empty file ~ 0kb

image

  1. Other times, it exits with code 1120

image

There is a minimal repo; please review: https://github.com/LuohuaRain/dotnet-9-aot-issue/

By the way, everything works fine with .NET 8, but there are issues with .NET 9 when compiling to DLL.

<OutputType>Library</OutputType>
<StackTraceSupport>false</StackTraceSupport>

@hez2010

hez2010 commented 3 months ago

I build your repro but still not able to reproduce it.

image

Can you try building it with command line dotnet publish -c Release -r win-x64 /p:PublishAot=true instead of using Visual Studio?

LuohuaRain commented 3 months ago

@hez2010

SDK: 9.0.100-preview.6.24328.19

The dotnet command still produces the same error.

Result 1

PS C:\source\repos\ConsoleApp1> dotnet publish -c Release -r win-x64 /p:PublishAot=true
Restore complete (0.6s)
You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  ConsoleApp1 succeeded (2.0s) → ConsoleApp1\bin\Release\net9.0\win-x64\publish\

Build succeeded in 2.9s

The build succeeded, but the output file is 0 kb

Result 2

After you clean bin and obj folders, it will cause Result 2

PS C:\source\repos\ConsoleApp1> dotnet publish -c Release -r win-x64 /p:PublishAot=true
Restore complete (1.5s)
You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  ConsoleApp1 failed with 3 error(s) (5.4s) → ConsoleApp1\bin\Release\net9.0\win-x64\ConsoleApp1.dll
    ConsoleApp1.def : error LNK2001: unresolved external symbol Test1
    bin\Release\net9.0\win-x64\native\ConsoleApp1.lib : fatal error LNK1120: 1 unresolved externals
    C:\Users\Admin\.nuget\packages\microsoft.dotnet.ilcompiler\9.0.0-preview.6.24327.7\build\Microsoft.NETCore.Native.targets(373,5): error MSB3073: The command ""C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.40.33807\bin\Hostx64\x64\link.exe" @"obj\Release\net9.0\win-x64\native\link.rsp"" exited with code 1120.

Build failed with 3 error(s) in 7.1s

Could it be that this issue has been fixed in the .NET 9 preview nightly build?

https://aka.ms/dotnet/9.0.1xx/daily/dotnet-sdk-win-x64.exe https://aka.ms/dotnet/9.0.1xx/daily/dotnet-sdk-win-x86.exe

I tried installing 9.0.100-rc.1.24377.5, but some required dependencies were missing, so I couldn't test it.

hez2010 commented 3 months ago

I tried installing 9.0.100-rc.1.24377.5, but some required dependencies were missing, so I couldn't test it.

To use the nightly sdk, you need to add an additional package source to your nuget config:

<configuration>
  <packageSources>
    <add key="dotnet9" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json" />
  </packageSources>
</configuration>
MichalStrehovsky commented 3 months ago

This is broken in preview 6, got fixed a month ago in #103884 so that's why it no longer reproes.