Not using aligned_alloc with gcc4 + asan

Summary:
GCC < 5 + ASAN does not instrument aligned_alloc, which can make ASAN
report false-positive with "free on address which was not malloc" error.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61693

Also suppress leak warning with LRUCache::DisownData().
Closes https://github.com/facebook/rocksdb/pull/2783

Differential Revision: D5696465

Pulled By: yiwu-arbug

fbshipit-source-id: 87c607c002511fa089b18cc35e24909bee0e74b4
This commit is contained in:
Yi Wu 2017-08-29 21:41:46 -07:00 committed by Facebook Github Bot
parent 0980dc6c9a
commit e83d6a02e3
2 changed files with 9 additions and 18 deletions

23
cache/lru_cache.cc vendored
View File

@ -234,35 +234,19 @@ void LRUCacheShard::EvictFromLRU(size_t charge,
} }
void* LRUCacheShard::operator new(size_t size) { void* LRUCacheShard::operator new(size_t size) {
#if __SANITIZE_ADDRESS__
return malloc(size);
#else
return port::cacheline_aligned_alloc(size); return port::cacheline_aligned_alloc(size);
#endif
} }
void* LRUCacheShard::operator new[](size_t size) { void* LRUCacheShard::operator new[](size_t size) {
#if __SANITIZE_ADDRESS__
return malloc(size);
#else
return port::cacheline_aligned_alloc(size); return port::cacheline_aligned_alloc(size);
#endif
} }
void LRUCacheShard::operator delete(void *memblock) { void LRUCacheShard::operator delete(void *memblock) {
#if __SANITIZE_ADDRESS__
free(memblock);
#else
port::cacheline_aligned_free(memblock); port::cacheline_aligned_free(memblock);
#endif
} }
void LRUCacheShard::operator delete[](void* memblock) { void LRUCacheShard::operator delete[](void* memblock) {
#if __SANITIZE_ADDRESS__
free(memblock);
#else
port::cacheline_aligned_free(memblock); port::cacheline_aligned_free(memblock);
#endif
} }
void LRUCacheShard::SetCapacity(size_t capacity) { void LRUCacheShard::SetCapacity(size_t capacity) {
@ -518,7 +502,12 @@ uint32_t LRUCache::GetHash(Handle* handle) const {
return reinterpret_cast<const LRUHandle*>(handle)->hash; return reinterpret_cast<const LRUHandle*>(handle)->hash;
} }
void LRUCache::DisownData() { shards_ = nullptr; } void LRUCache::DisownData() {
// Do not drop data if compile with ASAN to suppress leak warning.
#ifndef __SANITIZE_ADDRESS__
shards_ = nullptr;
#endif // !__SANITIZE_ADDRESS__
}
size_t LRUCache::TEST_GetLRUSize() { size_t LRUCache::TEST_GetLRUSize() {
size_t lru_size_of_all_shards = 0; size_t lru_size_of_all_shards = 0;

View File

@ -185,7 +185,9 @@ int GetMaxOpenFiles() {
} }
void *cacheline_aligned_alloc(size_t size) { void *cacheline_aligned_alloc(size_t size) {
#if defined (_ISOC11_SOURCE) #if __GNUC__ < 5 && defined(__SANITIZE_ADDRESS__)
return malloc(size);
#elif defined(_ISOC11_SOURCE)
return aligned_alloc(CACHE_LINE_SIZE, size); return aligned_alloc(CACHE_LINE_SIZE, size);
#elif ( _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__APPLE__)) #elif ( _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__APPLE__))
void *m; void *m;