Skip to content

Bounds checking bounds-safe interfaces in unchecked scopes #1169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 30, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for validating the bounds of an unchecked pointer with a bo…
…unds-safe interface
  • Loading branch information
kakje committed Aug 23, 2021
commit b473bb03963e1143b1b9a037eec5349a3efc7ca6
52 changes: 52 additions & 0 deletions clang/test/CheckedC/static-checking/bounds-decl-checking.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,55 @@ void f96(_Nt_array_ptr<char> p : bounds(p, (p + (len + 4)) + 4), int len) {
// expected-note {{(expanded) declared bounds are 'bounds(v, (v + (1 + len)) + 2)'}} \
// expected-note {{(expanded) inferred bounds are 'bounds(p, (p + (len + 4)) + 4)'}}
}

//
// Test validating bounds for unchecked pointer with bounds-safe interfaces
// in unchecked and checked scopes.
//

_Unchecked extern int *get_unchecked(_Array_ptr<int> a);
extern _Array_ptr<int> get_checked(unsigned int i) : count(i);

_Unchecked void f97(int *p : count(i), // expected-note 7 {{(expanded) declared bounds are 'bounds(p, p + i)'}}
int *q : bounds(unknown),
_Array_ptr<int> r,
unsigned int i) {
// For statements where a checked pointer is not assigned to p, do not
// validate the bounds of p.
i = 0;
p++;
p = q;
q = r, p = q;
p = get_unchecked(r);
p += i;

// The type of the RHS expression p + r is int *, so a checked pointer is not
// assigned to p here.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the code that the comment on lines 841 and 842 is referring to is missing (not sure).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on lines 841 and 842 is referring to the code on line 843, since the type of the RHS expression p - (_Array_ptr<int>)q is int *.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment has a typo, so I'll update the comment - thanks for catching this!

p -= (_Array_ptr<int>)q; // expected-warning {{incompatible integer to pointer conversion assigning to 'int *' from 'long long'}}

// For statements where a checked pointer is assigned to p, validate the
// bounds of p.
p = (_Array_ptr<int>)(p - q); // expected-error {{inferred bounds for 'p' are unknown after assignment}} \
// expected-note {{assigned expression '(_Array_ptr<int>)(p - q)' with unknown bounds to 'p'}}

p = r; // expected-error {{inferred bounds for 'p' are unknown after assignment}} \
// expected-note {{assigned expression 'r' with unknown bounds to 'p'}}

p = (_Array_ptr<int>)q; // expected-error {{inferred bounds for 'p' are unknown after assignment}} \
// expected-note {{assigned expression '(_Array_ptr<int>)q' with unknown bounds to 'p'}}

p = get_checked(i), i++; // expected-warning {{cannot prove declared bounds for 'p' are valid after increment}} \
// expected-note {{(expanded) inferred bounds are 'bounds(value of get_checked(i), value of get_checked(i) + i - 1U)'}}

p = (_Array_ptr<int>)p, --p; // expected-warning {{cannot prove declared bounds for 'p' are valid after decrement}} \
// expected-note {{'bounds((int *)p + 1, (int *)p + 1 + i)'}}

// In a checked scope, always validate the bounds of p.
_Checked {
i = 1; // expected-error {{inferred bounds for 'p' are unknown after assignment}} \
// expected-note {{lost the value of the expression 'i' which is used in the (expanded) inferred bounds 'bounds(p, p + i)' of 'p'}}

++p; // expected-warning {{cannot prove declared bounds for 'p' are valid after increment}} \
// expected-note {{(expanded) inferred bounds are 'bounds(p - 1, p - 1 + i)'}}
}
}