Skip to content

Commit e0deaa7

Browse files
committed
A handful of fixes and a lot of changes
All globals are gone, classes that were instantiated are now fully static.
1 parent d95ad57 commit e0deaa7

29 files changed

+636
-634
lines changed

src/monodroid/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ endif()
332332
set(LINK_LIBS "")
333333

334334
set(LOCAL_COMMON_COMPILER_ARGS
335-
-fno-threadsafe-statics
336335
-Wall
337336
-Wconversion
338337
-Wdeprecated
@@ -360,6 +359,9 @@ endif()
360359

361360
set(LOCAL_COMMON_LINKER_ARGS "")
362361
if(ANDROID)
362+
# We don't need thread-safe initialization of statics on Android, our entire init sequence runs in a single thread
363+
list(APPEND LOCAL_COMMON_COMPILER_ARGS -fno-threadsafe-statics)
364+
363365
if (ENABLE_CLANG_ASAN OR ENABLE_CLANG_UBSAN)
364366
list(APPEND LOCAL_COMMON_COMPILER_ARGS
365367
-fno-omit-frame-pointer

src/monodroid/clang-tidy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Checks: 'clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-avoid-c-arrays,android-*,bugprone-*,-bugprone-easily-swappable-parameters,performance-*,-bugprone-dynamic-static-initializers,modernize-*,-modernize-use-trailing-return-type,-modernize-avoid-c-arrays,readability-*'
2+
Checks: 'clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-avoid-c-arrays,android-*,bugprone-*,-bugprone-easily-swappable-parameters,performance-*,-bugprone-dynamic-static-initializers,modernize-*,-modernize-use-trailing-return-type,-modernize-avoid-c-arrays,readability-*,-readability-braces-around-statements,-readability-identifier-length'
33
WarningsAsErrors: ''
44
HeaderFilterRegex: ''
55
AnalyzeTemporaryDtors: false

src/monodroid/jni/android-system.cc

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
#include <shlwapi.h>
1717
#endif
1818

19-
#include "globals.hh"
19+
#include <mono/metadata/object.h>
20+
2021
#include "android-system.hh"
22+
#include "debug.hh"
2123
#include "monodroid.h"
2224
#include "monodroid-glue-internal.hh"
2325
#include "jni-wrappers.hh"
@@ -163,7 +165,7 @@ AndroidSystem::_monodroid__system_property_get (const char *name, char *sp_value
163165
if (!name || !sp_value)
164166
return -1;
165167

166-
char *env_name = utils.monodroid_strdup_printf ("__XA_%s", name);
168+
char *env_name = Util::monodroid_strdup_printf ("__XA_%s", name);
167169
monodroid_strreplace (env_name, '.', '_');
168170
char *env_value = getenv (env_name);
169171
free (env_name);
@@ -243,7 +245,7 @@ AndroidSystem::monodroid_get_system_property_impl (const char *name, dynamic_loc
243245
int
244246
AndroidSystem::monodroid_get_system_property (const char *name, gsl::owner<char**> value) noexcept
245247
{
246-
if (value)
248+
if (value != nullptr)
247249
*value = nullptr;
248250

249251
dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN> sp_value;
@@ -259,7 +261,7 @@ AndroidSystem::monodroid_get_system_property (const char *name, gsl::owner<char*
259261
}
260262
}
261263

262-
if (len >= 0 && value) {
264+
if (len >= 0 && value != nullptr) {
263265
auto alloc_size = ADD_WITH_OVERFLOW_CHECK (size_t, static_cast<size_t>(len), 1);
264266
*value = new char [alloc_size];
265267
if (*value == nullptr)
@@ -285,7 +287,7 @@ AndroidSystem::_monodroid_get_system_property_from_file (const char *path, char
285287
if (value != nullptr)
286288
*value = nullptr;
287289

288-
FILE* fp = utils.monodroid_fopen (path, "r");
290+
FILE* fp = Util::monodroid_fopen (path, "r");
289291
if (fp == nullptr)
290292
return 0;
291293

@@ -325,7 +327,7 @@ AndroidSystem::monodroid_get_system_property_from_overrides ([[maybe_unused]] co
325327
continue;
326328
}
327329

328-
std::unique_ptr<char[]> override_file {utils.path_combine (dir, name)};
330+
std::unique_ptr<char[]> override_file {Util::path_combine (dir, name)};
329331
log_info (LOG_DEFAULT, "Trying to get property from %s", override_file.get ());
330332

331333
size_t result = _monodroid_get_system_property_from_file (override_file.get (), value);
@@ -350,13 +352,13 @@ AndroidSystem::create_update_dir (char *override_dir) noexcept
350352
* However, if any logging is enabled (which should _not_ happen with
351353
* pre-loaded apps!), we need the .__override__ directory...
352354
*/
353-
if (log_categories == 0 && monodroid_system_property_exists (Debug::DEBUG_MONO_PROFILE_PROPERTY) == 0) {
355+
if (log_categories == 0 && monodroid_system_property_exists (Debug::DEBUG_MONO_PROFILE_PROPERTY)) {
354356
return;
355357
}
356358
#endif
357359

358360
set_override_dir (0, override_dir);
359-
utils.create_public_directory (override_dir);
361+
Util::create_public_directory (override_dir);
360362
log_warn (LOG_DEFAULT, "Creating public update directory: `%s`", override_dir);
361363
}
362364

@@ -367,7 +369,7 @@ AndroidSystem::get_full_dso_path (const char *base_dir, const char *dso_path, dy
367369
return false;
368370
}
369371

370-
if (base_dir == nullptr || utils.is_path_rooted (dso_path)) {
372+
if (base_dir == nullptr || Util::is_path_rooted (dso_path)) {
371373
path.assign_c (dso_path); // Absolute path or no base path, can't do much with it
372374
return true;
373375
}
@@ -386,14 +388,14 @@ AndroidSystem::load_dso (const char *path, unsigned int dl_flags, bool skip_exis
386388
return nullptr;
387389

388390
log_info (LOG_ASSEMBLY, "Trying to load shared library '%s'", path);
389-
if (!skip_exists_check && !is_embedded_dso_mode_enabled () && !utils.file_exists (path)) {
391+
if (!skip_exists_check && !is_embedded_dso_mode_enabled () && !Util::file_exists (path)) {
390392
log_info (LOG_ASSEMBLY, "Shared library '%s' not found", path);
391393
return nullptr;
392394
}
393395

394396
char *error = nullptr;
395397
void *handle = java_interop_lib_load (path, dl_flags, &error);
396-
if (handle == nullptr && utils.should_log (LOG_ASSEMBLY))
398+
if (handle == nullptr && Util::should_log (LOG_ASSEMBLY))
397399
log_info_nocheck (LOG_ASSEMBLY, "Failed to load shared library '%s'. %s", path, error);
398400
java_interop_free (error);
399401
return handle;
@@ -438,10 +440,7 @@ AndroidSystem::load_dso_from_any_directories (const char *name, unsigned int dl_
438440
bool
439441
AndroidSystem::get_existing_dso_path_on_disk (const char *base_dir, const char *dso_name, dynamic_local_string<SENSIBLE_PATH_MAX>& path) noexcept
440442
{
441-
if (get_full_dso_path (base_dir, dso_name, path) && utils.file_exists (path.get ()))
442-
return true;
443-
444-
return false;
443+
return get_full_dso_path (base_dir, dso_name, path) && Util::file_exists (path.get ());
445444
}
446445

447446
bool
@@ -474,19 +473,19 @@ AndroidSystem::count_override_assemblies () noexcept
474473
int c = 0;
475474

476475
for (auto const dir_path : override_dirs ()) {
477-
if (dir_path == nullptr || !utils.directory_exists (dir_path))
476+
if (dir_path == nullptr || !Util::directory_exists (dir_path))
478477
continue;
479478

480-
monodroid_dir_t *dir = utils.monodroid_opendir (dir_path);
479+
monodroid_dir_t *dir = Util::monodroid_opendir (dir_path);
481480
if (dir == nullptr)
482481
continue;
483482

484483
monodroid_dirent_t *e = nullptr;
485484
while ((e = readdir (dir)) != nullptr && e) {
486-
if (utils.monodroid_dirent_hasextension (e, ".dll"))
485+
if (Util::monodroid_dirent_hasextension (e, ".dll"))
487486
++c;
488487
}
489-
utils.monodroid_closedir (dir);
488+
Util::monodroid_closedir (dir);
490489
}
491490

492491
return c;
@@ -565,7 +564,7 @@ AndroidSystem::setup_environment_from_override_file (const char *path) noexcept
565564
using read_count_type = size_t;
566565
#endif
567566
monodroid_stat_t sbuf;
568-
if (utils.monodroid_stat (path, &sbuf) < 0) {
567+
if (Util::monodroid_stat (path, &sbuf) < 0) {
569568
log_warn (LOG_DEFAULT, "Failed to stat the environment override file %s: %s", path, strerror (errno));
570569
return;
571570
}
@@ -717,8 +716,8 @@ AndroidSystem::setup_environment () noexcept
717716
#if defined (DEBUG) || !defined (ANDROID)
718717
// TODO: for debug read from file in the override directory named `environment`
719718
for (const auto override_dir : override_dirs ()) {
720-
std::unique_ptr<char[]> env_override_file {utils.path_combine (override_dir, OVERRIDE_ENVIRONMENT_FILE_NAME)};
721-
if (utils.file_exists (env_override_file.get ())) {
719+
std::unique_ptr<char[]> env_override_file {Util::path_combine (override_dir, OVERRIDE_ENVIRONMENT_FILE_NAME)};
720+
if (Util::file_exists (env_override_file.get ())) {
722721
setup_environment_from_override_file (env_override_file.get ());
723722
}
724723
}
@@ -783,15 +782,15 @@ AndroidSystem::get_libmonoandroid_directory_path () noexcept
783782

784783
GetModuleFileNameW (module, module_path, sizeof (module_path) / sizeof (module_path[0]));
785784
PathRemoveFileSpecW (module_path);
786-
libmonoandroid_directory_path = utils.utf16_to_utf8 (module_path);
785+
libmonoandroid_directory_path = Util::utf16_to_utf8 (module_path);
787786
return libmonoandroid_directory_path;
788787
}
789788

790789
int
791790
AndroidSystem::setenv (const char *name, const char *value, [[maybe_unused]] int overwrite) noexcept
792791
{
793-
wchar_t *wname = utils.utf8_to_utf16 (name);
794-
wchar_t *wvalue = utils.utf8_to_utf16 (value);
792+
wchar_t *wname = Util::utf8_to_utf16 (name);
793+
wchar_t *wvalue = Util::utf8_to_utf16 (value);
795794

796795
BOOL result = SetEnvironmentVariableW (wname, wvalue);
797796
free (wname);
@@ -803,7 +802,7 @@ AndroidSystem::setenv (const char *name, const char *value, [[maybe_unused]] int
803802
int
804803
AndroidSystem::symlink (const char *target, const char *linkpath) noexcept
805804
{
806-
return utils.file_copy (target, linkpath);
805+
return Util::file_copy (target, linkpath);
807806
}
808807
#else
809808
#endif

src/monodroid/jni/basic-android-system.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
#include "basic-android-system.hh"
44
#include "cpp-util.hh"
5-
#include "globals.hh"
5+
#include "shared-constants.hh"
6+
#include "util.hh"
67

78
using namespace xamarin::android;
89
using namespace xamarin::android::internal;
@@ -11,9 +12,9 @@ void
1112
BasicAndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcept
1213
{
1314
// appDirs[SharedConstants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory
14-
std::unique_ptr<char> libmonodroid_path {utils.path_combine (appDirs[SharedConstants::APP_DIRS_DATA_DIR_INDEX].get_cstr (), "libmonodroid.so")};
15+
std::unique_ptr<char> libmonodroid_path {Util::path_combine (appDirs[SharedConstants::APP_DIRS_DATA_DIR_INDEX].get_cstr (), "libmonodroid.so")};
1516
log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to %s", libmonodroid_path.get ());
16-
if (!utils.file_exists (libmonodroid_path.get ())) {
17+
if (!Util::file_exists (libmonodroid_path.get ())) {
1718
log_debug (LOG_ASSEMBLY, "%s not found, assuming application/android:extractNativeLibs == false", libmonodroid_path.get ());
1819
set_embedded_dso_mode_enabled (true);
1920
} else {
@@ -27,7 +28,7 @@ BasicAndroidSystem::setup_app_library_directories (jstring_array_wrapper& runtim
2728
{
2829
if (!is_embedded_dso_mode_enabled ()) {
2930
log_info (LOG_DEFAULT, "Setting up for DSO lookup in app data directories");
30-
app_lib_directories ().push_back (utils.strdup_new (appDirs[SharedConstants::APP_DIRS_DATA_DIR_INDEX].get_cstr ()));
31+
app_lib_directories ().push_back (Util::strdup_new (appDirs[SharedConstants::APP_DIRS_DATA_DIR_INDEX].get_cstr ()));
3132
log_debug (LOG_ASSEMBLY, "Added filesystem DSO lookup location: %s", appDirs[SharedConstants::APP_DIRS_DATA_DIR_INDEX].get_cstr ());
3233
} else {
3334
log_info (LOG_DEFAULT, "Setting up for DSO lookup directly in the APK");
@@ -53,7 +54,7 @@ BasicAndroidSystem::for_each_apk (jstring_array_wrapper &runtimeApks, ForEachApk
5354
force_inline void
5455
BasicAndroidSystem::add_apk_libdir (const char *apk, size_t &index, const char *abi) noexcept
5556
{
56-
app_lib_directories ().push_back (utils.string_concat (apk, "!/lib/", abi));
57+
app_lib_directories ().push_back (Util::string_concat (apk, "!/lib/", abi));
5758
log_debug (LOG_ASSEMBLY, "Added APK DSO lookup location: %s", app_lib_directories ()[index]);
5859
index++;
5960
}
@@ -69,7 +70,7 @@ BasicAndroidSystem::setup_apk_directories (unsigned short running_on_cpu, jstrin
6970
const char *apk = e.get_cstr ();
7071

7172
if (have_split_apks) {
72-
if (utils.ends_with (apk, SharedConstants::split_config_abi_apk_name)) {
73+
if (Util::ends_with (apk, SharedConstants::split_config_abi_apk_name)) {
7374
add_apk_libdir (apk, number_of_added_directories, abi);
7475
break;
7576
}
@@ -82,7 +83,7 @@ BasicAndroidSystem::setup_apk_directories (unsigned short running_on_cpu, jstrin
8283
gsl::owner<char*>
8384
BasicAndroidSystem::determine_primary_override_dir (jstring_wrapper &home) noexcept
8485
{
85-
return utils.path_combine (home.get_cstr (), ".__override__");
86+
return Util::path_combine (home.get_cstr (), ".__override__");
8687
}
8788

8889
const char*

src/monodroid/jni/basic-utilities.cc

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,6 @@ BasicUtilities::set_user_executable ([[maybe_unused]] const char *path) noexcept
125125
#endif
126126
}
127127

128-
bool
129-
BasicUtilities::file_exists (const char *file) noexcept
130-
{
131-
monodroid_stat_t s;
132-
if (monodroid_stat (file, &s) == 0 && (s.st_mode & S_IFMT) == S_IFREG)
133-
return true;
134-
return false;
135-
}
136-
137-
bool
138-
BasicUtilities::directory_exists (const char *directory) noexcept
139-
{
140-
monodroid_stat_t s;
141-
if (monodroid_stat (directory, &s) == 0 && (s.st_mode & S_IFMT) == S_IFDIR)
142-
return true;
143-
return false;
144-
}
145-
146128
bool
147129
BasicUtilities::file_copy (const char *to, const char *from) noexcept
148130
{

src/monodroid/jni/basic-utilities.hh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,20 @@ namespace xamarin::android
3939
static int create_directory (const char *pathname, mode_t mode) noexcept;
4040
static void set_world_accessable (const char *path) noexcept;
4141
static void set_user_executable (const char *path) noexcept;
42-
static bool file_exists (const char *file) noexcept;
43-
static bool directory_exists (const char *directory) noexcept;
44-
static bool file_copy (const char *to, const char *from) noexcept;
4542

43+
static bool file_exists (const char *file) noexcept
44+
{
45+
monodroid_stat_t s;
46+
return monodroid_stat (file, &s) == 0 && (s.st_mode & S_IFMT) == S_IFREG;
47+
}
48+
49+
static bool directory_exists (const char *directory) noexcept
50+
{
51+
monodroid_stat_t s;
52+
return monodroid_stat (directory, &s) == 0 && (s.st_mode & S_IFMT) == S_IFDIR;
53+
}
54+
55+
static bool file_copy (const char *to, const char *from) noexcept;
4656

4757
// Make sure that `buf` has enough space! This is by design, the methods are supposed to be fast.
4858
template<size_t MaxStackSpace, typename TBuffer>

0 commit comments

Comments
 (0)