Commit 7220268
authored
[Clang] Extend __builtin_counted_by_ref to support pointers with 'counted_by' (#170750)
The __builtin_counted_by_ref builtin was previously limited to flexible
array members (FAMs). This change extends it to also support pointer
members that have the 'counted_by' attribute.
The 'counted_by' attribute can be applied to both FAMs and pointer
members:
struct fam_struct {
int count;
int array[] __attribute__((counted_by(count)));
};
struct ptr_struct {
int count;
int *buf __attribute__((counted_by(count)));
};
With this change, __builtin_counted_by_ref works with both:
*__builtin_counted_by_ref(p->array) = size; // FAM - already worked
*__builtin_counted_by_ref(p->buf) = size; // pointer - now works
This enables the same allocation pattern for pointer members that was
previously only available for FAMs:
#define alloc_buf(P, MEMBER, COUNT) ({ \
typeof(P) __p = malloc(sizeof(*__p)); \
__p->MEMBER = malloc(sizeof(*__p->MEMBER) * COUNT); \
*_Generic(__builtin_counted_by_ref(__p->MEMBER), \
void *: &(size_t){0}, \
default: __builtin_counted_by_ref(__p->MEMBER)) = COUNT; \
__p; \
})
The builtin returns:
- A pointer to the count field if the member has 'counted_by'
- void* (null) if the member is an array or pointer without 'counted_by'
- An error for other member types (e.g., int, struct)
This was requested by upstream Linux kernel devs:
https://lore.kernel.org/linux-hardening/202512041215.44484FCACD@keescook/1 parent 5911754 commit 7220268
File tree
6 files changed
+167
-43
lines changed- clang
- docs
- include/clang/Basic
- lib
- CodeGen
- Sema
- test
- CodeGen
- Sema
6 files changed
+167
-43
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4313 | 4313 | | |
4314 | 4314 | | |
4315 | 4315 | | |
4316 | | - | |
4317 | | - | |
4318 | | - | |
| 4316 | + | |
| 4317 | + | |
| 4318 | + | |
4319 | 4319 | | |
4320 | 4320 | | |
4321 | 4321 | | |
| |||
4346 | 4346 | | |
4347 | 4347 | | |
4348 | 4348 | | |
4349 | | - | |
4350 | | - | |
4351 | | - | |
| 4349 | + | |
| 4350 | + | |
| 4351 | + | |
4352 | 4352 | | |
4353 | 4353 | | |
4354 | 4354 | | |
| |||
4366 | 4366 | | |
4367 | 4367 | | |
4368 | 4368 | | |
4369 | | - | |
4370 | | - | |
4371 | | - | |
4372 | | - | |
| 4369 | + | |
| 4370 | + | |
| 4371 | + | |
4373 | 4372 | | |
4374 | 4373 | | |
4375 | 4374 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7021 | 7021 | | |
7022 | 7022 | | |
7023 | 7023 | | |
7024 | | - | |
7025 | | - | |
| 7024 | + | |
| 7025 | + | |
| 7026 | + | |
7026 | 7027 | | |
7027 | 7028 | | |
7028 | 7029 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3693 | 3693 | | |
3694 | 3694 | | |
3695 | 3695 | | |
3696 | | - | |
3697 | | - | |
3698 | | - | |
| 3696 | + | |
| 3697 | + | |
| 3698 | + | |
3699 | 3699 | | |
3700 | 3700 | | |
3701 | 3701 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6362 | 6362 | | |
6363 | 6363 | | |
6364 | 6364 | | |
6365 | | - | |
6366 | | - | |
6367 | | - | |
| 6365 | + | |
| 6366 | + | |
| 6367 | + | |
6368 | 6368 | | |
6369 | | - | |
| 6369 | + | |
6370 | 6370 | | |
6371 | | - | |
| 6371 | + | |
6372 | 6372 | | |
6373 | 6373 | | |
6374 | 6374 | | |
| |||
6377 | 6377 | | |
6378 | 6378 | | |
6379 | 6379 | | |
6380 | | - | |
6381 | | - | |
6382 | | - | |
6383 | | - | |
6384 | | - | |
| 6380 | + | |
| 6381 | + | |
6385 | 6382 | | |
6386 | | - | |
6387 | | - | |
6388 | | - | |
6389 | | - | |
6390 | | - | |
| 6383 | + | |
| 6384 | + | |
| 6385 | + | |
| 6386 | + | |
6391 | 6387 | | |
6392 | 6388 | | |
6393 | 6389 | | |
6394 | 6390 | | |
| 6391 | + | |
| 6392 | + | |
| 6393 | + | |
| 6394 | + | |
| 6395 | + | |
| 6396 | + | |
| 6397 | + | |
6395 | 6398 | | |
6396 | 6399 | | |
6397 | | - | |
| 6400 | + | |
6398 | 6401 | | |
6399 | 6402 | | |
6400 | 6403 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
85 | 87 | | |
86 | 88 | | |
87 | 89 | | |
| |||
122 | 124 | | |
123 | 125 | | |
124 | 126 | | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
0 commit comments