From 45557e737d2fa3c7afa2d6583e3b42fe7fc5d4a3 Mon Sep 17 00:00:00 2001 From: Milos Kotlar Date: Thu, 28 May 2026 09:09:21 +0200 Subject: [PATCH 1/4] [runtime] Register host_runtime_contract callbacks so CoreCLR finds System.Private.CoreLib.dll dotnet/runtime#128278 (.NET 11 preview 5) gated the BindToSystem CoreLib fallback behind `Bundle::AppIsBundle () && Bundle::AppBundle->HasExtractedFiles ()`. macios doesn't bundle assemblies and never registered `bundle_probe` nor a `BUNDLE_EXTRACTION_PATH` property, so the gate fails and CoreCLR can't locate `System.Private.CoreLib.dll` (which lives at the app bundle root, not next to `libcoreclr.framework/libcoreclr`). The app aborts in `xamarin_vm_initialize` with "Failed to initialize the VM". Fix: register a no-op `bundle_probe` so `Bundle::AppBundle` is non-null, and answer `BUNDLE_EXTRACTION_PATH` from `get_runtime_property` with the directory that actually contains CoreLib (app bundle root, then `.xamarin/`, matching the TPA probe order). The fallback then finds CoreLib and the VM initializes. Fixes #25542. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- runtime/runtime.m | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/runtime/runtime.m b/runtime/runtime.m index 74ad2e77af4..79346b60a78 100644 --- a/runtime/runtime.m +++ b/runtime/runtime.m @@ -2463,6 +2463,55 @@ -(struct NSObjectData*) xamarinGetNSObjectData; return true; } + +// dotnet/runtime#128278 (.NET 11 preview 5) gates the BindToSystem CoreLib fallback on +// Bundle::AppIsBundle () && Bundle::AppBundle->HasExtractedFiles (). Register a no-op +// bundle_probe so AppBundle is non-null, and answer BUNDLE_EXTRACTION_PATH with the +// directory containing System.Private.CoreLib.dll so the fallback finds it. +static bool xamarin_coreclr_bundle_probe(const char *path, int64_t *offset, int64_t *size, int64_t *compressed_size) +{ + return false; +} + +static const char *xamarin_compute_corelib_directory(void) +{ + static char *cached = NULL; + + if (cached) + return cached; + + const char *bundle_path = xamarin_get_bundle_path (); + NSFileManager *manager = [NSFileManager defaultManager]; + NSString *candidates [] = { + [NSString stringWithUTF8String: bundle_path], + [NSString stringWithFormat: @"%s/.xamarin/%s", bundle_path, RUNTIMEIDENTIFIER], + }; + for (size_t i = 0; i < sizeof (candidates) / sizeof (candidates [0]); i++) { + NSString *probe = [candidates [i] stringByAppendingPathComponent: @"System.Private.CoreLib.dll"]; + if ([manager fileExistsAtPath: probe]) { + cached = strdup ([candidates [i] UTF8String]); + break; + } + } + + return cached; +} + +static size_t xamarin_coreclr_get_runtime_property(const char *key, char *value_buffer, size_t value_buffer_size, void *contract_context) +{ + if (strcmp (key, HOST_PROPERTY_BUNDLE_EXTRACTION_PATH) != 0) + return (size_t) -1; + + const char *dir = xamarin_compute_corelib_directory (); + if (dir == NULL) + return (size_t) -1; + + size_t len = strlen (dir); + size_t required = len + 1; // include null terminator + if (value_buffer != NULL && value_buffer_size >= required) + memcpy (value_buffer, dir, required); + return required; +} #endif // defined (CORECLR_RUNTIME) void @@ -2471,6 +2520,8 @@ -(struct NSObjectData*) xamarinGetNSObjectData; #if defined (CORECLR_RUNTIME) struct host_runtime_contract host_contract = { .size = sizeof (struct host_runtime_contract), + .get_runtime_property = &xamarin_coreclr_get_runtime_property, + .bundle_probe = &xamarin_coreclr_bundle_probe, .pinvoke_override = &xamarin_pinvoke_override, .get_native_code_data = &xamarin_get_native_code_data }; From bd63b6a0fd256381e470d8f14d10de264d5ebad4 Mon Sep 17 00:00:00 2001 From: Milos Kotlar Date: Thu, 28 May 2026 09:36:23 +0200 Subject: [PATCH 2/4] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- runtime/runtime.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/runtime.m b/runtime/runtime.m index 79346b60a78..b7908e8489a 100644 --- a/runtime/runtime.m +++ b/runtime/runtime.m @@ -2497,7 +2497,7 @@ static bool xamarin_coreclr_bundle_probe(const char *path, int64_t *offset, int6 return cached; } -static size_t xamarin_coreclr_get_runtime_property(const char *key, char *value_buffer, size_t value_buffer_size, void *contract_context) +static size_t xamarin_coreclr_get_runtime_property (const char *key, char *value_buffer, size_t value_buffer_size, void *contract_context) { if (strcmp (key, HOST_PROPERTY_BUNDLE_EXTRACTION_PATH) != 0) return (size_t) -1; From 3d647c8bb35498854fc5da8b745d01afd65408ce Mon Sep 17 00:00:00 2001 From: Milos Kotlar Date: Thu, 28 May 2026 09:36:32 +0200 Subject: [PATCH 3/4] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- runtime/runtime.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/runtime.m b/runtime/runtime.m index b7908e8489a..8f89ded4250 100644 --- a/runtime/runtime.m +++ b/runtime/runtime.m @@ -2473,7 +2473,7 @@ static bool xamarin_coreclr_bundle_probe(const char *path, int64_t *offset, int6 return false; } -static const char *xamarin_compute_corelib_directory(void) +static const char *xamarin_compute_corelib_directory (void) { static char *cached = NULL; From 0a31d6e20ad6c1e154ac761d43cd949d81a1c29d Mon Sep 17 00:00:00 2001 From: Milos Kotlar Date: Thu, 28 May 2026 09:36:44 +0200 Subject: [PATCH 4/4] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- runtime/runtime.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/runtime.m b/runtime/runtime.m index 8f89ded4250..86902f2adeb 100644 --- a/runtime/runtime.m +++ b/runtime/runtime.m @@ -2468,7 +2468,7 @@ -(struct NSObjectData*) xamarinGetNSObjectData; // Bundle::AppIsBundle () && Bundle::AppBundle->HasExtractedFiles (). Register a no-op // bundle_probe so AppBundle is non-null, and answer BUNDLE_EXTRACTION_PATH with the // directory containing System.Private.CoreLib.dll so the fallback finds it. -static bool xamarin_coreclr_bundle_probe(const char *path, int64_t *offset, int64_t *size, int64_t *compressed_size) +static bool xamarin_coreclr_bundle_probe (const char *path, int64_t *offset, int64_t *size, int64_t *compressed_size) { return false; }