[PATCH] D68451: [Sanitizers] Porting getrandom/getentropy interceptors to FreeBSD

David CARLIER via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 10 04:34:45 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rG90c8b59cfc6e: [Sanitizers] Porting getrandom/getentropy interceptors to FreeBSD (authored by devnexen).

Changed prior to commit:
  https://reviews.llvm.org/D68451?vs=223170&id=224314#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D68451/new/

https://reviews.llvm.org/D68451

Files:
  compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
  compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
  compiler-rt/test/sanitizer_common/TestCases/Linux/getrandom.cpp
  compiler-rt/test/sanitizer_common/TestCases/Posix/getrandom.c


Index: compiler-rt/test/sanitizer_common/TestCases/Posix/getrandom.c
===================================================================
--- compiler-rt/test/sanitizer_common/TestCases/Posix/getrandom.c
+++ compiler-rt/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);
Index: compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
===================================================================
--- compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ compiler-rt/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
Index: compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
===================================================================
--- compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -9608,6 +9608,21 @@
 #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 @@
   INIT_GETRANDOM;
   INIT_CRYPT;
   INIT_CRYPT_R;
+  INIT_GETENTROPY;
 
   INIT___PRINTF_CHK;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68451.224314.patch
Type: text/x-patch
Size: 2756 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191010/67da02c5/attachment.bin>


More information about the llvm-commits mailing list