Set a default value for slot in SharedMutex.h
authorMichael Lee <mzlee@fb.com>
Wed, 28 Sep 2016 00:31:22 +0000 (17:31 -0700)
committerFacebook Github Bot 2 <facebook-github-bot-2-bot@fb.com>
Wed, 28 Sep 2016 00:38:47 +0000 (17:38 -0700)
Summary:
`SharedMutexImpl::lockSharedImpl` has a potentially uninitialized access:

  Assume state = 0
  canAlreadyDefer = (state & kMayDefer) != 0 ==> false
  aboveDeferThreshold = (state & kHasS) >= (kNumSharedToStartDeferring - 1) * kIncrHasS ==> false

  if (canAlreadyDefer || (aboveDeferThreshold && !drainInProgress)) ==> false

  line:1452: gotSlot(slot)->compare_exchange_strong(...) uses slot uninitialized

Reviewed By: Orvid

Differential Revision: D3933638

fbshipit-source-id: 0fbce5c00b8b1f34e50c302cb88def97853c5afe

folly/SharedMutex.h

index 24bce0d7c73a9b8328836bb23cd9d3c7eaf811b6..48041c118896765ffe0ff2383488fe2a45b8a061 100644 (file)
@@ -1390,7 +1390,7 @@ bool SharedMutexImpl<ReaderPriority, Tag_, Atom, BlockImmediately>::
       return false;
     }
 
-    uint32_t slot;
+    uint32_t slot = tls_lastDeferredReaderSlot;
     uintptr_t slotValue = 1; // any non-zero value will do
 
     bool canAlreadyDefer = (state & kMayDefer) != 0;
@@ -1399,7 +1399,6 @@ bool SharedMutexImpl<ReaderPriority, Tag_, Atom, BlockImmediately>::
     bool drainInProgress = ReaderPriority && (state & kBegunE) != 0;
     if (canAlreadyDefer || (aboveDeferThreshold && !drainInProgress)) {
       /* Try using the most recent slot first. */
-      slot = tls_lastDeferredReaderSlot;
       slotValue = deferredReader(slot)->load(std::memory_order_relaxed);
       if (slotValue != 0) {
         // starting point for our empty-slot search, can change after