@@ -1012,19 +1012,69 @@ MonodroidRuntime::convert_dl_flags (int flags)
1012
1012
return lflags;
1013
1013
}
1014
1014
1015
- force_inline DSOCacheEntry*
1016
- MonodroidRuntime::find_dso_cache_entry (hash_t hash) noexcept
1015
+ template <bool AotCache>
1016
+ [[gnu::always_inline, gnu::flatten]]
1017
+ DSOCacheEntry*
1018
+ MonodroidRuntime::find_dso_cache_entry_common (hash_t hash) noexcept
1017
1019
{
1020
+ DSOCacheEntry *arr;
1021
+ size_t arr_size;
1022
+
1023
+ if constexpr (AotCache) {
1024
+ log_debug (LOG_ASSEMBLY, " Looking for hash 0x%x in AOT cache" , hash);
1025
+ arr = aot_dso_cache;
1026
+ arr_size = application_config.number_of_aot_cache_entries ;
1027
+ } else {
1028
+ log_debug (LOG_ASSEMBLY, " Looking for hash 0x%x in DSO cache" , hash);
1029
+ arr = dso_cache;
1030
+ arr_size = application_config.number_of_dso_cache_entries ;
1031
+ }
1032
+
1018
1033
auto equal = [](DSOCacheEntry const & entry, hash_t key) -> bool { return entry.hash == key; };
1019
1034
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 );
1035
+
1036
+ ssize_t idx = Search::binary_search<DSOCacheEntry, equal, less_than> (hash, arr, arr_size);
1021
1037
if (idx >= 0 ) {
1022
- return &dso_cache [idx];
1038
+ return &arr [idx];
1023
1039
}
1024
1040
1025
1041
return nullptr ;
1026
1042
}
1027
1043
1044
+ [[gnu::always_inline, gnu::flatten]]
1045
+ DSOCacheEntry*
1046
+ MonodroidRuntime::find_only_aot_cache_entry (hash_t hash) noexcept
1047
+ {
1048
+ constexpr bool IsAotCache = true ;
1049
+ return find_dso_cache_entry_common<IsAotCache> (hash);
1050
+ }
1051
+
1052
+ [[gnu::always_inline, gnu::flatten]]
1053
+ DSOCacheEntry*
1054
+ MonodroidRuntime::find_only_dso_cache_entry (hash_t hash) noexcept
1055
+ {
1056
+ constexpr bool IsAotCache = false ;
1057
+ return find_dso_cache_entry_common<IsAotCache> (hash);
1058
+ }
1059
+
1060
+ [[gnu::always_inline, gnu::flatten]]
1061
+ DSOCacheEntry*
1062
+ MonodroidRuntime::find_any_dso_cache_entry (hash_t hash) noexcept
1063
+ {
1064
+ // If we're asked to look in the AOT DSO cache, do it first. This is because we're likely called from the
1065
+ // MonoVM's dlopen fallback handler and it will not be a request to resolved a p/invoke, but most likely to
1066
+ // find and load an AOT image for a managed assembly. Since there might be naming/hash conflicts in this
1067
+ // scenario, we look at the AOT cache first.
1068
+ //
1069
+ // See: https://github.com/dotnet/android/issues/9081
1070
+ DSOCacheEntry *ret = find_only_aot_cache_entry (hash);
1071
+ if (ret != nullptr ) {
1072
+ return ret;
1073
+ }
1074
+
1075
+ return find_only_dso_cache_entry (hash);
1076
+ }
1077
+
1028
1078
force_inline void *
1029
1079
MonodroidRuntime::monodroid_dlopen_log_and_return (void *handle, char **err, const char *full_name, bool free_memory, [[maybe_unused]] bool need_api_init)
1030
1080
{
@@ -1087,12 +1137,13 @@ MonodroidRuntime::monodroid_dlopen_ignore_component_or_load ([[maybe_unused]] ha
1087
1137
return monodroid_dlopen_log_and_return (handle, err, name, false /* name_needs_free */ );
1088
1138
}
1089
1139
1090
- force_inline void *
1091
- MonodroidRuntime::monodroid_dlopen (const char *name, int flags, char **err) noexcept
1140
+ [[gnu::always_inline, gnu::flatten]]
1141
+ void *
1142
+ MonodroidRuntime::monodroid_dlopen (const char *name, int flags, char **err, bool use_aot_cache) noexcept
1092
1143
{
1093
1144
hash_t name_hash = xxhash::hash (name, strlen (name));
1094
1145
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);
1146
+ DSOCacheEntry *dso = use_aot_cache ? find_any_dso_cache_entry (name_hash) : find_only_dso_cache_entry (name_hash);
1096
1147
log_debug (LOG_ASSEMBLY, " monodroid_dlopen: hash match %sfound, DSO name is '%s'" , dso == nullptr ? " not " : " " , dso == nullptr ? " <unknown>" : dso->name );
1097
1148
1098
1149
if (dso == nullptr ) {
@@ -1149,7 +1200,10 @@ MonodroidRuntime::monodroid_dlopen (const char *name, int flags, char **err, [[m
1149
1200
return nullptr ;
1150
1201
}
1151
1202
1152
- return monodroid_dlopen (name, flags, err);
1203
+ // We're called by MonoVM via a callback, we might need to return an AOT DSO.
1204
+ // See: https://github.com/dotnet/android/issues/9081
1205
+ constexpr bool USE_AOT_CACHE = true ;
1206
+ return monodroid_dlopen (name, flags, err, USE_AOT_CACHE);
1153
1207
}
1154
1208
1155
1209
void *
0 commit comments