[compiler-rt] r315753 - LowerTypeTests: Give imported symbols a type with size 0 so that they are not assumed not to alias.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 13 14:02:16 PDT 2017


Author: pcc
Date: Fri Oct 13 14:02:16 2017
New Revision: 315753

URL: http://llvm.org/viewvc/llvm-project?rev=315753&view=rev
Log:
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

Added:
    compiler-rt/trunk/test/cfi/vtable-may-alias.cpp

Added: compiler-rt/trunk/test/cfi/vtable-may-alias.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/cfi/vtable-may-alias.cpp?rev=315753&view=auto
==============================================================================
--- compiler-rt/trunk/test/cfi/vtable-may-alias.cpp (added)
+++ compiler-rt/trunk/test/cfi/vtable-may-alias.cpp Fri Oct 13 14:02:16 2017
@@ -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);
+}




More information about the llvm-commits mailing list