C#: Use explicit native extension loading whenever possible (#25490)

* add IsNet5OrHigher to PlatformApis

* use explicit native library loading whenever possible
pull/25507/head
Jan Tattermusch 4 years ago committed by GitHub
parent 45e41137a8
commit 3944bfaf76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 112
      src/csharp/Grpc.Core/Internal/NativeExtension.cs
  2. 17
      src/csharp/Grpc.Core/Internal/PlatformApis.cs

@ -29,6 +29,8 @@ namespace Grpc.Core.Internal
/// </summary>
internal sealed class NativeExtension
{
// Enviroment variable can be used to force loading the native extension from given location.
private const string CsharpExtOverrideLocationEnvVarName = "GRPC_CSHARP_EXT_OVERRIDE_LOCATION";
static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<NativeExtension>();
static readonly object staticLock = new object();
static volatile NativeExtension instance;
@ -78,50 +80,50 @@ namespace Grpc.Core.Internal
}
/// <summary>
/// Detects which configuration of native extension to load and load it.
/// Detects which configuration of native extension to load and explicitly loads the dynamic library.
/// The explicit load makes sure that we can detect any loading problems early on.
/// </summary>
private static NativeMethods LoadNativeMethodsLegacyNetFramework()
private static NativeMethods LoadNativeMethodsUsingExplicitLoad()
{
// TODO: allow customizing path to native extension (possibly through exposing a GrpcEnvironment property).
// See https://github.com/grpc/grpc/pull/7303 for one option.
// NOTE: a side effect of searching the native extension's library file relatively to the assembly location is that when Grpc.Core assembly
// is loaded via reflection from a different app's context, the native extension is still loaded correctly
// (while if we used [DllImport], the native extension won't be on the other app's search path for shared libraries).
var assemblyDirectory = GetAssemblyDirectory();
// With "classic" VS projects, the native libraries get copied using a .targets rule to the build output folder
// alongside the compiled assembly.
// With dotnet SDK projects targeting net45 framework, the native libraries (just the required ones)
// are similarly copied to the built output folder, through the magic of Microsoft.NETCore.Platforms.
var classicPath = Path.Combine(assemblyDirectory, GetNativeLibraryFilename());
// With dotnet SDK project targeting netcoreappX.Y, projects will use Grpc.Core assembly directly in the location where it got restored
// by nuget. We locate the native libraries based on known structure of Grpc.Core nuget package.
// When "dotnet publish" is used, the runtimes directory is copied next to the published assemblies.
string runtimesDirectory = string.Format("runtimes/{0}/native", GetRuntimeIdString());
var netCorePublishedAppStylePath = Path.Combine(assemblyDirectory, runtimesDirectory, GetNativeLibraryFilename());
var netCoreAppStylePath = Path.Combine(assemblyDirectory, "../..", runtimesDirectory, GetNativeLibraryFilename());
// Look for the native library in all possible locations in given order.
string[] paths = new[] { classicPath };
string[] paths = new[] { classicPath, netCorePublishedAppStylePath, netCoreAppStylePath};
// TODO(jtattermusch): the UnmanagedLibrary mechanism for loading the native extension while avoiding
// direct use of DllImport is quite complicated and is currently only needed to cover some niche scenarios
// (such legacy .NET Framework projects that use assembly shadowing) - everything else can be covered
// by using the [DllImport]. We should investigate the possibility of eliminating UnmanagedLibrary completely
// in the future.
// The UnmanagedLibrary mechanism for loading the native extension while avoiding
// direct use of DllImport is quite complicated but it is currently needed to ensure:
// 1.) the native extension is loaded eagerly (needed to avoid startup issues)
// 2.) less common scenarios (such as loading Grpc.Core.dll by reflection) still work
// 3.) loading native extension from an arbitrary location when set by an enviroment variable
// TODO(jtattermusch): revisit the possibility of eliminating UnmanagedLibrary completely in the future.
return new NativeMethods(new UnmanagedLibrary(paths));
}
/// <summary>
/// Loads native extension and return native methods delegates.
/// Loads native methods using the <c>[DllImport(LIBRARY_NAME)]</c> attributes.
/// Note that this way of loading the native extension is "lazy" and doesn't
/// detect any "missing library" problems until we actually try to invoke the native methods
/// (which could be too late and could cause weird hangs at startup)
/// </summary>
private static NativeMethods LoadNativeMethods()
{
if (PlatformApis.IsUnity)
{
return LoadNativeMethodsUnity();
}
if (PlatformApis.IsXamarin)
{
return LoadNativeMethodsXamarin();
}
if (PlatformApis.IsNetCore)
private static NativeMethods LoadNativeMethodsUsingDllImports()
{
// On .NET Core, native libraries are a supported feature and the SDK makes
// sure that the native library is made available in the right location and that
// they will be discoverable by the [DllImport] default loading mechanism,
// even in some of the more exotic situations such as single file apps.
//
// While in theory, we could just [DllImport("grpc_csharp_ext")] for all the platforms
// While in theory, we could just use [DllImport("grpc_csharp_ext")] for all the platforms
// and operating systems, the native libraries in the nuget package
// need to be laid out in a way that still allows things to work well under
// the legacy .NET Framework (where native libraries are a concept unknown to the runtime).
@ -130,8 +132,9 @@ namespace Grpc.Core.Internal
// The classes with the list of DllImport'd methods are code generated,
// so having more than just one doesn't really bother us.
// on Windows, the DllImport("grpc_csharp_ext.x64") doesn't work for some reason,
// on Windows, the DllImport("grpc_csharp_ext.x64") doesn't work
// but DllImport("grpc_csharp_ext.x64.dll") does, so we need a special case for that.
// See https://github.com/dotnet/coreclr/pull/17505 (fixed in .NET Core 3.1+)
bool useDllSuffix = PlatformApis.IsWindows;
if (PlatformApis.Is64Bit)
{
@ -150,7 +153,41 @@ namespace Grpc.Core.Internal
return new NativeMethods(new NativeMethods.DllImportsFromSharedLib_x86());
}
}
return LoadNativeMethodsLegacyNetFramework();
/// <summary>
/// Loads native extension and return native methods delegates.
/// </summary>
private static NativeMethods LoadNativeMethods()
{
if (PlatformApis.IsUnity)
{
return LoadNativeMethodsUnity();
}
if (PlatformApis.IsXamarin)
{
return LoadNativeMethodsXamarin();
}
// Override location of grpc_csharp_ext native library with an environment variable
// Use at your own risk! By doing this you take all the responsibility that the dynamic library
// is of the correct version (needs to match the Grpc.Core assembly exactly) and of the correct platform/architecture.
var nativeExtPathFromEnv = System.Environment.GetEnvironmentVariable(CsharpExtOverrideLocationEnvVarName);
if (!string.IsNullOrEmpty(nativeExtPathFromEnv))
{
return new NativeMethods(new UnmanagedLibrary(new string[] { nativeExtPathFromEnv }));
}
if (IsNet5SingleFileApp())
{
// Ideally we'd want to always load the native extension explicitly
// (to detect any potential problems early on and to avoid hard-to-debug startup issues)
// but the mechanism we normally use doesn't work when running
// as a single file app (see https://github.com/grpc/grpc/pull/24744).
// Therefore in this case we simply rely
// on the automatic [DllImport] loading logic to do the right thing.
return LoadNativeMethodsUsingDllImports();
}
return LoadNativeMethodsUsingExplicitLoad();
}
/// <summary>
@ -194,13 +231,14 @@ namespace Grpc.Core.Internal
// Assembly.EscapedCodeBase does not exist under CoreCLR, but assemblies imported from a nuget package
// don't seem to be shadowed by DNX-based projects at all.
var assemblyLocation = assembly.Location;
if (!string.IsNullOrEmpty(assemblyLocation))
if (string.IsNullOrEmpty(assemblyLocation))
{
return Path.GetDirectoryName(assemblyLocation);
}
// In .NET5 single-file deployments, assembly.Location won't be available
// and we can use it for detecting whether we are running as a single file app.
// Also see https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file#other-considerations
return AppContext.BaseDirectory;
return null;
}
return Path.GetDirectoryName(assemblyLocation);
#else
// If assembly is shadowed (e.g. in a webapp), EscapedCodeBase is pointing
// to the original location of the assembly, and Location is pointing
@ -216,6 +254,12 @@ namespace Grpc.Core.Internal
#endif
}
private static bool IsNet5SingleFileApp()
{
// Use a heuristic that GetAssemblyDirectory() will return null for single file apps.
return PlatformApis.IsNet5OrHigher && GetAssemblyDirectory() == null;
}
#if !NETSTANDARD
private static bool IsFileUri(string uri)
{

@ -45,6 +45,7 @@ namespace Grpc.Core.Internal
static readonly bool isMacOSX;
static readonly bool isWindows;
static readonly bool isMono;
static readonly bool isNet5OrHigher;
static readonly bool isNetCore;
static readonly string unityApplicationPlatform;
static readonly bool isXamarin;
@ -57,11 +58,13 @@ namespace Grpc.Core.Internal
isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
isMacOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
isNetCore =
#if NETSTANDARD2_0
Environment.Version.Major >= 5 ||
isNet5OrHigher = Environment.Version.Major >= 5;
#else
// assume that on .NET 5+, the netstandard2.0 TFM is going to be selected.
isNet5OrHigher = false;
#endif
RuntimeInformation.FrameworkDescription.StartsWith(".NET Core");
isNetCore = isNet5OrHigher || RuntimeInformation.FrameworkDescription.StartsWith(".NET Core");
#else
var platform = Environment.OSVersion.Platform;
@ -69,6 +72,7 @@ namespace Grpc.Core.Internal
isMacOSX = (platform == PlatformID.Unix && GetUname() == "Darwin");
isLinux = (platform == PlatformID.Unix && !isMacOSX);
isWindows = (platform == PlatformID.Win32NT || platform == PlatformID.Win32S || platform == PlatformID.Win32Windows);
isNet5OrHigher = false;
isNetCore = false;
#endif
isMono = Type.GetType("Mono.Runtime") != null;
@ -117,7 +121,12 @@ namespace Grpc.Core.Internal
public static bool IsXamarinAndroid => isXamarinAndroid;
/// <summary>
/// true if running on .NET Core (CoreCLR), false otherwise.
/// true if running on .NET 5+, false otherwise.
/// </summary>
public static bool IsNet5OrHigher => isNet5OrHigher;
/// <summary>
/// true if running on .NET Core (CoreCLR) or NET 5+, false otherwise.
/// </summary>
public static bool IsNetCore => isNetCore;

Loading…
Cancel
Save