Merge "Fix broken return code of M_INITIALIZE_GWP_ASAN." am: b47aa42a11 am: 10878a22ca am: a588c14e26

Original change: https://android-review.googlesource.com/c/platform/bionic/+/1831562

Change-Id: Idb6525e4bf4541c1a897daf15a617fd9abb563e6
This commit is contained in:
Christopher Ferris 2021-09-21 18:14:15 +00:00 committed by Automerger Merge Worker
commit 8ebc26d871
5 changed files with 34 additions and 6 deletions

View file

@ -277,3 +277,14 @@ bool MaybeInitGwpAsan(libc_globals* globals, bool force_init) {
bool DispatchIsGwpAsan(const MallocDispatch* dispatch) {
return dispatch == &gwp_asan_dispatch;
}
bool EnableGwpAsan(bool force_init) {
if (GwpAsanInitialized) {
return true;
}
bool ret_value;
__libc_globals.mutate(
[&](libc_globals* globals) { ret_value = MaybeInitGwpAsan(globals, force_init); });
return ret_value;
}

View file

@ -32,6 +32,9 @@
#include <private/bionic_malloc_dispatch.h>
#include <stddef.h>
// Enable GWP-ASan, used by android_mallopt.
bool EnableGwpAsan(bool force_init);
// Hooks for libc to possibly install GWP-ASan.
bool MaybeInitGwpAsanFromLibc(libc_globals* globals);

View file

@ -330,9 +330,8 @@ extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
errno = EINVAL;
return false;
}
__libc_globals.mutate([&](libc_globals* globals) {
return MaybeInitGwpAsan(globals, *reinterpret_cast<bool*>(arg));
});
return EnableGwpAsan(*reinterpret_cast<bool*>(arg));
}
errno = ENOTSUP;
return false;

View file

@ -530,9 +530,8 @@ extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
errno = EINVAL;
return false;
}
__libc_globals.mutate([&](libc_globals* globals) {
return MaybeInitGwpAsan(globals, *reinterpret_cast<bool*>(arg));
});
return EnableGwpAsan(*reinterpret_cast<bool*>(arg));
}
// Try heapprofd's mallopt, as it handles options not covered here.
return HeapprofdMallopt(opcode, arg, arg_size);

View file

@ -1268,6 +1268,22 @@ TEST(android_mallopt, set_allocation_limit_multiple_threads) {
#endif
}
TEST(android_mallopt, force_init_gwp_asan) {
#if defined(__BIONIC__)
bool force_init = true;
ASSERT_TRUE(android_mallopt(M_INITIALIZE_GWP_ASAN, &force_init, sizeof(force_init)));
// Verify that trying to do the call again also passes no matter the
// value of force_init.
force_init = false;
ASSERT_TRUE(android_mallopt(M_INITIALIZE_GWP_ASAN, &force_init, sizeof(force_init)));
force_init = true;
ASSERT_TRUE(android_mallopt(M_INITIALIZE_GWP_ASAN, &force_init, sizeof(force_init)));
#else
GTEST_SKIP() << "bionic extension";
#endif
}
void TestHeapZeroing(int num_iterations, int (*get_alloc_size)(int iteration)) {
std::vector<void*> allocs;
constexpr int kMaxBytesToCheckZero = 64;