[compiler-rt] [ASan] Change strdup interceptor to allow null input on Windows (PR #122803)
Zack Johnson via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 14 08:49:06 PST 2025
https://github.com/zacklj89 updated https://github.com/llvm/llvm-project/pull/122803
>From dd3b042e065450f1cfd79910c3705dce6e1d0810 Mon Sep 17 00:00:00 2001
From: Zack Johnson <zacklj89 at gmail.com>
Date: Mon, 13 Jan 2025 13:57:19 -0500
Subject: [PATCH 1/4] Change strdup interceptor to allow null input on Windows
---
compiler-rt/lib/asan/asan_interceptors.cpp | 6 ++++++
.../TestCases/Windows/msvc/strdup_null_input.cpp | 16 ++++++++++++++++
2 files changed, 22 insertions(+)
create mode 100644 compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp
diff --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp
index e6c95dfb82c320..a2dc2bbf54bdb1 100644
--- a/compiler-rt/lib/asan/asan_interceptors.cpp
+++ b/compiler-rt/lib/asan/asan_interceptors.cpp
@@ -584,6 +584,12 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
INTERCEPTOR(char*, strdup, const char *s) {
void *ctx;
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
+#if SANITIZER_WINDOWS
+ // Allowing null input is Windows-specific
+ if (UNLIKELY(!s)) {
+ return nullptr;
+ }
+#endif
if (UNLIKELY(!TryAsanInitFromRtl()))
return internal_strdup(s);
uptr length = internal_strlen(s);
diff --git a/compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp b/compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp
new file mode 100644
index 00000000000000..d84b2f501927ce
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cl_asan %s -Fe%t.exe
+// RUN: %run %t.exe | FileCheck %s
+
+// CHECK: Success
+
+#include <malloc.h>
+#include <stdio.h>
+#include <string.h>
+
+int main() {
+ // Null input is valid to strdup on Windows.
+ char *nullStr = _strdup(nullptr);
+ free(nullStr);
+ puts("Success");
+ return 0;
+}
\ No newline at end of file
>From 087256742f64ad21bd3a4ffdbee03c2be1d44461 Mon Sep 17 00:00:00 2001
From: Zack Johnson <zacklj89 at gmail.com>
Date: Mon, 13 Jan 2025 17:07:55 -0500
Subject: [PATCH 2/4] Formatting fix
---
compiler-rt/lib/asan/asan_interceptors.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp
index a2dc2bbf54bdb1..8411b1e17f211c 100644
--- a/compiler-rt/lib/asan/asan_interceptors.cpp
+++ b/compiler-rt/lib/asan/asan_interceptors.cpp
@@ -584,12 +584,12 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
INTERCEPTOR(char*, strdup, const char *s) {
void *ctx;
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
-#if SANITIZER_WINDOWS
+# if SANITIZER_WINDOWS
// Allowing null input is Windows-specific
if (UNLIKELY(!s)) {
return nullptr;
}
-#endif
+# endif
if (UNLIKELY(!TryAsanInitFromRtl()))
return internal_strdup(s);
uptr length = internal_strlen(s);
>From 132b53e92583d66eb7901c048cf9e4486883d78d Mon Sep 17 00:00:00 2001
From: Zack Johnson <zacklj89 at gmail.com>
Date: Mon, 13 Jan 2025 19:52:40 -0500
Subject: [PATCH 3/4] Single line conditional and remove braces
---
compiler-rt/lib/asan/asan_interceptors.cpp | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp
index 8411b1e17f211c..247ea1b92f1f42 100644
--- a/compiler-rt/lib/asan/asan_interceptors.cpp
+++ b/compiler-rt/lib/asan/asan_interceptors.cpp
@@ -584,12 +584,9 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
INTERCEPTOR(char*, strdup, const char *s) {
void *ctx;
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
-# if SANITIZER_WINDOWS
// Allowing null input is Windows-specific
- if (UNLIKELY(!s)) {
+ if (SANITIZER_WINDOWS && UNLIKELY(!s))
return nullptr;
- }
-# endif
if (UNLIKELY(!TryAsanInitFromRtl()))
return internal_strdup(s);
uptr length = internal_strlen(s);
>From 68cab2d51bd77a8e82f45415c3634723fc3218eb Mon Sep 17 00:00:00 2001
From: Zack Johnson <zacklj89 at gmail.com>
Date: Tue, 14 Jan 2025 11:48:34 -0500
Subject: [PATCH 4/4] Updating test file end
---
.../test/asan/TestCases/Windows/msvc/strdup_null_input.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp b/compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp
index d84b2f501927ce..9fd34fe9a08dd1 100644
--- a/compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp
+++ b/compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp
@@ -13,4 +13,4 @@ int main() {
free(nullStr);
puts("Success");
return 0;
-}
\ No newline at end of file
+}
More information about the llvm-commits
mailing list