[llvm-bugs] [Bug 33990] New: clang -O2 miscompiles pointer arithmetics

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Jul 29 19:28:11 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=33990

            Bug ID: 33990
           Summary: clang -O2 miscompiles pointer arithmetics
           Product: new-bugs
           Version: 4.0
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: rhash.admin at gmail.com
                CC: llvm-bugs at lists.llvm.org

Created attachment 18870
  --> https://bugs.llvm.org/attachment.cgi?id=18870&action=edit
the miscompiled file

Hi,

I've got clang miscompilation bug on 64-bit Ubuntu 17.04 with its default clang
4.0.

Bug description:
`clang -S -O2` miscompiles the following C code:

--- Begin of ud2_main.c ---
unsigned long long foo();

int main()
{
        char* c = (char*)0 + foo();
        int res = c[0] + c[1];
        return res;
}
--- End of ud2_main.c ---

The call `clang -S -O2 ud2_main.c` compiles the function main() to:

        pushq   %rax
.Lcfi0:
        .cfi_def_cfa_offset 16
        xorl    %eax, %eax
        callq   foo
        ud2

Then ud2 instruction leads to core dump.

To compile and test the program you need the second file with foo():

--- Begin of ud2_foo.c ---
unsigned long long foo()
{
        static char* ptr = "\3\5";
        return (unsigned long long)(ptr - (char*)0);
}
--- End of ud2_foo.c ---

Here is testing results:
$ clang -O2 ud2_main.c ud2_foo.c -o ud2_clang
$ ./ud2_clang
Illegal instruction (core dumped)

# succesfull tests with gcc and 'clang -O0'
$ gcc -O2 ud2_main.c ud2_foo.c -o ud2_gcc && ./ud2_gcc; echo $?
8
$ clang -O0 ud2_main.c ud2_foo.c -o ud2_clangO0 && ./ud2_clangO0; echo $?
8

The bug occurs with -O1 or -O2 optimizations (doesn't with -O0), and only with
the foo() function declared in a separate file.

The system info:
$ uname -a
Linux ubuntu64 4.10.0-28-generic #32-Ubuntu SMP Fri Jun 30 05:32:18 UTC 2017
x86_64 x86_64 x86_64 GNU/Linux
$ clang -v
clang version 4.0.0-1ubuntu1 (tags/RELEASE_400/rc1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170730/237dd9e0/attachment.html>


More information about the llvm-bugs mailing list