[PATCH] D56125: [Sanitizer] Intercept arc4random_buf on FreeBSD/NetBSD

David CARLIER via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 28 05:35:17 PST 2018


devnexen updated this revision to Diff 179630.

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

https://reviews.llvm.org/D56125

Files:
  lib/sanitizer_common/sanitizer_common_interceptors.inc
  lib/sanitizer_common/sanitizer_platform_interceptors.h
  test/sanitizer_common/TestCases/Posix/arc4random.cc


Index: test/sanitizer_common/TestCases/Posix/arc4random.cc
===================================================================
--- /dev/null
+++ test/sanitizer_common/TestCases/Posix/arc4random.cc
@@ -0,0 +1,23 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+//
+// UNSUPPORTED: linux, darwin, solaris
+
+#include <cstdlib>
+#include <ctime>
+#include <cstdio>
+#include <inttypes.h>
+
+int main(void) {
+  printf("arc4random\n");
+  time_t now = ::time(nullptr);
+  arc4random_addrandom((unsigned char *)&now, sizeof(now));
+  char buf[10];
+  arc4random_buf(buf, sizeof(buf));
+  printf("buf '");
+  for (auto i = 0; i < sizeof(buf); i ++)
+    printf("%" PRIx8, buf[i]);
+  printf("'\n");
+  return 0;
+  // CHECK: arc4random
+  // CHECK: buf '{{.*}}'
+}
Index: lib/sanitizer_common/sanitizer_platform_interceptors.h
===================================================================
--- lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -546,5 +546,6 @@
 #define SANITIZER_INTERCEPT_CDB SI_NETBSD
 #define SANITIZER_INTERCEPT_VIS (SI_NETBSD || SI_FREEBSD)
 #define SANITIZER_INTERCEPT_GETFSENT (SI_FREEBSD || SI_NETBSD)
+#define SANITIZER_INTERCEPT_ARC4RANDOM (SI_FREEBSD || SI_NETBSD)
 
 #endif  // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H
Index: lib/sanitizer_common/sanitizer_common_interceptors.inc
===================================================================
--- lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -9084,6 +9084,31 @@
 #else
 #define INIT_GETFSENT
 #endif
+
+#if SANITIZER_INTERCEPT_ARC4RANDOM
+INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
+  REAL(arc4random_buf)(buf, len);
+  if (buf && len)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
+}
+
+INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
+  if (dat && datlen)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
+  REAL(arc4random_addrandom)(dat, datlen);
+}
+
+#define INIT_ARC4RANDOM \
+  COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
+  COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
+#else
+#define INIT_ARC4RANDOM
+#endif
+
 static void InitializeCommonInterceptors() {
   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
   interceptor_metadata_map =
@@ -9365,6 +9390,7 @@
   INIT_VIS;
   INIT_CDB;
   INIT_GETFSENT;
+  INIT_ARC4RANDOM;
 
   INIT___PRINTF_CHK;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56125.179630.patch
Type: text/x-patch
Size: 2628 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181228/df4461c1/attachment.bin>


More information about the llvm-commits mailing list