Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions compiler-rt/lib/asan/asan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
return internal_strnlen(s, maxlen);
}

static inline uptr MaybeRealWcsnlen(const wchar_t *s, uptr maxlen) {
#if SANITIZER_INTERCEPT_WCSLEN
if (REAL(wcsnlen)) {
return REAL(wcsnlen)(s, maxlen);
}
#endif
return internal_wcsnlen(s, maxlen);
}

void SetThreadName(const char *name) {
AsanThread *t = GetCurrentThread();
if (t)
Expand Down Expand Up @@ -570,6 +579,26 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
return REAL(strcpy)(to, from);
}

INTERCEPTOR(wchar_t *, wcscpy, wchar_t *to, const wchar_t *from) {
void *ctx;
ASAN_INTERCEPTOR_ENTER(ctx, wcscpy);
if constexpr (SANITIZER_APPLE) {
if (UNLIKELY(!AsanInited()))
return REAL(wcscpy)(to, from);
} else {
if (!TryAsanInitFromRtl())
return REAL(wcscpy)(to, from);
}

if (flags()->replace_str) {
uptr from_size = (internal_wcslen(from) + 1) * sizeof(wchar_t);
CHECK_RANGES_OVERLAP("wcscpy", to, from_size, from, from_size);
ASAN_READ_RANGE(ctx, from, from_size);
ASAN_WRITE_RANGE(ctx, to, from_size);
}
return REAL(wcscpy)(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
Expand Down Expand Up @@ -633,6 +662,19 @@ INTERCEPTOR(char*, strncpy, char *to, const char *from, usize size) {
return REAL(strncpy)(to, from, size);
}

INTERCEPTOR(wchar_t *, wcsncpy, wchar_t *to, const wchar_t *from, usize size) {
void *ctx;
ASAN_INTERCEPTOR_ENTER(ctx, wcsncpy);
AsanInitFromRtl();
if (flags()->replace_str) {
uptr from_size = Min<uptr>(size, MaybeRealWcsnlen(from, size) + 1) * sizeof(wchar_t);
CHECK_RANGES_OVERLAP("wcsncpy", to, from_size, from, from_size);
ASAN_READ_RANGE(ctx, from, from_size);
ASAN_WRITE_RANGE(ctx, to, size * sizeof(wchar_t));
}
return REAL(wcsncpy)(to, from, size);
}

template <typename Fn>
static ALWAYS_INLINE auto StrtolImpl(void *ctx, Fn real, const char *nptr,
char **endptr, int base)
Expand Down Expand Up @@ -808,6 +850,8 @@ void InitializeAsanInterceptors() {
ASAN_INTERCEPT_FUNC(strcpy);
ASAN_INTERCEPT_FUNC(strncat);
ASAN_INTERCEPT_FUNC(strncpy);
ASAN_INTERCEPT_FUNC(wcscpy);
ASAN_INTERCEPT_FUNC(wcsncpy);
ASAN_INTERCEPT_FUNC(strdup);
# if ASAN_INTERCEPT___STRDUP
ASAN_INTERCEPT_FUNC(__strdup);
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/asan/asan_interceptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ DECLARE_REAL(char*, strchr, const char *str, int c)
DECLARE_REAL(SIZE_T, strlen, const char *s)
DECLARE_REAL(char*, strncpy, char *to, const char *from, SIZE_T size)
DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen)
DECLARE_REAL(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T maxlen)
DECLARE_REAL(char*, strstr, const char *s1, const char *s2)

# if !SANITIZER_APPLE
Expand Down
4 changes: 4 additions & 0 deletions compiler-rt/lib/asan/asan_win_static_runtime_thunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ INTERCEPT_LIBRARY_FUNCTION_ASAN(strstr);
INTERCEPT_LIBRARY_FUNCTION_ASAN(strtok);
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcslen);
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsnlen);
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcscat);
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsncat);
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcscpy);
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsncpy);

// Note: Don't intercept strtol(l). They are supposed to set errno for out-of-
// range values, but since the ASan runtime is linked against the dynamic CRT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
#define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE (!SI_MAC && !SI_NETBSD)
#define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID
#define SANITIZER_INTERCEPT_WCSLEN 1
#define SANITIZER_INTERCEPT_WCSCAT SI_POSIX
#define SANITIZER_INTERCEPT_WCSCAT (SI_POSIX || SI_WINDOWS)
#define SANITIZER_INTERCEPT_WCSDUP SI_POSIX
#define SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION (!SI_WINDOWS && SI_NOT_FUCHSIA)
#define SANITIZER_INTERCEPT_BSD_SIGNAL SI_ANDROID
Expand Down
13 changes: 13 additions & 0 deletions compiler-rt/test/asan/TestCases/wcscat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// REQUIRES: !windows
// RUN: %clangxx_asan -O0 -fno-builtin %s -o %t && %run %t

#include <wchar.h>

int main() {
wchar_t dst[16] = L"ab";
const wchar_t *src = L"c";
wcscat(dst, src);
return 0;
}


17 changes: 17 additions & 0 deletions compiler-rt/test/asan/TestCases/wcscpy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// REQUIRES: !windows
// RUN: %clangxx_asan -O0 -fno-builtin %s -o %t && not %run %t 2>&1 | FileCheck %s

#include <wchar.h>

__attribute__((noinline)) void bad_wcs(void) {
wchar_t buf[] = L"hello";
// CHECK: wcscpy-param-overlap: memory ranges
wcscpy(buf, buf + 1);
}

int main() {
bad_wcs();
return 0;
}


13 changes: 13 additions & 0 deletions compiler-rt/test/asan/TestCases/wcsncat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// REQUIRES: !windows
// RUN: %clangxx_asan -O0 -fno-builtin %s -o %t && %run %t

#include <wchar.h>

int main() {
wchar_t dst[16] = L"ab";
const wchar_t *src = L"cd";
wcsncat(dst, src, 1);
return 0;
}


13 changes: 13 additions & 0 deletions compiler-rt/test/asan/TestCases/wcsncpy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// REQUIRES: !windows
// RUN: %clangxx_asan -O0 -fno-builtin %s -o %t && %run %t

#include <wchar.h>

int main() {
wchar_t src[] = L"abc";
wchar_t dst[4] = {0};
wcsncpy(dst, src, 3);
return 0;
}