summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/sanitizer_common/sanitizer_common_interceptors.inc16
-rw-r--r--lib/sanitizer_common/sanitizer_platform_interceptors.h3
-rw-r--r--test/sanitizer_common/TestCases/Posix/getrandom.c (renamed from test/sanitizer_common/TestCases/Linux/getrandom.cpp)12
3 files changed, 26 insertions, 5 deletions
diff --git a/lib/sanitizer_common/sanitizer_common_interceptors.inc b/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 7cae94559..50e3558b5 100644
--- a/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -9608,6 +9608,21 @@ INTERCEPTOR(char *, crypt_r, char *key, char *salt, void *data) {
#define INIT_CRYPT_R
#endif
+#if SANITIZER_INTERCEPT_GETENTROPY
+INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {
+ void *ctx;
+ COMMON_INTERCEPTOR_ENTER(ctx, getentropy, buf, buflen);
+ int r = REAL(getentropy)(buf, buflen);
+ if (r == 0) {
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
+ }
+ return r;
+}
+#define INIT_GETENTROPY COMMON_INTERCEPT_FUNCTION(getentropy)
+#else
+#define INIT_GETENTROPY
+#endif
+
static void InitializeCommonInterceptors() {
#if SI_POSIX
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
@@ -9908,6 +9923,7 @@ static void InitializeCommonInterceptors() {
INIT_GETRANDOM;
INIT_CRYPT;
INIT_CRYPT_R;
+ INIT_GETENTROPY;
INIT___PRINTF_CHK;
}
diff --git a/lib/sanitizer_common/sanitizer_platform_interceptors.h b/lib/sanitizer_common/sanitizer_platform_interceptors.h
index 54a1699f5..4aeecf120 100644
--- a/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -569,9 +569,10 @@
#define SANITIZER_INTERCEPT_CRYPT (SI_POSIX && !SI_ANDROID)
#define SANITIZER_INTERCEPT_CRYPT_R (SI_LINUX && !SI_ANDROID)
-#define SANITIZER_INTERCEPT_GETRANDOM (SI_LINUX && __GLIBC_PREREQ(2, 25))
+#define SANITIZER_INTERCEPT_GETRANDOM ((SI_LINUX && __GLIBC_PREREQ(2, 25)) || SI_FREEBSD)
#define SANITIZER_INTERCEPT___CXA_ATEXIT SI_NETBSD
#define SANITIZER_INTERCEPT_ATEXIT SI_NETBSD
#define SANITIZER_INTERCEPT_PTHREAD_ATFORK SI_NETBSD
+#define SANITIZER_INTERCEPT_GETENTROPY SI_FREEBSD
#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H
diff --git a/test/sanitizer_common/TestCases/Linux/getrandom.cpp b/test/sanitizer_common/TestCases/Posix/getrandom.c
index 08337f537..fdc08bc4e 100644
--- a/test/sanitizer_common/TestCases/Linux/getrandom.cpp
+++ b/test/sanitizer_common/TestCases/Posix/getrandom.c
@@ -1,5 +1,5 @@
-// RUN: %clangxx -O2 %s -o %t && %run %t
-// UNSUPPORTED: android
+// RUN: %clang -O2 %s -o %t && %run %t
+// UNSUPPORTED: android netbsd darwin solaris
//
#include <sys/types.h>
@@ -8,14 +8,18 @@
#define __GLIBC_PREREQ(a, b) 0
#endif
-#if __GLIBC_PREREQ(2, 25)
+#if (defined(__linux__) && __GLIBC_PREREQ(2, 25)) || defined(__FreeBSD__)
+#define HAS_GETRANDOM
+#endif
+
+#if defined(HAS_GETRANDOM)
#include <sys/random.h>
#endif
int main() {
char buf[16];
ssize_t n = 1;
-#if __GLIBC_PREREQ(2, 25)
+#if defined(HAS_GETRANDOM)
n = getrandom(buf, sizeof(buf), 0);
#endif
return (int)(n <= 0);