Skip to content

Commit d4cd6cd

Browse files
stephenkyle-ARMeustas
authored andcommitted
platform: fix unaligned 64-bit accesses on AArch32 (#702)
Ensures that Aarch32 Arm builds with an Armv8 compiler do not set BROTLI_64_BITS. This scenario is possible with ChromeOS builds, as they may use a toolchain with the target armv7-cros-gnueabi, but with -march=armv8. This will set __ARM_ARCH to 8 (defining BROTLI_TARGET_ARMV8), but will also set __ARM_32BIT_STATE and not __ARM_64BIT_STATE. Without this, illegal 64-bit non-word-aligned reads (LDRD) may be emitted. Also fix unaligned 64-bit reads on AArch32 - STRD was still possible to emit.
1 parent 8a073bd commit d4cd6cd

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

c/common/platform.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,14 @@ To apply compiler hint, enclose the branching condition into macros, like this:
187187

188188
#if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \
189189
defined(__aarch64__) || defined(__ARM64_ARCH_8__)
190-
#define BROTLI_TARGET_ARMV8
190+
#define BROTLI_TARGET_ARMV8_ANY
191+
192+
#if defined(__ARM_32BIT_STATE)
193+
#define BROTLI_TARGET_ARMV8_32
194+
#elif defined(__ARM_64BIT_STATE)
195+
#define BROTLI_TARGET_ARMV8_64
196+
#endif
197+
191198
#endif /* ARMv8 */
192199

193200
#if defined(__i386) || defined(_M_IX86)
@@ -210,7 +217,7 @@ To apply compiler hint, enclose the branching condition into macros, like this:
210217
#define BROTLI_64_BITS 1
211218
#elif defined(BROTLI_BUILD_32_BIT)
212219
#define BROTLI_64_BITS 0
213-
#elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8) || \
220+
#elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \
214221
defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64)
215222
#define BROTLI_64_BITS 1
216223
#else
@@ -261,7 +268,7 @@ To apply compiler hint, enclose the branching condition into macros, like this:
261268
#if defined(BROTLI_BUILD_PORTABLE)
262269
#define BROTLI_ALIGNED_READ (!!1)
263270
#elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \
264-
defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8) || \
271+
defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \
265272
defined(BROTLI_TARGET_RISCV64)
266273
/* Allow unaligned read only for white-listed CPUs. */
267274
#define BROTLI_ALIGNED_READ (!!0)
@@ -306,15 +313,29 @@ static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
306313
}
307314
#else /* BROTLI_64_BITS */
308315
/* Avoid emitting LDRD / STRD, which require properly aligned address. */
316+
/* If __attribute__(aligned) is available, use that. Otherwise, memcpy. */
317+
318+
#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
319+
typedef __attribute__((aligned(1))) uint64_t unaligned_uint64_t;
320+
309321
static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
310-
const uint32_t* dwords = (const uint32_t*)p;
311-
return dwords[0] | ((uint64_t)dwords[1] << 32);
322+
return (uint64_t) ((unaligned_uint64_t*) p)[0];
312323
}
313324
static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
314-
uint32_t* dwords = (uint32_t *)p;
315-
dwords[0] = (uint32_t)v;
316-
dwords[1] = (uint32_t)(v >> 32);
325+
unaligned_uint64_t* dwords = (unaligned_uint64_t*) p;
326+
dwords[0] = (unaligned_uint64_t) v;
327+
}
328+
#else /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */
329+
static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
330+
uint64_t v;
331+
memcpy(&v, p, sizeof(uint64_t));
332+
return v;
333+
}
334+
335+
static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
336+
memcpy(p, &v, sizeof(uint64_t));
317337
}
338+
#endif /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */
318339
#endif /* BROTLI_64_BITS */
319340
#endif /* BROTLI_ALIGNED_READ */
320341

@@ -400,7 +421,7 @@ static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
400421
#define BROTLI_IS_CONSTANT(x) (!!0)
401422
#endif
402423

403-
#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8)
424+
#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
404425
#define BROTLI_HAS_UBFX (!!1)
405426
#else
406427
#define BROTLI_HAS_UBFX (!!0)
@@ -427,7 +448,7 @@ static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) {
427448
/* TODO: add appropriate icc/sunpro/arm/ibm/ti checks. */
428449
#if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \
429450
!defined(BROTLI_BUILD_NO_RBIT)
430-
#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8)
451+
#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
431452
/* TODO: detect ARMv6T2 and enable this code for it. */
432453
static BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) {
433454
brotli_reg_t output;

0 commit comments

Comments
 (0)