[llvm-bugs] [Bug 47963] New: Miscompilation Removes Necessary Function Call in Clang 10.0.1

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Oct 25 08:36:34 PDT 2020


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

            Bug ID: 47963
           Summary: Miscompilation Removes Necessary Function Call in
                    Clang 10.0.1
           Product: new-bugs
           Version: 10.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: gavin.d.howard at gmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

Created attachment 24097
  --> https://bugs.llvm.org/attachment.cgi?id=24097&action=edit
bugpoint reduced test case

# Overview

Clang 10.0.1, as packaged by Gentoo on x86_64, miscompiles a library I develop
by removing a call to a necessary function and then optimizing to `return 0;`

The library in question is https://git.yzena.com/Yzena/Yc, specifically the
function `ymap_at()` at
https://git.yzena.com/Yzena/Yc/src/branch/master/src/map/map.c#L422.

When compiling using either CMAKE_BUILD_TYPE as Release (`-O3 -DNDEBUG`) or
RelWithDebInfo (`-O2 -g -DNDEBUG`), ymap_at() is compiled to:

```
00000000004066b0 <ymap_at>:
  4066b0:       31 c0                   xor    %eax,%eax
  4066b2:       c3                      retq   
  4066b3:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
  4066ba:       00 00 00 
  4066bd:       0f 1f 00                nopl   (%rax)
```

as shown by `objdump -d`. As you can see, despite having a call to
`ymap_index()` and a check on the return value, clang compiled it as `return
0;`.

However, when compiling with CMAKE_BUILD_TYPE as Debug (`-g`), there is no
miscompilation.

# Steps to Reproduce

```
cd $ROOT
git clone https://git.yzena.com/Yzena/Yc.git yc
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/path/to/clang ../yc
make
./tests/map/map_all
```

## Actual Results

An abort(). This happens because of these lines:

https://git.yzena.com/Yzena/Yc/src/branch/master/tests/map/map_all.c#L56-L61

Basically, because `ymap_at()` is just returning 0 (NULL), the test thinks the
item requested from the map does not exist and aborts (fails) the test.

## Expected Results

Test case passes (with 0 exit code).

# Build Date and Hardware

Up-to-date Gentoo Linux x86_64.

Latest master compiled on 2020-10-25 and tested. Bug exists there as well.

# Additional Information

I have verified that gcc does *not* miscompile that code, and I have also
verified that valgrind returns clean when compiling with both gcc and clang.

Attached is the bugpoint reduced test case that I created with the following:

```
cd $ROOT/build
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS="-flto" \
   -DCMAKE_C_COMPILER=/path/to/clang \
   -DCMAKE_EXE_LINKER_FLAGS="-Wl,-plugin-opt=emit-llvm,-O0" \
   ../yc
make
cd tests/map
ln -s map_all map_all.bc
cd ../..
bugpoint --llc-safe tests/map/map_all.bc
```

For the commands above, bugpoint took 6 hours 26 minutes to run.

I am putting this bug in the `new-bugs` product because I am new and don't know
where to put this bug, though I will suspect it goes into libraries because the
bug is not in clang's frontend.

-- 
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/20201025/76b4c829/attachment.html>


More information about the llvm-bugs mailing list