Issue 235766
Summary [VFE] noexcept virtual function is removed when called through a non-noexcept member-function pointer
Labels new issue
Assignees
Reporter yqtian-se
    With C++17, full LTO, and `-fvirtual-function-elimination`, Clang removes a live virtual function whose type is `noexcept` when the function is called through a non-`noexcept` member-function pointer. GlobalDCE replaces the live vtable slot with null, and the program crashes when it calls that slot.

## Reproducer

```cpp
#include <cstdio>

struct A {
  virtual int f() { return 1; }
  virtual int g() noexcept { return 2; }
};

int (A::*volatile pm)() = &A::g;

int main() {
  A a;
  A* p = &a;
 std::printf("%d\n", (p->*pm)());
}
```

```text
clang++ -std=c++17 -O2 -flto -fuse-ld=lld -fwhole-program-vtables -fvisibility=hidden -fvirtual-function-elimination testcase.cpp -o test
./test
```

## Actual behavior

The program crashes with SIGSEGV and exits with status 139. After GlobalDCE, the vtable slot for `A::g()` has been replaced with null:

```llvm
@_ZTV1A = ... [ptr null, ptr @_ZTI1A, ptr @_ZN1A1fEv, ptr null] ...
```

The member-function-pointer call loads and calls this null slot.

## Expected behavior

The program should print `2` and exit normally. In C++17, converting a pointer to a `noexcept` member function to the corresponding potentially-throwing member-function-pointer type is a standard function pointer conversion under [[conv.fctptr]](https://eel.is/c++draft/conv.fctptr). The result still designates `A::g()`.

The program prints `2` with `-std=c++14`, without `-fvirtual-function-elimination`, or at `-O0`. Clang and GCC at `-O0` with AddressSanitizer and UndefinedBehaviorSanitizer also print `2` without diagnostics.

The same failure occurs without an explicit pointer conversion when an override adds `noexcept`, which [[except.spec]/5](https://eel.is/c++draft/except.spec#5) permits:

```cpp
#include <cstdio>

struct A {
  virtual int f() { return 1; }
  virtual int g() { return 2; }
};
struct B : A {
  int g() noexcept override { return 3; }
};

int (A::*volatile pm)() = &A::g;

int main() {
  B b;
  A* p = &b;
  std::printf("%d\n", (p->*pm)());
}
```

This form should print `3`, but it also crashes with SIGSEGV under the failing command.

## IR evidence

Clang assigns different virtual-member-pointer type identifiers to the vtable slots:

```llvm
!type !{i64 16, !"_ZTSM1AFivE.virtual"}
!type !{i64 24, !"_ZTSM1ADoFivE.virtual"}
```

The member-pointer call uses the identifier for its non-`noexcept` static type:

```llvm
%fn = call { ptr, i1 } @llvm.type.checked.load(
    ptr %slot, i32 0, metadata !"_ZTSM1AFivE.virtual")
```

The `Do` identifier on the `A::g()` slot is therefore not considered live, even though the standard conversion allows the non-`noexcept` member pointer to designate that function.

The same type-identifier mismatch causes `-fsanitize=cfi-mfcall` to reject both calls.

## Suspected cause

`CodeGenVTables` creates vtable type metadata from each virtual function declaration's complete member-function-pointer type, including its exception specification. The virtual branch of a member-pointer call creates its checked-load identifier from the pointer's static type. These identifiers must remain compatible across the standard conversion that drops `noexcept` and when an overriding function has a stricter exception specification.

## Compiler version

```text
clang version 24.0.0git (/src/clang 94601109aeb6424e4167e7209078b4090d83edfa)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/llvm/bin
Build config: +assertions
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to