runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[NativeAOT] Doesn't add methods to export-table via UnmanagedCallersOnly

Open 3fbaea00 opened this issue 2 years ago • 4 comments

Description

I updated VS from 17.8 to 17.8.3 and after that I ran into a problem that now in export-table of my dll there are no methods that are in reference projects with attribute UnmanagedCallersOnly with property EntryPoint. This happened not only with this project, but also happened in all others and subsequent ones that I created for the test.

For test I downloaded VS 17.8.3 on another PC and found that it was a problem there too. I don’t know if this is a problem or just for some reason the compiler previously put all methods, including reference libraries, in the export table and now it, on the contrary, works as it should.

In any case, I find it strange that when publishing, methods from reference projects with UnmanagedCallerOnly with property EntryPoint are not added to export-table of output .dll

Reproduction Steps

Create 2 .net8.0 Library projects:

A.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
	<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
	<IncludeSymbolsInSingleFile>true</IncludeSymbolsInSingleFile>
	<StripSymbols>true</StripSymbols>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\B\B.csproj" />
  </ItemGroup>
</Project>

B.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>disable</Nullable>
    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
  </PropertyGroup>
</Project>

In A.Class1 add method:

[UnmanagedCallersOnly(EntryPoint = "Method")]
static void Method() { } 

in B.Class1 add method:

[UnmanagedCallersOnly(EntryPoint = "ReferenceMethod")]
static void ReferenceMethod() { } 

Execute commands: PS ..\A> cd A PS ..\A\A> dotnet publish -r win-x64 -c Release

After successful publishing in A\A\bin\Release\net8.0\win-x64\native will appear A.dll

Expected behavior

Expected export's of A.dll: 0 DotNetRuntimeDebugHeader 1 Method 2 ReferenceMethod

Actual behavior

Actual export's of A.dll: 0 DotNetRuntimeDebugHeader 1 Method

Regression?

It stopped working at 1 January of 2024, before it works great

Known Workarounds

No response

Configuration

Net version: .net 8 (SDK 8.100) Windows. Version: Windows 11 Build 25936.rs_prerealese.230818-1326 Architecture: x64

Other information

No response

3fbaea00 avatar Jan 02 '24 15:01 3fbaea00

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

Issue Details

Description

I updated VS from 17.8 to 17.8.3 and after that I ran into a problem that now in export-table of my dll there are no methods that are in reference projects with attribute UnmanagedCallersOnly with property EntryPoint. This happened not only with this project, but also happened in all others and subsequent ones that I created for the test.

For test I downloaded VS 17.8.3 on another PC and found that it was a problem there too. I don’t know if this is a problem or just for some reason the compiler previously put all methods, including reference libraries, in the export table and now it, on the contrary, works as it should.

In any case, I find it strange that when publishing, methods with UnmanagedCallerOnly with property EntryPoint are not added to export-table of output .dll

Reproduction Steps

Create 2 .net8.0 Library projects:

A.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
	<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
	<IncludeSymbolsInSingleFile>true</IncludeSymbolsInSingleFile>
	<StripSymbols>true</StripSymbols>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\B\B.csproj" />
  </ItemGroup>
</Project>

B.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>disable</Nullable>
    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
  </PropertyGroup>
</Project>

In A.Class1 add method:

[UnmanagedCallersOnly(EntryPoint = "Method")]
static void Method() { } 

in B.Class1 add method:

[UnmanagedCallersOnly(EntryPoint = "ReferenceMethod")]
static void ReferenceMethod() { } 

Execute commands: PS ..\A> cd A PS ..\A\A> dotnet publish -r win-x64 -c Release

After successful publishing in A\A\bin\Release\net8.0\win-x64\native will appear A.dll

Expected behavior

Expected export's of A.dll: 0 DotNetRuntimeDebugHeader 1 Method 2 ReferenceMethod

Actual behavior

Actual export's of A.dll: 0 DotNetRuntimeDebugHeader 1 Method

Regression?

It stopped working at 1 june of 2024, before it works great

Known Workarounds

No response

Configuration

Net version: .net 8 (SDK 8.100) Windows. Version: Windows 11 Build 25936.rs_prerealese.230818-1326 Architecture: x64

Other information

No response

Author: Yoticc
Assignees: -
Labels:

untriaged, area-NativeAOT-coreclr

Milestone: -

ghost avatar Jan 02 '24 15:01 ghost

methods from reference projects with UnmanagedCallerOnly with property EntryPoint are not added to export-table of output .dll

It has always been the case that UnmanagedCallerOnly methods are exported from the library project only. The toolchain never scanned all referenced libraries for unmanaged exports. It would be a lot of libraries to scan and it would make the compilation quite a bit slower. I am surprised that it worked for you.

Net version: .net 8 (SDK 8.100)

Do you happen to know if the exact same .NET SDK was used for both cases?

In any case, you can use this as a workaround - it will tell the compiler to scan the additional library for unmanaged exports:

  <ItemGroup>
    <ProjectReference Include="..\B\B.csproj" />
    <IlcCompileInput Include="$(OutputPath)\B.dll" />
  </ItemGroup>

jkotas avatar Jan 02 '24 17:01 jkotas

Do you happen to know if the exact same .NET SDK was used for both cases?

No. But I’m sure that I always downloaded only 8.100 from the official website. Didn't use preview.

I tried add IlcCompileInput tag and it work like expected, fine. Thank you.

It’s a shame, that can’t do it without IlcCompileInput tag. We'll have to tell library users to add this tag to their project. It just worked without IlcCompileInput tag before and it’s very strange. I have a saved yt stream + code from Github when it worked.

In this example, I'm making an analyzer for convenient use of unmanaged things like ldftn, native EntryPoint and Export functions. In this moment at stream I opened export table of compiled dll - https://youtu.be/hjcOHCbKjVk?t=1336 Here exports: NAOT.Internal.DllMain - https://github.com/Yoticc/NAOT/blob/master/NAOT/Internal/DllMain.cs (DllMain_) - NAOT.csproj *DllExportFuncA - https://github.com/Yoticc/NAOT/blob/master/Test/Program.cs (TestNative2) - TEST.csproj *NAOT.Analyzer.EntryPoint - https://github.com/Yoticc/NAOT/blob/master/Test/Program.cs (Main) - TEST.csproj * - Generated automatically by analyzer after compiling in IL

No any additional tags in .csproj file. I added reference to NAOT.dll and it put methods in export table, as I expected...

3fbaea00 avatar Jan 02 '24 17:01 3fbaea00

For my analyzer, I solved this problem by adding all libraries (excpect bcl) in IlcCompileInput lines to .ilc.rsp

3fbaea00 avatar Jan 04 '24 01:01 3fbaea00