[asan] if calloc returns a freshly-mmaped memory, don't clear it with memset. Speeds up calloc-intensive code

llvm-svn: 176185
This commit is contained in:
Kostya Serebryany 2013-02-27 13:38:19 +00:00
parent 24b4bb06ed
commit 8ee2a5adc7
2 changed files with 21 additions and 1 deletions

View File

@ -604,7 +604,9 @@ void *asan_malloc(uptr size, StackTrace *stack) {
void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack) {
if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return 0;
void *ptr = Allocate(nmemb * size, 8, stack, FROM_MALLOC);
if (ptr)
// If the memory comes from the secondary allocator no need to clear it
// as it comes directly from mmap.
if (ptr && allocator.FromPrimary(ptr))
REAL(memset)(ptr, 0, nmemb * size);
return ptr;
}

View File

@ -857,3 +857,21 @@ TEST(AddressSanitizerInterface, CallocOverflow2) {
EXPECT_EQ(0L, Ident(p));
#endif
}
TEST(AddressSanitizerInterface, CallocReturnsZeroMem) {
size_t sizes[] = {16, 1000, 10000, 100000, 2100000};
for (size_t s = 0; s < ARRAY_SIZE(sizes); s++) {
size_t size = sizes[s];
for (size_t iter = 0; iter < 5; iter++) {
char *x = Ident((char*)calloc(1, size));
EXPECT_EQ(x[0], 0);
EXPECT_EQ(x[size - 1], 0);
EXPECT_EQ(x[size / 2], 0);
EXPECT_EQ(x[size / 3], 0);
EXPECT_EQ(x[size / 4], 0);
memset(x, 0x42, size);
free(Ident(x));
free(Ident(malloc(Ident(1 << 27)))); // Try to drain the quarantine.
}
}
}