brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · 91ce45a Raw
103 lines · cpp
1// RUN: mkdir -p %t.dir && cd %t.dir2// RUN: %clangxx_cfi_dso -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so3// RUN: %clangxx_cfi_dso %s -o %t.dir/exe %ld_flags_rpath_exe4// RUN: %expect_crash %t.dir/exe 2>&1 | FileCheck --check-prefix=CFI %s5// RUN: %expect_crash %t.dir/exe x 2>&1 | FileCheck --check-prefix=CFI-CAST %s6 7// RUN: %clangxx_cfi_dso -DB32 -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so8// RUN: %clangxx_cfi_dso -DB32 %s -o %t.dir/exe %ld_flags_rpath_exe9// RUN: %expect_crash %t.dir/exe 2>&1 | FileCheck --check-prefix=CFI %s10// RUN: %expect_crash %t.dir/exe x 2>&1 | FileCheck --check-prefix=CFI-CAST %s11 12// RUN: %clangxx_cfi_dso -DB64 -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so13// RUN: %clangxx_cfi_dso -DB64 %s -o %t.dir/exe %ld_flags_rpath_exe14// RUN: %expect_crash %t.dir/exe 2>&1 | FileCheck --check-prefix=CFI %s15// RUN: %expect_crash %t.dir/exe x 2>&1 | FileCheck --check-prefix=CFI-CAST %s16 17// RUN: %clangxx_cfi_dso -DBM -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so18// RUN: %clangxx_cfi_dso -DBM %s -o %t.dir/exe %ld_flags_rpath_exe19// RUN: %expect_crash %t.dir/exe 2>&1 | FileCheck --check-prefix=CFI %s20// RUN: %expect_crash %t.dir/exe x 2>&1 | FileCheck --check-prefix=CFI-CAST %s21 22// RUN: %clangxx -DBM -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so23// RUN: %clangxx -DBM %s -o %t.dir/exe %ld_flags_rpath_exe24// RUN: %t.dir/exe 2>&1 | FileCheck --check-prefix=NCFI %s25// RUN: %t.dir/exe x 2>&1 | FileCheck --check-prefix=NCFI %s26 27// RUN: %clangxx -DBM -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so28// RUN: %clangxx_cfi_dso -DBM %s -o %t.dir/exe %ld_flags_rpath_exe29// RUN: %t.dir/exe 2>&1 | FileCheck --check-prefix=NCFI %s30// RUN: %t.dir/exe x 2>&1 | FileCheck --check-prefix=NCFI %s31 32// RUN: %clangxx_cfi_dso_diag -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so33// RUN: %clangxx_cfi_dso_diag %s -o %t.dir/exe %ld_flags_rpath_exe34// RUN: %t.dir/exe 2>&1 | FileCheck --check-prefix=CFI-DIAG-CALL %s35// RUN: %t.dir/exe x 2>&1 | FileCheck --check-prefix=CFI-DIAG-CALL --check-prefix=CFI-DIAG-CAST %s36 37// Tests that the CFI mechanism crashes the program when making a virtual call38// to an object of the wrong class but with a compatible vtable, by casting a39// pointer to such an object and attempting to make a call through it.40 41// REQUIRES: cxxabi42 43#include <stdio.h>44#include <string.h>45 46struct A {47  virtual void f();48};49 50void *create_B();51 52#ifdef SHARED_LIB53 54#include "../utils.h"55struct B {56  virtual void f();57};58void B::f() {}59 60void *create_B() {61  create_derivers<B>();62  return (void *)(new B());63}64 65#else66 67void A::f() {}68 69int main(int argc, char *argv[]) {70  void *p = create_B();71  A *a;72 73  // CFI: =0=74  // CFI-CAST: =0=75  // NCFI: =0=76  fprintf(stderr, "=0=\n");77 78  if (argc > 1 && argv[1][0] == 'x') {79    // Test cast. BOOM.80    // CFI-DIAG-CAST: runtime error: control flow integrity check for type 'A' failed during cast to unrelated type81    // CFI-DIAG-CAST-NEXT: note: vtable is of type '{{(struct )?}}B'82    a = (A*)p;83  } else {84    // Invisible to CFI. Test virtual call later.85    memcpy(&a, &p, sizeof(a));86  }87 88  // CFI: =1=89  // CFI-CAST-NOT: =1=90  // NCFI: =1=91  fprintf(stderr, "=1=\n");92 93  // CFI-DIAG-CALL: runtime error: control flow integrity check for type 'A' failed during virtual call94  // CFI-DIAG-CALL-NEXT: note: vtable is of type '{{(struct )?}}B'95  a->f(); // UB here96 97  // CFI-NOT: =2=98  // CFI-CAST-NOT: =2=99  // NCFI: =2=100  fprintf(stderr, "=2=\n");101}102#endif103