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

Fix for malloc incorrectly preventing checked region addition (issue #486) #527

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
4 changes: 4 additions & 0 deletions clang/include/clang/3C/ConstraintVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class ConstraintVariable {
// Here, AIdx is the pointer level which needs to be checked.
// By default, we check for all pointer levels (or VarAtoms)
virtual bool hasWild(const EnvironmentMap &E, int AIdx = -1) const = 0;
virtual bool hasParamWild(const EnvironmentMap &E) const = 0;
virtual bool hasArr(const EnvironmentMap &E, int AIdx = -1) const = 0;
virtual bool hasNtArr(const EnvironmentMap &E, int AIdx = -1) const = 0;

Expand Down Expand Up @@ -416,6 +417,7 @@ class PointerVariableConstraint : public ConstraintVariable {
bool anyChanges(const EnvironmentMap &E) const override;
bool anyArgumentIsWild(const EnvironmentMap &E);
bool hasWild(const EnvironmentMap &E, int AIdx = -1) const override;
bool hasParamWild(const EnvironmentMap &E) const override;
bool hasArr(const EnvironmentMap &E, int AIdx = -1) const override;
bool hasNtArr(const EnvironmentMap &E, int AIdx = -1) const override;

Expand Down Expand Up @@ -476,6 +478,7 @@ class FVComponentVariable {

bool hasItypeSolution(Constraints &CS) const;
bool hasCheckedSolution(Constraints &CS) const;
bool hasWild(const EnvironmentMap &E) const;

PVConstraint *getInternal() const { return InternalConstraint; }
PVConstraint *getExternal() const { return ExternalConstraint; }
Expand Down Expand Up @@ -585,6 +588,7 @@ class FunctionVariableConstraint : public ConstraintVariable {
PersistentSourceLoc *PL) const override;
bool anyChanges(const EnvironmentMap &E) const override;
bool hasWild(const EnvironmentMap &E, int AIdx = -1) const override;
bool hasParamWild(const EnvironmentMap &E) const override;
bool hasArr(const EnvironmentMap &E, int AIdx = -1) const override;
bool hasNtArr(const EnvironmentMap &E, int AIdx = -1) const override;

Expand Down
18 changes: 12 additions & 6 deletions clang/lib/3C/CheckedRegions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ bool CheckedRegionFinder::VisitCStyleCastExpr(CStyleCastExpr *E) {
return true;
}





bool CheckedRegionFinder::VisitCallExpr(CallExpr *C) {
auto *FD = C->getDirectCallee();
FoldingSetNodeID ID;
Expand All @@ -227,9 +231,10 @@ bool CheckedRegionFinder::VisitCallExpr(CallExpr *C) {
if (Info.hasTypeParamBindings(C,Context))
for (auto Entry : Info.getTypeParamBindings(C, Context))
Wild |= (Entry.second == nullptr);
auto Type = FD->getReturnType();
const auto IT = FD->getInteropType();
const auto Type = FD->getReturnType();
Wild |= (!(FD->hasPrototype() || FD->doesThisDeclarationHaveABody())) ||
containsUncheckedPtr(Type);
containsUncheckedPtr(IT.getTypePtrOrNull() ? IT : Type);
auto *FV = Info.getFuncConstraint(FD, Context);
for (unsigned I = 0; I < FV->numParams(); I++)
Wild |= isWild(*FV->getExternalParam(I));
Expand Down Expand Up @@ -340,10 +345,11 @@ bool CheckedRegionFinder::isInStatementPosition(CallExpr *C) {
}

bool CheckedRegionFinder::isWild(CVarOption Cv) {
if (Cv.hasValue() &&
Cv.getValue().hasWild(Info.getConstraints().getVariables()))
return true;
return false;
const auto &E = Info.getConstraints().getVariables();
if (Cv.hasValue())
return Cv.getValue().hasWild(E) || Cv.getValue().hasParamWild(E);
else
return false;
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering where the presence of type instantiations (i.e., that you return malloc<int>(...) and not malloc(...)) is showing up?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Line 227 in the handling of call expressions

}

bool CheckedRegionFinder::containsUncheckedPtr(QualType Qt) {
Expand Down
18 changes: 17 additions & 1 deletion clang/lib/3C/ConstraintVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/CommandLine.h"
#include <sstream>
#include <algorithm>

using namespace clang;
// Macro for boolean implication.
Expand Down Expand Up @@ -1049,11 +1050,15 @@ bool FunctionVariableConstraint::anyChanges(const EnvironmentMap &E) const {
return CV.ExternalConstraint->anyChanges(E);
});
}

bool FunctionVariableConstraint::hasWild(const EnvironmentMap &E,
int AIdx) const {
return ReturnVar.ExternalConstraint->hasWild(E, AIdx);
}
bool FunctionVariableConstraint::hasParamWild(const EnvironmentMap &E) const {
return std::any_of(this->ParamVars.begin(), this->ParamVars.end(),
[&E](const auto V) { return V.hasWild(E); } );
}


bool FunctionVariableConstraint::hasArr(const EnvironmentMap &E,
int AIdx) const {
Expand Down Expand Up @@ -1241,6 +1246,13 @@ bool PointerVariableConstraint::hasWild(const EnvironmentMap &E,
return false;
}

bool PointerVariableConstraint::hasParamWild(const EnvironmentMap &E) const {
if (const auto *TFV = this->getFV())
return TFV->hasParamWild(E);
else
return false;
}

bool PointerVariableConstraint::hasArr(const EnvironmentMap &E,
int AIdx) const {
int VarIdx = 0;
Expand Down Expand Up @@ -1915,6 +1927,10 @@ std::string FVComponentVariable::mkItypeStr(Constraints &CS) const {
return "";
}

bool FVComponentVariable::hasWild(const EnvironmentMap &E) const {
return InternalConstraint->hasWild(E);
}

bool FVComponentVariable::hasCheckedSolution(Constraints &CS) const {
// If the external constraint variable is checked, then the variable should
// be advertised as checked to callers. If the internal and external
Expand Down
2 changes: 1 addition & 1 deletion clang/test/3C/3d-allocation.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern _Itype_for_any(T) void *malloc(size_t size)

int ***malloc3d(int y, int x, int z) {
//CHECK_NOALL: int ***malloc3d(int y, int x, int z) : itype(_Ptr<int **>) {
//CHECK_ALL: _Array_ptr<_Array_ptr<_Array_ptr<int>>> malloc3d(int y, int x, int z) : count(y) {
//CHECK_ALL: _Array_ptr<_Array_ptr<_Array_ptr<int>>> malloc3d(int y, int x, int z) : count(y) _Checked {

int i, j;

Expand Down
2 changes: 2 additions & 0 deletions clang/test/3C/b13_calleestructp.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct r {
};

struct p sus(struct p x) {
//CHECK_NOALL: struct p sus(struct p x) {
//CHECK_ALL: struct p sus(struct p x) _Checked {
x.x += 1;
struct p *n = malloc(sizeof(struct p));
//CHECK: _Ptr<struct p> n = malloc<struct p>(sizeof(struct p));
Expand Down
2 changes: 2 additions & 0 deletions clang/test/3C/b14_callerstructp.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct r {
};

struct p sus(struct p x) {
//CHECK_NOALL: struct p sus(struct p x) {
//CHECK_ALL: struct p sus(struct p x) _Checked {
struct p *n = malloc(sizeof(struct p));
//CHECK: _Ptr<struct p> n = malloc<struct p>(sizeof(struct p));
return *n;
Expand Down
2 changes: 2 additions & 0 deletions clang/test/3C/b18_bothstructp.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct r {
};

struct p sus(struct p x) {
//CHECK_NOALL: struct p sus(struct p x) {
//CHECK_ALL: struct p sus(struct p x) _Checked {
x.x += 1;
struct p *n = malloc(sizeof(struct p));
//CHECK: _Ptr<struct p> n = malloc<struct p>(sizeof(struct p));
Expand Down
8 changes: 3 additions & 5 deletions clang/test/3C/b1_allsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern _Unchecked char *strcpy(char *restrict dest, const char *restrict src

int *sus(int *x, int *y) {
//CHECK_NOALL: _Ptr<int> sus(int *x : itype(_Ptr<int>), _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) _Checked {
int *z = malloc(sizeof(int));
//CHECK: _Ptr<int> z = malloc<int>(sizeof(int));
*z = 1;
Expand All @@ -32,8 +32,7 @@ int *sus(int *x, int *y) {
}

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Ptr<int> foo(void) {
//CHECK: _Ptr<int> foo(void) {
int sx = 3, sy = 4;
int *x = &sx;
//CHECK_NOALL: _Ptr<int> x = &sx;
Expand All @@ -48,8 +47,7 @@ int *foo() {
}

int *bar() {
//CHECK_NOALL: _Ptr<int> bar(void) _Checked {
//CHECK_ALL: _Ptr<int> bar(void) {
//CHECK: _Ptr<int> bar(void) {
int sx = 3, sy = 4;
int *x = &sx;
//CHECK_NOALL: _Ptr<int> x = &sx;
Expand Down
2 changes: 1 addition & 1 deletion clang/test/3C/b23_explicitunsafecast.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern _Unchecked char *strcpy(char *restrict dest, const char *restrict src

int *sus(int *x, int *y) {
//CHECK_NOALL: _Ptr<int> sus(int *x : itype(_Ptr<int>), _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) _Checked {
int *z = malloc(sizeof(int));
//CHECK: _Ptr<int> z = malloc<int>(sizeof(int));
*z = 1;
Expand Down
2 changes: 1 addition & 1 deletion clang/test/3C/b24_implicitunsafecast.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern _Unchecked char *strcpy(char *restrict dest, const char *restrict src

int *sus(int *x, int *y) {
//CHECK_NOALL: _Ptr<int> sus(int *x : itype(_Ptr<int>), _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) _Checked {
int *z = malloc(sizeof(int));
//CHECK: _Ptr<int> z = malloc<int>(sizeof(int));
*z = 1;
Expand Down
2 changes: 1 addition & 1 deletion clang/test/3C/b24_retswitchimplicit.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern _Unchecked char *strcpy(char *restrict dest, const char *restrict src

char *sus(int *x, int *y) {
//CHECK_NOALL: _Ptr<char> sus(int *x : itype(_Ptr<int>), _Ptr<int> y) {
//CHECK_ALL: _Ptr<char> sus(_Array_ptr<int> x, _Ptr<int> y) {
//CHECK_ALL: _Ptr<char> sus(_Array_ptr<int> x, _Ptr<int> y) _Checked {
char *z = malloc(sizeof(char));
//CHECK: _Ptr<char> z = malloc<char>(sizeof(char));
*z = 1;
Expand Down
2 changes: 1 addition & 1 deletion clang/test/3C/b25_castprotosafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int *bar() {

int *sus(int *x, int *y) {
//CHECK_NOALL: _Ptr<int> sus(int *x : itype(_Ptr<int>), _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) _Checked {
int *z = malloc(sizeof(int));
//CHECK: _Ptr<int> z = malloc<int>(sizeof(int));
*z = 1;
Expand Down
2 changes: 1 addition & 1 deletion clang/test/3C/b26_castprotounsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ char *bar() {

int *sus(int *x, int *y) {
//CHECK_NOALL: _Ptr<int> sus(int *x : itype(_Ptr<int>), _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) _Checked {
int *z = malloc(sizeof(int));
//CHECK: _Ptr<int> z = malloc<int>(sizeof(int));
*z = 1;
Expand Down
2 changes: 1 addition & 1 deletion clang/test/3C/b26_castprotounsafeimplicit.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ char *bar() {

int *sus(int *x, int *y) {
//CHECK_NOALL: _Ptr<int> sus(int *x : itype(_Ptr<int>), _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) _Checked {
int *z = malloc(sizeof(int));
//CHECK: _Ptr<int> z = malloc<int>(sizeof(int));
*z = 1;
Expand Down
2 changes: 1 addition & 1 deletion clang/test/3C/b26_castprotounsafeimplicitretswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int *bar() {

char *sus(int *x, int *y) {
//CHECK_NOALL: _Ptr<char> sus(int *x : itype(_Ptr<int>), _Ptr<int> y) {
//CHECK_ALL: _Ptr<char> sus(_Array_ptr<int> x, _Ptr<int> y) {
//CHECK_ALL: _Ptr<char> sus(_Array_ptr<int> x, _Ptr<int> y) _Checked {
char *z = malloc(sizeof(char));
//CHECK: _Ptr<char> z = malloc<char>(sizeof(char));
*z = 1;
Expand Down
6 changes: 2 additions & 4 deletions clang/test/3C/b2_calleeunsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ int *sus(int *x, int *y) {
}

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Ptr<int> foo(void) {
//CHECK: _Ptr<int> foo(void) {
int sx = 3, sy = 4;
int *x = &sx;
//CHECK_NOALL: _Ptr<int> x = &sx;
Expand All @@ -49,8 +48,7 @@ int *foo() {
}

int *bar() {
//CHECK_NOALL: _Ptr<int> bar(void) _Checked {
//CHECK_ALL: _Ptr<int> bar(void) {
//CHECK: _Ptr<int> bar(void) {
int sx = 3, sy = 4;
int *x = &sx;
//CHECK_NOALL: _Ptr<int> x = &sx;
Expand Down
3 changes: 2 additions & 1 deletion clang/test/3C/b30_structprotoconflictbodyconvert.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ struct np *bar() {
}

struct np *sus(struct r *x, struct r *y) {
//CHECK: _Ptr<struct np> sus(_Ptr<struct r> x, _Ptr<struct r> y) {
//CHECK_NOALL: _Ptr<struct np> sus(_Ptr<struct r> x, _Ptr<struct r> y) {
//CHECK_ALL: _Ptr<struct np> sus(_Ptr<struct r> x, _Ptr<struct r> y) _Checked {
x->next += 1;
struct np *z = malloc(sizeof(struct np));
//CHECK: _Ptr<struct np> z = malloc<struct np>(sizeof(struct np));
Expand Down
3 changes: 1 addition & 2 deletions clang/test/3C/b3_onecallerunsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ int *sus(int *x, int *y) {
}

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Ptr<int> foo(void) {
//CHECK: _Ptr<int> foo(void) {
int sx = 3;
int sy = 4;
int *x = &sx;
Expand Down
3 changes: 1 addition & 2 deletions clang/test/3C/b4_bothunsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ int *sus(int *x, int *y) {
}

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Ptr<int> foo(void) {
//CHECK: _Ptr<int> foo(void) {
int sx = 3, sy = 4;
int *x = &sx;
//CHECK_NOALL: _Ptr<int> x = &sx;
Expand Down
6 changes: 2 additions & 4 deletions clang/test/3C/b5_calleeunsafeproto.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ int *sus(int *, int *);
//CHECK_ALL: int *sus(_Array_ptr<int> x, _Ptr<int> y) : itype(_Ptr<int>);

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Ptr<int> foo(void) {
//CHECK: _Ptr<int> foo(void) {
int sx = 3, sy = 4;
int *x = &sx;
//CHECK_NOALL: _Ptr<int> x = &sx;
Expand All @@ -41,8 +40,7 @@ int *foo() {
}

int *bar() {
//CHECK_NOALL: _Ptr<int> bar(void) _Checked {
//CHECK_ALL: _Ptr<int> bar(void) {
//CHECK: _Ptr<int> bar(void) {
int sx = 3, sy = 4;
int *x = &sx;
//CHECK_NOALL: _Ptr<int> x = &sx;
Expand Down
3 changes: 1 addition & 2 deletions clang/test/3C/b6_callerunsafeproto.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ int *sus(int *, int *);
//CHECK_ALL: int *sus(_Array_ptr<int> x, _Ptr<int> y) : itype(_Array_ptr<int>);

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Ptr<int> foo(void) {
//CHECK: _Ptr<int> foo(void) {
int sx = 3, sy = 4;
int *x = &sx;
//CHECK_NOALL: _Ptr<int> x = &sx;
Expand Down
8 changes: 3 additions & 5 deletions clang/test/3C/b7_allsafeproto.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ int *sus(int *, int *);
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y);

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Ptr<int> foo(void) {
//CHECK: _Ptr<int> foo(void) {
int sx = 3, sy = 4;
int *x = &sx;
//CHECK_NOALL: _Ptr<int> x = &sx;
Expand All @@ -41,8 +40,7 @@ int *foo() {
}

int *bar() {
//CHECK_NOALL: _Ptr<int> bar(void) _Checked {
//CHECK_ALL: _Ptr<int> bar(void) {
//CHECK: _Ptr<int> bar(void) {
int sx = 3, sy = 4;
int *x = &sx;
//CHECK_NOALL: _Ptr<int> x = &sx;
Expand All @@ -57,7 +55,7 @@ int *bar() {

int *sus(int *x, int *y) {
//CHECK_NOALL: _Ptr<int> sus(int *x : itype(_Ptr<int>), _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) {
//CHECK_ALL: _Ptr<int> sus(_Array_ptr<int> x, _Ptr<int> y) _Checked {
int *z = malloc(sizeof(int));
//CHECK: _Ptr<int> z = malloc<int>(sizeof(int));
*z = 1;
Expand Down
1 change: 1 addition & 0 deletions clang/test/3C/b9_allsafestructp.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct r {
};

struct p sus(struct p x) {
//CHECK: struct p sus(struct p x) _Checked {
struct p *n = malloc(sizeof(struct p));
//CHECK: _Ptr<struct p> n = malloc<struct p>(sizeof(struct p));
return *n;
Expand Down
4 changes: 2 additions & 2 deletions clang/test/3C/complex_expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ int *foo(int *y, int *w) {
//CHECK-NEXT: return x;
//CHECK-NEXT: }

//CHECK_ALL: _Array_ptr<int> foo(_Array_ptr<int> y, _Ptr<int> w) _Checked {
//CHECK_ALL: _Array_ptr<int> foo(_Array_ptr<int> y, _Ptr<int> w) {
//CHECK_ALL: _Array_ptr<int> z = 0;
//CHECK_NOALL: _Ptr<int> foo(_Ptr<int> y, _Ptr<int> w) _Checked {
//CHECK_NOALL: _Ptr<int> foo(_Ptr<int> y, _Ptr<int> w) {
//CHECK_NOALL: _Ptr<int> z = 0;

void baz(int *p) {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/3C/fn_sets.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int *g(int *y) {
}

void foo(int *z) {
//CHECK: void foo(_Ptr<int> z) _Checked {
//CHECK: void foo(_Ptr<int> z) {
int *w = (0 ? f : g)(z);
//CHECK: _Ptr<int> w = (0 ? f : g)(z);
}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/3C/fptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ char *bar(int x) {
}

int f(char c) {
//CHECK: int f(char c) _Checked {
//CHECK: int f(char c) {
int (*x)(char *) = &foo;
//CHECK: _Ptr<int (char * : itype(_Ptr<char>))> x = &foo;

Expand Down
Loading