[llvm-bugs] [Bug 35057] New: Clang "-O2" option cause if statement check error

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Oct 24 06:33:34 PDT 2017


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

            Bug ID: 35057
           Summary: Clang "-O2" option cause if statement check error
           Product: clang
           Version: 3.8
          Hardware: PC
                OS: FreeBSD
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: Zhixin.Wan at watchguard.com
                CC: llvm-bugs at lists.llvm.org

Created attachment 19333
  --> https://bugs.llvm.org/attachment.cgi?id=19333&action=edit
a small C file to reproduce this issue

Overview:
While building Apple's mDNSResponder on FreeBSD 11.0, I found a strange issue:
one if statement in mDNS_Lock() function failed to return true under extreme
condition where m->timenow_last=2147483647 and m->timenow=-2147483648.

(line 109, test.c)
        if (m->timenow_last - m->timenow > 0)
...

To reproduce this problem, I write a small C file, and just copy mDNS_Lock()
function implementation from mDNSResponder, and comment/replace some
unimportant function inside, but keep main statement of it.

Please see the attachment: test.c

Steps to Reproduce:
 1) Compile it with Clang 3.8.0 built in FreeBSD 11.0 AMD64 with "-O2" option:
 $ cc -O2 -o test test.c
 $ ./test
 $ mDNSPlatformRawTime went backwards by 4294967295 ticks; setting correction
factor to 4294967295

 2) Compile it again without "-O2" option:
 $ cc -o test test.c
 $ ./test
 $ 

It looks like "-O2" option would optimize the statement to unexpected assemble
codes:
 if (m->timenow_last - m->timenow > 0)

What I expected is that (m->timenow_last - m->timenow) should result to signed
integer -1, but it's treated as signed integer 4294967295 with "-O2" option.

To verify if latest Clang version has fixed this issue, install latest Clang
6.0 compiler to try:
# pkg install clang-devel-6.0.d20170920_1

After installlation, compile it with new Clang compiler:
 1) $ clang-devel -O2 -o test test.c
    $ ./test
    $ mDNSPlatformRawTime went backwards by 4294967295 ticks; setting
correction factor to 4294967295

 2) $ clang-devel -o test test.c
    $ ./test
    $
Got the same result as previous.

Use gcc version 4.9.4, I didn't encounter this problem.

Build Date & Hardware:
I can reproduced this issue when compiled test.c with
Clang 3.8.0, Clang 6.0 on FreeBSD 11.0 AMD64.
Apple LLVM version 9.0.0 (clang-900.0.38) on macOS 10.13, MacBook Air.

-- 
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/20171024/550e215f/attachment.html>


More information about the llvm-bugs mailing list