Skip to content

Exceptions cannot be handled when using amd64.c or x86.c on Windows #45

@CzB404

Description

@CzB404

Hello!

When C++ code on a cothread throws an exception the stack unwinding thinks it is from a noexcept method and calls terminate instead of correctly unwinding the stack within the cothread. This seems to be a bug in how the new stack is prepared by libco in the amd64.c and x86.c backends.

Repro:

#include <stdexcept>
#include <cstdio>
#include <libco.h>

cothread_t parent;

void will_throw()
{
    throw std::runtime_error("Catch me!");
}

void entry()
{
    try
    {
        will_throw();
    }
    catch (const std::exception& e)
    {
        std::printf("%s\n", e.what());
        co_switch(parent);
    }
}

int main()
{
    parent = co_active();
    auto cothread = co_create(262144 * sizeof(void*), &entry);
    co_switch(cothread);
    return 0;
}

Edit:
Correction: The statement "the stack unwinding thinks it is from a noexcept method and calls terminate" is only true when the exception is thrown from a function called by the entry function from a try block and only in the Debug configuration. If the exception is thrown directly from the try block then the exception object won't be caught properly and the executable tries to read from invalid memory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions