<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - Control Flow Guard on a simple program causes a crash"
   href="https://bugs.llvm.org/show_bug.cgi?id=39517">39517</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Control Flow Guard on a simple program causes a crash
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </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>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>charles.milette@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, peter.smith@linaro.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When compiling the following program

#include <iostream>
#include <sstream>

int main()
{
    std::wostringstream str;
    str << L"Lorem ipsum dolor sit amet";

    std::wcout << str.str();
}

using the following commands

clang-cl /c control-bug.cpp -m32 /guard:cf /MDd
--- OR ---
clang-cl /c control-bug.cpp -m32 /guard:cf /MD


lld-link /guard:cf control-bug.obj

will cause a binary that crashes before printing Lorem Ipsum to the command
prompt.

Using the static (/MT and /MTd) over the DLL (/MD and /MDd) Microsoft Runtime
Library will make a correctly working executable, as well as replacing lld-link
by link and/or clang-cl by cl.

After further debugging by setting a breakpoint on the failing
_guard_check_icall_fptr call in x32dbg, it seems that
std::basic_streambuf<unsigned short, std::char_traits<unsigned short>>::sputn
(which is a function embedded in the MS Runtime library) is calling
std::basic_streambuf<unsigned short, std::char_traits<unsigned short>>::xsputn
(which is a function embedded in the main executable file) by function pointer
but it is not registered as a valid call target to Control Flow Guard.</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>