Skip to content

Commit

Permalink
Avoid custom attributes when resolving lazy pinvokes (dotnet#7484)
Browse files Browse the repository at this point in the history
We already know the DllImportSearchPath wasn't specified in the assembly - no point in looking for the custom attribute.

The custom attribute lookup was failing when reflection is disabled.
  • Loading branch information
MichalStrehovsky authored and jkotas committed Jun 4, 2019
1 parent 9327c74 commit 1e80bc0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ internal static unsafe void FixupModuleCell(ModuleFixupCell* pCell)
// TODO: this should not actually throw yet: AssemblyLoadContext.ResolvingUnmanagedDll is
// a last chance callback we should call before giving up.
hModule = NativeLibrary.LoadLibraryByName(
moduleName, callingAssembly, hasDllImportSearchPath ? (DllImportSearchPath?)dllImportSearchPath : null, true);
moduleName,
callingAssembly,
hasDllImportSearchPath ? (DllImportSearchPath?)dllImportSearchPath : NativeLibrary.DefaultDllImportSearchPath,
throwOnError: true);
}

Debug.Assert(hModule != IntPtr.Zero);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ internal static IntPtr LoadLibraryByName(string libraryName, Assembly assembly,
return ret;
}

internal const DllImportSearchPath DefaultDllImportSearchPath = DllImportSearchPath.AssemblyDirectory;

// TODO: make this into a reflection callback so that we can make this work when reflection is disabled.
private static void GetDllImportSearchPathFlags(Assembly callingAssembly, out int searchPathFlags, out bool searchAssemblyDirectory)
{
searchAssemblyDirectory = true;
searchPathFlags = 0;
DllImportSearchPath searchPath = DefaultDllImportSearchPath;

foreach (CustomAttributeData cad in callingAssembly.CustomAttributes)
{
if (cad.AttributeType == typeof(DefaultDllImportSearchPathsAttribute))
{
var attributeValue = (DllImportSearchPath)cad.ConstructorArguments[0].Value;
searchPathFlags = (int)(attributeValue & ~DllImportSearchPath.AssemblyDirectory);
searchAssemblyDirectory = (attributeValue & DllImportSearchPath.AssemblyDirectory) != 0;
searchPath = (DllImportSearchPath)cad.ConstructorArguments[0].Value;
}
}

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

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

0 comments on commit 1e80bc0

Please sign in to comment.