From 7af6ced14baebc702700fd16417849ae711b9796 Mon Sep 17 00:00:00 2001 From: Peter Dillinger Date: Mon, 9 Sep 2019 15:24:54 -0700 Subject: [PATCH] Fix block allocation bug in new DynamicBloom (#5783) Summary: Bug found by valgrind. New DynamicBloom wasn't allocating in block sizes. New assertion added that probes starting in final word would be in bounds. Pull Request resolved: https://github.com/facebook/rocksdb/pull/5783 Test Plan: ROCKSDB_VALGRIND_RUN=1 DISABLE_JEMALLOC=1 valgrind --leak-check=full ./dynamic_bloom_test Differential Revision: D17270623 Pulled By: pdillinger fbshipit-source-id: 1e0407504b875133a771383cd488c70f91be2b87 --- util/dynamic_bloom.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/util/dynamic_bloom.cc b/util/dynamic_bloom.cc index 3c153c719..0ad06942e 100644 --- a/util/dynamic_bloom.cc +++ b/util/dynamic_bloom.cc @@ -39,11 +39,18 @@ DynamicBloom::DynamicBloom(Allocator* allocator, uint32_t total_bits, // a valid u64 index if x is a valid u64 index and 0 <= i < kNumDoubleProbes. uint32_t block_bytes = /*bytes/u64*/ 8 * /*u64s*/ std::max(1U, roundUpToPow2(kNumDoubleProbes)); - kLen = (total_bits + (/*bits/byte*/ 8 * block_bytes - 1)) / - /*bits/u64*/ 64; + uint32_t block_bits = block_bytes * 8; + uint32_t blocks = (total_bits + block_bits - 1) / block_bits; + uint32_t sz = blocks * block_bytes; + kLen = sz / /*bytes/u64*/8; assert(kLen > 0); +#ifndef NDEBUG + for (uint32_t i = 0; i < kNumDoubleProbes; ++i) { + // Ensure probes starting at last word are in range + assert(((kLen - 1) ^ i) < kLen); + } +#endif - uint32_t sz = kLen * /*bytes/u64*/ 8; // Padding to correct for allocation not originally aligned on block_bytes // boundary sz += block_bytes - 1;