Files
clang-p2996/compiler-rt/test/cfi/vtable-may-alias.cpp
Peter Collingbourne 868783e855 LowerTypeTests: Give imported symbols a type with size 0 so that they are not assumed not to alias.
It is possible for both a base and a derived class to be satisfied
with a unique vtable. If a program contains casts of the same pointer
to both of those types, the CFI checks will be lowered to this
(with ThinLTO):

if (p != &__typeid_base_global_addr)
  trap();
if (p != &__typeid_derived_global_addr)
  trap();

The optimizer may then use the first condition combined
with the assumption that __typeid_base_global_addr and
__typeid_derived_global_addr may not alias to optimize away the second
comparison, resulting in an unconditional trap.

This patch fixes the bug by giving imported globals the type [0 x i8]*,
which prevents the optimizer from assuming that they do not alias.

Differential Revision: https://reviews.llvm.org/D38873

llvm-svn: 315753
2017-10-13 21:02:16 +00:00

26 lines
431 B
C++

// RUN: %clangxx_cfi -o %t %s
// RUN: %run %t
// In this example, both __typeid_A_global_addr and __typeid_B_global_addr will
// refer to the same address. Make sure that the compiler does not assume that
// they do not alias.
struct A {
virtual void f() = 0;
};
struct B : A {
virtual void f() {}
};
__attribute__((weak)) void foo(void *p) {
B *b = (B *)p;
A *a = (A *)b;
a->f();
}
int main() {
B b;
foo(&b);
}