Skip to content

Commit

Permalink
Reapply "[sanitizer][asan][win] Intercept _strdup on Windows (#85006)
Browse files Browse the repository at this point in the history
Reapply "[sanitizer][asan][win] Intercept _strdup on Windows instead of
strdup

This includes test changes and interface changes that are duplicated in
#81677

This reverts commit 03dc87e.
  • Loading branch information
barcharcraz authored Mar 13, 2024
1 parent 2dc9ec4 commit 7009c98
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
19 changes: 17 additions & 2 deletions compiler-rt/lib/asan/asan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion compiler-rt/lib/asan/asan_win_dll_thunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 13 additions & 13 deletions compiler-rt/test/asan/TestCases/Windows/intercept_strdup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 7009c98

Please sign in to comment.