8000 Avoid reading and writing global state when loading native library by tmat · Pull Request #1563 · libgit2/libgit2sharp · GitHub
[go: up one dir, main page]

Skip to content

Avoid reading and writing global state when loading native library #1563

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

Merged
merged 14 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Do not set default value for NativeLibararyPath on .NET Core
  • Loading branch information
tmat committed Apr 16, 2018
commit e618156c150d0bf2c318f5e6fa30b468f63ee45f
26 changes: 15 additions & 11 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,22 @@ static NativeMethods()
{
if (Platform.IsRunningOnNetFramework() || Platform.IsRunningOnNetCore())
{
string dllPath = Path.Combine(GlobalSettings.GetAndLockNativeLibraryPath(), libgit2 + Platform.GetNativeLibraryExtension());

// Try to load the .dll from the path explicitly.
// If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
// If it fails the next DllImport will load the library from safe directories.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
LoadWindowsLibrary(dllPath);
}
else
string nativeLibraryDir = GlobalSettings.GetAndLockNativeLibraryPath();
if (nativeLibraryDir != null)
{
LoadUnixLibrary(dllPath, RTLD_NOW);
string nativeLibraryPath = Path.Combine(nativeLibraryDir, libgit2 + Platform.GetNativeLibraryExtension());

// Try to load the .dll from the path explicitly.
// If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
// If it fails the next DllImport will load the library from safe directories.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
LoadWindowsLibrary(nativeLibraryPath);
}
else
{
LoadUnixLibrary(nativeLibraryPath, RTLD_NOW);
}
}
}

Expand Down
21 changes: 0 additions & 21 deletions LibGit2Sharp/Core/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,6 @@ public static OperatingSystemType OperatingSystem
}
}

/// <summary>
/// Determines the RID to use when loading libgit2 native library.
/// This method only supports RIDs that are currently used by LibGit2Sharp.NativeBinaries.
/// </summary>
public static string GetNativeLibraryRuntimeId()
{
switch (OperatingSystem)
{
case OperatingSystemType.MacOSX:
return "osx";

case OperatingSystemType.Unix:
return "linux-" + ProcessorArchitecture;

case OperatingSystemType.Windows:
return "win7-" + ProcessorArchitecture;
}

throw new PlatformNotSupportedException();
}

public static string GetNativeLibraryExtension()
{
switch (OperatingSystem)
Expand Down
37 changes: 15 additions & 22 deletions LibGit2Sharp/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class GlobalSettings

private static string nativeLibraryPath;
private static bool nativeLibraryPathLocked;
private static string nativeLibraryDefaultPath;

static GlobalSettings()
{
Expand All @@ -28,24 +29,14 @@ static GlobalSettings()

nativeLibraryPathAllowed = netFX || netCore;

if (nativeLibraryPathAllowed)
if (netFX)
{
string assemblyDirectory = GetExecutingAssemblyDirectory();

if (netFX)
{
// For .NET Framework apps the dependencies are deployed to lib/win32/{architecture} directory
nativeLibraryPath = Path.Combine(assemblyDirectory, "lib", "win32");
}
else
{
// .NET Core apps that depend on native libraries load them directly from paths specified
// in .deps.json file of that app and the native library loader just works.
// However, .NET Core doesn't support .deps.json for plugins yet (such as msbuild tasks).
// To address that shortcoming we assume that the plugin deploys the native binaries to runtimes\{rid}\native
// directories and search there.
nativeLibraryPath = Path.Combine(assemblyDirectory, "runtimes", Platform.GetNativeLibraryRuntimeId(), "native");
}
// For .NET Framework apps the dependencies are deployed to lib/win32/{architecture} directory
nativeLibraryDefaultPath = Path.Combine(GetExecutingAssemblyDirectory(), "lib", "win32");
}
else
{
nativeLibraryDefaultPath = null;
}

registeredFilters = new Dictionary<Filter, FilterRegistration>();
Expand Down Expand Up @@ -186,6 +177,10 @@ public static LogConfiguration LogConfiguration
/// This must be set before any other calls to the library,
/// and is not available on other platforms than .NET Framework and .NET Core.
/// </para>
/// <para>
/// If not specified on .NET Framework it defaults to lib/win32 subdirectory
/// of the directory where this assembly is loaded from.
/// </para>
/// </summary>
public static string NativeLibraryPath
{
Expand All @@ -196,7 +191,7 @@ public static string NativeLibraryPath
throw new LibGit2SharpException("Querying the native hint path is only supported on .NET Framework and .NET Core platforms");
}

return nativeLibraryPath;
return nativeLibraryPath ?? nativeLibraryDefaultPath;
}

set
Expand Down Expand Up @@ -225,10 +220,8 @@ public static string NativeLibraryPath
internal static string GetAndLockNativeLibraryPath()
{
nativeLibraryPathLocked = true;

return Platform.IsRunningOnNetFramework() ?
Path.Combine(nativeLibraryPath, Platform.ProcessorArchitecture) :
nativeLibraryPath;
string result = nativeLibraryPath ?? nativeLibraryDefaultPath;
return Platform.IsRunningOnNetFramework() ? Path.Combine(result, Platform.ProcessorArchitecture) : result;
}

/// <summary>
Expand Down
0