Skip to content

[wasm][wasm-exceptions][c++] Destructors called too early when throwing call is in inline assembly #65116

Open
@lambdageek

Description

@lambdageek

Repro: https://godbolt.org/z/8W8Eb9nr3

int foo();
int bar();

static
__attribute__((noinline))
int
baz(){
    int x = 0;
#if 1
    __asm(
        "try i32; "
        "call _bar;"
        "catch_all;"
        " rethrow 0;"
        " "
        "end_try;"
        "i32.const 1;"
        "i32.add;"
        "local.set %0;"
    : "=r"(x)
    );
#else
    try {
      x = 1 + bar ();
    } catch (...) {
        throw;
    }
#endif
    return x;
}

class X {
public:
    explicit X() {}
    ~X() {foo();};
};


int square(int num) {
    X x{}; // call to foo() happens here
    return baz();
    // expected the call to foo to happen after baz returns
}

When I compile this with -O -fexceptions -fwasm-exceptions I expect the destructor for x to be called after baz() returns (or when we're unwinding because it throws). Indeed that is the case if you change #if 1 to #if 0 and compile the C++ version of the code. On the other hand when the exception is in the inline asm code, the destructor is called before the call to baz().

Am I doing something wrong? Is there some way to tell clang that the inline wasm may throw?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions