Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Use NativeLibrary.TryLoad and prefer RTLD_LAZY over RTLD_NOW (#35548)
Browse files Browse the repository at this point in the history
* Use NativeLibrary.TryLoad in ODBC test

* Use RTLD_LAZY instead of RTLD_NOW

* Add missing using statement

* More RTLD_NOW -> RTLD_LAZY

* Remove unused RTLD_NOW constant

* Use NativeLibrary.TryLoad instead of P/Invoke dlopen

* Inline method body in property

* Add fallback for netcoreapp2x and below

* Reduce visibility of const
  • Loading branch information
am11 authored and stephentoub committed Feb 26, 2019
1 parent 572a154 commit 863b9bf
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Common/src/Interop/Unix/libdl/Interop.dlopen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal partial class Interop
{
internal partial class Libdl
{
public const int RTLD_NOW = 0x002;
public const int RTLD_LAZY = 0x001;

[DllImport(Libraries.Libdl)]
public static extern IntPtr dlopen(string fileName, int flag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,21 @@ public static partial class PlatformDetection
public static bool IsNetfx471OrNewer => false;
public static bool IsNetfx472OrNewer => false;

public static bool IsDrawingSupported { get; } = GetGdiplusIsAvailable();
public static bool IsSoundPlaySupported { get; } = false;
public static bool IsDrawingSupported { get; } =
RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
#if netcoreapp30
? NativeLibrary.TryLoad("libgdiplus.dylib", out _)
: NativeLibrary.TryLoad("libgdiplus.so", out _) || NativeLibrary.TryLoad("libgdiplus.so.0", out _);
#else
? dlopen("libgdiplus.dylib", RTLD_LAZY) != IntPtr.Zero
: dlopen("libgdiplus.so", RTLD_LAZY) != IntPtr.Zero || dlopen("libgdiplus.so.0", RTLD_LAZY) != IntPtr.Zero;

[DllImport("libdl")]
private static extern IntPtr dlopen(string libName, int flags);
public const int RTLD_NOW = 0x002;

private static bool GetGdiplusIsAvailable()
{
IntPtr nativeLib;
private const int RTLD_LAZY = 0x001;
#endif

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
nativeLib = dlopen("libgdiplus.dylib", RTLD_NOW);
}
else
{
nativeLib = dlopen("libgdiplus.so", RTLD_NOW);
if (nativeLib == IntPtr.Zero)
{
nativeLib = dlopen("libgdiplus.so.0", RTLD_NOW);
}
}

return nativeLib != IntPtr.Zero;
}
public static bool IsSoundPlaySupported { get; } = false;

public static Version OSXVersion { get; } = ToVersion(PlatformApis.GetOSVersion());

Expand Down
3 changes: 2 additions & 1 deletion src/System.Data.Odbc/tests/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;

namespace System.Data.Odbc.Tests
{
Expand All @@ -16,7 +17,7 @@ private static bool CheckOdbcIsAvailable() =>
#if TargetsWindows
!PlatformDetection.IsWindowsNanoServer && (!PlatformDetection.IsWindowsServerCore || Environment.Is64BitProcess);
#else
Interop.Libdl.dlopen(Interop.Libraries.Odbc32, Interop.Libdl.RTLD_NOW) != IntPtr.Zero;
NativeLibrary.TryLoad(Interop.Libraries.Odbc32, out _);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static IntPtr LoadNativeLibrary()
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
libraryName = "libgdiplus.dylib";
lib = Interop.Libdl.dlopen(libraryName, Interop.Libdl.RTLD_NOW);
lib = Interop.Libdl.dlopen(libraryName, Interop.Libdl.RTLD_LAZY);
}
else
{
Expand All @@ -41,10 +41,10 @@ private static IntPtr LoadNativeLibrary()
// a global configuration setting. We prefer the "unversioned" shared object name, and fallback to
// the name suffixed with ".0".
libraryName = "libgdiplus.so";
lib = Interop.Libdl.dlopen(libraryName, Interop.Libdl.RTLD_NOW);
lib = Interop.Libdl.dlopen(libraryName, Interop.Libdl.RTLD_LAZY);
if (lib == IntPtr.Zero)
{
lib = Interop.Libdl.dlopen("libgdiplus.so.0", Interop.Libdl.RTLD_NOW);
lib = Interop.Libdl.dlopen("libgdiplus.so.0", Interop.Libdl.RTLD_LAZY);
}
}

Expand All @@ -58,7 +58,7 @@ private static IntPtr LoadNativeLibrary()
{
var searchPath = Path.Combine(searchDirectory, libraryName);

lib = Interop.Libdl.dlopen(searchPath, Interop.Libdl.RTLD_NOW);
lib = Interop.Libdl.dlopen(searchPath, Interop.Libdl.RTLD_LAZY);

if (lib != IntPtr.Zero)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ internal static class LibcupsNative
private static IntPtr LoadLibcups()
{
// We allow both "libcups.so" and "libcups.so.2" to be loaded.
IntPtr lib = Interop.Libdl.dlopen("libcups.so", Interop.Libdl.RTLD_NOW);
IntPtr lib = Interop.Libdl.dlopen("libcups.so", Interop.Libdl.RTLD_LAZY);
if (lib == IntPtr.Zero)
{
lib = Interop.Libdl.dlopen("libcups.so.2", Interop.Libdl.RTLD_NOW);
lib = Interop.Libdl.dlopen("libcups.so.2", Interop.Libdl.RTLD_LAZY);
}

return lib;
Expand Down

0 comments on commit 863b9bf

Please sign in to comment.