Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5e4e286

Browse files
swaroop-sridharjkotas
authored andcommitted
Sequel to NativeLibrary API (#21148)
Small changes to address furhter feedback: - Perform argument validation in the Marshall.cs instead of DllImport helpers - Improve doc comments.
1 parent f572782 commit 5e4e286

File tree

3 files changed

+78
-45
lines changed

3 files changed

+78
-45
lines changed

src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,12 +1770,14 @@ public static void ZeroFreeGlobalAllocUnicode(IntPtr s)
17701770
/// <param name="libraryName">The name of the native library to be loaded</param>
17711771
/// <returns>The handle for the loaded native library</returns>
17721772
/// <exception cref="System.ArgumentNullException">If libraryPath is null</exception>
1773-
/// <exception cref="System.DllNotFoundException ">Thrown if the library can't be found.</exception>
1774-
/// <exception cref="System.BadImageFormatException">Thrown if the library is not valid.</exception>
1773+
/// <exception cref="System.DllNotFoundException ">If the library can't be found.</exception>
1774+
/// <exception cref="System.BadImageFormatException">If the library is not valid.</exception>
17751775
public static IntPtr LoadLibrary(string libraryPath)
17761776
{
1777-
bool throwOnError = true;
1778-
return LoadLibraryFromPath(libraryPath, throwOnError);
1777+
if (libraryPath == null)
1778+
throw new ArgumentNullException(nameof(libraryPath));
1779+
1780+
return LoadLibraryFromPath(libraryPath, throwOnError: true);
17791781
}
17801782

17811783
/// <summary>
@@ -1787,32 +1789,45 @@ public static IntPtr LoadLibrary(string libraryPath)
17871789
/// <exception cref="System.ArgumentNullException">If libraryPath is null</exception>
17881790
public static bool TryLoadLibrary(string libraryPath, out IntPtr handle)
17891791
{
1790-
bool throwOnError = false;
1791-
handle = LoadLibraryFromPath(libraryPath, throwOnError);
1792+
if (libraryPath == null)
1793+
throw new ArgumentNullException(nameof(libraryPath));
1794+
1795+
handle = LoadLibraryFromPath(libraryPath, throwOnError: false);
17921796
return handle != IntPtr.Zero;
17931797
}
17941798

17951799
/// <summary>
17961800
/// NativeLibrary Loader: High-level API
17971801
/// Given a library name, this function searches specific paths based on the
1798-
/// runtime configuration and attributes of the calling module.
1802+
/// runtime configuration, input parameters, and attributes of the calling assembly.
1803+
/// If DllImportSearchPath parameter is non-null, the flags in this enumeration are used.
1804+
/// Otherwise, the flags specified by the DefaultDllImportSearchPaths attribute on the
1805+
/// calling assembly (if any) are used.
17991806
/// This LoadLibrary() method does not invoke the managed call-backs for native library resolution:
18001807
/// * AssemblyLoadContext.LoadUnmanagedDll()
18011808
/// </summary>
18021809
/// <param name="libraryName">The name of the native library to be loaded</param>
18031810
/// <param name="dllImportSearchPath">The search path</param>
18041811
/// <param name="assembly">The assembly loading the native library</param>
18051812
/// <returns>The handle for the loaded library</returns>
1806-
/// <exception cref="System.ArgumentNullException">If libraryPath is null</exception>
1807-
/// <exception cref="System.ArgumentNullException">If assembly is null</exception>
1808-
/// <exception cref="System.DllNotFoundException ">Thrown if the library can't be found.</exception>
1809-
/// <exception cref="System.BadImageFormatException">Thrown if the library is not valid.</exception>
1813+
/// <exception cref="System.ArgumentNullException">If libraryPath or assembly is null</exception>
1814+
/// <exception cref="System.ArgumentException">If assembly is not a RuntimeAssembly</exception>
1815+
/// <exception cref="System.DllNotFoundException ">If the library can't be found.</exception>
1816+
/// <exception cref="System.BadImageFormatException">If the library is not valid.</exception>
18101817
public static IntPtr LoadLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
18111818
{
1812-
RuntimeAssembly runtimeAssembly = (assembly != null) ? ((RuntimeAssembly)assembly).GetNativeHandle() : null;
1813-
bool throwOnError = true;
1814-
uint searchPathFlag = searchPath.HasValue ? (uint)searchPath.Value : 0;
1815-
return LoadLibraryByName(libraryName, runtimeAssembly, searchPath.HasValue, searchPathFlag, throwOnError);
1819+
if (libraryName == null)
1820+
throw new ArgumentNullException(nameof(libraryName));
1821+
if (assembly == null)
1822+
throw new ArgumentNullException(nameof(assembly));
1823+
if (!(assembly is RuntimeAssembly))
1824+
throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly);
1825+
1826+
return LoadLibraryByName(libraryName,
1827+
((RuntimeAssembly)assembly).GetNativeHandle(),
1828+
searchPath.HasValue,
1829+
(uint) searchPath.GetValueOrDefault(),
1830+
throwOnError: true);
18161831
}
18171832

18181833
/// <summary>
@@ -1823,14 +1838,22 @@ public static IntPtr LoadLibrary(string libraryName, Assembly assembly, DllImpor
18231838
/// <param name="assembly">The assembly loading the native library</param>
18241839
/// <param name="handle">The out-parameter for the loaded native library handle</param>
18251840
/// <returns>True on successful load, false otherwise</returns>
1826-
/// <exception cref="System.ArgumentNullException">If libraryPath is null</exception>
1827-
/// <exception cref="System.ArgumentNullException">If assembly is null and dllImportSearchPath includes AssemblyDirectory</exception>
1841+
/// <exception cref="System.ArgumentNullException">If libraryPath or assembly is null</exception>
1842+
/// <exception cref="System.ArgumentException">If assembly is not a RuntimeAssembly</exception>
18281843
public static bool TryLoadLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr handle)
18291844
{
1830-
RuntimeAssembly runtimeAssembly = (assembly != null) ? ((RuntimeAssembly)assembly).GetNativeHandle() : null;
1831-
bool throwOnError = false;
1832-
uint searchPathFlag = searchPath.HasValue ? (uint)searchPath.Value : 0;
1833-
handle = LoadLibraryByName(libraryName, runtimeAssembly, searchPath.HasValue, searchPathFlag, throwOnError);
1845+
if (libraryName == null)
1846+
throw new ArgumentNullException(nameof(libraryName));
1847+
if (assembly == null)
1848+
throw new ArgumentNullException(nameof(assembly));
1849+
if (!(assembly is RuntimeAssembly))
1850+
throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly);
1851+
1852+
handle = LoadLibraryByName(libraryName,
1853+
((RuntimeAssembly)assembly).GetNativeHandle(),
1854+
searchPath.HasValue,
1855+
(uint) searchPath.GetValueOrDefault(),
1856+
throwOnError: false);
18341857
return handle != IntPtr.Zero;
18351858
}
18361859

@@ -1854,12 +1877,15 @@ public static void FreeLibrary(IntPtr handle)
18541877
/// <param name="name">The name of the exported symbol</param>
18551878
/// <returns>The address of the symbol</returns>
18561879
/// <exception cref="System.ArgumentNullException">If handle or name is null</exception>
1857-
/// <exception cref="System.ArgumentException">If name cannot be converted to UTF8</exception>
18581880
/// <exception cref="System.EntryPointNotFoundException">If the symbol is not found</exception>
18591881
public static IntPtr GetLibraryExport(IntPtr handle, string name)
18601882
{
1861-
bool throwOnError = true;
1862-
return GetNativeLibraryExport(handle, name, throwOnError);
1883+
if (handle == IntPtr.Zero)
1884+
throw new ArgumentNullException(nameof(handle));
1885+
if (name == null)
1886+
throw new ArgumentNullException(nameof(name));
1887+
1888+
return GetNativeLibraryExport(handle, name, throwOnError: true);
18631889
}
18641890

18651891
/// <summary>
@@ -1869,10 +1895,15 @@ public static IntPtr GetLibraryExport(IntPtr handle, string name)
18691895
/// <param name="name">The name of the exported symbol</param>
18701896
/// <param name="address"> The out-parameter for the symbol address, if it exists</param>
18711897
/// <returns>True on success, false otherwise</returns>
1898+
/// <exception cref="System.ArgumentNullException">If handle or name is null</exception>
18721899
public static bool TryGetLibraryExport(IntPtr handle, string name, out IntPtr address)
18731900
{
1874-
bool throwOnError = false;
1875-
address = GetNativeLibraryExport(handle, name, throwOnError);
1901+
if (handle == IntPtr.Zero)
1902+
throw new ArgumentNullException(nameof(handle));
1903+
if (name == null)
1904+
throw new ArgumentNullException(nameof(name));
1905+
1906+
address = GetNativeLibraryExport(handle, name, throwOnError: false);
18761907
return address != IntPtr.Zero;
18771908
}
18781909

src/vm/dllimport.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6136,10 +6136,12 @@ bool NDirect::s_fSecureLoadLibrarySupported = false;
61366136
// static
61376137
NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryFromPath(LPCWSTR libraryPath, BOOL throwOnError)
61386138
{
6139-
STANDARD_VM_CONTRACT;
6140-
6141-
if (libraryPath == NULL)
6142-
COMPlusThrowArgumentNull(W("libraryPath"), W("ArgumentNull_String"));
6139+
CONTRACTL
6140+
{
6141+
STANDARD_VM_CHECK;
6142+
PRECONDITION(CheckPointer(libraryPath));
6143+
}
6144+
CONTRACTL_END;
61436145

61446146
LoadLibErrorTracker errorTracker;
61456147
const NATIVE_LIBRARY_HANDLE hmod =
@@ -6158,16 +6160,16 @@ NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryByName(LPCWSTR libraryName, Assembly *
61586160
BOOL hasDllImportSearchFlag, DWORD dllImportSearchFlag,
61596161
BOOL throwOnError)
61606162
{
6161-
STANDARD_VM_CONTRACT;
6163+
CONTRACTL
6164+
{
6165+
STANDARD_VM_CHECK;
6166+
PRECONDITION(CheckPointer(libraryName));
6167+
PRECONDITION(CheckPointer(callingAssembly));
6168+
}
6169+
CONTRACTL_END;
61626170

61636171
LoadLibErrorTracker errorTracker;
61646172

6165-
if (libraryName == NULL)
6166-
COMPlusThrowArgumentNull(W("libraryName"), W("ArgumentNull_String"));
6167-
6168-
if (callingAssembly == NULL)
6169-
COMPlusThrowArgumentNull(W("callingAssembly"), W("ArgumentNull_Assembly"));
6170-
61716173
// First checks if a default DllImportSearchPathFlag was passed in, if so, use that value.
61726174
// Otherwise checks if the assembly has the DefaultDllImportSearchPathsAttribute attribute. If so, use that value.
61736175
BOOL searchAssemblyDirectory = TRUE;
@@ -6255,13 +6257,13 @@ void NDirect::FreeNativeLibrary(NATIVE_LIBRARY_HANDLE handle)
62556257
//static
62566258
INT_PTR NDirect::GetNativeLibraryExport(NATIVE_LIBRARY_HANDLE handle, LPCWSTR symbolName, BOOL throwOnError)
62576259
{
6258-
STANDARD_VM_CONTRACT;
6259-
6260-
if (handle == NULL)
6261-
COMPlusThrowArgumentNull(W("handle"), W("Arg_InvalidHandle"));
6262-
6263-
if (symbolName == NULL)
6264-
COMPlusThrowArgumentNull(W("symbolName"), W("ArgumentNull_String"));
6260+
CONTRACTL
6261+
{
6262+
STANDARD_VM_CHECK;
6263+
PRECONDITION(CheckPointer(handle));
6264+
PRECONDITION(CheckPointer(symbolName));
6265+
}
6266+
CONTRACTL_END;
62656267

62666268
MAKE_UTF8PTR_FROMWIDE(lpstr, symbolName);
62676269

src/vm/marshalnative.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ INT_PTR QCALLTYPE MarshalNative::LoadLibraryByName(LPCWSTR name, QCall::Assembly
947947
QCALL_CONTRACT;
948948

949949
NATIVE_LIBRARY_HANDLE handle = nullptr;
950-
Assembly *pAssembly = (callingAssembly != NULL) ? callingAssembly->GetAssembly() : NULL;
950+
Assembly *pAssembly = callingAssembly->GetAssembly();
951951

952952
BEGIN_QCALL;
953953

0 commit comments

Comments
 (0)