Runtime detect HWASan where appropriate
With unbundled HWASan, libraries can be HWASan-ified even if ART is not.
This means we need to detect HWASan at runtime for the stack cleanup
methods, because we cannot be sure that no stack frames are tagged just
because ART does not use HWASan.
Bug: 276930343
Test: flash hwasan build & check apps work
Change-Id: I39c60f98de35c046bb69ab3bf318eaf5d11d4617
diff --git a/runtime/arch/arm64/context_arm64.cc b/runtime/arch/arm64/context_arm64.cc
index 82dd22b..eca9ed7 100644
--- a/runtime/arch/arm64/context_arm64.cc
+++ b/runtime/arch/arm64/context_arm64.cc
@@ -23,16 +23,13 @@
#include "quick/quick_method_frame_info.h"
#include "thread-current-inl.h"
-#if __has_feature(hwaddress_sanitizer)
-#include <sanitizer/hwasan_interface.h>
-#else
-#define __hwasan_handle_longjmp(sp)
-#endif
#if defined(__aarch64__) && defined(__BIONIC__)
#include <bionic/malloc.h>
#endif
+extern "C" __attribute__((weak)) void __hwasan_handle_longjmp(const void* sp_dst);
+
namespace art {
namespace arm64 {
@@ -171,7 +168,8 @@
untag_memory(__builtin_frame_address(0), reinterpret_cast<void*>(gprs[SP]));
#endif
// Tell HWASan about the new stack top.
- __hwasan_handle_longjmp(reinterpret_cast<void*>(gprs[SP]));
+ if (__hwasan_handle_longjmp != nullptr)
+ __hwasan_handle_longjmp(reinterpret_cast<void*>(gprs[SP]));
// The Marking Register will be updated by art_quick_do_long_jump.
art_quick_do_long_jump(gprs, fprs);
}
diff --git a/runtime/thread.cc b/runtime/thread.cc
index 094da2e..16f5f87 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -23,12 +23,6 @@
#include <sys/resource.h>
#include <sys/time.h>
-#if __has_feature(hwaddress_sanitizer)
-#include <sanitizer/hwasan_interface.h>
-#else
-#define __hwasan_tag_pointer(p, t) (p)
-#endif
-
#include <algorithm>
#include <atomic>
#include <bitset>
@@ -130,6 +124,9 @@
#pragma clang diagnostic push
#pragma clang diagnostic error "-Wconversion"
+extern "C" __attribute__((weak)) void* __hwasan_tag_pointer(const volatile void* p,
+ unsigned char tag);
+
namespace art {
using android::base::StringAppendV;
@@ -815,7 +812,8 @@
volatile char space[kPageSize - (kAsanMultiplier * 256)] __attribute__((uninitialized));
char sink ATTRIBUTE_UNUSED = space[zero]; // NOLINT
// Remove tag from the pointer. Nop in non-hwasan builds.
- uintptr_t addr = reinterpret_cast<uintptr_t>(__hwasan_tag_pointer(space, 0));
+ uintptr_t addr = reinterpret_cast<uintptr_t>(
+ __hwasan_tag_pointer != nullptr ? __hwasan_tag_pointer(space, 0) : space);
if (addr >= target + kPageSize) {
Touch(target);
}