|
| 1 | +// RUN: %clang_cc1 -fsanitize=alloc-token -triple x86_64-linux-gnu -std=c++20 -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s |
| 2 | + |
| 3 | +#include "../Analysis/Inputs/system-header-simulator-cxx.h" |
| 4 | + |
| 5 | +typedef __UINTPTR_TYPE__ uintptr_t; |
| 6 | + |
| 7 | +extern "C" { |
| 8 | +void *malloc(size_t size); |
| 9 | +} |
| 10 | + |
| 11 | +// CHECK-LABEL: define dso_local noundef ptr @_Z15test_malloc_intv( |
| 12 | +// CHECK: call ptr @malloc(i64 noundef 4) |
| 13 | +void *test_malloc_int() { |
| 14 | + int *a = (int *)malloc(sizeof(int)); |
| 15 | + *a = 42; |
| 16 | + return a; |
| 17 | +} |
| 18 | + |
| 19 | +// CHECK-LABEL: define dso_local noundef ptr @_Z15test_malloc_ptrv( |
| 20 | +// CHECK: call ptr @malloc(i64 noundef 8) |
| 21 | +int **test_malloc_ptr() { |
| 22 | + int **a = (int **)malloc(sizeof(int*)); |
| 23 | + *a = nullptr; |
| 24 | + return a; |
| 25 | +} |
| 26 | + |
| 27 | +// CHECK-LABEL: define dso_local noundef ptr @_Z12test_new_intv( |
| 28 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4){{.*}} !alloc_token [[META_INT:![0-9]+]] |
| 29 | +int *test_new_int() { |
| 30 | + return new int; |
| 31 | +} |
| 32 | + |
| 33 | +// CHECK-LABEL: define dso_local noundef ptr @_Z20test_new_ulong_arrayv( |
| 34 | +// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 80){{.*}} !alloc_token [[META_ULONG:![0-9]+]] |
| 35 | +unsigned long *test_new_ulong_array() { |
| 36 | + return new unsigned long[10]; |
| 37 | +} |
| 38 | + |
| 39 | +// CHECK-LABEL: define dso_local noundef ptr @_Z12test_new_ptrv( |
| 40 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 8){{.*}} !alloc_token [[META_INTPTR:![0-9]+]] |
| 41 | +int **test_new_ptr() { |
| 42 | + return new int*; |
| 43 | +} |
| 44 | + |
| 45 | +// CHECK-LABEL: define dso_local noundef ptr @_Z18test_new_ptr_arrayv( |
| 46 | +// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 80){{.*}} !alloc_token [[META_INTPTR]] |
| 47 | +int **test_new_ptr_array() { |
| 48 | + return new int*[10]; |
| 49 | +} |
| 50 | + |
| 51 | +struct ContainsPtr { |
| 52 | + int a; |
| 53 | + char *buf; |
| 54 | +}; |
| 55 | + |
| 56 | +// CHECK-LABEL: define dso_local noundef ptr @_Z27test_malloc_struct_with_ptrv( |
| 57 | +// CHECK: call ptr @malloc(i64 noundef 16) |
| 58 | +ContainsPtr *test_malloc_struct_with_ptr() { |
| 59 | + ContainsPtr *c = (ContainsPtr *)malloc(sizeof(ContainsPtr)); |
| 60 | + return c; |
| 61 | +} |
| 62 | + |
| 63 | +// CHECK-LABEL: define dso_local noundef ptr @_Z33test_malloc_struct_array_with_ptrv( |
| 64 | +// CHECK: call ptr @malloc(i64 noundef 160) |
| 65 | +ContainsPtr *test_malloc_struct_array_with_ptr() { |
| 66 | + ContainsPtr *c = (ContainsPtr *)malloc(10 * sizeof(ContainsPtr)); |
| 67 | + return c; |
| 68 | +} |
| 69 | + |
| 70 | +// CHECK-LABEL: define dso_local noundef ptr @_Z32test_operatornew_struct_with_ptrv( |
| 71 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 16) |
| 72 | +ContainsPtr *test_operatornew_struct_with_ptr() { |
| 73 | + ContainsPtr *c = (ContainsPtr *)__builtin_operator_new(sizeof(ContainsPtr)); |
| 74 | + return c; |
| 75 | +} |
| 76 | + |
| 77 | +// CHECK-LABEL: define dso_local noundef ptr @_Z38test_operatornew_struct_array_with_ptrv( |
| 78 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 160) |
| 79 | +ContainsPtr *test_operatornew_struct_array_with_ptr() { |
| 80 | + ContainsPtr *c = (ContainsPtr *)__builtin_operator_new(10 * sizeof(ContainsPtr)); |
| 81 | + return c; |
| 82 | +} |
| 83 | + |
| 84 | +// CHECK-LABEL: define dso_local noundef ptr @_Z33test_operatornew_struct_with_ptr2v( |
| 85 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 16) |
| 86 | +ContainsPtr *test_operatornew_struct_with_ptr2() { |
| 87 | + ContainsPtr *c = (ContainsPtr *)__builtin_operator_new(sizeof(*c)); |
| 88 | + return c; |
| 89 | +} |
| 90 | + |
| 91 | +// CHECK-LABEL: define dso_local noundef ptr @_Z39test_operatornew_struct_array_with_ptr2v( |
| 92 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 160) |
| 93 | +ContainsPtr *test_operatornew_struct_array_with_ptr2() { |
| 94 | + ContainsPtr *c = (ContainsPtr *)__builtin_operator_new(10 * sizeof(*c)); |
| 95 | + return c; |
| 96 | +} |
| 97 | + |
| 98 | +// CHECK-LABEL: define dso_local noundef ptr @_Z24test_new_struct_with_ptrv( |
| 99 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 16){{.*}} !alloc_token [[META_CONTAINSPTR:![0-9]+]] |
| 100 | +ContainsPtr *test_new_struct_with_ptr() { |
| 101 | + return new ContainsPtr; |
| 102 | +} |
| 103 | + |
| 104 | +// CHECK-LABEL: define dso_local noundef ptr @_Z30test_new_struct_array_with_ptrv( |
| 105 | +// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 160){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 106 | +ContainsPtr *test_new_struct_array_with_ptr() { |
| 107 | + return new ContainsPtr[10]; |
| 108 | +} |
| 109 | + |
| 110 | +class TestClass { |
| 111 | +public: |
| 112 | + void Foo(); |
| 113 | + ~TestClass(); |
| 114 | + int data[16]; |
| 115 | +}; |
| 116 | + |
| 117 | +// CHECK-LABEL: define dso_local noundef ptr @_Z14test_new_classv( |
| 118 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 64){{.*}} !alloc_token [[META_TESTCLASS:![0-9]+]] |
| 119 | +TestClass *test_new_class() { |
| 120 | + return new TestClass(); |
| 121 | +} |
| 122 | + |
| 123 | +// CHECK-LABEL: define dso_local noundef ptr @_Z20test_new_class_arrayv( |
| 124 | +// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 648){{.*}} !alloc_token [[META_TESTCLASS]] |
| 125 | +TestClass *test_new_class_array() { |
| 126 | + return new TestClass[10]; |
| 127 | +} |
| 128 | + |
| 129 | +// Test that we detect that virtual classes have implicit vtable pointer. |
| 130 | +class VirtualTestClass { |
| 131 | +public: |
| 132 | + virtual void Foo(); |
| 133 | + virtual ~VirtualTestClass(); |
| 134 | + int data[16]; |
| 135 | +}; |
| 136 | + |
| 137 | +// CHECK-LABEL: define dso_local noundef ptr @_Z22test_new_virtual_classv( |
| 138 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 72){{.*}} !alloc_token [[META_VIRTUALTESTCLASS:![0-9]+]] |
| 139 | +VirtualTestClass *test_new_virtual_class() { |
| 140 | + return new VirtualTestClass(); |
| 141 | +} |
| 142 | + |
| 143 | +// CHECK-LABEL: define dso_local noundef ptr @_Z28test_new_virtual_class_arrayv( |
| 144 | +// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 728){{.*}} !alloc_token [[META_VIRTUALTESTCLASS]] |
| 145 | +VirtualTestClass *test_new_virtual_class_array() { |
| 146 | + return new VirtualTestClass[10]; |
| 147 | +} |
| 148 | + |
| 149 | +// uintptr_t is treated as a pointer. |
| 150 | +struct MyStructUintptr { |
| 151 | + int a; |
| 152 | + uintptr_t ptr; |
| 153 | +}; |
| 154 | + |
| 155 | +// CHECK-LABEL: define dso_local noundef ptr @_Z18test_uintptr_isptrv( |
| 156 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 16){{.*}} !alloc_token [[META_MYSTRUCTUINTPTR:![0-9]+]] |
| 157 | +MyStructUintptr *test_uintptr_isptr() { |
| 158 | + return new MyStructUintptr; |
| 159 | +} |
| 160 | + |
| 161 | +using uptr = uintptr_t; |
| 162 | +// CHECK-LABEL: define dso_local noundef ptr @_Z19test_uintptr_isptr2v( |
| 163 | +// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 8){{.*}} !alloc_token [[META_UINTPTR:![0-9]+]] |
| 164 | +uptr *test_uintptr_isptr2() { |
| 165 | + return new uptr; |
| 166 | +} |
| 167 | + |
| 168 | +// CHECK: [[META_INT]] = !{!"int", i1 false} |
| 169 | +// CHECK: [[META_ULONG]] = !{!"unsigned long", i1 false} |
| 170 | +// CHECK: [[META_INTPTR]] = !{!"int *", i1 true} |
| 171 | +// CHECK: [[META_CONTAINSPTR]] = !{!"ContainsPtr", i1 true} |
| 172 | +// CHECK: [[META_TESTCLASS]] = !{!"TestClass", i1 false} |
| 173 | +// CHECK: [[META_VIRTUALTESTCLASS]] = !{!"VirtualTestClass", i1 true} |
| 174 | +// CHECK: [[META_MYSTRUCTUINTPTR]] = !{!"MyStructUintptr", i1 true} |
| 175 | +// CHECK: [[META_UINTPTR]] = !{!"unsigned long", i1 true} |
0 commit comments