emscripten-core / emscripten-core/emscripten

"Pure virtual function called" is not displayed

Open
#17,339 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

While it doesn't make sense to do so, people can write (probably accidentally) code that calls a pure virtual function. This is one example of such program:
```cpp
struct Parent {
Parent() {
// At this point the child class instance hasn't been created, so the
// parent's `foo` is called
foo();
}
virtual void foo() = 0;
};

struct Child : Parent {
virtual void foo() {}
};

int main() {
Child c;
return 0;
}
```

In this case this library function is called and the program is terminated: https://github.com/emscripten-core/emscripten/blob/e91ea21531264f12e38a681485887212315ba08c/system/lib/libcxxabi/src/cxa_virtual.cpp#L14-L17

The case is similar for calling a deleted virtual function is similar with this library function: https://github.com/emscripten-core/emscripten/blob/e91ea21531264f12e38a681485887212315ba08c/system/lib/libcxxabi/src/cxa_virtual.cpp#L19-L22

But in Wasm we don't end up calling these functions because virtual functions are called by `call_indirect` and `__cxa_pure_virtual` signature, `void(void)`, doesn't match with the actual virtual function, unless the virtual function's signature is also `void(void)`, which cannot happen because class member functions take `this` as the first parameter.

So while we see this helpful message in native platforms:
```
pure virtual method called
terminate called without an active exception
Aborted
```
Wasm crashes with this cryptic message:
```
RuntimeError: null function or function signature mismatch
```
which makes diagnosing the error difficult.

This can probably be fixed by creating a `__cxa_pure_virtual` for every signature that's in the function table, but this will certainly be detrimental to the code size. But this may be justified in debug / assertion builds.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.