[asan] better message for parameter overlap bugs

llvm-svn: 147317
This commit is contained in:
Kostya Serebryany 2011-12-28 19:24:31 +00:00
parent 50bc2a71b2
commit e4a84c4f1f
2 changed files with 22 additions and 19 deletions

View File

@ -85,13 +85,13 @@ static inline bool RangesOverlap(const char *offset1, size_t length1,
const char *offset2, size_t length2) {
return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
}
#define CHECK_RANGES_OVERLAP(_offset1, length1, _offset2, length2) do { \
#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
const char *offset1 = (const char*)_offset1; \
const char *offset2 = (const char*)_offset2; \
if (RangesOverlap(offset1, length1, offset2, length2)) { \
Report("ERROR: AddressSanitizer strcpy-param-overlap: " \
Report("ERROR: AddressSanitizer %s-param-overlap: " \
"memory ranges [%p,%p) and [%p, %p) overlap\n", \
offset1, offset1 + length1, offset2, offset2 + length2); \
name, offset1, offset1 + length1, offset2, offset2 + length2); \
PRINT_CURRENT_STACK(); \
ShowStatsAndAbort(); \
} \
@ -186,7 +186,7 @@ void *WRAP(memcpy)(void *to, const void *from, size_t size) {
}
ENSURE_ASAN_INITED();
if (FLAG_replace_intrin) {
CHECK_RANGES_OVERLAP(to, size, from, size);
CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
ASAN_WRITE_RANGE(from, size);
ASAN_READ_RANGE(to, size);
}
@ -256,7 +256,7 @@ char *WRAP(strcat)(char *to, const char *from) { // NOLINT
size_t to_length = real_strlen(to);
ASAN_READ_RANGE(to, to_length);
ASAN_WRITE_RANGE(to + to_length, from_length + 1);
CHECK_RANGES_OVERLAP(to, to_length + 1, from, from_length + 1);
CHECK_RANGES_OVERLAP("strcat", to, to_length + 1, from, from_length + 1);
}
}
return real_strcat(to, from);
@ -289,7 +289,7 @@ char *WRAP(strcpy)(char *to, const char *from) { // NOLINT
ENSURE_ASAN_INITED();
if (FLAG_replace_str) {
size_t from_size = real_strlen(from) + 1;
CHECK_RANGES_OVERLAP(to, from_size, from, from_size);
CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
ASAN_READ_RANGE(from, from_size);
ASAN_WRITE_RANGE(to, from_size);
}
@ -355,7 +355,7 @@ char *WRAP(strncpy)(char *to, const char *from, size_t size) {
ENSURE_ASAN_INITED();
if (FLAG_replace_str) {
size_t from_size = Min(size, internal_strnlen(from, size) + 1);
CHECK_RANGES_OVERLAP(to, from_size, from, from_size);
CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
ASAN_READ_RANGE(from, from_size);
ASAN_WRITE_RANGE(to, size);
}

View File

@ -1357,7 +1357,9 @@ TEST(AddressSanitizer, StrCatOOBTest) {
strcat(to, from + 1);
}
static const char *kOverlapErrorMessage = "strcpy-param-overlap";
static string OverlapErrorMessage(const string &func) {
return func + "-param-overlap";
}
TEST(AddressSanitizer, StrArgsOverlapTest) {
size_t size = Ident(100);
@ -1368,27 +1370,28 @@ TEST(AddressSanitizer, StrArgsOverlapTest) {
memset(str, 'z', size);
Ident(memcpy)(str + 1, str + 11, 10);
Ident(memcpy)(str, str, 0);
EXPECT_DEATH(Ident(memcpy)(str, str + 14, 15), kOverlapErrorMessage);
EXPECT_DEATH(Ident(memcpy)(str + 14, str, 15), kOverlapErrorMessage);
EXPECT_DEATH(Ident(memcpy)(str + 20, str + 20, 1), kOverlapErrorMessage);
EXPECT_DEATH(Ident(memcpy)(str, str + 14, 15), OverlapErrorMessage("memcpy"));
EXPECT_DEATH(Ident(memcpy)(str + 14, str, 15), OverlapErrorMessage("memcpy"));
EXPECT_DEATH(Ident(memcpy)(str + 20, str + 20, 1),
OverlapErrorMessage("memcpy"));
#endif
// Check "strcpy".
memset(str, 'z', size);
str[9] = '\0';
strcpy(str + 10, str);
EXPECT_DEATH(strcpy(str + 9, str), kOverlapErrorMessage);
EXPECT_DEATH(strcpy(str, str + 4), kOverlapErrorMessage);
EXPECT_DEATH(strcpy(str + 9, str), OverlapErrorMessage("strcpy"));
EXPECT_DEATH(strcpy(str, str + 4), OverlapErrorMessage("strcpy"));
strcpy(str, str + 5);
// Check "strncpy".
memset(str, 'z', size);
strncpy(str, str + 10, 10);
EXPECT_DEATH(strncpy(str, str + 9, 10), kOverlapErrorMessage);
EXPECT_DEATH(strncpy(str + 9, str, 10), kOverlapErrorMessage);
EXPECT_DEATH(strncpy(str, str + 9, 10), OverlapErrorMessage("strncpy"));
EXPECT_DEATH(strncpy(str + 9, str, 10), OverlapErrorMessage("strncpy"));
str[10] = '\0';
strncpy(str + 11, str, 20);
EXPECT_DEATH(strncpy(str + 10, str, 20), kOverlapErrorMessage);
EXPECT_DEATH(strncpy(str + 10, str, 20), OverlapErrorMessage("strncpy"));
// Check "strcat".
memset(str, 'z', size);
@ -1398,9 +1401,9 @@ TEST(AddressSanitizer, StrArgsOverlapTest) {
strcat(str, str + 11);
str[10] = '\0';
strcat(str + 11, str);
EXPECT_DEATH(strcat(str, str + 9), kOverlapErrorMessage);
EXPECT_DEATH(strcat(str + 9, str), kOverlapErrorMessage);
EXPECT_DEATH(strcat(str + 10, str), kOverlapErrorMessage);
EXPECT_DEATH(strcat(str, str + 9), OverlapErrorMessage("strcat"));
EXPECT_DEATH(strcat(str + 9, str), OverlapErrorMessage("strcat"));
EXPECT_DEATH(strcat(str + 10, str), OverlapErrorMessage("strcat"));
free(str);
}