Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 90 additions & 15 deletions src/native/clr/host/fastdev-assemblies.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <unistd.h>

#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <string>

#include <constants.hh>
#include <host/fastdev-assemblies.hh>
Expand All @@ -15,6 +15,49 @@

using namespace xamarin::android;

namespace {
// A minimal growable NUL-terminated character buffer. The TPA list has no useful upper bound -
// it holds one absolute path per assembly in the override directory - so it grows on demand
// rather than using a fixed buffer.
struct char_buffer final
{
char *data = nullptr;
size_t length = 0;
size_t capacity = 0;
};

constexpr size_t CHAR_BUFFER_INITIAL_CAPACITY = 512uz;

// Appends `text` to `buffer`, growing it if needed. Returns `false` if the buffer could not be
// grown, in which case `buffer` is left unchanged and still owns its storage.
auto buffer_append (char_buffer &buffer, std::string_view text) noexcept -> bool
{
size_t required_capacity = Helpers::add_with_overflow_check<size_t> (buffer.length, text.length ());
required_capacity = Helpers::add_with_overflow_check<size_t> (required_capacity, 1uz);

if (required_capacity > buffer.capacity) {
size_t new_capacity = buffer.capacity == 0 ? CHAR_BUFFER_INITIAL_CAPACITY : buffer.capacity;
while (new_capacity < required_capacity) {
new_capacity = Helpers::add_with_overflow_check<size_t> (new_capacity, new_capacity);
}

char *grown = static_cast<char*> (std::realloc (buffer.data, new_capacity));
if (grown == nullptr) {
return false;
}

buffer.data = grown;
buffer.capacity = new_capacity;
}

memcpy (buffer.data + buffer.length, text.data (), text.length ());
buffer.length += text.length ();
buffer.data [buffer.length] = '\0';

return true;
}
}

auto FastDevAssemblies::open_assembly (std::string_view const& name, int64_t &size) noexcept -> void*
{
size = 0;
Expand Down Expand Up @@ -104,15 +147,27 @@ auto FastDevAssemblies::open_assembly (std::string_view const& name, int64_t &si
// the allocated space is no longer (if ever) needed by CoreCLR. Probably would be best if
// CoreCLR notified us when it wants to free the data, as that eliminates any races as well
// as ambiguity.
auto buffer = new uint8_t[file_size.value ()];
auto buffer = static_cast<uint8_t*> (std::malloc (file_size.value ()));
if (buffer == nullptr) [[unlikely]] {
log_warnf (
LOG_ASSEMBLY,
"Unable to allocate memory for FastDev assembly '%.*s'",
static_cast<int>(name.length ()), name.data ()
);

close (asm_fd);
size = 0;
return nullptr;
}

ssize_t nread = 0;
do {
nread = read (asm_fd, reinterpret_cast<void*>(buffer), file_size.value ());
} while (nread == -1 && errno == EINTR);
close (asm_fd);

if (nread != size) [[unlikely]] {
delete[] buffer;
std::free (buffer);

log_warnf (
LOG_ASSEMBLY,
Expand All @@ -129,26 +184,28 @@ auto FastDevAssemblies::open_assembly (std::string_view const& name, int64_t &si
return reinterpret_cast<void*>(buffer);
}

auto FastDevAssemblies::build_tpa_list (std::string &tpa_list) noexcept -> bool
auto FastDevAssemblies::build_tpa_list () noexcept -> char*
{
tpa_list.clear ();
tpa_in_use = false;

const char *override_dir_path = AndroidSystem::get_primary_override_dir ();
if (!Util::dir_exists (override_dir_path)) {
return false;
return nullptr;
}
Comment thread
simonrozsival marked this conversation as resolved.
Resolved

DIR *dir = opendir (override_dir_path);
if (dir == nullptr) {
log_warnf (LOG_ASSEMBLY, "FastDev: failed to open override dir '%s'. %s", override_dir_path, std::strerror (errno));
return false;
return nullptr;
}

constexpr std::string_view dll_ext { ".dll" };
constexpr std::string_view r2r_ext { ".r2r.dll" };
constexpr std::string_view corelib_name { "System.Private.CoreLib.dll" };
char_buffer tpa_list {};
bool found_corelib = false;
bool found_r2r = false;
bool out_of_memory = false;
size_t count = 0;
dirent *e;
while ((e = readdir (dir)) != nullptr) {
Expand All @@ -166,19 +223,31 @@ auto FastDevAssemblies::build_tpa_list (std::string &tpa_list) noexcept -> bool
break;
}

if (!tpa_list.empty ()) {
tpa_list.append (":");
if (tpa_list.length > 0 && !buffer_append (tpa_list, ":"sv)) {
out_of_memory = true;
break;
}
if (!buffer_append (tpa_list, override_dir_path) ||
!buffer_append (tpa_list, "/"sv) ||
!buffer_append (tpa_list, name)) {
out_of_memory = true;
break;
}
tpa_list.append (override_dir_path);
tpa_list.append ("/");
tpa_list.append (name);
if (name == corelib_name) {
found_corelib = true;
}
count++;
}
closedir (dir);

if (out_of_memory) {
// FastDev is a Debug-only convenience, so fall back to the probe-only path rather than
// aborting the application.
log_warn (LOG_ASSEMBLY, "FastDev: unable to allocate memory for the TPA list");
std::free (tpa_list.data);
return nullptr;
}

log_debugf (
LOG_ASSEMBLY,
"FastDev: built TPA list with %zu assemblies from '%s' (corelib=%s, r2r=%s)",
Expand All @@ -197,8 +266,14 @@ auto FastDevAssemblies::build_tpa_list (std::string &tpa_list) noexcept -> bool
// CoreCLR's `.r2r.dll` probes aren't compatible with our TPA path.
if (count > 0 && found_corelib && !found_r2r) {
tpa_in_use = true;
return true;
return tpa_list.data;
}
tpa_list.clear ();
return false;
std::free (tpa_list.data);
return nullptr;
}

void FastDevAssemblies::discard_tpa_list (char *tpa_list) noexcept
{
std::free (tpa_list);
tpa_in_use = false;
}
84 changes: 56 additions & 28 deletions src/native/clr/host/host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#include <cerrno>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <unistd.h>

#include <android/looper.h>
Expand All @@ -31,6 +30,7 @@
#include <runtime-base/monodroid-dl.hh>
#include <runtime-base/monodroid-state.hh>
#include <runtime-base/timing-internal.hh>
#include <runtime-base/util.hh>
#include <shared/log_types.hh>

using namespace xamarin::android;
Expand Down Expand Up @@ -140,10 +140,12 @@ void Host::scan_filesystem_for_assemblies_and_libraries () noexcept

log_debugf (LOG_ASSEMBLY, "Found assembly store in '%s/%s'", native_lib_dir, Constants::assembly_store_file_name.data ());

std::string store_path = native_lib_dir;
store_path.append ("/"sv);
store_path.append (cur->d_name);
map_assembly_store_via_dlopen (store_path.c_str ());
char stack_buffer [Util::LocalPathBufferSize];
char *store_path = Util::join_paths (stack_buffer, sizeof (stack_buffer), native_lib_dir, cur->d_name);
map_assembly_store_via_dlopen (store_path);
if (store_path != stack_buffer) {
std::free (store_path);
}
break; // we've found all we need
}
} while (true);
Expand Down Expand Up @@ -374,14 +376,15 @@ void Host::Java_mono_android_Runtime_initInternal (
// string for us since assemblies are read straight out of the APK. Point it at the application's
// files directory, the same value MonoVM has always used. `.NET` terminates the base directory
// with a directory separator, so we do too.
// Storage must outlive `coreclr_initialize`, hence the function-local static. Assign on every
// call rather than relying on the initializer, which would only ever see the first `files_dir`.
static std::string app_context_base_directory;
app_context_base_directory.assign (files_dir.get_cstr ());
if (!app_context_base_directory.ends_with ('/')) {
app_context_base_directory.push_back ('/');
}
init_runtime_property_values[RUNTIME_PROPERTY_INDEX_APP_CONTEXT_BASE_DIRECTORY] = const_cast<char*>(app_context_base_directory.c_str ());
// Storage must outlive `coreclr_initialize`, hence the function-local static. A plain pointer is
// constant-initialized, so it needs no guard variable. Assign on every call rather than relying
// on the initializer, which would only ever see the first `files_dir`. Runtime initialization is
// process-global, so no live CLR instance can retain the previous value when it is replaced.
static char *app_context_base_directory = nullptr;
std::free (app_context_base_directory);
// A null stack buffer makes `join_paths` return a `malloc`ed result with explicit ownership.
app_context_base_directory = Util::join_paths (nullptr, 0uz, files_dir.get_cstr (), "/"sv);
init_runtime_property_values[RUNTIME_PROPERTY_INDEX_APP_CONTEXT_BASE_DIRECTORY] = app_context_base_directory;

const char **prop_names = init_runtime_property_names;
const char **prop_values = const_cast<const char**>(init_runtime_property_values);
Expand All @@ -394,20 +397,45 @@ void Host::Java_mono_android_Runtime_initInternal (
// managed stack traces (file/line).
if constexpr (Constants::is_debug_build) {
// Storage must outlive `coreclr_initialize`; function-local statics
// give us process lifetime without polluting global namespace.
static std::string fastdev_tpa_list;
static std::vector<const char*> fastdev_prop_names;
static std::vector<const char*> fastdev_prop_values;

if (FastDevAssemblies::build_tpa_list (fastdev_tpa_list)) {
fastdev_prop_names.assign (prop_names, prop_names + prop_count);
fastdev_prop_values.assign (prop_values, prop_values + prop_count);
fastdev_prop_names.push_back (HOST_PROPERTY_TRUSTED_PLATFORM_ASSEMBLIES);
fastdev_prop_values.push_back (fastdev_tpa_list.c_str ());

prop_names = fastdev_prop_names.data ();
prop_values = fastdev_prop_values.data ();
prop_count = static_cast<int>(fastdev_prop_names.size ());
// give us process lifetime without polluting global namespace. All
// three are plain pointers, so they are constant-initialized and need
// no guard variables.
static char *fastdev_tpa_list = nullptr;
static const char **fastdev_prop_names = nullptr;
static const char **fastdev_prop_values = nullptr;

FastDevAssemblies::discard_tpa_list (fastdev_tpa_list);
std::free (fastdev_prop_names);
std::free (fastdev_prop_values);
fastdev_tpa_list = nullptr;
fastdev_prop_names = nullptr;
fastdev_prop_values = nullptr;

fastdev_tpa_list = FastDevAssemblies::build_tpa_list ();
if (fastdev_tpa_list != nullptr) {
size_t old_count = static_cast<size_t>(prop_count);
size_t new_count = Helpers::add_with_overflow_check<size_t> (old_count, 1uz);

fastdev_prop_names = static_cast<const char**> (std::calloc (new_count, sizeof (const char*)));
fastdev_prop_values = static_cast<const char**> (std::calloc (new_count, sizeof (const char*)));
if (fastdev_prop_names == nullptr || fastdev_prop_values == nullptr) [[unlikely]] {
log_warn (LOG_ASSEMBLY, "FastDev: unable to allocate memory for the runtime properties; falling back to assembly probing");
std::free (fastdev_prop_names);
std::free (fastdev_prop_values);
FastDevAssemblies::discard_tpa_list (fastdev_tpa_list);
fastdev_prop_names = nullptr;
fastdev_prop_values = nullptr;
fastdev_tpa_list = nullptr;
} else {
memcpy (fastdev_prop_names, prop_names, old_count * sizeof (const char*));
memcpy (fastdev_prop_values, prop_values, old_count * sizeof (const char*));
fastdev_prop_names [old_count] = HOST_PROPERTY_TRUSTED_PLATFORM_ASSEMBLIES;
fastdev_prop_values [old_count] = fastdev_tpa_list;

prop_names = fastdev_prop_names;
prop_values = fastdev_prop_values;
prop_count = static_cast<int>(new_count);
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions src/native/clr/include/host/fastdev-assemblies.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <dirent.h>

#include <cstdint>
#include <string>
#include <string_view>

#include <runtime-base/mutex.hh>
Expand All @@ -14,16 +13,29 @@ namespace xamarin::android {
public:
#if defined(DEBUG)
static auto open_assembly (std::string_view const& name, int64_t &size) noexcept -> void*;
static auto build_tpa_list (std::string &tpa_list) noexcept -> bool;

// Builds the `TRUSTED_PLATFORM_ASSEMBLIES` list from the override directory. Returns a
// `malloc`ed, NUL-terminated, `:`-separated list of absolute paths which the caller owns,
// or `nullptr` when no usable list could be built.
static auto build_tpa_list () noexcept -> char*;

// Frees a list returned by `build_tpa_list ()` and puts assembly loading back on the
// probe-only path. Callers use this instead of touching `tpa_in_use` directly, which does
// not exist in Release builds.
static void discard_tpa_list (char *tpa_list) noexcept;
#else
static auto open_assembly ([[maybe_unused]] std::string_view const& name, [[maybe_unused]] int64_t &size) noexcept -> void*
{
return nullptr;
}

static auto build_tpa_list ([[maybe_unused]] std::string &tpa_list) noexcept -> bool
static auto build_tpa_list () noexcept -> char*
{
return nullptr;
}

static void discard_tpa_list ([[maybe_unused]] char *tpa_list) noexcept
{
return false;
}
#endif

Expand Down
Loading
Loading