From dc85be5456ec2ca081ef6e94c38cbf3d78addee2 Mon Sep 17 00:00:00 2001 From: Eddie Kohler Date: Fri, 26 Jun 2020 21:12:51 -0700 Subject: [PATCH] Mark Keccak functions that contain expected misaligned accesses. To avoid undefined behavior warnings for those accesses. --- ext/hash/sha3/generic32lc/KeccakP-1600-inplace32BI.c | 12 ++++++++++++ ext/hash/sha3/generic64lc/KeccakP-1600-opt64.c | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/ext/hash/sha3/generic32lc/KeccakP-1600-inplace32BI.c b/ext/hash/sha3/generic32lc/KeccakP-1600-inplace32BI.c index b566291220..74ade78eb5 100644 --- a/ext/hash/sha3/generic32lc/KeccakP-1600-inplace32BI.c +++ b/ext/hash/sha3/generic32lc/KeccakP-1600-inplace32BI.c @@ -17,6 +17,14 @@ http://creativecommons.org/publicdomain/zero/1.0/ #include "brg_endian.h" #include "KeccakP-1600-SnP.h" #include "SnP-Relaned.h" +#ifdef __has_feature +# if __has_feature(undefined_behavior_sanitizer) +# define ALLOW_MISALIGNED_ACCESS __attribute__((no_sanitize("alignment"))) +# endif +#endif +#ifndef ALLOW_MISALIGNED_ACCESS +# define ALLOW_MISALIGNED_ACCESS +#endif typedef unsigned char UINT8; typedef unsigned int UINT32; @@ -162,6 +170,7 @@ void KeccakP1600_AddBytesInLane(void *state, unsigned int lanePosition, const un /* ---------------------------------------------------------------- */ +ALLOW_MISALIGNED_ACCESS void KeccakP1600_AddLanes(void *state, const unsigned char *data, unsigned int laneCount) { #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) @@ -217,6 +226,7 @@ void KeccakP1600_OverwriteBytesInLane(void *state, unsigned int lanePosition, co /* ---------------------------------------------------------------- */ +ALLOW_MISALIGNED_ACCESS void KeccakP1600_OverwriteLanes(void *state, const unsigned char *data, unsigned int laneCount) { #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) @@ -304,6 +314,7 @@ void KeccakP1600_ExtractBytesInLane(const void *state, unsigned int lanePosition /* ---------------------------------------------------------------- */ +ALLOW_MISALIGNED_ACCESS void KeccakP1600_ExtractLanes(const void *state, unsigned char *data, unsigned int laneCount) { #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) @@ -378,6 +389,7 @@ void KeccakP1600_ExtractAndAddBytesInLane(const void *state, unsigned int lanePo /* ---------------------------------------------------------------- */ +ALLOW_MISALIGNED_ACCESS void KeccakP1600_ExtractAndAddLanes(const void *state, const unsigned char *input, unsigned char *output, unsigned int laneCount) { #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) diff --git a/ext/hash/sha3/generic64lc/KeccakP-1600-opt64.c b/ext/hash/sha3/generic64lc/KeccakP-1600-opt64.c index 40853ff997..fa708ad833 100644 --- a/ext/hash/sha3/generic64lc/KeccakP-1600-opt64.c +++ b/ext/hash/sha3/generic64lc/KeccakP-1600-opt64.c @@ -17,6 +17,14 @@ http://creativecommons.org/publicdomain/zero/1.0/ #include #include "brg_endian.h" #include "KeccakP-1600-opt64-config.h" +#ifdef __has_feature +# if __has_feature(undefined_behavior_sanitizer) +# define ALLOW_MISALIGNED_ACCESS __attribute__((no_sanitize("alignment"))) +# endif +#endif +#ifndef ALLOW_MISALIGNED_ACCESS +# define ALLOW_MISALIGNED_ACCESS +#endif typedef unsigned char UINT8; typedef unsigned long long int UINT64; @@ -114,6 +122,7 @@ void KeccakP1600_AddBytesInLane(void *state, unsigned int lanePosition, const un /* ---------------------------------------------------------------- */ +ALLOW_MISALIGNED_ACCESS void KeccakP1600_AddLanes(void *state, const unsigned char *data, unsigned int laneCount) { #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) @@ -462,6 +471,7 @@ void KeccakP1600_ExtractAndAddBytes(const void *state, const unsigned char *inpu /* ---------------------------------------------------------------- */ +ALLOW_MISALIGNED_ACCESS size_t KeccakF1600_FastLoop_Absorb(void *state, unsigned int laneCount, const unsigned char *data, size_t dataByteLen) { size_t originalDataByteLen = dataByteLen; -- 2.40.0