Skip to content

Simple normalizations for +1/-1 bounds scenarios #1128

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 22 commits into from
Jul 20, 2021
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f60113f
Add NormalizeUtils.h and NormalizeUtils.cpp
Jul 7, 2021
4e0ab90
Add NormalizeUtil::AddExprs helper method
Jul 7, 2021
de4c40e
Add NormalizeUtil::TransformAdditiveOp method
Jul 7, 2021
db8a01e
Fix typos
Jul 7, 2021
f0c0b14
Add ExprCreatorUtil::CreateUnaryOperator method
Jul 7, 2021
f8faae2
Add NormalizeUtil::GetAdditionOperands helper method
Jul 7, 2021
24d6c66
Rename variable in NormalizeUtil::TransformSingleAdditiveOp
Jul 7, 2021
2b4b9c9
Add ExprUtil::EnsureEqualBitWidths method
Jul 8, 2021
f6443fd
Add NormalizeUtil::GetRHSConstant helper method
Jul 8, 2021
e1a9630
Add NormalizeUtil::TransformAssocLeft method
Jul 8, 2021
7ee773e
Add NormalizeUtil::ConstantFold method
Jul 8, 2021
d6b25a6
Remove ConstantFoldUpperOffsets, GetRHSConstant, and EnsureEqualBitWi…
Jul 8, 2021
b99804d
Fix typos
Jul 12, 2021
fc09e82
Avoid creating an unnecessary binary operator in TransformAdditiveOp
Jul 12, 2021
5ca588b
Return argument expression from TransformAssocLeft if the argument is…
Jul 12, 2021
0ddfa57
Add NormalizeUpperBound method to CheckBoundsDeclarations
Jul 12, 2021
0078abd
Add CompareNormalizeBounds method to CheckBoundsDeclarations
Jul 12, 2021
b50dc1f
Remove expected warning from bounds widening test in bounds-context.c
Jul 12, 2021
ced9e4f
Add tests for comparing normalized bounds to bounds-decl-checking.c
Jul 12, 2021
cdd7b58
Merge branch 'master' of https://github.com/microsoft/checkedc-clang …
Jul 12, 2021
a375cae
Move declaration of PointerAndConst
Jul 13, 2021
ff5130b
Add comment explaining why we don't check for B - P
Jul 16, 2021
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 CompareNormalizeBounds method to CheckBoundsDeclarations
  • Loading branch information
kakje committed Jul 12, 2021
commit 0078abdb6a191db5b947c9d0e56f8626d938facf
60 changes: 59 additions & 1 deletion clang/lib/Sema/SemaBounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2010,10 +2010,68 @@ namespace {
}
}
return R;
}
} else if (CompareNormalizedBounds(DeclaredBounds, SrcBounds, EquivExprs))
return ProofResult::True;
return ProofResult::Maybe;
}

// CompareNormalizedBounds returns true if SrcBounds implies DeclaredBounds
// after applying certain transformations to the upper bound expressions
// of both bounds.
bool CompareNormalizedBounds(const BoundsExpr *DeclaredBounds,
const BoundsExpr *SrcBounds,
EquivExprSets *EquivExprs) {
// DeclaredBounds and SrcBounds must both be range bounds in order
// to normalize their upper bound expression.
const RangeBoundsExpr *DeclaredRangeBounds =
dyn_cast<RangeBoundsExpr>(DeclaredBounds);
if (!DeclaredRangeBounds)
return false;
const RangeBoundsExpr *SrcRangeBounds =
dyn_cast<RangeBoundsExpr>(SrcBounds);
if (!SrcRangeBounds)
return false;

// The lower bound expressions must be equivalent.
if (!ExprUtil::EqualValue(S.Context, DeclaredRangeBounds->getLowerExpr(),
SrcRangeBounds->getLowerExpr(), EquivExprs))
return false;

// Attempt to get a variable part and a constant part from the
// declared upper bound by applying certain normalizations.
Expr *DeclaredVariable = nullptr;
llvm::APSInt DeclaredConstant;
bool NormalizedDeclared =
NormalizeUpperBound(DeclaredRangeBounds->getUpperExpr(),
DeclaredVariable, DeclaredConstant);

// Attempt to get a variable part and a constant part from the
// source upper bound by applying certain normalizations.
Expr *SrcVariable = nullptr;
llvm::APSInt SrcConstant;
bool NormalizedSrc =
NormalizeUpperBound(SrcRangeBounds->getUpperExpr(),
SrcVariable, SrcConstant);

// We must be able to normalize at least one of the upper bounds in
// order to compare them.
if (!NormalizedDeclared && !NormalizedSrc)
return false;

// Both upper bounds must have a Variable part.
if (!DeclaredVariable || !SrcVariable)
return false;

// The variable parts of the upper bounds must be equivalent.
if (!ExprUtil::EqualValue(S.Context, DeclaredVariable, SrcVariable, EquivExprs))
return false;

// SrcBounds implies DeclaredBounds if and only if the declared upper
// constant part is less than or equal to the source upper constant part.
ExprUtil::EnsureEqualBitWidths(DeclaredConstant, SrcConstant);
return DeclaredConstant <= SrcConstant;
}

// NormalizeUpperBound attempts to extract a Variable part and a Constant
// part from the upper bound expression E.
//
Expand Down