Skip to content

Commit

Permalink
Use a common setup method to probe for symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdageek committed Mar 1, 2022
1 parent a631c42 commit 4d063ea
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 26 deletions.
31 changes: 28 additions & 3 deletions src/tests/Interop/MonoAPI/Common/MonoAPISupport.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
using System;
using System.Runtime.InteropServices;

namespace MonoAPI.Tests {
public class MonoAPISupport {
public const string TestLibName = "mono-embedding-api-test";
namespace MonoAPI.Tests;

public class MonoAPISupport
{
public const string TestLibName = "mono-embedding-api-test";

[DllImport(TestLibName)]
private static extern byte libtest_initialize_runtime_symbols(IntPtr libcoreclr_name);

public static void Setup()
{
string libName = TestLibrary.XPlatformUtils.GetStandardNativeLibraryFileName("coreclr");
if (!SetupSymbols(libName))
throw new Exception ($"Native library could not probe for runtime embedding API symbols in {libName}");
}

private static bool SetupSymbols(string libName)
{
IntPtr ptr = IntPtr.Zero;
byte res = 0;
try {
ptr = Marshal.StringToHGlobalAnsi(libName);
res = libtest_initialize_runtime_symbols(ptr);
} finally {
Marshal.FreeHGlobal(ptr);
}
return res != 0;
}
}
7 changes: 7 additions & 0 deletions src/tests/Interop/MonoAPI/MonoMono/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\Directory.Build.props" />

<ItemGroup>
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions src/tests/Interop/MonoAPI/MonoMono/InstallEHCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public static int test_0_throw_and_raise_exception ()

static int Main ()
{
MonoAPI.Tests.MonoAPISupport.Setup();
int result;
result = test_0_setjmp_exn_handler ();
if (result != 0)
Expand All @@ -180,3 +181,4 @@ static int Main ()
return 100;
}
}

1 change: 1 addition & 0 deletions src/tests/Interop/MonoAPI/MonoMono/PInvokeDetach.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class PInvokeDetach {

public static int Main ()
{
MonoAPI.Tests.MonoAPISupport.Setup();
int result;
result = test_0_attach_invoke_foreign_thread ();
if (result != 0)
Expand Down
1 change: 1 addition & 0 deletions src/tests/Interop/MonoAPI/MonoMono/Thunks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static void RunTests(int series, Type type)

public static int Main ()
{
MonoAPI.Tests.MonoAPISupport.Setup();
RunTests (0, typeof (Thunks));
RunTests (100, typeof (TestStruct));
return 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ static char* marshal_strdup (const char *str)
#endif
}

static char *libtest_api_coreclr_name;

static uint8_t
mono_test_init_symbols (void);

LIBTEST_API uint8_t
libtest_initialize_runtime_symbols (const char *libcoreclr_name)
{
libtest_api_coreclr_name = marshal_strdup (libcoreclr_name);
return mono_test_init_symbols();
}

/*
* mono_method_get_unmanaged_thunk tests
*/
Expand All @@ -167,7 +179,6 @@ static char* marshal_strdup (const char *str)
#define ALIGN(size)
#endif


/* thunks.cs:TestStruct */
typedef struct _TestStruct {
int A;
Expand Down Expand Up @@ -233,22 +244,25 @@ w32_find_symbol (const char *symbol_name)
#endif
#endif/*WIN32*/

#ifdef WIN32
static HMODULE libcoreclr_module;
#else
static void* libcoreclr_module;
#endif

/* Searches for mono symbols in all loaded modules */
static gpointer
lookup_mono_symbol (const char *symbol_name)
{
#ifndef HOST_WIN32
#ifdef HOST_DARWIN
void *module = dlopen ("libcoreclr.dylib", RTLD_LAZY);
if (!libcoreclr_module)
libcoreclr_module = dlopen (libtest_api_coreclr_name, RTLD_LAZY);
return dlsym (/*RTLD_DEFAULT*/ libcoreclr_module, symbol_name);
#else
void *module = dlopen ("libcoreclr.so", RTLD_LAZY);
#endif
g_assert (module);
return dlsym (/*RTLD_DEFAULT*/ module, symbol_name);
#else
HMODULE main_module = GetModuleHandle (NULL);
if (!libcoreclr_module)
libcoreclr_module = GetModuleHandle (NULL);
gpointer symbol = NULL;
symbol = (gpointer)(intptr_t)GetProcAddress(main_module, symbol_name);
symbol = (gpointer)(intptr_t)GetProcAddress(libcoreclr_module, symbol_name);
if (symbol)
return symbol;
return w32_find_symbol (symbol_name);
Expand All @@ -262,10 +276,6 @@ mono_test_marshal_lookup_symbol (const char *symbol_name)
}


static void
mono_test_init_symbols (void);


typedef void (*VoidVoidCallback) (void);
typedef void (*MonoFtnPtrEHCallback) (guint32 gchandle);

Expand Down Expand Up @@ -303,8 +313,6 @@ test_method_thunk (int test_id, gpointer test_method_handle, gpointer create_obj
{
int ret = 0;

mono_test_init_symbols ();

gpointer (*mono_method_get_unmanaged_thunk)(gpointer)
= (gpointer (*)(gpointer))sym_mono_method_get_unmanaged_thunk;

Expand Down Expand Up @@ -834,11 +842,11 @@ mono_test_native_to_managed_exception_rethrow (NativeToManagedExceptionRethrowFu
sym_##name = g_cast (lookup_mono_symbol (#name)); \
} while (0)

static void
static uint8_t
mono_test_init_symbols (void)
{
if (sym_inited)
return;
return 1;

SYM_LOOKUP (mono_install_ftnptr_eh_callback);
g_assert (sym_mono_install_ftnptr_eh_callback);
Expand Down Expand Up @@ -869,6 +877,9 @@ mono_test_init_symbols (void)
SYM_LOOKUP (mono_threads_exit_gc_unsafe_region);

sym_inited = 1;

return ((void*)sym_mono_gchandle_new != NULL);

}

#ifndef TARGET_WASM
Expand All @@ -886,7 +897,6 @@ mono_test_longjmp_callback (guint32 gchandle)
LIBTEST_API void STDCALL
mono_test_setjmp_and_call (VoidVoidCallback managedCallback, intptr_t *out_handle)
{
mono_test_init_symbols ();
if (setjmp (test_jmp_buf) == 0) {
*out_handle = 0;
sym_mono_install_ftnptr_eh_callback (mono_test_longjmp_callback);
Expand Down Expand Up @@ -931,7 +941,6 @@ mono_test_ftnptr_eh_callback (guint32 gchandle)
LIBTEST_API void STDCALL
mono_test_setup_ftnptr_eh_callback (VoidVoidCallback managed_entry, void (*capture_throw_callback) (guint32, guint32 *))
{
mono_test_init_symbols ();
mono_test_capture_throw_callback = capture_throw_callback;
sym_mono_install_ftnptr_eh_callback (mono_test_ftnptr_eh_callback);
managed_entry ();
Expand All @@ -940,7 +949,6 @@ mono_test_setup_ftnptr_eh_callback (VoidVoidCallback managed_entry, void (*captu
LIBTEST_API void STDCALL
mono_test_cleanup_ftptr_eh_callback (void)
{
mono_test_init_symbols ();
sym_mono_install_ftnptr_eh_callback (NULL);
}

Expand Down Expand Up @@ -975,8 +983,6 @@ destroy_invoke_names (struct invoke_names *n)
static void
test_invoke_by_name (struct invoke_names *names)
{
mono_test_init_symbols ();

MonoDomain *domain = sym_mono_domain_get ();
MonoThread *thread = NULL;
if (!domain) {
Expand Down

0 comments on commit 4d063ea

Please sign in to comment.