Skip to content

Commit 6d37bfe

Browse files
authored
Merge pull request #260 from plum-umd/issue_239
Fixing issue 239
2 parents c81f1fd + 4422d00 commit 6d37bfe

File tree

6 files changed

+78
-342
lines changed

6 files changed

+78
-342
lines changed

clang/include/clang/CConv/ArrayBoundsInferenceConsumer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class LocalVarABVisitor : public clang::RecursiveASTVisitor<LocalVarABVisitor> {
6262
bool isNonLengthParameter(ParmVarDecl *PVD);
6363

6464
private:
65+
void handleAssignment(BoundsKey LK, QualType LHSType, Expr *RHS);
6566
void addUsedParmVarDecl(Expr *CE);
6667
std::set<ParmVarDecl *> NonLengthParameters;
6768
ASTContext *Context;

clang/include/clang/CConv/ArrayBoundsInformation.h

Lines changed: 0 additions & 90 deletions
This file was deleted.

clang/lib/CConv/ArrayBoundsInferenceConsumer.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,17 @@ static bool needNTArrayBounds(ConstraintVariable *CV,
122122
static bool needArrayBounds(Expr *E, ProgramInfo &Info, ASTContext *C) {
123123
ConstraintResolver CR(Info, C);
124124
CVarSet ConsVar = CR.getExprConstraintVars(E);
125+
auto &EnvMap = Info.getConstraints().getVariables();
125126
for (auto CurrCVar : ConsVar) {
126-
if (needArrayBounds(CurrCVar, Info.getConstraints().getVariables()))
127+
if (needArrayBounds(CurrCVar, EnvMap) || needNTArrayBounds(CurrCVar, EnvMap))
127128
return true;
128129
return false;
129130
}
130131
return false;
131132
}
132133

133134
static bool needArrayBounds(Decl *D, ProgramInfo &Info, ASTContext *C,
134-
bool IsNtArr = false) {
135+
bool IsNtArr) {
135136
CVarSet ConsVar = Info.getVariable(D, C);
136137
auto &E = Info.getConstraints().getVariables();
137138
for (auto CurrCVar : ConsVar) {
@@ -143,6 +144,11 @@ static bool needArrayBounds(Decl *D, ProgramInfo &Info, ASTContext *C,
143144
return false;
144145
}
145146

147+
static bool needArrayBounds(Decl *D, ProgramInfo &Info, ASTContext *C) {
148+
return needArrayBounds(D, Info, C, false) ||
149+
needArrayBounds(D, Info, C, true);
150+
}
151+
146152
// Map that contains association of allocator functions and indexes of
147153
// parameters that correspond to the size of the object being assigned.
148154
static std::map<std::string, std::set<unsigned>> AllocatorSizeAssoc = {
@@ -440,14 +446,14 @@ bool GlobalABVisitor::VisitFunctionDecl(FunctionDecl *FD) {
440446
// Here, we are using heuristics. So we only use heuristics when
441447
// there are no bounds already computed.
442448
if (!ABInfo.getBounds(PK)) {
443-
if (needArrayBounds(PVD, Info, Context)) {
444-
// Is this an array?
445-
ParamArrays[i] = PVal;
446-
}
447449
if (needArrayBounds(PVD, Info, Context, true)) {
448450
// Is this an NTArray?
449451
ParamNtArrays[i] = PVal;
450452
}
453+
if (needArrayBounds(PVD, Info, Context, false)) {
454+
// Is this an array?
455+
ParamArrays[i] = PVal;
456+
}
451457
}
452458

453459
// If this is a length field?
@@ -530,6 +536,21 @@ bool GlobalABVisitor::VisitFunctionDecl(FunctionDecl *FD) {
530536
return true;
531537
}
532538

539+
void LocalVarABVisitor::handleAssignment(BoundsKey LK, QualType LHSType, Expr *RHS) {
540+
auto &ABoundsInfo = Info.getABoundsInfo();
541+
handleAllocatorCall(LHSType, LK, RHS, Info, Context);
542+
clang::StringLiteral *SL =
543+
dyn_cast_or_null<clang::StringLiteral>(RHS->IgnoreParenCasts());
544+
if (SL != nullptr) {
545+
ABounds *ByBounds =
546+
new ByteBound(ABoundsInfo.getConstKey(SL->getByteLength()));
547+
if (!ABoundsInfo.mergeBounds(LK, Allocator, ByBounds)) {
548+
delete (ByBounds);
549+
} else {
550+
ABoundsInfo.getBStats().AllocatorMatch.insert(LK);
551+
}
552+
}
553+
}
533554

534555
bool LocalVarABVisitor::HandleBinAssign(BinaryOperator *O) {
535556
Expr *LHS = O->getLHS()->IgnoreParenCasts();
@@ -541,7 +562,7 @@ bool LocalVarABVisitor::HandleBinAssign(BinaryOperator *O) {
541562
// is the RHS expression a call to allocator function?
542563
if (needArrayBounds(LHS, Info, Context) &&
543564
tryGetBoundsKeyVar(LHS, LK, Info, Context)) {
544-
handleAllocatorCall(LHS->getType(), LK, RHS, Info, Context);
565+
handleAssignment(LK, LHS->getType(), RHS);
545566
}
546567

547568
// Any parameter directly used as a condition in ternary expression
@@ -606,23 +627,9 @@ bool LocalVarABVisitor::VisitDeclStmt(DeclStmt *S) {
606627
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
607628
Expr *InitE = VD->getInit();
608629
BoundsKey DeclKey;
609-
if (InitE != nullptr && (needArrayBounds(VD, Info, Context) ||
610-
needArrayBounds(VD, Info, Context, true))) {
611-
clang::StringLiteral *SL =
612-
dyn_cast<clang::StringLiteral>(InitE->IgnoreParenCasts());
613-
if (tryGetBoundsKeyVar(VD, DeclKey, Info, Context)) {
614-
handleAllocatorCall(VD->getType(), DeclKey, InitE,
615-
Info, Context);
616-
if (SL != nullptr) {
617-
ABounds *ByBounds =
618-
new ByteBound(ABoundsInfo.getConstKey(SL->getByteLength()));
619-
if (!ABoundsInfo.mergeBounds(DeclKey, Allocator, ByBounds)) {
620-
delete (ByBounds);
621-
} else {
622-
ABoundsInfo.getBStats().AllocatorMatch.insert(DeclKey);
623-
}
624-
}
625-
}
630+
if (InitE != nullptr && needArrayBounds(VD, Info, Context) &&
631+
tryGetBoundsKeyVar(VD, DeclKey, Info, Context)) {
632+
handleAssignment(DeclKey, VD->getType(), InitE);
626633
}
627634
}
628635

0 commit comments

Comments
 (0)