Fix RocksDB SIGILL error on Raspberry PI 4 (#7233)

Summary:
Issue:https://github.com/facebook/rocksdb/issues/7042

No PMULL runtime check will lead to SIGILL on a Raspberry pi 4.

Leverage 'getauxval' to get Hardware-Cap to detect whether target
platform does support PMULL or not in runtime.

Consider the condition that the target platform does support crc32 but not support PMULL.
In this condition, the code should leverage the crc32 instruction
rather than skip all hardware crc32 instruction.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/7233

Reviewed By: jay-zhuang

Differential Revision: D23790116

fbshipit-source-id: a3ebd821fbd4a38dd2f59064adbb7c3013ee8140
main
Yuqi Gu 4 years ago committed by Facebook GitHub Bot
parent 3591da33c0
commit 29f7bbef99
  1. 6
      util/crc32c.cc
  2. 23
      util/crc32c_arm64.cc
  3. 1
      util/crc32c_arm64.h

@ -41,6 +41,10 @@
#endif #endif
#if defined(__linux__) && defined(HAVE_ARM64_CRC)
bool pmull_runtime_flag = false;
#endif
namespace ROCKSDB_NAMESPACE { namespace ROCKSDB_NAMESPACE {
namespace crc32c { namespace crc32c {
@ -494,6 +498,7 @@ std::string IsFastCrc32Supported() {
if (crc32c_runtime_check()) { if (crc32c_runtime_check()) {
has_fast_crc = true; has_fast_crc = true;
arch = "Arm64"; arch = "Arm64";
pmull_runtime_flag = crc32c_pmull_runtime_check();
} else { } else {
has_fast_crc = false; has_fast_crc = false;
arch = "Arm64"; arch = "Arm64";
@ -1224,6 +1229,7 @@ static inline Function Choose_Extend() {
return isAltiVec() ? ExtendPPCImpl : ExtendImpl<Slow_CRC32>; return isAltiVec() ? ExtendPPCImpl : ExtendImpl<Slow_CRC32>;
#elif defined(__linux__) && defined(HAVE_ARM64_CRC) #elif defined(__linux__) && defined(HAVE_ARM64_CRC)
if(crc32c_runtime_check()) { if(crc32c_runtime_check()) {
pmull_runtime_flag = crc32c_pmull_runtime_check();
return ExtendARMImpl; return ExtendARMImpl;
} else { } else {
return ExtendImpl<Slow_CRC32>; return ExtendImpl<Slow_CRC32>;

@ -14,6 +14,9 @@
#ifndef HWCAP_CRC32 #ifndef HWCAP_CRC32
#define HWCAP_CRC32 (1 << 7) #define HWCAP_CRC32 (1 << 7)
#endif #endif
#ifndef HWCAP_PMULL
#define HWCAP_PMULL (1 << 4)
#endif
#ifdef HAVE_ARM64_CRYPTO #ifdef HAVE_ARM64_CRYPTO
/* unfolding to compute 8 * 3 = 24 bytes parallelly */ /* unfolding to compute 8 * 3 = 24 bytes parallelly */
@ -35,6 +38,8 @@
} while (0) } while (0)
#endif #endif
extern bool pmull_runtime_flag;
uint32_t crc32c_runtime_check(void) { uint32_t crc32c_runtime_check(void) {
#ifdef ROCKSDB_AUXV_GETAUXVAL_PRESENT #ifdef ROCKSDB_AUXV_GETAUXVAL_PRESENT
uint64_t auxv = getauxval(AT_HWCAP); uint64_t auxv = getauxval(AT_HWCAP);
@ -44,6 +49,15 @@ uint32_t crc32c_runtime_check(void) {
#endif #endif
} }
bool crc32c_pmull_runtime_check(void) {
#ifdef ROCKSDB_AUXV_GETAUXVAL_PRESENT
uint64_t auxv = getauxval(AT_HWCAP);
return (auxv & HWCAP_PMULL) != 0;
#else
return false;
#endif
}
#ifdef ROCKSDB_UBSAN_RUN #ifdef ROCKSDB_UBSAN_RUN
#if defined(__clang__) #if defined(__clang__)
__attribute__((__no_sanitize__("alignment"))) __attribute__((__no_sanitize__("alignment")))
@ -58,6 +72,13 @@ uint32_t crc32c_arm64(uint32_t crc, unsigned char const *data,
int length = (int)len; int length = (int)len;
crc ^= 0xffffffff; crc ^= 0xffffffff;
/*
* Pmull runtime check here.
* Raspberry Pi supports crc32 but doesn't support pmull.
* Skip Crc32c Parallel computation if no crypto extension available.
*/
if (pmull_runtime_flag) {
/* Macro (HAVE_ARM64_CRYPTO) is used for compiling check */
#ifdef HAVE_ARM64_CRYPTO #ifdef HAVE_ARM64_CRYPTO
/* Crc32c Parallel computation /* Crc32c Parallel computation
* Algorithm comes from Intel whitepaper: * Algorithm comes from Intel whitepaper:
@ -113,6 +134,8 @@ uint32_t crc32c_arm64(uint32_t crc, unsigned char const *data,
if (length == 0) return crc ^ (0xffffffffU); if (length == 0) return crc ^ (0xffffffffU);
#endif #endif
} // if Pmull runtime check here
buf8 = (const uint8_t *)buf64; buf8 = (const uint8_t *)buf64;
while (length >= 8) { while (length >= 8) {
crc = crc32c_u64(crc, *(const uint64_t *)buf8); crc = crc32c_u64(crc, *(const uint64_t *)buf8);

@ -35,6 +35,7 @@
extern uint32_t crc32c_arm64(uint32_t crc, unsigned char const *data, unsigned len); extern uint32_t crc32c_arm64(uint32_t crc, unsigned char const *data, unsigned len);
extern uint32_t crc32c_runtime_check(void); extern uint32_t crc32c_runtime_check(void);
extern bool crc32c_pmull_runtime_check(void);
#ifdef __ARM_FEATURE_CRYPTO #ifdef __ARM_FEATURE_CRYPTO
#define HAVE_ARM64_CRYPTO #define HAVE_ARM64_CRYPTO

Loading…
Cancel
Save