Skip to content

Commit fa5fa64

Browse files
Swaroop Manjunathfacebook-github-bot
Swaroop Manjunath
authored andcommitted
Prevent divide by zero errors in ObjSize controller.
Summary: AdRanker ASAN canary flagged a possible UBSan violation. ## Error Failed Run: https://fburl.com/servicelab/apytosry ``` #0 0x562e3adb59bc in facebook::cachelib::objcache2::ObjectCacheSizeController<facebook::cachelib::CacheAllocator<facebook::cachelib::LruCacheTrait> >::work() buck-out/v2/gen/fbcode/47d914adeee3d982/cachelib/experimental/objcache2/__object-cache__/headers/cachelib/experimental/objcache2/ObjectCacheSizeController-inl.h #1 0x562de7610f78 in facebook::cachelib::PeriodicWorker::loop() fbcode/cachelib/common/PeriodicWorker.cpp:55 #2 0x7f7632c524e4 in execute_native_thread_routine /home/engshare/third-party2/libgcc/11.x/src/gcc-11.x/x86_64-facebook-linux/libstdc++-v3/src/c++11/../../../.././libstdc++-v3/src/c++11/thread.cc:82:18 #3 0x7f7632f6ec0e in start_thread /home/engshare/third-party2/glibc/2.34/src/glibc-2.34/nptl/pthread_create.c:434:8 #4 0x7f76330011db in clone3 /home/engshare/third-party2/glibc/2.34/src/glibc-2.34/misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81 UndefinedBehaviorSanitizer: integer-divide-by-zero buck-out/v2/gen/fbcode/47d914adeee3d982/cachelib/experimental/objcache2/__object-cache__/headers/cachelib/experimental/objcache2/ObjectCacheSizeController-inl.h:33:40 in ``` Reviewed By: jiayuebao Differential Revision: D39024188 fbshipit-source-id: 64ad644c360565e638fa3ca74616a315038382ab
1 parent cabd034 commit fa5fa64

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

cachelib/experimental/objcache2/ObjectCacheSizeController-inl.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ namespace cachelib {
2121
namespace objcache2 {
2222
template <typename AllocatorT>
2323
void ObjectCacheSizeController<AllocatorT>::work() {
24-
auto totalObjSize = objCache_.getTotalObjectSize();
2524
auto currentNumEntries = objCache_.getNumEntries();
25+
if (currentNumEntries == 0) {
26+
return;
27+
}
28+
auto totalObjSize = objCache_.getTotalObjectSize();
2629
// Do the calculation only when total object size or total object number
2730
// achieves the threshold. This is to avoid unreliable calculation of average
2831
// object size when the cache is new and only has a few objects.

0 commit comments

Comments
 (0)