Skip to content

Commit 15060e4

Browse files
committed
Prepare for new p/invoke code, step #2
1 parent ed85cd0 commit 15060e4

File tree

7 files changed

+204
-199
lines changed

7 files changed

+204
-199
lines changed

src/native/monodroid/monodroid-glue-internal.hh

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "timing.hh"
1212
#include "cpp-util.hh"
1313
#include "xxhash.hh"
14+
#include "monodroid-dl.hh"
1415

1516
#include <mono/utils/mono-counters.h>
1617
#include <mono/metadata/profiler.h>
@@ -146,18 +147,6 @@ namespace xamarin::android::internal
146147
static constexpr size_t SMALL_STRING_PARSE_BUFFER_LEN = 50;
147148
static constexpr bool is_running_on_desktop = false;
148149

149-
static constexpr std::string_view mono_component_debugger_name { "libmono-component-debugger.so" };
150-
static constexpr hash_t mono_component_debugger_hash = xxhash::hash (mono_component_debugger_name);
151-
152-
static constexpr std::string_view mono_component_hot_reload_name { "libmono-component-hot_reload.so" };
153-
static constexpr hash_t mono_component_hot_reload_hash = xxhash::hash (mono_component_hot_reload_name);
154-
155-
static constexpr std::string_view mono_component_diagnostics_tracing_name { "libmono-component-diagnostics_tracing.so" };
156-
static constexpr hash_t mono_component_diagnostics_tracing_hash = xxhash::hash (mono_component_diagnostics_tracing_name);
157-
158-
static constexpr std::string_view xamarin_native_tracing_name { "libxamarin-native-tracing.so" };
159-
static constexpr hash_t xamarin_native_tracing_name_hash = xxhash::hash (xamarin_native_tracing_name);
160-
161150
public:
162151
static constexpr int XA_LOG_COUNTERS = MONO_COUNTER_JIT | MONO_COUNTER_METADATA | MONO_COUNTER_GC | MONO_COUNTER_GENERICS | MONO_COUNTER_INTERP;
163152

@@ -222,7 +211,7 @@ namespace xamarin::android::internal
222211
static void load_symbol (void *handle, const char *name, TFunc*& fnptr) noexcept
223212
{
224213
char *err = nullptr;
225-
void *symptr = monodroid_dlsym (handle, name, &err, nullptr);
214+
void *symptr = MonodroidDl::monodroid_dlsym (handle, name, &err, nullptr);
226215

227216
if (symptr == nullptr) {
228217
log_warn (LOG_DEFAULT, "Failed to load symbol '%s' library with handle %p. %s", name, handle, err == nullptr ? "Unknown error" : err);
@@ -233,13 +222,6 @@ namespace xamarin::android::internal
233222
fnptr = reinterpret_cast<TFunc*>(symptr);
234223
}
235224

236-
static void* monodroid_dlopen_ignore_component_or_load (hash_t hash, const char *name, int flags, char **err) noexcept;
237-
static void* monodroid_dlopen (const char *name, int flags, char **err) noexcept;
238-
static void* monodroid_dlopen (const char *name, int flags, char **err, void *user_data) noexcept;
239-
static void* monodroid_dlsym (void *handle, const char *name, char **err, void *user_data);
240-
static void* monodroid_dlopen_log_and_return (void *handle, char **err, const char *full_name, bool free_memory, bool need_api_init = false);
241-
static DSOCacheEntry* find_dso_cache_entry (hash_t hash) noexcept;
242-
243225
int LocalRefsAreIndirect (JNIEnv *env, jclass runtimeClass, int version);
244226
void create_xdg_directory (jstring_wrapper& home, size_t home_len, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept;
245227
void create_xdg_directories_and_environment (jstring_wrapper &homeDir);
@@ -348,7 +330,6 @@ namespace xamarin::android::internal
348330
static void *system_native_library_handle;
349331
static void *system_security_cryptography_native_android_library_handle;
350332
static void *system_io_compression_native_library_handle;
351-
static std::mutex dso_handle_write_lock;
352333
};
353334
}
354335
#endif

src/native/monodroid/monodroid-glue.cc

Lines changed: 1 addition & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ MonoCoreRuntimeProperties MonodroidRuntime::monovm_core_properties = {
8787
.pinvoke_override = &MonodroidRuntime::monodroid_pinvoke_override
8888
};
8989

90-
std::mutex MonodroidRuntime::dso_handle_write_lock;
91-
9290
void
9391
MonodroidRuntime::thread_start ([[maybe_unused]] MonoProfiler *prof, [[maybe_unused]] uintptr_t tid)
9492
{
@@ -1003,173 +1001,6 @@ setup_gc_logging (void)
10031001
}
10041002
#endif
10051003

1006-
force_inline unsigned int
1007-
MonodroidRuntime::convert_dl_flags (int flags)
1008-
{
1009-
unsigned int lflags = (flags & static_cast<int> (MONO_DL_LOCAL))
1010-
? JAVA_INTEROP_LIB_LOAD_LOCALLY
1011-
: JAVA_INTEROP_LIB_LOAD_GLOBALLY;
1012-
return lflags;
1013-
}
1014-
1015-
force_inline DSOCacheEntry*
1016-
MonodroidRuntime::find_dso_cache_entry (hash_t hash) noexcept
1017-
{
1018-
auto equal = [](DSOCacheEntry const& entry, hash_t key) -> bool { return entry.hash == key; };
1019-
auto less_than = [](DSOCacheEntry const& entry, hash_t key) -> bool { return entry.hash < key; };
1020-
ssize_t idx = Search::binary_search<DSOCacheEntry, equal, less_than> (hash, dso_cache, application_config.number_of_dso_cache_entries);
1021-
if (idx >= 0) {
1022-
return &dso_cache[idx];
1023-
}
1024-
1025-
return nullptr;
1026-
}
1027-
1028-
force_inline void*
1029-
MonodroidRuntime::monodroid_dlopen_log_and_return (void *handle, char **err, const char *full_name, bool free_memory, [[maybe_unused]] bool need_api_init)
1030-
{
1031-
if (handle == nullptr && err != nullptr) {
1032-
const char *load_error = dlerror ();
1033-
if (load_error == nullptr) {
1034-
load_error = "Unknown error";
1035-
}
1036-
*err = Util::monodroid_strdup_printf ("Could not load library '%s'. %s", full_name, load_error);
1037-
}
1038-
1039-
if (free_memory) {
1040-
delete[] full_name;
1041-
}
1042-
1043-
return handle;
1044-
}
1045-
1046-
force_inline void*
1047-
MonodroidRuntime::monodroid_dlopen_ignore_component_or_load ([[maybe_unused]] hash_t name_hash, const char *name, int flags, char **err) noexcept
1048-
{
1049-
if (MonodroidState::is_startup_in_progress ()) {
1050-
auto ignore_component = [&](const char *label, MonoComponent component) -> bool {
1051-
if ((application_config.mono_components_mask & component) != component) {
1052-
log_info (LOG_ASSEMBLY, "Mono '%s' component requested but not packaged, ignoring", label);
1053-
return true;
1054-
}
1055-
1056-
return false;
1057-
};
1058-
1059-
switch (name_hash) {
1060-
case mono_component_debugger_hash:
1061-
if (ignore_component ("Debugger", MonoComponent::Debugger)) {
1062-
return nullptr;
1063-
}
1064-
break;
1065-
1066-
case mono_component_hot_reload_hash:
1067-
if (ignore_component ("Hot Reload", MonoComponent::HotReload)) {
1068-
return nullptr;
1069-
}
1070-
break;
1071-
1072-
case mono_component_diagnostics_tracing_hash:
1073-
if (ignore_component ("Diagnostics Tracing", MonoComponent::Tracing)) {
1074-
return nullptr;
1075-
}
1076-
break;
1077-
}
1078-
}
1079-
1080-
unsigned int dl_flags = monodroidRuntime.convert_dl_flags (flags);
1081-
void * handle = AndroidSystem::load_dso_from_any_directories (name, dl_flags);
1082-
if (handle != nullptr) {
1083-
return monodroid_dlopen_log_and_return (handle, err, name, false /* name_needs_free */);
1084-
}
1085-
1086-
handle = AndroidSystem::load_dso (name, dl_flags, false /* skip_existing_check */);
1087-
return monodroid_dlopen_log_and_return (handle, err, name, false /* name_needs_free */);
1088-
}
1089-
1090-
force_inline void*
1091-
MonodroidRuntime::monodroid_dlopen (const char *name, int flags, char **err) noexcept
1092-
{
1093-
hash_t name_hash = xxhash::hash (name, strlen (name));
1094-
log_debug (LOG_ASSEMBLY, "monodroid_dlopen: hash for name '%s' is 0x%zx", name, name_hash);
1095-
DSOCacheEntry *dso = find_dso_cache_entry (name_hash);
1096-
log_debug (LOG_ASSEMBLY, "monodroid_dlopen: hash match %sfound, DSO name is '%s'", dso == nullptr ? "not " : "", dso == nullptr ? "<unknown>" : dso->name);
1097-
1098-
if (dso == nullptr) {
1099-
// DSO not known at build time, try to load it
1100-
return monodroid_dlopen_ignore_component_or_load (name_hash, name, flags, err);
1101-
} else if (dso->handle != nullptr) {
1102-
return monodroid_dlopen_log_and_return (dso->handle, err, dso->name, false /* name_needs_free */);
1103-
}
1104-
1105-
if (dso->ignore) {
1106-
log_info (LOG_ASSEMBLY, "Request to load '%s' ignored, it is known not to exist", dso->name);
1107-
return nullptr;
1108-
}
1109-
1110-
StartupAwareLock lock (dso_handle_write_lock);
1111-
#if defined (RELEASE)
1112-
if (AndroidSystem::is_embedded_dso_mode_enabled ()) {
1113-
DSOApkEntry *apk_entry = dso_apk_entries;
1114-
for (size_t i = 0; i < application_config.number_of_shared_libraries; i++) {
1115-
if (apk_entry->name_hash != dso->real_name_hash) {
1116-
apk_entry++;
1117-
continue;
1118-
}
1119-
1120-
android_dlextinfo dli;
1121-
dli.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
1122-
dli.library_fd = apk_entry->fd;
1123-
dli.library_fd_offset = apk_entry->offset;
1124-
dso->handle = android_dlopen_ext (dso->name, flags, &dli);
1125-
1126-
if (dso->handle != nullptr) {
1127-
return monodroid_dlopen_log_and_return (dso->handle, err, dso->name, false /* name_needs_free */);
1128-
}
1129-
break;
1130-
}
1131-
}
1132-
#endif
1133-
unsigned int dl_flags = monodroidRuntime.convert_dl_flags (flags);
1134-
dso->handle = AndroidSystem::load_dso_from_any_directories (dso->name, dl_flags);
1135-
1136-
if (dso->handle != nullptr) {
1137-
return monodroid_dlopen_log_and_return (dso->handle, err, dso->name, false /* name_needs_free */);
1138-
}
1139-
1140-
dso->handle = AndroidSystem::load_dso_from_any_directories (name, dl_flags);
1141-
return monodroid_dlopen_log_and_return (dso->handle, err, name, false /* name_needs_free */);
1142-
}
1143-
1144-
void*
1145-
MonodroidRuntime::monodroid_dlopen (const char *name, int flags, char **err, [[maybe_unused]] void *user_data) noexcept
1146-
{
1147-
if (name == nullptr) {
1148-
log_warn (LOG_ASSEMBLY, "monodroid_dlopen got a null name. This is not supported in NET+");
1149-
return nullptr;
1150-
}
1151-
1152-
return monodroid_dlopen (name, flags, err);
1153-
}
1154-
1155-
void*
1156-
MonodroidRuntime::monodroid_dlsym (void *handle, const char *name, char **err, [[maybe_unused]] void *user_data)
1157-
{
1158-
void *s;
1159-
char *e = nullptr;
1160-
1161-
s = java_interop_lib_symbol (handle, name, &e);
1162-
1163-
if (!s && err) {
1164-
*err = Util::monodroid_strdup_printf ("Could not find symbol '%s': %s", name, e);
1165-
}
1166-
if (e) {
1167-
java_interop_free (e);
1168-
}
1169-
1170-
return s;
1171-
}
1172-
11731004
inline void
11741005
MonodroidRuntime::set_environment_variable_for_directory (const char *name, jstring_wrapper &value, bool createDirectory, mode_t mode)
11751006
{
@@ -1650,7 +1481,7 @@ MonodroidRuntime::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass kl
16501481
}
16511482

16521483
AndroidSystem::setup_process_args (runtimeApks);
1653-
mono_dl_fallback_register (monodroid_dlopen, monodroid_dlsym, nullptr, nullptr);
1484+
mono_dl_fallback_register (MonodroidDl::monodroid_dlopen, MonodroidDl::monodroid_dlsym, nullptr, nullptr);
16541485

16551486
set_profile_options ();
16561487

src/native/monodroid/monodroid-tracing.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ MonodroidRuntime::log_traces (JNIEnv *env, TraceKind kind, const char *first_lin
2525
std::lock_guard lock (tracing_init_lock);
2626

2727
char *err = nullptr;
28-
void *handle = monodroid_dlopen (xamarin_native_tracing_name.data (), MONO_DL_EAGER, &err, nullptr);
28+
void *handle = MonodroidDl::monodroid_dlopen (SharedConstants::xamarin_native_tracing_name.data (), MONO_DL_EAGER, &err, nullptr);
2929
if (handle == nullptr) {
30-
log_warn (LOG_DEFAULT, "Failed to load native tracing library '%s'. %s", xamarin_native_tracing_name, err == nullptr ? "Unknown error" : err);
30+
log_warn (LOG_DEFAULT, "Failed to load native tracing library '%s'. %s", SharedConstants::xamarin_native_tracing_name, err == nullptr ? "Unknown error" : err);
3131
} else {
3232
load_symbol (handle, "xa_get_native_backtrace", _xa_get_native_backtrace);
3333
load_symbol (handle, "xa_get_managed_backtrace", _xa_get_managed_backtrace);

src/native/monodroid/pinvoke-override-api.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <mono/utils/mono-dl-fallback.h>
66

77
#include "globals.hh"
8+
#include "monodroid-dl.hh"
89
#include "monodroid-glue.hh"
910
#include "monodroid-glue-internal.hh"
1011
#include "timing.hh"
@@ -376,7 +377,7 @@ MonodroidRuntime::load_library_symbol (const char *library_name, const char *sym
376377
void *lib_handle = dso_handle == nullptr ? nullptr : *dso_handle;
377378

378379
if (lib_handle == nullptr) {
379-
lib_handle = monodroid_dlopen (library_name, MONO_DL_LOCAL, nullptr, nullptr);
380+
lib_handle = MonodroidDl::monodroid_dlopen (library_name, MONO_DL_LOCAL, nullptr, nullptr);
380381
if (lib_handle == nullptr) {
381382
log_warn (LOG_ASSEMBLY, "Shared library '%s' not loaded, p/invoke '%s' may fail", library_name, symbol_name);
382383
return nullptr;
@@ -390,7 +391,7 @@ MonodroidRuntime::load_library_symbol (const char *library_name, const char *sym
390391
}
391392
}
392393

393-
void *entry_handle = monodroid_dlsym (lib_handle, symbol_name, nullptr, nullptr);
394+
void *entry_handle = MonodroidDl::monodroid_dlsym (lib_handle, symbol_name, nullptr, nullptr);
394395
if (entry_handle == nullptr) {
395396
log_warn (LOG_ASSEMBLY, "Symbol '%s' not found in shared library '%s', p/invoke may fail", symbol_name, library_name);
396397
return nullptr;

0 commit comments

Comments
 (0)