Skip to content

Commit e87d63d

Browse files
committed
Reapply "[sanitizer][asan][win] Intercept _strdup on Windows instead of strdup"
This reverts commit 03dc87e.
1 parent dcd9f49 commit e87d63d

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

compiler-rt/lib/asan/asan_interceptors.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,17 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
570570
return REAL(strcpy)(to, from);
571571
}
572572

573+
// Windows doesn't always define the strdup identifier,
574+
// and when it does it's a macro defined to either _strdup
575+
// or _strdup_dbg, _strdup_dbg ends up calling _strdup, so
576+
// we want to intercept that. push/pop_macro are used to avoid problems
577+
// if this file ends up including <string.h> in the future.
578+
# if SANITIZER_WINDOWS
579+
# pragma push_macro("strdup")
580+
# undef strdup
581+
# define strdup _strdup
582+
# endif
583+
573584
INTERCEPTOR(char*, strdup, const char *s) {
574585
void *ctx;
575586
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
@@ -587,7 +598,7 @@ INTERCEPTOR(char*, strdup, const char *s) {
587598
return reinterpret_cast<char*>(new_mem);
588599
}
589600

590-
#if ASAN_INTERCEPT___STRDUP
601+
# if ASAN_INTERCEPT___STRDUP
591602
INTERCEPTOR(char*, __strdup, const char *s) {
592603
void *ctx;
593604
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
@@ -770,7 +781,7 @@ void InitializeAsanInterceptors() {
770781
ASAN_INTERCEPT_FUNC(strncat);
771782
ASAN_INTERCEPT_FUNC(strncpy);
772783
ASAN_INTERCEPT_FUNC(strdup);
773-
#if ASAN_INTERCEPT___STRDUP
784+
# if ASAN_INTERCEPT___STRDUP
774785
ASAN_INTERCEPT_FUNC(__strdup);
775786
#endif
776787
#if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
@@ -866,6 +877,10 @@ void InitializeAsanInterceptors() {
866877
VReport(1, "AddressSanitizer: libc interceptors initialized\n");
867878
}
868879

880+
# if SANITIZER_WINDOWS
881+
# pragma pop_macro("strdup")
882+
# endif
883+
869884
} // namespace __asan
870885

871886
#endif // !SANITIZER_FUCHSIA

compiler-rt/lib/asan/asan_win_dll_thunk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ INTERCEPT_LIBRARY_FUNCTION(strchr);
8080
INTERCEPT_LIBRARY_FUNCTION(strcmp);
8181
INTERCEPT_LIBRARY_FUNCTION(strcpy);
8282
INTERCEPT_LIBRARY_FUNCTION(strcspn);
83-
INTERCEPT_LIBRARY_FUNCTION(strdup);
83+
INTERCEPT_LIBRARY_FUNCTION(_strdup);
8484
INTERCEPT_LIBRARY_FUNCTION(strlen);
8585
INTERCEPT_LIBRARY_FUNCTION(strncat);
8686
INTERCEPT_LIBRARY_FUNCTION(strncmp);

compiler-rt/test/asan/TestCases/Windows/intercept_strdup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int main() {
2323
//
2424
// The first frame is our wrapper normally but will be malloc in the dynamic
2525
// config.
26-
// CHECK: #0 {{.*}} in {{malloc|strdup}}
26+
// CHECK: #0 {{.*}} in {{malloc|_strdup}}
2727
//
2828
// The local call to _strdup above may be the second or third frame depending
2929
// on whether we're using the dynamic config.

0 commit comments

Comments
 (0)