Merge "[GWP-ASan] Enable GWP-ASan w/ process sampling ~1%."
diff --git a/libc/bionic/gwp_asan_wrappers.cpp b/libc/bionic/gwp_asan_wrappers.cpp
index 2eb6a7e..03c0f97 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) {