From 3fcd20fb5aaa920afd8ab8fccaccb5ea7371b02b Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 8 Jul 2016 14:38:39 +0100 Subject: Overhaul how the native extension is found, loaded and used The goal of this is to fix #7230. The changes here are: - The layout in the nuget package; the files are now in `/runtimes/{os}/native/{library}` - The filename of each library, which now includes the architecture, e.g `grpc_csharp_ext.x64.dll` - The targets file used to copy those files in msbuild-based projects; note that we now don't build up a folder structure. - The way the functions are found Before this change, on Linux and OSX we used to find library symbols manually, and use DllImport on Windows. With this change, the name of the library file changes based on architecture, so `DllImport` doesn't work. Instead, we have to use `GetProcAddress` to fetch the function. This is further convoluted by the convention on Windows-x86 to prefix the function name with `_` and suffix it based on the stack size of the arguments. We can't easily tell the argument size here, so we just try 0, 4, 8...128. (128 bytes should be enough for anyone.) This is inefficient, but it's a one-time hit with a known number of functions, and doesn't seem to have any significant impact. The benefit of this in code is we don't need the DllImports any more, and we don't need to conditionally use `FindSymbol` - we just use it for everything, so things are rather more uniform and tidy. The further benefit of this is that the library name is no longer tied to a particular filename format - so if someone wanted to have a directory with the libraries for every version in, with the version in the filename, we'd handle that just fine. (At least once Testing: - Windows: - Console app under msbuild, dotnet cli and DNX - ASP.NET Classic under msbuild - ASP.NET Core (still running under net451) - Ubuntu 16.04 - Console app under dotnet cli, run with dotnet run and mono - OSX: - Console app under dotnet cli, run with dotnet run and mono Under dotnet cli, a dependency on `Microsoft.NETCore.Platforms` is required in order to force the libraries to be copied. This change does *not* further enable .NET Core. It attempts to keep the existing experimental .NET Core project files in line with the msbuild files, but I expect further work to be required for .NET Core, which has a different build/publication model. --- src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs | 33 ++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs') diff --git a/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs b/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs index 5a80746101..dc629bd714 100644 --- a/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs +++ b/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs @@ -82,6 +82,32 @@ namespace Grpc.Core.Internal /// public IntPtr LoadSymbol(string symbolName) { + if (PlatformApis.IsWindows) + { + // See http://stackoverflow.com/questions/10473310 for background on this. + if (PlatformApis.Is64Bit) + { + return Windows.GetProcAddress(this.handle, symbolName); + } + else + { + // Yes, we could potentially predict the size... but it's a lot simpler to just try + // all the candidates. Most functions have a suffix of @0, @4 or @8 so we won't be trying + // many options - and if it takes a little bit longer to fail if we've really got the wrong + // library, that's not a big problem. This is only called once per function in the native library. + symbolName = "_" + symbolName + "@"; + for (int stackSize = 0; stackSize < 128; stackSize += 4) + { + IntPtr candidate = Windows.GetProcAddress(this.handle, symbolName + stackSize); + if (candidate != IntPtr.Zero) + { + return candidate; + } + } + // Fail. + return IntPtr.Zero; + } + } if (PlatformApis.IsLinux) { if (PlatformApis.IsMono) @@ -142,13 +168,18 @@ namespace Grpc.Core.Internal return path; } } - throw new FileNotFoundException(String.Format("Error loading native library. Not found in any of the possible locations {0}", libraryPathAlternatives)); + throw new FileNotFoundException( + String.Format("Error loading native library. Not found in any of the possible locations: {0}", + string.Join(",", libraryPathAlternatives))); } private static class Windows { [DllImport("kernel32.dll")] internal static extern IntPtr LoadLibrary(string filename); + + [DllImport("kernel32.dll")] + internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName); } private static class Linux -- cgit v1.2.3