Drop libc++ from Android NativeAOT linking#11311
Conversation
Emulator validation updateI tested the signed NativeAOT + trimmable typemap sample APK on an arm64 emulator after rebuilding/overlaying the local validation packs. What works:
Important caveats:
|
|
@copilot resolve the merge conflicts in this pull request |
Resolved by merging |
|
@copilot resolve the merge conflicts in this pull request |
Resolved locally by merging |
Remove the explicit NativeAOT final-link dependency on libc++/libc++abi and keep the Android NativeAOT link guarded with -nostdlib++. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Centralize gref log message formatting in OSBridge so call sites can pass printf-style arguments directly and formatting is skipped when gref logging is disabled. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep gref file guards at the call sites, allow literal single-argument gref log calls, and centralize printf-style log formatting helpers for CLR native logging. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1c6205f to
6a4feea
Compare
There was a problem hiding this comment.
Pull request overview
This PR (draft/WIP) explores removing the Android NativeAOT link-time dependency on libc++/libc++abi by adjusting MSBuild NativeAOT linking inputs and runtime component packaging, and by refactoring native runtime code paths to avoid pulling in C++ standard library symbols (including introducing minimal C++ allocation/nothrow shims).
Changes:
- Remove explicit libc++/libc++abi static link inputs and runtime component entries for NativeAOT Android builds.
- Add NativeAOT-local C++ runtime shims (operator new/delete +
std::nothrow) and refactor multiple native components to avoidstd::format/other STL usage. - Refactor CLR/native logging and GC bridge processing (including callback plumbing for bridge processing and temporary peer map implementation changes).
Reviewed changes
Copilot reviewed 34 out of 34 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Xamarin.Android.Build.Tasks/Utilities/NativeRuntimeComponents.cs | Drops libc++/libc++abi from the known runtime archive list. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.NativeAOT.targets | Removes libc++/libc++abi from NativeAOT link inputs; updates linker/stdlib commentary. |
| src/native/nativeaot/include/host/host.hh | Adds NativeAOT host header shim include. |
| src/native/nativeaot/include/host/bridge-processing.hh | Adapts NativeAOT bridge-processing to callback-based shared implementation. |
| src/native/nativeaot/host/internal-pinvoke-stubs.cc | Simplifies abort path for unimplemented pinvokes to avoid extra std deps. |
| src/native/nativeaot/host/host.cc | Removes std::format usage from NativeAOT host logging path. |
| src/native/nativeaot/host/cxx-shims.cc | Adds minimal C++ allocation / nothrow shims to avoid libc++ dependency. |
| src/native/nativeaot/host/CMakeLists.txt | Adds cxx-shims.cc to NativeAOT host build. |
| src/native/nativeaot/host/bridge-processing.cc | Removes std::format usage and wires callbacks for NativeAOT bridge processing. |
| src/native/common/include/shared/helpers.hh | Adds Helpers::abort_applicationf formatted abort helper. |
| src/native/common/include/shared/cpp-util.hh | Removes C++ ranges/string allocations from diagnostics helpers and logging. |
| src/native/common/include/runtime-base/timing.hh | Replaces std::format timing log formatting with snprintf. |
| src/native/common/include/runtime-base/timing-internal.hh | Replaces std::format timing/internal warnings with snprintf-based logging. |
| src/native/common/include/runtime-base/strings.hh | Replaces some std-container usage and updates formatting/logging to avoid STL pulls. |
| src/native/common/include/runtime-base/jni-wrappers.hh | Replaces new[]/delete[] usage with malloc + placement-new where needed. |
| src/native/clr/shared/log_functions.cc | Introduces vprintf-style log helpers (log_writev, log_*_fmt). |
| src/native/clr/shared/helpers.cc | Adds implementation of Helpers::abort_applicationf; switches some fatal logging to printf-style. |
| src/native/clr/runtime-base/util.cc | Updates logging calls to new printf-style logging helpers. |
| src/native/clr/runtime-base/logger.cc | Refactors gref/lref log path storage away from std::string; updates open/log messages. |
| src/native/clr/runtime-base/android-system-shared.cc | Replaces some new[]/delete[] allocations and formatting with malloc/snprintf and fmt logging. |
| src/native/clr/include/shared/log_types.hh | Adds printf-style logging APIs and adjusts macro behavior for XA_HOST_NATIVEAOT builds. |
| src/native/clr/include/runtime-base/util.hh | Switches to formatted abort/log helpers; adds mmap failure formatted aborts and info logging. |
| src/native/clr/include/runtime-base/android-system.hh | Changes override-dir storage away from std::string for NativeAOT compatibility; API now returns const char*. |
| src/native/clr/include/host/os-bridge.hh | Adds formatted gref logging overloads and refactors internal logging helpers signatures. |
| src/native/clr/include/host/host-environment.hh | Updates debug/warn logging calls to avoid std::format and skip work when disabled. |
| src/native/clr/include/host/gc-bridge.hh | Replaces std::thread/semaphore with pthread/sem_t for compatibility and reduced STL usage. |
| src/native/clr/include/host/bridge-processing.hh | Removes now-unneeded CoreCLR no-op overrides after shared callback refactor. |
| src/native/clr/include/host/bridge-processing-shared.hh | Introduces callback plumbing and switches temporary peer map to robin_map. |
| src/native/clr/host/os-bridge.cc | Refactors stack-trace/gref logging to avoid std::format and centralize formatted logging. |
| src/native/clr/host/internal-pinvokes-shared.cc | Updates managed->native log forwarding to respect category enablement and use log_write. |
| src/native/clr/host/host-shared.cc | Replaces std::format/string_view literal usage in error logging with printf-style. |
| src/native/clr/host/gc-bridge.cc | Migrates to sem_wait/atomic builtins and updates logging formatting. |
| src/native/clr/host/fastdev-assemblies.cc | Updates override-dir handling to match AndroidSystem API change to const char*. |
| src/native/clr/host/bridge-processing.cc | Implements callback-based bridge processing shared logic and temporary peer lifecycle handling. |
Comments suppressed due to low confidence (1)
src/native/clr/host/os-bridge.cc:203
- OSBridge::log_it() logs the main line to logcat unconditionally via log_write(...), even when logcat_enabled is false. This makes
gref/lreflogging emit to logcat even in the “file-only” configuration, and thelogcat_enabledparameter only affects stack traces. Consider only calling log_write when logcat_enabled is true (and keep file writes independent), so behavior matches Logger::{gref,lref}_to_logcat().
void OSBridge::log_it (LogCategories category, const char *line, FILE *to, const char *const from, bool logcat_enabled) noexcept
{
log_write (category, LogLevel::Info, line);
// We skip logcat here when logging to file is enabled because _write_stack_trace will output to logcat as well, if enabled
if (to == nullptr) {
if (logcat_enabled) {
_write_stack_trace (nullptr, from, category);
}
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add the missing standard and logging declarations needed by the native runtime headers when they are compiled directly by the Android runtime ninja builds. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Avoid ambiguous gref log overload resolution, insert temporary GC bridge peers into robin_map without mutating through the iterator proxy, and include robin_map headers in the NativeAOT host build. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep the robin_map-backed temporary peer lookup for the CLR host, but use a simple indexed JNI handle table for the NativeAOT host so app-linked static runtimes do not pull in libc++/c++abi symbols. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Avoid allocating a temporary peer slot for every GC bridge component in the NativeAOT host. The NativeAOT path now stores only the temporary peers it creates, while the CLR host continues to use robin_map. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove the CoreCLR-only robin_map implementation so bridge processing uses the same sparse temporary peer list for both CoreCLR and NativeAOT. Leave a note about a possible future lookup optimization using the SCC Count field. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Encapsulate temporary peer storage in a dedicated RAII type and encode temporary peer indexes in the SCC count while the bridge owns the GC cross-reference arguments. Share the implementation across CoreCLR and NativeAOT, keeping only the NativeAOT GCUserPeerable callbacks runtime-specific. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Convert native runtime logging macros to use printf-style helpers across CLR, Mono, and NativeAOT. This removes the NativeAOT-only preformatted logging branch while preserving category gating for debug and info logs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Avoid calling GetMethodID with a null jclass when initializing the temporary peer map. This preserves the intended abort message when the runtime field lookup fails. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Explain that these shims intentionally cover only the no-libc++ allocation symbols needed by the NativeAOT runtime pack while native code is built without C++ exceptions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Drop libc++ from NativeAOT linking reduces libUnnamedProject.so by ~625KB. Refresh the BuildReleaseArm64 size-regression reference descriptions from the latest CI build. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@copilot resolve the merge conflicts in this pull request |
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
|
@copilot resolve the merge conflicts in this pull request |
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Resolved in |
Use the negative-index TemporaryPeerMap approach from #11311 so CoreCLR and NativeAOT no longer need robin-map for GC bridge processing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
## Summary Replace the shared CoreCLR/NativeAOT GC bridge's temporary-peer `std::unordered_map` with an allocation-backed `TemporaryPeerMap`. The design follows the approach previously developed in #11311: an empty strongly connected component temporarily carries an encoded peer-array index in `StronglyConnectedComponent.Count`, so the bridge does not need a general-purpose C++ hash table during its scoped cross-reference pass. Split from #12142. The two PRs are independent and can merge in either order. Part of #12139. ## Background During GC bridge processing, each strongly connected component (SCC) must behave like one Java object: - `Count == 1`: the existing Java peer represents the SCC directly; - `Count > 1`: the bridge adds circular references so all peers remain alive or are collected together; - `Count == 0`: there is no Java peer, so the bridge creates a temporary `mono.android.GCUserPeer` solely to represent that SCC while cross-SCC references are established. The previous implementation stored those temporary peers in `std::unordered_map<size_t, jobject>`, keyed by SCC index. The required key set and capacity are already known before processing begins, and lookup is only needed within one short scope, making a hash table unnecessary. ## Implementation Files: - `src/native/clr/include/host/bridge-processing-shared.hh` - `src/native/clr/host/bridge-processing.cc` ### `TemporaryPeerMap` lifetime 1. The constructor scans all SCCs, rejects pre-existing marker values, and counts exactly how many temporary peers are required. 2. If none are required, it performs no allocation. 3. Otherwise it reserves JNI local-reference capacity for all temporary peers plus slack, then allocates one zero-initialized `jobject` array with `calloc`. 4. `add()` creates the temporary `GCUserPeer`, stores it in the next array slot, and writes the encoded slot index into the SCC's `Count` field. 5. Cross-reference target selection detects the encoded marker and retrieves the peer directly from the array. 6. At the end of the scoped cross-reference pass, the destructor deletes every temporary JNI local reference, resets every marked SCC to `Count == 0`, frees the array, and clears its bookkeeping. 7. Normal weak-global-reference processing starts only after the destructor has restored the original SCC shape. ### Index encoding `Count` is unsigned, so the temporary index is stored as `~index`, which has the same bit pattern as `-(index + 1)`: - index zero remains representable; - the high bit acts as the temporary-peer marker; - encoding rejects indexes that already use the marker bit; - decoding verifies the marker and bounds-checks the resulting array index; - the constructor verifies that runtime-provided SCC counts do not already use the reserved marker space. ### JNI initialization and safety - cache the `mono.android.GCUserPeer` class and constructor during runtime initialization; - preserve the existing cached `mono.android.IGCUserPeer` method IDs used for reference callbacks; - reserve local-reference capacity before creating a potentially large temporary-peer set; - clear and log an `EnsureLocalCapacity` failure consistently with the previous implementation; - fail fast on allocation failure, peer-construction failure, invalid markers, capacity overruns, missing peers, and out-of-range indexes; - preserve existing fail-fast handling for Java exceptions raised by `monodroidAddReference` or `monodroidClearReferences`; - explicitly delete copy and move construction/assignment so the owning array and JNI local references cannot be shallow-copied. ## Behavior preserved - temporary peers remain alive until every cross-SCC reference has been added; - temporary local references are released before the Java GC is triggered; - zero-, one-, and multi-peer SCC handling remains unchanged; - cross-reference source/destination selection and `refs_added` bookkeeping remain unchanged; - the runtime receives its SCC array back with all temporary markers removed; - CoreCLR and NativeAOT continue to use the same shared bridge implementation and host-specific peer callback hooks. ## Scope and non-goals - This PR changes only temporary-peer storage; it does not change the GC bridge graph algorithm or collection policy. - It does not change Java peer APIs, reference callback names, or GC trigger behavior. - It does not introduce robin-map or another replacement hash table. - The unrelated logging/path/source-location ownership cleanup remains in #12142. ## Expected impact - remove `std::unordered_map` from shared CoreCLR/NativeAOT temporary-peer processing; - remove the associated `std::__ndk1::__next_prime` and hash-table allocation/code roots; - replace per-node hash-table bookkeeping with one exact-size array allocation; - make temporary JNI reference ownership and SCC marker restoration explicit through RAII; - preserve GC bridge semantics while reducing C++ standard-library reachability. ## Validation The implementation is the isolated GC bridge change previously carried in #12142, plus explicit non-copyable/non-movable ownership semantics for `TemporaryPeerMap`. - `git diff --check` passed; - the ownership hardening is declaration-only and introduces no runtime code; - CI is validating head `d4315727e`.
Summary
This removes the Android NativeAOT app link-time dependency on libc++.
The branch now:
libc++_static.aandlibc++abi.alink inputs;ld.lld/NativeLinkerpath;tsl::robin_mapor other libc++-requiring containers;snprintfboilerplate and skip formatting when logging is disabled.Context
This is related to #9926 and the NDK r29 NativeAOT linking work.
The relevant background is that Android NativeAOT should avoid depending on libc++ instead of working around duplicate
libunwindsymbols or switching to shared libc++. Related references:libunwindsymbol failure and discusses removing the libc++ dependency from Android NativeAOT.GC bridge / Java peer shape history
The GC bridge sharing in this PR exposed one remaining runtime-specific difference: classic Mono/CoreCLR Java peers use
mono.android.*andmonodroidAddReference()/monodroidClearReferences(), while NativeAOT trimmable proxy objects usenet.dot.jni.GCUserPeerableandjiAddManagedReference()/jiClearManagedReferences().Brief history:
mono.android.GCUserPeertemporary-peer model.managedReferencesand exposemonodroidAddReference()/monodroidClearReferences().Type.GetType()? java-interop#1165 and [Java.Interop] AvoidType.GetType()inManagedPeerjava-interop#1168 show why Java.Interop's Java-sideManagedPeer.registerNativeMembers()/ string-based type lookup path was problematic for trimming and NativeAOT.Microsoft.Android.Runtime.NativeAOT.dll#9760 introducedMicrosoft.Android.Runtime.NativeAOT.dlland moved NativeAOT Java runtime pieces from the sample into build-time generated sources.JavaProxyObject.java/JavaProxyThrowable.javaonly for_AndroidTypeMapImplementation=trimmable.JnienvInitializeArgswith GC-user-peer class refs, which is where the NativeAOT bridge path had to account for the trimmableGCUserPeerableshape.ManagedPeernative registration.So the current difference appears to be historical layering rather than a fundamental GC bridge requirement: Mono/CoreCLR kept the older Android-compatible
monodroid*Java peer API, while NativeAOT trimmable support was built around new generatednet.dot.jni.*proxy sources that avoid the legacy Java.Interop native-registration shape. Follow-up issue: #11526 tracks unifying these Java peer reference APIs, likely by moving the trimmable path toward a singlenet.dot.*shape while NativeAOT is still experimental.Size impact
Latest measurements on this branch use
samples/NativeAOT/NativeAOT.csprojbuilt in Release with_AndroidTypeMapImplementation=trimmable. APK sizes are for the signed APK.libNativeAOT.solibNativeAOT.solibNativeAOT.sois the per-ABI native shared library packaged in the APK, for examplelib/arm64-v8a/libNativeAOT.so. It is not the whole Android app package; the APK also contains manifest, resources, Java stubs/classes, signatures, and packaging assets.Validation
Latest local validation:
git diff --checkmake prepare CONFIGURATION=Debugmake all CONFIGURATION=Debugprogressed throughnative-mono,native-nativeaot, andnative-clr; the remaining failure was a stale test restore asset issue unrelated to native code../dotnet-local.sh build src/native/native-nativeaot.csproj -c Debug --no-restore -v minimal./dotnet-local.sh build src/native/native-clr.csproj -c Debug --no-restore -v minimal./dotnet-local.sh build src/native/native-mono.csproj -c Debug --no-restore -v minimal./dotnet-local.sh build samples/NativeAOT/NativeAOT.csproj -c Release -p:_AndroidTypeMapImplementation=trimmable -p:RuntimeIdentifier=android-arm64 -v minimal./dotnet-local.sh build samples/NativeAOT/NativeAOT.csproj -c Release -p:_AndroidTypeMapImplementation=trimmable -p:RuntimeIdentifier=android-x64 -v minimal./dotnet-local.sh build samples/NativeAOT/NativeAOT.csproj -c Debug -t:Install -p:RuntimeIdentifier=android-arm64samples/NativeAOTon an arm64 emulator and confirmedApplication.OnCreate()andMainActivity.OnCreate()ran without fatal logcat entries.Previous branch validation:
dotnet test tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csproj --no-restore— 457 passed.src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj;android-arm64andandroid-x64;samples/NativeAOT/NativeAOT.csprojwith_AndroidTypeMapImplementation=trimmableforandroid-arm64andandroid-x64;libc++/libc++abiinputs;llvm-nm -ureports no undefined C++ runtime-looking symbols in the finallibNativeAOT.sooutputs;libNativeAOT.soloaded without libc++,MainApplicationandMainActivitynative callbacks ran,MainActivity.OnCreate()logged, and the process remained alive.Notes
The x64 APK builds and packages without libc++, but local x64 runtime validation still needs an x64 emulator host because Android emulator x86_64 system images are not accepted on Apple Silicon/aarch64 hosts.