<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 - - -fsanitize=undefined doesn't pay attention to __builtin_assume_aligned()"
   href="https://bugs.llvm.org/show_bug.cgi?id=34213">34213</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>- -fsanitize=undefined doesn't pay attention to __builtin_assume_aligned()
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>4.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>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kobalicek.petr@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I'm having problem with -fsanitize=undefined and __builtin_assume_aligned()
builtin.

The following code `sanitizer-test.cpp`:

  #include <stdint.h>

  static __attribute((__noinline__)) uint32_t readu32(const void* p) {
    p = __builtin_assume_aligned(p, 1);
    return static_cast<const uint32_t*>(p)[0];
  }

  static __attribute((__noinline__)) void writeu32(void* p, uint32_t x) {
    p = __builtin_assume_aligned(p, 1);
    static_cast<uint32_t*>(p)[0] = x;
  }

  int main(int argc, char* argv[]) {
    char buf[] = { 0, 1, 2, 3, 4, 5, 6 };
    writeu32(buf + 1, 0x44332211);
    uint32_t ret = readu32(buf + 1);
    return static_cast<int>(ret);
  }

Compiled as:

  clang++-4.0 -fsanitize=undefined sanitizer-test.cpp -o sanitizer-test

Outputs the following when executed:

$ ./sanitizer-test
sanitizer-test.cpp:10:3: runtime error: store to misaligned address
0x7ffcdb09e13a for type 'uint32_t' (aka 'unsigned int'), which requires 4 byte
alignment
0x7ffcdb09e13a: note: pointer points here
 00 00  00 00 01 02 03 04 05 06  38 e2 09 db fc 7f 00 00  01 00 00 00 00 00 00
00  a0 b3 42 00 00 00
              ^ 
sanitizer-test.cpp:5:10: runtime error: load of misaligned address
0x7ffcdb09e13a for type 'const uint32_t' (aka 'const unsigned int'), which
requires 4 byte alignment
0x7ffcdb09e13a: note: pointer points here
 00 00  00 00 11 22 33 44 05 06  38 e2 09 db fc 7f 00 00  01 00 00 00 00 00 00
00  a0 b3 42 00 00 00

I think that in this case the sanitizer should not report the runtime error as
the pointer was marked to be aligned to 1 byte.

I reported the same also on GCC's bugzilla as I tested both gcc and clang:
  <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81870">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81870</a></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>