forked from mathnet/mathnet-numerics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeProviderLoader.cs
More file actions
466 lines (414 loc) · 18.4 KB
/
Copy pathNativeProviderLoader.cs
File metadata and controls
466 lines (414 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// <copyright file="NativeProviderLoader.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2021 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
#if !NET5_0_OR_GREATER
using System.Security;
#endif
using System.Threading;
// ReSharper disable InconsistentNaming
namespace MathNet.Numerics.Providers.Common
{
internal enum ProcArchitecture
{
X64,
X86,
Arm,
Arm64
}
/// <summary>
/// Helper class to load native libraries depending on the architecture of the OS and process.
/// </summary>
internal static class NativeProviderLoader
{
static readonly object StaticLock = new object();
/// <summary>
/// Dictionary of handles to previously loaded libraries,
/// </summary>
static readonly Lazy<Dictionary<string, IntPtr>> NativeHandles = new Lazy<Dictionary<string, IntPtr>>(LazyThreadSafetyMode.PublicationOnly);
#if !NET5_0_OR_GREATER
/// <summary>
/// If the last native library failed to load then gets the corresponding exception
/// which occurred or null if the library was successfully loaded.
/// </summary>
internal static Exception LastException { get; private set; }
#endif
static bool IsWindows { get; }
static bool IsLinux { get; }
static bool IsMac { get; }
static bool IsUnix { get; }
static ProcArchitecture ProcArchitecture { get; }
static string Extension { get; }
static NativeProviderLoader()
{
#if !NET461
IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
IsMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
var a = RuntimeInformation.ProcessArchitecture;
bool arm = a == Architecture.Arm || a == Architecture.Arm64;
#else
var p = Environment.OSVersion.Platform;
IsLinux = p == PlatformID.Unix;
IsMac = p == PlatformID.MacOSX;
bool arm = false;
#endif
IsUnix = IsLinux || IsMac;
IsWindows = !IsUnix;
Extension = IsWindows
? ".dll"
: IsLinux
? ".so"
: ".dylib";
ProcArchitecture = Environment.Is64BitProcess
? arm
? ProcArchitecture.Arm64
: ProcArchitecture.X64
: arm
? ProcArchitecture.Arm
: ProcArchitecture.X86;
}
/// <summary>
/// Load the native library with the given filename.
/// </summary>
/// <param name="fileName">The file name of the library to load.</param>
/// <param name="hintPath">Hint path where to look for the native binaries. Can be null.</param>
/// <returns>True if the library was successfully loaded or if it has already been loaded.</returns>
internal static bool TryLoad(string fileName, string hintPath)
{
if (string.IsNullOrEmpty(fileName))
{
throw new ArgumentNullException(nameof(fileName));
}
// First just try to load it with the file name only
if (TryLoadDirect(fileName))
{
return true;
}
// NOTE: doesn't actually work since the p/invoke is then wrong
//if (!IsWindows && fileName.StartsWith("lib") && TryLoadDirect(fileName.Substring(3)))
//{
// return true;
//}
if (string.IsNullOrEmpty(Path.GetExtension(fileName)))
{
fileName = Path.ChangeExtension(fileName, Extension);
// Try it also with the proper file extension
if (TryLoadDirect(fileName))
{
return true;
}
}
// If we have hint path provided by the user, look there next
if (hintPath != null && TryLoadFromDirectory(fileName, hintPath))
{
return true;
}
// If we have an overall hint path provided by the user, look there next
if (Control.NativeProviderPath != null && Control.NativeProviderPath != hintPath && TryLoadFromDirectory(fileName, Control.NativeProviderPath))
{
return true;
}
// Look under the current AppDomain's base directory
if (TryLoadFromDirectory(fileName, AppDomain.CurrentDomain.BaseDirectory))
{
return true;
}
// Look at this assembly's directory
if (TryLoadFromDirectory(fileName, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)))
{
return true;
}
return false;
}
/// <summary>
/// Try to load a native library by providing its name and a directory.
/// Tries to load an implementation suitable for the current CPU architecture
/// and process mode if there is a matching subfolder.
/// </summary>
/// <returns>True if the library was successfully loaded or if it has already been loaded.</returns>
static bool TryLoadFromDirectory(string fileName, string directory)
{
if (!Directory.Exists(directory))
{
return false;
}
directory = Path.GetFullPath(directory);
if (IsWindows)
{
switch (ProcArchitecture)
{
case ProcArchitecture.X64:
return TryLoadFile(directory, "runtimes/win-x64/native", fileName)
|| TryLoadFile(directory, "win-x64/native", fileName)
|| TryLoadFile(directory, "win-x64", fileName)
|| TryLoadFile(directory, "x64", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
case ProcArchitecture.X86:
return TryLoadFile(directory, "runtimes/win-x86/native", fileName)
|| TryLoadFile(directory, "win-x86/native", fileName)
|| TryLoadFile(directory, "win-x86", fileName)
|| TryLoadFile(directory, "x86", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
case ProcArchitecture.Arm64:
return TryLoadFile(directory, "runtimes/win-arm64/native", fileName)
|| TryLoadFile(directory, "win-arm64/native", fileName)
|| TryLoadFile(directory, "win-arm64", fileName)
|| TryLoadFile(directory, "arm64", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
case ProcArchitecture.Arm:
return TryLoadFile(directory, "runtimes/win-arm/native", fileName)
|| TryLoadFile(directory, "win-arm/native", fileName)
|| TryLoadFile(directory, "win-arm", fileName)
|| TryLoadFile(directory, "arm", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
default:
return TryLoadFile(directory, string.Empty, fileName);
}
}
if (IsLinux)
{
switch (ProcArchitecture)
{
case ProcArchitecture.X64:
return TryLoadFile(directory, "runtimes/linux-x64/native", fileName)
|| TryLoadFile(directory, "linux-x64/native", fileName)
|| TryLoadFile(directory, "linux-x64", fileName)
|| TryLoadFile(directory, "x64", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
case ProcArchitecture.X86:
return TryLoadFile(directory, "runtimes/linux-x86/native", fileName)
|| TryLoadFile(directory, "linux-x86/native", fileName)
|| TryLoadFile(directory, "linux-x86", fileName)
|| TryLoadFile(directory, "x86", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
case ProcArchitecture.Arm64:
return TryLoadFile(directory, "runtimes/linux-arm64/native", fileName)
|| TryLoadFile(directory, "linux-arm64/native", fileName)
|| TryLoadFile(directory, "linux-arm64", fileName)
|| TryLoadFile(directory, "arm64", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
case ProcArchitecture.Arm:
return TryLoadFile(directory, "runtimes/linux-arm/native", fileName)
|| TryLoadFile(directory, "linux-arm/native", fileName)
|| TryLoadFile(directory, "linux-arm", fileName)
|| TryLoadFile(directory, "arm", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
default:
return TryLoadFile(directory, string.Empty, fileName);
}
}
if (IsMac)
{
switch (ProcArchitecture)
{
case ProcArchitecture.X64:
return TryLoadFile(directory, "runtimes/osx-x64/native", fileName)
|| TryLoadFile(directory, "osx-x64/native", fileName)
|| TryLoadFile(directory, "osx-x64", fileName)
|| TryLoadFile(directory, "x64", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
case ProcArchitecture.Arm64:
return TryLoadFile(directory, "runtimes/osx-arm64/native", fileName)
|| TryLoadFile(directory, "osx-arm64/native", fileName)
|| TryLoadFile(directory, "osx-arm64", fileName)
|| TryLoadFile(directory, "arm64", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
default:
return TryLoadFile(directory, string.Empty, fileName);
}
}
switch (ProcArchitecture)
{
case ProcArchitecture.X64:
return TryLoadFile(directory, "x64", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
case ProcArchitecture.X86:
return TryLoadFile(directory, "x86", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
case ProcArchitecture.Arm64:
return TryLoadFile(directory, "arm64", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
case ProcArchitecture.Arm:
return TryLoadFile(directory, "arm", fileName)
|| TryLoadFile(directory, string.Empty, fileName);
default:
return TryLoadFile(directory, string.Empty, fileName);
}
}
/// <summary>
/// Try to load a native library by only the file name of the library.
/// </summary>
/// <returns>True if the library was successfully loaded or if it has already been loaded.</returns>
static bool TryLoadDirect(string fileName)
{
lock (StaticLock)
{
if (NativeHandles.Value.TryGetValue(fileName, out IntPtr libraryHandle))
{
return true;
}
#if NET5_0_OR_GREATER
try
{
if (!NativeLibrary.TryLoad(fileName, out libraryHandle) || libraryHandle == IntPtr.Zero)
{
return false;
}
}
catch
{
return false;
}
NativeHandles.Value[fileName] = libraryHandle;
return true;
#else
try
{
// If successful this will return a handle to the library
libraryHandle = IsWindows ? WindowsLoader.LoadLibrary(fileName) : IsMac ? MacLoader.LoadLibrary(fileName) : LinuxLoader.LoadLibrary(fileName);
}
catch (Exception e)
{
LastException = e;
return false;
}
if (libraryHandle == IntPtr.Zero)
{
int lastError = Marshal.GetLastWin32Error();
var exception = new System.ComponentModel.Win32Exception(lastError);
LastException = exception;
return false;
}
LastException = null;
NativeHandles.Value[fileName] = libraryHandle;
return true;
#endif
}
}
/// <summary>
/// Try to load a native library by providing the full path including the file name of the library.
/// </summary>
/// <returns>True if the library was successfully loaded or if it has already been loaded.</returns>
static bool TryLoadFile(string directory, string relativePath, string fileName)
{
lock (StaticLock)
{
if (NativeHandles.Value.TryGetValue(fileName, out IntPtr libraryHandle))
{
return true;
}
var fullPath = Path.GetFullPath(Path.Combine(Path.Combine(directory, relativePath), fileName));
if (!File.Exists(fullPath))
{
// If the library isn't found within an architecture specific folder then return false
// to allow normal P/Invoke searching behavior when the library is called
return false;
}
#if NET5_0_OR_GREATER
try
{
if (!NativeLibrary.TryLoad(fullPath, out libraryHandle) || libraryHandle == IntPtr.Zero)
{
return false;
}
}
catch
{
return false;
}
NativeHandles.Value[fileName] = libraryHandle;
return true;
#else
try
{
// If successful this will return a handle to the library
libraryHandle = IsWindows ? WindowsLoader.LoadLibrary(fullPath) : IsMac ? MacLoader.LoadLibrary(fullPath) : LinuxLoader.LoadLibrary(fullPath);
}
catch (Exception e)
{
LastException = e;
return false;
}
if (libraryHandle == IntPtr.Zero)
{
int lastError = Marshal.GetLastWin32Error();
var exception = new System.ComponentModel.Win32Exception(lastError);
LastException = exception;
return false;
}
LastException = null;
NativeHandles.Value[fileName] = libraryHandle;
return true;
#endif
}
}
#if !NET5_0_OR_GREATER
[SuppressUnmanagedCodeSecurity]
[SecurityCritical]
static class WindowsLoader
{
public static IntPtr LoadLibrary(string fileName)
{
return LoadLibraryEx(fileName, IntPtr.Zero, LOAD_WITH_ALTERED_SEARCH_PATH);
}
// Search for dependencies in the library's directory rather than the calling process's directory
const uint LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008;
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)]
static extern IntPtr LoadLibraryEx(string fileName, IntPtr reservedNull, uint flags);
}
[SuppressUnmanagedCodeSecurity]
[SecurityCritical]
static class LinuxLoader
{
public static IntPtr LoadLibrary(string fileName)
{
return dlopen(fileName, RTLD_NOW);
}
const int RTLD_NOW = 2;
[DllImport("libdl.so.2", SetLastError = true)]
static extern IntPtr dlopen(string fileName, int flags);
}
[SuppressUnmanagedCodeSecurity]
[SecurityCritical]
static class MacLoader
{
public static IntPtr LoadLibrary(string fileName)
{
return dlopen(fileName, RTLD_NOW);
}
const int RTLD_NOW = 2;
[DllImport("libdl.dylib", SetLastError = true)]
static extern IntPtr dlopen(string fileName, int flags);
}
#endif
}
}