2019-01-31 04:34:53 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm %s -o - 2>&1 | FileCheck %s
|
|
|
|
// RUN: %clang_cc1 -DDYNAMIC -triple x86_64-apple-darwin -emit-llvm %s -o - 2>&1 | FileCheck %s
|
|
|
|
|
|
|
|
#ifdef DYNAMIC
|
|
|
|
#define OBJECT_SIZE_BUILTIN __builtin_dynamic_object_size
|
|
|
|
#else
|
|
|
|
#define OBJECT_SIZE_BUILTIN __builtin_object_size
|
|
|
|
#endif
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
#define NULL ((void *)0)
|
|
|
|
|
|
|
|
int gi;
|
|
|
|
|
|
|
|
typedef unsigned long size_t;
|
|
|
|
|
|
|
|
// CHECK-DAG-RE: define void @my_malloc({{.*}}) #[[MALLOC_ATTR_NUMBER:[0-9]+]]
|
|
|
|
// N.B. LLVM's allocsize arguments are base-0, whereas ours are base-1 (for
|
|
|
|
// compat with GCC)
|
|
|
|
// CHECK-DAG-RE: attributes #[[MALLOC_ATTR_NUMBER]] = {.*allocsize(0).*}
|
|
|
|
void *my_malloc(size_t) __attribute__((alloc_size(1)));
|
|
|
|
|
|
|
|
// CHECK-DAG-RE: define void @my_calloc({{.*}}) #[[CALLOC_ATTR_NUMBER:[0-9]+]]
|
|
|
|
// CHECK-DAG-RE: attributes #[[CALLOC_ATTR_NUMBER]] = {.*allocsize(0, 1).*}
|
|
|
|
void *my_calloc(size_t, size_t) __attribute__((alloc_size(1, 2)));
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test1
|
|
|
|
void test1() {
|
|
|
|
void *const vp = my_malloc(100);
|
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(vp, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(vp, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(vp, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(vp, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
void *const arr = my_calloc(100, 5);
|
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_malloc(100), 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_malloc(100), 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_malloc(100), 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_malloc(100), 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_calloc(100, 5), 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_calloc(100, 5), 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_calloc(100, 5), 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_calloc(100, 5), 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
void *const zeroPtr = my_malloc(0);
|
|
|
|
// CHECK: store i32 0
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(zeroPtr, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 0
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_malloc(0), 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
void *const zeroArr1 = my_calloc(0, 1);
|
|
|
|
void *const zeroArr2 = my_calloc(1, 0);
|
|
|
|
// CHECK: store i32 0
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(zeroArr1, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 0
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(zeroArr2, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 0
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_calloc(1, 0), 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 0
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_calloc(0, 1), 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test2
|
|
|
|
void test2() {
|
|
|
|
void *const vp = my_malloc(gi);
|
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(vp, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
void *const arr1 = my_calloc(gi, 1);
|
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr1, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
void *const arr2 = my_calloc(1, gi);
|
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr2, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test3
|
|
|
|
void test3() {
|
|
|
|
char *const buf = (char *)my_calloc(100, 5);
|
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Data {
|
|
|
|
int a;
|
|
|
|
int t[10];
|
|
|
|
char pad[3];
|
|
|
|
char end[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test5
|
|
|
|
void test5() {
|
|
|
|
struct Data *const data = my_malloc(sizeof(*data));
|
|
|
|
// CHECK: store i32 48
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 48
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 48
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 48
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
// CHECK: store i32 40
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(&data->t[1], 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 36
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(&data->t[1], 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 40
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(&data->t[1], 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 36
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(&data->t[1], 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
struct Data *const arr = my_calloc(sizeof(*data), 2);
|
|
|
|
// CHECK: store i32 96
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 96
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 96
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 96
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
// CHECK: store i32 88
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(&arr->t[1], 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 36
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(&arr->t[1], 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 88
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(&arr->t[1], 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 36
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(&arr->t[1], 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test6
|
|
|
|
void test6() {
|
|
|
|
// Things that would normally trigger conservative estimates don't need to do
|
|
|
|
// so when we know the source of the allocation.
|
|
|
|
struct Data *const data = my_malloc(sizeof(*data) + 10);
|
|
|
|
// CHECK: store i32 11
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data->end, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 11
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data->end, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 11
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data->end, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 11
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data->end, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
struct Data *const arr = my_calloc(sizeof(*arr) + 5, 3);
|
|
|
|
// AFAICT, GCC treats malloc and calloc identically. So, we should do the
|
|
|
|
// same.
|
|
|
|
//
|
|
|
|
// Additionally, GCC ignores the initial array index when determining whether
|
|
|
|
// we're writing off the end of an alloc_size base. e.g.
|
|
|
|
// arr[0].end
|
|
|
|
// arr[1].end
|
|
|
|
// arr[2].end
|
|
|
|
// ...Are all considered "writing off the end", because there's no way to tell
|
|
|
|
// with high accuracy if the user meant "allocate a single N-byte `Data`",
|
|
|
|
// or "allocate M smaller `Data`s with extra padding".
|
|
|
|
|
|
|
|
// CHECK: store i32 112
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr->end, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 112
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr->end, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 112
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr->end, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 112
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr->end, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
// CHECK: store i32 112
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[0].end, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 112
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[0].end, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 112
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[0].end, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 112
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[0].end, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
// CHECK: store i32 64
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[1].end, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 64
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[1].end, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 64
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[1].end, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 64
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[1].end, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
// CHECK: store i32 16
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[2].end, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 16
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[2].end, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 16
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[2].end, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 16
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr[2].end, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test7
|
|
|
|
void test7() {
|
|
|
|
struct Data *const data = my_malloc(sizeof(*data) + 5);
|
|
|
|
// CHECK: store i32 9
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data->pad, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 3
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data->pad, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 9
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data->pad, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 3
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(data->pad, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test8
|
|
|
|
void test8() {
|
|
|
|
// Non-const pointers aren't currently supported.
|
|
|
|
void *buf = my_calloc(100, 5);
|
2019-01-31 04:34:53 +08:00
|
|
|
// CHECK: @llvm.objectsize.i64.p0i8(i8* %{{.*}}, i1 false, i1 true, i1
|
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 0
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test9
|
|
|
|
void test9() {
|
|
|
|
// Check to be sure that we unwrap things correctly.
|
|
|
|
short *const buf0 = (my_malloc(100));
|
|
|
|
short *const buf1 = (short*)(my_malloc(100));
|
|
|
|
short *const buf2 = ((short*)(my_malloc(100)));
|
|
|
|
|
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf0, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf1, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf2, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test10
|
|
|
|
void test10() {
|
|
|
|
// Yay overflow
|
|
|
|
short *const arr = my_calloc((size_t)-1 / 2 + 1, 2);
|
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 0
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
// As an implementation detail, CharUnits can't handle numbers greater than or
|
|
|
|
// equal to 2**63. Realistically, this shouldn't be a problem, but we should
|
|
|
|
// be sure we don't emit crazy results for this case.
|
|
|
|
short *const buf = my_malloc((size_t)-1);
|
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 0
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(buf, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
short *const arr_big = my_calloc((size_t)-1 / 2 - 1, 2);
|
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr_big, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr_big, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr_big, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 0
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr_big, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *my_tiny_malloc(char) __attribute__((alloc_size(1)));
|
|
|
|
void *my_tiny_calloc(char, char) __attribute__((alloc_size(1, 2)));
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test11
|
|
|
|
void test11() {
|
|
|
|
void *const vp = my_tiny_malloc(100);
|
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(vp, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(vp, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(vp, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(vp, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
// N.B. This causes char overflow, but not size_t overflow, so it should be
|
|
|
|
// supported.
|
|
|
|
void *const arr = my_tiny_calloc(100, 5);
|
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 1);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 2);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr, 3);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *my_signed_malloc(long) __attribute__((alloc_size(1)));
|
|
|
|
void *my_signed_calloc(long, long) __attribute__((alloc_size(1, 2)));
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test12
|
|
|
|
void test12() {
|
|
|
|
// CHECK: store i32 100
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_signed_malloc(100), 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 500
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_signed_calloc(100, 5), 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
void *const vp = my_signed_malloc(-2);
|
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(vp, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// N.B. These get lowered to -1 because the function calls may have
|
|
|
|
// side-effects, and we can't determine the objectsize.
|
|
|
|
// CHECK: store i32 -1
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_signed_malloc(-2), 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
|
|
|
|
void *const arr1 = my_signed_calloc(-2, 1);
|
|
|
|
void *const arr2 = my_signed_calloc(1, -2);
|
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr1, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: @llvm.objectsize
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(arr2, 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 -1
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_signed_calloc(1, -2), 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
// CHECK: store i32 -1
|
2019-01-31 04:34:53 +08:00
|
|
|
gi = OBJECT_SIZE_BUILTIN(my_signed_calloc(-2, 1), 0);
|
2016-12-22 10:50:20 +08:00
|
|
|
}
|
[APSInt][OpenMP] Fix isNegative, etc. for unsigned types
Without this patch, APSInt inherits APInt::isNegative, which merely
checks the sign bit without regard to whether the type is actually
signed. isNonNegative and isStrictlyPositive call isNegative and so
are also affected.
This patch adjusts APSInt to override isNegative, isNonNegative, and
isStrictlyPositive with implementations that consider whether the type
is signed.
A large set of Clang OpenMP tests are affected. Without this patch,
these tests assume that `true` is not a valid argument for clauses
like `collapse`. Indeed, `true` fails APInt::isStrictlyPositive but
not APSInt::isStrictlyPositive. This patch adjusts those tests to
assume `true` should be accepted.
This patch also adds tests revealing various other similar fixes due
to APSInt::isNegative calls in Clang's ExprConstant.cpp and
SemaExpr.cpp: `++` and `--` overflow in `constexpr`, evaluated object
size based on `alloc_size`, `<<` and `>>` shift count validation, and
OpenMP array section validation.
Reviewed By: lebedev.ri, ABataev, hfinkel
Differential Revision: https://reviews.llvm.org/D59712
llvm-svn: 359012
2019-04-24 01:04:15 +08:00
|
|
|
|
|
|
|
void *alloc_uchar(unsigned char) __attribute__((alloc_size(1)));
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test13
|
|
|
|
void test13() {
|
|
|
|
// If 128 were incorrectly seen as negative, the result would become -1.
|
|
|
|
// CHECK: store i32 128,
|
|
|
|
gi = OBJECT_SIZE_BUILTIN(alloc_uchar(128), 0);
|
|
|
|
}
|
2018-12-06 20:22:08 +08:00
|
|
|
|
|
|
|
void *(*malloc_function_pointer)(int)__attribute__((alloc_size(1)));
|
|
|
|
void *(*calloc_function_pointer)(int, int)__attribute__((alloc_size(1, 2)));
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test_fn_pointer
|
|
|
|
void test_fn_pointer() {
|
|
|
|
void *const vp = malloc_function_pointer(100);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(vp, 0);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(vp, 1);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(vp, 2);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(vp, 3);
|
|
|
|
|
|
|
|
void *const arr = calloc_function_pointer(100, 5);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(arr, 0);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(arr, 1);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(arr, 2);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(arr, 3);
|
|
|
|
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(malloc_function_pointer(100), 0);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(malloc_function_pointer(100), 1);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(malloc_function_pointer(100), 2);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(malloc_function_pointer(100), 3);
|
|
|
|
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer(100, 5), 0);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer(100, 5), 1);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer(100, 5), 2);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer(100, 5), 3);
|
|
|
|
|
|
|
|
void *const zeroPtr = malloc_function_pointer(0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(zeroPtr, 0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(malloc_function_pointer(0), 0);
|
|
|
|
|
|
|
|
void *const zeroArr1 = calloc_function_pointer(0, 1);
|
|
|
|
void *const zeroArr2 = calloc_function_pointer(1, 0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(zeroArr1, 0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(zeroArr2, 0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer(1, 0), 0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer(0, 1), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void *(__attribute__((warn_unused_result, alloc_size(1))) * my_malloc_function_pointer_type)(int);
|
|
|
|
typedef void *(__attribute__((alloc_size(1, 2))) * my_calloc_function_pointer_type)(int, int);
|
|
|
|
extern my_malloc_function_pointer_type malloc_function_pointer_with_typedef;
|
|
|
|
extern my_calloc_function_pointer_type calloc_function_pointer_with_typedef;
|
|
|
|
|
|
|
|
// CHECK-LABEL: @test_fn_pointer_typedef
|
|
|
|
void test_fn_pointer_typedef() {
|
|
|
|
malloc_function_pointer_with_typedef(100);
|
|
|
|
void *const vp = malloc_function_pointer_with_typedef(100);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(vp, 0);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(vp, 1);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(vp, 2);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(vp, 3);
|
|
|
|
|
|
|
|
void *const arr = calloc_function_pointer_with_typedef(100, 5);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(arr, 0);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(arr, 1);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(arr, 2);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(arr, 3);
|
|
|
|
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(malloc_function_pointer_with_typedef(100), 0);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(malloc_function_pointer_with_typedef(100), 1);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(malloc_function_pointer_with_typedef(100), 2);
|
|
|
|
// CHECK: store i32 100
|
|
|
|
gi = __builtin_object_size(malloc_function_pointer_with_typedef(100), 3);
|
|
|
|
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer_with_typedef(100, 5), 0);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer_with_typedef(100, 5), 1);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer_with_typedef(100, 5), 2);
|
|
|
|
// CHECK: store i32 500
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer_with_typedef(100, 5), 3);
|
|
|
|
|
|
|
|
void *const zeroPtr = malloc_function_pointer_with_typedef(0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(zeroPtr, 0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(malloc_function_pointer_with_typedef(0), 0);
|
|
|
|
|
|
|
|
void *const zeroArr1 = calloc_function_pointer_with_typedef(0, 1);
|
|
|
|
void *const zeroArr2 = calloc_function_pointer_with_typedef(1, 0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(zeroArr1, 0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(zeroArr2, 0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer_with_typedef(1, 0), 0);
|
|
|
|
// CHECK: store i32 0
|
|
|
|
gi = __builtin_object_size(calloc_function_pointer_with_typedef(0, 1), 0);
|
|
|
|
}
|