-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Implement NativeLibrary.GetEntryPointModuleHandle #57610
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
Changes from all commits
e4e799c
5f16ec8
e0542ee
57643bc
4843165
376cd45
d19ed4a
d50f07f
d338682
3710af5
93fc783
58554e0
952903e
9087136
071b119
af32231
d71bc7b
4db27cb
5c61dfb
9c41825
5f36838
e9b70ae
4d58716
31fe53b
19bd0a9
b31fd00
d191560
745c9bc
efc285f
3f020e8
c9a9dba
7ffac68
a175e5c
81f9daa
a6117ba
2d4959a
f17ec5a
b8aa4fe
4e7a69c
c58228b
6bc7f4c
4266aab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,4 +70,3 @@ extern "C" INT_PTR QCALLTYPE NativeLibrary_GetSymbol(INT_PTR handle, LPCWSTR sym | |
|
||
return address; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,3 +50,20 @@ void SystemNative_FreeLibrary(void* handle) | |
{ | ||
dlclose(handle); | ||
} | ||
|
||
#ifdef TARGET_ANDROID | ||
void* SystemNative_GetDefaultSearchOrderPseudoHandle(void) | ||
{ | ||
return (void*)RTLD_DEFAULT; | ||
} | ||
#else | ||
static void* g_defaultSearchOrderPseudoHandle = NULL; | ||
void* SystemNative_GetDefaultSearchOrderPseudoHandle(void) | ||
{ | ||
if (g_defaultSearchOrderPseudoHandle == NULL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has potential race condition depending on how the C++ decides to compile it. The C/C++ compiler is free to reorder the if check and reading of This is real problem. We had many bugs like that over the years - they are always very hard to trace down. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be fixed by making There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. Also, you may want to cache the value in a local so that it does not need to be fetched from memory twice. |
||
{ | ||
g_defaultSearchOrderPseudoHandle = dlopen(NULL, RTLD_LAZY); | ||
} | ||
return g_defaultSearchOrderPseudoHandle; | ||
} | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
using static TestHelpers; | ||
|
||
class GetLibraryExportTests : IDisposable | ||
{ | ||
private readonly IntPtr handle; | ||
|
||
public GetLibraryExportTests() | ||
{ | ||
handle = NativeLibrary.Load(NativeLibraryToLoad.GetFullPath()); | ||
} | ||
|
||
[ConditionalFact(typeof(TestLibrary.Utilities), nameof(TestLibrary.Utilities.IsX86))] | ||
public void GetValidExport_ManualMangling() | ||
{ | ||
EXPECT(GetLibraryExport(handle, "_NativeSum@8")); | ||
EXPECT(TryGetLibraryExport(handle, "_NativeSum@8")); | ||
} | ||
|
||
[ConditionalFact(typeof(TestLibrary.Utilities), nameof(TestLibrary.Utilities.IsNotX86))] | ||
public void GetValidExport() | ||
{ | ||
EXPECT(GetLibraryExport(handle, "NativeSum")); | ||
EXPECT(TryGetLibraryExport(handle, "NativeSum")); | ||
} | ||
|
||
[Fact] | ||
public void NullHandle() | ||
{ | ||
EXPECT(GetLibraryExport(IntPtr.Zero, "NativeSum"), TestResult.ArgumentNull); | ||
EXPECT(TryGetLibraryExport(IntPtr.Zero, "NativeSum"), TestResult.ArgumentNull); | ||
} | ||
|
||
[Fact] | ||
public void NullExport() | ||
{ | ||
EXPECT(GetLibraryExport(handle, null), TestResult.ArgumentNull); | ||
EXPECT(TryGetLibraryExport(handle, null), TestResult.ArgumentNull); | ||
} | ||
|
||
[Fact] | ||
public void ExportDoesNotExist() | ||
{ | ||
EXPECT(GetLibraryExport(handle, "NonNativeSum"), TestResult.EntryPointNotFound); | ||
EXPECT(TryGetLibraryExport(handle, "NonNativeSum"), TestResult.ReturnFailure); | ||
} | ||
|
||
|
||
public void Dispose() => NativeLibrary.Free(handle); | ||
|
||
static TestResult GetLibraryExport(IntPtr handle, string name) | ||
{ | ||
return Run(() => { | ||
IntPtr address = NativeLibrary.GetExport(handle, name); | ||
if (address == IntPtr.Zero) | ||
return TestResult.ReturnNull; | ||
if (RunExportedFunction(address, 1, 1) != 2) | ||
return TestResult.IncorrectEvaluation; | ||
return TestResult.Success; | ||
}); | ||
} | ||
|
||
static TestResult TryGetLibraryExport(IntPtr handle, string name) | ||
{ | ||
return Run(() => { | ||
IntPtr address = IntPtr.Zero; | ||
bool success = NativeLibrary.TryGetExport(handle, name, out address); | ||
if (!success) | ||
return TestResult.ReturnFailure; | ||
if (address == IntPtr.Zero) | ||
return TestResult.ReturnNull; | ||
if (RunExportedFunction(address, 1, 1) != 2) | ||
return TestResult.IncorrectEvaluation; | ||
return TestResult.Success; | ||
}); | ||
} | ||
|
||
private static unsafe int RunExportedFunction(IntPtr address, int arg1, int arg2) | ||
{ | ||
// We use a delegate here instead of a function pointer to avoid hitting issues | ||
// where Mono AOT doesn't generate the managed->native wrapper and then fails | ||
// when in AOT-only mode. | ||
NativeFunctionWrapper wrapper = Marshal.GetDelegateForFunctionPointer<NativeFunctionWrapper>(address); | ||
return wrapper(arg1, arg2); | ||
} | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.Winapi)] | ||
private delegate int NativeFunctionWrapper(int arg1, int arg2); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.