Skip to content

Commit 3126321

Browse files
Charlie BartoCaseyCarter
authored andcommitted
[sanitizer][asan][win] Intercept _strdup on Windows instead of strdup
Intercept `_strdup` on windows, instead of the nonexistent `strdup`.
1 parent 0d65307 commit 3126321

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

compiler-rt/lib/asan/asan_interceptors.cpp

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

542+
// Windows doesn't always define the strdup identifier,
543+
// and when it does it's a macro defined to either _strdup
544+
// or _strdup_dbg, _strdup_dbg ends up calling _strdup, so
545+
// we want to intercept that. push/pop_macro are used to avoid problems
546+
// if this file ends up including <string.h> in the future.
547+
# if SANITIZER_WINDOWS
548+
# pragma push_macro("strdup")
549+
# undef strdup
550+
# define strdup _strdup
551+
# endif
552+
542553
INTERCEPTOR(char*, strdup, const char *s) {
543554
void *ctx;
544555
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
@@ -556,7 +567,7 @@ INTERCEPTOR(char*, strdup, const char *s) {
556567
return reinterpret_cast<char*>(new_mem);
557568
}
558569

559-
#if ASAN_INTERCEPT___STRDUP
570+
# if ASAN_INTERCEPT___STRDUP
560571
INTERCEPTOR(char*, __strdup, const char *s) {
561572
void *ctx;
562573
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
@@ -742,7 +753,7 @@ void InitializeAsanInterceptors() {
742753
ASAN_INTERCEPT_FUNC(strncat);
743754
ASAN_INTERCEPT_FUNC(strncpy);
744755
ASAN_INTERCEPT_FUNC(strdup);
745-
#if ASAN_INTERCEPT___STRDUP
756+
# if ASAN_INTERCEPT___STRDUP
746757
ASAN_INTERCEPT_FUNC(__strdup);
747758
#endif
748759
#if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
@@ -831,6 +842,10 @@ void InitializeAsanInterceptors() {
831842
VReport(1, "AddressSanitizer: libc interceptors initialized\n");
832843
}
833844

845+
# if SANITIZER_WINDOWS
846+
# pragma pop_macro("strdup")
847+
# endif
848+
834849
} // namespace __asan
835850

836851
#endif // !SANITIZER_FUCHSIA

0 commit comments

Comments
 (0)