Commit 99b91ff
committed
[-Wunsafe-buffer-usage] Check count-attributed assignment groups
This change adds support for checking count-attributed assignment
groups. This commit adds the following checks:
1. Standalone assignments to count-attributed objects (pointers/dependent
counts) that are not directly inside of a compound statement. Our
model rejects those and requires the user to simplify their code if
necessary. For example:
```
void foo(int *__counted_by(count) p, int count) {
q = p = ...;
^ this is rejected
n = count = ...;
^ this is rejected
// the following is fine:
p = ...;
count = ...;
}
```
2. Assignments to count-attributed objects that are implicitly
immutable. For example, assigning to a dependent count that is used
in an inout pointer is not allowed, since the update won't be visible
on the call-site:
```
void foo(int *__counted_by(count) *out_p, int count) {
*out_p = ...;
count = ...; // immutable
}
```
3. Missing and duplicated assignments:
```
void foo(int *__counted_by(a + b) p, int a, int b) {
p = ...;
p = ...; // duplicated
a = ...;
// b missing
}
```
4. Count-attributed objects that are assigned and used in the same
group. Allowing such assignments can cause the bounds-check to use
the old dependent count, while updating the count to a new value. For
example, the bounds-check in `sp.first()` uses the value of `b`
before the later update, which can lead to OOB if `b` was less than
42:
```
void foo(int *__counted_by(a + b) p, int a, int b, std::span<int> sp) {
p = sp.first(b + 42).data();
b = 42; // b is assigned and used
a = b;
}
```
5. Safe assignment patterns. This uses the infrastructure that is
already available for count-attributed arguments, and checks for each
assigned pointer in the group that the RHS has enough elements.
This analysis is hidden behind `-fexperimental-bounds-safety-attributes`
flag, so that we don't waste cycles traversing the AST when the
attributes are not enabled.
rdar://128160398
rdar://1281615801 parent 16f18a3 commit 99b91ff
File tree
9 files changed
+1268
-30
lines changed- clang
- include/clang
- AST
- Analysis/Analyses
- Basic
- lib
- AST
- Analysis
- Sema
- test/SemaCXX
9 files changed
+1268
-30
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
732 | 732 | | |
733 | 733 | | |
734 | 734 | | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
735 | 739 | | |
736 | 740 | | |
737 | 741 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2651 | 2651 | | |
2652 | 2652 | | |
2653 | 2653 | | |
| 2654 | + | |
2654 | 2655 | | |
2655 | 2656 | | |
2656 | 2657 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
142 | 143 | | |
143 | 144 | | |
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 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
145 | 193 | | |
146 | 194 | | |
147 | 195 | | |
| |||
196 | 244 | | |
197 | 245 | | |
198 | 246 | | |
199 | | - | |
| 247 | + | |
| 248 | + | |
200 | 249 | | |
201 | 250 | | |
202 | 251 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14236 | 14236 | | |
14237 | 14237 | | |
14238 | 14238 | | |
| 14239 | + | |
| 14240 | + | |
| 14241 | + | |
| 14242 | + | |
| 14243 | + | |
| 14244 | + | |
| 14245 | + | |
| 14246 | + | |
| 14247 | + | |
| 14248 | + | |
| 14249 | + | |
| 14250 | + | |
| 14251 | + | |
| 14252 | + | |
| 14253 | + | |
| 14254 | + | |
| 14255 | + | |
| 14256 | + | |
| 14257 | + | |
| 14258 | + | |
| 14259 | + | |
| 14260 | + | |
| 14261 | + | |
14239 | 14262 | | |
14240 | 14263 | | |
14241 | 14264 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5502 | 5502 | | |
5503 | 5503 | | |
5504 | 5504 | | |
| 5505 | + | |
| 5506 | + | |
| 5507 | + | |
| 5508 | + | |
| 5509 | + | |
| 5510 | + | |
| 5511 | + | |
| 5512 | + | |
| 5513 | + | |
| 5514 | + | |
| 5515 | + | |
| 5516 | + | |
| 5517 | + | |
| 5518 | + | |
| 5519 | + | |
| 5520 | + | |
| 5521 | + | |
| 5522 | + | |
| 5523 | + | |
| 5524 | + | |
| 5525 | + | |
5505 | 5526 | | |
5506 | 5527 | | |
5507 | 5528 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
829 | 829 | | |
830 | 830 | | |
831 | 831 | | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
832 | 840 | | |
833 | 841 | | |
834 | 842 | | |
| |||
0 commit comments