This commit is contained in:
Pavel Savara 2025-07-30 13:15:41 +02:00 committed by GitHub
commit 34cc93fca2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 55 additions and 9 deletions

View File

@ -244,9 +244,11 @@ namespace System.DirectoryServices.Protocols
{
_managed = managed;
_native.sizeofcallback = sizeof(Native);
#pragma warning disable CA1416 // This call site is reachable on all platforms.
_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;

View File

@ -33,6 +33,7 @@ namespace System
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"));

View File

@ -671,7 +671,9 @@ namespace Microsoft.Win32
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
};

View File

@ -141,7 +141,9 @@ namespace System.Data.OleDb
// 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;
@ -158,7 +160,9 @@ namespace System.Data.OleDb
// 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 = (UnsafeNativeMethods.IDBInitializeInitialize)Marshal.GetDelegateForFunctionPointer(method, typeof(UnsafeNativeMethods.IDBInitializeInitialize));
constr.DangerousIDBInitializeInitialize = Initialize;
@ -187,7 +191,9 @@ namespace System.Data.OleDb
// 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 = (UnsafeNativeMethods.IDBCreateSessionCreateSession)Marshal.GetDelegateForFunctionPointer(method, typeof(UnsafeNativeMethods.IDBCreateSessionCreateSession));
constr.DangerousIDBCreateSessionCreateSession = CreateSession;
@ -386,7 +392,9 @@ namespace System.Data.OleDb
// 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;

View File

@ -642,7 +642,9 @@ namespace System.DirectoryServices.ActiveDirectory
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);
}

View File

@ -18,6 +18,7 @@ namespace Internal.Runtime.InteropServices
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("tvos")]
@ -56,6 +57,7 @@ namespace Internal.Runtime.InteropServices
[RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")]
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("tvos")]
@ -106,6 +108,7 @@ namespace Internal.Runtime.InteropServices
[RequiresDynamicCode(NativeAOTIncompatibleWarningMessage)]
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("tvos")]
@ -139,6 +142,7 @@ namespace Internal.Runtime.InteropServices
[RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")]
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("tvos")]
@ -281,6 +285,7 @@ namespace Internal.Runtime.InteropServices
[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)
@ -350,7 +355,9 @@ namespace Internal.Runtime.InteropServices
{
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)
{

View File

@ -16,6 +16,7 @@ namespace Internal.Runtime.InteropServices
///</summary>
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("tvos")]

View File

@ -1202,15 +1202,21 @@ namespace System.Runtime.InteropServices
[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);

View File

@ -8,6 +8,7 @@ namespace System.Runtime.Loader
{
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
public sealed class AssemblyDependencyResolver

View File

@ -12,6 +12,7 @@ namespace System.Runtime.Loader
{
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("wasi")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
public sealed class AssemblyDependencyResolver
@ -47,7 +48,9 @@ namespace System.Runtime.Loader
// 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

View File

@ -1072,7 +1072,11 @@ namespace System.Runtime.InteropServices
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; }

View File

@ -11,7 +11,7 @@ using Xunit;
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))]

View File

@ -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);
@ -23,8 +22,7 @@ namespace System.Runtime.InteropServices.Tests
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);
@ -40,8 +38,7 @@ namespace System.Runtime.InteropServices.Tests
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);
@ -53,8 +50,7 @@ namespace System.Runtime.InteropServices.Tests
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);

View File

@ -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

View File

@ -697,11 +697,13 @@ namespace System.ServiceProcess
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)

View File

@ -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);

View File

@ -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.