Debian Codegraph @

gcc/testsuite/gcc.dg/flex-array-counted-by-4.c

raw
1 /* Test the attribute counted_by and its usage in
2 __builtin_dynamic_object_size: what's the correct behavior when the
3 allocation size mismatched with the value of counted_by attribute?
4 We should always use the latest value that is hold by the counted_by
5 field. */
6 /* { dg-do run } */
7 /* { dg-options "-O -fstrict-flex-arrays=3" } */
8
9 #include "builtin-object-size-common.h"
10
11 struct annotated {
12 size_t foo;
13 char others;
14 char array[] __attribute__((counted_by (foo)));
15 };
16
17 #define noinline __attribute__((__noinline__))
18 #define SIZE_BUMP 10
19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
20
21 /* In general, Due to type casting, the type for the pointee of a pointer
22 does not say anything about the object it points to,
23 So, __builtin_object_size can not directly use the type of the pointee
24 to decide the size of the object the pointer points to.
25
26 There are only two reliable ways:
27 A. observed allocations (call to the allocation functions in the routine)
28 B. observed accesses (read or write access to the location of the
29 pointer points to)
30
31 That provide information about the type/existence of an object at
32 the corresponding address.
33
34 For A, we use the "alloc_size" attribute for the corresponding allocation
35 functions to determine the object size;
36 (We treat counted_by attribute the same as the "alloc_size" attribute)
37
38 For B, we use the SIZE info of the TYPE attached to the corresponding access.
39
40 The only other way in C which ensures that a pointer actually points
41 to an object of the correct type is 'static':
42
43 void foo(struct P *p[static 1]);
44
45 See https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624814.html
46 for more details. */
47
48 /* In the following function, malloc allocated more space than the value
49 of counted_by attribute. Then what's the correct behavior we expect
50 the __builtin_dynamic_object_size should have for each of the cases? */
51
52 static struct annotated * noinline alloc_buf_more (size_t index)
53 {
54 struct annotated *p;
55 size_t allocated_size
56 = MAX (sizeof (struct annotated),
57 (__builtin_offsetof (struct annotated, array[0])
58 + (index + SIZE_BUMP) * sizeof (char)));
59 p = (struct annotated *) malloc (allocated_size);
60
61 p->foo = index;
62
63 /* When checking the observed access p->array, we have info on both
64 observered allocation and observed access,
65 A.1 from observed allocation:
66 allocated_size - offsetof (struct annotated, array[0])
67
68 A.2 from the counted-by attribute:
69 p->foo * sizeof (char)
70
71 We always use the latest value that is hold by the counted-by field.
72 */
73
74 EXPECT(__builtin_dynamic_object_size(p->array, 0),
75 (p->foo) * sizeof(char));
76
77 EXPECT(__builtin_dynamic_object_size(p->array, 1),
78 (p->foo) * sizeof(char));
79
80 EXPECT(__builtin_dynamic_object_size(p->array, 2),
81 (p->foo) * sizeof(char));
82
83 EXPECT(__builtin_dynamic_object_size(p->array, 3),
84 (p->foo) * sizeof(char));
85
86 /* When checking the pointer p, we only have info on the observed allocation.
87 So, the object size info can only been obtained from the call to malloc.
88 For both MAXIMUM and MINIMUM: A = (index + SIZE_BUMP) * sizeof (char) */
89 EXPECT(__builtin_dynamic_object_size(p, 0), allocated_size);
90 EXPECT(__builtin_dynamic_object_size(p, 1), allocated_size);
91 EXPECT(__builtin_dynamic_object_size(p, 2), allocated_size);
92 EXPECT(__builtin_dynamic_object_size(p, 3), allocated_size);
93 return p;
94 }
95
96 /* In the following function, malloc allocated less space than the value
97 of counted_by attribute. Then what's the correct behavior we expect
98 the __builtin_dynamic_object_size should have for each of the cases?
99 NOTE: this is an user error, GCC should issue warnings for such case.
100 This is a seperate issue we should address later. */
101
102 static struct annotated * noinline alloc_buf_less (size_t index)
103 {
104 struct annotated *p;
105 size_t allocated_size
106 = MAX (sizeof (struct annotated),
107 (__builtin_offsetof (struct annotated, array[0])
108 + (index) * sizeof (char)));
109 p = (struct annotated *) malloc (allocated_size);
110
111 p->foo = index + SIZE_BUMP;
112
113 /* When checking the observed access p->array, we have info on both
114 observered allocation and observed access,
115 A.1 from observed allocation:
116 allocated_size - offsetof (struct annotated, array[0])
117 A.2 from the counted-by attribute:
118 p->foo * sizeof (char)
119
120 We always use the latest value that is hold by the counted-by field.
121 */
122
123 EXPECT(__builtin_dynamic_object_size(p->array, 0),
124 (p->foo) * sizeof(char));
125
126 EXPECT(__builtin_dynamic_object_size(p->array, 1),
127 (p->foo) * sizeof(char));
128
129 EXPECT(__builtin_dynamic_object_size(p->array, 2),
130 (p->foo) * sizeof(char));
131
132 EXPECT(__builtin_dynamic_object_size(p->array, 3),
133 (p->foo) * sizeof(char));
134
135 /* When checking the pointer p, we only have info on the observed
136 allocation. So, the object size info can only been obtained from
137 the call to malloc. */
138 EXPECT(__builtin_dynamic_object_size(p, 0), allocated_size);
139 EXPECT(__builtin_dynamic_object_size(p, 1), allocated_size);
140 EXPECT(__builtin_dynamic_object_size(p, 2), allocated_size);
141 EXPECT(__builtin_dynamic_object_size(p, 3), allocated_size);
142 return p;
143 }
144
145 int main ()
146 {
147 struct annotated *p, *q;
148 p = alloc_buf_more (10);
149 q = alloc_buf_less (10);
150
151 /* When checking the access p->array, we only have info on the counted-by
152 value. */
153 EXPECT(__builtin_dynamic_object_size(p->array, 0), p->foo * sizeof(char));
154 EXPECT(__builtin_dynamic_object_size(p->array, 1), p->foo * sizeof(char));
155 EXPECT(__builtin_dynamic_object_size(p->array, 2), p->foo * sizeof(char));
156 EXPECT(__builtin_dynamic_object_size(p->array, 3), p->foo * sizeof(char));
157 /* When checking the pointer p, we have no observed allocation nor observed
158 access, therefore, we cannot determine the size info here. */
159 EXPECT(__builtin_dynamic_object_size(p, 0), -1);
160 EXPECT(__builtin_dynamic_object_size(p, 1), -1);
161 EXPECT(__builtin_dynamic_object_size(p, 2), 0);
162 EXPECT(__builtin_dynamic_object_size(p, 3), 0);
163
164 /* When checking the access p->array, we only have info on the counted-by
165 value. */
166 EXPECT(__builtin_dynamic_object_size(q->array, 0), q->foo * sizeof(char));
167 EXPECT(__builtin_dynamic_object_size(q->array, 1), q->foo * sizeof(char));
168 EXPECT(__builtin_dynamic_object_size(q->array, 2), q->foo * sizeof(char));
169 EXPECT(__builtin_dynamic_object_size(q->array, 3), q->foo * sizeof(char));
170 /* When checking the pointer p, we have no observed allocation nor observed
171 access, therefore, we cannot determine the size info here. */
172 EXPECT(__builtin_dynamic_object_size(q, 0), -1);
173 EXPECT(__builtin_dynamic_object_size(q, 1), -1);
174 EXPECT(__builtin_dynamic_object_size(q, 2), 0);
175 EXPECT(__builtin_dynamic_object_size(q, 3), 0);
176
177 DONE ();
178 }