Skip to content

Commit a3d4825

Browse files
authored
[CoreCLR] Code cleanup and filling-in gaps (#10148)
This commit performs a comprehensive code cleanup by converting function parameter types and log messages to use `std::string_view` literal (`sv`) syntax and by removing unused or redundant parameters. `std::string_view` is used to avoid potential conversions between `char*` and `char[]` code and make it faster, at points. It also implements a handful of `p/invokes` which haven't had implementation so far. One notable exception here is `_monodroid_gref_log_delete` which, when enabled, causes a managed exception towards the end of `Mono.Android_Tests` run: FATAL EXCEPTION: Instr: xamarin.android.runtimetests.NUnitInstrumentation Process: Mono.Android.NET_Tests, PID: 16194 android.runtime.JavaProxyThrowable: [System.ObjectDisposedException]: Cannot access disposed object with JniIdentityHashCode=166175665. Object name: 'Xamarin.Android.RuntimeTests.NUnitInstrumentation'. at Java.Interop.JniPeerMembers.AssertSelf + 0x2f(Unknown Source) at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod + 0x1(Unknown Source) at Android.App.Instrumentation.Finish + 0x46(Unknown Source) at Xamarin.Android.UnitTests.TestInstrumentation`1.OnStart + 0x6d(Unknown Source) at Android.App.Instrumentation.n_OnStart + 0x1f(Unknown Source) at crc643df67da7b13bb6b1.TestInstrumentation_1.n_onStart(Native Method) at crc643df67da7b13bb6b1.TestInstrumentation_1.onStart(TestInstrumentation_1.java:32) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2606) The above issue likely stems from the imbalance in calls to `_monodroid_gref_log_new` and `_monodroid_gref_log_delete` in `Mono.Android.dll` or `Java.Interop.dll`. It will be investigated and fixed in a separate PR. Key changes include: * Updating function signatures to replace `const char*` with `std::string_view` and removal of unused parameters. * Converting logging format strings throughout the codebase to use the new sv literal. * Minor refactoring in host and assembly-related source files to improve code consistency. * Add implementation of a handful of missing internal `p/invokes`
1 parent 4b435a7 commit a3d4825

File tree

12 files changed

+195
-118
lines changed

12 files changed

+195
-118
lines changed

src/native/clr/host/assembly-store.cc

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ void AssemblyStore::set_assembly_data_and_size (uint8_t* source_assembly_data, u
2121
}
2222

2323
[[gnu::always_inline]]
24-
auto AssemblyStore::get_assembly_data (AssemblyStoreSingleAssemblyRuntimeData const& e, std::string_view const& name, bool force_rw) noexcept -> std::tuple<uint8_t*, uint32_t>
24+
auto AssemblyStore::get_assembly_data (AssemblyStoreSingleAssemblyRuntimeData const& e, std::string_view const& name) noexcept -> std::tuple<uint8_t*, uint32_t>
2525
{
2626
uint8_t *assembly_data = nullptr;
2727
uint32_t assembly_data_size = 0;
2828

2929
#if defined (HAVE_LZ4) && defined (RELEASE)
3030
auto header = reinterpret_cast<const CompressedAssemblyHeader*>(e.image_data);
3131
if (header->magic == COMPRESSED_DATA_MAGIC) {
32-
log_debug (LOG_ASSEMBLY, "Decompressing assembly '{}' from the assembly store", name);
32+
log_debug (LOG_ASSEMBLY, "Decompressing assembly '{}' from the assembly store"sv, name);
3333

3434
if (FastTiming::enabled ()) [[unlikely]] {
3535
internal_timing.start_event (TimingEventKind::AssemblyDecompression);
@@ -42,7 +42,7 @@ auto AssemblyStore::get_assembly_data (AssemblyStoreSingleAssemblyRuntimeData co
4242
Helpers::abort_application (
4343
LOG_ASSEMBLY,
4444
std::format (
45-
"Invalid compressed assembly descriptor index {}",
45+
"Invalid compressed assembly descriptor index {}"sv,
4646
header->descriptor_index
4747
)
4848
);
@@ -71,7 +71,7 @@ auto AssemblyStore::get_assembly_data (AssemblyStoreSingleAssemblyRuntimeData co
7171
Helpers::abort_application (
7272
LOG_ASSEMBLY,
7373
std::format (
74-
"Invalid compressed assembly descriptor at {}: no data",
74+
"Invalid compressed assembly descriptor at {}: no data"sv,
7575
header->descriptor_index
7676
)
7777
);
@@ -82,14 +82,14 @@ auto AssemblyStore::get_assembly_data (AssemblyStoreSingleAssemblyRuntimeData co
8282
Helpers::abort_application (
8383
LOG_ASSEMBLY,
8484
std::format (
85-
"Compressed assembly '{}' is larger than when the application was built (expected at most {}, got {}). Assemblies don't grow just like that!",
85+
"Compressed assembly '{}' is larger than when the application was built (expected at most {}, got {}). Assemblies don't grow just like that!"sv,
8686
name,
8787
cad.uncompressed_file_size,
8888
header->uncompressed_length
8989
)
9090
);
9191
} else {
92-
log_debug (LOG_ASSEMBLY, "Compressed assembly '{}' is smaller than when the application was built. Adjusting accordingly.", name);
92+
log_debug (LOG_ASSEMBLY, "Compressed assembly '{}' is smaller than when the application was built. Adjusting accordingly."sv, name);
9393
}
9494
cad.uncompressed_file_size = header->uncompressed_length;
9595
}
@@ -101,7 +101,7 @@ auto AssemblyStore::get_assembly_data (AssemblyStoreSingleAssemblyRuntimeData co
101101
Helpers::abort_application (
102102
LOG_ASSEMBLY,
103103
std::format (
104-
"Decompression of assembly {} failed with code {}",
104+
"Decompression of assembly {} failed with code {}"sv,
105105
name,
106106
ret
107107
)
@@ -112,7 +112,7 @@ auto AssemblyStore::get_assembly_data (AssemblyStoreSingleAssemblyRuntimeData co
112112
Helpers::abort_application (
113113
LOG_ASSEMBLY,
114114
std::format (
115-
"Decompression of assembly {} yielded a different size (expected {}, got {})",
115+
"Decompression of assembly {} yielded a different size (expected {}, got {})"sv,
116116
name,
117117
cad.uncompressed_file_size,
118118
static_cast<uint32_t>(ret)
@@ -130,12 +130,12 @@ auto AssemblyStore::get_assembly_data (AssemblyStoreSingleAssemblyRuntimeData co
130130
} else
131131
#endif // def HAVE_LZ4 && def RELEASE
132132
{
133-
log_debug (LOG_ASSEMBLY, "Assembly '{}' is not compressed in the assembly store", name);
133+
log_debug (LOG_ASSEMBLY, "Assembly '{}' is not compressed in the assembly store"sv, name);
134134

135135
// HACK! START
136136
// Currently, MAUI crashes when we return a pointer to read-only data, so we must copy
137137
// the assembly data to a read-write area.
138-
log_debug (LOG_ASSEMBLY, "Copying assembly data to an r/w memory area");
138+
log_debug (LOG_ASSEMBLY, "Copying assembly data to an r/w memory area"sv);
139139

140140
if (FastTiming::enabled ()) [[unlikely]] {
141141
internal_timing.start_event (TimingEventKind::AssemblyLoad);
@@ -176,29 +176,29 @@ auto AssemblyStore::find_assembly_store_entry (hash_t hash, const AssemblyStoreI
176176
auto AssemblyStore::open_assembly (std::string_view const& name, int64_t &size) noexcept -> void*
177177
{
178178
hash_t name_hash = xxhash::hash (name.data (), name.length ());
179-
log_debug (LOG_ASSEMBLY, "AssemblyStore::open_assembly: looking for bundled name: '{}' (hash {:x})", optional_string (name.data ()), name_hash);
179+
log_debug (LOG_ASSEMBLY, "AssemblyStore::open_assembly: looking for bundled name: '{}' (hash {:x})"sv, optional_string (name.data ()), name_hash);
180180

181181
if constexpr (Constants::is_debug_build) {
182182
// TODO: implement filesystem lookup here
183183

184184
// In fastdev mode we might not have any assembly store.
185185
if (assembly_store_hashes == nullptr) {
186-
log_warn (LOG_ASSEMBLY, "Assembly store not registered. Unable to look up assembly '{}'", name);
186+
log_warn (LOG_ASSEMBLY, "Assembly store not registered. Unable to look up assembly '{}'"sv, name);
187187
return nullptr;
188188
}
189189
}
190190

191191
const AssemblyStoreIndexEntry *hash_entry = find_assembly_store_entry (name_hash, assembly_store_hashes, assembly_store.index_entry_count);
192192
if (hash_entry == nullptr) {
193-
log_warn (LOG_ASSEMBLY, "Assembly '{}' (hash 0x{:x}) not found", optional_string (name.data ()), name_hash);
193+
log_warn (LOG_ASSEMBLY, "Assembly '{}' (hash 0x{:x}) not found"sv, optional_string (name.data ()), name_hash);
194194
return nullptr;
195195
}
196196

197197
if (hash_entry->descriptor_index >= assembly_store.assembly_count) {
198198
Helpers::abort_application (
199199
LOG_ASSEMBLY,
200200
std::format (
201-
"Invalid assembly descriptor index {}, exceeds the maximum value of {}",
201+
"Invalid assembly descriptor index {}, exceeds the maximum value of {}"sv,
202202
hash_entry->descriptor_index,
203203
assembly_store.assembly_count - 1
204204
)
@@ -219,7 +219,7 @@ auto AssemblyStore::open_assembly (std::string_view const& name, int64_t &size)
219219

220220
log_debug (
221221
LOG_ASSEMBLY,
222-
"Mapped: image_data == {:p}; debug_info_data == {:p}; config_data == {:p}; descriptor == {:p}; data size == {}; debug data size == {}; config data size == {}; name == '{}'",
222+
"Mapped: image_data == {:p}; debug_info_data == {:p}; config_data == {:p}; descriptor == {:p}; data size == {}; debug data size == {}; config data size == {}; name == '{}'"sv,
223223
static_cast<void*>(assembly_runtime_info.image_data),
224224
static_cast<void*>(assembly_runtime_info.debug_info_data),
225225
static_cast<void*>(assembly_runtime_info.config_data),
@@ -231,8 +231,7 @@ auto AssemblyStore::open_assembly (std::string_view const& name, int64_t &size)
231231
);
232232
}
233233

234-
constexpr hash_t mscorlib_hash = 0x579a06fed6eec900;
235-
auto [assembly_data, assembly_data_size] = get_assembly_data (assembly_runtime_info, name, name_hash == mscorlib_hash);
234+
auto [assembly_data, assembly_data_size] = get_assembly_data (assembly_runtime_info, name);
236235
size = assembly_data_size;
237236
return assembly_data;
238237
}
@@ -242,7 +241,7 @@ void AssemblyStore::map (int fd, std::string_view const& apk_path, std::string_v
242241
detail::mmap_info assembly_store_map = Util::mmap_file (fd, offset, size, store_path);
243242

244243
auto [payload_start, payload_size] = Util::get_wrapper_dso_payload_pointer_and_size (assembly_store_map, store_path);
245-
log_debug (LOG_ASSEMBLY, "Adjusted assembly store pointer: {:p}; size: {}", payload_start, payload_size);
244+
log_debug (LOG_ASSEMBLY, "Adjusted assembly store pointer: {:p}; size: {}"sv, payload_start, payload_size);
246245
auto header = static_cast<AssemblyStoreHeader*>(payload_start);
247246

248247
auto get_full_store_path = [&apk_path, &store_path]() -> std::string {
@@ -264,7 +263,7 @@ void AssemblyStore::map (int fd, std::string_view const& apk_path, std::string_v
264263
Helpers::abort_application (
265264
LOG_ASSEMBLY,
266265
std::format (
267-
"Assembly store '{}' is not a valid .NET for Android assembly store file",
266+
"Assembly store '{}' is not a valid .NET for Android assembly store file"sv,
268267
get_full_store_path ()
269268
)
270269
);
@@ -274,7 +273,7 @@ void AssemblyStore::map (int fd, std::string_view const& apk_path, std::string_v
274273
Helpers::abort_application (
275274
LOG_ASSEMBLY,
276275
std::format (
277-
"Assembly store '{}' uses format version {:x}, instead of the expected {:x}",
276+
"Assembly store '{}' uses format version {:x}, instead of the expected {:x}"sv,
278277
get_full_store_path (),
279278
header->version,
280279
ASSEMBLY_STORE_FORMAT_VERSION
@@ -290,5 +289,5 @@ void AssemblyStore::map (int fd, std::string_view const& apk_path, std::string_v
290289
assembly_store.assemblies = reinterpret_cast<AssemblyStoreEntryDescriptor*>(assembly_store.data_start + header_size + header->index_size);
291290
assembly_store_hashes = reinterpret_cast<AssemblyStoreIndexEntry*>(assembly_store.data_start + header_size);
292291

293-
log_debug (LOG_ASSEMBLY, "Mapped assembly store {}", get_full_store_path ());
292+
log_debug (LOG_ASSEMBLY, "Mapped assembly store {}"sv, get_full_store_path ());
294293
}

src/native/clr/host/fastdev-assemblies.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ auto FastDevAssemblies::open_assembly (std::string_view const& name, int64_t &si
2020

2121
std::string const& override_dir_path = AndroidSystem::get_primary_override_dir ();
2222
if (!Util::dir_exists (override_dir_path)) [[unlikely]] {
23-
log_debug (LOG_ASSEMBLY, "Override directory '{}' does not exist", override_dir_path);
23+
log_debug (LOG_ASSEMBLY, "Override directory '{}' does not exist"sv, override_dir_path);
2424
return nullptr;
2525
}
2626

@@ -31,7 +31,7 @@ auto FastDevAssemblies::open_assembly (std::string_view const& name, int64_t &si
3131
if (override_dir_fd < 0) [[likely]] {
3232
override_dir = opendir (override_dir_path.c_str ());
3333
if (override_dir == nullptr) [[unlikely]] {
34-
log_warn (LOG_ASSEMBLY, "Failed to open override dir '{}'. {}", override_dir_path, strerror (errno));
34+
log_warn (LOG_ASSEMBLY, "Failed to open override dir '{}'. {}"sv, override_dir_path, strerror (errno));
3535
return nullptr;
3636
}
3737
override_dir_fd = dirfd (override_dir);
@@ -40,20 +40,20 @@ auto FastDevAssemblies::open_assembly (std::string_view const& name, int64_t &si
4040

4141
log_debug (
4242
LOG_ASSEMBLY,
43-
"Attempting to load FastDev assembly '{}' from override directory '{}'",
43+
"Attempting to load FastDev assembly '{}' from override directory '{}'"sv,
4444
name,
4545
override_dir_path
4646
);
4747

4848
if (!Util::file_exists (override_dir_fd, name)) {
49-
log_warn (LOG_ASSEMBLY, "FastDev assembly '{}' not found.", name);
49+
log_warn (LOG_ASSEMBLY, "FastDev assembly '{}' not found."sv, name);
5050
return nullptr;
5151
}
52-
log_debug (LOG_ASSEMBLY, "Found FastDev assembly '{}'", name);
52+
log_debug (LOG_ASSEMBLY, "Found FastDev assembly '{}'"sv, name);
5353

5454
auto file_size = Util::get_file_size_at (override_dir_fd, name);
5555
if (!file_size) [[unlikely]] {
56-
log_warn (LOG_ASSEMBLY, "Unable to determine FastDev assembly '{}' file size", name);
56+
log_warn (LOG_ASSEMBLY, "Unable to determine FastDev assembly '{}' file size"sv, name);
5757
return nullptr;
5858
}
5959

@@ -62,7 +62,7 @@ auto FastDevAssemblies::open_assembly (std::string_view const& name, int64_t &si
6262
Helpers::abort_application (
6363
LOG_ASSEMBLY,
6464
std::format (
65-
"FastDev assembly '{}' size exceeds the maximum supported value of {}",
65+
"FastDev assembly '{}' size exceeds the maximum supported value of {}"sv,
6666
name,
6767
MAX_SIZE
6868
)
@@ -74,7 +74,7 @@ auto FastDevAssemblies::open_assembly (std::string_view const& name, int64_t &si
7474
if (asm_fd < 0) {
7575
log_warn (
7676
LOG_ASSEMBLY,
77-
"Failed to open FastDev assembly '{}' for reading. {}",
77+
"Failed to open FastDev assembly '{}' for reading. {}"sv,
7878
name,
7979
strerror (errno)
8080
);
@@ -99,15 +99,15 @@ auto FastDevAssemblies::open_assembly (std::string_view const& name, int64_t &si
9999

100100
log_warn (
101101
LOG_ASSEMBLY,
102-
"Failed to read FastDev assembly '{}' data. {}",
102+
"Failed to read FastDev assembly '{}' data. {}"sv,
103103
name,
104104
strerror (errno)
105105
);
106106

107107
size = 0;
108108
return nullptr;
109109
}
110-
log_debug (LOG_ASSEMBLY, "Read {} bytes of FastDev assembly '{}'", nread, name);
110+
log_debug (LOG_ASSEMBLY, "Read {} bytes of FastDev assembly '{}'"sv, nread, name);
111111

112112
return reinterpret_cast<void*>(buffer);
113113
}

src/native/clr/host/host-jni.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang,
4949
}
5050

5151
JNIEXPORT void
52-
JNICALL Java_mono_android_Runtime_propagateUncaughtException (JNIEnv *env, [[maybe_unused]] jclass klass, jobject javaThread, jthrowable javaException)
52+
JNICALL Java_mono_android_Runtime_propagateUncaughtException ([[maybe_unused]] JNIEnv *env, [[maybe_unused]] jclass klass, [[maybe_unused]] jobject javaThread, [[maybe_unused]] jthrowable javaException)
5353
{
54+
// TODO: implement or remove
5455
}
5556

5657
JNIEXPORT void
5758
JNICALL Java_mono_android_Runtime_notifyTimeZoneChanged ([[maybe_unused]] JNIEnv *env, [[maybe_unused]] jclass klass)
5859
{
60+
// TODO: implement or remove
5961
}

0 commit comments

Comments
 (0)