Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make reading custom attributes in NativeLibrary avoidable #80677

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -10,23 +10,34 @@ namespace System.Runtime.InteropServices
{
public static partial class NativeLibrary
{
// Not a public API. We expose this so that it's possible to bypass the codepath that tries to read search path
// from custom attributes.
internal static bool TryLoad(string libraryName, Assembly assembly, DllImportSearchPath searchPath, out IntPtr handle)
{
handle = LoadLibraryByName(libraryName,
assembly,
searchPath,
throwOnError: false);
return handle != IntPtr.Zero;
}

internal static IntPtr LoadLibraryByName(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, bool throwOnError)
{
// First checks if a default dllImportSearchPathFlags was passed in, if so, use that value.
// Otherwise checks if the assembly has the DefaultDllImportSearchPathsAttribute attribute.
// If so, use that value.

int searchPathFlags;
bool searchAssemblyDirectory;
if (searchPath.HasValue)
{
searchPathFlags = (int)(searchPath.Value & ~DllImportSearchPath.AssemblyDirectory);
searchAssemblyDirectory = (searchPath.Value & DllImportSearchPath.AssemblyDirectory) != 0;
}
else
if (!searchPath.HasValue)
{
GetDllImportSearchPathFlags(assembly, out searchPathFlags, out searchAssemblyDirectory);
searchPath = GetDllImportSearchPath(assembly);
}
return LoadLibraryByName(libraryName, assembly, searchPath.Value, throwOnError);
}

internal static IntPtr LoadLibraryByName(string libraryName, Assembly assembly, DllImportSearchPath searchPath, bool throwOnError)
{
int searchPathFlags = (int)(searchPath & ~DllImportSearchPath.AssemblyDirectory);
bool searchAssemblyDirectory = (searchPath & DllImportSearchPath.AssemblyDirectory) != 0;

LoadLibErrorTracker errorTracker = default;
IntPtr ret = LoadBySearch(assembly, searchAssemblyDirectory, searchPathFlags, ref errorTracker, libraryName);
Expand All @@ -38,20 +49,17 @@ internal static IntPtr LoadLibraryByName(string libraryName, Assembly assembly,
return ret;
}

internal static void GetDllImportSearchPathFlags(Assembly callingAssembly, out int searchPathFlags, out bool searchAssemblyDirectory)
internal static DllImportSearchPath GetDllImportSearchPath(Assembly callingAssembly)
{
var searchPath = DllImportSearchPath.AssemblyDirectory;

foreach (CustomAttributeData cad in callingAssembly.CustomAttributes)
{
if (cad.AttributeType == typeof(DefaultDllImportSearchPathsAttribute))
{
searchPath = (DllImportSearchPath)cad.ConstructorArguments[0].Value!;
return (DllImportSearchPath)cad.ConstructorArguments[0].Value!;
}
}

searchPathFlags = (int)(searchPath & ~DllImportSearchPath.AssemblyDirectory);
searchAssemblyDirectory = (searchPath & DllImportSearchPath.AssemblyDirectory) != 0;
return DllImportSearchPath.AssemblyDirectory;
}

internal static IntPtr LoadBySearch(Assembly callingAssembly, bool searchAssemblyDirectory, int dllImportSearchPathFlags, ref LoadLibErrorTracker errorTracker, string libraryName)
Expand Down