From 51b5ff59ec3bc76ce1d000a1b5bef6a9d32ad82f Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 12:28:15 +0200 Subject: [PATCH 01/21] [Native] Join fallback log paths without dynamic strings Add a bounded caller-buffer path join helper and use it for reference log fallback paths, removing one dynamic_local_string use shared by CoreCLR and NativeAOT. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/runtime-base/util.hh | 36 +++++++++++++++++++++ src/native/clr/runtime-base/logger.cc | 14 ++++---- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index f3dd0b22e28..94f2833f0c9 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -5,9 +5,11 @@ #include #include +#include #include #include #include +#include #include #include @@ -330,6 +332,40 @@ namespace xamarin::android { return !path.empty () && path.contains ('/'); } + template + static auto join_paths (std::array& buffer, std::string_view first, std::string_view second) noexcept -> std::optional + { + static_assert (Size > 0); + + bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); + bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); + size_t second_offset = remove_duplicate_separator ? 1uz : 0uz; + size_t path_length = Helpers::add_with_overflow_check (first.length (), second.length () - second_offset); + if (add_separator) { + path_length = Helpers::add_with_overflow_check (path_length, 1uz); + } + + if (path_length >= Size) { + return std::nullopt; + } + + char *destination = buffer.data (); + if (!first.empty ()) { + memcpy (destination, first.data (), first.length ()); + destination += first.length (); + } + if (add_separator) { + *destination++ = '/'; + } + size_t second_length = second.length () - second_offset; + if (second_length > 0) { + memcpy (destination, second.data () + second_offset, second_length); + } + buffer [path_length] = '\0'; + + return std::string_view { buffer.data (), path_length }; + } + private: // TODO: needs some work to accept mixed params of different accepted types template TBuffer, detail::PathComponentString ...TPart> diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index ffbf9da565c..71f26c7ebfa 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -78,13 +78,15 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p } Util::create_public_directory (override_dir); - dynamic_local_string p; - p.append (override_dir) - .append ("/") - .append (fallback_filename); + std::array path_buffer; + auto path = Util::join_paths (path_buffer, override_dir, fallback_filename); + if (!path.has_value ()) { + log_warnf (category, "Unable to open fallback log file: path is too long"); + return nullptr; + } - std::string_view path = p.as_string_view (); - return log_and_return (open_file (path), path); + std::string_view path_view = *path; + return log_and_return (open_file (path_view), path_view); } void From 3472b76dabed87953b458cd1522813e43d3494d4 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 12:30:13 +0200 Subject: [PATCH 02/21] Accept caller-owned path buffers Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/runtime-base/util.hh | 13 +++++++------ src/native/clr/runtime-base/logger.cc | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 94f2833f0c9..0c1a9ed3b16 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -332,10 +332,11 @@ namespace xamarin::android { return !path.empty () && path.contains ('/'); } - template - static auto join_paths (std::array& buffer, std::string_view first, std::string_view second) noexcept -> std::optional + static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> std::optional { - static_assert (Size > 0); + if (buffer == nullptr || buffer_size == 0) { + return std::nullopt; + } bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); @@ -345,11 +346,11 @@ namespace xamarin::android { path_length = Helpers::add_with_overflow_check (path_length, 1uz); } - if (path_length >= Size) { + if (path_length >= buffer_size) { return std::nullopt; } - char *destination = buffer.data (); + char *destination = buffer; if (!first.empty ()) { memcpy (destination, first.data (), first.length ()); destination += first.length (); @@ -363,7 +364,7 @@ namespace xamarin::android { } buffer [path_length] = '\0'; - return std::string_view { buffer.data (), path_length }; + return std::string_view { buffer, path_length }; } private: diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index 71f26c7ebfa..69652830761 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -79,7 +79,7 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p Util::create_public_directory (override_dir); std::array path_buffer; - auto path = Util::join_paths (path_buffer, override_dir, fallback_filename); + auto path = Util::join_paths (path_buffer.data (), path_buffer.size (), override_dir, fallback_filename); if (!path.has_value ()) { log_warnf (category, "Unable to open fallback log file: path is too long"); return nullptr; From 3faeae4b9c6a00868c4347437df5e9dcf5d6ae65 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 12:41:38 +0200 Subject: [PATCH 03/21] Migrate additional two-part path joins Use caller-owned buffers for XDG directories, environment override files, and libmonodroid detection. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../clr/include/host/host-environment.hh | 26 ++++---- .../include/runtime-base/android-system.hh | 2 +- src/native/clr/runtime-base/android-system.cc | 63 ++++++++++++------- 3 files changed, 56 insertions(+), 35 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index d0c5a7ab871..7dd1fddf47c 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -89,34 +90,37 @@ namespace xamarin::android { private: [[gnu::flatten, gnu::always_inline]] - static 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 + static void create_xdg_directory (jstring_wrapper &home, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept { - static_local_string dir (home_len + relative_path.length ()); - Util::path_combine (dir, home.get_string_view (), relative_path); + std::array dir_buffer; + auto dir = Util::join_paths (dir_buffer.data (), dir_buffer.size (), home.get_string_view (), relative_path); + if (!dir.has_value ()) { + log_warnf (LOG_DEFAULT, "Failed to create XDG directory: path is too long"); + return; + } - log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", optional_string (dir.get ())); - int rv = Util::create_directory (dir.get (), Constants::DEFAULT_DIRECTORY_MODE); + const char *dir_path = dir->data (); + log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); + int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); if (rv < 0 && errno != EEXIST) { - log_warnf (LOG_DEFAULT, "Failed to create XDG directory %s. %s", optional_string (dir.get ()), strerror (errno)); + log_warnf (LOG_DEFAULT, "Failed to create XDG directory %s. %s", dir_path, strerror (errno)); } if (!environment_variable_name.empty ()) { - set_variable (environment_variable_name.data (), dir.get ()); + set_variable (environment_variable_name.data (), dir_path); } } [[gnu::flatten, gnu::always_inline]] static void create_xdg_directories_and_environment (jstring_wrapper &homeDir) noexcept { - size_t home_len = strlen (homeDir.get_cstr ()); - constexpr auto XDG_DATA_HOME = "XDG_DATA_HOME"sv; constexpr auto HOME_PATH = ".local/share"sv; - create_xdg_directory (homeDir, home_len, HOME_PATH, XDG_DATA_HOME); + create_xdg_directory (homeDir, HOME_PATH, XDG_DATA_HOME); constexpr auto XDG_CONFIG_HOME = "XDG_CONFIG_HOME"sv; constexpr auto CONFIG_PATH = ".config"sv; - create_xdg_directory (homeDir, home_len, CONFIG_PATH, XDG_CONFIG_HOME); + create_xdg_directory (homeDir, CONFIG_PATH, XDG_CONFIG_HOME); } public: diff --git a/src/native/clr/include/runtime-base/android-system.hh b/src/native/clr/include/runtime-base/android-system.hh index fe5be44035e..356d36a8dc7 100644 --- a/src/native/clr/include/runtime-base/android-system.hh +++ b/src/native/clr/include/runtime-base/android-system.hh @@ -148,7 +148,7 @@ namespace xamarin::android { #if defined(DEBUG) static void add_system_property (const char *name, const char *value) noexcept; static void setup_environment (const char *name, const char *value) noexcept; - static void setup_environment_from_override_file (dynamic_local_string const& path) noexcept; + static void setup_environment_from_override_file (std::string_view path) noexcept; #endif static void set_embedded_dso_mode_enabled (bool yesno) noexcept diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index aee00900d55..f1be1b4a116 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -1,3 +1,4 @@ +#include #include #include @@ -58,19 +59,19 @@ AndroidSystem::setup_environment (const char *name, const char *value) noexcept } void -AndroidSystem::setup_environment_from_override_file (dynamic_local_string const& path) noexcept +AndroidSystem::setup_environment_from_override_file (std::string_view path) noexcept { using read_count_type = size_t; struct stat sbuf; - if (::stat (path.get (), &sbuf) < 0) { - log_warn (LOG_DEFAULT, "Failed to stat the environment override file {}: {}", path.get (), strerror (errno)); + if (::stat (path.data (), &sbuf) < 0) { + log_warn (LOG_DEFAULT, "Failed to stat the environment override file {}: {}", path, strerror (errno)); return; } - int fd = open (path.get (), O_RDONLY); + int fd = open (path.data (), O_RDONLY); if (fd < 0) { - log_warn (LOG_DEFAULT, "Failed to open the environment override file {}: {}", path.get (), strerror (errno)); + log_warn (LOG_DEFAULT, "Failed to open the environment override file {}: {}", path, strerror (errno)); return; } @@ -88,7 +89,7 @@ AndroidSystem::setup_environment_from_override_file (dynamic_local_string::max () && errno == ERANGE) || (buf[0] != '\0' && *endptr != '\0')) { - log_warn (LOG_DEFAULT, "Malformed header of the environment override file {}: name width has invalid format", path.get ()); + log_warn (LOG_DEFAULT, "Malformed header of the environment override file {}: name width has invalid format", path); return; } unsigned long value_width = strtoul (buf.get () + 11, &endptr, 16); if ((value_width == std::numeric_limits::max () && errno == ERANGE) || (buf[0] != '\0' && *endptr != '\0')) { - log_warn (LOG_DEFAULT, "Malformed header of the environment override file {}: value width has invalid format", path.get ()); + log_warn (LOG_DEFAULT, "Malformed header of the environment override file {}: value width has invalid format", path); return; } uint64_t data_width = name_width + value_width; if (data_width > file_size - Constants::OVERRIDE_ENVIRONMENT_FILE_HEADER_SIZE || (file_size - Constants::OVERRIDE_ENVIRONMENT_FILE_HEADER_SIZE) % data_width != 0) { - log_warn (LOG_DEFAULT, "Malformed environment override file {}: invalid data size", path.get ()); + log_warn (LOG_DEFAULT, "Malformed environment override file {}: invalid data size", path); return; } @@ -136,11 +137,11 @@ AndroidSystem::setup_environment_from_override_file (dynamic_local_string 0 && data_size >= data_width) { if (*name == '\0') { - log_warn (LOG_DEFAULT, "Malformed environment override file {}: name at offset {} is empty", path.get (), name - buf.get ()); + log_warn (LOG_DEFAULT, "Malformed environment override file {}: name at offset {} is empty", path, name - buf.get ()); return; } - log_debug (LOG_DEFAULT, "Setting environment variable from the override file {}: '{}' = '{}'", path.get (), name, name + name_width); + log_debug (LOG_DEFAULT, "Setting environment variable from the override file {}: '{}' = '{}'", path, name, name + name_width); setup_environment (name, name + name_width); name += data_width; data_size -= data_width; @@ -257,12 +258,18 @@ AndroidSystem::setup_environment () noexcept #if defined(DEBUG) log_debug (LOG_DEFAULT, "Loading environment from the override directory."sv); - dynamic_local_string env_override_file; - Util::path_combine (env_override_file, std::string_view {primary_override_dir}, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); - log_debug (LOG_DEFAULT, "{}", env_override_file.get ()); - if (Util::file_exists (env_override_file)) { - log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file.get ()); - setup_environment_from_override_file (env_override_file); + std::array env_override_file_buffer; + auto env_override_file = Util::join_paths ( + env_override_file_buffer.data (), + env_override_file_buffer.size (), + primary_override_dir, + Constants::OVERRIDE_ENVIRONMENT_FILE_NAME + ); + if (!env_override_file.has_value ()) { + log_warn (LOG_DEFAULT, "Environment override file path is too long"); + } else if (Util::file_exists (*env_override_file)) { + log_debug (LOG_DEFAULT, "Loading {}"sv, *env_override_file); + setup_environment_from_override_file (*env_override_file); } #endif // def DEBUG } @@ -271,12 +278,22 @@ void AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcept { // appDirs[Constants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory - dynamic_local_string libmonodroid_path; - Util::path_combine (libmonodroid_path, appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (), "libmonodroid.so"sv); + std::array libmonodroid_path_buffer; + auto libmonodroid_path = Util::join_paths ( + libmonodroid_path_buffer.data (), + libmonodroid_path_buffer.size (), + appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (), + "libmonodroid.so"sv + ); + if (!libmonodroid_path.has_value ()) { + log_warn (LOG_ASSEMBLY, "libmonodroid path is too long, assuming application/android:extractNativeLibs == false"); + set_embedded_dso_mode_enabled (true); + return; + } - log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path.get ()); - if (!Util::file_exists (libmonodroid_path)) { - log_debug (LOG_ASSEMBLY, "{} not found, assuming application/android:extractNativeLibs == false", libmonodroid_path.get ()); + log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", *libmonodroid_path); + if (!Util::file_exists (*libmonodroid_path)) { + log_debug (LOG_ASSEMBLY, "{} not found, assuming application/android:extractNativeLibs == false", *libmonodroid_path); set_embedded_dso_mode_enabled (true); } else { log_debug (LOG_ASSEMBLY, "Native libs extracted to {}, assuming application/android:extractNativeLibs == true", appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_cstr ()); From 0b08b7e7354d25d2e490e74aa7e0aa74074ea683 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 12:50:58 +0200 Subject: [PATCH 04/21] Return joined path length Return bytes written including the terminating NUL so callers can use their buffers directly and create string views only when needed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../clr/include/host/host-environment.hh | 5 ++-- .../include/runtime-base/android-system.hh | 2 +- src/native/clr/include/runtime-base/util.hh | 9 ++++--- src/native/clr/runtime-base/android-system.cc | 27 ++++++++++--------- src/native/clr/runtime-base/logger.cc | 6 ++--- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index 7dd1fddf47c..d901d3e2eea 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -93,13 +93,12 @@ namespace xamarin::android { static void create_xdg_directory (jstring_wrapper &home, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept { std::array dir_buffer; - auto dir = Util::join_paths (dir_buffer.data (), dir_buffer.size (), home.get_string_view (), relative_path); - if (!dir.has_value ()) { + if (Util::join_paths (dir_buffer.data (), dir_buffer.size (), home.get_string_view (), relative_path) == 0) { log_warnf (LOG_DEFAULT, "Failed to create XDG directory: path is too long"); return; } - const char *dir_path = dir->data (); + const char *dir_path = dir_buffer.data (); log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); if (rv < 0 && errno != EEXIST) { diff --git a/src/native/clr/include/runtime-base/android-system.hh b/src/native/clr/include/runtime-base/android-system.hh index 356d36a8dc7..380407eac1a 100644 --- a/src/native/clr/include/runtime-base/android-system.hh +++ b/src/native/clr/include/runtime-base/android-system.hh @@ -148,7 +148,7 @@ namespace xamarin::android { #if defined(DEBUG) static void add_system_property (const char *name, const char *value) noexcept; static void setup_environment (const char *name, const char *value) noexcept; - static void setup_environment_from_override_file (std::string_view path) noexcept; + static void setup_environment_from_override_file (const char *path) noexcept; #endif static void set_embedded_dso_mode_enabled (bool yesno) noexcept diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 0c1a9ed3b16..8cee1329083 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -332,10 +332,11 @@ namespace xamarin::android { return !path.empty () && path.contains ('/'); } - static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> std::optional + // Returns the number of bytes written, including the terminating NUL, or 0 on failure. + static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> size_t { if (buffer == nullptr || buffer_size == 0) { - return std::nullopt; + return 0; } bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); @@ -347,7 +348,7 @@ namespace xamarin::android { } if (path_length >= buffer_size) { - return std::nullopt; + return 0; } char *destination = buffer; @@ -364,7 +365,7 @@ namespace xamarin::android { } buffer [path_length] = '\0'; - return std::string_view { buffer, path_length }; + return path_length + 1uz; } private: diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index f1be1b4a116..e3966779278 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -59,17 +59,17 @@ AndroidSystem::setup_environment (const char *name, const char *value) noexcept } void -AndroidSystem::setup_environment_from_override_file (std::string_view path) noexcept +AndroidSystem::setup_environment_from_override_file (const char *path) noexcept { using read_count_type = size_t; struct stat sbuf; - if (::stat (path.data (), &sbuf) < 0) { + if (::stat (path, &sbuf) < 0) { log_warn (LOG_DEFAULT, "Failed to stat the environment override file {}: {}", path, strerror (errno)); return; } - int fd = open (path.data (), O_RDONLY); + int fd = open (path, O_RDONLY); if (fd < 0) { log_warn (LOG_DEFAULT, "Failed to open the environment override file {}: {}", path, strerror (errno)); return; @@ -259,17 +259,17 @@ AndroidSystem::setup_environment () noexcept log_debug (LOG_DEFAULT, "Loading environment from the override directory."sv); std::array env_override_file_buffer; - auto env_override_file = Util::join_paths ( + size_t env_override_file_length = Util::join_paths ( env_override_file_buffer.data (), env_override_file_buffer.size (), primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME ); - if (!env_override_file.has_value ()) { + if (env_override_file_length == 0) { log_warn (LOG_DEFAULT, "Environment override file path is too long"); - } else if (Util::file_exists (*env_override_file)) { - log_debug (LOG_DEFAULT, "Loading {}"sv, *env_override_file); - setup_environment_from_override_file (*env_override_file); + } else if (Util::file_exists (env_override_file_buffer.data ())) { + log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file_buffer.data ()); + setup_environment_from_override_file (env_override_file_buffer.data ()); } #endif // def DEBUG } @@ -279,21 +279,22 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep { // appDirs[Constants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory std::array libmonodroid_path_buffer; - auto libmonodroid_path = Util::join_paths ( + size_t libmonodroid_path_length = Util::join_paths ( libmonodroid_path_buffer.data (), libmonodroid_path_buffer.size (), appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (), "libmonodroid.so"sv ); - if (!libmonodroid_path.has_value ()) { + if (libmonodroid_path_length == 0) { log_warn (LOG_ASSEMBLY, "libmonodroid path is too long, assuming application/android:extractNativeLibs == false"); set_embedded_dso_mode_enabled (true); return; } - log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", *libmonodroid_path); - if (!Util::file_exists (*libmonodroid_path)) { - log_debug (LOG_ASSEMBLY, "{} not found, assuming application/android:extractNativeLibs == false", *libmonodroid_path); + const char *libmonodroid_path = libmonodroid_path_buffer.data (); + log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path); + if (!Util::file_exists (libmonodroid_path)) { + log_debug (LOG_ASSEMBLY, "{} not found, assuming application/android:extractNativeLibs == false", libmonodroid_path); set_embedded_dso_mode_enabled (true); } else { log_debug (LOG_ASSEMBLY, "Native libs extracted to {}, assuming application/android:extractNativeLibs == true", appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_cstr ()); diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index 69652830761..989c64c73c7 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -79,13 +79,13 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p Util::create_public_directory (override_dir); std::array path_buffer; - auto path = Util::join_paths (path_buffer.data (), path_buffer.size (), override_dir, fallback_filename); - if (!path.has_value ()) { + size_t path_length = Util::join_paths (path_buffer.data (), path_buffer.size (), override_dir, fallback_filename); + if (path_length == 0) { log_warnf (category, "Unable to open fallback log file: path is too long"); return nullptr; } - std::string_view path_view = *path; + std::string_view path_view { path_buffer.data (), path_length - 1uz }; return log_and_return (open_file (path_view), path_view); } From f8662654dfcad9abe2f21f23e7a6359b07b30146 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 12:56:12 +0200 Subject: [PATCH 05/21] Use conventional joined path length Return -1 on failure and the path length excluding the terminating NUL on success, matching strlen-style length semantics. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/host/host-environment.hh | 2 +- src/native/clr/include/runtime-base/util.hh | 13 +++++++------ src/native/clr/runtime-base/android-system.cc | 8 ++++---- src/native/clr/runtime-base/logger.cc | 6 +++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index d901d3e2eea..a8d0d71d9ca 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -93,7 +93,7 @@ namespace xamarin::android { static void create_xdg_directory (jstring_wrapper &home, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept { std::array dir_buffer; - if (Util::join_paths (dir_buffer.data (), dir_buffer.size (), home.get_string_view (), relative_path) == 0) { + if (Util::join_paths (dir_buffer.data (), dir_buffer.size (), home.get_string_view (), relative_path) < 0) { log_warnf (LOG_DEFAULT, "Failed to create XDG directory: path is too long"); return; } diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 8cee1329083..ebe14ebca13 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -332,11 +333,11 @@ namespace xamarin::android { return !path.empty () && path.contains ('/'); } - // Returns the number of bytes written, including the terminating NUL, or 0 on failure. - static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> size_t + // Returns the path length excluding the terminating NUL, or -1 on failure. + static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> ssize_t { if (buffer == nullptr || buffer_size == 0) { - return 0; + return -1; } bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); @@ -347,8 +348,8 @@ namespace xamarin::android { path_length = Helpers::add_with_overflow_check (path_length, 1uz); } - if (path_length >= buffer_size) { - return 0; + if (path_length >= buffer_size || path_length > static_cast(std::numeric_limits::max ())) { + return -1; } char *destination = buffer; @@ -365,7 +366,7 @@ namespace xamarin::android { } buffer [path_length] = '\0'; - return path_length + 1uz; + return static_cast(path_length); } private: diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index e3966779278..32415ff4dd0 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -259,13 +259,13 @@ AndroidSystem::setup_environment () noexcept log_debug (LOG_DEFAULT, "Loading environment from the override directory."sv); std::array env_override_file_buffer; - size_t env_override_file_length = Util::join_paths ( + ssize_t env_override_file_length = Util::join_paths ( env_override_file_buffer.data (), env_override_file_buffer.size (), primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME ); - if (env_override_file_length == 0) { + if (env_override_file_length < 0) { log_warn (LOG_DEFAULT, "Environment override file path is too long"); } else if (Util::file_exists (env_override_file_buffer.data ())) { log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file_buffer.data ()); @@ -279,13 +279,13 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep { // appDirs[Constants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory std::array libmonodroid_path_buffer; - size_t libmonodroid_path_length = Util::join_paths ( + ssize_t libmonodroid_path_length = Util::join_paths ( libmonodroid_path_buffer.data (), libmonodroid_path_buffer.size (), appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (), "libmonodroid.so"sv ); - if (libmonodroid_path_length == 0) { + if (libmonodroid_path_length < 0) { log_warn (LOG_ASSEMBLY, "libmonodroid path is too long, assuming application/android:extractNativeLibs == false"); set_embedded_dso_mode_enabled (true); return; diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index 989c64c73c7..37a4f74bb61 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -79,13 +79,13 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p Util::create_public_directory (override_dir); std::array path_buffer; - size_t path_length = Util::join_paths (path_buffer.data (), path_buffer.size (), override_dir, fallback_filename); - if (path_length == 0) { + ssize_t path_length = Util::join_paths (path_buffer.data (), path_buffer.size (), override_dir, fallback_filename); + if (path_length < 0) { log_warnf (category, "Unable to open fallback log file: path is too long"); return nullptr; } - std::string_view path_view { path_buffer.data (), path_length - 1uz }; + std::string_view path_view { path_buffer.data (), static_cast(path_length) }; return log_and_return (open_file (path_view), path_view); } From c6f26f0d93f0dfbf39a3bbe331a23bd92916076d Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 13:06:06 +0200 Subject: [PATCH 06/21] Use plain path buffers Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../clr/include/host/host-environment.hh | 7 +++---- src/native/clr/runtime-base/android-system.cc | 21 +++++++++---------- src/native/clr/runtime-base/logger.cc | 7 +++---- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index a8d0d71d9ca..7535267a0ee 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -2,7 +2,6 @@ #include -#include #include #include #include @@ -92,13 +91,13 @@ namespace xamarin::android { [[gnu::flatten, gnu::always_inline]] static void create_xdg_directory (jstring_wrapper &home, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept { - std::array dir_buffer; - if (Util::join_paths (dir_buffer.data (), dir_buffer.size (), home.get_string_view (), relative_path) < 0) { + char dir_buffer [SENSIBLE_PATH_MAX]; + if (Util::join_paths (dir_buffer, sizeof (dir_buffer), home.get_string_view (), relative_path) < 0) { log_warnf (LOG_DEFAULT, "Failed to create XDG directory: path is too long"); return; } - const char *dir_path = dir_buffer.data (); + const char *dir_path = dir_buffer; log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); if (rv < 0 && errno != EEXIST) { diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index 32415ff4dd0..e6e2a9e54d9 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -1,4 +1,3 @@ -#include #include #include @@ -258,18 +257,18 @@ AndroidSystem::setup_environment () noexcept #if defined(DEBUG) log_debug (LOG_DEFAULT, "Loading environment from the override directory."sv); - std::array env_override_file_buffer; + char env_override_file_buffer [Constants::SENSIBLE_PATH_MAX]; ssize_t env_override_file_length = Util::join_paths ( - env_override_file_buffer.data (), - env_override_file_buffer.size (), + env_override_file_buffer, + sizeof (env_override_file_buffer), primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME ); if (env_override_file_length < 0) { log_warn (LOG_DEFAULT, "Environment override file path is too long"); - } else if (Util::file_exists (env_override_file_buffer.data ())) { - log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file_buffer.data ()); - setup_environment_from_override_file (env_override_file_buffer.data ()); + } else if (Util::file_exists (env_override_file_buffer)) { + log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file_buffer); + setup_environment_from_override_file (env_override_file_buffer); } #endif // def DEBUG } @@ -278,10 +277,10 @@ void AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcept { // appDirs[Constants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory - std::array libmonodroid_path_buffer; + char libmonodroid_path_buffer [Constants::SENSIBLE_PATH_MAX]; ssize_t libmonodroid_path_length = Util::join_paths ( - libmonodroid_path_buffer.data (), - libmonodroid_path_buffer.size (), + libmonodroid_path_buffer, + sizeof (libmonodroid_path_buffer), appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (), "libmonodroid.so"sv ); @@ -291,7 +290,7 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep return; } - const char *libmonodroid_path = libmonodroid_path_buffer.data (); + const char *libmonodroid_path = libmonodroid_path_buffer; log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path); if (!Util::file_exists (libmonodroid_path)) { log_debug (LOG_ASSEMBLY, "{} not found, assuming application/android:extractNativeLibs == false", libmonodroid_path); diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index 37a4f74bb61..f3c4976b407 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -1,4 +1,3 @@ -#include #include #include #include @@ -78,14 +77,14 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p } Util::create_public_directory (override_dir); - std::array path_buffer; - ssize_t path_length = Util::join_paths (path_buffer.data (), path_buffer.size (), override_dir, fallback_filename); + char path_buffer [Constants::SENSIBLE_PATH_MAX]; + ssize_t path_length = Util::join_paths (path_buffer, sizeof (path_buffer), override_dir, fallback_filename); if (path_length < 0) { log_warnf (category, "Unable to open fallback log file: path is too long"); return nullptr; } - std::string_view path_view { path_buffer.data (), static_cast(path_length) }; + std::string_view path_view { path_buffer, static_cast(path_length) }; return log_and_return (open_file (path_view), path_view); } From b287fc42ede4fc5eddbdf14d1c2602e6cf01edc3 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 13:11:13 +0200 Subject: [PATCH 07/21] Use plain hexadecimal lookup table Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/runtime-base/util.hh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index ebe14ebca13..059df09656c 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -5,7 +5,6 @@ #include #include -#include #include #include #include @@ -48,7 +47,7 @@ namespace xamarin::android { class Util { - static constexpr inline std::array hex_map { + static constexpr inline char hex_map [16] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; From 89b97269d2ef8fc20a748d5fef0ba21701094421 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 13:24:04 +0200 Subject: [PATCH 08/21] Use join_paths for timing output Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../common/runtime-base/timing-internal.cc | 18 +++++---- src/native/mono/runtime-base/util.hh | 38 +++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/native/common/runtime-base/timing-internal.cc b/src/native/common/runtime-base/timing-internal.cc index 209e1af6eb4..fd44deffe1b 100644 --- a/src/native/common/runtime-base/timing-internal.cc +++ b/src/native/common/runtime-base/timing-internal.cc @@ -190,27 +190,29 @@ void FastTiming::dump_to_file (size_t entries) noexcept return; } - dynamic_local_path_string timing_log_path; + char timing_log_path [SENSIBLE_PATH_MAX]; // We can count on the envvar being there, since we set it ourselves at startup // Note that to access the file for a release app, the app must be made debuggable // and `run-as` must be used. - timing_log_path.assign_c (getenv("TMPDIR")); - timing_log_path.append ("/"sv); - timing_log_path.append (output_file_name == nullptr ? default_timing_file_name : *output_file_name); + std::string_view file_name = output_file_name == nullptr ? default_timing_file_name : *output_file_name; + if (Util::join_paths (timing_log_path, sizeof (timing_log_path), getenv ("TMPDIR"), file_name) < 0) { + log_error (LOG_TIMING, "[2/2] Unable to create the performance measurements file: path is too long"); + return; + } - FILE *timing_log = Util::monodroid_fopen (timing_log_path.get (), "w"); + FILE *timing_log = Util::monodroid_fopen (timing_log_path, "w"); if (timing_log == nullptr) { - log_error (LOG_TIMING, "[2/2] Unable to create the performance measurements file '{}'"sv, timing_log_path.get ()); + log_error (LOG_TIMING, "[2/2] Unable to create the performance measurements file '{}'"sv, timing_log_path); return; } if (!Util::set_world_accessible (fileno (timing_log))) { - log_warn (LOG_TIMING, "[2/2] Failed to make performance measurements file '{}' world-readable"sv, timing_log_path.get ()); + log_warn (LOG_TIMING, "[2/2] Failed to make performance measurements file '{}' world-readable"sv, timing_log_path); return; } - log_info (LOG_TIMING, "[2/2] Performance measurement results logged to file: {}"sv, timing_log_path.get ()); + log_info (LOG_TIMING, "[2/2] Performance measurement results logged to file: {}"sv, timing_log_path); auto line_writer = [=](std::string_view const& msg) { if (!msg.empty ()) { diff --git a/src/native/mono/runtime-base/util.hh b/src/native/mono/runtime-base/util.hh index 8f4e6882bfe..797656f1c23 100644 --- a/src/native/mono/runtime-base/util.hh +++ b/src/native/mono/runtime-base/util.hh @@ -24,6 +24,7 @@ static inline constexpr int FALSE = 0; #include #include #include +#include #include #include #include @@ -42,6 +43,7 @@ static inline constexpr int FALSE = 0; #include #include +#include #include "java-interop-util.h" #include "logger.hh" #include @@ -107,6 +109,42 @@ namespace xamarin::android return fd; } + // Returns the path length excluding the terminating NUL, or -1 on failure. + static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> ssize_t + { + if (buffer == nullptr || buffer_size == 0) { + return -1; + } + + bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); + bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); + size_t second_offset = remove_duplicate_separator ? 1uz : 0uz; + size_t path_length = Helpers::add_with_overflow_check (first.length (), second.length () - second_offset); + if (add_separator) { + path_length = Helpers::add_with_overflow_check (path_length, 1uz); + } + + if (path_length >= buffer_size || path_length > static_cast(std::numeric_limits::max ())) { + return -1; + } + + char *destination = buffer; + if (!first.empty ()) { + memcpy (destination, first.data (), first.length ()); + destination += first.length (); + } + if (add_separator) { + *destination++ = '/'; + } + size_t second_length = second.length () - second_offset; + if (second_length > 0) { + memcpy (destination, second.data () + second_offset, second_length); + } + buffer [path_length] = '\0'; + + return static_cast(path_length); + } + // Make sure that `buf` has enough space! This is by design, the methods are supposed to be fast. template static void path_combine (TBuffer& buf, const char* path1, const char* path2) noexcept From 8550663bd25041da5116022e4005682d127ee4d5 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 16:51:18 +0200 Subject: [PATCH 09/21] Remove obsolete local-string path helpers The join_paths migrations remove the remaining callers of the local-string file_exists and path_combine helpers, so delete the dead overload family and strings.hh dependency. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/runtime-base/util.hh | 61 --------------------- 1 file changed, 61 deletions(-) diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 059df09656c..014426b6922 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -17,7 +16,6 @@ #include #include #include "logger.hh" -#include #if !defined(XA_HOST_NATIVEAOT) #include "archive-dso-stub-config.hh" @@ -25,19 +23,6 @@ namespace xamarin::android { namespace detail { - template - concept PathComponentString = requires { - std::same_as, char*> || - std::same_as, std::string_view> || - std::same_as, std::string>; - }; - - template - concept PathBuffer = requires { - std::derived_from, dynamic_local_storage> || - std::derived_from, static_local_storage>; - }; - struct mmap_info { void *area; @@ -121,16 +106,6 @@ namespace xamarin::android { return file_exists_no_null_check (file); } - template - static auto file_exists (dynamic_local_string const& file) noexcept -> bool - { - if (file.empty ()) { - return false; - } - - return file_exists_no_null_check (file.get ()); - } - static auto file_exists (int dirfd, std::string_view const& file) noexcept -> bool { struct stat sbuf; @@ -367,42 +342,6 @@ namespace xamarin::android { return static_cast(path_length); } - - private: - // TODO: needs some work to accept mixed params of different accepted types - template TBuffer, detail::PathComponentString ...TPart> - static void path_combine_common (TBuffer& buf, TPart&&... parts) noexcept - { - buf.clear (); - - for (auto const& part : {parts...}) { - if (!buf.empty ()) { - buf.append ("/"sv); - } - - if constexpr (std::same_as, char*>) { - if (part != nullptr) { - buf.append_c (part); - } - } else { - buf.append (part); - } - } - } - - public: - template - static void path_combine (dynamic_local_string& buf, TParts&&... parts) noexcept - { - path_combine_common (buf, std::forward(parts)...); - } - - template - static void path_combine (static_local_string& buf, TParts&&... parts) noexcept - { - path_combine_common (buf, std::forward(parts)...); - } - private: static inline int page_size = getpagesize (); }; From 02349b16a4efec3d931057e9a36367be2c2e5740 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 16:55:18 +0200 Subject: [PATCH 10/21] Remove obsolete strings include from host environment Use the CLR path capacity constant directly now that XDG paths use caller-owned buffers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/host/host-environment.hh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index 7535267a0ee..d3baf1bfc74 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -9,7 +9,6 @@ #include #include -#include #include struct AppEnvironmentVariable; @@ -91,7 +90,7 @@ namespace xamarin::android { [[gnu::flatten, gnu::always_inline]] static void create_xdg_directory (jstring_wrapper &home, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept { - char dir_buffer [SENSIBLE_PATH_MAX]; + char dir_buffer [Constants::SENSIBLE_PATH_MAX]; if (Util::join_paths (dir_buffer, sizeof (dir_buffer), home.get_string_view (), relative_path) < 0) { log_warnf (LOG_DEFAULT, "Failed to create XDG directory: path is too long"); return; From 0df55c57fafe2f44034bf93b1362c80467daae52 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 17:20:03 +0200 Subject: [PATCH 11/21] Allocate joined paths to their exact size Preserve dynamic_local_string semantics by treating SENSIBLE_PATH_MAX as an inline-storage threshold rather than a path limit. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../clr/include/host/host-environment.hh | 9 ++----- src/native/clr/include/runtime-base/util.hh | 27 ++++++++++--------- src/native/clr/runtime-base/android-system.cc | 25 +++++------------ src/native/clr/runtime-base/logger.cc | 11 +++----- .../common/runtime-base/timing-internal.cc | 8 ++---- 5 files changed, 27 insertions(+), 53 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index d3baf1bfc74..306fbfb87f3 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -90,13 +90,8 @@ namespace xamarin::android { [[gnu::flatten, gnu::always_inline]] static void create_xdg_directory (jstring_wrapper &home, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept { - char dir_buffer [Constants::SENSIBLE_PATH_MAX]; - if (Util::join_paths (dir_buffer, sizeof (dir_buffer), home.get_string_view (), relative_path) < 0) { - log_warnf (LOG_DEFAULT, "Failed to create XDG directory: path is too long"); - return; - } - - const char *dir_path = dir_buffer; + auto dir_buffer = Util::join_paths (home.get_string_view (), relative_path); + const char *dir_path = dir_buffer.get (); log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); if (rv < 0 && errno != EEXIST) { diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 014426b6922..50c52ee6c54 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -307,26 +308,19 @@ namespace xamarin::android { return !path.empty () && path.contains ('/'); } - // Returns the path length excluding the terminating NUL, or -1 on failure. - static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> ssize_t + static auto join_paths (std::string_view first, std::string_view second, size_t &path_length) noexcept -> std::unique_ptr { - if (buffer == nullptr || buffer_size == 0) { - return -1; - } - bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); size_t second_offset = remove_duplicate_separator ? 1uz : 0uz; - size_t path_length = Helpers::add_with_overflow_check (first.length (), second.length () - second_offset); + path_length = Helpers::add_with_overflow_check (first.length (), second.length () - second_offset); if (add_separator) { path_length = Helpers::add_with_overflow_check (path_length, 1uz); } - if (path_length >= buffer_size || path_length > static_cast(std::numeric_limits::max ())) { - return -1; - } - - char *destination = buffer; + size_t allocation_size = Helpers::add_with_overflow_check (path_length, 1uz); + auto buffer = std::make_unique (allocation_size); + char *destination = buffer.get (); if (!first.empty ()) { memcpy (destination, first.data (), first.length ()); destination += first.length (); @@ -340,8 +334,15 @@ namespace xamarin::android { } buffer [path_length] = '\0'; - return static_cast(path_length); + return buffer; } + + static auto join_paths (std::string_view first, std::string_view second) noexcept -> std::unique_ptr + { + size_t path_length; + return join_paths (first, second, path_length); + } + private: static inline int page_size = getpagesize (); }; diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index e6e2a9e54d9..28d33f5ba86 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -257,18 +257,13 @@ AndroidSystem::setup_environment () noexcept #if defined(DEBUG) log_debug (LOG_DEFAULT, "Loading environment from the override directory."sv); - char env_override_file_buffer [Constants::SENSIBLE_PATH_MAX]; - ssize_t env_override_file_length = Util::join_paths ( - env_override_file_buffer, - sizeof (env_override_file_buffer), + auto env_override_file_buffer = Util::join_paths ( primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME ); - if (env_override_file_length < 0) { - log_warn (LOG_DEFAULT, "Environment override file path is too long"); - } else if (Util::file_exists (env_override_file_buffer)) { - log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file_buffer); - setup_environment_from_override_file (env_override_file_buffer); + if (Util::file_exists (env_override_file_buffer.get ())) { + log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file_buffer.get ()); + setup_environment_from_override_file (env_override_file_buffer.get ()); } #endif // def DEBUG } @@ -277,20 +272,12 @@ void AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcept { // appDirs[Constants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory - char libmonodroid_path_buffer [Constants::SENSIBLE_PATH_MAX]; - ssize_t libmonodroid_path_length = Util::join_paths ( - libmonodroid_path_buffer, - sizeof (libmonodroid_path_buffer), + auto libmonodroid_path_buffer = Util::join_paths ( appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (), "libmonodroid.so"sv ); - if (libmonodroid_path_length < 0) { - log_warn (LOG_ASSEMBLY, "libmonodroid path is too long, assuming application/android:extractNativeLibs == false"); - set_embedded_dso_mode_enabled (true); - return; - } - const char *libmonodroid_path = libmonodroid_path_buffer; + const char *libmonodroid_path = libmonodroid_path_buffer.get (); log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path); if (!Util::file_exists (libmonodroid_path)) { log_debug (LOG_ASSEMBLY, "{} not found, assuming application/android:extractNativeLibs == false", libmonodroid_path); diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index f3c4976b407..4516732f0d5 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -77,14 +77,9 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p } Util::create_public_directory (override_dir); - char path_buffer [Constants::SENSIBLE_PATH_MAX]; - ssize_t path_length = Util::join_paths (path_buffer, sizeof (path_buffer), override_dir, fallback_filename); - if (path_length < 0) { - log_warnf (category, "Unable to open fallback log file: path is too long"); - return nullptr; - } - - std::string_view path_view { path_buffer, static_cast(path_length) }; + size_t path_length; + auto path_buffer = Util::join_paths (override_dir, fallback_filename, path_length); + std::string_view path_view { path_buffer.get (), path_length }; return log_and_return (open_file (path_view), path_view); } diff --git a/src/native/common/runtime-base/timing-internal.cc b/src/native/common/runtime-base/timing-internal.cc index fd44deffe1b..ad90167c504 100644 --- a/src/native/common/runtime-base/timing-internal.cc +++ b/src/native/common/runtime-base/timing-internal.cc @@ -190,16 +190,12 @@ void FastTiming::dump_to_file (size_t entries) noexcept return; } - char timing_log_path [SENSIBLE_PATH_MAX]; - // We can count on the envvar being there, since we set it ourselves at startup // Note that to access the file for a release app, the app must be made debuggable // and `run-as` must be used. std::string_view file_name = output_file_name == nullptr ? default_timing_file_name : *output_file_name; - if (Util::join_paths (timing_log_path, sizeof (timing_log_path), getenv ("TMPDIR"), file_name) < 0) { - log_error (LOG_TIMING, "[2/2] Unable to create the performance measurements file: path is too long"); - return; - } + auto timing_log_path_buffer = Util::join_paths (getenv ("TMPDIR"), file_name); + const char *timing_log_path = timing_log_path_buffer.get (); FILE *timing_log = Util::monodroid_fopen (timing_log_path, "w"); if (timing_log == nullptr) { From f3a31f448006c08d0963d8f0a67fb19d40a035c5 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 17:28:38 +0200 Subject: [PATCH 12/21] Use C allocation for joined paths Keep exact-size path allocation independent of libc++ and free each temporary after its final consumer. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/host/host-environment.hh | 5 +++-- src/native/clr/include/runtime-base/util.hh | 11 ++++++----- src/native/clr/runtime-base/android-system.cc | 14 ++++++++------ src/native/clr/runtime-base/logger.cc | 8 +++++--- src/native/common/runtime-base/timing-internal.cc | 8 ++++++-- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index 306fbfb87f3..69c6c6af3ba 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -90,8 +91,7 @@ namespace xamarin::android { [[gnu::flatten, gnu::always_inline]] static void create_xdg_directory (jstring_wrapper &home, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept { - auto dir_buffer = Util::join_paths (home.get_string_view (), relative_path); - const char *dir_path = dir_buffer.get (); + char *dir_path = Util::join_paths (home.get_string_view (), relative_path); log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); if (rv < 0 && errno != EEXIST) { @@ -101,6 +101,7 @@ namespace xamarin::android { if (!environment_variable_name.empty ()) { set_variable (environment_variable_name.data (), dir_path); } + std::free (dir_path); } [[gnu::flatten, gnu::always_inline]] diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 50c52ee6c54..7587ba66eea 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -6,10 +6,10 @@ #include #include +#include #include #include #include -#include #include #include @@ -308,7 +308,7 @@ namespace xamarin::android { return !path.empty () && path.contains ('/'); } - static auto join_paths (std::string_view first, std::string_view second, size_t &path_length) noexcept -> std::unique_ptr + static auto join_paths (std::string_view first, std::string_view second, size_t &path_length) noexcept -> char* { bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); @@ -319,8 +319,9 @@ namespace xamarin::android { } size_t allocation_size = Helpers::add_with_overflow_check (path_length, 1uz); - auto buffer = std::make_unique (allocation_size); - char *destination = buffer.get (); + auto buffer = static_cast (std::malloc (allocation_size)); + abort_unless (buffer != nullptr, "Failed to allocate joined path"); + char *destination = buffer; if (!first.empty ()) { memcpy (destination, first.data (), first.length ()); destination += first.length (); @@ -337,7 +338,7 @@ namespace xamarin::android { return buffer; } - static auto join_paths (std::string_view first, std::string_view second) noexcept -> std::unique_ptr + static auto join_paths (std::string_view first, std::string_view second) noexcept -> char* { size_t path_length; return join_paths (first, second, path_length); diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index 28d33f5ba86..3be528b2e07 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -1,3 +1,4 @@ +#include #include #include @@ -257,14 +258,15 @@ AndroidSystem::setup_environment () noexcept #if defined(DEBUG) log_debug (LOG_DEFAULT, "Loading environment from the override directory."sv); - auto env_override_file_buffer = Util::join_paths ( + char *env_override_file = Util::join_paths ( primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME ); - if (Util::file_exists (env_override_file_buffer.get ())) { - log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file_buffer.get ()); - setup_environment_from_override_file (env_override_file_buffer.get ()); + if (Util::file_exists (env_override_file)) { + log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file); + setup_environment_from_override_file (env_override_file); } + std::free (env_override_file); #endif // def DEBUG } @@ -272,12 +274,11 @@ void AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcept { // appDirs[Constants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory - auto libmonodroid_path_buffer = Util::join_paths ( + char *libmonodroid_path = Util::join_paths ( appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (), "libmonodroid.so"sv ); - const char *libmonodroid_path = libmonodroid_path_buffer.get (); log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path); if (!Util::file_exists (libmonodroid_path)) { log_debug (LOG_ASSEMBLY, "{} not found, assuming application/android:extractNativeLibs == false", libmonodroid_path); @@ -287,6 +288,7 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep set_embedded_dso_mode_enabled (false); native_libraries_dir.assign (appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_cstr ()); } + std::free (libmonodroid_path); } auto diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index 4516732f0d5..e83862c5574 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -78,9 +78,11 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p Util::create_public_directory (override_dir); size_t path_length; - auto path_buffer = Util::join_paths (override_dir, fallback_filename, path_length); - std::string_view path_view { path_buffer.get (), path_length }; - return log_and_return (open_file (path_view), path_view); + char *path_buffer = Util::join_paths (override_dir, fallback_filename, path_length); + std::string_view path_view { path_buffer, path_length }; + FILE *ret = log_and_return (open_file (path_view), path_view); + std::free (path_buffer); + return ret; } void diff --git a/src/native/common/runtime-base/timing-internal.cc b/src/native/common/runtime-base/timing-internal.cc index ad90167c504..684de6fb8f7 100644 --- a/src/native/common/runtime-base/timing-internal.cc +++ b/src/native/common/runtime-base/timing-internal.cc @@ -1,4 +1,5 @@ #include +#include #include #include @@ -194,17 +195,19 @@ void FastTiming::dump_to_file (size_t entries) noexcept // Note that to access the file for a release app, the app must be made debuggable // and `run-as` must be used. std::string_view file_name = output_file_name == nullptr ? default_timing_file_name : *output_file_name; - auto timing_log_path_buffer = Util::join_paths (getenv ("TMPDIR"), file_name); - const char *timing_log_path = timing_log_path_buffer.get (); + char *timing_log_path = Util::join_paths (getenv ("TMPDIR"), file_name); FILE *timing_log = Util::monodroid_fopen (timing_log_path, "w"); if (timing_log == nullptr) { log_error (LOG_TIMING, "[2/2] Unable to create the performance measurements file '{}'"sv, timing_log_path); + std::free (timing_log_path); return; } if (!Util::set_world_accessible (fileno (timing_log))) { log_warn (LOG_TIMING, "[2/2] Failed to make performance measurements file '{}' world-readable"sv, timing_log_path); + fclose (timing_log); + std::free (timing_log_path); return; } @@ -220,6 +223,7 @@ void FastTiming::dump_to_file (size_t entries) noexcept dump (entries, true /* indent */, line_writer); fflush (timing_log); fclose (timing_log); + std::free (timing_log_path); } void FastTiming::dump () noexcept From cfe62c3e1b4a60ba2c91af409d5f7fce03583a09 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 17:48:38 +0200 Subject: [PATCH 13/21] Keep short joined paths on the stack Calculate the exact path size, use the runtime-specific local buffer below its sensible threshold, and fall back to malloc only for larger paths. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../clr/include/host/host-environment.hh | 10 +++++-- src/native/clr/include/runtime-base/util.hh | 28 ++++++++++--------- src/native/clr/runtime-base/android-system.cc | 24 +++++++++------- src/native/clr/runtime-base/logger.cc | 10 +++++-- src/native/common/include/shared/helpers.hh | 22 +++++++++++++++ .../common/runtime-base/timing-internal.cc | 13 ++++++--- src/native/mono/runtime-base/util.hh | 23 ++++++++------- 7 files changed, 88 insertions(+), 42 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index 69c6c6af3ba..a2ba932d746 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -91,7 +91,13 @@ namespace xamarin::android { [[gnu::flatten, gnu::always_inline]] static void create_xdg_directory (jstring_wrapper &home, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept { - char *dir_path = Util::join_paths (home.get_string_view (), relative_path); + std::string_view home_path = home.get_string_view (); + size_t dir_path_length = Util::get_joined_path_length (home_path, relative_path); + size_t allocation_size = Helpers::add_with_overflow_check (dir_path_length, 1uz); + char local_buffer [Util::LocalPathBufferSize]; + char *dir_path = Helpers::get_temporary_buffer (local_buffer, allocation_size); + Util::join_paths (dir_path, allocation_size, home_path, relative_path); + log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); if (rv < 0 && errno != EEXIST) { @@ -101,7 +107,7 @@ namespace xamarin::android { if (!environment_variable_name.empty ()) { set_variable (environment_variable_name.data (), dir_path); } - std::free (dir_path); + Helpers::free_temporary_buffer (dir_path, local_buffer); } [[gnu::flatten, gnu::always_inline]] diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 7587ba66eea..22ae72bead8 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -39,6 +38,8 @@ namespace xamarin::android { }; public: + static constexpr size_t LocalPathBufferSize = Constants::SENSIBLE_PATH_MAX; + static int create_directory (const char *pathname, mode_t mode); static auto create_directory (std::string_view const& dir, mode_t mode) noexcept -> int @@ -308,19 +309,26 @@ namespace xamarin::android { return !path.empty () && path.contains ('/'); } - static auto join_paths (std::string_view first, std::string_view second, size_t &path_length) noexcept -> char* + static auto get_joined_path_length (std::string_view first, std::string_view second) noexcept -> size_t { bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); size_t second_offset = remove_duplicate_separator ? 1uz : 0uz; - path_length = Helpers::add_with_overflow_check (first.length (), second.length () - second_offset); + size_t path_length = Helpers::add_with_overflow_check (first.length (), second.length () - second_offset); if (add_separator) { path_length = Helpers::add_with_overflow_check (path_length, 1uz); } + return path_length; + } - size_t allocation_size = Helpers::add_with_overflow_check (path_length, 1uz); - auto buffer = static_cast (std::malloc (allocation_size)); - abort_unless (buffer != nullptr, "Failed to allocate joined path"); + static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> size_t + { + size_t path_length = get_joined_path_length (first, second); + abort_unless (buffer != nullptr && buffer_size > path_length, "Joined path buffer is too small"); + + bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); + bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); + size_t second_offset = remove_duplicate_separator ? 1uz : 0uz; char *destination = buffer; if (!first.empty ()) { memcpy (destination, first.data (), first.length ()); @@ -335,13 +343,7 @@ namespace xamarin::android { } buffer [path_length] = '\0'; - return buffer; - } - - static auto join_paths (std::string_view first, std::string_view second) noexcept -> char* - { - size_t path_length; - return join_paths (first, second, path_length); + return path_length; } private: diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index 3be528b2e07..aaebc1186e0 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -258,15 +258,17 @@ AndroidSystem::setup_environment () noexcept #if defined(DEBUG) log_debug (LOG_DEFAULT, "Loading environment from the override directory."sv); - char *env_override_file = Util::join_paths ( - primary_override_dir, - Constants::OVERRIDE_ENVIRONMENT_FILE_NAME - ); + size_t env_override_file_length = Util::get_joined_path_length (primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); + size_t allocation_size = Helpers::add_with_overflow_check (env_override_file_length, 1uz); + char local_buffer [Util::LocalPathBufferSize]; + char *env_override_file = Helpers::get_temporary_buffer (local_buffer, allocation_size); + Util::join_paths (env_override_file, allocation_size, primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); + if (Util::file_exists (env_override_file)) { log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file); setup_environment_from_override_file (env_override_file); } - std::free (env_override_file); + Helpers::free_temporary_buffer (env_override_file, local_buffer); #endif // def DEBUG } @@ -274,10 +276,12 @@ void AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcept { // appDirs[Constants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory - char *libmonodroid_path = Util::join_paths ( - appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (), - "libmonodroid.so"sv - ); + std::string_view app_data_dir = appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (); + size_t path_length = Util::get_joined_path_length (app_data_dir, "libmonodroid.so"sv); + size_t allocation_size = Helpers::add_with_overflow_check (path_length, 1uz); + char local_buffer [Util::LocalPathBufferSize]; + char *libmonodroid_path = Helpers::get_temporary_buffer (local_buffer, allocation_size); + Util::join_paths (libmonodroid_path, allocation_size, app_data_dir, "libmonodroid.so"sv); log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path); if (!Util::file_exists (libmonodroid_path)) { @@ -288,7 +292,7 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep set_embedded_dso_mode_enabled (false); native_libraries_dir.assign (appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_cstr ()); } - std::free (libmonodroid_path); + Helpers::free_temporary_buffer (libmonodroid_path, local_buffer); } auto diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index e83862c5574..e77ea99ab14 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -77,11 +77,15 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p } Util::create_public_directory (override_dir); - size_t path_length; - char *path_buffer = Util::join_paths (override_dir, fallback_filename, path_length); + size_t path_length = Util::get_joined_path_length (override_dir, fallback_filename); + size_t allocation_size = Helpers::add_with_overflow_check (path_length, 1uz); + char local_buffer [Util::LocalPathBufferSize]; + char *path_buffer = Helpers::get_temporary_buffer (local_buffer, allocation_size); + Util::join_paths (path_buffer, allocation_size, override_dir, fallback_filename); + std::string_view path_view { path_buffer, path_length }; FILE *ret = log_and_return (open_file (path_view), path_view); - std::free (path_buffer); + Helpers::free_temporary_buffer (path_buffer, local_buffer); return ret; } diff --git a/src/native/common/include/shared/helpers.hh b/src/native/common/include/shared/helpers.hh index f1ec1fb6ab4..10fdb2e2c4e 100644 --- a/src/native/common/include/shared/helpers.hh +++ b/src/native/common/include/shared/helpers.hh @@ -19,6 +19,28 @@ namespace xamarin::android class [[gnu::visibility("hidden")]] Helpers { public: + template + static auto get_temporary_buffer (char (&local_buffer)[Size], size_t required_size) noexcept -> char* + { + if (required_size <= Size) { + return local_buffer; + } + + auto buffer = static_cast (std::malloc (required_size)); + if (buffer == nullptr) { + abort_application ("Failed to allocate temporary buffer"); + } + return buffer; + } + + template + static void free_temporary_buffer (char *buffer, char (&local_buffer)[Size]) noexcept + { + if (buffer != local_buffer) { + std::free (buffer); + } + } + template [[gnu::always_inline]] static auto add_with_overflow_check (P1 a, P2 b, std::source_location sloc = std::source_location::current ()) noexcept -> Ret diff --git a/src/native/common/runtime-base/timing-internal.cc b/src/native/common/runtime-base/timing-internal.cc index 684de6fb8f7..94c1a58b0be 100644 --- a/src/native/common/runtime-base/timing-internal.cc +++ b/src/native/common/runtime-base/timing-internal.cc @@ -195,19 +195,24 @@ void FastTiming::dump_to_file (size_t entries) noexcept // Note that to access the file for a release app, the app must be made debuggable // and `run-as` must be used. std::string_view file_name = output_file_name == nullptr ? default_timing_file_name : *output_file_name; - char *timing_log_path = Util::join_paths (getenv ("TMPDIR"), file_name); + std::string_view temporary_directory = getenv ("TMPDIR"); + size_t timing_log_path_length = Util::get_joined_path_length (temporary_directory, file_name); + size_t allocation_size = Helpers::add_with_overflow_check (timing_log_path_length, 1uz); + char local_buffer [Util::LocalPathBufferSize]; + char *timing_log_path = Helpers::get_temporary_buffer (local_buffer, allocation_size); + Util::join_paths (timing_log_path, allocation_size, temporary_directory, file_name); FILE *timing_log = Util::monodroid_fopen (timing_log_path, "w"); if (timing_log == nullptr) { log_error (LOG_TIMING, "[2/2] Unable to create the performance measurements file '{}'"sv, timing_log_path); - std::free (timing_log_path); + Helpers::free_temporary_buffer (timing_log_path, local_buffer); return; } if (!Util::set_world_accessible (fileno (timing_log))) { log_warn (LOG_TIMING, "[2/2] Failed to make performance measurements file '{}' world-readable"sv, timing_log_path); fclose (timing_log); - std::free (timing_log_path); + Helpers::free_temporary_buffer (timing_log_path, local_buffer); return; } @@ -223,7 +228,7 @@ void FastTiming::dump_to_file (size_t entries) noexcept dump (entries, true /* indent */, line_writer); fflush (timing_log); fclose (timing_log); - std::free (timing_log_path); + Helpers::free_temporary_buffer (timing_log_path, local_buffer); } void FastTiming::dump () noexcept diff --git a/src/native/mono/runtime-base/util.hh b/src/native/mono/runtime-base/util.hh index 797656f1c23..08d4ca0535e 100644 --- a/src/native/mono/runtime-base/util.hh +++ b/src/native/mono/runtime-base/util.hh @@ -54,6 +54,8 @@ namespace xamarin::android class Util { public: + static constexpr size_t LocalPathBufferSize = SENSIBLE_PATH_MAX; + static void initialize () noexcept; static int monodroid_getpagesize () noexcept @@ -109,13 +111,8 @@ namespace xamarin::android return fd; } - // Returns the path length excluding the terminating NUL, or -1 on failure. - static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> ssize_t + static auto get_joined_path_length (std::string_view first, std::string_view second) noexcept -> size_t { - if (buffer == nullptr || buffer_size == 0) { - return -1; - } - bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); size_t second_offset = remove_duplicate_separator ? 1uz : 0uz; @@ -123,11 +120,17 @@ namespace xamarin::android if (add_separator) { path_length = Helpers::add_with_overflow_check (path_length, 1uz); } + return path_length; + } - if (path_length >= buffer_size || path_length > static_cast(std::numeric_limits::max ())) { - return -1; - } + static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> size_t + { + size_t path_length = get_joined_path_length (first, second); + abort_unless (buffer != nullptr && buffer_size > path_length, "Joined path buffer is too small"); + bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); + bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); + size_t second_offset = remove_duplicate_separator ? 1uz : 0uz; char *destination = buffer; if (!first.empty ()) { memcpy (destination, first.data (), first.length ()); @@ -142,7 +145,7 @@ namespace xamarin::android } buffer [path_length] = '\0'; - return static_cast(path_length); + return path_length; } // Make sure that `buf` has enough space! This is by design, the methods are supposed to be fast. From cef715be0b83bc4549cc5f20c0bd7e21ae781417 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 17:59:02 +0200 Subject: [PATCH 14/21] Reuse the fallback log file variable Avoid redeclaring the existing result while retaining cleanup before returning. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/runtime-base/logger.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index e77ea99ab14..d4b26dce5ab 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -84,7 +84,7 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p Util::join_paths (path_buffer, allocation_size, override_dir, fallback_filename); std::string_view path_view { path_buffer, path_length }; - FILE *ret = log_and_return (open_file (path_view), path_view); + ret = log_and_return (open_file (path_view), path_view); Helpers::free_temporary_buffer (path_buffer, local_buffer); return ret; } From f92e8a1ca28e91471f2930ca6948676d604eeda6 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 20:39:05 +0200 Subject: [PATCH 15/21] Report required joined path capacity Retry path formatting with exact heap storage only when the sensible stack buffer is insufficient. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../clr/include/host/host-environment.hh | 9 ++--- src/native/clr/include/runtime-base/util.hh | 37 +++++++++++++------ src/native/clr/runtime-base/android-system.cc | 18 ++++----- src/native/clr/runtime-base/logger.cc | 13 +++---- src/native/common/include/shared/helpers.hh | 22 ----------- .../common/runtime-base/timing-internal.cc | 13 +++---- src/native/mono/runtime-base/util.hh | 36 ++++++++++++------ 7 files changed, 75 insertions(+), 73 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index a2ba932d746..5afbf435dde 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -92,11 +92,10 @@ namespace xamarin::android { static void create_xdg_directory (jstring_wrapper &home, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept { std::string_view home_path = home.get_string_view (); - size_t dir_path_length = Util::get_joined_path_length (home_path, relative_path); - size_t allocation_size = Helpers::add_with_overflow_check (dir_path_length, 1uz); char local_buffer [Util::LocalPathBufferSize]; - char *dir_path = Helpers::get_temporary_buffer (local_buffer, allocation_size); - Util::join_paths (dir_path, allocation_size, home_path, relative_path); + char *heap_buffer; + Util::join_paths (local_buffer, heap_buffer, home_path, relative_path); + const char *dir_path = heap_buffer == nullptr ? local_buffer : heap_buffer; log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); @@ -107,7 +106,7 @@ namespace xamarin::android { if (!environment_variable_name.empty ()) { set_variable (environment_variable_name.data (), dir_path); } - Helpers::free_temporary_buffer (dir_path, local_buffer); + std::free (heap_buffer); } [[gnu::flatten, gnu::always_inline]] diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 22ae72bead8..ec9b767eea9 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -309,7 +310,8 @@ namespace xamarin::android { return !path.empty () && path.contains ('/'); } - static auto get_joined_path_length (std::string_view first, std::string_view second) noexcept -> size_t + // Returns the path length excluding NUL, or the negative required capacity including NUL. + static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> ssize_t { bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); @@ -318,17 +320,13 @@ namespace xamarin::android { if (add_separator) { path_length = Helpers::add_with_overflow_check (path_length, 1uz); } - return path_length; - } - static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> size_t - { - size_t path_length = get_joined_path_length (first, second); - abort_unless (buffer != nullptr && buffer_size > path_length, "Joined path buffer is too small"); + size_t required_capacity = Helpers::add_with_overflow_check (path_length, 1uz); + abort_unless (required_capacity <= static_cast(std::numeric_limits::max ()), "Joined path is too long"); + if (buffer == nullptr || buffer_size < required_capacity) { + return -static_cast(required_capacity); + } - bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); - bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); - size_t second_offset = remove_duplicate_separator ? 1uz : 0uz; char *destination = buffer; if (!first.empty ()) { memcpy (destination, first.data (), first.length ()); @@ -343,7 +341,24 @@ namespace xamarin::android { } buffer [path_length] = '\0'; - return path_length; + return static_cast(path_length); + } + + template + static auto join_paths (char (&stack_buffer)[Size], char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> ssize_t + { + heap_buffer = nullptr; + ssize_t result = join_paths (stack_buffer, Size, first, second); + if (result >= 0) { + return result; + } + + size_t required_capacity = static_cast(-result); + heap_buffer = static_cast (std::malloc (required_capacity)); + abort_unless (heap_buffer != nullptr, "Failed to allocate joined path"); + result = join_paths (heap_buffer, required_capacity, first, second); + abort_unless (result >= 0, "Failed to join path using the required capacity"); + return result; } private: diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index aaebc1186e0..5c5c198755d 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -258,17 +258,16 @@ AndroidSystem::setup_environment () noexcept #if defined(DEBUG) log_debug (LOG_DEFAULT, "Loading environment from the override directory."sv); - size_t env_override_file_length = Util::get_joined_path_length (primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); - size_t allocation_size = Helpers::add_with_overflow_check (env_override_file_length, 1uz); char local_buffer [Util::LocalPathBufferSize]; - char *env_override_file = Helpers::get_temporary_buffer (local_buffer, allocation_size); - Util::join_paths (env_override_file, allocation_size, primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); + char *heap_buffer; + Util::join_paths (local_buffer, heap_buffer, primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); + const char *env_override_file = heap_buffer == nullptr ? local_buffer : heap_buffer; if (Util::file_exists (env_override_file)) { log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file); setup_environment_from_override_file (env_override_file); } - Helpers::free_temporary_buffer (env_override_file, local_buffer); + std::free (heap_buffer); #endif // def DEBUG } @@ -277,11 +276,10 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep { // appDirs[Constants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory std::string_view app_data_dir = appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (); - size_t path_length = Util::get_joined_path_length (app_data_dir, "libmonodroid.so"sv); - size_t allocation_size = Helpers::add_with_overflow_check (path_length, 1uz); char local_buffer [Util::LocalPathBufferSize]; - char *libmonodroid_path = Helpers::get_temporary_buffer (local_buffer, allocation_size); - Util::join_paths (libmonodroid_path, allocation_size, app_data_dir, "libmonodroid.so"sv); + char *heap_buffer; + Util::join_paths (local_buffer, heap_buffer, app_data_dir, "libmonodroid.so"sv); + const char *libmonodroid_path = heap_buffer == nullptr ? local_buffer : heap_buffer; log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path); if (!Util::file_exists (libmonodroid_path)) { @@ -292,7 +290,7 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep set_embedded_dso_mode_enabled (false); native_libraries_dir.assign (appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_cstr ()); } - Helpers::free_temporary_buffer (libmonodroid_path, local_buffer); + std::free (heap_buffer); } auto diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index d4b26dce5ab..a4819b6c2f0 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -75,17 +75,16 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p if (ret != nullptr) { return log_and_return (ret, custom_path); } - Util::create_public_directory (override_dir); - size_t path_length = Util::get_joined_path_length (override_dir, fallback_filename); - size_t allocation_size = Helpers::add_with_overflow_check (path_length, 1uz); + Util::create_public_directory (override_dir); char local_buffer [Util::LocalPathBufferSize]; - char *path_buffer = Helpers::get_temporary_buffer (local_buffer, allocation_size); - Util::join_paths (path_buffer, allocation_size, override_dir, fallback_filename); + char *heap_buffer; + ssize_t path_length = Util::join_paths (local_buffer, heap_buffer, override_dir, fallback_filename); + const char *path_buffer = heap_buffer == nullptr ? local_buffer : heap_buffer; - std::string_view path_view { path_buffer, path_length }; + std::string_view path_view { path_buffer, static_cast(path_length) }; ret = log_and_return (open_file (path_view), path_view); - Helpers::free_temporary_buffer (path_buffer, local_buffer); + std::free (heap_buffer); return ret; } diff --git a/src/native/common/include/shared/helpers.hh b/src/native/common/include/shared/helpers.hh index 10fdb2e2c4e..f1ec1fb6ab4 100644 --- a/src/native/common/include/shared/helpers.hh +++ b/src/native/common/include/shared/helpers.hh @@ -19,28 +19,6 @@ namespace xamarin::android class [[gnu::visibility("hidden")]] Helpers { public: - template - static auto get_temporary_buffer (char (&local_buffer)[Size], size_t required_size) noexcept -> char* - { - if (required_size <= Size) { - return local_buffer; - } - - auto buffer = static_cast (std::malloc (required_size)); - if (buffer == nullptr) { - abort_application ("Failed to allocate temporary buffer"); - } - return buffer; - } - - template - static void free_temporary_buffer (char *buffer, char (&local_buffer)[Size]) noexcept - { - if (buffer != local_buffer) { - std::free (buffer); - } - } - template [[gnu::always_inline]] static auto add_with_overflow_check (P1 a, P2 b, std::source_location sloc = std::source_location::current ()) noexcept -> Ret diff --git a/src/native/common/runtime-base/timing-internal.cc b/src/native/common/runtime-base/timing-internal.cc index 94c1a58b0be..85be945343d 100644 --- a/src/native/common/runtime-base/timing-internal.cc +++ b/src/native/common/runtime-base/timing-internal.cc @@ -196,23 +196,22 @@ void FastTiming::dump_to_file (size_t entries) noexcept // and `run-as` must be used. std::string_view file_name = output_file_name == nullptr ? default_timing_file_name : *output_file_name; std::string_view temporary_directory = getenv ("TMPDIR"); - size_t timing_log_path_length = Util::get_joined_path_length (temporary_directory, file_name); - size_t allocation_size = Helpers::add_with_overflow_check (timing_log_path_length, 1uz); char local_buffer [Util::LocalPathBufferSize]; - char *timing_log_path = Helpers::get_temporary_buffer (local_buffer, allocation_size); - Util::join_paths (timing_log_path, allocation_size, temporary_directory, file_name); + char *heap_buffer; + Util::join_paths (local_buffer, heap_buffer, temporary_directory, file_name); + const char *timing_log_path = heap_buffer == nullptr ? local_buffer : heap_buffer; FILE *timing_log = Util::monodroid_fopen (timing_log_path, "w"); if (timing_log == nullptr) { log_error (LOG_TIMING, "[2/2] Unable to create the performance measurements file '{}'"sv, timing_log_path); - Helpers::free_temporary_buffer (timing_log_path, local_buffer); + std::free (heap_buffer); return; } if (!Util::set_world_accessible (fileno (timing_log))) { log_warn (LOG_TIMING, "[2/2] Failed to make performance measurements file '{}' world-readable"sv, timing_log_path); fclose (timing_log); - Helpers::free_temporary_buffer (timing_log_path, local_buffer); + std::free (heap_buffer); return; } @@ -228,7 +227,7 @@ void FastTiming::dump_to_file (size_t entries) noexcept dump (entries, true /* indent */, line_writer); fflush (timing_log); fclose (timing_log); - Helpers::free_temporary_buffer (timing_log_path, local_buffer); + std::free (heap_buffer); } void FastTiming::dump () noexcept diff --git a/src/native/mono/runtime-base/util.hh b/src/native/mono/runtime-base/util.hh index 08d4ca0535e..c50b0058d09 100644 --- a/src/native/mono/runtime-base/util.hh +++ b/src/native/mono/runtime-base/util.hh @@ -111,7 +111,8 @@ namespace xamarin::android return fd; } - static auto get_joined_path_length (std::string_view first, std::string_view second) noexcept -> size_t + // Returns the path length excluding NUL, or the negative required capacity including NUL. + static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> ssize_t { bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); @@ -120,17 +121,13 @@ namespace xamarin::android if (add_separator) { path_length = Helpers::add_with_overflow_check (path_length, 1uz); } - return path_length; - } - static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> size_t - { - size_t path_length = get_joined_path_length (first, second); - abort_unless (buffer != nullptr && buffer_size > path_length, "Joined path buffer is too small"); + size_t required_capacity = Helpers::add_with_overflow_check (path_length, 1uz); + abort_unless (required_capacity <= static_cast(std::numeric_limits::max ()), "Joined path is too long"); + if (buffer == nullptr || buffer_size < required_capacity) { + return -static_cast(required_capacity); + } - bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); - bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); - size_t second_offset = remove_duplicate_separator ? 1uz : 0uz; char *destination = buffer; if (!first.empty ()) { memcpy (destination, first.data (), first.length ()); @@ -145,7 +142,24 @@ namespace xamarin::android } buffer [path_length] = '\0'; - return path_length; + return static_cast(path_length); + } + + template + static auto join_paths (char (&stack_buffer)[Size], char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> ssize_t + { + heap_buffer = nullptr; + ssize_t result = join_paths (stack_buffer, Size, first, second); + if (result >= 0) { + return result; + } + + size_t required_capacity = static_cast(-result); + heap_buffer = static_cast (std::malloc (required_capacity)); + abort_unless (heap_buffer != nullptr, "Failed to allocate joined path"); + result = join_paths (heap_buffer, required_capacity, first, second); + abort_unless (result >= 0, "Failed to join path using the required capacity"); + return result; } // Make sure that `buf` has enough space! This is by design, the methods are supposed to be fast. From 406a23356b1ed0270618cd28ba78361b6f76ae7c Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 20:46:51 +0200 Subject: [PATCH 16/21] Simplify temporary path ownership Return the selected stack or heap buffer from the path helper and centralize conditional heap cleanup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/host/host-environment.hh | 5 ++--- src/native/clr/include/runtime-base/util.hh | 11 ++++++++--- src/native/clr/runtime-base/android-system.cc | 10 ++++------ src/native/clr/runtime-base/logger.cc | 7 +++---- src/native/common/runtime-base/timing-internal.cc | 9 ++++----- src/native/mono/runtime-base/util.hh | 11 ++++++++--- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index 5afbf435dde..ed4f820fe6c 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -94,8 +94,7 @@ namespace xamarin::android { std::string_view home_path = home.get_string_view (); char local_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - Util::join_paths (local_buffer, heap_buffer, home_path, relative_path); - const char *dir_path = heap_buffer == nullptr ? local_buffer : heap_buffer; + const char *dir_path = Util::join_paths (local_buffer, heap_buffer, home_path, relative_path); log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); @@ -106,7 +105,7 @@ namespace xamarin::android { if (!environment_variable_name.empty ()) { set_variable (environment_variable_name.data (), dir_path); } - std::free (heap_buffer); + Util::free_if_used (heap_buffer); } [[gnu::flatten, gnu::always_inline]] diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index ec9b767eea9..08306c8429d 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -345,12 +345,12 @@ namespace xamarin::android { } template - static auto join_paths (char (&stack_buffer)[Size], char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> ssize_t + static auto join_paths (char (&stack_buffer)[Size], char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> const char* { heap_buffer = nullptr; ssize_t result = join_paths (stack_buffer, Size, first, second); if (result >= 0) { - return result; + return stack_buffer; } size_t required_capacity = static_cast(-result); @@ -358,7 +358,12 @@ namespace xamarin::android { abort_unless (heap_buffer != nullptr, "Failed to allocate joined path"); result = join_paths (heap_buffer, required_capacity, first, second); abort_unless (result >= 0, "Failed to join path using the required capacity"); - return result; + return heap_buffer; + } + + static void free_if_used (void *heap_buffer) noexcept + { + std::free (heap_buffer); } private: diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index 5c5c198755d..c927b9d2071 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -260,14 +260,13 @@ AndroidSystem::setup_environment () noexcept char local_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - Util::join_paths (local_buffer, heap_buffer, primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); - const char *env_override_file = heap_buffer == nullptr ? local_buffer : heap_buffer; + const char *env_override_file = Util::join_paths (local_buffer, heap_buffer, primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); if (Util::file_exists (env_override_file)) { log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file); setup_environment_from_override_file (env_override_file); } - std::free (heap_buffer); + Util::free_if_used (heap_buffer); #endif // def DEBUG } @@ -278,8 +277,7 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep std::string_view app_data_dir = appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (); char local_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - Util::join_paths (local_buffer, heap_buffer, app_data_dir, "libmonodroid.so"sv); - const char *libmonodroid_path = heap_buffer == nullptr ? local_buffer : heap_buffer; + const char *libmonodroid_path = Util::join_paths (local_buffer, heap_buffer, app_data_dir, "libmonodroid.so"sv); log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path); if (!Util::file_exists (libmonodroid_path)) { @@ -290,7 +288,7 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep set_embedded_dso_mode_enabled (false); native_libraries_dir.assign (appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_cstr ()); } - std::free (heap_buffer); + Util::free_if_used (heap_buffer); } auto diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index a4819b6c2f0..9e3e70dc127 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -79,12 +79,11 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p Util::create_public_directory (override_dir); char local_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - ssize_t path_length = Util::join_paths (local_buffer, heap_buffer, override_dir, fallback_filename); - const char *path_buffer = heap_buffer == nullptr ? local_buffer : heap_buffer; + const char *path_buffer = Util::join_paths (local_buffer, heap_buffer, override_dir, fallback_filename); - std::string_view path_view { path_buffer, static_cast(path_length) }; + std::string_view path_view { path_buffer }; ret = log_and_return (open_file (path_view), path_view); - std::free (heap_buffer); + Util::free_if_used (heap_buffer); return ret; } diff --git a/src/native/common/runtime-base/timing-internal.cc b/src/native/common/runtime-base/timing-internal.cc index 85be945343d..474eadfd605 100644 --- a/src/native/common/runtime-base/timing-internal.cc +++ b/src/native/common/runtime-base/timing-internal.cc @@ -198,20 +198,19 @@ void FastTiming::dump_to_file (size_t entries) noexcept std::string_view temporary_directory = getenv ("TMPDIR"); char local_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - Util::join_paths (local_buffer, heap_buffer, temporary_directory, file_name); - const char *timing_log_path = heap_buffer == nullptr ? local_buffer : heap_buffer; + const char *timing_log_path = Util::join_paths (local_buffer, heap_buffer, temporary_directory, file_name); FILE *timing_log = Util::monodroid_fopen (timing_log_path, "w"); if (timing_log == nullptr) { log_error (LOG_TIMING, "[2/2] Unable to create the performance measurements file '{}'"sv, timing_log_path); - std::free (heap_buffer); + Util::free_if_used (heap_buffer); return; } if (!Util::set_world_accessible (fileno (timing_log))) { log_warn (LOG_TIMING, "[2/2] Failed to make performance measurements file '{}' world-readable"sv, timing_log_path); fclose (timing_log); - std::free (heap_buffer); + Util::free_if_used (heap_buffer); return; } @@ -227,7 +226,7 @@ void FastTiming::dump_to_file (size_t entries) noexcept dump (entries, true /* indent */, line_writer); fflush (timing_log); fclose (timing_log); - std::free (heap_buffer); + Util::free_if_used (heap_buffer); } void FastTiming::dump () noexcept diff --git a/src/native/mono/runtime-base/util.hh b/src/native/mono/runtime-base/util.hh index c50b0058d09..6d0457bf4cb 100644 --- a/src/native/mono/runtime-base/util.hh +++ b/src/native/mono/runtime-base/util.hh @@ -146,12 +146,12 @@ namespace xamarin::android } template - static auto join_paths (char (&stack_buffer)[Size], char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> ssize_t + static auto join_paths (char (&stack_buffer)[Size], char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> const char* { heap_buffer = nullptr; ssize_t result = join_paths (stack_buffer, Size, first, second); if (result >= 0) { - return result; + return stack_buffer; } size_t required_capacity = static_cast(-result); @@ -159,7 +159,12 @@ namespace xamarin::android abort_unless (heap_buffer != nullptr, "Failed to allocate joined path"); result = join_paths (heap_buffer, required_capacity, first, second); abort_unless (result >= 0, "Failed to join path using the required capacity"); - return result; + return heap_buffer; + } + + static void free_if_used (void *heap_buffer) noexcept + { + std::free (heap_buffer); } // Make sure that `buf` has enough space! This is by design, the methods are supposed to be fast. From ef83019d552693ded0c3f86245e6d81b80ffd2c8 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 23:22:24 +0200 Subject: [PATCH 17/21] Use free directly for temporary buffers Rely on the C guarantee that free accepts null pointers and name stack-backed storage explicitly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/host/host-environment.hh | 6 +++--- src/native/clr/include/runtime-base/util.hh | 5 ----- src/native/clr/runtime-base/android-system.cc | 12 ++++++------ src/native/clr/runtime-base/logger.cc | 6 +++--- src/native/common/runtime-base/timing-internal.cc | 10 +++++----- src/native/mono/runtime-base/util.hh | 5 ----- 6 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index ed4f820fe6c..1bece5840f8 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -92,9 +92,9 @@ namespace xamarin::android { static void create_xdg_directory (jstring_wrapper &home, std::string_view const& relative_path, std::string_view const& environment_variable_name) noexcept { std::string_view home_path = home.get_string_view (); - char local_buffer [Util::LocalPathBufferSize]; + char stack_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - const char *dir_path = Util::join_paths (local_buffer, heap_buffer, home_path, relative_path); + const char *dir_path = Util::join_paths (stack_buffer, heap_buffer, home_path, relative_path); log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); @@ -105,7 +105,7 @@ namespace xamarin::android { if (!environment_variable_name.empty ()) { set_variable (environment_variable_name.data (), dir_path); } - Util::free_if_used (heap_buffer); + std::free (heap_buffer); } [[gnu::flatten, gnu::always_inline]] diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 08306c8429d..2c5143a3dfc 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -361,11 +361,6 @@ namespace xamarin::android { return heap_buffer; } - static void free_if_used (void *heap_buffer) noexcept - { - std::free (heap_buffer); - } - private: static inline int page_size = getpagesize (); }; diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index c927b9d2071..1b30a478b60 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -258,15 +258,15 @@ AndroidSystem::setup_environment () noexcept #if defined(DEBUG) log_debug (LOG_DEFAULT, "Loading environment from the override directory."sv); - char local_buffer [Util::LocalPathBufferSize]; + char stack_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - const char *env_override_file = Util::join_paths (local_buffer, heap_buffer, primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); + const char *env_override_file = Util::join_paths (stack_buffer, heap_buffer, primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); if (Util::file_exists (env_override_file)) { log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file); setup_environment_from_override_file (env_override_file); } - Util::free_if_used (heap_buffer); + std::free (heap_buffer); #endif // def DEBUG } @@ -275,9 +275,9 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep { // appDirs[Constants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory std::string_view app_data_dir = appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (); - char local_buffer [Util::LocalPathBufferSize]; + char stack_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - const char *libmonodroid_path = Util::join_paths (local_buffer, heap_buffer, app_data_dir, "libmonodroid.so"sv); + const char *libmonodroid_path = Util::join_paths (stack_buffer, heap_buffer, app_data_dir, "libmonodroid.so"sv); log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path); if (!Util::file_exists (libmonodroid_path)) { @@ -288,7 +288,7 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep set_embedded_dso_mode_enabled (false); native_libraries_dir.assign (appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_cstr ()); } - Util::free_if_used (heap_buffer); + std::free (heap_buffer); } auto diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index 9e3e70dc127..97159254398 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -77,13 +77,13 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p } Util::create_public_directory (override_dir); Util::create_public_directory (override_dir); - char local_buffer [Util::LocalPathBufferSize]; + char stack_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - const char *path_buffer = Util::join_paths (local_buffer, heap_buffer, override_dir, fallback_filename); + const char *path_buffer = Util::join_paths (stack_buffer, heap_buffer, override_dir, fallback_filename); std::string_view path_view { path_buffer }; ret = log_and_return (open_file (path_view), path_view); - Util::free_if_used (heap_buffer); + std::free (heap_buffer); return ret; } diff --git a/src/native/common/runtime-base/timing-internal.cc b/src/native/common/runtime-base/timing-internal.cc index 474eadfd605..2e6defeccf7 100644 --- a/src/native/common/runtime-base/timing-internal.cc +++ b/src/native/common/runtime-base/timing-internal.cc @@ -196,21 +196,21 @@ void FastTiming::dump_to_file (size_t entries) noexcept // and `run-as` must be used. std::string_view file_name = output_file_name == nullptr ? default_timing_file_name : *output_file_name; std::string_view temporary_directory = getenv ("TMPDIR"); - char local_buffer [Util::LocalPathBufferSize]; + char stack_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - const char *timing_log_path = Util::join_paths (local_buffer, heap_buffer, temporary_directory, file_name); + const char *timing_log_path = Util::join_paths (stack_buffer, heap_buffer, temporary_directory, file_name); FILE *timing_log = Util::monodroid_fopen (timing_log_path, "w"); if (timing_log == nullptr) { log_error (LOG_TIMING, "[2/2] Unable to create the performance measurements file '{}'"sv, timing_log_path); - Util::free_if_used (heap_buffer); + std::free (heap_buffer); return; } if (!Util::set_world_accessible (fileno (timing_log))) { log_warn (LOG_TIMING, "[2/2] Failed to make performance measurements file '{}' world-readable"sv, timing_log_path); fclose (timing_log); - Util::free_if_used (heap_buffer); + std::free (heap_buffer); return; } @@ -226,7 +226,7 @@ void FastTiming::dump_to_file (size_t entries) noexcept dump (entries, true /* indent */, line_writer); fflush (timing_log); fclose (timing_log); - Util::free_if_used (heap_buffer); + std::free (heap_buffer); } void FastTiming::dump () noexcept diff --git a/src/native/mono/runtime-base/util.hh b/src/native/mono/runtime-base/util.hh index 6d0457bf4cb..c46142bc397 100644 --- a/src/native/mono/runtime-base/util.hh +++ b/src/native/mono/runtime-base/util.hh @@ -162,11 +162,6 @@ namespace xamarin::android return heap_buffer; } - static void free_if_used (void *heap_buffer) noexcept - { - std::free (heap_buffer); - } - // Make sure that `buf` has enough space! This is by design, the methods are supposed to be fast. template static void path_combine (TBuffer& buf, const char* path1, const char* path2) noexcept From 6597f40baacce2d7f4dc6cdbaaa3b9e270225559 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 23:31:05 +0200 Subject: [PATCH 18/21] Pass stack buffer sizes explicitly Use one non-template path helper implementation and make each caller provide its stack capacity. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/host/host-environment.hh | 2 +- src/native/clr/include/runtime-base/util.hh | 5 ++--- src/native/clr/runtime-base/android-system.cc | 4 ++-- src/native/clr/runtime-base/logger.cc | 2 +- src/native/common/runtime-base/timing-internal.cc | 2 +- src/native/mono/runtime-base/util.hh | 5 ++--- 6 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index 1bece5840f8..f13fb2723a4 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -94,7 +94,7 @@ namespace xamarin::android { std::string_view home_path = home.get_string_view (); char stack_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - const char *dir_path = Util::join_paths (stack_buffer, heap_buffer, home_path, relative_path); + const char *dir_path = Util::join_paths (stack_buffer, sizeof (stack_buffer), heap_buffer, home_path, relative_path); log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 2c5143a3dfc..204bd77b308 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -344,11 +344,10 @@ namespace xamarin::android { return static_cast(path_length); } - template - static auto join_paths (char (&stack_buffer)[Size], char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> const char* + static auto join_paths (char *stack_buffer, size_t stack_buffer_size, char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> const char* { heap_buffer = nullptr; - ssize_t result = join_paths (stack_buffer, Size, first, second); + ssize_t result = join_paths (stack_buffer, stack_buffer_size, first, second); if (result >= 0) { return stack_buffer; } diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index 1b30a478b60..765d916d8e7 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -260,7 +260,7 @@ AndroidSystem::setup_environment () noexcept char stack_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - const char *env_override_file = Util::join_paths (stack_buffer, heap_buffer, primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); + const char *env_override_file = Util::join_paths (stack_buffer, sizeof (stack_buffer), heap_buffer, primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); if (Util::file_exists (env_override_file)) { log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file); @@ -277,7 +277,7 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep std::string_view app_data_dir = appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (); char stack_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - const char *libmonodroid_path = Util::join_paths (stack_buffer, heap_buffer, app_data_dir, "libmonodroid.so"sv); + const char *libmonodroid_path = Util::join_paths (stack_buffer, sizeof (stack_buffer), heap_buffer, app_data_dir, "libmonodroid.so"sv); log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path); if (!Util::file_exists (libmonodroid_path)) { diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index 97159254398..ba072ce6d93 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -79,7 +79,7 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p Util::create_public_directory (override_dir); char stack_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - const char *path_buffer = Util::join_paths (stack_buffer, heap_buffer, override_dir, fallback_filename); + const char *path_buffer = Util::join_paths (stack_buffer, sizeof (stack_buffer), heap_buffer, override_dir, fallback_filename); std::string_view path_view { path_buffer }; ret = log_and_return (open_file (path_view), path_view); diff --git a/src/native/common/runtime-base/timing-internal.cc b/src/native/common/runtime-base/timing-internal.cc index 2e6defeccf7..b676e648d00 100644 --- a/src/native/common/runtime-base/timing-internal.cc +++ b/src/native/common/runtime-base/timing-internal.cc @@ -198,7 +198,7 @@ void FastTiming::dump_to_file (size_t entries) noexcept std::string_view temporary_directory = getenv ("TMPDIR"); char stack_buffer [Util::LocalPathBufferSize]; char *heap_buffer; - const char *timing_log_path = Util::join_paths (stack_buffer, heap_buffer, temporary_directory, file_name); + const char *timing_log_path = Util::join_paths (stack_buffer, sizeof (stack_buffer), heap_buffer, temporary_directory, file_name); FILE *timing_log = Util::monodroid_fopen (timing_log_path, "w"); if (timing_log == nullptr) { diff --git a/src/native/mono/runtime-base/util.hh b/src/native/mono/runtime-base/util.hh index c46142bc397..6e5f90c3d4d 100644 --- a/src/native/mono/runtime-base/util.hh +++ b/src/native/mono/runtime-base/util.hh @@ -145,11 +145,10 @@ namespace xamarin::android return static_cast(path_length); } - template - static auto join_paths (char (&stack_buffer)[Size], char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> const char* + static auto join_paths (char *stack_buffer, size_t stack_buffer_size, char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> const char* { heap_buffer = nullptr; - ssize_t result = join_paths (stack_buffer, Size, first, second); + ssize_t result = join_paths (stack_buffer, stack_buffer_size, first, second); if (result >= 0) { return stack_buffer; } From cd1c29eb0c2ad2852c69cf01034d18e40c8babe9 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 23:38:39 +0200 Subject: [PATCH 19/21] Use returned pointers for buffer ownership Remove heap-buffer out parameters, free only returned heap pointers, and keep the former static XDG path buffer stack-only. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/host/host-environment.hh | 13 ++++++------- src/native/clr/include/runtime-base/util.hh | 5 ++--- src/native/clr/runtime-base/android-system.cc | 14 ++++++++------ src/native/clr/runtime-base/logger.cc | 7 ++++--- src/native/common/runtime-base/timing-internal.cc | 15 ++++++++++----- src/native/mono/runtime-base/util.hh | 5 ++--- 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index f13fb2723a4..03f216a8bb2 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -93,19 +93,18 @@ namespace xamarin::android { { std::string_view home_path = home.get_string_view (); char stack_buffer [Util::LocalPathBufferSize]; - char *heap_buffer; - const char *dir_path = Util::join_paths (stack_buffer, sizeof (stack_buffer), heap_buffer, home_path, relative_path); + ssize_t result = Util::join_paths (stack_buffer, sizeof (stack_buffer), home_path, relative_path); + abort_unless (result >= 0, "XDG directory path is too long"); - log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", dir_path); - int rv = Util::create_directory (dir_path, Constants::DEFAULT_DIRECTORY_MODE); + log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", stack_buffer); + int rv = Util::create_directory (stack_buffer, Constants::DEFAULT_DIRECTORY_MODE); if (rv < 0 && errno != EEXIST) { - log_warnf (LOG_DEFAULT, "Failed to create XDG directory %s. %s", dir_path, strerror (errno)); + log_warnf (LOG_DEFAULT, "Failed to create XDG directory %s. %s", stack_buffer, strerror (errno)); } if (!environment_variable_name.empty ()) { - set_variable (environment_variable_name.data (), dir_path); + set_variable (environment_variable_name.data (), stack_buffer); } - std::free (heap_buffer); } [[gnu::flatten, gnu::always_inline]] diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 204bd77b308..962bcee2154 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -344,16 +344,15 @@ namespace xamarin::android { return static_cast(path_length); } - static auto join_paths (char *stack_buffer, size_t stack_buffer_size, char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> const char* + static auto join_paths (char *stack_buffer, size_t stack_buffer_size, std::string_view first, std::string_view second) noexcept -> char* { - heap_buffer = nullptr; ssize_t result = join_paths (stack_buffer, stack_buffer_size, first, second); if (result >= 0) { return stack_buffer; } size_t required_capacity = static_cast(-result); - heap_buffer = static_cast (std::malloc (required_capacity)); + char *heap_buffer = static_cast (std::malloc (required_capacity)); abort_unless (heap_buffer != nullptr, "Failed to allocate joined path"); result = join_paths (heap_buffer, required_capacity, first, second); abort_unless (result >= 0, "Failed to join path using the required capacity"); diff --git a/src/native/clr/runtime-base/android-system.cc b/src/native/clr/runtime-base/android-system.cc index 765d916d8e7..bafb0276289 100644 --- a/src/native/clr/runtime-base/android-system.cc +++ b/src/native/clr/runtime-base/android-system.cc @@ -259,14 +259,15 @@ AndroidSystem::setup_environment () noexcept log_debug (LOG_DEFAULT, "Loading environment from the override directory."sv); char stack_buffer [Util::LocalPathBufferSize]; - char *heap_buffer; - const char *env_override_file = Util::join_paths (stack_buffer, sizeof (stack_buffer), heap_buffer, primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); + char *env_override_file = Util::join_paths (stack_buffer, sizeof (stack_buffer), primary_override_dir, Constants::OVERRIDE_ENVIRONMENT_FILE_NAME); if (Util::file_exists (env_override_file)) { log_debug (LOG_DEFAULT, "Loading {}"sv, env_override_file); setup_environment_from_override_file (env_override_file); } - std::free (heap_buffer); + if (env_override_file != stack_buffer) { + std::free (env_override_file); + } #endif // def DEBUG } @@ -276,8 +277,7 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep // appDirs[Constants::APP_DIRS_DATA_DIR_INDEX] points to the native library directory std::string_view app_data_dir = appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_string_view (); char stack_buffer [Util::LocalPathBufferSize]; - char *heap_buffer; - const char *libmonodroid_path = Util::join_paths (stack_buffer, sizeof (stack_buffer), heap_buffer, app_data_dir, "libmonodroid.so"sv); + char *libmonodroid_path = Util::join_paths (stack_buffer, sizeof (stack_buffer), app_data_dir, "libmonodroid.so"sv); log_debug (LOG_ASSEMBLY, "Checking if libmonodroid was unpacked to {}", libmonodroid_path); if (!Util::file_exists (libmonodroid_path)) { @@ -288,7 +288,9 @@ AndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcep set_embedded_dso_mode_enabled (false); native_libraries_dir.assign (appDirs[Constants::APP_DIRS_DATA_DIR_INDEX].get_cstr ()); } - std::free (heap_buffer); + if (libmonodroid_path != stack_buffer) { + std::free (libmonodroid_path); + } } auto diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index ba072ce6d93..d2a9a1291fe 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -78,12 +78,13 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p Util::create_public_directory (override_dir); Util::create_public_directory (override_dir); char stack_buffer [Util::LocalPathBufferSize]; - char *heap_buffer; - const char *path_buffer = Util::join_paths (stack_buffer, sizeof (stack_buffer), heap_buffer, override_dir, fallback_filename); + char *path_buffer = Util::join_paths (stack_buffer, sizeof (stack_buffer), override_dir, fallback_filename); std::string_view path_view { path_buffer }; ret = log_and_return (open_file (path_view), path_view); - std::free (heap_buffer); + if (path_buffer != stack_buffer) { + std::free (path_buffer); + } return ret; } diff --git a/src/native/common/runtime-base/timing-internal.cc b/src/native/common/runtime-base/timing-internal.cc index b676e648d00..480bf582381 100644 --- a/src/native/common/runtime-base/timing-internal.cc +++ b/src/native/common/runtime-base/timing-internal.cc @@ -197,20 +197,23 @@ void FastTiming::dump_to_file (size_t entries) noexcept std::string_view file_name = output_file_name == nullptr ? default_timing_file_name : *output_file_name; std::string_view temporary_directory = getenv ("TMPDIR"); char stack_buffer [Util::LocalPathBufferSize]; - char *heap_buffer; - const char *timing_log_path = Util::join_paths (stack_buffer, sizeof (stack_buffer), heap_buffer, temporary_directory, file_name); + char *timing_log_path = Util::join_paths (stack_buffer, sizeof (stack_buffer), temporary_directory, file_name); FILE *timing_log = Util::monodroid_fopen (timing_log_path, "w"); if (timing_log == nullptr) { log_error (LOG_TIMING, "[2/2] Unable to create the performance measurements file '{}'"sv, timing_log_path); - std::free (heap_buffer); + if (timing_log_path != stack_buffer) { + std::free (timing_log_path); + } return; } if (!Util::set_world_accessible (fileno (timing_log))) { log_warn (LOG_TIMING, "[2/2] Failed to make performance measurements file '{}' world-readable"sv, timing_log_path); fclose (timing_log); - std::free (heap_buffer); + if (timing_log_path != stack_buffer) { + std::free (timing_log_path); + } return; } @@ -226,7 +229,9 @@ void FastTiming::dump_to_file (size_t entries) noexcept dump (entries, true /* indent */, line_writer); fflush (timing_log); fclose (timing_log); - std::free (heap_buffer); + if (timing_log_path != stack_buffer) { + std::free (timing_log_path); + } } void FastTiming::dump () noexcept diff --git a/src/native/mono/runtime-base/util.hh b/src/native/mono/runtime-base/util.hh index 6e5f90c3d4d..192b9b6a6a1 100644 --- a/src/native/mono/runtime-base/util.hh +++ b/src/native/mono/runtime-base/util.hh @@ -145,16 +145,15 @@ namespace xamarin::android return static_cast(path_length); } - static auto join_paths (char *stack_buffer, size_t stack_buffer_size, char *&heap_buffer, std::string_view first, std::string_view second) noexcept -> const char* + static auto join_paths (char *stack_buffer, size_t stack_buffer_size, std::string_view first, std::string_view second) noexcept -> char* { - heap_buffer = nullptr; ssize_t result = join_paths (stack_buffer, stack_buffer_size, first, second); if (result >= 0) { return stack_buffer; } size_t required_capacity = static_cast(-result); - heap_buffer = static_cast (std::malloc (required_capacity)); + char *heap_buffer = static_cast (std::malloc (required_capacity)); abort_unless (heap_buffer != nullptr, "Failed to allocate joined path"); result = join_paths (heap_buffer, required_capacity, first, second); abort_unless (result >= 0, "Failed to join path using the required capacity"); From e30f0aaee0e96fb9506db57fce978b558047c2d0 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 25 Aug 2026 23:50:22 +0200 Subject: [PATCH 20/21] Avoid duplicate logger directory setup Create the fallback logging directory only once before opening the fallback file. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/runtime-base/logger.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/native/clr/runtime-base/logger.cc b/src/native/clr/runtime-base/logger.cc index d2a9a1291fe..c00d23e6cad 100644 --- a/src/native/clr/runtime-base/logger.cc +++ b/src/native/clr/runtime-base/logger.cc @@ -76,7 +76,6 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p return log_and_return (ret, custom_path); } Util::create_public_directory (override_dir); - Util::create_public_directory (override_dir); char stack_buffer [Util::LocalPathBufferSize]; char *path_buffer = Util::join_paths (stack_buffer, sizeof (stack_buffer), override_dir, fallback_filename); From c49e3cead2012890e250cdad516b960c6b91d020 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Wed, 26 Aug 2026 00:23:26 +0200 Subject: [PATCH 21/21] Disambiguate path formatting helpers Give the length-returning formatter a distinct name while preserving join_paths as the stack-or-heap pointer API. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/native/clr/include/host/host-environment.hh | 2 +- src/native/clr/include/runtime-base/util.hh | 6 +++--- src/native/mono/runtime-base/util.hh | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/native/clr/include/host/host-environment.hh b/src/native/clr/include/host/host-environment.hh index 03f216a8bb2..5d50fb46959 100644 --- a/src/native/clr/include/host/host-environment.hh +++ b/src/native/clr/include/host/host-environment.hh @@ -93,7 +93,7 @@ namespace xamarin::android { { std::string_view home_path = home.get_string_view (); char stack_buffer [Util::LocalPathBufferSize]; - ssize_t result = Util::join_paths (stack_buffer, sizeof (stack_buffer), home_path, relative_path); + ssize_t result = Util::format_joined_path (stack_buffer, sizeof (stack_buffer), home_path, relative_path); abort_unless (result >= 0, "XDG directory path is too long"); log_debugf (LOG_DEFAULT, "Creating XDG directory: %s", stack_buffer); diff --git a/src/native/clr/include/runtime-base/util.hh b/src/native/clr/include/runtime-base/util.hh index 962bcee2154..ea6280418db 100644 --- a/src/native/clr/include/runtime-base/util.hh +++ b/src/native/clr/include/runtime-base/util.hh @@ -311,7 +311,7 @@ namespace xamarin::android { } // Returns the path length excluding NUL, or the negative required capacity including NUL. - static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> ssize_t + static auto format_joined_path (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> ssize_t { bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); @@ -346,7 +346,7 @@ namespace xamarin::android { static auto join_paths (char *stack_buffer, size_t stack_buffer_size, std::string_view first, std::string_view second) noexcept -> char* { - ssize_t result = join_paths (stack_buffer, stack_buffer_size, first, second); + ssize_t result = format_joined_path (stack_buffer, stack_buffer_size, first, second); if (result >= 0) { return stack_buffer; } @@ -354,7 +354,7 @@ namespace xamarin::android { size_t required_capacity = static_cast(-result); char *heap_buffer = static_cast (std::malloc (required_capacity)); abort_unless (heap_buffer != nullptr, "Failed to allocate joined path"); - result = join_paths (heap_buffer, required_capacity, first, second); + result = format_joined_path (heap_buffer, required_capacity, first, second); abort_unless (result >= 0, "Failed to join path using the required capacity"); return heap_buffer; } diff --git a/src/native/mono/runtime-base/util.hh b/src/native/mono/runtime-base/util.hh index 192b9b6a6a1..22dc3d4cf6c 100644 --- a/src/native/mono/runtime-base/util.hh +++ b/src/native/mono/runtime-base/util.hh @@ -112,7 +112,7 @@ namespace xamarin::android } // Returns the path length excluding NUL, or the negative required capacity including NUL. - static auto join_paths (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> ssize_t + static auto format_joined_path (char *buffer, size_t buffer_size, std::string_view first, std::string_view second) noexcept -> ssize_t { bool remove_duplicate_separator = first.ends_with ('/') && second.starts_with ('/'); bool add_separator = !first.empty () && !second.empty () && !first.ends_with ('/') && !second.starts_with ('/'); @@ -147,7 +147,7 @@ namespace xamarin::android static auto join_paths (char *stack_buffer, size_t stack_buffer_size, std::string_view first, std::string_view second) noexcept -> char* { - ssize_t result = join_paths (stack_buffer, stack_buffer_size, first, second); + ssize_t result = format_joined_path (stack_buffer, stack_buffer_size, first, second); if (result >= 0) { return stack_buffer; } @@ -155,7 +155,7 @@ namespace xamarin::android size_t required_capacity = static_cast(-result); char *heap_buffer = static_cast (std::malloc (required_capacity)); abort_unless (heap_buffer != nullptr, "Failed to allocate joined path"); - result = join_paths (heap_buffer, required_capacity, first, second); + result = format_joined_path (heap_buffer, required_capacity, first, second); abort_unless (result >= 0, "Failed to join path using the required capacity"); return heap_buffer; }