<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - CFI: unclear behavior discrepancy with -O0 vs -Os when libstdc++ is linked statically"
   href="https://llvm.org/bugs/show_bug.cgi?id=31775">31775</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>CFI: unclear behavior discrepancy with -O0 vs -Os when libstdc++ is linked statically
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>krasin@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=17900" name="attach_17900" title="main.cc">attachment 17900</a> <a href="attachment.cgi?id=17900&action=edit" title="main.cc">[details]</a></span>
main.cc

Consider the following c++ program:

$ cat main.cc
#include <iostream>

int main(int argc, const char *argv[]) {
  std::cout << "Hello" << std::endl;
}

And the following build script:

$ cat build.sh
#!/bin/bash

set -ue

export CFLAGS="-g -c -flto -fsanitize=cfi -fsanitize-cfi-cross-dso
-fno-sanitize-trap=all -fvisibility=default"
export LDFLAGS="-flto -fuse-ld=gold -fno-sanitize-trap=all -flto -fuse-ld=gold
-fsanitize=cfi -fsanitize-cfi-cross-dso -fvisibility=default -static-libstdc++"

clang++ -O0 -o main_0.o main.cc ${CFLAGS}
clang++ -o main_0 main_0.o ${LDFLAGS}
echo "Running a binary built with -O0:"
./main_0

clang++ -Os -o main_s.o main.cc ${CFLAGS}
clang++ -o main_s main_s.o ${LDFLAGS}
echo "Running a binary built with -Os:"
./main_s

When running the script it outputs:

$ ./build.sh 
Running a binary built with -O0:
Hello
Running a binary built with -Os:
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:113:9:
runtime error: control flow integrity check for type 'std::basic_ostream<char>
&(std::basic_ostream<char> &)' failed during indirect function call
(/usr/local/google/home/krasin/play/cfi-inline/main_s+0x71b70): note:
std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
defined here
SUMMARY: CFI: undefined-behavior
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:113:9 in

There are few unclear moments in the report:

1. Why is it only happening at -Os, but not -O0. The answer is likely related
to inlining, but the exact cause is still not found.

2. Why isn't the address symbolized (main_s+0x71b70) and what does it point to?

3. Given that the main linkage unit has CFI and non-CFI together, it's an
unsupported combination. Do we want to do anything in order to support it? It
has been the reason for a major loss of productivity for a user.</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>