<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 - clang incorrectly produces ARM STM instead of STR in thumb mode"
   href="https://bugs.llvm.org/show_bug.cgi?id=50785">50785</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang incorrectly produces ARM STM instead of STR in thumb mode
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>12.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>release blocker
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>C
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>matthieu.bouron@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When compiling to ARM (32-bit) thumb mode, clang incorrectly produces stm
instead of str in some cases. This is causing issues (sigbus: illegal
alignment) if the destination pointer is not aligned.

This can be reproduced with the following minimal C code:

    #include <stdint.h>

    uint8_t *f(uint8_t *buf)
    {
        *(uint32_t*)buf = 0;
        return buf + 4;
    }

which, when compiled with -O2 -mthumb, produces the following ARM ASM:

    f:
        movs    r1, #0
        stm     r0!, {r1}
        bx      lr

whereas GCC (or clang without -mthumb) produces:

    f:
        movs    r3, #0
        str     r3, [r0], #4
        bx      lr


This happens on clang 9.x, 10.x, 11.x and 12.x as well as the versions provided
by the Android NDK (r21e, r22b).

This issue was found when debugging a crash occurring in libjpeg-turbo 2.1.0
when compiled to ARM (32-bit) thumb node / Android. The crash happens at:
<a href="https://github.com/libjpeg-turbo/libjpeg-turbo/blob/1a1fb615db39880044b789bdb36b351865d9ec4a/simd/arm/jchuff.h#L84">https://github.com/libjpeg-turbo/libjpeg-turbo/blob/1a1fb615db39880044b789bdb36b351865d9ec4a/simd/arm/jchuff.h#L84</a>
(called by
<a href="https://github.com/libjpeg-turbo/libjpeg-turbo/blob/1a1fb615db39880044b789bdb36b351865d9ec4a/simd/arm/aarch32/jchuff-neon.c#L295">https://github.com/libjpeg-turbo/libjpeg-turbo/blob/1a1fb615db39880044b789bdb36b351865d9ec4a/simd/arm/aarch32/jchuff-neon.c#L295</a>)

where:

    *((uint32_t *)buffer) = BUILTIN_BSWAP32(put_buffer);
    buffer += 4;

gets compiled to rev + stm instead of rev + str, causing a sigbus error
(illegal alignment) because buffer is not necessarily aligned.</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>