https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128871
Bug ID: 128871
Summary: std::noop_coroutine frame stores void(*)() functions
that are called as void(*)(void*)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: filip.sajdak at gmail dot com
Target Milestone: ---
Host: aarch64-unknown-linux-gnu
Build: aarch64-unknown-linux-gnu
The frame of std::noop_coroutine stores its resume and destroy functions with
the type void(), but the coroutine machinery calls them with the frame pointer
as an argument.
libstdc++-v3/include/std/coroutine, lines 304 to 307 on trunk (the same lines
are in 16.2.0):
struct __frame
{
static void __dummy_resume_destroy() { }
void (*__r)() = __dummy_resume_destroy;
void (*__d)() = __dummy_resume_destroy;
struct noop_coroutine_promise __p;
};
A coroutine frame begins with a resume pointer and a destroy pointer, and the
compiler gives both the type void (*)(frame*) (act_des_fn_type in
gcc/cp/coroutines.cc). std::coroutine_handle<>::resume() and destroy() are
__builtin_coro_resume(_M_fr_ptr) and __builtin_coro_destroy(_M_fr_ptr), which
load that pointer from the frame and call it with the frame address.
A noop_coroutine_handle converts implicitly to coroutine_handle<>. resume() on
the converted handle therefore calls __dummy_resume_destroy, a function of type
void(), through a pointer used as void (*)(void*). Calling a function through
an expression of a different function type is undefined behaviour
([expr.call]).
Targets where an extra argument in a register is ignored (x86-64, AArch64)
tolerate it, which is probably why it went unnoticed. A target that checks the
type of an indirect call does not.
$ cat nc.cpp
#include <coroutine>
int main() {
std::coroutine_handle<> h = std::noop_coroutine();
h.resume();
h.destroy();
return 0;
}
(The conversion matters: noop_coroutine_handle's own resume() is an empty
function and makes no indirect call.)
How I observed it: with an out-of-tree WebAssembly back end (not part of GCC:
the back end of https://github.com/seo-rii/wasm-llvm, pull request 57, applied
to trunk e632e263a5ca1fb5157db28def311bbc62b8f100) and a libstdc++ built from
the same commit for wasm32-wasi. WebAssembly validates call_indirect against
the function type, so the program above traps:
$ g++ -std=c++20 -fno-exceptions -fno-rtti -O0 nc.cpp -o nc.wasm
$ node run.mjs nc.wasm
RuntimeError: function signature mismatch
With the patch below it exits with status 0. In a real program the first
symmetric transfer to std::noop_coroutine() trapped in the same way.
I did not reproduce a failure on an in-tree target, and GCC has no
-fsanitize=function to show it there. The type mismatch itself is visible in
the header on every target.
Suggested fix:
--- a/libstdc++-v3/include/std/coroutine
+++ b/libstdc++-v3/include/std/coroutine
@@
struct __frame
{
- static void __dummy_resume_destroy() { }
+ static void __dummy_resume_destroy(void*) { }
- void (*__r)() = __dummy_resume_destroy;
- void (*__d)() = __dummy_resume_destroy;
+ void (*__r)(void*) = __dummy_resume_destroy;
+ void (*__d)(void*) = __dummy_resume_destroy;
struct noop_coroutine_promise __p;
};
It adds a parameter that is never read and keeps the layout of the frame, since
the pointers keep their size.
Not checked: whether other headers hold a function pointer with the same kind
of mismatch.
This report was prepared with AI assistance. The program was built and run as
described above.