<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Enabling -fsanitize=integer generates wrong assembly in certain cases for AArch64"
   href="https://bugs.llvm.org/show_bug.cgi?id=40104">40104</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Enabling -fsanitize=integer generates wrong assembly in certain cases for AArch64
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>7.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>yschandra@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=21254" name="attach_21254" title="source, preprocessed source, asm dump from objects">attachment 21254</a> <a href="attachment.cgi?id=21254&action=edit" title="source, preprocessed source, asm dump from objects">[details]</a></span>
source, preprocessed source, asm dump from objects

I have Android N based source code which I build using clang-6, most of the
parts have integer sanitizer "-fsanitize=integer" enabled and code is targeted
for AArch64 (A57). Clang-6 builds work good, however when I try to use
clang-7.0.0 or the trunk in certain cases where open() system call is involved
clang-7 and latest trunk generated wrong assembly code.

For following code:

    int fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
    if (fd == -1) {
        ERROR("Failed to open %s: %s\n", DEV_NAME, strerror(errno));
        return 1;
    }

Generated code is

; int fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
      64: 00 00 00 90   adrp  x0, #0
      68: 00 00 00 91   add x0, x0, #0
      6c: 41 00 80 52   mov w1, #2
      70: 01 01 a0 72   movk  w1, #8, lsl #16
      74: 00 00 00 94   bl  #0 <_Z14watchdogd_mainiPPc+0x74>
; if (fd == -1) {
      78: 1f 04 00 31   cmn w0, #1
      7c: e1 01 00 54   b.ne  #60 <_Z14watchdogd_mainiPPc+0xb8>
; ERROR("Failed to open %s: %s\n", DEV_NAME, strerror(errno));
      80: 00 00 00 94   bl  #0 <_Z14watchdogd_mainiPPc+0x80>
      84: 00 00 40 b9   ldr w0, [x0]
      88: 00 00 00 94   bl  #0 <_Z14watchdogd_mainiPPc+0x88>
      8c: e2 03 00 aa   mov x2, x0
      90: 00 00 00 90   adrp  x0, #0
      94: 00 00 00 91   add x0, x0, #0
      98: 01 00 00 90   adrp  x1, #0
      9c: 21 00 00 91   add x1, x1, #0
      a0: 00 00 00 94   bl  #0 <_Z14watchdogd_mainiPPc+0xa0>
; return 1;

The fd check for -1 at address 7c: b.ne goes to abort() and actually it should
have been b.eq instruction and there is no code generated for logic after if
statement (see attached watchdogd.cpp and watchdogd-prep7-clang7.asm)

      7c: e1 01 00 54   b.ne  #60 <_Z14watchdogd_mainiPPc+0xb8>

This is readily not reproducible but in my build I have 3 different places
where this happens, one of them is watchdogd implementation
(<a href="https://android.googlesource.com/platform/system/core/+/nougat-release/init/watchdogd.cpp">https://android.googlesource.com/platform/system/core/+/nougat-release/init/watchdogd.cpp</a>)

I have here attached the watchdogd.cpp taken out from my build and a makefile
which has exact compiler flags I use to compile watchdogd.cpp in my build.
Since this code depends on bionic C, standalone compilation is difficult so, I
have generated pre-processor output so that it can be compiled standalone.

Also if I remove -fsanitizer=integer option code generated is good and issue is
not seen if I use clang-6.

Files Attached (in side watchdogd.tar.gz):
1) watchdogd.cpp - Actual Source code
2) watchdogd-prep6.cpp - Preprocessed code using clang-6
3) watchdogd-prep7.cpp - Preprocessed code using clang-7
4) watchdogd-prep6-clang6.asm - Object text Dump clang-6 preprocessed code
compiled with clang-6
5) watchdogd-prep6-clang7.asm - Object text Dump clang-6 preprocessed code
compiled with clang-7 
6) watchdogd-prep7-clang6.asm - Object text Dump clang-7 preprocessed code
compiled with clang-6
7) watchdogd-prep7-clang7.asm - Object text Dump clang-7 preprocessed code
compiled with clang-7
8) compile.mk - Makefile for compiling

To recompile pre-processed use (fix compiler paths in compile.mk)
   make -f compile.mk dump

If you would like compile watchdogd.cpp you will need bionic c
(<a href="https://android.googlesource.com/platform/bionic/+/nougat-release/libc/">https://android.googlesource.com/platform/bionic/+/nougat-release/libc/</a>) and
fix paths in compile.mk</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>