[go: nahoru, domu]

Skip to content

Commit

Permalink
[mac] Fix failing Designer test (dotnet#6202)
Browse files Browse the repository at this point in the history
Context: dotnet@69289d7

69289d7 introduced the use of POSIX semaphore API to protect assembly
mmap code from race conditions.   However, it turns out that macOS
implementation of the semaphore is deprecated and always returns an
error, which in turn makes the Xamarin.Android runtime to [abort][0]
the application:

    [2021-08-18 23:45:32.5] Renderer (error) >> ../../../jni/embedded-assemblies.hh:89 (prepare_for_multiple_threads): Failed to initialize assembly mapping semaphore. Function not implemented
    [2021-08-18 23:45:32.5] Renderer >>
    [2021-08-18 23:45:32.5] Renderer >> =================================================================
    [2021-08-18 23:45:32.5] Renderer >> 	Native Crash Reporting
    [2021-08-18 23:45:32.5] Renderer >> =================================================================
    [2021-08-18 23:45:32.5] Renderer >> Got a abrt while executing native code. This usually indicates
    [2021-08-18 23:45:32.5] Renderer >> a fatal error in the mono runtime or one of the native libraries
    [2021-08-18 23:45:32.5] Renderer >> used by your application.
    [2021-08-18 23:45:32.5] Renderer >> =================================================================

While our MinGW Windows build supports and implements the semaphore API,
this commit changes the runtime code to use a standard mutex
(`std::mutex`) for both Windows and macOS instead of the semaphore API.

Additionally, fix a handful of warnings when building for desktop
platforms.

[0]: https://github.com/xamarin/xamarin-android/pull/6188/files#r691713959
  • Loading branch information
grendello committed Aug 19, 2021
1 parent e811ecb commit ef09bf7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/monodroid/jni/application_dso_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ApplicationConfig application_config = {
.environment_variable_count = 0,
.system_property_count = 0,
.number_of_assemblies_in_apk = 0,
.bundled_assembly_name_width = 0,
.android_package_name = "com.xamarin.test",
};

Expand All @@ -66,6 +67,7 @@ XamarinAndroidBundledAssembly bundled_assemblies[] = {
.data_offset = 0,
.data_size = 0,
.data = nullptr,
.name_length = 0,
.name = first_assembly_name,
},

Expand All @@ -74,6 +76,7 @@ XamarinAndroidBundledAssembly bundled_assemblies[] = {
.data_offset = 0,
.data_size = 0,
.data = nullptr,
.name_length = 0,
.name = second_assembly_name,
},
};
9 changes: 8 additions & 1 deletion src/monodroid/jni/embedded-assemblies.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class MonoGuidString final
char *guid = nullptr;
};

#if !defined (ANDROID)
std::mutex EmbeddedAssemblies::assembly_mmap_mutex;
#endif

void EmbeddedAssemblies::set_assemblies_prefix (const char *prefix)
{
if (assemblies_prefix_override != nullptr)
Expand Down Expand Up @@ -188,6 +192,7 @@ EmbeddedAssemblies::map_runtime_file (XamarinAndroidBundledAssembly& file) noexc
//
// POSIX semaphores should be perfectly fine for us here.
//
#if defined (ANDROID)
PosixSemaphoreGuard sem (assembly_mmap_semaphore);

if constexpr (AbortOnFailure) {
Expand All @@ -198,7 +203,9 @@ EmbeddedAssemblies::map_runtime_file (XamarinAndroidBundledAssembly& file) noexc
return;
}
}

#else
std::lock_guard<std::mutex> mmap_mutex (assembly_mmap_mutex);
#endif
if (file.data == nullptr) {
file.data = static_cast<uint8_t*>(map_info.area);
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/monodroid/jni/embedded-assemblies.hh
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ namespace xamarin::android::internal {

void prepare_for_multiple_threads () noexcept
{
#if defined (ANDROID)
int ret = sem_init (&assembly_mmap_semaphore, 0 /* pshared */, 1 /* value */);
abort_unless (ret == 0, "Failed to initialize assembly mapping semaphore. %s", strerror (errno));
#endif
}

/* returns current number of *all* assemblies found from all invocations */
Expand Down Expand Up @@ -234,7 +236,11 @@ namespace xamarin::android::internal {

bool register_debug_symbols;
bool have_and_want_debug_symbols;
#if defined (ANDROID)
sem_t assembly_mmap_semaphore;
#else
static std::mutex assembly_mmap_mutex;
#endif
size_t bundled_assembly_index = 0;
#if defined (DEBUG) || !defined (ANDROID)
TypeMappingInfo *java_to_managed_maps;
Expand Down

0 comments on commit ef09bf7

Please sign in to comment.