Make Bits.h respect FOLLY_HAVE_UNALIGNED_ACCESS.
authorKyle Nekritz <knekritz@fb.com>
Wed, 9 Mar 2016 17:33:14 +0000 (09:33 -0800)
committerFacebook Github Bot 6 <facebook-github-bot-6-bot@fb.com>
Wed, 9 Mar 2016 17:35:21 +0000 (09:35 -0800)
Summary: This was causing a bunch of SIGBUS's on ARM platforms.

Reviewed By: yfeldblum, mzlee

Differential Revision: D3013458

fb-gh-sync-id: 6d3f60c3f59d16cd3454e3a4231b967e5378724a
shipit-source-id: 6d3f60c3f59d16cd3454e3a4231b967e5378724a

folly/Bits.h
folly/Portability.h
folly/SpookyHashV2.cpp
folly/configure.ac

index 8e4204f076524c8bd9a4bc3acfe93540f2e5748a..61a7407d10ddd26c97d495c1d44451b7259b3953 100644 (file)
@@ -550,7 +550,13 @@ template <class T>
 inline T loadUnaligned(const void* p) {
   static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
   static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
-  return static_cast<const Unaligned<T>*>(p)->value;
+  if (kHasUnalignedAccess) {
+    return static_cast<const Unaligned<T>*>(p)->value;
+  } else {
+    T value;
+    memcpy(&value, p, sizeof(T));
+    return value;
+  }
 }
 
 /**
@@ -560,7 +566,11 @@ template <class T>
 inline void storeUnaligned(void* p, T value) {
   static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
   static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
-  new (p) Unaligned<T>(value);
+  if (kHasUnalignedAccess) {
+    new (p) Unaligned<T>(value);
+  } else {
+    memcpy(p, &value, sizeof(T));
+  }
 }
 
 }  // namespace folly
index 63dcc5cfb0bdd4a21c824c44be97c624c387f628..dc33d3b4a45a322bbd5aad6788f630d3cfaa35d5 100644 (file)
  #endif
 #endif
 
-#ifndef FOLLY_HAVE_UNALIGNED_READS
-#define FOLLY_HAVE_UNALIGNED_READS 0
+// Unaligned loads and stores
+namespace folly {
+#if FOLLY_HAVE_UNALIGNED_ACCESS
+constexpr bool kHasUnalignedAccess = true;
+#else
+constexpr bool kHasUnalignedAccess = false;
 #endif
+}
 
 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
 // MAP_ANON on OSX/BSD.
index d55a62be2acdeb36e2c01a7aeb34aed79f4e2538..b945bc6f49badd6dafb63c6ab786b7fbb4ec8d52 100644 (file)
@@ -35,8 +35,6 @@
 namespace folly {
 namespace hash {
 
-static constexpr auto kAllowUnalignedReads = bool(FOLLY_HAVE_UNALIGNED_READS);
-
 //
 // short hash ... it could be used on any message,
 // but it's used by Spooky just for short messages.
@@ -58,7 +56,7 @@ void SpookyHashV2::Short(
 
     u.p8 = (const uint8_t *)message;
 
-    if (!kAllowUnalignedReads && (u.i & 0x7))
+    if (!kHasUnalignedAccess && (u.i & 0x7))
     {
         memcpy(buf, message, length);
         u.p64 = buf;
@@ -178,7 +176,7 @@ void SpookyHashV2::Hash128(
     end = u.p64 + (length/sc_blockSize)*sc_numVars;
 
     // handle all whole sc_blockSize blocks of bytes
-    if (kAllowUnalignedReads || ((u.i & 0x7) == 0))
+    if (kHasUnalignedAccess || ((u.i & 0x7) == 0))
     {
         while (u.p64 < end)
         {
@@ -286,7 +284,7 @@ void SpookyHashV2::Update(const void *message, size_t length)
     // handle all whole blocks of sc_blockSize bytes
     end = u.p64 + (length/sc_blockSize)*sc_numVars;
     remainder = (uint8_t)(length-((const uint8_t *)end-u.p8));
-    if (kAllowUnalignedReads || (u.i & 0x7) == 0)
+    if (kHasUnalignedAccess || (u.i & 0x7) == 0)
     {
         while (u.p64 < end)
         {
index 2b52035ad6e801ad8f1e97ae41c854c7b8ee7cb3..62c81230b590cdccf0cb5ba03c750c582777d20b 100644 (file)
@@ -339,10 +339,10 @@ if test "$folly_cv_prog_cc_weak_symbols" = yes; then
             [Define to 1 if the linker supports weak symbols.])
 fi
 
-# Figure out whether the architecture supports unaligned reads
+# Figure out whether the architecture supports unaligned accesses
 AC_CACHE_CHECK(
-  [for unaligned reads support],
-  [folly_cv_prog_cc_unaligned_reads],
+  [for unaligned access support],
+  [folly_cv_prog_cc_unaligned_access],
   [AC_RUN_IFELSE(
     [AC_LANG_SOURCE[
       int main(int argc, char** argv) {
@@ -352,11 +352,11 @@ AC_CACHE_CHECK(
         return (*ptr & 0xff) == 0xef ? 0 : 1;
       }
     ]],
-    [folly_cv_prog_cc_unaligned_reads=yes],
-    [folly_cv_prog_cc_unaligned_reads=no])])
+    [folly_cv_prog_cc_unaligned_access=yes],
+    [folly_cv_prog_cc_unaligned_access=no])])
 
-if test "$folly_cv_prog_cc_unaligned_reads" = "yes"; then
-  AC_DEFINE([HAVE_UNALIGNED_READS], [1], [Define to 1 if the architecture allows unaligned reads])
+if test "$folly_cv_prog_cc_unaligned_access" = "yes"; then
+  AC_DEFINE([HAVE_UNALIGNED_ACCESS], [1], [Define to 1 if the architecture allows unaligned accesses])
 fi
 
 AC_CACHE_CHECK(