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 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
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
20 changes: 11 additions & 9 deletions clang/lib/3C/CheckedRegions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,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 @@ -272,16 +273,16 @@ bool CheckedRegionFinder::VisitDeclRefExpr(DeclRefExpr *DR) {
auto T = DR->getType();
auto *D = DR->getDecl();
CVarOption CV = Info.getVariable(D, Context);
bool IW = isWild(CV) || containsUncheckedPtr(T);

bool IW = false;
if (auto *FD = dyn_cast<FunctionDecl>(D)) {
auto *FV = Info.getFuncConstraint(FD, Context);
IW |= FV->hasWild(Info.getConstraints().getVariables());
for (unsigned I = 0; I < FV->numParams(); I++) {
PVConstraint *ParamCV = FV->getExternalParam(I);
IW |= isWild(*ParamCV);
}
}
} else
IW = isWild(CV) || containsUncheckedPtr(T);

Wild |= IW;
return true;
Expand Down Expand Up @@ -340,10 +341,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
6 changes: 3 additions & 3 deletions clang/test/3C/arrboth.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ int *sus(int *x, int *y) {
}

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) {
//CHECK_ALL: _Array_ptr<int> foo(void) {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Array_ptr<int> foo(void) _Checked {
int *x = malloc(sizeof(int));
//CHECK: _Ptr<int> x = malloc<int>(sizeof(int));
int *y = malloc(sizeof(int));
Expand All @@ -147,7 +147,7 @@ int *foo() {

int *bar() {
//CHECK_NOALL: int *bar(void) : itype(_Ptr<int>) {
//CHECK_ALL: _Array_ptr<int> bar(void) {
//CHECK_ALL: _Array_ptr<int> bar(void) _Checked {
int *x = malloc(sizeof(int));
//CHECK: _Ptr<int> x = malloc<int>(sizeof(int));
int *y = malloc(sizeof(int));
Expand Down
6 changes: 3 additions & 3 deletions clang/test/3C/arrbothmulti1.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ int *sus(int *, int *);
//CHECK_ALL: _Array_ptr<int> sus(int *x : itype(_Ptr<int>), _Ptr<int> y);

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) {
//CHECK_ALL: _Array_ptr<int> foo(void) {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Array_ptr<int> foo(void) _Checked {
int *x = malloc(sizeof(int));
//CHECK: _Ptr<int> x = malloc<int>(sizeof(int));
int *y = malloc(sizeof(int));
Expand All @@ -137,7 +137,7 @@ int *foo() {

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

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) {
//CHECK_ALL: _Array_ptr<int> foo(void) {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Array_ptr<int> foo(void) _Checked {
int *x = malloc(sizeof(int));
//CHECK: _Ptr<int> x = malloc<int>(sizeof(int));
int *y = malloc(sizeof(int));
Expand All @@ -146,8 +146,8 @@ int *foo() {
}

int *bar() {
//CHECK_NOALL: _Ptr<int> bar(void) {
//CHECK_ALL: _Array_ptr<int> bar(void) {
//CHECK_NOALL: _Ptr<int> bar(void) _Checked {
//CHECK_ALL: _Array_ptr<int> bar(void) _Checked {
int *x = malloc(sizeof(int));
//CHECK: _Ptr<int> x = malloc<int>(sizeof(int));
int *y = malloc(sizeof(int));
Expand Down
8 changes: 4 additions & 4 deletions clang/test/3C/arrcalleemulti1.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ int *sus(int *, int *);
//CHECK_ALL: _Array_ptr<int> sus(int *x : itype(_Ptr<int>), _Ptr<int> y);

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) {
//CHECK_ALL: _Array_ptr<int> foo(void) {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Array_ptr<int> foo(void) _Checked {
int *x = malloc(sizeof(int));
//CHECK: _Ptr<int> x = malloc<int>(sizeof(int));
int *y = malloc(sizeof(int));
Expand All @@ -136,8 +136,8 @@ int *foo() {
}

int *bar() {
//CHECK_NOALL: _Ptr<int> bar(void) {
//CHECK_ALL: _Array_ptr<int> bar(void) {
//CHECK_NOALL: _Ptr<int> bar(void) _Checked {
//CHECK_ALL: _Array_ptr<int> bar(void) _Checked {
int *x = malloc(sizeof(int));
//CHECK: _Ptr<int> x = malloc<int>(sizeof(int));
int *y = malloc(sizeof(int));
Expand Down
6 changes: 3 additions & 3 deletions clang/test/3C/arrcaller.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ int *sus(int *x, int *y) {
}

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) {
//CHECK_ALL: _Array_ptr<int> foo(void) : count(5) {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Array_ptr<int> foo(void) : count(5) _Checked {
int *x = malloc(sizeof(int));
//CHECK: _Ptr<int> x = malloc<int>(sizeof(int));
int *y = malloc(sizeof(int));
Expand All @@ -146,7 +146,7 @@ int *foo() {

int *bar() {
//CHECK_NOALL: int *bar(void) : itype(_Ptr<int>) {
//CHECK_ALL: _Array_ptr<int> bar(void) {
//CHECK_ALL: _Array_ptr<int> bar(void) _Checked {
int *x = malloc(sizeof(int));
//CHECK: _Ptr<int> x = malloc<int>(sizeof(int));
int *y = malloc(sizeof(int));
Expand Down
6 changes: 3 additions & 3 deletions clang/test/3C/arrcallermulti1.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ int *sus(int *, int *);
//CHECK_ALL: _Array_ptr<int> sus(int *x : itype(_Ptr<int>), _Ptr<int> y) : count(5);

int *foo() {
//CHECK_NOALL: _Ptr<int> foo(void) {
//CHECK_ALL: _Array_ptr<int> foo(void) : count(5) {
//CHECK_NOALL: _Ptr<int> foo(void) _Checked {
//CHECK_ALL: _Array_ptr<int> foo(void) : count(5) _Checked {
int *x = malloc(sizeof(int));
//CHECK: _Ptr<int> x = malloc<int>(sizeof(int));
int *y = malloc(sizeof(int));
Expand All @@ -137,7 +137,7 @@ int *foo() {

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

struct warr *foo() {
//CHECK: _Ptr<struct warr> foo(void) {
//CHECK_NOALL: _Ptr<struct warr> foo(void) {
//CHECK_ALL: _Ptr<struct warr> foo(void) _Checked {
struct warr *x = malloc(sizeof(struct warr));
//CHECK: _Ptr<struct warr> x = malloc<struct warr>(sizeof(struct warr));
struct warr *y = malloc(sizeof(struct warr));
Expand All @@ -129,7 +130,8 @@ struct warr *foo() {
}

struct warr *bar() {
//CHECK: _Ptr<struct warr> bar(void) {
//CHECK_NOALL: _Ptr<struct warr> bar(void) {
//CHECK_ALL: _Ptr<struct warr> bar(void) _Checked {
struct warr *x = malloc(sizeof(struct warr));
//CHECK: _Ptr<struct warr> x = malloc<struct warr>(sizeof(struct warr));
struct warr *y = malloc(sizeof(struct warr));
Expand Down
6 changes: 4 additions & 2 deletions clang/test/3C/arrinstructsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ struct warr *sus(struct warr *x, struct warr *y) {
}

struct warr *foo() {
//CHECK: _Ptr<struct warr> foo(void) {
//CHECK_NOALL: _Ptr<struct warr> foo(void) {
//CHECK_ALL: _Ptr<struct warr> foo(void) _Checked {
struct warr *x = malloc(sizeof(struct warr));
//CHECK: _Ptr<struct warr> x = malloc<struct warr>(sizeof(struct warr));
struct warr *y = malloc(sizeof(struct warr));
Expand All @@ -141,7 +142,8 @@ struct warr *foo() {
}

struct warr *bar() {
//CHECK: _Ptr<struct warr> bar(void) {
//CHECK_NOALL: _Ptr<struct warr> bar(void) {
//CHECK_ALL: _Ptr<struct warr> bar(void) _Checked {
struct warr *x = malloc(sizeof(struct warr));
//CHECK: _Ptr<struct warr> x = malloc<struct warr>(sizeof(struct warr));
struct warr *y = malloc(sizeof(struct warr));
Expand Down
6 changes: 4 additions & 2 deletions clang/test/3C/arrinstructsafemulti1.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ struct warr *sus(struct warr *, struct warr *);
//CHECK: _Ptr<struct warr> sus(struct warr *x : itype(_Ptr<struct warr>), _Ptr<struct warr> y);

struct warr *foo() {
//CHECK: _Ptr<struct warr> foo(void) {
//CHECK_NOALL: _Ptr<struct warr> foo(void) {
//CHECK_ALL: _Ptr<struct warr> foo(void) _Checked {
struct warr *x = malloc(sizeof(struct warr));
//CHECK: _Ptr<struct warr> x = malloc<struct warr>(sizeof(struct warr));
struct warr *y = malloc(sizeof(struct warr));
Expand All @@ -132,7 +133,8 @@ struct warr *foo() {
}

struct warr *bar() {
//CHECK: _Ptr<struct warr> bar(void) {
//CHECK_NOALL: _Ptr<struct warr> bar(void) {
//CHECK_ALL: _Ptr<struct warr> bar(void) _Checked {
struct warr *x = malloc(sizeof(struct warr));
//CHECK: _Ptr<struct warr> x = malloc<struct warr>(sizeof(struct warr));
struct warr *y = malloc(sizeof(struct warr));
Expand Down
4 changes: 2 additions & 2 deletions clang/test/3C/arrofstructboth.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct general **sus(struct general *x, struct general *y) {

struct general **foo() {
//CHECK_NOALL: _Ptr<struct general *> foo(void) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> foo(void) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> foo(void) _Checked {
struct general *x = malloc(sizeof(struct general));
//CHECK: _Ptr<struct general> x = malloc<struct general>(sizeof(struct general));
struct general *y = malloc(sizeof(struct general));
Expand All @@ -161,7 +161,7 @@ struct general **foo() {

struct general **bar() {
//CHECK_NOALL: struct general **bar(void) : itype(_Ptr<struct general *>) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> bar(void) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> bar(void) _Checked {
struct general *x = malloc(sizeof(struct general));
//CHECK: _Ptr<struct general> x = malloc<struct general>(sizeof(struct general));
struct general *y = malloc(sizeof(struct general));
Expand Down
8 changes: 6 additions & 2 deletions clang/test/3C/arrofstructbothmulti1.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ struct general **sus(struct general *, struct general *);

struct general **foo() {
//CHECK_NOALL: _Ptr<struct general *> foo(void) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> foo(void) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> foo(void) _Checked {
struct general *x = malloc(sizeof(struct general));
//CHECK: _Ptr<struct general> x = malloc<struct general>(sizeof(struct general));
struct general *y = malloc(sizeof(struct general));
Expand All @@ -134,6 +134,8 @@ struct general **foo() {
//CHECK: _Ptr<struct general> curr = y;
int i;
for (i = 1; i < 5; i++, curr = curr->next) {
//CHECK_NOALL: for (i = 1; i < 5; i++, curr = curr->next) _Checked {
//CHECK_ALL: for (i = 1; i < 5; i++, curr = curr->next) {
curr->data = i;
curr->next = malloc(sizeof(struct general));
curr->next->data = i + 1;
Expand All @@ -146,7 +148,7 @@ struct general **foo() {

struct general **bar() {
//CHECK_NOALL: struct general **bar(void) : itype(_Ptr<struct general *>) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> bar(void) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> bar(void) _Checked {
struct general *x = malloc(sizeof(struct general));
//CHECK: _Ptr<struct general> x = malloc<struct general>(sizeof(struct general));
struct general *y = malloc(sizeof(struct general));
Expand All @@ -156,6 +158,8 @@ struct general **bar() {
//CHECK: _Ptr<struct general> curr = y;
int i;
for (i = 1; i < 5; i++, curr = curr->next) {
//CHECK_NOALL: for (i = 1; i < 5; i++, curr = curr->next) _Checked {
//CHECK_ALL: for (i = 1; i < 5; i++, curr = curr->next) {
curr->data = i;
curr->next = malloc(sizeof(struct general));
curr->next->data = i + 1;
Expand Down
4 changes: 2 additions & 2 deletions clang/test/3C/arrofstructcallee.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct general **sus(struct general *x, struct general *y) {

struct general **foo() {
//CHECK_NOALL: _Ptr<struct general *> foo(void) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> foo(void) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> foo(void) _Checked {
struct general *x = malloc(sizeof(struct general));
//CHECK: _Ptr<struct general> x = malloc<struct general>(sizeof(struct general));
struct general *y = malloc(sizeof(struct general));
Expand All @@ -161,7 +161,7 @@ struct general **foo() {

struct general **bar() {
//CHECK_NOALL: _Ptr<struct general *> bar(void) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> bar(void) {
//CHECK_ALL: _Array_ptr<_Ptr<struct general>> bar(void) _Checked {
struct general *x = malloc(sizeof(struct general));
//CHECK: _Ptr<struct general> x = malloc<struct general>(sizeof(struct general));
struct general *y = malloc(sizeof(struct general));
Expand Down
Loading