Skip to content

Major refactor #6

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

Closed
wants to merge 63 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
43051e6
Update DeclStmt to Stmt inside DAndReplace
rchyena Feb 20, 2019
22a1cd3
Add comments to output for array pointers.
rchyena Jun 5, 2019
a17cf58
Refectoring Utils and ConstraintVariables
Machiry Jun 7, 2019
1eb2c0c
Minor edits
Machiry Jun 7, 2019
2bc726c
Merge pull request #1 from Machiry/arr_ptr
Machiry Jun 7, 2019
ed23ffa
Finishing up refactoring
Machiry Jun 7, 2019
3062366
initial template
Machiry Jun 10, 2019
4d5e7d5
Initial prototype
Machiry Jun 10, 2019
2f62258
Minor fixes
Machiry Jun 11, 2019
0d6a6f2
Fixing minor
Machiry Jun 12, 2019
736b0d1
Fixing the void * issue
Machiry Jun 12, 2019
57472e7
Adding NT Array contraint type
Machiry Jun 13, 2019
3b14f74
Finalizing NT_array detection
Machiry Jun 16, 2019
539b7ca
Minor refactoring
Machiry Jun 16, 2019
4e344b4
Adidng unit tests
Machiry Jun 17, 2019
b998f5d
Minor fixes
Machiry Jun 17, 2019
eb3d503
Minor refactoring and fixed unit tests
Machiry Jun 18, 2019
987110a
Modified lattice
Machiry Jun 18, 2019
5d39407
Clean up and refactoring
Machiry Jun 19, 2019
e7f5bd8
Finalizing NT array implementation
Machiry Jun 19, 2019
86c919d
Merge branch 'master' into debuggingNT
Machiry Jun 19, 2019
f782fa0
Merge pull request #2 from Machiry/debuggingNT
Machiry Jun 19, 2019
2bff6eb
Refactoring
Machiry Jun 19, 2019
4d4c4d5
Added to json methods
Machiry Jun 20, 2019
a781b9f
Minor fix
Machiry Jun 20, 2019
25c25bb
Adding dump to json functionality
Machiry Jun 20, 2019
772fc2c
WIP/getVariable
Machiry Jun 21, 2019
5ce5a7f
WIP/Fixed constraint generation of CallExpression
Machiry Jun 21, 2019
42693cf
Fixing Adding equality constraints
Machiry Jun 21, 2019
c220c98
Fixing local assignement'
Machiry Jun 21, 2019
0f3fb74
Refactored Rewriter
Machiry Jun 21, 2019
1aa949b
Minor fixes
Machiry Jun 21, 2019
7b3fb35
Fix: Never store information based on AST objects
Machiry Jun 22, 2019
9dde35e
Adding return type handling for itypes
Machiry Jun 23, 2019
8b019e8
Adding flag to control the merging of types from multiple declarations
Machiry Jun 23, 2019
bf802d0
Minor fixes
Machiry Jun 24, 2019
60230fd
Efficient constraint propagation: check before we propogatte based on…
Machiry Jun 24, 2019
28f010f
Avoid getting inscope variables for external functions:
Machiry Jun 24, 2019
90b3682
Added functionality tests
Machiry Jun 24, 2019
f769b9b
Added readme for functionality tests
Machiry Jun 24, 2019
b06f1b1
Minor fir for handling function pointers
Machiry Jun 25, 2019
1bca902
Fixed the addition of storage qualifiers (i.e., static and extern)
Machiry Jun 25, 2019
ec19850
Handling passing of functions as variables (for function pointers)
Machiry Jun 25, 2019
2517d84
Fixing handling of compiler args
Machiry Jun 26, 2019
0312dcc
Removing unnecessary debug output
Machiry Jun 26, 2019
73b8199
Minor
Machiry Jun 26, 2019
c3e6578
Merge branch 'major_refactor' of github.com:plum-umd/checkedc-clang i…
Machiry Jun 26, 2019
999330a
Ensuring function declaration to be used for all CallExpressions and …
Machiry Jun 26, 2019
084f910
Merge branch 'major_refactor' of github.com:plum-umd/checkedc-clang i…
Machiry Jun 26, 2019
68d4c11
Avoid Rewriting function return type, if it was handled by cast rewri…
Machiry Jun 27, 2019
5f8c2f0
Using absolute paths for files - this is needed to consistently displ…
Machiry Jun 27, 2019
b03af46
Handling NULL assignment, fixing rewriter, fixing duplicate constrain…
Machiry Jun 28, 2019
cdadea2
Adding base_dir argument to be passed to the tool
Machiry Jun 28, 2019
837be11
Fixing script to produce valid checked-c-convert commands
Machiry Jun 28, 2019
57e2e1a
Fixing minor regression
Machiry Jun 28, 2019
0141a69
Ensuring that we do not make an itype constraint variable to be WILD
Machiry Jun 28, 2019
c3bdbb8
Small and cute constraint debugger
Machiry Jun 29, 2019
f76d88f
Flag to enable handling of varargs
Machiry Jun 29, 2019
11f061c
Ignoring varargs and handling of itypes, Huge Improvement (Ptr: 27%, …
Machiry Jun 30, 2019
d5f8778
Removing unused variables
Machiry Jun 30, 2019
97662d5
Fixing missing * in the return type during itype
Machiry Jun 30, 2019
a9cf209
Adding correct type
Machiry Jul 9, 2019
2e4436a
Adding correct NT array type
Machiry Jul 9, 2019
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ test/debuginfo-tests
.vscode
.vs

# Pycharm IDE files.
.idea/
.cmake-build-debug/

153 changes: 153 additions & 0 deletions tools/checked-c-convert/ArrayBoundsInferenceConsumer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Implementation of all the methods of the class ArrayBoundsInferenceConsumer.
//===----------------------------------------------------------------------===//

#include "ArrayBoundsInferenceConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"

// This visitor handles the bounds of function local array variables.

bool LocalVarABVisitor::VisitBinAssign(BinaryOperator *O) {
Expr *LHS = removeImpCasts(O->getLHS());
Expr *RHS = removeImpCasts(O->getRHS());

Expr *sizeExpression;
// is the RHS expression a call to allocator function?
if(isAllocatorCall(RHS, &sizeExpression)) {
// if this an allocator function then
// sizeExpression contains the argument
// used for size argument

// if LHS is just a variable?
// i.e., ptr = ..
// if yes, get the AST node of the target variable
Decl *targetVar;
if(isExpressionSimpleLocalVar(LHS, &targetVar)) {
if(Info.isIdentifiedArrayVar(targetVar)) {
Info.addAllocationBasedSizeExpr(targetVar, sizeExpression);
} else {
dumpNotArrayIdentifiedVariable(targetVar, RHS, llvm::dbgs());
}
}
}

return true;
}

bool LocalVarABVisitor::VisitDeclStmt(DeclStmt *S) {
// Build rules based on initializers.
for (const auto &D : S->decls()) {
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Expr *InitE = VD->getInit();
Expr *sizeArg;
if(isAllocatorCall(InitE, &sizeArg)) {
if(Info.isIdentifiedArrayVar(D)) {
Info.addAllocationBasedSizeExpr(D, sizeArg);
} else {
dumpNotArrayIdentifiedVariable(D, InitE, llvm::dbgs());
}
}
}
}

return true;
}

// check if the provided expression is a call
// to known memory allocators.
// if yes, return true along with the argument used as size
// assigned to the second paramter i.e., sizeArgument
bool LocalVarABVisitor::isAllocatorCall(Expr *currExpr, Expr **sizeArgument) {
if(currExpr != nullptr) {
currExpr = removeAuxillaryCasts(currExpr);
// check if this is a call expression.
if (CallExpr *CA = dyn_cast<CallExpr>(currExpr)) {
// Is this a call to a named function?
FunctionDecl *calleeDecl = dyn_cast<FunctionDecl>(CA->getCalleeDecl());
if (calleeDecl) {
StringRef funcName = calleeDecl->getName();
// check if the called function is a known allocator?
if (LocalVarABVisitor::AllocatorFunctionNames.find(funcName) !=
LocalVarABVisitor::AllocatorFunctionNames.end()) {
if (sizeArgument != nullptr) {
*sizeArgument = CA->getArg(0);
}
return true;
}
}
}
}
return false;
}

// check if expression is a simple local variable
// i.e., ptr = .
// if yes, return the referenced local variable as the return
// value of the argument.
bool LocalVarABVisitor::isExpressionSimpleLocalVar(Expr *toCheck, Decl **targetDecl) {
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(toCheck)) {
if (DeclaratorDecl *FD = dyn_cast<DeclaratorDecl>(DRE->getDecl())) {
if (Decl *V = dyn_cast<Decl>(FD)) {
*targetDecl = V;
return true;
}
}
}
return false;
}

Expr *LocalVarABVisitor::removeImpCasts(Expr *toConvert) {
if(ImplicitCastExpr *impCast =dyn_cast<ImplicitCastExpr>(toConvert)) {
return impCast->getSubExpr();
}
return toConvert;
}

Expr *LocalVarABVisitor::removeCHKCBindTempExpr(Expr *toVeri) {
if(CHKCBindTemporaryExpr *toChkExpr = dyn_cast<CHKCBindTemporaryExpr>(toVeri)) {
return toChkExpr->getSubExpr();
}
return toVeri;
}

void LocalVarABVisitor::dumpNotArrayIdentifiedVariable(Decl *LHS, Expr *RHS, raw_ostream &O) {
#ifdef DEBUG
O << "Not identified as a array variable.\n RHS:";
RHS->dump(O);
O << "\n LHS:";
LHS->dump(O);
O << "\n";
#endif
}

Expr *LocalVarABVisitor::removeAuxillaryCasts(Expr *srcExpr) {
srcExpr = removeCHKCBindTempExpr(srcExpr);
if (CStyleCastExpr *C = dyn_cast<CStyleCastExpr>(srcExpr)) {
srcExpr = C->getSubExpr();
}
srcExpr = removeCHKCBindTempExpr(srcExpr);
srcExpr = removeImpCasts(srcExpr);
return srcExpr;
}

std::set<std::string> LocalVarABVisitor::AllocatorFunctionNames = {"malloc", "calloc"};

void HandleArrayVariablesBoundsDetection(ASTContext *C, ProgramInfo &I) {
// Run array bounds
LocalVarABVisitor LVAB(C, I);
TranslationUnitDecl *TUD = C->getTranslationUnitDecl();
for (const auto &D : TUD->decls()) {
if (dyn_cast<FunctionDecl>(D)) {
FunctionDecl *fb = dyn_cast<FunctionDecl>(D);
#ifdef DEBUG
llvm::dbgs() << "Analyzing function:" << fb->getName() << "\n";
#endif
}
LVAB.TraverseDecl(D);
}
}
59 changes: 59 additions & 0 deletions tools/checked-c-convert/ArrayBoundsInferenceConsumer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// This is an ASTConsumer that tries to infer the CheckedC style bounds
// for identified array variables.
//===----------------------------------------------------------------------===//

#ifndef _ARRAYBOUNDSINFERENCECONSUMER_H
#define _ARRAYBOUNDSINFERENCECONSUMER_H

#include "clang/AST/ASTConsumer.h"

#include "ProgramInfo.h"


class LocalVarABVisitor: public clang::RecursiveASTVisitor<LocalVarABVisitor> {
public:
explicit LocalVarABVisitor(ASTContext *C, ProgramInfo &I)
: Context(C), Info(I) {}

// handles assignment expression.
bool VisitBinAssign(BinaryOperator *O);


bool VisitDeclStmt(DeclStmt *S);
private:
// check if the provided expression is a call
// to known memory allocators.
// if yes, return true along with the argument used as size
// assigned to the second paramter i.e., sizeArgument
bool isAllocatorCall(Expr *currExpr, Expr **sizeArgument);

// check if expression is a simple local variable
// i.e., ptr = .
// if yes, return the referenced local variable as the return
// value of the argument.
bool isExpressionSimpleLocalVar(Expr *toCheck, Decl **targetDecl);

Expr *removeCHKCBindTempExpr(Expr *toVeri);

// remove implicit casts added by clang to the AST
Expr *removeImpCasts(Expr *toConvert);

Expr *removeAuxillaryCasts(Expr *srcExpr);

// print variables that should have been detected as arrays but not.
void dumpNotArrayIdentifiedVariable(Decl *LHS, Expr *RHS, raw_ostream &O);

ASTContext *Context;
ProgramInfo &Info;
static std::set<std::string> AllocatorFunctionNames;
};

void HandleArrayVariablesBoundsDetection(ASTContext *C, ProgramInfo &I);

#endif //_ARRAYBOUNDSINFERENCECONSUMER_H
4 changes: 4 additions & 0 deletions tools/checked-c-convert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ add_clang_executable(checked-c-convert
ConstraintBuilder.cpp
PersistentSourceLoc.cpp
Constraints.cpp
ConstraintVariables.cpp
ArrayBoundsInferenceConsumer.cpp
RewriteUtils.cpp
Utils.cpp
)

target_link_libraries(checked-c-convert
Expand Down
Loading