[compiler-rt] 7009c98 - Reapply "[sanitizer][asan][win] Intercept _strdup on Windows (#85006)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 13 16:11:25 PDT 2024


Author: Charlie Barto
Date: 2024-03-13T16:11:21-07:00
New Revision: 7009c981ecd99756ed08bcad47c4fa595b5e2426

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

LOG: Reapply "[sanitizer][asan][win] Intercept _strdup on Windows (#85006)

Reapply "[sanitizer][asan][win] Intercept _strdup on Windows instead of
strdup

This includes test changes and interface changes that are duplicated in
https://github.com/llvm/llvm-project/pull/81677

This reverts commit 03dc87e93937bf25a48bc77d0006c59f37b1855d.

Added: 
    

Modified: 
    compiler-rt/lib/asan/asan_interceptors.cpp
    compiler-rt/lib/asan/asan_win_dll_thunk.cpp
    compiler-rt/test/asan/TestCases/Windows/intercept_strdup.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp
index 9d383b9d37c4a1..da47317c0e5764 100644
--- a/compiler-rt/lib/asan/asan_interceptors.cpp
+++ b/compiler-rt/lib/asan/asan_interceptors.cpp
@@ -570,6 +570,17 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
   return REAL(strcpy)(to, from);
 }
 
+// Windows doesn't always define the strdup identifier,
+// and when it does it's a macro defined to either _strdup
+// or _strdup_dbg, _strdup_dbg ends up calling _strdup, so
+// we want to intercept that. push/pop_macro are used to avoid problems
+// if this file ends up including <string.h> in the future.
+#  if SANITIZER_WINDOWS
+#    pragma push_macro("strdup")
+#    undef strdup
+#    define strdup _strdup
+#  endif
+
 INTERCEPTOR(char*, strdup, const char *s) {
   void *ctx;
   ASAN_INTERCEPTOR_ENTER(ctx, strdup);
@@ -587,7 +598,7 @@ INTERCEPTOR(char*, strdup, const char *s) {
   return reinterpret_cast<char*>(new_mem);
 }
 
-#if ASAN_INTERCEPT___STRDUP
+#  if ASAN_INTERCEPT___STRDUP
 INTERCEPTOR(char*, __strdup, const char *s) {
   void *ctx;
   ASAN_INTERCEPTOR_ENTER(ctx, strdup);
@@ -770,7 +781,7 @@ void InitializeAsanInterceptors() {
   ASAN_INTERCEPT_FUNC(strncat);
   ASAN_INTERCEPT_FUNC(strncpy);
   ASAN_INTERCEPT_FUNC(strdup);
-#if ASAN_INTERCEPT___STRDUP
+#  if ASAN_INTERCEPT___STRDUP
   ASAN_INTERCEPT_FUNC(__strdup);
 #endif
 #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
@@ -866,6 +877,10 @@ void InitializeAsanInterceptors() {
   VReport(1, "AddressSanitizer: libc interceptors initialized\n");
 }
 
+#  if SANITIZER_WINDOWS
+#    pragma pop_macro("strdup")
+#  endif
+
 } // namespace __asan
 
 #endif  // !SANITIZER_FUCHSIA

diff  --git a/compiler-rt/lib/asan/asan_win_dll_thunk.cpp b/compiler-rt/lib/asan/asan_win_dll_thunk.cpp
index 0fa636bec0d001..35871a942a7a12 100644
--- a/compiler-rt/lib/asan/asan_win_dll_thunk.cpp
+++ b/compiler-rt/lib/asan/asan_win_dll_thunk.cpp
@@ -80,7 +80,7 @@ INTERCEPT_LIBRARY_FUNCTION(strchr);
 INTERCEPT_LIBRARY_FUNCTION(strcmp);
 INTERCEPT_LIBRARY_FUNCTION(strcpy);
 INTERCEPT_LIBRARY_FUNCTION(strcspn);
-INTERCEPT_LIBRARY_FUNCTION(strdup);
+INTERCEPT_LIBRARY_FUNCTION(_strdup);
 INTERCEPT_LIBRARY_FUNCTION(strlen);
 INTERCEPT_LIBRARY_FUNCTION(strncat);
 INTERCEPT_LIBRARY_FUNCTION(strncmp);

diff  --git a/compiler-rt/test/asan/TestCases/Windows/intercept_strdup.cpp b/compiler-rt/test/asan/TestCases/Windows/intercept_strdup.cpp
index b519f53fe42698..fec4d1ec35597c 100644
--- a/compiler-rt/test/asan/TestCases/Windows/intercept_strdup.cpp
+++ b/compiler-rt/test/asan/TestCases/Windows/intercept_strdup.cpp
@@ -15,18 +15,18 @@ int main() {
 
   subscript = -1;
   ptr[subscript] = 42;
-// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
-// CHECK: WRITE of size 1 at [[ADDR]] thread T0
-// CHECK:   {{#0 .* main .*}}intercept_strdup.cpp:[[@LINE-3]]
-// CHECK: [[ADDR]] is located 1 bytes before 6-byte region
-// CHECK: allocated by thread T0 here:
-//
-// The first frame is our wrapper normally but will be malloc in the dynamic
-// config.
-// CHECK:   #0 {{.*}} in {{malloc|strdup}}
-//
-// The local call to _strdup above may be the second or third frame depending
-// on whether we're using the dynamic config.
-// CHECK:   #{{[12]}} {{.*}} in main {{.*}}intercept_strdup.cpp:[[@LINE-21]]
+  // CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+  // CHECK: WRITE of size 1 at [[ADDR]] thread T0
+  // CHECK:   {{#0 .* main .*}}intercept_strdup.cpp:[[@LINE-3]]
+  // CHECK: [[ADDR]] is located 1 bytes before 6-byte region
+  // CHECK: allocated by thread T0 here:
+  //
+  // The first frame is our wrapper normally but will be malloc in the dynamic
+  // config.
+  // CHECK:   #0 {{.*}} in {{malloc|_strdup}}
+  //
+  // The local call to _strdup above may be the second or third frame depending
+  // on whether we're using the dynamic config.
+  // CHECK:   #{{[12]}} {{.*}} in main {{.*}}intercept_strdup.cpp:[[@LINE-21]]
   free(ptr);
 }


        


More information about the llvm-commits mailing list