From fdfcb2a172215c605261a34d7e8d9097d7a3c11c Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Fri, 11 Jan 2019 16:37:26 -0500 Subject: [PATCH] Make AlignedAllocate available in simulator debug builds (GH #779, PR #780) * Make AlignedAllocate available in simulator debug builds (GH #779) * Use CRYPTOPP_CONSTANT for rotate params --- misc.cpp | 4 ---- misc.h | 72 +++++++++++++++++++++++++++++++------------------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/misc.cpp b/misc.cpp index 6752a81a..12d383fc 100644 --- a/misc.cpp +++ b/misc.cpp @@ -280,8 +280,6 @@ void CallNewHandler() throw std::bad_alloc(); } -#if CRYPTOPP_BOOL_ALIGN16 - void * AlignedAllocate(size_t size) { byte *p; @@ -320,8 +318,6 @@ void AlignedDeallocate(void *p) #endif } -#endif // CRYPTOPP_BOOL_ALIGN16 - void * UnalignedAllocate(size_t size) { void *p; diff --git a/misc.h b/misc.h index 450c147a..3d45bf04 100644 --- a/misc.h +++ b/misc.h @@ -632,7 +632,7 @@ template std::string IntToString(T value, unsigned int base = 10) { // Hack... set the high bit for uppercase. - static const unsigned int HIGH_BIT = (1U << 31); + const unsigned int HIGH_BIT = (1U << 31); const char CH = !!(base & HIGH_BIT) ? 'A' : 'a'; base &= ~HIGH_BIT; @@ -1156,6 +1156,7 @@ inline CipherDir GetCipherDir(const T &obj) /// to free memory. There is no guarantee CallNewHandler will be able to procure more memory so /// an allocation succeeds. If the call to set_new_handler fails, then CallNewHandler throws /// a bad_alloc exception. +/// \sa AlignedAllocate, AlignedDeallocate, UnalignedAllocate, UnalignedDeallocate CRYPTOPP_DLL void CRYPTOPP_API CallNewHandler(); /// \brief Performs an addition with carry on a block of bytes @@ -1380,37 +1381,44 @@ std::string StringNarrow(const wchar_t *str, bool throwOnError = true); /// then a 0x21 error is returned on Windows which eventually results in an InvalidArgument() exception. std::wstring StringWiden(const char *str, bool throwOnError = true); -#ifdef CRYPTOPP_DOXYGEN_PROCESSING - /// \brief Allocates a buffer on 16-byte boundary /// \param size the size of the buffer -/// \details AlignedAllocate is primarily used when the data will be proccessed by MMX, SSE2 and NEON -/// instructions. The assembly language routines rely on the alignment. If the alignment is not +/// \details AlignedAllocate is primarily used when the data will be +/// proccessed by SSE, NEON, ARMv8 or PowerPC instructions. The assembly +/// language routines rely on the alignment. If the alignment is not /// respected, then a SIGBUS could be generated on Unix and Linux, and an /// EXCEPTION_DATATYPE_MISALIGNMENT could be generated on Windows. -/// \note AlignedAllocate and AlignedDeallocate are available when CRYPTOPP_BOOL_ALIGN16 is -/// defined. CRYPTOPP_BOOL_ALIGN16 is defined in config.h +/// \details Formerly, AlignedAllocate and AlignedDeallocate were only +/// available on certain platforms when CRYTPOPP_DISABLE_ASM was not in +/// effect. However, Android and iOS debug simulator builds got into a +/// state where the aligned allocator was not available and caused link +/// failures. +/// \since AlignedAllocate for SIMD since Crypto++ 1.0, AlignedAllocate +/// for all builds since Crypto++ 8.1 +/// \sa AlignedDeallocate, UnalignedAllocate, UnalignedDeallocate, CallNewHandler, +/// Issue 779 CRYPTOPP_DLL void* CRYPTOPP_API AlignedAllocate(size_t size); /// \brief Frees a buffer allocated with AlignedAllocate /// \param ptr the buffer to free -/// \note AlignedAllocate and AlignedDeallocate are available when CRYPTOPP_BOOL_ALIGN16 is -/// defined. CRYPTOPP_BOOL_ALIGN16 is defined in config.h +/// \since AlignedDeallocate for SIMD since Crypto++ 1.0, AlignedAllocate +/// for all builds since Crypto++ 8.1 +/// \sa AlignedAllocate, UnalignedAllocate, UnalignedDeallocate, CallNewHandler, +/// Issue 779 CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *ptr); -#endif // CRYPTOPP_DOXYGEN_PROCESSING - -#if CRYPTOPP_BOOL_ALIGN16 -CRYPTOPP_DLL void* CRYPTOPP_API AlignedAllocate(size_t size); -CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *ptr); -#endif // CRYPTOPP_BOOL_ALIGN16 - /// \brief Allocates a buffer /// \param size the size of the buffer +/// \since Crypto++ 1.0 +/// \sa AlignedAllocate, AlignedDeallocate, UnalignedDeallocate, CallNewHandler, +/// Issue 779 CRYPTOPP_DLL void * CRYPTOPP_API UnalignedAllocate(size_t size); /// \brief Frees a buffer allocated with UnalignedAllocate /// \param ptr the buffer to free +/// \since Crypto++ 1.0 +/// \sa AlignedAllocate, AlignedDeallocate, UnalignedAllocate, CallNewHandler, +/// Issue 779 CRYPTOPP_DLL void CRYPTOPP_API UnalignedDeallocate(void *ptr); // ************** rotate functions *************** @@ -1436,8 +1444,8 @@ template inline T rotlConstant(T x) // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57157, // http://software.intel.com/en-us/forums/topic/580884 // and http://llvm.org/bugs/show_bug.cgi?id=24226 - static const unsigned int THIS_SIZE = sizeof(T)*8; - static const unsigned int MASK = THIS_SIZE-1; + CRYPTOPP_CONSTANT(THIS_SIZE = sizeof(T)*8) + CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1) CRYPTOPP_ASSERT(R < THIS_SIZE); return T((x<>(-R&MASK))); } @@ -1462,8 +1470,8 @@ template inline T rotrConstant(T x) // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57157, // http://software.intel.com/en-us/forums/topic/580884 // and http://llvm.org/bugs/show_bug.cgi?id=24226 - static const unsigned int THIS_SIZE = sizeof(T)*8; - static const unsigned int MASK = THIS_SIZE-1; + CRYPTOPP_CONSTANT(THIS_SIZE = sizeof(T)*8) + CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1) CRYPTOPP_ASSERT(R < THIS_SIZE); return T((x >> R)|(x<<(-R&MASK))); } @@ -1487,8 +1495,8 @@ template inline T rotlFixed(T x, unsigned int y) // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57157, // http://software.intel.com/en-us/forums/topic/580884 // and http://llvm.org/bugs/show_bug.cgi?id=24226 - static const unsigned int THIS_SIZE = sizeof(T)*8; - static const unsigned int MASK = THIS_SIZE-1; + CRYPTOPP_CONSTANT(THIS_SIZE = sizeof(T)*8) + CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1) CRYPTOPP_ASSERT(y < THIS_SIZE); return T((x<>(-y&MASK))); } @@ -1512,8 +1520,8 @@ template inline T rotrFixed(T x, unsigned int y) // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57157, // http://software.intel.com/en-us/forums/topic/580884 // and http://llvm.org/bugs/show_bug.cgi?id=24226 - static const unsigned int THIS_SIZE = sizeof(T)*8; - static const unsigned int MASK = THIS_SIZE-1; + CRYPTOPP_CONSTANT(THIS_SIZE = sizeof(T)*8) + CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1) CRYPTOPP_ASSERT(y < THIS_SIZE); return T((x >> y)|(x<<(-y&MASK))); } @@ -1532,8 +1540,8 @@ template inline T rotrFixed(T x, unsigned int y) /// \since Crypto++ 3.0 template inline T rotlVariable(T x, unsigned int y) { - static const unsigned int THIS_SIZE = sizeof(T)*8; - static const unsigned int MASK = THIS_SIZE-1; + CRYPTOPP_CONSTANT(THIS_SIZE = sizeof(T)*8) + CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1) CRYPTOPP_ASSERT(y < THIS_SIZE); return T((x<>(-y&MASK))); } @@ -1552,8 +1560,8 @@ template inline T rotlVariable(T x, unsigned int y) /// \since Crypto++ 3.0 template inline T rotrVariable(T x, unsigned int y) { - static const unsigned int THIS_SIZE = sizeof(T)*8; - static const unsigned int MASK = THIS_SIZE-1; + CRYPTOPP_CONSTANT(THIS_SIZE = sizeof(T)*8) + CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1) CRYPTOPP_ASSERT(y < THIS_SIZE); return T((x>>y)|(x<<(-y&MASK))); } @@ -1569,8 +1577,8 @@ template inline T rotrVariable(T x, unsigned int y) /// \since Crypto++ 3.0 template inline T rotlMod(T x, unsigned int y) { - static const unsigned int THIS_SIZE = sizeof(T)*8; - static const unsigned int MASK = THIS_SIZE-1; + CRYPTOPP_CONSTANT(THIS_SIZE = sizeof(T)*8) + CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1) return T((x<<(y&MASK))|(x>>(-y&MASK))); } @@ -1585,8 +1593,8 @@ template inline T rotlMod(T x, unsigned int y) /// \since Crypto++ 3.0 template inline T rotrMod(T x, unsigned int y) { - static const unsigned int THIS_SIZE = sizeof(T)*8; - static const unsigned int MASK = THIS_SIZE-1; + CRYPTOPP_CONSTANT(THIS_SIZE = sizeof(T)*8) + CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1) return T((x>>(y&MASK))|(x<<(-y&MASK))); }