Skip to content

[IILink] UnsafeAccessor work for CorLib #106009

@pavelsavara

Description

@pavelsavara

Description

It seems that ILLink doesn't process UnsafeAccessorAttribute when used inside another method.

     internal static Task RegisterWasiPollable(IPoll.Pollable pollable) 
     { 
         return CallRegisterWasiPollable((Thread)null!, 123); 
  
         [UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "RegisterWasiPollable")] 
         static extern Task CallRegisterWasiPollable(Thread t, int handle); 
     } 

Reproduction Steps

See

private static class WasiEventLoop
{
internal static Task RegisterWasiPollable(IPoll.Pollable pollable)
{
var handle = pollable.Handle;
pollable.Handle = 0;
return CallRegisterWasiPollable((Thread)null!, handle);
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "RegisterWasiPollable")]
static extern Task CallRegisterWasiPollable(Thread t, int handle);
}
}

internal static System.Threading.Tasks.Task RegisterWasiPollable(int handle)
{
return WasiEventLoop.RegisterWasiPollable(handle);
}
internal static void DispatchWasiEventLoop()
{
WasiEventLoop.DispatchWasiEventLoop();
}

Expected behavior

[UnsafeAccessor] on the nested external method will protect Thread.RegisterWasiPollable from trimming

Actual behavior

System.AggregateException: One or more errors occurred. (Could not find RegisterWasiPollableHandle)
   ---> System.MissingMethodException: Could not find RegisterWasiPollable
     at System.Net.Http.WasiHttpHandler.WasiEventLoop.RegisterWasiPollable(Pollable )
     at System.Net.Http.WasiHttpHandler.SendRequestAsync(OutgoingRequest )
     at System.Net.Http.WasiHttpHandler.SendAsync(HttpRequestMessage , CancellationToken )
     at System.Net.Http.HttpClient.GetStringAsyncCore(HttpRequestMessage , CancellationToken )
     at Test.Work()

Regression?

No response

Known Workarounds

use ILLink.Descriptors.WASI.xml

Configuration

Net9 Prev 7

Other information

Discovered here

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILLink.NET linker development as well as trimming analyzers

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions