Skip to content

Drop libc++ from Android NativeAOT linking#11311

Open
simonrozsival wants to merge 17 commits into
mainfrom
dev/simonrozsival/nativeaot-drop-libcpp
Open

Drop libc++ from Android NativeAOT linking#11311
simonrozsival wants to merge 17 commits into
mainfrom
dev/simonrozsival/nativeaot-drop-libcpp

Conversation

@simonrozsival
Copy link
Copy Markdown
Member

@simonrozsival simonrozsival commented May 8, 2026

Summary

This removes the Android NativeAOT app link-time dependency on libc++.

The branch now:

  • removes the explicit NativeAOT libc++_static.a and libc++abi.a link inputs;
  • removes libc++ packaging from the Android native runtime component list;
  • keeps the final NativeAOT app link on the direct ld.lld/NativeLinker path;
  • reduces NativeAOT-reachable host code that pulled in C++ runtime/STL symbols;
  • adds NativeAOT-local C++ allocation/nothrow shims for the remaining runtime-pack allocation references;
  • shares GC bridge processing between CoreCLR and NativeAOT without tsl::robin_map or other libc++-requiring containers;
  • adds printf-style native logging helpers so call sites can avoid repeated snprintf boilerplate 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 libunwind symbols or switching to shared libc++. Related references:

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.* and monodroidAddReference() / monodroidClearReferences(), while NativeAOT trimmable proxy objects use net.dot.jni.GCUserPeerable and jiAddManagedReference() / jiClearManagedReferences().

Brief history:

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 generated net.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 single net.dot.* shape while NativeAOT is still experimental.

Size impact

Latest measurements on this branch use samples/NativeAOT/NativeAOT.csproj built in Release with _AndroidTypeMapImplementation=trimmable. APK sizes are for the signed APK.

Artifact Parent libc++ baseline No-libc++ build Difference
arm64 APK 1,575,849 B 1,382,336 B -193,513 B (-12.28%)
x64 APK 1,639,459 B 1,439,677 B -199,782 B (-12.19%)
arm64 libNativeAOT.so 3,481,880 B 2,943,232 B -538,648 B (-15.47%)
x64 libNativeAOT.so 3,404,896 B 2,866,728 B -538,168 B (-15.81%)

libNativeAOT.so is the per-ABI native shared library packaged in the APK, for example lib/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 --check
  • make prepare CONFIGURATION=Debug
  • make all CONFIGURATION=Debug progressed through native-mono, native-nativeaot, and native-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-arm64
  • launched samples/NativeAOT on an arm64 emulator and confirmed Application.OnCreate() and MainActivity.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.
  • rebuilt src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj;
  • rebuilt NativeAOT runtime archives for android-arm64 and android-x64;
  • built samples/NativeAOT/NativeAOT.csproj with _AndroidTypeMapImplementation=trimmable for android-arm64 and android-x64;
  • verified generated NativeAOT link response files contain no libc++/libc++abi inputs;
  • verified produced APKs contain no libc++ entries;
  • verified llvm-nm -u reports no undefined C++ runtime-looking symbols in the final libNativeAOT.so outputs;
  • smoke-tested the arm64 APK on an arm64 emulator: libNativeAOT.so loaded without libc++, MainApplication and MainActivity native 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.

@simonrozsival
Copy link
Copy Markdown
Member Author

Emulator validation update

I tested the signed NativeAOT + trimmable typemap sample APK on an arm64 emulator after rebuilding/overlaying the local validation packs.

What works:

  • The arm64 APK installs and launches on emulator-5554.
  • lib/arm64-v8a/libNativeAOT.so loads successfully with no libc++ packaged in the APK.
  • Startup reaches managed code: MainApplication and MainActivity native callbacks run, MainActivity.OnCreate() logs, and the process remains alive.

Important caveats:

  • This arm64 smoke currently requires trimmable NativeAOT startup fixes plus two validation-only workarounds:
    • pointing ILC TypeMapEntryAssembly at the app typemap (_NativeAOT.TypeMap) instead of the root _Microsoft.Android.TypeMaps, because scanning _Mono.Android.TypeMap currently fails on Android.Text.IInputType being present in the ref assembly but absent from the runtime assembly;
    • skipping UncaughtExceptionMarshaler setup for trimmable NativeAOT to avoid the IUncaughtExceptionHandler/JavaProxyThrowable startup path.
  • I do not consider those two workarounds shippable as-is; they need separate root-cause fixes before this can be more than a feasibility proof.
  • x64 builds and packages without libc++ (-nostdlib++ is in the link response and no libc++ entries are in the APK), but I could not run the x64 APK locally: the Android emulator rejects x86_64 system images on this Apple Silicon host (Avd's CPU Architecture 'x86_64' is not supported by the QEMU2 emulator on aarch64 host). x64 runtime behavior still needs validation on an x64 emulator host.

@simonrozsival simonrozsival changed the title [WIP] Explore dropping libc++ from NativeAOT linking [WIP] Drop libc++ from Android NativeAOT linking May 12, 2026
@simonrozsival
Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

Copy link
Copy Markdown
Contributor

Copilot AI commented May 23, 2026

@copilot resolve the merge conflicts in this pull request

Resolved by merging origin/main into this branch and fixing all reported conflicts in commit 6e8b48a.

@simonrozsival
Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

Copy link
Copy Markdown
Contributor

Copilot AI commented May 26, 2026

@copilot resolve the merge conflicts in this pull request

Resolved locally by merging origin/main and fixing the conflict hunks in commit 0199a84. Push from the agent is currently blocked by branch protection (GH013: Cannot update this protected ref), so this commit is ready but not yet on the remote branch.

Copy link
Copy Markdown
Contributor

Copilot AI commented May 26, 2026

@copilot resolve the merge conflicts in this pull request

Done in 1c6205f. I merged latest origin/main into this branch and resolved all merge conflicts in the affected typemap generator/targets/tests files.

simonrozsival and others added 5 commits May 27, 2026 17:35
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>
@simonrozsival simonrozsival force-pushed the dev/simonrozsival/nativeaot-drop-libcpp branch from 1c6205f to 6a4feea Compare May 27, 2026 16:12
@simonrozsival simonrozsival changed the title [WIP] Drop libc++ from Android NativeAOT linking Drop libc++ from Android NativeAOT linking May 27, 2026
@simonrozsival simonrozsival marked this pull request as ready for review May 27, 2026 16:20
Copilot AI review requested due to automatic review settings May 27, 2026 16:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 avoid std::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/lref logging emit to logcat even in the “file-only” configuration, and the logcat_enabled parameter 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);
		}

Comment thread src/native/clr/include/runtime-base/util.hh
Comment thread src/native/clr/include/runtime-base/util.hh
Comment thread src/native/clr/include/runtime-base/util.hh Outdated
simonrozsival and others added 5 commits May 27, 2026 20:58
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>
simonrozsival and others added 7 commits May 28, 2026 07:15
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants