asan: Add a wcslen interceptor mirroring strlen

Tested on Linux, since I can't build the tests on Windows yet.

llvm-svn: 190022
This commit is contained in:
Reid Kleckner 2013-09-05 01:13:49 +00:00
parent 3af441f1af
commit 0071525492
3 changed files with 22 additions and 0 deletions

View File

@ -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);

View File

@ -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

View File

@ -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);