From: Amos Jeffries Date: Tue, 12 Jul 2016 16:15:51 +0000 (+1200) Subject: Bug 4534: assertion failure in xcalloc when using many cache_dir X-Git-Tag: SQUID_4_0_13~35 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=831e953c433124a8e8313e3fc585fee186f742bd;p=thirdparty%2Fsquid.git Bug 4534: assertion failure in xcalloc when using many cache_dir --- diff --git a/src/CacheDigest.cc b/src/CacheDigest.cc index 518c0cef47..16060e3d3a 100644 --- a/src/CacheDigest.cc +++ b/src/CacheDigest.cc @@ -35,11 +35,11 @@ static void cacheDigestHashKey(const CacheDigest * cd, const cache_key * key); static uint32_t hashed_keys[4]; void -CacheDigest::init(int newCapacity) +CacheDigest::init(uint64_t newCapacity) { const auto newMaskSz = CacheDigest::CalcMaskSize(newCapacity, bits_per_entry); assert(newCapacity > 0 && bits_per_entry > 0); - assert(newMaskSz > 0); + assert(newMaskSz != 0); capacity = newCapacity; mask_size = newMaskSz; mask = static_cast(xcalloc(mask_size,1)); @@ -47,13 +47,13 @@ CacheDigest::init(int newCapacity) << mask_size << " bytes"); } -CacheDigest::CacheDigest(int aCapacity, int bpe) : +CacheDigest::CacheDigest(uint64_t aCapacity, uint8_t bpe) : + count(0), + del_count(0), + capacity(0), mask(nullptr), mask_size(0), - capacity(0), - bits_per_entry(bpe), - count(0), - del_count(0) + bits_per_entry(bpe) { assert(SQUID_MD5_DIGEST_LENGTH == 16); /* our hash functions rely on 16 byte keys */ updateCapacity(aCapacity); @@ -83,7 +83,7 @@ CacheDigest::clear() } void -CacheDigest::updateCapacity(int newCapacity) +CacheDigest::updateCapacity(uint64_t newCapacity) { safe_free(mask); init(newCapacity); // will re-init mask and mask_size @@ -261,12 +261,12 @@ cacheDigestReport(CacheDigest * cd, const char *label, StoreEntry * e) storeAppendPrintf(e, "%s digest: size: %d bytes\n", label ? label : "", stats.bit_count / 8 ); - storeAppendPrintf(e, "\t entries: count: %d capacity: %d util: %d%%\n", + storeAppendPrintf(e, "\t entries: count: %" PRIu64 " capacity: %" PRIu64 " util: %d%%\n", cd->count, cd->capacity, xpercentInt(cd->count, cd->capacity) ); - storeAppendPrintf(e, "\t deletion attempts: %d\n", + storeAppendPrintf(e, "\t deletion attempts: %" PRIu64 "\n", cd->del_count ); storeAppendPrintf(e, "\t bits: per entry: %d on: %d capacity: %d util: %d%%\n", @@ -280,17 +280,18 @@ cacheDigestReport(CacheDigest * cd, const char *label, StoreEntry * e) ); } -size_t -CacheDigest::CalcMaskSize(int cap, int bpe) +uint32_t +CacheDigest::CalcMaskSize(uint64_t cap, uint8_t bpe) { - // XXX: might 32-bit overflow during multiply - return (size_t) (cap * bpe + 7) / 8; + uint64_t bitCount = (cap * bpe) + 7; + assert(bitCount < INT_MAX); // dont 31-bit overflow later + return static_cast(bitCount / 8); } static void cacheDigestHashKey(const CacheDigest * cd, const cache_key * key) { - const unsigned int bit_count = cd->mask_size * 8; + const uint32_t bit_count = cd->mask_size * 8; unsigned int tmp_keys[4]; /* we must memcpy to ensure alignment */ memcpy(tmp_keys, key, sizeof(tmp_keys)); diff --git a/src/CacheDigest.h b/src/CacheDigest.h index 8c6f04ee9b..be0decbea4 100644 --- a/src/CacheDigest.h +++ b/src/CacheDigest.h @@ -21,7 +21,7 @@ class CacheDigest { MEMPROXY_CLASS(CacheDigest); public: - CacheDigest(int capacity, int bpe); + CacheDigest(uint64_t capacity, uint8_t bpe); ~CacheDigest(); // NP: only used by broken unit-test @@ -32,7 +32,7 @@ public: void clear(); /// changes mask size to fit newCapacity, resets bits to 0 - void updateCapacity(int newCapacity); + void updateCapacity(uint64_t newCapacity); void add(const cache_key * key); void remove(const cache_key * key); @@ -45,19 +45,19 @@ public: /// calculate the size of mask required to digest up to /// a specified capacity and bitsize. - static size_t CalcMaskSize(int cap, int bpe); + static uint32_t CalcMaskSize(uint64_t cap, uint8_t bpe); private: - void init(int newCapacity); + void init(uint64_t newCapacity); public: /* public, read-only */ - char *mask; /* bit mask */ - int mask_size; /* mask size in bytes */ - int capacity; /* expected maximum for .count, not a hard limit */ - int bits_per_entry; /* number of bits allocated for each entry from capacity */ - int count; /* number of digested entries */ - int del_count; /* number of deletions performed so far */ + uint64_t count; /* number of digested entries */ + uint64_t del_count; /* number of deletions performed so far */ + uint64_t capacity; /* expected maximum for .count, not a hard limit */ + char *mask; /* bit mask */ + uint32_t mask_size; /* mask size in bytes */ + int8_t bits_per_entry; /* number of bits allocated for each entry from capacity */ }; void cacheDigestGuessStatsUpdate(CacheDigestGuessStats * stats, int real_hit, int guess_hit); diff --git a/src/PeerDigest.h b/src/PeerDigest.h index f3740da1fd..63108ac29c 100644 --- a/src/PeerDigest.h +++ b/src/PeerDigest.h @@ -56,7 +56,7 @@ public: store_client *old_sc; HttpRequest *request; int offset; - int mask_offset; + uint32_t mask_offset; time_t start_time; time_t resp_time; time_t expires; diff --git a/src/peer_digest.cc b/src/peer_digest.cc index 9920581bfe..1ac908cd5f 100644 --- a/src/peer_digest.cc +++ b/src/peer_digest.cc @@ -746,7 +746,7 @@ peerDigestFetchedEnough(DigestFetchState * fetch, char *buf, ssize_t size, const if (!reason && !size) { if (!pd->cd) reason = "null digest?!"; - else if (fetch->mask_offset != (int)pd->cd->mask_size) + else if (fetch->mask_offset != pd->cd->mask_size) reason = "premature end of digest?!"; else if (!peerDigestUseful(pd)) reason = "useless digest"; diff --git a/src/store_digest.cc b/src/store_digest.cc index 28712b0dcd..1fbacda670 100644 --- a/src/store_digest.cc +++ b/src/store_digest.cc @@ -77,21 +77,49 @@ static void storeDigestRewriteResume(void); static void storeDigestRewriteFinish(StoreEntry * e); static EVH storeDigestSwapOutStep; static void storeDigestCBlockSwapOut(StoreEntry * e); -static int storeDigestCalcCap(void); -static int storeDigestResize(void); static void storeDigestAdd(const StoreEntry *); -#endif /* USE_CACHE_DIGESTS */ - static void storeDigestRegisterWithCacheManager(void) { Mgr::RegisterAction("store_digest", "Store Digest", storeDigestReport, 0, 1); } -/* - * PUBLIC FUNCTIONS - */ +/// calculates digest capacity +static uint64_t +storeDigestCalcCap() +{ + /* + * To-Do: Bloom proved that the optimal filter utilization is 50% (half of + * the bits are off). However, we do not have a formula to calculate the + * number of _entries_ we want to pre-allocate for. + */ + const uint64_t hi_cap = Store::Root().maxSize() / Config.Store.avgObjectSize; + const uint64_t lo_cap = 1 + Store::Root().currentSize() / Config.Store.avgObjectSize; + const uint64_t e_count = StoreEntry::inUseCount(); + uint64_t cap = e_count ? e_count : hi_cap; + debugs(71, 2, "have: " << e_count << ", want " << cap << + " entries; limits: [" << lo_cap << ", " << hi_cap << "]"); + + if (cap < lo_cap) + cap = lo_cap; + + /* do not enforce hi_cap limit, average-based estimation may be wrong + *if (cap > hi_cap) + * cap = hi_cap; + */ + + // Bug 4534: we still have to set an upper-limit at some reasonable value though. + // this matches cacheDigestCalcMaskSize doing (cap*bpe)+7 < INT_MAX + const uint64_t absolute_max = (INT_MAX -8) / Config.digest.bits_per_entry; + if (cap > absolute_max) { + debugs(71, DBG_CRITICAL, "WARNING: Cache Digest cannot store " << cap << " entries. Limiting to " << absolute_max); + cap = absolute_max; + } + + return cap; +} +#endif /* USE_CACHE_DIGESTS */ void storeDigestInit(void) @@ -99,14 +127,13 @@ storeDigestInit(void) storeDigestRegisterWithCacheManager(); #if USE_CACHE_DIGESTS - const int cap = storeDigestCalcCap(); - if (!Config.onoff.digest_generation) { store_digest = NULL; debugs(71, 3, "Local cache digest generation disabled"); return; } + const uint64_t cap = storeDigestCalcCap(); store_digest = new CacheDigest(cap, Config.digest.bits_per_entry); debugs(71, DBG_IMPORTANT, "Local cache digest enabled; rebuild/rewrite every " << (int) Config.digest.rebuild_period << "/" << @@ -291,6 +318,27 @@ storeDigestRebuildStart(void *datanotused) storeDigestRebuildResume(); } +/// \returns true if we actually resized the digest +static bool +storeDigestResize() +{ + const uint64_t cap = storeDigestCalcCap(); + assert(store_digest); + uint64_t diff = abs(cap - store_digest->capacity); + debugs(71, 2, store_digest->capacity << " -> " << cap << "; change: " << + diff << " (" << xpercentInt(diff, store_digest->capacity) << "%)" ); + /* avoid minor adjustments */ + + if (diff <= store_digest->capacity / 10) { + debugs(71, 2, "small change, will not resize."); + return false; + } else { + debugs(71, 2, "big change, resizing."); + store_digest->updateCapacity(cap); + } + return true; +} + /* called be Rewrite to push Rebuild forward */ static void storeDigestRebuildResume(void) @@ -440,7 +488,7 @@ storeDigestSwapOutStep(void *data) assert(e); /* _add_ check that nothing bad happened while we were waiting @?@ @?@ */ - if (sd_state.rewrite_offset + chunk_size > store_digest->mask_size) + if (static_cast(sd_state.rewrite_offset + chunk_size) > store_digest->mask_size) chunk_size = store_digest->mask_size - sd_state.rewrite_offset; e->append(store_digest->mask + sd_state.rewrite_offset, chunk_size); @@ -452,7 +500,7 @@ storeDigestSwapOutStep(void *data) sd_state.rewrite_offset += chunk_size; /* are we done ? */ - if (sd_state.rewrite_offset >= store_digest->mask_size) + if (static_cast(sd_state.rewrite_offset) >= store_digest->mask_size) storeDigestRewriteFinish(e); else eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep, data, 0.0, 1, false); @@ -468,60 +516,10 @@ storeDigestCBlockSwapOut(StoreEntry * e) sd_state.cblock.count = htonl(store_digest->count); sd_state.cblock.del_count = htonl(store_digest->del_count); sd_state.cblock.mask_size = htonl(store_digest->mask_size); - sd_state.cblock.bits_per_entry = (unsigned char) - Config.digest.bits_per_entry; + sd_state.cblock.bits_per_entry = Config.digest.bits_per_entry; sd_state.cblock.hash_func_count = (unsigned char) CacheDigestHashFuncCount; e->append((char *) &sd_state.cblock, sizeof(sd_state.cblock)); } -/* calculates digest capacity */ -static int -storeDigestCalcCap(void) -{ - /* - * To-Do: Bloom proved that the optimal filter utilization is 50% (half of - * the bits are off). However, we do not have a formula to calculate the - * number of _entries_ we want to pre-allocate for. - */ - const int hi_cap = Store::Root().maxSize() / Config.Store.avgObjectSize; - const int lo_cap = 1 + Store::Root().currentSize() / Config.Store.avgObjectSize; - const int e_count = StoreEntry::inUseCount(); - int cap = e_count ? e_count :hi_cap; - debugs(71, 2, "storeDigestCalcCap: have: " << e_count << ", want " << cap << - " entries; limits: [" << lo_cap << ", " << hi_cap << "]"); - - if (cap < lo_cap) - cap = lo_cap; - - /* do not enforce hi_cap limit, average-based estimation may be wrong - *if (cap > hi_cap) - * cap = hi_cap; - */ - return cap; -} - -/* returns true if we actually resized the digest */ -static int -storeDigestResize(void) -{ - const int cap = storeDigestCalcCap(); - int diff; - assert(store_digest); - diff = abs(cap - store_digest->capacity); - debugs(71, 2, "storeDigestResize: " << - store_digest->capacity << " -> " << cap << "; change: " << - diff << " (" << xpercentInt(diff, store_digest->capacity) << "%)" ); - /* avoid minor adjustments */ - - if (diff <= store_digest->capacity / 10) { - debugs(71, 2, "storeDigestResize: small change, will not resize."); - return 0; - } else { - debugs(71, 2, "storeDigestResize: big change, resizing."); - store_digest->updateCapacity(cap); - return 1; - } -} - #endif /* USE_CACHE_DIGESTS */ diff --git a/src/tests/stub_CacheDigest.cc b/src/tests/stub_CacheDigest.cc index ed6bf660df..aee85bce8a 100644 --- a/src/tests/stub_CacheDigest.cc +++ b/src/tests/stub_CacheDigest.cc @@ -17,11 +17,11 @@ class CacheDigestGuessStats; class StoreEntry; #include "CacheDigest.h" -CacheDigest::CacheDigest(int, int) {STUB} +CacheDigest::CacheDigest(uint64_t, uint8_t) {STUB} CacheDigest::~CacheDigest() {STUB} CacheDigest *CacheDigest::clone() const STUB_RETVAL(nullptr) void CacheDigest::clear() STUB -void CacheDigest::updateCapacity(int) STUB +void CacheDigest::updateCapacity(uint64_t) STUB bool CacheDigest::contains(const cache_key *) const STUB_RETVAL(false) void CacheDigest::add(const cache_key *) STUB void CacheDigest::remove(const cache_key *) STUB @@ -29,5 +29,5 @@ double CacheDigest::usedMaskPercent() const STUB_RETVAL(0.0) void cacheDigestGuessStatsUpdate(CacheDigestGuessStats *, int, int) STUB void cacheDigestGuessStatsReport(const CacheDigestGuessStats *, StoreEntry *, const char *) STUB void cacheDigestReport(CacheDigest *, const char *, StoreEntry *) STUB -size_t CacheDigest::CalcMaskSize(int, int) STUB_RETVAL(1) +uint32_t CacheDigest::CalcMaskSize(uint64_t, uint8_t) STUB_RETVAL(1)