Skip to content

Commit 7009c98

Browse files
authored
Reapply "[sanitizer][asan][win] Intercept _strdup on Windows (llvm#85006)
Reapply "[sanitizer][asan][win] Intercept _strdup on Windows instead of strdup This includes test changes and interface changes that are duplicated in llvm#81677 This reverts commit 03dc87e.
1 parent 2dc9ec4 commit 7009c98

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ int main() {
1515

1616
subscript = -1;
1717
ptr[subscript] = 42;
18-
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
19-
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
20-
// CHECK: {{#0 .* main .*}}intercept_strdup.cpp:[[@LINE-3]]
21-
// CHECK: [[ADDR]] is located 1 bytes before 6-byte region
22-
// CHECK: allocated by thread T0 here:
23-
//
24-
// The first frame is our wrapper normally but will be malloc in the dynamic
25-
// config.
26-
// CHECK: #0 {{.*}} in {{malloc|strdup}}
27-
//
28-
// The local call to _strdup above may be the second or third frame depending
29-
// on whether we're using the dynamic config.
30-
// CHECK: #{{[12]}} {{.*}} in main {{.*}}intercept_strdup.cpp:[[@LINE-21]]
18+
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
19+
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
20+
// CHECK: {{#0 .* main .*}}intercept_strdup.cpp:[[@LINE-3]]
21+
// CHECK: [[ADDR]] is located 1 bytes before 6-byte region
22+
// CHECK: allocated by thread T0 here:
23+
//
24+
// The first frame is our wrapper normally but will be malloc in the dynamic
25+
// config.
26+
// CHECK: #0 {{.*}} in {{malloc|_strdup}}
27+
//
28+
// The local call to _strdup above may be the second or third frame depending
29+
// on whether we're using the dynamic config.
30+
// CHECK: #{{[12]}} {{.*}} in main {{.*}}intercept_strdup.cpp:[[@LINE-21]]
3131
free(ptr);
3232
}

0 commit comments

Comments
 (0)