Skip to content
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

Revert "[BoundsWidening] Determine checked scope specifier per statement (#1139)" #1141

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 4 additions & 17 deletions clang/include/clang/Sema/BoundsWideningAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,14 @@ namespace clang {
Lexicographic Lex;
BoundsVarsTy &BoundsVarsLower;
BoundsVarsTy &BoundsVarsUpper;
CheckedScopeMapTy &CheckedScopeMap;

public:
BoundsWideningUtil(Sema &SemaRef, CFG *Cfg,
ASTContext &Ctx, Lexicographic Lex,
BoundsVarsTy &BoundsVarsLower,
BoundsVarsTy &BoundsVarsUpper,
CheckedScopeMapTy &CheckedScopeMap) :
BoundsVarsTy &BoundsVarsUpper) :
SemaRef(SemaRef), Cfg(Cfg), Ctx(Ctx), Lex(Lex),
BoundsVarsLower(BoundsVarsLower), BoundsVarsUpper(BoundsVarsUpper),
CheckedScopeMap(CheckedScopeMap) {}
BoundsVarsLower(BoundsVarsLower), BoundsVarsUpper(BoundsVarsUpper) {}

// Check if B2 is a subrange of B1.
// @param[in] B1 is the first range.
Expand Down Expand Up @@ -175,14 +172,6 @@ namespace clang {
// expression.
const VarDecl *GetNullTermPtrInExpr(Expr *E) const;

// Update the checked scope specifier for the current statement if it has
// changed from that of the previous statement.
// @param[in] CurrStmt is the current statement.
// @param[out] CSS is updated with the checked scope specifier for the
// current statement if it has changed from that of the previous statement.
void UpdateCheckedScopeSpecifier(const Stmt *CurrStmt,
CheckedScopeSpecifier &CSS) const;

// Invoke IgnoreValuePreservingOperations to strip off casts.
// @param[in] E is the expression whose casts must be stripped.
// @return E with casts stripped off.
Expand Down Expand Up @@ -343,13 +332,11 @@ namespace clang {

BoundsWideningAnalysis(Sema &SemaRef, CFG *Cfg,
BoundsVarsTy &BoundsVarsLower,
BoundsVarsTy &BoundsVarsUpper,
CheckedScopeMapTy &CheckedScopeMap) :
BoundsVarsTy &BoundsVarsUpper) :
SemaRef(SemaRef), Cfg(Cfg), Ctx(SemaRef.Context),
Lex(Lexicographic(Ctx, nullptr)), OS(llvm::outs()),
BWUtil(BoundsWideningUtil(SemaRef, Cfg, Ctx, Lex,
BoundsVarsLower, BoundsVarsUpper,
CheckedScopeMap)) {}
BoundsVarsLower, BoundsVarsUpper)) {}

// Run the dataflow analysis to widen bounds for null-terminated arrays.
// @param[in] FD is the current function.
Expand Down
7 changes: 0 additions & 7 deletions clang/include/clang/Sema/CheckedCAnalysesPrepass.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ namespace clang {
// of F in whose declared bounds expressions F occurs.
using BoundsSiblingFieldsTy = llvm::DenseMap<const FieldDecl *, FieldSetTy>;

// CheckedScopeMapTy maps a statement to its checked scope specifier.
using CheckedScopeMapTy = llvm::DenseMap<const Stmt *, CheckedScopeSpecifier>;

struct PrepassInfo {
// VarUses maps each VarDecl V in a function to the DeclRefExpr (if any)
// that is the first use of V, if V fulfills the following conditions:
Expand Down Expand Up @@ -85,10 +82,6 @@ namespace clang {
// a deterministic iteration order we must remember to sort the keys as
// well as the values.
BoundsSiblingFieldsTy BoundsSiblingFields;

// A map of statements to their checked scope specifiers. An entry in this
// map is made only when the checked scope specifier changes.
CheckedScopeMapTy CheckedScopeMap;
};
} // end namespace clang
#endif
15 changes: 3 additions & 12 deletions clang/lib/Sema/BoundsWideningAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ BoundsMapTy BoundsWideningAnalysis::GetOutOfLastStmt(
// statement and return the StmtOut for the last statement of the block.

BoundsMapTy StmtOut = EB->In;
CheckedScopeSpecifier CSS = CheckedScopeSpecifier::CSS_None;

for (CFGBlock::const_iterator I = EB->Block->begin(),
E = EB->Block->end();
Expand All @@ -94,9 +93,6 @@ BoundsMapTy BoundsWideningAnalysis::GetOutOfLastStmt(
if (!CurrStmt)
continue;

// Update the checked scope specifier for the current statement.
BWUtil.UpdateCheckedScopeSpecifier(CurrStmt, CSS);

// The In of the current statement is the value of StmtOut computed so far.
BoundsMapTy InOfCurrStmt = StmtOut;

Expand Down Expand Up @@ -125,6 +121,9 @@ BoundsMapTy BoundsWideningAnalysis::GetOutOfLastStmt(
Expr *OriginalLValue = std::get<1>(ValuesToReplaceInBounds);
VarSetTy PtrsWithAffectedBounds = std::get<2>(ValuesToReplaceInBounds);

// TODO: Determine the Checked scope for each statement.
CheckedScopeSpecifier CSS = CheckedScopeSpecifier::CSS_None;

for (const VarDecl *V : PtrsWithAffectedBounds) {
auto StmtInIt = InOfCurrStmt.find(V);
if (StmtInIt == InOfCurrStmt.end())
Expand Down Expand Up @@ -1420,14 +1419,6 @@ const VarDecl *BoundsWideningUtil::GetNullTermPtrInExpr(Expr *E) const {
return nullptr;
}

void BoundsWideningUtil::UpdateCheckedScopeSpecifier(
const Stmt *CurrStmt, CheckedScopeSpecifier &CSS) const {

auto ScopeIt = CheckedScopeMap.find(CurrStmt);
if (ScopeIt != CheckedScopeMap.end())
CSS = ScopeIt->second;
}

Expr *BoundsWideningUtil::IgnoreCasts(const Expr *E) const {
return Lex.IgnoreValuePreservingOperations(Ctx, const_cast<Expr *>(E));
}
Expand Down
46 changes: 0 additions & 46 deletions clang/lib/Sema/CheckedCAnalysesPrepass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,52 +277,6 @@ class PrepassHelper : public RecursiveASTVisitor<PrepassHelper> {
return ProcessWhereClause(S->getWhereClause());
}

bool VisitCompoundStmt(CompoundStmt *CS) {
if (!CS)
return true;

// Get the checked scope specifier for the current compound statement.
CheckedScopeSpecifier CSS = CS->getCheckedSpecifier();

bool FirstStmt = true;

// A compound statement is of the form {stmt stmt}. Iterate through the
// nested statements of the compound statement.
for (auto I = CS->body_begin(), E = CS->body_end(); I != E; ++I) {
const Stmt *CurrStmt = *I;

// If this is a compound statement the RecursiveASTVisitor will visit
// it later. So we don't need to process it now.
if (isa<CompoundStmt>(CurrStmt)) {
continue;

// Else if this is a label statement. Basic blocks may begin at label
// statements. We need to be able to update the CSS value correctly at
// the beginning of a basic block as we are assured of ordered lookup
// only within a basic block (i.e. the first statement of a basic block
// may be accessed out of statement-order). The AST only stores the
// sub-statement of a label statement. So accordingly we store the CSS
// for the sub-statement.
} else if (auto *L = dyn_cast<LabelStmt>(CurrStmt)) {
Info.CheckedScopeMap[L->getSubStmt()] = CSS;

// Else if this is the first statement in a compound statement store
// its checked scope specifier.
} else if (FirstStmt) {
Info.CheckedScopeMap[CurrStmt] = CSS;

// Else if the previous statement was a compound statement store the
// checked scope specifier of the current statement.
} else if (isa<CompoundStmt>(*(I - 1))) {
Info.CheckedScopeMap[CurrStmt] = CSS;
}

FirstStmt = false;
}

return true;
}

void DumpBoundsVars(FunctionDecl *FD) {
PrintDeclMap<const VarDecl *>(FD, "BoundsVars Lower",
Info.BoundsVarsLower);
Expand Down
6 changes: 2 additions & 4 deletions clang/lib/Sema/SemaBounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2576,8 +2576,7 @@ namespace {
Facts(Facts),
BoundsWideningAnalyzer(BoundsWideningAnalysis(SemaRef, Cfg,
Info.BoundsVarsLower,
Info.BoundsVarsUpper,
Info.CheckedScopeMap)),
Info.BoundsVarsUpper)),
AbstractSetMgr(AbstractSetManager(SemaRef, Info.VarUses)),
BoundsSiblingFields(Info.BoundsSiblingFields),
IncludeNullTerminator(false) {}
Expand All @@ -2594,8 +2593,7 @@ namespace {
Facts(Facts),
BoundsWideningAnalyzer(BoundsWideningAnalysis(SemaRef, nullptr,
Info.BoundsVarsLower,
Info.BoundsVarsUpper,
Info.CheckedScopeMap)),
Info.BoundsVarsUpper)),
AbstractSetMgr(AbstractSetManager(SemaRef, Info.VarUses)),
BoundsSiblingFields(Info.BoundsSiblingFields),
IncludeNullTerminator(false) {}
Expand Down