92 lines · cpp
1// RUN: %clangxx_cfi -c -DTU1 -o %t1.o %s2// RUN: %clangxx_cfi -c -DTU2 -o %t2.o %S/../cfi/anon-namespace.cpp3// RUN: %clangxx_cfi -o %t1 %t1.o %t2.o4// RUN: %expect_crash %run %t1 2>&1 | FileCheck --check-prefix=CFI %s5 6// RUN: %clangxx_cfi -c -DTU1 -DB32 -o %t1.o %s7// RUN: %clangxx_cfi -c -DTU2 -DB32 -o %t2.o %S/../cfi/anon-namespace.cpp8// RUN: %clangxx_cfi -o %t2 %t1.o %t2.o9// RUN: %expect_crash %run %t2 2>&1 | FileCheck --check-prefix=CFI %s10 11// RUN: %clangxx_cfi -c -DTU1 -DB64 -o %t1.o %s12// RUN: %clangxx_cfi -c -DTU2 -DB64 -o %t2.o %S/../cfi/anon-namespace.cpp13// RUN: %clangxx_cfi -o %t3 %t1.o %t2.o14// RUN: %expect_crash %run %t3 2>&1 | FileCheck --check-prefix=CFI %s15 16// RUN: %clangxx_cfi -c -DTU1 -DBM -o %t1.o %s17// RUN: %clangxx_cfi -c -DTU2 -DBM -o %t2.o %S/../cfi/anon-namespace.cpp18// RUN: %clangxx_cfi -o %t4 %t1.o %t2.o19// RUN: %expect_crash %run %t4 2>&1 | FileCheck --check-prefix=CFI %s20 21// RUN: %clangxx -c -DTU1 -o %t1.o %s22// RUN: %clangxx -c -DTU2 -o %t2.o %S/../cfi/anon-namespace.cpp23// RUN: %clangxx -o %t5 %t1.o %t2.o24// RUN: %run %t5 2>&1 | FileCheck --check-prefix=NCFI %s25 26// RUN: %clangxx_cfi_diag -c -DTU1 -o %t1.o %s27// RUN: %clangxx_cfi_diag -c -DTU2 -o %t2.o %S/../cfi/anon-namespace.cpp28// RUN: %clangxx_cfi_diag -o %t6 %t1.o %t2.o29// RUN: %run %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG %s30 31// Tests that the CFI mechanism treats classes in the anonymous namespace in32// different translation units as having distinct identities. This is done by33// compiling two translation units TU1 and TU2 containing a class named B in an34// anonymous namespace, and testing that the program crashes if TU2 attempts to35// use a TU1 B as a TU2 B.36 37// FIXME: This test should not require that the paths supplied to the compiler38// are different. It currently does so because bitset names have global scope39// so we have to mangle the file path into the bitset name.40 41// REQUIRES: cxxabi42 43#include <stdio.h>44#include "utils.h"45 46struct A {47 virtual void f() = 0;48};49 50namespace {51 52struct B : A {53 virtual void f() {}54};55 56}57 58A *mkb();59 60#ifdef TU161 62A *mkb() {63 return new B;64}65 66#endif // TU167 68#ifdef TU269 70int main() {71 create_derivers<B>();72 73 A *a = mkb();74 break_optimization(a);75 76 // CFI: 177 // NCFI: 178 fprintf(stderr, "1\n");79 80 // CFI-DIAG: runtime error: control flow integrity check for type '(anonymous namespace)::B' failed during base-to-derived cast81 // CFI-DIAG-NEXT: note: vtable is of type '{{.*}}anonymous namespace{{.*}}::B'82 // CFI-DIAG: runtime error: control flow integrity check for type '(anonymous namespace)::B' failed during virtual call83 // CFI-DIAG-NEXT: note: vtable is of type '{{.*}}anonymous namespace{{.*}}::B'84 ((B *)a)->f(); // UB here85 86 // CFI-NOT: {{^2$}}87 // NCFI: {{^2$}}88 fprintf(stderr, "2\n");89}90 91#endif // TU292