]>
Commit | Line | Data |
---|---|---|
fe7f75cf MS |
1 | /* PR middle-end/94527 - Add an attribute that marks a function as freeing |
2 | an object | |
3 | Verify that attribute malloc with one or two arguments has the expected | |
4 | effect on diagnostics. | |
5 | { dg-options "-Wall -ftrack-macro-expansion=0" } */ | |
6 | ||
7 | #define A(...) __attribute__ ((malloc (__VA_ARGS__), noipa)) | |
8 | ||
9 | typedef __SIZE_TYPE__ size_t; | |
10 | typedef struct A A; | |
11 | typedef struct B B; | |
12 | ||
13 | /* A pointer returned by any of the four functions must be deallocated | |
14 | either by dealloc() or by realloc_{A,B}(). */ | |
15 | A (__builtin_free) A* alloc_A (int); | |
16 | A (__builtin_free) B* alloc_B (int); | |
17 | A (__builtin_free) A* realloc_A (A *p, int n) { return p; } | |
18 | A (__builtin_free) B* realloc_B (B *p, int n) { return p; } | |
19 | ||
20 | A (realloc_A) A* alloc_A (int); | |
21 | A (realloc_B) B* alloc_B (int); | |
22 | A (realloc_A) A* realloc_A (A*, int); | |
23 | A (realloc_B) B* realloc_B (B*, int); | |
24 | ||
25 | void dealloc (void*); | |
26 | A (dealloc) void* alloc (int); | |
27 | ||
28 | void sink (void*); | |
29 | ||
30 | void test_alloc_A (void) | |
31 | { | |
32 | { | |
33 | void *p = alloc_A (1); | |
34 | p = realloc_A (p, 2); | |
35 | __builtin_free (p); | |
36 | } | |
37 | ||
38 | { | |
39 | void *p = alloc_A (1); | |
40 | /* Verify that calling realloc doesn't trigger a warning even though | |
41 | alloc_A is not directly associated with it. */ | |
42 | p = __builtin_realloc (p, 2); | |
43 | sink (p); | |
44 | } | |
45 | ||
46 | { | |
47 | void *p = alloc_A (1); // { dg-message "returned from 'alloc_A'" } | |
48 | dealloc (p); // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" } | |
49 | } | |
50 | ||
51 | { | |
52 | /* Because alloc_A() and realloc_B() share free() as a deallocator | |
53 | they must also be valid as each other's deallocators. */ | |
54 | void *p = alloc_A (1); | |
55 | p = realloc_B ((B*)p, 2); | |
56 | __builtin_free (p); | |
57 | } | |
58 | ||
59 | { | |
60 | void *p = alloc_A (1); | |
61 | p = realloc_A (p, 2); | |
62 | p = __builtin_realloc (p, 3); | |
63 | __builtin_free (p); | |
64 | } | |
65 | } | |
66 | ||
67 | ||
68 | void test_realloc_A (void *ptr) | |
69 | { | |
70 | { | |
71 | void *p = realloc_A (0, 1); | |
72 | p = realloc_A (p, 2); | |
73 | __builtin_free (p); | |
74 | } | |
75 | ||
76 | { | |
77 | void *p = realloc_A (ptr, 2); | |
78 | p = realloc_A (p, 2); | |
79 | __builtin_free (p); | |
80 | } | |
81 | ||
82 | { | |
83 | void *p = realloc_A (0, 3); | |
84 | p = __builtin_realloc (p, 2); | |
85 | sink (p); | |
86 | } | |
87 | ||
88 | { | |
89 | void *p = realloc_A (0, 4); // { dg-message "returned from 'realloc_A'" } | |
90 | dealloc (p); // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" } | |
91 | } | |
92 | ||
93 | { | |
94 | /* Because realloc_A() and realloc_B() share free() as a deallocator | |
95 | they must also be valid as each other's deallocators. */ | |
96 | void *p = realloc_A (0, 5); | |
97 | p = realloc_B ((B*)p, 2); | |
98 | __builtin_free (p); | |
99 | } | |
100 | ||
101 | { | |
102 | void *p = realloc_A (0, 6); | |
103 | p = realloc_A ((A*)p, 2); | |
104 | p = __builtin_realloc (p, 3); | |
105 | __builtin_free (p); | |
106 | } | |
107 | } | |
108 | ||
109 | ||
110 | void test_realloc (void *ptr) | |
111 | { | |
112 | extern void free (void*); | |
113 | extern void* realloc (void*, size_t); | |
114 | ||
115 | { | |
116 | void *p = realloc (ptr, 1); | |
117 | p = realloc_A (p, 2); | |
118 | __builtin_free (p); | |
119 | } | |
120 | ||
121 | { | |
122 | void *p = realloc (ptr, 2); | |
123 | p = realloc_A (p, 2); | |
124 | free (p); | |
125 | } | |
126 | ||
127 | { | |
128 | void *p = realloc (ptr, 3); | |
129 | free (p); | |
130 | } | |
131 | ||
132 | { | |
133 | void *p = realloc (ptr, 4); | |
134 | __builtin_free (p); | |
135 | } | |
136 | ||
137 | { | |
138 | void *p = realloc (ptr, 5); // { dg-message "returned from 'realloc'" } | |
139 | dealloc (p); // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" } | |
140 | } | |
141 | } |