[compiler-rt] b4c840e - [Sanitizers] intercept hexdump on FreeBSD.

David Carlier via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 20 10:00:49 PST 2023


Author: David Carlier
Date: 2023-01-20T18:00:38Z
New Revision: b4c840e70b630532325267a3dd81550569c5f0f9

URL: https://github.com/llvm/llvm-project/commit/b4c840e70b630532325267a3dd81550569c5f0f9
DIFF: https://github.com/llvm/llvm-project/commit/b4c840e70b630532325267a3dd81550569c5f0f9.diff

LOG: [Sanitizers] intercept hexdump on FreeBSD.

Reviewers: vitalybuka
Reviewed-By: vitalybuka

Differential Revision: https://reviews.llvm.org/D110471

Added: 
    compiler-rt/test/sanitizer_common/TestCases/FreeBSD/hexdump.cc

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index e50bc87834115..e999239549ccc 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -10357,6 +10357,20 @@ INTERCEPTOR(int, __xuname, int size, void *utsname) {
 #define INIT___XUNAME
 #endif
 
+#if SANITIZER_INTERCEPT_HEXDUMP
+INTERCEPTOR(void, hexdump, const void *ptr, int length, const char *header, int flags) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, hexdump, ptr, length, header, flags);
+  COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, length);
+  COMMON_INTERCEPTOR_READ_RANGE(ctx, header, internal_strlen(header) + 1);
+  REAL(hexdump)(ptr, length, header, flags);
+}
+
+#define INIT_HEXDUMP COMMON_INTERCEPT_FUNCTION(hexdump);
+#else
+#define INIT_HEXDUMP
+#endif
+
 #include "sanitizer_common_interceptors_netbsd_compat.inc"
 
 static void InitializeCommonInterceptors() {
@@ -10675,6 +10689,7 @@ static void InitializeCommonInterceptors() {
   INIT_PROCCTL
   INIT_UNAME;
   INIT___XUNAME;
+  INIT_HEXDUMP;
 
   INIT___PRINTF_CHK;
 }

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index fca769d37bd7f..814ff462d1cf5 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -591,6 +591,7 @@
 #define SANITIZER_INTERCEPT___XUNAME SI_FREEBSD
 #define SANITIZER_INTERCEPT_FLOPEN SI_FREEBSD
 #define SANITIZER_INTERCEPT_PROCCTL SI_FREEBSD
+#define SANITIZER_INTERCEPT_HEXDUMP SI_FREEBSD
 
 // This macro gives a way for downstream users to override the above
 // interceptor macros irrespective of the platform they are on. They have

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/hexdump.cc b/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/hexdump.cc
new file mode 100644
index 0000000000000..e07650d64102a
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/hexdump.cc
@@ -0,0 +1,23 @@
+// RUN: %clangxx -O0 -g %s -o %t -lutil && %run %t 2>&1 | FileCheck %s
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <libutil.h>
+
+int main(void) {
+  printf("hexdump");
+  char *line;
+  size_t lineno = 0, len;
+  const char *delim = "\\\\#";
+  FILE *fp = fopen("/etc/fstab", "r");
+  assert(fp);
+  line = fparseln(fp, &len, &lineno, delim, 0);
+  hexdump(line, len, nullptr, 0);
+  free(line);
+  fclose(fp);
+  assert(lineno != 0);
+  assert(len > 0);
+
+  return 0;
+}


        


More information about the llvm-commits mailing list