1 // RUN: %clangxx_cfi_dso -std=c++11 -g -DSHARED_LIB %s -fPIC -shared -o %t-cfi-so.so
2 // RUN: %clangxx -std=c++11 -g -DSHARED_LIB %s -fPIC -shared -o %t-nocfi-so.so
3 // RUN: %clangxx_cfi_dso -std=c++11 -g %s -o %t
5 // RUN: %expect_crash %t start 2>&1 | FileCheck %s
6 // RUN: %expect_crash %t mmap 2>&1 | FileCheck %s
7 // RUN: %expect_crash %t dlopen %t-cfi-so.so 2>&1 | FileCheck %s
8 // RUN: %expect_crash %t dlclose %t-cfi-so.so 2>&1 | FileCheck %s
9 // RUN: %expect_crash %t dlopen %t-nocfi-so.so 2>&1 | FileCheck %s
10 // RUN: %expect_crash %t dlclose %t-nocfi-so.so 2>&1 | FileCheck %s
12 // Tests that shadow is read-only most of the time.
15 // Uses private API that is not available on Android.
16 // UNSUPPORTED: android
34 extern "C" A
*create_A() { return new A(); }
38 constexpr unsigned kShadowGranularity
= 12;
41 uintptr_t GetShadow();
44 void write_shadow(void *ptr
) {
45 uintptr_t base
= __cfi::GetShadow();
47 (uint16_t *)(base
+ (((uintptr_t)ptr
>> kShadowGranularity
) << 1));
48 fprintf(stderr
, "going to crash\n");
49 // CHECK: going to crash
51 fprintf(stderr
, "did not crash\n");
52 // CHECK-NOT: did not crash
56 int main(int argc
, char *argv
[]) {
58 const bool test_mmap
= strcmp(argv
[1], "mmap") == 0;
59 const bool test_start
= strcmp(argv
[1], "start") == 0;
60 const bool test_dlopen
= strcmp(argv
[1], "dlopen") == 0;
61 const bool test_dlclose
= strcmp(argv
[1], "dlclose") == 0;
62 const char *lib
= argc
> 2 ? argv
[2] : nullptr;
65 write_shadow((void *)&main
);
68 void *p
= mmap(nullptr, 1 << 20, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
69 MAP_PRIVATE
| MAP_ANONYMOUS
, 0, 0);
70 assert(p
!= MAP_FAILED
);
71 write_shadow((char *)p
+ 100);
73 void *handle
= dlopen(lib
, RTLD_NOW
);
75 void *create_A
= dlsym(handle
, "create_A");
79 write_shadow(create_A
);
81 int res
= dlclose(handle
);
85 write_shadow(create_A
);