ndk icon indicating copy to clipboard operation
ndk copied to clipboard

Compiler optimizations cause incorrect results in certain CPU archs

Open bashok001 opened this issue 8 years ago • 2 comments

Description

When compiler optimizations are turned on, armeabi architecture running on a 64 bit phones is giving wrong outputs for bit shift operations.

This is getting triggered when running 32 bit binaries, compiled with any optimization flags (-O1 to -Ofast), on 64 bit arm architectures performing bitwise operations (specifically shift and xor), likely when the memory operated on, is not properly aligned on a long word boundary.

https://github.com/bashok001/bitshift-arm-ndk-repro

Environment Details

  • NDK Version: 16.1.4479499
  • Build sytem: cmake
  • Host OS: OS X
  • ABI: armeabi
  • STL: gnustl_static
  • NDK API level: Any
  • Device API level: Any

bashok001 avatar Jan 25 '18 23:01 bashok001

armeabi isn't in the NDK any more.

DanAlbert avatar Jan 25 '18 23:01 DanAlbert

I was curious about this bug, so I looked at it. I don't think there's anything for us to do in the NDK, and it looks like upstream Clang is already fixed. Switching from the armeabi ABI to armeabi-v7a avoids the problem.

I reduced the test case to:

/*
$ /ssd/stand-arm-14-libc++-r16b/bin/clang -pie -fPIE miscompile-github-629-armeabi-shift.c -Os -mthumb -march=armv5 && arun ./a.out
./a.out: 1 file pushed. 1.6 MB/s (6620 bytes in 0.004s)
expected: 0xc0000300
actual:   0x3c0000300
*/

#include <stdint.h>
#include <stdio.h>

__attribute__((weak))
uint64_t bug(uint64_t x0) {
  uint64_t x1 = x0 + 0x100;
  uint64_t x2 = x1 ^ (x1 << 1);
  return x2;
}

int main() {
  uint64_t x0 = 0x40000000;
  uint64_t expected = (x0 + 0x100) ^ ((x0 + 0x100) << 1);
  uint64_t actual = bug(x0);
  printf("expected: 0x%llx\n", expected);
  printf("actual:   0x%llx\n", actual);
  return 0;
}

It passes when compiled for armeabi-v7a or arm64-v8a. It only fails when compiled for armeabi (ARMv5) in Thumb mode. It fails equally on 32-bit and 64-bit ARM devices.

The assembly Clang generates is wrong: for a 64-bit addition, it emits an ADDS/ADCS pair. ADDS sets the Carry flag (in the CPSR register), and ADCS uses Carry. The Clang in r16b fails to track ADCS's usage of CPSR, so instructions end up between the two that clobber it. Newer versions of Clang track ADCS's CPSR usage correctly. The r16b Clang also appears to mark CPSR usage correctly for armeabi-v7a's Thumb2 ADC instruction.

With a brief look at the version history, I'd guess this change was the fix: https://reviews.llvm.org/D31815

Knowing the problem, I can write a better repro case zeroing in on the problem:

/*
$ /ssd/stand-arm-14-r16b/bin/clang -pie -fPIE adc-bug.c -Os -march=armv5 -mthumb && arun ./a.out
./a.out: 1 file pushed. 1.5 MB/s (6592 bytes in 0.004s)
expected: 5
actual:   4
*/

#include <stdint.h>
#include <stdio.h>

__attribute__((weak))
uint32_t bug(uint64_t x, uint32_t y) {
  return (x + y) >> 32;
}

int main() {
  bug(0, 0); // clear carry bit
  uint64_t actual = bug(0x480000000, 0x80000000);
  printf("expected: %d\n", 5);
  printf("actual:   %lld\n", (long long)actual);
  return 0;
}

The compiler optimizes away the addition of the low-order words. It compiles to:

bug:
	movs	r0, #0
	adcs	r0, r1
	bx	lr

It looks like this LLVM bug only affects ARMv5/armeabi, which is deprecated in NDK r16 and removed in r17.

rprichard avatar Jan 26 '18 10:01 rprichard