Merge "Disable stack protector test with stack MTE" into main

This commit is contained in:
Treehugger Robot 2024-01-23 09:22:16 +00:00 committed by Gerrit Code Review
commit e8cfc14651

View file

@ -28,6 +28,7 @@
#include <android-base/silent_death_test.h> #include <android-base/silent_death_test.h>
#include "platform/bionic/mte.h"
#include "private/bionic_tls.h" #include "private/bionic_tls.h"
extern "C" pid_t gettid(); // glibc defines this but doesn't declare it anywhere. extern "C" pid_t gettid(); // glibc defines this but doesn't declare it anywhere.
@ -100,16 +101,44 @@ TEST(stack_protector, global_guard) {
#endif #endif
} }
// Make sure that a stack variable (`*p`) is tagged under MTE, by forcing the
// stack safety analysis to fail.
int z;
__attribute__((noinline)) void escape_stack_safety_analysis(int* p) {
*p = z;
}
bool stack_mte_enabled() {
if (!mte_supported()) return false;
int stack_variable;
escape_stack_safety_analysis(&stack_variable);
#if defined(__aarch64__)
return reinterpret_cast<uintptr_t>(&stack_variable) & (0xfull << 56);
#else // !defined(__aarch64__)
return false;
#endif // defined(__aarch64__)
}
bool hwasan_enabled() {
#if __has_feature(hwaddress_sanitizer)
return true;
#else
return false;
#endif // __has_feature(hwaddress_sanitizer)
}
using stack_protector_DeathTest = SilentDeathTest; using stack_protector_DeathTest = SilentDeathTest;
TEST_F(stack_protector_DeathTest, modify_stack_protector) { TEST_F(stack_protector_DeathTest, modify_stack_protector) {
// In another file to prevent inlining, which removes stack protection. // In another file to prevent inlining, which removes stack protection.
extern void modify_stack_protector_test(); extern void modify_stack_protector_test();
#if __has_feature(hwaddress_sanitizer)
ASSERT_EXIT(modify_stack_protector_test(), if (stack_mte_enabled()) {
testing::KilledBySignal(SIGABRT), "tag-mismatch"); GTEST_SKIP() << "Stack MTE is enabled, stack protector is not available";
#else } else if (hwasan_enabled()) {
ASSERT_EXIT(modify_stack_protector_test(), ASSERT_EXIT(modify_stack_protector_test(), testing::KilledBySignal(SIGABRT), "tag-mismatch");
testing::KilledBySignal(SIGABRT), "stack corruption detected"); } else {
#endif ASSERT_EXIT(modify_stack_protector_test(), testing::KilledBySignal(SIGABRT),
"stack corruption detected");
}
} }