<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] Use-after-free by access to coroutine frame after await_suspend()"
   href="https://bugs.llvm.org/show_bug.cgi?id=46990">46990</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[coroutines] Use-after-free by access to coroutine frame after await_suspend()
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>11.0
          </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>C2x
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>alexander.vandergrinten@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=23813" name="attach_23813" title="Preprocessed source code">attachment 23813</a> <a href="attachment.cgi?id=23813&action=edit" title="Preprocessed source code">[details]</a></span>
Preprocessed source code

With some optimization options (-O2 and -fsanitize=undefined in this case),
LLVM generates instructions that access coroutine frames after calling
await_suspend() but before returning to the caller/resumer of a coroutine. This
leads to use-after-free if the coroutine is freed before control returns to the
caller. Below is a snippet of LLVM code that demonstrates this; the GEP
instructions cause a bogus access.

The preprocessed source is attached (but unfortunately, I was unable to reduce
the bug to a toy example). Compile with:
clang++ -std=c++20 -O2 -fsanitize=undefined -S -emit-llvm -o out.S out.cpp

This bug happens both with Clang 10.0.1 and 11.0.0-rc1.

Relevant LLVM code:
-------------------
  invoke void
@_ZN5async14sender_awaiterIN4thor18CopyFromViewSenderEvE13await_suspendENSt12experimental16coroutine_handleIvEE(%"struct.async::sender_awaiter.44"*
nonnull %31, i8* nonnull %2)
          to label %725 unwind label %745

725:                                              ; preds = %723
  %726 = getelementptr inbounds
%_ZN4thor15loadModuleImageEN7smarter10shared_ptrINS_12AddressSpaceE14BindableHandleEEmN5frigg9SharedPtrINS_10MemoryViewENS5_13SharedControlEEE.Frame,
%_ZN4thor15loadModuleImageEN7smarter10shared_ptrINS_12AddressSpaceE14BindableHandleEEmN5frigg9SharedPtrINS_10MemoryViewENS5_13SharedControlEEE.Frame*
%0, i64 0, i32 32
  %727 = getelementptr inbounds
%_ZN4thor15loadModuleImageEN7smarter10shared_ptrINS_12AddressSpaceE14BindableHandleEEmN5frigg9SharedPtrINS_10MemoryViewENS5_13SharedControlEEE.Frame,
%_ZN4thor15loadModuleImageEN7smarter10shared_ptrINS_12AddressSpaceE14BindableHandleEEmN5frigg9SharedPtrINS_10MemoryViewENS5_13SharedControlEEE.Frame*
%0, i64 0, i32 31
  br label %856</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>