| 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) __attribute__((malloc)); |
| 9 |
} |
| 10 |
|
| 11 |
void *sink; // prevent optimizations from removing the calls |
| 12 |
|
| 13 |
// CHECK-LABEL: define dso_local noundef ptr @_Z15test_malloc_intv( |
| 14 |
// CHECK: call noalias ptr @malloc(i64 noundef 4){{.*}} !alloc_token [[META_INT:![0-9]+]] |
| 15 |
void *test_malloc_int() { |
| 16 |
int *a = (int *)malloc(sizeof(int)); |
| 17 |
*a = 42; |
| 18 |
return a; |
| 19 |
} |
| 20 |
|
| 21 |
// CHECK-LABEL: define dso_local noundef ptr @_Z15test_malloc_ptrv( |
| 22 |
// CHECK: call noalias ptr @malloc(i64 noundef 8){{.*}} !alloc_token [[META_INTPTR:![0-9]+]] |
| 23 |
int **test_malloc_ptr() { |
| 24 |
int **a = (int **)malloc(sizeof(int*)); |
| 25 |
*a = nullptr; |
| 26 |
return a; |
| 27 |
} |
| 28 |
|
| 29 |
// CHECK-LABEL: define dso_local noundef ptr @_Z12test_new_intv( |
| 30 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4){{.*}} !alloc_token [[META_INT]] |
| 31 |
int *test_new_int() { |
| 32 |
return new int; |
| 33 |
} |
| 34 |
|
| 35 |
// CHECK-LABEL: define dso_local noundef ptr @_Z20test_new_ulong_arrayv( |
| 36 |
// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 80){{.*}} !alloc_token [[META_ULONG:![0-9]+]] |
| 37 |
unsigned long *test_new_ulong_array() { |
| 38 |
return new unsigned long[10]; |
| 39 |
} |
| 40 |
|
| 41 |
// CHECK-LABEL: define dso_local noundef ptr @_Z12test_new_ptrv( |
| 42 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 8){{.*}} !alloc_token [[META_INTPTR]] |
| 43 |
int **test_new_ptr() { |
| 44 |
return new int*; |
| 45 |
} |
| 46 |
|
| 47 |
// CHECK-LABEL: define dso_local noundef ptr @_Z18test_new_ptr_arrayv( |
| 48 |
// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 80){{.*}} !alloc_token [[META_INTPTR]] |
| 49 |
int **test_new_ptr_array() { |
| 50 |
return new int*[10]; |
| 51 |
} |
| 52 |
|
| 53 |
struct ContainsPtr { |
| 54 |
int a; |
| 55 |
char *buf; |
| 56 |
}; |
| 57 |
|
| 58 |
// CHECK-LABEL: define dso_local noundef ptr @_Z27test_malloc_struct_with_ptrv( |
| 59 |
// CHECK: call noalias ptr @malloc(i64 noundef 16){{.*}} !alloc_token [[META_CONTAINSPTR:![0-9]+]] |
| 60 |
void *test_malloc_struct_with_ptr() { |
| 61 |
return malloc(sizeof(ContainsPtr)); |
| 62 |
} |
| 63 |
|
| 64 |
// CHECK-LABEL: define dso_local noundef ptr @_Z33test_malloc_struct_array_with_ptrv( |
| 65 |
// CHECK: call noalias ptr @malloc(i64 noundef 160){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 66 |
void *test_malloc_struct_array_with_ptr() { |
| 67 |
return malloc(10 * sizeof(ContainsPtr)); |
| 68 |
} |
| 69 |
|
| 70 |
// CHECK-LABEL: define dso_local noundef ptr @_Z31test_malloc_with_ptr_sizeof_vari( |
| 71 |
// CHECK: call noalias ptr @malloc(i64 noundef {{.*}}){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 72 |
void *test_malloc_with_ptr_sizeof_var(int x) { |
| 73 |
unsigned long size = sizeof(ContainsPtr); |
| 74 |
size *= x; |
| 75 |
return malloc(size); |
| 76 |
} |
| 77 |
|
| 78 |
// CHECK-LABEL: define dso_local noundef ptr @_Z29test_malloc_with_ptr_castonlyv( |
| 79 |
// CHECK: call noalias ptr @malloc(i64 noundef 4096){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 80 |
ContainsPtr *test_malloc_with_ptr_castonly() { |
| 81 |
return (ContainsPtr *)malloc(4096); |
| 82 |
} |
| 83 |
|
| 84 |
// CHECK-LABEL: define dso_local noundef ptr @_Z32test_operatornew_struct_with_ptrv( |
| 85 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 16){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 86 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 16){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 87 |
ContainsPtr *test_operatornew_struct_with_ptr() { |
| 88 |
ContainsPtr *c = (ContainsPtr *)__builtin_operator_new(sizeof(ContainsPtr)); |
| 89 |
sink = ::operator new(sizeof(ContainsPtr)); |
| 90 |
return c; |
| 91 |
} |
| 92 |
|
| 93 |
// CHECK-LABEL: define dso_local noundef ptr @_Z38test_operatornew_struct_array_with_ptrv( |
| 94 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 160){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 95 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 160){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 96 |
ContainsPtr *test_operatornew_struct_array_with_ptr() { |
| 97 |
ContainsPtr *c = (ContainsPtr *)__builtin_operator_new(10 * sizeof(ContainsPtr)); |
| 98 |
sink = ::operator new(10 * sizeof(ContainsPtr)); |
| 99 |
return c; |
| 100 |
} |
| 101 |
|
| 102 |
// CHECK-LABEL: define dso_local noundef ptr @_Z33test_operatornew_struct_with_ptr2v( |
| 103 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 16){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 104 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 16){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 105 |
ContainsPtr *test_operatornew_struct_with_ptr2() { |
| 106 |
ContainsPtr *c = (ContainsPtr *)__builtin_operator_new(sizeof(*c)); |
| 107 |
sink = ::operator new(sizeof(*c)); |
| 108 |
return c; |
| 109 |
} |
| 110 |
|
| 111 |
// CHECK-LABEL: define dso_local noundef ptr @_Z39test_operatornew_struct_array_with_ptr2v( |
| 112 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 160){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 113 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 160){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 114 |
ContainsPtr *test_operatornew_struct_array_with_ptr2() { |
| 115 |
ContainsPtr *c = (ContainsPtr *)__builtin_operator_new(10 * sizeof(*c)); |
| 116 |
sink = ::operator new(10 * sizeof(*c)); |
| 117 |
return c; |
| 118 |
} |
| 119 |
|
| 120 |
// CHECK-LABEL: define dso_local noundef ptr @_Z24test_new_struct_with_ptrv( |
| 121 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 16){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 122 |
ContainsPtr *test_new_struct_with_ptr() { |
| 123 |
return new ContainsPtr; |
| 124 |
} |
| 125 |
|
| 126 |
// CHECK-LABEL: define dso_local noundef ptr @_Z30test_new_struct_array_with_ptrv( |
| 127 |
// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 160){{.*}} !alloc_token [[META_CONTAINSPTR]] |
| 128 |
ContainsPtr *test_new_struct_array_with_ptr() { |
| 129 |
return new ContainsPtr[10]; |
| 130 |
} |
| 131 |
|
| 132 |
class TestClass { |
| 133 |
public: |
| 134 |
void Foo(); |
| 135 |
~TestClass(); |
| 136 |
int data[16]; |
| 137 |
}; |
| 138 |
|
| 139 |
// CHECK-LABEL: define dso_local noundef ptr @_Z14test_new_classv( |
| 140 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 64){{.*}} !alloc_token [[META_TESTCLASS:![0-9]+]] |
| 141 |
TestClass *test_new_class() { |
| 142 |
return new TestClass(); |
| 143 |
} |
| 144 |
|
| 145 |
// CHECK-LABEL: define dso_local noundef ptr @_Z20test_new_class_arrayv( |
| 146 |
// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 648){{.*}} !alloc_token [[META_TESTCLASS]] |
| 147 |
TestClass *test_new_class_array() { |
| 148 |
return new TestClass[10]; |
| 149 |
} |
| 150 |
|
| 151 |
// Test that we detect that virtual classes have implicit vtable pointer. |
| 152 |
class VirtualTestClass { |
| 153 |
public: |
| 154 |
virtual void Foo(); |
| 155 |
virtual ~VirtualTestClass(); |
| 156 |
int data[16]; |
| 157 |
}; |
| 158 |
|
| 159 |
// CHECK-LABEL: define dso_local noundef ptr @_Z22test_new_virtual_classv( |
| 160 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 72){{.*}} !alloc_token [[META_VIRTUALTESTCLASS:![0-9]+]] |
| 161 |
VirtualTestClass *test_new_virtual_class() { |
| 162 |
return new VirtualTestClass(); |
| 163 |
} |
| 164 |
|
| 165 |
// CHECK-LABEL: define dso_local noundef ptr @_Z28test_new_virtual_class_arrayv( |
| 166 |
// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 728){{.*}} !alloc_token [[META_VIRTUALTESTCLASS]] |
| 167 |
VirtualTestClass *test_new_virtual_class_array() { |
| 168 |
return new VirtualTestClass[10]; |
| 169 |
} |
| 170 |
|
| 171 |
// uintptr_t is treated as a pointer. |
| 172 |
struct MyStructUintptr { |
| 173 |
int a; |
| 174 |
uintptr_t ptr; |
| 175 |
}; |
| 176 |
|
| 177 |
// CHECK-LABEL: define dso_local noundef ptr @_Z18test_uintptr_isptrv( |
| 178 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 16){{.*}} !alloc_token [[META_MYSTRUCTUINTPTR:![0-9]+]] |
| 179 |
MyStructUintptr *test_uintptr_isptr() { |
| 180 |
return new MyStructUintptr; |
| 181 |
} |
| 182 |
|
| 183 |
using uptr = uintptr_t; |
| 184 |
// CHECK-LABEL: define dso_local noundef ptr @_Z19test_uintptr_isptr2v( |
| 185 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 8){{.*}} !alloc_token [[META_UINTPTR:![0-9]+]] |
| 186 |
uptr *test_uintptr_isptr2() { |
| 187 |
return new uptr; |
| 188 |
} |
| 189 |
|
| 190 |
// CHECK: [[META_INT]] = !{!"int", i1 false} |
| 191 |
// CHECK: [[META_INTPTR]] = !{!"int *", i1 true} |
| 192 |
// CHECK: [[META_ULONG]] = !{!"unsigned long", i1 false} |
| 193 |
// CHECK: [[META_CONTAINSPTR]] = !{!"ContainsPtr", i1 true} |
| 194 |
// CHECK: [[META_TESTCLASS]] = !{!"TestClass", i1 false} |
| 195 |
// CHECK: [[META_VIRTUALTESTCLASS]] = !{!"VirtualTestClass", i1 true} |
| 196 |
// CHECK: [[META_MYSTRUCTUINTPTR]] = !{!"MyStructUintptr", i1 true} |
| 197 |
// CHECK: [[META_UINTPTR]] = !{!"unsigned long", i1 true} |