Debian Codegraph @

gcc/testsuite/gcc.dg/builtin-dynamic-object-size-pr120780.c

raw
1 /* { dg-do run } */
2 /* { dg-options "-O2" } */
3
4 #include "builtin-object-size-common.h"
5 typedef __SIZE_TYPE__ size_t;
6 #define NUM_MCAST_RATE 6
7
8 #define MIN(a,b) ((a) < (b) ? (a) : (b))
9 #define MAX(a,b) ((a) > (b) ? (a) : (b))
10
11 struct inner
12 {
13 int dummy[4];
14 };
15
16 struct container
17 {
18 int mcast_rate[NUM_MCAST_RATE];
19 struct inner mesh;
20 };
21
22 static void
23 test1_child (struct inner *ifmsh, size_t expected)
24 {
25 struct container *sdata =
26 (struct container *) ((void *) ifmsh
27 - __builtin_offsetof (struct container, mesh));
28
29 if (__builtin_dynamic_object_size (sdata->mcast_rate, 1)
30 != sizeof (sdata->mcast_rate))
31 FAIL ();
32
33 if (__builtin_dynamic_object_size (&sdata->mesh, 1) != expected)
34 FAIL ();
35 }
36
37 void
38 __attribute__((noinline))
39 test1 (size_t sz)
40 {
41 struct container *sdata = __builtin_malloc (sz);
42 struct inner *ifmsh = &sdata->mesh;
43
44 test1_child (ifmsh,
45 (sz > sizeof (sdata->mcast_rate)
46 ? sz - sizeof (sdata->mcast_rate) : 0));
47
48 __builtin_free (sdata);
49 }
50
51 struct container2
52 {
53 int mcast_rate[NUM_MCAST_RATE];
54 union
55 {
56 int dummy;
57 double dbl;
58 struct inner mesh;
59 } u;
60 };
61
62 static void
63 test2_child (struct inner *ifmsh, size_t sz)
64 {
65 struct container2 *sdata =
66 (struct container2 *) ((void *) ifmsh
67 - __builtin_offsetof (struct container2, u.mesh));
68
69 if (__builtin_dynamic_object_size (sdata->mcast_rate, 1)
70 != sizeof (sdata->mcast_rate))
71 FAIL ();
72
73 size_t diff = sizeof (*sdata) - sz;
74 size_t expected = MIN(sizeof (double), MAX (sizeof (sdata->u), diff) - diff);
75
76 if (__builtin_dynamic_object_size (&sdata->u.dbl, 1) != expected)
77 FAIL ();
78
79 expected = MAX (sizeof (sdata->u.mesh), diff) - diff;
80 if (__builtin_dynamic_object_size (&sdata->u.mesh, 1) != expected)
81 FAIL ();
82 }
83
84 void
85 __attribute__((noinline))
86 test2 (size_t sz)
87 {
88 struct container2 *sdata = __builtin_malloc (sz);
89 struct inner *ifmsh = &sdata->u.mesh;
90
91 test2_child (ifmsh, sz);;
92
93 __builtin_free (sdata);
94 }
95
96 struct container3
97 {
98 int mcast_rate[NUM_MCAST_RATE];
99 char mesh[8];
100 };
101
102 static void
103 test3_child (char ifmsh[], size_t expected)
104 {
105 struct container3 *sdata =
106 (struct container3 *) ((void *) ifmsh
107 - __builtin_offsetof (struct container3, mesh));
108
109 if (__builtin_dynamic_object_size (sdata->mcast_rate, 1)
110 != sizeof (sdata->mcast_rate))
111 FAIL ();
112
113 if (__builtin_dynamic_object_size (sdata->mesh, 1) != expected)
114 FAIL ();
115 }
116
117 void
118 __attribute__((noinline))
119 test3 (size_t sz)
120 {
121 struct container3 *sdata = __builtin_malloc (sz);
122 char *ifmsh = sdata->mesh;
123 size_t diff = sizeof (*sdata) - sz;
124
125 test3_child (ifmsh, MAX(sizeof (sdata->mesh), diff) - diff);
126
127 __builtin_free (sdata);
128 }
129
130
131 struct container4
132 {
133 int mcast_rate[NUM_MCAST_RATE];
134 struct
135 {
136 int dummy;
137 struct inner mesh;
138 } s;
139 };
140
141 static void
142 test4_child (struct inner *ifmsh, size_t expected)
143 {
144 struct container4 *sdata =
145 (struct container4 *) ((void *) ifmsh
146 - __builtin_offsetof (struct container4, s.mesh));
147
148
149 if (__builtin_dynamic_object_size (sdata->mcast_rate, 1)
150 != sizeof (sdata->mcast_rate))
151 FAIL ();
152
153 if (__builtin_dynamic_object_size (&sdata->s.mesh, 1) != expected)
154 FAIL ();
155 }
156
157 void
158 __attribute__((noinline))
159 test4 (size_t sz)
160 {
161 struct container4 *sdata = __builtin_malloc (sz);
162 struct inner *ifmsh = &sdata->s.mesh;
163 size_t diff = sizeof (*sdata) - sz;
164
165 test4_child (ifmsh, MAX(sizeof (sdata->s.mesh), diff) - diff);
166
167 __builtin_free (sdata);
168 }
169
170 struct container5
171 {
172 int mcast_rate[NUM_MCAST_RATE];
173 struct
174 {
175 int dummy;
176 struct inner *mesh;
177 } s;
178 };
179
180 static void
181 test5_child (struct inner **ifmsh, size_t expected)
182 {
183 struct container5 *sdata =
184 (struct container5 *) ((void *) ifmsh
185 - __builtin_offsetof (struct container5, s.mesh));
186
187
188 if (__builtin_dynamic_object_size (sdata->mcast_rate, 1)
189 != sizeof (sdata->mcast_rate))
190 FAIL ();
191
192 if (__builtin_dynamic_object_size (&sdata->s.mesh, 1) != expected)
193 FAIL ();
194 }
195
196 void
197 __attribute__((noinline))
198 test5 (size_t sz)
199 {
200 struct container5 *sdata = __builtin_malloc (sz);
201 struct inner **ifmsh = &sdata->s.mesh;
202 size_t diff = sizeof (*sdata) - sz;
203
204 test5_child (ifmsh, MAX(sizeof (sdata->s.mesh), diff) - diff);
205
206 __builtin_free (sdata);
207 }
208
209 int
210 main (void)
211 {
212 test1 (sizeof (struct container));
213 test1 (sizeof (struct container) - sizeof (int));
214 test1 (sizeof (int) * NUM_MCAST_RATE - 1);
215
216 test2 (sizeof (struct container2));
217 test2 (sizeof (struct container2) - sizeof (int));
218 test2 (sizeof (int) * NUM_MCAST_RATE - 1);
219
220 test3 (sizeof (struct container3));
221 test3 (sizeof (struct container3) - sizeof (int));
222 test3 (sizeof (int) * NUM_MCAST_RATE - 1);
223
224 test4 (sizeof (struct container4));
225 test4 (sizeof (struct container4) - sizeof (int));
226 test4 (sizeof (int) * NUM_MCAST_RATE - 1);
227
228 test5 (sizeof (struct container5));
229 test5 (sizeof (struct container5) - sizeof (int));
230 test5 (sizeof (int) * NUM_MCAST_RATE - 1);
231
232 DONE ();
233 }