xref: /llvm-project/clang/test/CodeGen/alloc-size-fnptr.c (revision 39db5e1ed87363a9ffea81e53520b542201b3262)
1 // Check that the alloc_size attribute is propagated to the call instruction
2 // for both direct and indirect calls
3 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm %s -o - 2>&1 | FileCheck %s
4 
5 #define NULL ((void *)0)
6 
7 int gi;
8 
9 typedef unsigned long size_t;
10 
11 extern void *my_malloc(int) __attribute__((alloc_size(1)));
12 extern void *my_calloc(int, int) __attribute__((alloc_size(1, 2)));
13 
14 // CHECK-LABEL: @call_direct
call_direct(void)15 void call_direct(void) {
16   my_malloc(50);
17   // CHECK: call ptr @my_malloc(i32 noundef 50) [[DIRECT_MALLOC_ATTR:#[0-9]+]]
18   my_calloc(1, 16);
19   // CHECK: call ptr @my_calloc(i32 noundef 1, i32 noundef 16) [[DIRECT_CALLOC_ATTR:#[0-9]+]]
20 }
21 
22 extern void *(*malloc_function_pointer)(void *, int)__attribute__((alloc_size(2)));
23 extern void *(*calloc_function_pointer)(void *, int, int)__attribute__((alloc_size(2, 3)));
24 
25 // CHECK-LABEL: @call_function_pointer
call_function_pointer(void)26 void call_function_pointer(void) {
27   malloc_function_pointer(NULL, 100);
28   // CHECK: [[MALLOC_FN_PTR:%.+]] = load ptr, ptr @malloc_function_pointer, align 8
29   // CHECK: call ptr [[MALLOC_FN_PTR]](ptr noundef null, i32 noundef 100) [[INDIRECT_MALLOC_ATTR:#[0-9]+]]
30   calloc_function_pointer(NULL, 2, 4);
31   // CHECK: [[CALLOC_FN_PTR:%.+]] = load ptr, ptr @calloc_function_pointer, align 8
32   // CHECK: call ptr [[CALLOC_FN_PTR]](ptr noundef null, i32 noundef 2, i32 noundef 4) [[INDIRECT_CALLOC_ATTR:#[0-9]+]]
33 }
34 
35 typedef void *(__attribute__((alloc_size(3))) * my_malloc_fn_pointer_type)(void *, void *, int);
36 typedef void *(__attribute__((alloc_size(3, 4))) * my_calloc_fn_pointer_type)(void *, void *, int, int);
37 extern my_malloc_fn_pointer_type malloc_function_pointer_with_typedef;
38 extern my_calloc_fn_pointer_type calloc_function_pointer_with_typedef;
39 
40 // CHECK-LABEL: @call_function_pointer_typedef
call_function_pointer_typedef(void)41 void call_function_pointer_typedef(void) {
42   malloc_function_pointer_with_typedef(NULL, NULL, 200);
43   // CHECK: [[INDIRECT_TYPEDEF_MALLOC_FN_PTR:%.+]] = load ptr, ptr @malloc_function_pointer_with_typedef, align 8
44   // CHECK: call ptr [[INDIRECT_TYPEDEF_MALLOC_FN_PTR]](ptr noundef null, ptr noundef null, i32 noundef 200) [[INDIRECT_TYPEDEF_MALLOC_ATTR:#[0-9]+]]
45   calloc_function_pointer_with_typedef(NULL, NULL, 8, 4);
46   // CHECK: [[INDIRECT_TYPEDEF_CALLOC_FN_PTR:%.+]] = load ptr, ptr @calloc_function_pointer_with_typedef, align 8
47   // CHECK: call ptr [[INDIRECT_TYPEDEF_CALLOC_FN_PTR]](ptr noundef null, ptr noundef null, i32 noundef 8, i32 noundef 4) [[INDIRECT_TYPEDEF_CALLOC_ATTR:#[0-9]+]]
48 }
49 
50 // CHECK: attributes [[DIRECT_MALLOC_ATTR]] = { allocsize(0) }
51 // CHECK: attributes [[DIRECT_CALLOC_ATTR]] = { allocsize(0,1) }
52 // CHECK: attributes [[INDIRECT_MALLOC_ATTR]] = { allocsize(1) }
53 // CHECK: attributes [[INDIRECT_CALLOC_ATTR]] = { allocsize(1,2) }
54 // CHECK: attributes [[INDIRECT_TYPEDEF_MALLOC_ATTR]] = { allocsize(2) }
55 // CHECK: attributes [[INDIRECT_TYPEDEF_CALLOC_ATTR]] = { allocsize(2,3) }
56