From 22ff8c5af7655d38c0355a7ccebacc1e00198eef Mon Sep 17 00:00:00 2001 From: Levi Tamasi Date: Thu, 27 Oct 2022 15:39:29 -0700 Subject: [PATCH] Use malloc/free for LRUHandle instead of new[]/delete[] (#10884) Summary: It's unsafe to call `malloc_usable_size` with an address not returned by a function from the `malloc` family (see https://github.com/facebook/rocksdb/issues/10798). The patch switches from using `new[]` / `delete[]` for `LRUHandle` to `malloc` / `free`. Pull Request resolved: https://github.com/facebook/rocksdb/pull/10884 Test Plan: `make check` Reviewed By: pdillinger Differential Revision: D40738089 Pulled By: ltamasi fbshipit-source-id: ac5583f88125fee49c314639be6b6df85937fbee --- cache/lru_cache.cc | 9 ++++----- cache/lru_cache.h | 4 +++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/cache/lru_cache.cc b/cache/lru_cache.cc index 06d223f3a..c8e4d29ba 100644 --- a/cache/lru_cache.cc +++ b/cache/lru_cache.cc @@ -386,7 +386,7 @@ Status LRUCacheShard::InsertItem(LRUHandle* e, LRUHandle** handle, last_reference_list.push_back(e); } else { if (free_handle_on_fail) { - delete[] reinterpret_cast(e); + free(e); *handle = nullptr; } s = Status::MemoryLimit("Insert failed due to LRU cache being full."); @@ -559,8 +559,7 @@ LRUHandle* LRUCacheShard::Lookup(const Slice& key, uint32_t hash, secondary_cache_->Lookup(key, create_cb, wait, found_dummy_entry, is_in_sec_cache); if (secondary_handle != nullptr) { - e = reinterpret_cast( - new char[sizeof(LRUHandle) - 1 + key.size()]); + e = static_cast(malloc(sizeof(LRUHandle) - 1 + key.size())); e->m_flags = 0; e->im_flags = 0; @@ -683,8 +682,8 @@ Status LRUCacheShard::Insert(const Slice& key, uint32_t hash, void* value, // Allocate the memory here outside of the mutex. // If the cache is full, we'll have to release it. // It shouldn't happen very often though. - LRUHandle* e = reinterpret_cast( - new char[sizeof(LRUHandle) - 1 + key.size()]); + LRUHandle* e = + static_cast(malloc(sizeof(LRUHandle) - 1 + key.size())); e->value = value; e->m_flags = 0; diff --git a/cache/lru_cache.h b/cache/lru_cache.h index ff5d36467..99b2f2b20 100644 --- a/cache/lru_cache.h +++ b/cache/lru_cache.h @@ -212,6 +212,7 @@ struct LRUHandle { void Free() { assert(refs == 0); + if (!IsSecondaryCacheCompatible() && info_.deleter) { (*info_.deleter)(key(), value); } else if (IsSecondaryCacheCompatible()) { @@ -226,7 +227,8 @@ struct LRUHandle { (*info_.helper->del_cb)(key(), value); } } - delete[] reinterpret_cast(this); + + free(this); } inline size_t CalcuMetaCharge(