[llvm-bugs] [Bug 49913] New: [coroutines] errno address is reused after context switch

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Apr 10 04:47:33 PDT 2021


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

            Bug ID: 49913
           Summary: [coroutines] errno address is reused after context
                    switch
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: antoshkka at gmail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

Created attachment 24742
  --> https://bugs.llvm.org/attachment.cgi?id=24742&action=edit
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: https://godbolt.org/z/Wh9xd9oME

Looks like the common subexpression elimination pass must not be applied
between the context switches for the functions with __attribute__((const))

-- 
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/20210410/29f2cc34/attachment.html>


More information about the llvm-bugs mailing list