[GWP-ASan] Enable GWP-ASan w/ process sampling ~1%.
This patch enables GWP-ASan with process sampling.
**Note**: If you are visiting this patch because this broke a test or
otherwise is causing failures, please contact mitchp@ directly (or
respond to this patchset). GWP-ASan is designed to cause heap-based
memory safety bugs to manifest in SEGV on a sampled basis.
Bug: 135634846
Test: atest bionic-unit-tests gwp_asan_unittest
Change-Id: I58ca9373def105fdd718cf283482b3220b770698
diff --git a/libc/bionic/gwp_asan_wrappers.cpp b/libc/bionic/gwp_asan_wrappers.cpp
index 1feb871..63673f4 100644
--- a/libc/bionic/gwp_asan_wrappers.cpp
+++ b/libc/bionic/gwp_asan_wrappers.cpp
@@ -27,6 +27,7 @@
*/
#include <platform/bionic/malloc.h>
+#include <private/bionic_arc4random.h>
#include <private/bionic_malloc_dispatch.h>
#include <stddef.h>
#include <stdint.h>
@@ -190,9 +191,15 @@
Malloc(malloc_info),
};
-// TODO(mitchp): Turn on GWP-ASan here probabilistically.
+// The probability (1 / kProcessSampleRate) that a process will be ranodmly
+// selected for sampling. kProcessSampleRate should always be a power of two to
+// avoid modulo bias.
+static constexpr uint8_t kProcessSampleRate = 128;
+
bool ShouldGwpAsanSampleProcess() {
- return false;
+ uint8_t random_number;
+ __libc_safe_arc4random_buf(&random_number, sizeof(random_number));
+ return random_number % kProcessSampleRate == 0;
}
bool MaybeInitGwpAsanFromLibc(libc_globals* globals) {