`detail::Futex` wants 4 bytes but MicroLock only gives you one
authorMichael Lee <mzlee@fb.com>
Tue, 19 Apr 2016 14:52:23 +0000 (07:52 -0700)
committerFacebook Github Bot 6 <facebook-github-bot-6-bot@fb.com>
Tue, 19 Apr 2016 15:05:19 +0000 (08:05 -0700)
Summary: ^^^ this is a stack overflow in the test, and a possible stack or heap overflow.

Reviewed By: dcolascione

Differential Revision: D3151717

fb-gh-sync-id: b4f0660ebbb89139dff003870e132c312068d9a8
fbshipit-source-id: b4f0660ebbb89139dff003870e132c312068d9a8

folly/MicroLock.h
folly/test/SmallLocksTest.cpp

index 8f0b09f7d2902a1591bae1d48ab77c716e665476..5c50955336f6599e2d2cbf6f0154b59f4958077b 100644 (file)
 #include <folly/detail/Futex.h>
 #include <folly/Portability.h>
 
+#if defined(__clang__)
+#define NO_SANITIZE_ADDRESS __attribute__((__no_sanitize__("address")))
+#else
+#define NO_SANITIZE_ADDRESS
+#endif
+
 namespace folly {
 
 /**
@@ -88,7 +94,12 @@ namespace folly {
 
 class MicroLockCore {
  protected:
+#if defined(__SANITIZE_ADDRESS__) && !defined(__clang__) && \
+    (defined(__GNUC__) || defined(__GNUG__))
+  uint32_t lock_;
+#else
   uint8_t lock_;
+#endif
   inline detail::Futex<>* word() const;
   inline uint32_t baseShift(unsigned slot) const;
   inline uint32_t heldBit(unsigned slot) const;
@@ -100,7 +111,7 @@ class MicroLockCore {
                            unsigned maxYields);
 
  public:
-  inline void unlock(unsigned slot);
+  inline void unlock(unsigned slot) NO_SANITIZE_ADDRESS;
   inline void unlock() { unlock(0); }
   // Initializes all the slots.
   inline void init() { lock_ = 0; }
@@ -151,9 +162,9 @@ void MicroLockCore::unlock(unsigned slot) {
 template <unsigned MaxSpins = 1000, unsigned MaxYields = 0>
 class MicroLockBase : public MicroLockCore {
  public:
-  inline void lock(unsigned slot);
+  inline void lock(unsigned slot) NO_SANITIZE_ADDRESS;
   inline void lock() { lock(0); }
-  inline bool try_lock(unsigned slot);
+  inline bool try_lock(unsigned slot) NO_SANITIZE_ADDRESS;
   inline bool try_lock() { return try_lock(0); }
 };
 
index ad6a83785ace8020e1c93114d8e22af511b6b06e..5aef9ea175debcba09864226d995041e8e5a0079 100644 (file)
@@ -181,7 +181,12 @@ TEST(SmallLocks, RegClobber) {
 }
 
 FOLLY_PACK_PUSH
+#if defined(__SANITIZE_ADDRESS__) && !defined(__clang__) && \
+    (defined(__GNUC__) || defined(__GNUG__))
+static_assert(sizeof(MicroLock) == 4, "Size check failed");
+#else
 static_assert(sizeof(MicroLock) == 1, "Size check failed");
+#endif
 FOLLY_PACK_POP
 
 namespace {