[llvm] [BOLT][AArch64][Android] Recover from dump failure instead of exit (PR #221292)
YongKang Zhu via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 6 15:38:28 PDT 2026
https://github.com/yozhu updated https://github.com/llvm/llvm-project/pull/221292
>From 15b615bde863e9703c0197299d65fa4e938a269f Mon Sep 17 00:00:00 2001
From: YongKang Zhu <yongzhu at fb.com>
Date: Fri, 4 Sep 2026 09:57:48 -0700
Subject: [PATCH] [BOLT][runtime][Android] Recover from dump failure instead of
exit
---
bolt/runtime/common.h | 41 +++++++++++++++++++++++++++++------
bolt/runtime/instr.cpp | 37 ++++++++++++++++++++++++++------
bolt/runtime/sys_aarch64.h | 44 ++++++++++++++++++++++++++++++++++++++
3 files changed, 109 insertions(+), 13 deletions(-)
diff --git a/bolt/runtime/common.h b/bolt/runtime/common.h
index 96e269e1bb79b..2bcd260b51a80 100644
--- a/bolt/runtime/common.h
+++ b/bolt/runtime/common.h
@@ -153,6 +153,9 @@ struct timespec {
};
#if defined(__aarch64__) || defined(__arm64__)
+#if defined(__ANDROID__)
+#define ANDROID_AARCH64
+#endif
#include "sys_aarch64.h"
#elif defined(__riscv)
#include "sys_riscv64.h"
@@ -233,7 +236,7 @@ void *strStr(const char *const Haystack, const char *const Needle) {
}
void reportNumber(const char *Msg, uint64_t Num, uint32_t Base) {
-#if !defined(__ANDROID__)
+#if !defined(ANDROID_AARCH64)
char Buf[BufSize];
char *Ptr = Buf;
Ptr = strCopy(Ptr, Msg, BufSize - 23);
@@ -244,7 +247,7 @@ void reportNumber(const char *Msg, uint64_t Num, uint32_t Base) {
}
void report(const char *Msg) {
-#if !defined(__ANDROID__)
+#if !defined(ANDROID_AARCH64)
__write(2, Msg, strLen(Msg));
#endif
}
@@ -284,18 +287,44 @@ static bool scanUInt32(const char *&Buf, const char *End, uint32_t &Ret) {
return false;
}
+#if defined(ANDROID_AARCH64)
+// On Android the BOLT instrumentation runtime is embedded in a shipping app
+// process. Any failure in runtime should not terminate the host process with
+// __exit(1), which would turn a profiling-tool defect into a foreground app
+// crash. Instead, __bolt_instr_data_dump() installs a setjmp() recovery point
+// and any failed assert() or reportError() will record the failure and then
+// longjmp() back reporting failure to the caller.
+alignas(16) void *__bolt_instr_longjmp_buf[16];
+bool __bolt_instr_recovery_active = false;
+bool __bolt_instr_dump_failed = false;
+
+void boltHandleFatalAndRecover() {
+ __atomic_store_n(&__bolt_instr_dump_failed, true, __ATOMIC_RELAXED);
+ if (__bolt_instr_recovery_active) {
+ __bolt_instr_recovery_active = false;
+ __bolt_longjmp(__bolt_instr_longjmp_buf, 1);
+ }
+ __exit(1);
+}
+#endif // defined(ANDROID_AARCH64)
+
void reportError(const char *Msg, uint64_t Size) {
-#if !defined(__ANDROID__)
+#if defined(ANDROID_AARCH64)
+ (void)Msg;
+ (void)Size;
+ boltHandleFatalAndRecover();
+#else
__write(2, Msg, Size);
-#endif
__exit(1);
+#endif
}
void assert(bool Assertion, const char *Msg) {
if (Assertion)
return;
-#if defined(__ANDROID__)
- __exit(1);
+#if defined(ANDROID_AARCH64)
+ (void)Msg;
+ boltHandleFatalAndRecover();
#else
char Buf[BufSize];
char *Ptr = Buf;
diff --git a/bolt/runtime/instr.cpp b/bolt/runtime/instr.cpp
index cfd49a0158273..9f651081ca215 100644
--- a/bolt/runtime/instr.cpp
+++ b/bolt/runtime/instr.cpp
@@ -137,7 +137,7 @@ class BumpPtrAllocator {
};
public:
-#if defined(__ANDROID__)
+#if defined(ANDROID_AARCH64)
__attribute__((noinline))
#endif
void *allocate(size_t Size) {
@@ -167,7 +167,7 @@ class BumpPtrAllocator {
/// bugs by checking magic bytes. Ordinarily, we reset the allocator once
/// we are done with it. Reset is done with clear(). There's no need
/// to deallocate each element individually.
-#if defined(__ANDROID__)
+#if defined(ANDROID_AARCH64)
__attribute__((noinline))
#endif
void deallocate(void *Ptr) {
@@ -271,7 +271,7 @@ struct SimpleHashTableEntryBase {
uint64_t Key;
uint64_t Val;
void dump(const char *Msg = nullptr) {
-#if !defined(__ANDROID__)
+#if !defined(ANDROID_AARCH64)
// TODO: make some sort of formatting function
// Currently we have to do it the ugly way because
// we want every message to be printed atomically via a single call to
@@ -615,7 +615,7 @@ int compareStr(const char *Str1, const char *Str2, int Size) {
}
/// Output Location to the fdata file
-#if defined(__ANDROID__)
+#if defined(ANDROID_AARCH64)
__attribute__((noinline))
#endif
char *serializeLoc(const ProfileWriterContext &Ctx, char *OutBuf,
@@ -841,7 +841,7 @@ ProfileWriterContext readDescriptions() {
#if !defined(__APPLE__)
/// Debug by printing overall metadata global numbers to check it is sane
void printStats(const ProfileWriterContext &Ctx) {
-#if !defined(__ANDROID__)
+#if !defined(ANDROID_AARCH64)
char StatMsg[BufSize];
char *StatPtr = StatMsg;
StatPtr =
@@ -1565,15 +1565,34 @@ extern "C" void __bolt_instr_clear_counters() {
/// to get a pointer to this function and call through the acquired
/// function pointer to dump profile data.
///
-extern "C" void ALIGN_ARG_POINTER __bolt_instr_data_dump(
+/// Returns 0 on success, non-zero if the dump failed.
+extern "C" int ALIGN_ARG_POINTER __bolt_instr_data_dump(
int FD, const char *LibPath = nullptr, const uint8_t *LibContents = nullptr,
uint64_t LibSize = 0) {
+#if defined(ANDROID_AARCH64)
+ // A prior dump already failed. No recovery is attempted so don't retry.
+ if (__bolt_instr_dump_failed)
+ return 1;
+#endif
+
if (LibPath)
strCopy(TargetPath, LibPath, NameMax);
// Already dumping
if (!GlobalWriteProfileMutex->acquire())
- return;
+ return 0;
+
+#if defined(ANDROID_AARCH64)
+ // Install a recovery point for failed assert() or reportError() instead
+ // of exiting the host app. We land here with the write mutex held; release
+ // it and report failure.
+ if (__bolt_setjmp(__bolt_instr_longjmp_buf)) {
+ __bolt_instr_recovery_active = false;
+ GlobalWriteProfileMutex->release();
+ return 1;
+ }
+ __bolt_instr_recovery_active = true;
+#endif
int ret = __lseek(FD, 0, SEEK_SET);
assert(ret == 0, "Failed to lseek!");
@@ -1606,8 +1625,12 @@ extern "C" void ALIGN_ARG_POINTER __bolt_instr_data_dump(
__close(Ctx.FileDesc);
}
HashAlloc.destroy();
+#if defined(ANDROID_AARCH64)
+ __bolt_instr_recovery_active = false;
+#endif
GlobalWriteProfileMutex->release();
DEBUG(report("Finished writing profile.\n"));
+ return 0;
}
/// Event loop for our child process spawned during setup to dump profile data
diff --git a/bolt/runtime/sys_aarch64.h b/bolt/runtime/sys_aarch64.h
index 9cb8e022f58df..5a10abec2a080 100644
--- a/bolt/runtime/sys_aarch64.h
+++ b/bolt/runtime/sys_aarch64.h
@@ -391,6 +391,50 @@ int __prctl(int option, unsigned long arg2, unsigned long arg3,
return ret;
}
+#if defined(__ANDROID__)
+// Clang does not support __builtin_setjmp/__builtin_longjmp for the aarch64
+// Android target, and this freestanding runtime cannot use libc <setjmp.h>.
+//
+// Minimal AAPCS64 setjmp/longjmp: save callee-saved x19-x28, fp(x29), lr(x30),
+// and sp. Built with -mgeneral-regs-only, so there are no callee-saved FP/SIMD
+// (d8-d15) to save. Buffer (8-byte slots): [0..9]=x19..x28, [10]=fp, [11]=lr,
+// [12]=sp - long jump buffer must hold at least 13 doublewords.
+__attribute__((naked, returns_twice)) int __bolt_setjmp(void ** /*buf in x0*/) {
+ // clang-format off
+ __asm__ __volatile__(
+ "stp x19, x20, [x0, #0]\n"
+ "stp x21, x22, [x0, #16]\n"
+ "stp x23, x24, [x0, #32]\n"
+ "stp x25, x26, [x0, #48]\n"
+ "stp x27, x28, [x0, #64]\n"
+ "stp x29, x30, [x0, #80]\n"
+ "mov x2, sp\n"
+ "str x2, [x0, #96]\n"
+ "mov x0, #0\n" // direct call returns 0
+ "ret\n" :::);
+ // clang-format on
+}
+
+__attribute__((naked, noreturn)) void __bolt_longjmp(void ** /*buf in x0*/,
+ int /*val in x1*/) {
+ // clang-format off
+ __asm__ __volatile__(
+ "ldp x19, x20, [x0, #0]\n"
+ "ldp x21, x22, [x0, #16]\n"
+ "ldp x23, x24, [x0, #32]\n"
+ "ldp x25, x26, [x0, #48]\n"
+ "ldp x27, x28, [x0, #64]\n"
+ "ldp x29, x30, [x0, #80]\n"
+ "ldr x2, [x0, #96]\n"
+ "mov sp, x2\n"
+ "cmp w1, #0\n"
+ "csinc w0, w1, wzr, ne\n" // setjmp returns (val ? val : 1)
+ "ret\n" // ret -> saved lr == setjmp's caller
+ :::);
+ // clang-format on
+}
+#endif // defined(__ANDROID__)
+
} // anonymous namespace
#endif
More information about the llvm-commits
mailing list