Issue 234913
Summary [WholeProgramDevirt][GlobalOpt] Internal branch funnel loses nest and passes the vtable as this
Labels new issue
Assignees
Reporter yqtian-se
    ## Description

With full LTO, `-fwhole-program-vtables`, and `-mretpoline`, Clang creates an internal branch
funnel for a virtual call on an anonymous-namespace class. A later GlobalOpt pass removes the
`nest` attribute from the funnel and its call site. This changes which x86-64 register carries the
vtable pointer, and the selected virtual function receives the vtable address as `this`. The
program segfaults instead of printing `100`.

## Reproducer

```cpp
#include <cstdio>
namespace {
struct A { int x = 0; virtual void f() { x += 1; } };
struct B : A { void f() override { x += 10; } };
struct C : A { void f() override { x += 100; } };
}
int main(int argc, char**) {
  A* p = argc > 5 ? static_cast<A*>(new B) : new C;
  p->f();
  std::printf("%d\n", p->x);
}
```

## Command

```text
clang++ -std=c++17 -O2 -flto -fuse-ld=lld -fwhole-program-vtables -mretpoline test.cpp -o test
./test
```

## Actual behavior

The program terminates with `SIGSEGV` and produces no output. The shell reports exit status 139.

After WholeProgramDevirt, the funnel and call site both use `nest`:

```llvm
define internal void @branch_funnel(ptr nest %0, ...) {
 musttail call void (...) @llvm.icall.branch.funnel(ptr %0, ...)
  ret void
}

call void @branch_funnel(ptr nest %vtable, ptr %object)
```

A later GlobalOpt pass removes `nest` from both places:

```llvm
define internal void @branch_funnel(ptr %0, ...) {
  musttail call void (...) @llvm.icall.branch.funnel(ptr %0, ...)
  ret void
}

call void (ptr, ...) @branch_funnel(ptr %vtable, ptr %object)
```

On x86-64, the generated call passes the vtable address in the first ordinary argument register.
The funnel tail-calls `C::f`, which then treats that vtable address as `this` and faults when it
writes to `x`.

## Expected behavior

The program should print `100`. With no command-line arguments, `p` points to a `C` object, so the
virtual call must select `C::f` and add 100 to `p->x`.

The program prints `100` when `-mretpoline` is removed, when `-O1` is used, when full LTO is
replaced with ThinLTO, or when the three classes have external linkage. GCC also prints `100`.
The testcase passes GCC ASan+UBSan and Clang accepts it with
`-fsyntax-only -Wall -Wextra -pedantic-errors`.

## Compiler version

```text
clang version 24.0.0git (/src/clang afee4adae612f88e09f705b9f44066091d8c7ef5)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/llvm/bin
Build config: +assertions
```

## Suspected cause

`WholeProgramDevirtPass` creates `branch_funnel` with an internal linkage and marks its first
parameter and the corresponding call-site argument as `nest`. `OptimizeFunctions` in
`GlobalOpt.cpp` removes every `nest` attribute from a function whose address is not taken. Its
comment assumes that only trampoline intrinsics use `nest`, but WPD branch funnels also use it to
pass the vtable pointer in the x86-64 nest register.

The same removal condition is still present in LLVM `main` at commit
`beded2c4e47599416bf6a96e5972ff688454dfb6`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to