https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81870

            Bug ID: 81870
           Summary: -fsanitize=undefined doesn't pay attention to
                    __builtin_assume_aligned()
           Product: gcc
           Version: 7.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kobalicek.petr at gmail dot com
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at 
gcc dot gnu.org
  Target Milestone: ---

I'm having problem with GCC -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:

  gcc-7 -fsanitize=undefined sanitizer-test.cpp -o sanitizer-test

Outputs the following when executed:

$ ./sanitizer-test
sanitizer-test.cpp:10:32: runtime error: store to misaligned address
0x7ffd643f6ab6 for type 'uint32_t', which requires 4 byte alignment
0x7ffd643f6ab6: note: pointer points here
 3f 64 fd 00 01 02  03 04 05 06 00 00 00 00  60 b8 a8 09 b3 55 00 00  b1 f2 ab
be 80 7f 00 00  01 00
             ^ 
sanitizer-test.cpp:5:43: runtime error: load of misaligned address
0x7ffd643f6ab6 for type 'const uint32_t', which requires 4 byte alignment
0x7ffd643f6ab6: note: pointer points here
 3f 64 fd 00 11 22  33 44 05 06 00 00 00 00  60 b8 a8 09 b3 55 00 00  b1 f2 ab
be 80 7f 00 00  01 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.

Reply via email to