From 0071525492b6876ddf2a2920d5295f697def85cf Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 5 Sep 2013 01:13:49 +0000 Subject: [PATCH] asan: Add a wcslen interceptor mirroring strlen Tested on Linux, since I can't build the tests on Windows yet. llvm-svn: 190022 --- compiler-rt/lib/asan/asan_intercepted_functions.h | 1 + compiler-rt/lib/asan/asan_interceptors.cc | 10 ++++++++++ compiler-rt/lib/asan/tests/asan_str_test.cc | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/compiler-rt/lib/asan/asan_intercepted_functions.h b/compiler-rt/lib/asan/asan_intercepted_functions.h index 842781cdb17f..7b19b3dad903 100644 --- a/compiler-rt/lib/asan/asan_intercepted_functions.h +++ b/compiler-rt/lib/asan/asan_intercepted_functions.h @@ -99,6 +99,7 @@ char* strchr(const char *str, int c); int strcmp(const char *s1, const char* s2); char* strcpy(char *to, const char* from); // NOLINT uptr strlen(const char *s); +uptr wcslen(const wchar_t *s); char* strncat(char *to, const char* from, uptr size); int strncmp(const char *s1, const char* s2, uptr size); char* strncpy(char *to, const char* from, uptr size); diff --git a/compiler-rt/lib/asan/asan_interceptors.cc b/compiler-rt/lib/asan/asan_interceptors.cc index d7b43da08fe2..c996b66135bb 100644 --- a/compiler-rt/lib/asan/asan_interceptors.cc +++ b/compiler-rt/lib/asan/asan_interceptors.cc @@ -482,6 +482,15 @@ INTERCEPTOR(uptr, strlen, const char *s) { return length; } +INTERCEPTOR(uptr, wcslen, const wchar_t *s) { + uptr length = REAL(wcslen)(s); + if (!asan_init_is_running) { + ENSURE_ASAN_INITED(); + ASAN_READ_RANGE(s, (length + 1) * sizeof(wchar_t)); + } + return length; +} + INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) { ENSURE_ASAN_INITED(); if (flags()->replace_str) { @@ -680,6 +689,7 @@ void InitializeAsanInterceptors() { ASAN_INTERCEPT_FUNC(strchr); ASAN_INTERCEPT_FUNC(strcpy); // NOLINT ASAN_INTERCEPT_FUNC(strlen); + ASAN_INTERCEPT_FUNC(wcslen); ASAN_INTERCEPT_FUNC(strncat); ASAN_INTERCEPT_FUNC(strncpy); #if ASAN_INTERCEPT_STRDUP diff --git a/compiler-rt/lib/asan/tests/asan_str_test.cc b/compiler-rt/lib/asan/tests/asan_str_test.cc index 128fb61c25a9..34be0b50cf82 100644 --- a/compiler-rt/lib/asan/tests/asan_str_test.cc +++ b/compiler-rt/lib/asan/tests/asan_str_test.cc @@ -61,6 +61,17 @@ TEST(AddressSanitizer, StrLenOOBTest) { free(heap_string); } +TEST(AddressSanitizer, WcsLenTest) { + EXPECT_EQ(0, wcslen(Ident(L""))); + size_t hello_len = 13; + size_t hello_size = (hello_len + 1) * sizeof(wchar_t); + EXPECT_EQ(hello_len, wcslen(Ident(L"Hello, World!"))); + wchar_t *heap_string = Ident((wchar_t*)malloc(hello_size)); + memcpy(heap_string, L"Hello, World!", hello_size); + EXPECT_EQ(hello_len, Ident(wcslen(heap_string))); + EXPECT_DEATH(Ident(wcslen(heap_string + 14)), RightOOBReadMessage(0)); +} + #ifndef __APPLE__ TEST(AddressSanitizer, StrNLenOOBTest) { size_t size = Ident(123);