From: Kyle Nekritz Date: Wed, 9 Mar 2016 17:33:14 +0000 (-0800) Subject: Make Bits.h respect FOLLY_HAVE_UNALIGNED_ACCESS. X-Git-Tag: 2016.07.26~455 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=d7a2b20b4df53ee1e622ebc7cb58d1b1f0959ec7;p=folly.git Make Bits.h respect FOLLY_HAVE_UNALIGNED_ACCESS. 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 --- diff --git a/folly/Bits.h b/folly/Bits.h index 8e4204f0..61a7407d 100644 --- a/folly/Bits.h +++ b/folly/Bits.h @@ -550,7 +550,13 @@ template inline T loadUnaligned(const void* p) { static_assert(sizeof(Unaligned) == sizeof(T), "Invalid unaligned size"); static_assert(alignof(Unaligned) == 1, "Invalid alignment"); - return static_cast*>(p)->value; + if (kHasUnalignedAccess) { + return static_cast*>(p)->value; + } else { + T value; + memcpy(&value, p, sizeof(T)); + return value; + } } /** @@ -560,7 +566,11 @@ template inline void storeUnaligned(void* p, T value) { static_assert(sizeof(Unaligned) == sizeof(T), "Invalid unaligned size"); static_assert(alignof(Unaligned) == 1, "Invalid alignment"); - new (p) Unaligned(value); + if (kHasUnalignedAccess) { + new (p) Unaligned(value); + } else { + memcpy(p, &value, sizeof(T)); + } } } // namespace folly diff --git a/folly/Portability.h b/folly/Portability.h index 63dcc5cf..dc33d3b4 100644 --- a/folly/Portability.h +++ b/folly/Portability.h @@ -48,9 +48,14 @@ #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. diff --git a/folly/SpookyHashV2.cpp b/folly/SpookyHashV2.cpp index d55a62be..b945bc6f 100644 --- a/folly/SpookyHashV2.cpp +++ b/folly/SpookyHashV2.cpp @@ -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) { diff --git a/folly/configure.ac b/folly/configure.ac index 2b52035a..62c81230 100644 --- a/folly/configure.ac +++ b/folly/configure.ac @@ -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(