Skip to content

Commit ac85372

Browse files
committed
[compiler-rt][asan] Enable wchar checks: wcscpy/wcsncpy; enable wcscat/wcsncat on Windows
Summary: - Add ASan interceptors for wcscpy/wcsncpy and register them. - Enable wcscat/wcsncat on Windows via platform interceptor macro. - Add MaybeRealWcsnlen guarded by SANITIZER_INTERCEPT_WCSLEN. - Update Windows static thunk to forward wchar functions. - Add tests for wcscpy/wcsncpy/wcscat/wcsncat with standard RUN lines. Prior work and attribution: - Based on and extends PR #90909 (author: branh, Microsoft). Thanks for the prior work and review feedback. #90909 Testing: - AArch64 Linux: 4 new tests pass; full check-asan passed (0 failures) after enabling profile runtime. Context: - Related research background: Tech-ASan (Two-stage check for AddressSanitizer): https://arxiv.org/abs/2506.05022
1 parent 13daa1e commit ac85372

File tree

8 files changed

+106
-1
lines changed

8 files changed

+106
-1
lines changed

compiler-rt/lib/asan/asan_interceptors.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
6565
return internal_strnlen(s, maxlen);
6666
}
6767

68+
static inline uptr MaybeRealWcsnlen(const wchar_t *s, uptr maxlen) {
69+
#if SANITIZER_INTERCEPT_STRNLEN
70+
if (REAL(wcsnlen)) {
71+
return REAL(wcsnlen)(s, maxlen);
72+
}
73+
#endif
74+
return internal_wcsnlen(s, maxlen);
75+
}
76+
6877
void SetThreadName(const char *name) {
6978
AsanThread *t = GetCurrentThread();
7079
if (t)
@@ -570,6 +579,26 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
570579
return REAL(strcpy)(to, from);
571580
}
572581

582+
INTERCEPTOR(wchar_t *, wcscpy, wchar_t *to, const wchar_t *from) {
583+
void *ctx;
584+
ASAN_INTERCEPTOR_ENTER(ctx, wcscpy);
585+
if constexpr (SANITIZER_APPLE) {
586+
if (UNLIKELY(!AsanInited()))
587+
return REAL(wcscpy)(to, from);
588+
} else {
589+
if (!TryAsanInitFromRtl())
590+
return REAL(wcscpy)(to, from);
591+
}
592+
593+
if (flags()->replace_str) {
594+
uptr from_size = (internal_wcslen(from) + 1) * sizeof(wchar_t);
595+
CHECK_RANGES_OVERLAP("wcscpy", to, from_size, from, from_size);
596+
ASAN_READ_RANGE(ctx, from, from_size);
597+
ASAN_WRITE_RANGE(ctx, to, from_size);
598+
}
599+
return REAL(wcscpy)(to, from);
600+
}
601+
573602
// Windows doesn't always define the strdup identifier,
574603
// and when it does it's a macro defined to either _strdup
575604
// or _strdup_dbg, _strdup_dbg ends up calling _strdup, so
@@ -633,6 +662,19 @@ INTERCEPTOR(char*, strncpy, char *to, const char *from, usize size) {
633662
return REAL(strncpy)(to, from, size);
634663
}
635664

665+
INTERCEPTOR(wchar_t *, wcsncpy, wchar_t *to, const wchar_t *from, usize size) {
666+
void *ctx;
667+
ASAN_INTERCEPTOR_ENTER(ctx, wcsncpy);
668+
AsanInitFromRtl();
669+
if (flags()->replace_str) {
670+
uptr from_size = Min<uptr>(size, MaybeRealWcsnlen(from, size) + 1) * sizeof(wchar_t);
671+
CHECK_RANGES_OVERLAP("wcsncpy", to, from_size, from, from_size);
672+
ASAN_READ_RANGE(ctx, from, from_size);
673+
ASAN_WRITE_RANGE(ctx, to, size * sizeof(wchar_t));
674+
}
675+
return REAL(wcsncpy)(to, from, size);
676+
}
677+
636678
template <typename Fn>
637679
static ALWAYS_INLINE auto StrtolImpl(void *ctx, Fn real, const char *nptr,
638680
char **endptr, int base)
@@ -808,6 +850,8 @@ void InitializeAsanInterceptors() {
808850
ASAN_INTERCEPT_FUNC(strcpy);
809851
ASAN_INTERCEPT_FUNC(strncat);
810852
ASAN_INTERCEPT_FUNC(strncpy);
853+
ASAN_INTERCEPT_FUNC(wcscpy);
854+
ASAN_INTERCEPT_FUNC(wcsncpy);
811855
ASAN_INTERCEPT_FUNC(strdup);
812856
# if ASAN_INTERCEPT___STRDUP
813857
ASAN_INTERCEPT_FUNC(__strdup);

compiler-rt/lib/asan/asan_interceptors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ DECLARE_REAL(char*, strchr, const char *str, int c)
129129
DECLARE_REAL(SIZE_T, strlen, const char *s)
130130
DECLARE_REAL(char*, strncpy, char *to, const char *from, SIZE_T size)
131131
DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen)
132+
DECLARE_REAL(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T maxlen)
132133
DECLARE_REAL(char*, strstr, const char *s1, const char *s2)
133134

134135
# if !SANITIZER_APPLE

compiler-rt/lib/asan/asan_win_static_runtime_thunk.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ INTERCEPT_LIBRARY_FUNCTION_ASAN(strstr);
6565
INTERCEPT_LIBRARY_FUNCTION_ASAN(strtok);
6666
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcslen);
6767
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsnlen);
68+
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcscat);
69+
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsncat);
70+
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcscpy);
71+
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsncpy);
6872

6973
// Note: Don't intercept strtol(l). They are supposed to set errno for out-of-
7074
// range values, but since the ASan runtime is linked against the dynamic CRT,

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
551551
#define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE (!SI_MAC && !SI_NETBSD)
552552
#define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID
553553
#define SANITIZER_INTERCEPT_WCSLEN 1
554-
#define SANITIZER_INTERCEPT_WCSCAT SI_POSIX
554+
#define SANITIZER_INTERCEPT_WCSCAT (SI_POSIX || SI_WINDOWS)
555555
#define SANITIZER_INTERCEPT_WCSDUP SI_POSIX
556556
#define SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION (!SI_WINDOWS && SI_NOT_FUCHSIA)
557557
#define SANITIZER_INTERCEPT_BSD_SIGNAL SI_ANDROID
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// REQUIRES: !windows
2+
// RUN: %clangxx_asan -O0 -fno-builtin %s -o %t && %run %t
3+
4+
#include <wchar.h>
5+
6+
int main() {
7+
wchar_t dst[16] = L"ab";
8+
const wchar_t *src = L"c";
9+
wcscat(dst, src);
10+
return 0;
11+
}
12+
13+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// REQUIRES: !windows
2+
// RUN: %clangxx_asan -O0 -fno-builtin %s -o %t && not %run %t 2>&1 | FileCheck %s
3+
4+
#include <wchar.h>
5+
6+
__attribute__((noinline)) void bad_wcs(void) {
7+
wchar_t buf[] = L"hello";
8+
// CHECK: wcscpy-param-overlap: memory ranges
9+
wcscpy(buf, buf + 1);
10+
}
11+
12+
int main() {
13+
bad_wcs();
14+
return 0;
15+
}
16+
17+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// REQUIRES: !windows
2+
// RUN: %clangxx_asan -O0 -fno-builtin %s -o %t && %run %t
3+
4+
#include <wchar.h>
5+
6+
int main() {
7+
wchar_t dst[16] = L"ab";
8+
const wchar_t *src = L"cd";
9+
wcsncat(dst, src, 1);
10+
return 0;
11+
}
12+
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// REQUIRES: !windows
2+
// RUN: %clangxx_asan -O0 -fno-builtin %s -o %t && %run %t
3+
4+
#include <wchar.h>
5+
6+
int main() {
7+
wchar_t src[] = L"abc";
8+
wchar_t dst[4] = {0};
9+
wcsncpy(dst, src, 3);
10+
return 0;
11+
}
12+
13+

0 commit comments

Comments
 (0)