Skip to content

Commit 90c8b59

Browse files
committed
[Sanitizers] Porting getrandom/getentropy interceptors to FreeBSD
- Available from 12.x branch, by the time it lands next year in FreeBSD tree, the 11.x's might be EOL. - Intentionally changed the getrandom test to C code as with 12.0 (might be fixed in CURRENT since), there is a linkage issue in C++ context. Reviewers: emaste, dim, vitalybuka Reviewed-By: vitalybuka Differential Revision: https://reviews.llvm.org/D68451 llvm-svn: 374315
1 parent d79c3be commit 90c8b59

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9608,6 +9608,21 @@ INTERCEPTOR(char *, crypt_r, char *key, char *salt, void *data) {
96089608
#define INIT_CRYPT_R
96099609
#endif
96109610

9611+
#if SANITIZER_INTERCEPT_GETENTROPY
9612+
INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {
9613+
void *ctx;
9614+
COMMON_INTERCEPTOR_ENTER(ctx, getentropy, buf, buflen);
9615+
int r = REAL(getentropy)(buf, buflen);
9616+
if (r == 0) {
9617+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
9618+
}
9619+
return r;
9620+
}
9621+
#define INIT_GETENTROPY COMMON_INTERCEPT_FUNCTION(getentropy)
9622+
#else
9623+
#define INIT_GETENTROPY
9624+
#endif
9625+
96119626
static void InitializeCommonInterceptors() {
96129627
#if SI_POSIX
96139628
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
@@ -9908,6 +9923,7 @@ static void InitializeCommonInterceptors() {
99089923
INIT_GETRANDOM;
99099924
INIT_CRYPT;
99109925
INIT_CRYPT_R;
9926+
INIT_GETENTROPY;
99119927

99129928
INIT___PRINTF_CHK;
99139929
}

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,10 @@
569569
#define SANITIZER_INTERCEPT_CRYPT (SI_POSIX && !SI_ANDROID)
570570
#define SANITIZER_INTERCEPT_CRYPT_R (SI_LINUX && !SI_ANDROID)
571571

572-
#define SANITIZER_INTERCEPT_GETRANDOM (SI_LINUX && __GLIBC_PREREQ(2, 25))
572+
#define SANITIZER_INTERCEPT_GETRANDOM ((SI_LINUX && __GLIBC_PREREQ(2, 25)) || SI_FREEBSD)
573573
#define SANITIZER_INTERCEPT___CXA_ATEXIT SI_NETBSD
574574
#define SANITIZER_INTERCEPT_ATEXIT SI_NETBSD
575575
#define SANITIZER_INTERCEPT_PTHREAD_ATFORK SI_NETBSD
576+
#define SANITIZER_INTERCEPT_GETENTROPY SI_FREEBSD
576577

577578
#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

compiler-rt/test/sanitizer_common/TestCases/Linux/getrandom.cpp renamed to compiler-rt/test/sanitizer_common/TestCases/Posix/getrandom.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clangxx -O2 %s -o %t && %run %t
2-
// UNSUPPORTED: android
1+
// RUN: %clang -O2 %s -o %t && %run %t
2+
// UNSUPPORTED: android netbsd darwin solaris
33
//
44

55
#include <sys/types.h>
@@ -8,14 +8,18 @@
88
#define __GLIBC_PREREQ(a, b) 0
99
#endif
1010

11-
#if __GLIBC_PREREQ(2, 25)
11+
#if (defined(__linux__) && __GLIBC_PREREQ(2, 25)) || defined(__FreeBSD__)
12+
#define HAS_GETRANDOM
13+
#endif
14+
15+
#if defined(HAS_GETRANDOM)
1216
#include <sys/random.h>
1317
#endif
1418

1519
int main() {
1620
char buf[16];
1721
ssize_t n = 1;
18-
#if __GLIBC_PREREQ(2, 25)
22+
#if defined(HAS_GETRANDOM)
1923
n = getrandom(buf, sizeof(buf), 0);
2024
#endif
2125
return (int)(n <= 0);

0 commit comments

Comments
 (0)