[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
This commit is contained in:
David Carlier 2019-10-10 11:31:37 +00:00
parent d79c3be618
commit 90c8b59cfc
3 changed files with 26 additions and 5 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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);