Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libraries/Common/src/Interop/Interop.Ldap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,11 @@ public void FromManaged(LdapReferralCallback managed)
{
_managed = managed;
_native.sizeofcallback = sizeof(Native);
#pragma warning disable CA1416 // This call site is reachable on all platforms.
Copy link
Member

@jkotas jkotas Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the Wasm compat check be disabled in .csproj files for libraries like Ldap instead?

_native.query = managed.query is not null ? Marshal.GetFunctionPointerForDelegate(managed.query) : IntPtr.Zero;
_native.notify = managed.notify is not null ? Marshal.GetFunctionPointerForDelegate(managed.notify) : IntPtr.Zero;
_native.dereference = managed.dereference is not null ? Marshal.GetFunctionPointerForDelegate(managed.dereference) : IntPtr.Zero;
#pragma warning restore CA1416
}

public Native ToUnmanaged() => _native;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static partial class PlatformDetection
public static bool IsNotMonoInterpreter => !IsMonoInterpreter;
public static bool IsMonoAOT => Environment.GetEnvironmentVariable("MONO_AOT_MODE") == "aot";
public static bool IsNotMonoAOT => Environment.GetEnvironmentVariable("MONO_AOT_MODE") != "aot";
public static bool IsNotMonoAOTOrBrowser => IsNotMonoAOT && !IsBrowser;
public static bool IsNativeAot => IsNotMonoRuntime && !IsReflectionEmitSupported;
public static bool IsNotNativeAot => !IsNativeAot;
public static bool IsFreeBSD => RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,9 @@ private unsafe void Initialize()
Interop.User32.WNDCLASS windowClass = new Interop.User32.WNDCLASS
{
hbrBackground = (IntPtr)(Interop.User32.COLOR_WINDOW + 1),
#pragma warning disable CA1416 // This call site is reachable on all platforms.
lpfnWndProc = Marshal.GetFunctionPointerForDelegate(_windowProc),
#pragma warning restore CA1416
lpszClassName = className,
hInstance = hInstance
};
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/System.Data.OleDb/src/OleDbWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@

// since the delegate lifetime is longer than the original instance used to create it
// we double check before each usage to verify the delegates function pointer
#pragma warning disable CA1416 // This call site is reachable on all platforms.
if ((null == QueryInterface) || (method != Marshal.GetFunctionPointerForDelegate(QueryInterface)))
#pragma warning restore CA1416
{
QueryInterface = (UnsafeNativeMethods.IUnknownQueryInterface)Marshal.GetDelegateForFunctionPointer(method, typeof(UnsafeNativeMethods.IUnknownQueryInterface));
constr.DangerousDataSourceIUnknownQueryInterface = QueryInterface;
Expand All @@ -158,7 +160,7 @@

// since the delegate lifetime is longer than the original instance used to create it
// we double check before each usage to verify the delegates function pointer
if ((null == Initialize) || (method != Marshal.GetFunctionPointerForDelegate(Initialize)))

Check failure on line 163 in src/libraries/System.Data.OleDb/src/OleDbWrapper.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/System.Data.OleDb/src/OleDbWrapper.cs#L163

src/libraries/System.Data.OleDb/src/OleDbWrapper.cs(163,56): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on: 'Windows'. 'Marshal.GetFunctionPointerForDelegate<UnsafeNativeMethods.IDBInitializeInitialize>(UnsafeNativeMethods.IDBInitializeInitialize)' is only supported on: 'browser', 'wasi'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check failure on line 163 in src/libraries/System.Data.OleDb/src/OleDbWrapper.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop

src/libraries/System.Data.OleDb/src/OleDbWrapper.cs#L163

src/libraries/System.Data.OleDb/src/OleDbWrapper.cs(163,56): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on: 'Windows'. 'Marshal.GetFunctionPointerForDelegate<UnsafeNativeMethods.IDBInitializeInitialize>(UnsafeNativeMethods.IDBInitializeInitialize)' is only supported on: 'browser', 'wasi'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
{
Initialize = (UnsafeNativeMethods.IDBInitializeInitialize)Marshal.GetDelegateForFunctionPointer(method, typeof(UnsafeNativeMethods.IDBInitializeInitialize));
constr.DangerousIDBInitializeInitialize = Initialize;
Expand Down Expand Up @@ -187,7 +189,7 @@

// since the delegate lifetime is longer than the original instance used to create it
// we double check before each usage to verify the delegates function pointer
if ((null == CreateSession) || (method != Marshal.GetFunctionPointerForDelegate(CreateSession)))

Check failure on line 192 in src/libraries/System.Data.OleDb/src/OleDbWrapper.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/System.Data.OleDb/src/OleDbWrapper.cs#L192

src/libraries/System.Data.OleDb/src/OleDbWrapper.cs(192,67): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on: 'Windows'. 'Marshal.GetFunctionPointerForDelegate<UnsafeNativeMethods.IDBCreateSessionCreateSession>(UnsafeNativeMethods.IDBCreateSessionCreateSession)' is only supported on: 'browser', 'wasi'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check failure on line 192 in src/libraries/System.Data.OleDb/src/OleDbWrapper.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop

src/libraries/System.Data.OleDb/src/OleDbWrapper.cs#L192

src/libraries/System.Data.OleDb/src/OleDbWrapper.cs(192,67): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on: 'Windows'. 'Marshal.GetFunctionPointerForDelegate<UnsafeNativeMethods.IDBCreateSessionCreateSession>(UnsafeNativeMethods.IDBCreateSessionCreateSession)' is only supported on: 'browser', 'wasi'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
{
CreateSession = (UnsafeNativeMethods.IDBCreateSessionCreateSession)Marshal.GetDelegateForFunctionPointer(method, typeof(UnsafeNativeMethods.IDBCreateSessionCreateSession));
constr.DangerousIDBCreateSessionCreateSession = CreateSession;
Expand Down Expand Up @@ -386,7 +388,9 @@

// since the delegate lifetime is longer than the original instance used to create it
// we double check before each usage to verify the delegates function pointer
#pragma warning disable CA1416 // This call site is reachable on all platforms.
if ((null == CreateCommand) || (method != Marshal.GetFunctionPointerForDelegate(CreateCommand)))
#pragma warning restore CA1416
{
CreateCommand = (UnsafeNativeMethods.IDBCreateCommandCreateCommand)Marshal.GetDelegateForFunctionPointer(method, typeof(UnsafeNativeMethods.IDBCreateCommandCreateCommand));
constr.DangerousIDBCreateCommandCreateCommand = CreateCommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,9 @@ internal unsafe void SyncReplicaAllHelper(IntPtr handle, SyncReplicaFromAllServe
int result;
fixed (char* partitionPtr = partition)
{
#pragma warning disable CA1416 // This call site is reachable on all platforms.
IntPtr syncAllFunctionPointer = Marshal.GetFunctionPointerForDelegate(syncAllCallback);
#pragma warning restore CA1416
result = dsReplicaSyncAllW(handle, partitionPtr, (int)option | DS_REPSYNCALL_ID_SERVERS_BY_DN, syncAllFunctionPointer, (IntPtr)0, &pErrors);
GC.KeepAlive(syncAllCallback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@
if (s_loadedInDefaultContext.Contains(assemblyPath))
return;

var resolver = new AssemblyDependencyResolver(assemblyPath);

Check failure on line 152 in src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs

View check run for this annotation

Azure Pipelines / runtime (Build wasi-wasm linux Release WasmBuildTests)

src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs#L152

src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs(152,32): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on all platforms. 'AssemblyDependencyResolver' is unsupported on: 'wasi'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
AssemblyLoadContext.Default.Resolving +=
(context, assemblyName) =>
{
string? assemblyPath = resolver.ResolveAssemblyToPath(assemblyName);

Check failure on line 156 in src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs

View check run for this annotation

Azure Pipelines / runtime (Build wasi-wasm linux Release WasmBuildTests)

src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs#L156

src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs(156,48): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on all platforms. 'AssemblyDependencyResolver.ResolveAssemblyToPath(AssemblyName)' is unsupported on: 'wasi'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
return assemblyPath != null
? context.LoadFromAssemblyPath(assemblyPath)
: null;
Expand Down Expand Up @@ -350,7 +350,9 @@
{
Delegate d = Delegate.CreateDelegate(delegateType, type, methodName)!;

#pragma warning disable CA1416 // This call site is reachable on all platforms.
functionPtr = Marshal.GetFunctionPointerForDelegate(d);
#pragma warning restore CA1416

lock (s_delegates)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,15 +1137,21 @@ public static TDelegate GetDelegateForFunctionPointer<TDelegate>(IntPtr ptr)

[RequiresDynamicCode("Marshalling code for the delegate might not be available. Use the GetFunctionPointerForDelegate<TDelegate> overload instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("browser")]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("wasi")]
public static IntPtr GetFunctionPointerForDelegate(Delegate d)
{
ArgumentNullException.ThrowIfNull(d);

if (OperatingSystem.IsWasi() || OperatingSystem.IsBrowser()) throw new PlatformNotSupportedException(SR.PlatformNotSupported_DynamicEntrypoint);

return GetFunctionPointerForDelegateInternal(d);
}

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:AotUnfriendlyApi",
Justification = "AOT compilers can see the T.")]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("browser")]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("wasi")]
public static IntPtr GetFunctionPointerForDelegate<TDelegate>(TDelegate d) where TDelegate : notnull
{
return GetFunctionPointerForDelegate((Delegate)(object)d);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace System.Runtime.Loader
{
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
public sealed class AssemblyDependencyResolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace System.Runtime.Loader
{
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
public sealed class AssemblyDependencyResolver
Expand Down Expand Up @@ -47,7 +48,9 @@ public AssemblyDependencyResolver(string componentAssemblyPath)
// to the writer specified. Have to store the previous writer to set it back once this is done.
var errorWriter = new Interop.HostPolicy.corehost_error_writer_fn(message => errorMessage.AppendLine(Marshal.PtrToStringAuto(message)));

#pragma warning disable CA1416 // This call site is reachable on all platforms.
IntPtr errorWriterPtr = Marshal.GetFunctionPointerForDelegate(errorWriter);
#pragma warning restore CA1416
IntPtr previousErrorWriterPtr = Interop.HostPolicy.corehost_set_error_writer(errorWriterPtr);

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,11 @@ public static void FreeHGlobal(System.IntPtr hglobal) { }
public static System.IntPtr GetExceptionPointers() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the delegate might not be available. Use the GetFunctionPointerForDelegate<TDelegate> overload instead.")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("browser")]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("wasi")]
public static System.IntPtr GetFunctionPointerForDelegate(System.Delegate d) { throw null; }
[System.Runtime.Versioning.SupportedOSPlatformAttribute("browser")]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("wasi")]
public static System.IntPtr GetFunctionPointerForDelegate<TDelegate>(TDelegate d) where TDelegate : notnull { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFiles("Windows only assigns HINSTANCE to assemblies loaded from disk. This API will return -1 for modules without a file on disk.")]
public static System.IntPtr GetHINSTANCE(System.Reflection.Module m) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace System.Runtime.InteropServices.Tests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/39187", TestPlatforms.Browser)]
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
public class GetDelegateForFunctionPointerTests
{
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOT))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ namespace System.Runtime.InteropServices.Tests
{
public class GetFunctionPointerForDelegateTests
{
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOT))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/39187", TestPlatforms.Browser)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOTOrBrowser))]
public void GetFunctionPointerForDelegate_NormalDelegateNonGeneric_ReturnsExpected()
{
MethodInfo targetMethod = typeof(GetFunctionPointerForDelegateTests).GetMethod(nameof(Method), BindingFlags.NonPublic | BindingFlags.Static);
Expand All @@ -23,8 +22,7 @@ public void GetFunctionPointerForDelegate_NormalDelegateNonGeneric_ReturnsExpect
Assert.Equal(pointer1, pointer2);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOT))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/39187", TestPlatforms.Browser)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOTOrBrowser))]
public void GetFunctionPointerForDelegate_MarshalledDelegateNonGeneric_ReturnsExpected()
{
MethodInfo targetMethod = typeof(GetFunctionPointerForDelegateTests).GetMethod(nameof(Method), BindingFlags.NonPublic | BindingFlags.Static);
Expand All @@ -40,8 +38,7 @@ public void GetFunctionPointerForDelegate_MarshalledDelegateNonGeneric_ReturnsEx
Assert.Equal(pointer1, pointer2);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOT))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/39187", TestPlatforms.Browser)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOTOrBrowser))]
public void GetFunctionPointerForDelegate_NormalDelegateGeneric_ReturnsExpected()
{
MethodInfo targetMethod = typeof(GetFunctionPointerForDelegateTests).GetMethod(nameof(Method), BindingFlags.NonPublic | BindingFlags.Static);
Expand All @@ -53,8 +50,7 @@ public void GetFunctionPointerForDelegate_NormalDelegateGeneric_ReturnsExpected(
Assert.Equal(pointer1, pointer2);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOT))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/39187", TestPlatforms.Browser)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOTOrBrowser))]
public void GetFunctionPointerForDelegate_MarshalledDelegateGeneric_ReturnsExpected()
{
MethodInfo targetMethod = typeof(GetFunctionPointerForDelegateTests).GetMethod(nameof(Method), BindingFlags.NonPublic | BindingFlags.Static);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace System.Runtime.Loader
{
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("wasi")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
public sealed partial class AssemblyDependencyResolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,11 +697,13 @@ private void Initialize(bool multipleServices)
private SERVICE_TABLE_ENTRY GetEntry()
{
_nameFrozen = true;
#pragma warning disable CA1416 // This call site is reachable on all platforms.
return new SERVICE_TABLE_ENTRY()
{
callback = Marshal.GetFunctionPointerForDelegate(_mainCallback!),
name = Marshal.StringToHGlobalUni(_serviceName)
};
#pragma warning restore CA1416
}

private int ServiceCommandCallbackEx(int command, int eventType, IntPtr eventData, IntPtr eventContext)
Expand Down
2 changes: 2 additions & 0 deletions src/mono/browser/runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ init_icall_table (void)
static void*
get_native_to_interp (MonoMethod *method, void *extra_arg)
{
assert (method);

void *addr = NULL;
MONO_ENTER_GC_UNSAFE;
MonoClass *klass = mono_method_get_class (method);
Expand Down
8 changes: 8 additions & 0 deletions src/mono/mono/mini/interp/interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3469,6 +3469,14 @@ interp_create_method_pointer (MonoMethod *method, gboolean compile, MonoError *e
if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
WrapperInfo *info = mono_marshal_get_wrapper_info (method);
MonoMethod *orig_method = info->d.native_to_managed.method;
if (!orig_method) {
char *s = mono_method_get_full_name (method);
char *msg = g_strdup_printf ("No native to managed transition for method '%s', missing [UnmanagedCallersOnly] attribute.", s);
mono_error_set_platform_not_supported (error, msg);
g_free (s);
g_free (msg);
return NULL;
}

/*
* These are called from native code. Ask the host app for a trampoline.
Expand Down
Loading