From 85cf7e60fe9d846f1e151015dc8448ddefdfb0b3 Mon Sep 17 00:00:00 2001 From: Philip Pronin Date: Fri, 7 Mar 2014 00:49:24 -0800 Subject: [PATCH] folly/experimental/Bits.h + ASan Summary: Allow disabling ASan for `folly::Bits`. Test Plan: [fb-only] Reviewed By: soren@fb.com FB internal diff: D1208584 --- folly/experimental/Bits.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/folly/experimental/Bits.h b/folly/experimental/Bits.h index 7750f3fc..62f73702 100644 --- a/folly/experimental/Bits.h +++ b/folly/experimental/Bits.h @@ -27,6 +27,9 @@ namespace folly { +template +struct UnalignedNoASan : public Unaligned { }; + // As a general rule, bit operations work on unsigned values only; // right-shift is arithmetic for signed values, and that can lead to // unpleasant bugs. @@ -62,6 +65,28 @@ struct BitsTraits, typename std::enable_if< } }; +// Special version that allows to disable address sanitizer on demand. +template +struct BitsTraits, typename std::enable_if< + (std::is_integral::value && std::is_unsigned::value)>::type> { + typedef T UnderlyingType; + static T FOLLY_DISABLE_ADDRESS_SANITIZER + load(const UnalignedNoASan& x) { return x.value; } + static void FOLLY_DISABLE_ADDRESS_SANITIZER + store(UnalignedNoASan& x, T v) { x.value = v; } + static T FOLLY_DISABLE_ADDRESS_SANITIZER + loadRMW(const UnalignedNoASan& x) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wuninitialized" +// make sure we compile without warning on gcc 4.6 with -Wpragmas +#if __GNUC_PREREQ(4, 7) +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif + return x.value; +#pragma GCC diagnostic pop + } +}; + // Partial specialization for T, where T is unsigned integral template struct BitsTraits