[compiler-rt] [SanitizerCommon] add null check for fopen64 interceptor (PR #68760)

Wu Yingcong via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 10 22:26:41 PDT 2023


https://github.com/yingcong-wu updated https://github.com/llvm/llvm-project/pull/68760

>From 232ee35e9bed4b77ffbef14c067b2ecdf33ed2cb Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Tue, 10 Oct 2023 20:01:23 -0700
Subject: [PATCH 1/6] add null check for fopen64

---
 .../lib/sanitizer_common/sanitizer_common_interceptors.inc      | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 80efaf54a0607f6..4da29d928fcc236 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -6145,7 +6145,7 @@ INTERCEPTOR(int, flopenat, int dirfd, const char *path, int flags, ...) {
 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
-  COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
+  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
   __sanitizer_FILE *res = REAL(fopen64)(path, mode);
   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);

>From 1dce2c91f1479cb4b67ab54ff5993ff539e88c58 Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Tue, 10 Oct 2023 20:14:43 -0700
Subject: [PATCH 2/6] add test

---
 .../test/sanitizer_common/TestCases/fopen64_nullptr.c    | 9 +++++++++
 1 file changed, 9 insertions(+)
 create mode 100644 compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c

diff --git a/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c b/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
new file mode 100644
index 000000000000000..2c260865c80a792
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
@@ -0,0 +1,9 @@
+// Check that fopen64(NULL, "r") is ok.
+// `-m32` and `-D_FILE_OFFSET_BITS=64` will make fopen() call fopen64()
+
+// REQUIRES: asan
+// RUN: %clang -m32 -D_FILE_OFFSET_BITS=64 -O2 %s -o %t && %run %t
+#include <stdio.h>
+const char *fn = NULL;
+FILE *f;
+int main() { f = fopen(fn, "r"); }

>From bbd0845b125678bd3dcbf99425b771bb3c6b7f2b Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Tue, 10 Oct 2023 20:49:18 -0700
Subject: [PATCH 3/6] update test

---
 .../test/sanitizer_common/TestCases/fopen64_nullptr.c       | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c b/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
index 2c260865c80a792..597fe7c8f2e9fc5 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
@@ -1,9 +1,9 @@
 // Check that fopen64(NULL, "r") is ok.
 // `-m32` and `-D_FILE_OFFSET_BITS=64` will make fopen() call fopen64()
 
-// REQUIRES: asan
-// RUN: %clang -m32 -D_FILE_OFFSET_BITS=64 -O2 %s -o %t && %run %t
+// REQUIRES: linux
 #include <stdio.h>
+FILE * fopen64 ( const char * filename, const char * mode );
 const char *fn = NULL;
 FILE *f;
-int main() { f = fopen(fn, "r"); }
+int main() { f = fopen64(fn, "r"); }

>From 0047863deb66b663998e48a1a640198f287917cb Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Tue, 10 Oct 2023 20:49:34 -0700
Subject: [PATCH 4/6] update test

---
 compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c b/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
index 597fe7c8f2e9fc5..b0738716ae971f3 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
@@ -1,5 +1,4 @@
 // Check that fopen64(NULL, "r") is ok.
-// `-m32` and `-D_FILE_OFFSET_BITS=64` will make fopen() call fopen64()
 
 // REQUIRES: linux
 #include <stdio.h>

>From 6368ae7bd51b59963e2298624f998735da660653 Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Tue, 10 Oct 2023 21:03:55 -0700
Subject: [PATCH 5/6] format test

---
 compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c b/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
index b0738716ae971f3..043fbee30393545 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
@@ -2,7 +2,7 @@
 
 // REQUIRES: linux
 #include <stdio.h>
-FILE * fopen64 ( const char * filename, const char * mode );
+FILE *fopen64(const char *filename, const char *mode);
 const char *fn = NULL;
 FILE *f;
 int main() { f = fopen64(fn, "r"); }

>From eb37a4ed8fd3193471b769d2f72ac29a019fa299 Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Tue, 10 Oct 2023 22:26:26 -0700
Subject: [PATCH 6/6] add run to test

---
 compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c b/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
index 043fbee30393545..4bcc8cd210e2ee1 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/fopen64_nullptr.c
@@ -1,6 +1,6 @@
 // Check that fopen64(NULL, "r") is ok.
-
 // REQUIRES: linux
+// RUN: %clang -O2 %s -o %t && %run %t
 #include <stdio.h>
 FILE *fopen64(const char *filename, const char *mode);
 const char *fn = NULL;



More information about the llvm-commits mailing list