<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 - [coroutines] errno address is reused after context switch"
   href="https://bugs.llvm.org/show_bug.cgi?id=49913">49913</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[coroutines] errno address is reused after context switch
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </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>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++2a
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>antoshkka@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=24742" name="attach_24742" title="Full example">attachment 24742</a> <a href="attachment.cgi?id=24742&action=edit" title="Full example">[details]</a></span>
Full example

Consider the example:

CoroTask CoroToDealWith() {
    if (errno) first_call();
    co_await writerQueue;
    if (errno) second_call();
}

With `-O2 -std=c++20 -stdlib=libc++` flags clang generates code that stores the
`errno` address on first call:

  call __errno_location
  mov qword ptr [rbx + 24], rax
  cmp dword ptr [rax], 0
  je .LBB1_2
  call first_call()

and reuses that address after wakeup:

CoroToDealWith() [clone .resume]: # @CoroToDealWith() [clone .resume]
  push rbx
  mov rbx, rdi
  mov rax, qword ptr [rdi + 24]
  cmp dword ptr [rax], 0
  je .LBB2_2
  call second_call()


The optimization is wrong: coroutine could be resumed on other thread and the
errno address changes.

Godbolt playground: <a href="https://godbolt.org/z/Wh9xd9oME">https://godbolt.org/z/Wh9xd9oME</a>

Looks like the common subexpression elimination pass must not be applied
between the context switches for the functions with __attribute__((const))</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>