brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.1 KiB · 9884220 Raw
156 lines · cpp
1// RUN: %clang_cc1 -fsanitize=alloc-token -triple x86_64-linux-gnu -std=c++20 -fexceptions -fcxx-exceptions -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s2 3#include "../Analysis/Inputs/system-header-simulator-cxx.h"4extern "C" {5void *aligned_alloc(size_t alignment, size_t size) __attribute__((malloc));6void *malloc(size_t size) __attribute__((malloc));7void *calloc(size_t num, size_t size) __attribute__((malloc));8void *realloc(void *ptr, size_t size) __attribute__((malloc));9void *reallocarray(void *ptr, size_t nmemb, size_t size) __attribute__((malloc));10void *memalign(size_t alignment, size_t size) __attribute__((malloc));11void *valloc(size_t size) __attribute__((malloc));12void *pvalloc(size_t size) __attribute__((malloc));13int posix_memalign(void **memptr, size_t alignment, size_t size);14 15struct __sized_ptr_t {16  void *p;17  size_t n;18};19enum class __hot_cold_t : uint8_t;20__sized_ptr_t __size_returning_new(size_t size) __attribute__((malloc_span));21__sized_ptr_t __size_returning_new_hot_cold(size_t, __hot_cold_t) __attribute__((malloc_span));22__sized_ptr_t __size_returning_new_aligned(size_t, std::align_val_t) __attribute__((malloc_span));23__sized_ptr_t __size_returning_new_aligned_hot_cold(size_t, std::align_val_t,  __hot_cold_t) __attribute__((malloc_span));24}25 26void *sink; // prevent optimizations from removing the calls27 28// CHECK-LABEL: define dso_local void @_Z16test_malloc_likev(29// CHECK: call noalias ptr @malloc(i64 noundef 4){{.*}} !alloc_token [[META_INT:![0-9]+]]30// CHECK: call noalias ptr @calloc(i64 noundef 3, i64 noundef 4){{.*}} !alloc_token [[META_INT]]31// CHECK: call noalias ptr @realloc(ptr noundef {{.*}}, i64 noundef 8){{.*}} !alloc_token [[META_LONG:![0-9]+]]32// CHECK: call noalias ptr @reallocarray(ptr noundef {{.*}}, i64 noundef 5, i64 noundef 8), !alloc_token [[META_LONG]]33// CHECK: call noalias align 128 ptr @aligned_alloc(i64 noundef 128, i64 noundef 4){{.*}} !alloc_token [[META_INT]]34// CHECK: call noalias ptr @memalign(i64 noundef 16, i64 noundef 4), !alloc_token [[META_INT]]35// CHECK: call noalias ptr @valloc(i64 noundef 4), !alloc_token [[META_INT]]36// CHECK: call noalias ptr @pvalloc(i64 noundef 4), !alloc_token [[META_INT]]37// CHECK: call i32 @posix_memalign(ptr noundef @sink, i64 noundef 64, i64 noundef 4)38void test_malloc_like() {39  sink = malloc(sizeof(int));40  sink = calloc(3, sizeof(int));41  sink = realloc(sink, sizeof(long));42  sink = reallocarray(sink, 5, sizeof(long));43  sink = aligned_alloc(128, sizeof(int));44  sink = memalign(16, sizeof(int));45  sink = valloc(sizeof(int));46  sink = pvalloc(sizeof(int));47  posix_memalign(&sink, 64, sizeof(int)); // FIXME: support posix_memalign48}49 50class ForwardDecl;51 52// CHECK-LABEL: define dso_local void @_Z21test_malloc_like_castv(53// CHECK: call noalias ptr @malloc(i64 noundef 64){{.*}} !alloc_token [[META_INT]]54// CHECK: call noalias ptr @malloc(i64 noundef 64){{.*}} !alloc_token [[META_INT]]55// CHECK-NOT: call noalias ptr @malloc(i64 noundef 64){{.*}} !alloc_token [[META_INT]]56void test_malloc_like_cast() {57  sink = (int *)malloc(64);58  sink = reinterpret_cast<int *>(malloc(64));59  // Always fails to assign token ID for incomplete types.60  sink = reinterpret_cast<ForwardDecl *>(malloc(64));61}62 63// CHECK-LABEL: define dso_local void @_Z17test_operator_newv(64// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4){{.*}} !alloc_token [[META_INT]]65// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4){{.*}} !alloc_token [[META_INT]]66void test_operator_new() {67  sink = __builtin_operator_new(sizeof(int));68  sink = ::operator new(sizeof(int));69}70 71// CHECK-LABEL: define dso_local void @_Z25test_operator_new_nothrowv(72// CHECK: call noalias noundef ptr @_ZnwmRKSt9nothrow_t(i64 noundef 4, ptr noundef nonnull align 1 dereferenceable(1) @_ZSt7nothrow){{.*}} !alloc_token [[META_INT]]73// CHECK: call noalias noundef ptr @_ZnwmRKSt9nothrow_t(i64 noundef 4, ptr noundef nonnull align 1 dereferenceable(1) @_ZSt7nothrow){{.*}} !alloc_token [[META_INT]]74void test_operator_new_nothrow() {75  sink = __builtin_operator_new(sizeof(int), std::nothrow);76  sink = ::operator new(sizeof(int), std::nothrow);77}78 79// CHECK-LABEL: define dso_local noundef ptr @_Z8test_newv(80// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4){{.*}} !alloc_token [[META_INT]]81int *test_new() {82  return new int;83}84 85// CHECK-LABEL: define dso_local noundef ptr @_Z14test_new_arrayv(86// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 40){{.*}} !alloc_token [[META_INT]]87int *test_new_array() {88  return new int[10];89}90 91// CHECK-LABEL: define dso_local noundef ptr @_Z16test_new_nothrowv(92// CHECK: call noalias noundef ptr @_ZnwmRKSt9nothrow_t(i64 noundef 4, ptr noundef nonnull align 1 dereferenceable(1) @_ZSt7nothrow){{.*}} !alloc_token [[META_INT]]93int *test_new_nothrow() {94  return new (std::nothrow) int;95}96 97// CHECK-LABEL: define dso_local noundef ptr @_Z22test_new_array_nothrowv(98// CHECK: call noalias noundef ptr @_ZnamRKSt9nothrow_t(i64 noundef 40, ptr noundef nonnull align 1 dereferenceable(1) @_ZSt7nothrow){{.*}} !alloc_token [[META_INT]]99int *test_new_array_nothrow() {100  return new (std::nothrow) int[10];101}102 103// CHECK-LABEL: define dso_local void @_Z23test_size_returning_newv(104// CHECK: call { ptr, i64 } @__size_returning_new(i64 noundef 8){{.*}} !alloc_token [[META_LONG]]105// CHECK: call { ptr, i64 } @__size_returning_new_hot_cold(i64 noundef 8, i8 noundef zeroext 1){{.*}} !alloc_token [[META_LONG]]106// CHECK: call { ptr, i64 } @__size_returning_new_aligned(i64 noundef 8, i64 noundef 32){{.*}} !alloc_token [[META_LONG]]107// CHECK: call { ptr, i64 } @__size_returning_new_aligned_hot_cold(i64 noundef 8, i64 noundef 32, i8 noundef zeroext 1){{.*}}_token [[META_LONG]]108void test_size_returning_new() {109  sink = __size_returning_new(sizeof(long)).p;110  sink = __size_returning_new_hot_cold(sizeof(long), __hot_cold_t{1}).p;111  sink = __size_returning_new_aligned(sizeof(long), std::align_val_t{32}).p;112  sink = __size_returning_new_aligned_hot_cold(sizeof(long), std::align_val_t{32}, __hot_cold_t{1}).p;113}114 115class TestClass {116public:117  virtual void Foo();118  virtual ~TestClass();119  int data[16];120};121 122void may_throw();123 124// CHECK-LABEL: define dso_local noundef ptr @_Z27test_exception_handling_newv(125// CHECK: invoke noalias noundef nonnull ptr @_Znwm(i64 noundef 72)126// CHECK-NEXT: !alloc_token [[META_TESTCLASS:![0-9]+]]127TestClass *test_exception_handling_new() {128  try {129    TestClass *obj = new TestClass();130    may_throw();131    return obj;132  } catch (...) {133    return nullptr;134  }135}136 137// CHECK-LABEL: define dso_local noundef ptr @_Z14test_new_classv(138// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 72){{.*}} !alloc_token [[META_TESTCLASS]]139TestClass *test_new_class() {140  TestClass *obj = new TestClass();141  obj->data[0] = 42;142  return obj;143}144 145// CHECK-LABEL: define dso_local noundef ptr @_Z20test_new_class_arrayv(146// CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 728){{.*}} !alloc_token [[META_TESTCLASS]]147TestClass *test_new_class_array() {148  TestClass* arr = new TestClass[10];149  arr[0].data[0] = 123;150  return arr;151}152 153// CHECK: [[META_INT]] = !{!"int", i1 false}154// CHECK: [[META_LONG]] = !{!"long", i1 false}155// CHECK: [[META_TESTCLASS]] = !{!"TestClass", i1 true}156