diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2017-10-13 21:02:16 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2017-10-13 21:02:16 +0000 |
commit | b1ac0a6f5296ba472af22fc3e62b011f6ea9d40f (patch) | |
tree | e0e95d0b2840c9d829dec38f71248cf12174439e /test/cfi | |
parent | e972b4cb282b92b0c15356660c4a95593066ef8c (diff) |
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
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@315753 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/cfi')
-rw-r--r-- | test/cfi/vtable-may-alias.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/test/cfi/vtable-may-alias.cpp b/test/cfi/vtable-may-alias.cpp new file mode 100644 index 000000000..f63b53b8c --- /dev/null +++ b/test/cfi/vtable-may-alias.cpp @@ -0,0 +1,25 @@ +// 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); +} |