Skip to content

Bounds widening for _Nt_array_ptr's #728

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 89 commits into from
Dec 20, 2019
Merged

Bounds widening for _Nt_array_ptr's #728

merged 89 commits into from
Dec 20, 2019

Conversation

mgrang
Copy link

@mgrang mgrang commented Nov 13, 2019

_Nt_array_ptr's are terminated by a null terminator. If we determine that the dereference of a
_Nt_array_ptr is non-null then we can widen the bounds of the pointer by 1 because there will always be at least the null terminator in the string.

Formally we can state the problem as:
∀ p | p ∈ _Nt_array_ptr & bounds(p) = [l, u)
*p != 0 => bounds(p) = [l, u + 1)

Example:
_Nt_array_ptr<char> p : count(0) = "abc"; // bounds = [0, 0]
if (*p) { // bounds = [0, 1) }

Mandeep Singh Grang and others added 3 commits November 13, 2019 15:43
Strings in Checked C are declared as _Nt_array_ptr which means they are
terminated by a null terminator. If we determine that the dereference of an
_Nt_array_ptr is non-null then we can widen the bounds of the pointer by 1
because there will always be at least the null terminator in the string.

Formally we can state the problem as:
∀ p | p ∈ _Nt_array_ptr & bounds(p) = [l, u)
*p != 0 => bounds(p) = [l, u + 1)

Example:
_Nt_array_ptr<char> p : count(0) = "abc"; // bounds = [0, 0]
if (*p) { // bounds = [0, 1) }
@mgrang
Copy link
Author

mgrang commented Nov 14, 2019

Dataflow equations:

Let p ∈ _Nt_array_ptr.
For each basic block B:

If there is an edge B1->B2 and the edge condition is of the form "if (*( p + i))", then Gen[B1] = {B2, p:i+1}

In a block B, if p is assigned to, then Kill[B] = {p}

In[B] = ∩ Out[B*->B], where B* are all preds(B)

Out[B1->B2] = (In[B1] - Kill[B1]) ∪ Gen[B1->B2]

Mandeep Singh Grang added 2 commits November 15, 2019 12:22
@mgrang mgrang requested review from dtarditi and kkjeer November 18, 2019 21:18
@mgrang
Copy link
Author

mgrang commented Nov 18, 2019

Please review this PR as WIP. I am currently testing it out. Will add unit tests soon.

namespace clang {

void BoundsAnalysis::WidenBounds() {
llvm::dbgs() << "### Debug bounds analysis\n";
Copy link
Author

Choose a reason for hiding this comment

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

Will remove the debug messages soon.

@mgrang mgrang requested a review from saeednj November 20, 2019 23:14
@mgrang mgrang changed the title Implement bounds widening for _Nt_array_ptr's Bounds widening for _Nt_array_ptr's Nov 21, 2019
Copy link
Contributor

@saeednj saeednj left a comment

Choose a reason for hiding this comment

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

Is it useful to have a base class for "DataFlowAnalysis" and implement case specific uses?
There is a very similar implementation with a different usage in AvailableFactAnalysis.

@mgrang
Copy link
Author

mgrang commented Nov 22, 2019

Is it useful to have a base class for "DataFlowAnalysis" and implement case specific uses?
There is a very similar implementation with a different usage in AvailableFactAnalysis.

Yes, the goal is ultimately to have a common base. We can do that in a separate clean-up PR.

@mgrang
Copy link
Author

mgrang commented Dec 17, 2019

Thanks for your review, David. If we change Gen to be an expression instead of a constant offset how would we compute the intersection of sets? One option is to assign a number to each expression in Gen. Numbers would increase from entry to exit for a particular V. Then to compute intersection we choose the expr with the smaller number. This would represent the LUB of the sets.

Copy link
Member

@dtarditi dtarditi left a comment

Choose a reason for hiding this comment

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

@mgrang, you are correct that changing gen sets to be expressions would complicate the algorithm. The gen set entries should be constant offsets relative to the declared upper bound of an nt_array_ptr variable. Given

nt_array_ptr<char> p : bounds(e1, e2)

it is not correct to widen p given *p. p should be widened given a dereference of the form *(e2 + k), where k is a constant integer.

Note that we have methods for determining whether some expression e3 is equivalent to the expression e2. See AST\CanonBounds.

// CHECK: [B8]
// CHECK: 1: p = "xy"
// CHECK: 2: *(p + 1)
// CHECK: upper_bound(p) = 1
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong. You need to widen against the upper bound of p, which is declared to p + j.


void BoundsAnalysis::ComputeGenSets(BlockMapTy BlockMap) {
// If there is an edge B1->B2 and the edge condition is of the form
// "if (*(p + i))" then Gen[B1] = {B2, p:i} .
Copy link
Member

Choose a reason for hiding this comment

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

Could you clarify whether i is a constant or a variable? I know that i is a constant from context, but it could confuse people later on.

Gen sets should be constant offsets relative to the declared upper bound of a pointer. If p has declared upper bounds of e, and this is a dereference of *(e + i), then Gen[B1] = i. You'll need to handle expressions that are equivalent to e + i. Note that we already have methods for checking the equivalence of expressions.

// For conditions of the form "if (*p)".
if (const auto *D = dyn_cast<DeclRefExpr>(Exp)) {
if (const auto *V = dyn_cast<VarDecl>(D->getDecl())) {
if (V->getType()->isCheckedPointerNtArrayType()) {
Copy link
Member

Choose a reason for hiding this comment

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

I agree that gen can remain a constant offset. However, it should be a constant offset relative to the declared upper bound of a pointer variable.

@mgrang
Copy link
Author

mgrang commented Dec 17, 2019

@mgrang, you are correct that changing gen sets to be expressions would complicate the algorithm. The gen set entries should be constant offsets relative to the declared upper bound of an nt_array_ptr variable. Given

nt_array_ptr<char> p : bounds(e1, e2)

it is not correct to widen p given *p. p should be widened given a dereference of the form *(e2 + k), where k is a constant integer.

Note that we have methods for determining whether some expression e3 is equivalent to the expression e2. See AST\CanonBounds.

Thanks David. I think we still have a complication. Let's say we have a case like:
nt_array_ptr<char> p : bounds(p, p + 1)

and then we encounter *(p + 2) which is essentially *((p + 1) + 1). How do we separate k from *(e) here? What about accesses like *(i + j + e + k + l). This can get complicated quickly!

I think a simpler option is to keep the implementation we have now as is, meaning the bounds widened by this algorithm are not relative to the declared upper bound. Instead we "adjust" the widened bounds at the call site of this algorithm. Let's say the widened bounds for p returned by this algorithm is k and the declared bounds of p are (l, u).

If (l + k) <= u, we do not need to widen.
If (l + k) > u, we can widen the bounds by l + k - u.

For example:
nt_array_ptr<char> p : bounds(p, p + 2)

if (*p) // calc_bounds: 1, adjusted_bounds: p + 1 <= p + 2 => no widening done
   if (*(p + 1)) // calc_bounds: 2, adjusted_bounds: p + 2 <= p + 2 => no widening done
     if (*(p + 2)) // calc_bounds: 3, adjusted_bounds: p + 3 > p + 2 => bounds widened by (p + 3) - (p + 2) = 1.
       if (*(p + 3)) // calc_bounds: 4, adjusted_bounds: p + 4 > p + 2 => bounds widened by (p + 4) - (p + 2) = 2

@dtarditi
Copy link
Member

I agree it could get complicated quickly. The way we will handle this is by putting expressions in a standard form, such that the constant is on the outside. We already face similar problems when checking bounds declarations.

The problem with the approach that you propose is that we cannot handle simple cases like widening bounds of a variable v with bounds(v, v + len).

I think it would be appropriate to address additional cases and using standard forms in separate PRs, and finish this PR. How about you add a check that the upper bound is the variable v? This would bring it into agreement with what I am proposing, and let us finish the current PR.

@mgrang
Copy link
Author

mgrang commented Dec 17, 2019

< How about you add a check that the upper bound is the variable v?
David, could you please clarify this? Should we only allow bounds widening when nt_array_ptr<T> v : bounds(v, v)? Or when the dereference is at the upper bound?

@dtarditi
Copy link
Member

I am suggesting that in this PR, we only widen bounds for nt_array_ptr variables whose bounds are bounds(v, v) or count(0). I am suggesting that in a future PR, we generalize this to widen bounds for dereferences involving constant offsets from the declared upper bound of a variable.

@mgrang
Copy link
Author

mgrang commented Dec 18, 2019

Done. Latest PR widens bounds only when the declared bounds are count(0) or bounds(v, v). Also removed the more generic tests and replaced them with only tests specific to count(0) bounds.

@mgrang
Copy link
Author

mgrang commented Dec 18, 2019

Copy link
Member

@dtarditi dtarditi left a comment

Choose a reason for hiding this comment

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

I noticed that in the testing code, you removed code that testeds nested conditionals of the form:

if (*p) {
  if (*(p + 1)) {
    if (*(p + 2)) {
   ...

Could you add a test of this back?

Added. Because we converted warnings to errors I cannot add more intensive tests for this commit. An nt_array_ptr with a non-zero upper bound won't do any widening because we explicitly test for zero upper bound. On the other hand, for an nt_array_ptr with zero upper bound, I cannot dereference anything other than *p without running into out-of-bounds access errors.

Copy link
Member

@dtarditi dtarditi left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants