Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions src/libraries/System.Data.OleDb/src/OleDbWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ internal unsafe OleDbHResult InitializeAndCreateSession(OleDbConnectionString co

// 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 = Marshal.GetDelegateForFunctionPointer<UnsafeNativeMethods.IUnknownQueryInterface>(method);
constr.DangerousDataSourceIUnknownQueryInterface = QueryInterface;
Expand All @@ -160,7 +162,9 @@ internal unsafe OleDbHResult InitializeAndCreateSession(OleDbConnectionString co

// 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 == Initialize) || (method != Marshal.GetFunctionPointerForDelegate(Initialize)))
#pragma warning restore CA1416
{
Initialize = Marshal.GetDelegateForFunctionPointer<UnsafeNativeMethods.IDBInitializeInitialize>(method);
constr.DangerousIDBInitializeInitialize = Initialize;
Expand Down Expand Up @@ -189,7 +193,9 @@ internal unsafe OleDbHResult InitializeAndCreateSession(OleDbConnectionString co

// 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 == CreateSession) || (method != Marshal.GetFunctionPointerForDelegate(CreateSession)))
#pragma warning restore CA1416
{
CreateSession = Marshal.GetDelegateForFunctionPointer<UnsafeNativeMethods.IDBCreateSessionCreateSession>(method);
constr.DangerousIDBCreateSessionCreateSession = CreateSession;
Expand Down Expand Up @@ -388,7 +394,9 @@ internal void VerifyIDBCreateCommand(OleDbConnectionString constr)

// 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 = Marshal.GetDelegateForFunctionPointer<UnsafeNativeMethods.IDBCreateCommandCreateCommand>(method);
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 @@ -18,6 +18,7 @@ internal static partial class ComponentActivator

[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("tvos")]
Expand Down Expand Up @@ -56,6 +57,7 @@ private static string MarshalToString(IntPtr arg, string argName)
[RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")]
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("tvos")]
Expand Down Expand Up @@ -106,6 +108,7 @@ public static unsafe int LoadAssemblyAndGetFunctionPointer(IntPtr assemblyPathNa
[RequiresDynamicCode(NativeAOTIncompatibleWarningMessage)]
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("tvos")]
Expand Down Expand Up @@ -139,6 +142,7 @@ public static int LoadAssembly(IntPtr assemblyPathNative, IntPtr loadContext, In
[RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")]
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("tvos")]
Expand Down Expand Up @@ -281,6 +285,7 @@ public static unsafe int GetFunctionPointer(IntPtr typeNameNative,
[RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")]
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
private static IsolatedComponentLoadContext GetIsolatedComponentLoadContext(string assemblyPath)
Expand Down Expand Up @@ -350,7 +355,9 @@ private static IntPtr InternalGetFunctionPointer(AssemblyLoadContext alc,
{
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 @@ -16,6 +16,7 @@ namespace Internal.Runtime.InteropServices
///</summary>
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("tvos")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1198,15 +1198,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 @@ -1072,7 +1072,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 @@ -3520,6 +3520,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