forked from yuchi1989/ErrDoc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ErrDocErrPath.cpp
1049 lines (900 loc) · 26.5 KB
/
ErrDocErrPath.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <vector>
using namespace clang;
using namespace ento;
using namespace std;
#define DONT_CARE -1
#define ERROR_SPEC_NAME "ERR_SPEC_FILE"
namespace {
/* CallRef structure stores each function call information. */
class CallRef: public llvm::FoldingSetNode {
public:
CallRef(string name):func_name(name),func_depth(0){}
CallRef(string name, unsigned depth):func_name(name),func_depth(depth){}
string func_name;
unsigned func_depth;
bool operator==(const CallRef &X) const
{
return func_name==X.func_name&&func_depth==X.func_depth;
}
bool operator<(const CallRef &X) const
{
if(func_name!=X.func_name){
return func_name < X.func_name;
}
else{
return func_depth < X.func_depth;
}
}
void Profile(llvm::FoldingSetNodeID &ID) const
{
ID.AddString(func_name);
ID.AddInteger(func_depth);
}
};
/* PathState structure stores path information for each CallRef */
struct PathState {
private:
/* the name of the caller function */
std::string func_name;
unsigned func_depth;
bool error_path;
bool path_valid;
public:
vector<string> path;
PathState(std::string InFuncName, unsigned depth) : func_name(InFuncName),func_depth(depth),
error_path(false),path_valid(true){
}
PathState(std::string InFuncName) : func_name(InFuncName),func_depth(0),
error_path(false),path_valid(true){
}
PathState(const PathState *ps)
{
vector<string> p = ps->path;
for(size_t i = 0;i<p.size();i++){
path.push_back(p[i]);
}
error_path = ps->isErrorPath();
func_name = ps->getFuncName();
func_depth = ps->getDepth();
path_valid = ps->isValid();
}
PathState(const PathState& ps)
{
vector<string> p = ps.path;
for(size_t i = 0;i<p.size();i++){
path.push_back(p[i]);
}
error_path = ps.isErrorPath();
func_name = ps.getFuncName();
func_depth = ps.getDepth();
path_valid = ps.isValid();
}
std::string getFuncName() const
{
return func_name;
}
unsigned getDepth() const
{
return func_depth;
}
bool isErrorPath() const
{
return error_path;
}
void setError(){
error_path = true;
}
bool isValid() const{
return path_valid;
}
void setInvalid(){
path_valid = false;
}
bool operator==(const PathState &X) const
{
return func_name==X.getFuncName();
}
void Profile(llvm::FoldingSetNodeID &ID) const
{
ID.AddString(func_name);
ID.AddInteger(func_depth);
ID.AddBoolean(error_path);
ID.AddBoolean(path_valid);
for(size_t i = 0;i<path.size();i++){
ID.AddString(path[i]);
}
}
};
/* the state of the explored path */
struct SymState {
private:
/* the name of the caller function */
std::string func_name;
/* the sequence of fallible functions encountered on the path */
std::string error_func_names;
/* the most recent, fallible function in the path */
std::string lst_error_func_name;
public:
/*
* InFuncName: the name of the caller function
* InErrFuncName: the sequence of fallible functions
* encountered on the path
* InLstErrName: the most recent, fallible function in the path
*/
SymState(std::string InFuncName, std::string InErrFuncNames,
std::string InLstErrName) : func_name(InFuncName),
error_func_names(InErrFuncNames),
lst_error_func_name(InLstErrName)
{
}
std::string getFuncName() const
{
return func_name;
}
std::string getErrorFuncNames() const
{
return error_func_names;
}
std::string getLstErrFuncName() const
{
return lst_error_func_name;
}
bool operator==(const SymState &X) const
{
return (func_name == X.func_name) &&
(error_func_names == X.error_func_names) &&
(lst_error_func_name == X.lst_error_func_name);
}
void Profile(llvm::FoldingSetNodeID &ID) const
{
ID.AddString(func_name);
ID.AddString(error_func_names);
ID.AddString(lst_error_func_name);
}
};
/* the error specification for a function, or all callers */
struct FuncErrSpec {
/* the name of the function */
std::string func_name;
/* the number of parameters in the function signature */
int nparameters;
/*
* the position of the first bound,
* which is usually the lower bound when both are used
*/
int err_lbound;
/* the comparator for the first, or lower bound */
int err_lbound_op;
/*
* the position of the second bound,
* which is usually the upper bound when both are used
*/
int err_ubound;
/* the comparator for the second, or upper bound */
int err_ubound_op;
/* one of the supported return types */
enum ReturnType {
PTR_TYPE, /* a NULL or non-NULL pointer */
INT_TYPE, /* ranges of integers */
BOOL_TYPE /* the C++ bool type */
} ret_type;
};
/* lookup table for functions' error specifications, mapped by the name */
struct FuncSpecs {
/* the lookup data structure */
mutable std::map<std::string, FuncErrSpec> specs_map;
public:
/*
* Fetch the error specification for the given function.
* fname: the name of the function
* whose error specification is wanted
* returns the error specification struct of the desired function
* if it exists,
* NULL otherwise
*/
FuncErrSpec *findSpec(StringRef fname) const
{
if (specs_map.count(fname) > 0) {
return (&specs_map[fname]);
} else {
return NULL;
}
}
/*
* Add the error specification for a function.
* name: the name of the function
* np: the number of parameters in the function
* lb: the position of the first, or lower bound
* lob: the comparator of the first, or lower bound
* ub: the position of the second, or upper bound
* uob: the comparator of the second, or upper bound
* ret: the return value type
*/
bool addSpec(std::string name, unsigned int np, int lb, int lop,
int ub, int uop, FuncErrSpec::ReturnType ret) const
{
FuncErrSpec fes;
fes.func_name = name;
fes.nparameters = np;
fes.err_lbound = lb;
fes.err_lbound_op = lop;
fes.err_ubound = ub;
fes.err_ubound_op = uop;
fes.ret_type = ret;
specs_map[name] = fes;
return true;
}
};
/* the error status of a path */
enum IsError {
NOT_ERROR = -1, /* definitely no error */
MAYBE_ERROR = 0, /* possibly an error */
SURE_ERROR = 1 /* definitely an error */
};
/* the ErrDocErrPath checker */
class ErrDocErrPath : public Checker<check::PreCall, check::PostCall, check::EndFunction,
check::PreStmt<ReturnStmt>>
{
struct FuncSpecs fSpecs; /* the error specifications */
/*
* Try to get the exact, integer value of an SVal.
* val: the SVal whose integer we want to extract
* ret: will store the integer value, if it exists
* returns true iff this function stores an exact value in ret
*/
bool getConcreteValue(SVal val, int64_t *ret) const;
/*
* Make a single parsing pass, given a new line from the file
* buf: newly-read line from the specification file
* returns the number of error specifications parsed
*/
size_t parseOnce(char *buf);
public:
ErrDocErrPath();
/*
* Print a single message line, with this checker's prefix.
* str: the message to print
*/
void printMsg(std::string str) const;
std::string getCaller(CheckerContext &C) const;
unsigned getStackDepth(CheckerContext &C) const;
/*
* Check if the path has become an error path
* after calling a fallible function.
*/
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
/*
* Check for exits and logging.
*/
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
/*
* Check for error propagation.
*/
void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
/*
* Perform sanity check for if there are still any unchecked states
* in the caller function.
*/
void checkEndFunction(CheckerContext &C) const;
/*
* Check if the function returned an error value.
* isErrorPathOut: the result output, which is NOT_ERROR by default
* name: the name of the function whose return value
* needs to be checked
* ret: the return value
* ret_type: the type of the return value
* C: the checker context
* old_state: the old checker state
* care_binary: for binary return types
* (NULL/non-NULL pointers and booleans),
* do we care about having a specification
* with the matching name and type?
* n_args: the number of arguments to the function
* returns the new state, if it changed, or NULL
*/
ProgramStateRef isError(enum IsError *isErrorPathOut, StringRef name,
SVal ret, QualType ret_type, CheckerContext &C,
ProgramStateRef old_state, bool care_binary,
int n_args) const;
};
} // end anonymous namespace
/* stack of error states of the path */
REGISTER_MAP_WITH_PROGRAMSTATE(PathMap, CallRef, PathState)
REGISTER_LIST_WITH_PROGRAMSTATE(AnalyzedFuncs, SymState)
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <time.h>
/* delimiters between entries in the error spec lines */
static const char delimiters[] = ", \t";
/*
* Parse bound information.
* saveptr: the parsing state
* bound: the bound position, which is not changed if it is not available
* boundop: the comparator, which is not changed if it is not available
*/
static void parseBound(char **saveptr, int *bound, int *boundop)
{
char *tok;
/* Parse the bound position. */
tok = strtok_r(NULL, delimiters, saveptr);
if (tok != NULL) {
*bound = atoi(tok);
}
/* Parse the comparator. */
tok = strtok_r(NULL, delimiters, saveptr);
if (tok != NULL) {
if (tok[0] == 'G' && tok[1] == 'T') {
*boundop = BO_GT;
} else if (tok[0] == 'G' && tok[1] == 'E') {
*boundop = BO_GE;
} else if (tok[0] == 'L' && tok[1] == 'T') {
*boundop = BO_LT;
} else if (tok[0] == 'L' && tok[1] == 'E') {
*boundop = BO_LE;
} else if (tok[0] == 'E' && tok[1] == 'Q') {
*boundop = BO_EQ;
} else if (tok[0] == 'N' && tok[1] == 'E') {
*boundop = BO_NE;
}
}
}
size_t ErrDocErrPath::parseOnce(char *buf)
{
size_t count = 0;
char *tok = NULL;
char *func_name = NULL;
int nargs = DONT_CARE, lbound = DONT_CARE, ubound = DONT_CARE;
int lboundop = DONT_CARE, uboundop = DONT_CARE;
FuncErrSpec::ReturnType ret_type = FuncErrSpec::INT_TYPE;
char *saveptr;
/* Get the name. */
tok = strtok_r(buf, delimiters, &saveptr);
if (tok != NULL) {
func_name = tok;
}
/* Get the number of parameters. */
tok = strtok_r(NULL, delimiters, &saveptr);
if (tok != NULL) {
nargs = atoi(tok);
}
/* Get the two bounds. */
parseBound(&saveptr, &lbound, &lboundop);
parseBound(&saveptr, &ubound, &uboundop);
/* Get the function return type. */
tok = strtok_r(NULL, delimiters, &saveptr);
if (tok != NULL) {
switch(tok[0]) {
case 'I':
case 'i':
ret_type = FuncErrSpec::INT_TYPE;
break;
case 'B':
case 'b':
ret_type = FuncErrSpec::BOOL_TYPE;
break;
case 'P':
case 'p':
ret_type = FuncErrSpec::PTR_TYPE;
break;
default:
break;
}
}
/* Count the function, if it is valid. */
if ((func_name) && (func_name[0] != '\n')) {
bool success = false;
success = fSpecs.addSpec(func_name, nargs, lbound, lboundop,
ubound, uboundop, ret_type);
assert(success);
count++;
}
return count;
}
ErrDocErrPath::ErrDocErrPath()
{
size_t count = 0;
FILE *fp = fopen(ERROR_SPEC_NAME, "r");
char buf[2048];
/* Find and parse specification. */
if (fp == NULL) {
printMsg("ERROR: failed to "
"open error spec file " ERROR_SPEC_NAME ", "
"exiting..");
exit(1);
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
count += parseOnce(buf);
}
fclose(fp);
llvm::errs() << "Loaded " + std::to_string(count) +
" error specs from " + ERROR_SPEC_NAME << "\n";
count = 0;
}
std::string ErrDocErrPath::getCaller(CheckerContext &C) const
{
const clang::Decl *DC = C.getCurrentAnalysisDeclContext()->getDecl();
return DC->getAsFunction()->getNameInfo().getAsString();
}
unsigned ErrDocErrPath::getStackDepth(CheckerContext &C) const
{
unsigned stack_depth = 0;
for (const LocationContext *LCtx = C.getLocationContext(); LCtx;
LCtx = LCtx->getParent()) {
if (LCtx->getKind() ==
LocationContext::ContextKind::StackFrame) {
stack_depth++;
}
}
return stack_depth;
}
void ErrDocErrPath::checkPostCall(const CallEvent &Call, CheckerContext &C) const
{
std::string last_err_call = "";
ProgramStateRef state = C.getState(), new_state, error_state, noerror_state;
SVal ret = Call.getReturnValue();
const IdentifierInfo *id_info = Call.getCalleeIdentifier();
if (!id_info) {
return;
}
const clang::Decl *DC = C.getCurrentAnalysisDeclContext()->getDecl();
std::string caller = DC->getAsFunction()->getNameInfo().getAsString();
/* print the error path for the specific post call function */
if(state){
StringRef name = Call.getCalleeIdentifier()->getName();
unsigned depth = getStackDepth(C);
const PathState *ps = state->get<PathMap>(CallRef(name.str(),depth));
if(ps!=nullptr){
if(ps->isErrorPath() && ps->isValid()){
if(ps->path.size()>0){
printMsg("Error Path in function " + Call.getCalleeIdentifier()->getName().str());
const FunctionDecl *func = static_cast<const FunctionDecl*>(Call.getRuntimeDefinition().getDecl());
if (func){
clang::FullSourceLoc fullLoc(func->getNameInfo().getBeginLoc(), C.getSourceManager());
std::string fileName = C.getSourceManager().getFilename(fullLoc);
//printMsg("name: " + func->getQualifiedNameAsString());
printMsg("C: " + Call.getCalleeIdentifier()->getName().str());
printMsg("File: " + fileName);
}
for(size_t i = 0; i<ps->path.size();i++ ){
printMsg("P: " + ps->path[i]);
}
}
}
state = state->remove<PathMap>(CallRef(name.str(),depth));
}
}
/* Get the name of the called function. */
StringRef FName = id_info->getName();
/*
* We don't track function calls inside a func not returning anything.
* But if the result type of the target function is void, we assume that the target returns no error.
*/
/* Make sure to return error for multiple calls to the same function. */
AnalyzedFuncsTy funcs = state->get<AnalyzedFuncs>();
if (!funcs.isEmpty()) {
const SymState sstate = funcs.getHead();
if (sstate.getLstErrFuncName() != id_info->getName()) {
return;
}
last_err_call = sstate.getErrorFuncNames();
}
QualType ResultType = DC->getAsFunction()->getReturnType();
/*
* We don't track function calls inside a func not returning anything.
*/
if (ResultType->isVoidType()) {
return;
}
enum IsError isErrorPath;
std::string loc = "";
if (const Expr *call_expr = Call.getOriginExpr()) {
loc = call_expr->getExprLoc()
.printToString(C.getSourceManager());
}
QualType ret_type = Call.getResultType();
/* if the function can be found from the error specification file, make this function return error. */
error_state = isError(&isErrorPath, FName, ret, ret_type, C, state, true,
(int) Call.getNumArgs());
new_state = error_state;
if (isErrorPath >= MAYBE_ERROR) {
std::string line;
if (new_state == NULL) {
new_state = state;
}
else{
}
if (last_err_call == "") {
line = loc + " " + FName.str();
} else {
new_state = new_state->remove<AnalyzedFuncs>();
line = loc + last_err_call;
}
new_state =
new_state->add<AnalyzedFuncs>(SymState(caller, line,
FName.str()));
StringRef name = Call.getCalleeIdentifier()->getName();
unsigned depth = getStackDepth(C);
string Callername = getCaller(C);
if(!Callername.empty()){
const PathState *ps = new_state->get<PathMap>(CallRef(Callername,depth-1));
if(ps!=nullptr){
PathState ps2 = PathState(ps);
ps2.setError();
// add the function that returns error to the error path
ps2.path.push_back(name.str()+",target(function that returns error)");
new_state = new_state->set<PathMap>(CallRef(Callername,depth-1), ps2);
C.addTransition(new_state);
}
}
if (new_state != NULL) {
C.addTransition(new_state);
}
}
if (new_state != NULL) {
C.addTransition(new_state);
}
}
bool ErrDocErrPath::getConcreteValue(SVal val, int64_t *ret) const
{
Optional<loc::ConcreteInt> LV = val.getAs<loc::ConcreteInt>();
Optional<nonloc::ConcreteInt> NV = val.getAs<nonloc::ConcreteInt>();
if (LV) {
*ret = LV->getValue().getExtValue();
return true;
}
if (NV) {
*ret = NV->getValue().getExtValue();
return true;
}
return false;
}
void ErrDocErrPath::checkPreCall(const CallEvent &Call, CheckerContext &C) const
{
if (Call.getCalleeIdentifier() == NULL) {
return;
}
/* Add a function call to the currernt path */
StringRef name = Call.getCalleeIdentifier()->getName();
ProgramStateRef State = C.getState();
if(State){
unsigned depth = getStackDepth(C);
State = State->set<PathMap>(CallRef(name.str(),depth), PathState(name.str()));
string Callername = getCaller(C);
if(!Callername.empty()){
const PathState *ps = State->get<PathMap>(CallRef(Callername,depth-1));
if(ps!=nullptr){
PathState ps2 = PathState(ps);
ps2.path.push_back(name.str());
State = State->set<PathMap>(CallRef(Callername,depth-1), ps2);
}
}
C.addTransition(State);
}
/* Check exit and that its parameter is an error value. */
if ((name == "exit") || (name == "_Exit") || (name == "_exit")) {
const clang::Decl *
caller_decl = C.getCurrentAnalysisDeclContext()->getDecl();
std::string
caller = caller_decl->getAsFunction()
->getNameInfo().getAsString();
ProgramStateRef state = C.getState();
std::string loc = "";
if (const Expr *call_expr = Call.getOriginExpr()) {
loc = call_expr->getExprLoc()
.printToString(C.getSourceManager());
}
AnalyzedFuncsTy funcs = state->get<AnalyzedFuncs>();
if (funcs.isEmpty()) {
return;
}
const SymState sstate = funcs.getHead();
if (sstate.getFuncName() != caller) {
return;
}
int64_t ret;
std::string return_status;
if (getConcreteValue(Call.getArgSVal(0), &ret)) {
if (ret == 0) {
return_status = "noerror";
} else {
return_status = "error";
}
} else {
return_status = "noerror_or_error";
}
if(state){
unsigned depth = getStackDepth(C);
//state = state->set<PathMap>(CallRef(name.str(),depth), PathState(name.str()));
string Callername = getCaller(C);
if(!Callername.empty()){
const PathState *ps = state->get<PathMap>(CallRef(Callername,depth-1));
if(ps!=nullptr){
PathState ps2 = PathState(ps);
ps2.path.push_back(sstate.getErrorFuncNames() + " " + loc+ " " + caller + " Return=" + return_status);
state = state->set<PathMap>(CallRef(Callername,depth-1), ps2);
}
}
}
/*
printMsg(sstate.getErrorFuncNames() + " " + loc+ " " +
caller + " Return=" + return_status);
*/
state = state->remove<AnalyzedFuncs>();
C.addTransition(state);
}
}
void ErrDocErrPath::printMsg(std::string str) const {
llvm::errs() << "ErrDoc: " << str << "\n";
}
void ErrDocErrPath::checkPreStmt(const ReturnStmt *ret_stmt, CheckerContext &C) const
{
const Expr *ret_expr = ret_stmt->getRetValue();
ProgramStateRef state = C.getState(), new_state;
bool need_printing = true;
std::string loc;
SVal ret_val;
QualType ret_type;
const CallExpr *CE;
const Decl *calleeDecl;
const FunctionDecl *function;
enum IsError isErrorPath;
const clang::Decl *DC = C.getCurrentAnalysisDeclContext()->getDecl();
std::string caller = DC->getAsFunction()->getNameInfo().getAsString();
AnalyzedFuncsTy analyzed_funcs = state->get<AnalyzedFuncs>();
if (analyzed_funcs.isEmpty()) {
return;
}
const SymState sstate = analyzed_funcs.getHead();
if (sstate.getFuncName() != caller) {
return;
}
if (!ret_expr) {
goto cleanup;
}
ret_val = C.getState()->getSVal(ret_expr, C.getLocationContext());
loc = ret_expr->getExprLoc().printToString(C.getSourceManager());
/* Ignore wrapper functions */
CE = dyn_cast<CallExpr>(ret_expr->IgnoreParens());
if (CE && (calleeDecl = CE->getCalleeDecl()) &&
(function = calleeDecl->getAsFunction())) {
std::string name = function->getNameInfo().getAsString();
if (sstate.getErrorFuncNames().find(" " + name) !=
std::string::npos) {
goto cleanup;
}
}
/*
* Check integers according to the global specification,
* and for the binary types, the value corresponding to 0
* is considered error, even if there is no error specification
* for the caller.
*/
ret_type = DC->getAsFunction()->getReturnType();
new_state = isError(&isErrorPath, "__RETURN_VAL__", ret_val, ret_type,
C, state, false, DONT_CARE);
if (new_state != NULL) {
state = new_state;
}
/* Check for NULL pointer derefences. */
if (ret_type->isPointerType()) {
DefinedOrUnknownSVal
location = ret_val.castAs<DefinedOrUnknownSVal>();
if (!location.getAs<Loc>()) {
printMsg(loc + " " + caller +
" return=NULL_pointer_dereference");
need_printing = false;
}
}
/* Print the error handling status. */
if (need_printing) {
std::string status = "";
switch (isErrorPath) {
case NOT_ERROR:
status = "noerror";
break;
case MAYBE_ERROR:
status = "noerror_or_error";
break;
case SURE_ERROR:
status = "error";
break;
default:
break;
}
/* add return information to the current path*/
if(state){
unsigned depth = getStackDepth(C);
//state = state->set<PathMap>(CallRef(name.str(),depth), PathState(name.str()));
string Callername = getCaller(C);
if(!Callername.empty()){
const PathState *ps = state->get<PathMap>(CallRef(Callername,depth-1));
if(ps!=nullptr){
PathState ps2 = PathState(ps);
if (status.length() > 0) {
ps2.path.push_back(sstate.getErrorFuncNames() + " " + loc+ " " + caller + " Return=" + status);
}
state = state->set<PathMap>(CallRef(Callername,depth-1), ps2);
}
}
//C.addTransition(state);
}
/*if (status.length() > 0) {
printMsg(sstate.getErrorFuncNames() + " " + loc +
" " + caller + " Return=" + status);
}*/
need_printing = false;
}
cleanup:
state = state->remove<AnalyzedFuncs>();
C.addTransition(state);
}
void ErrDocErrPath::checkEndFunction(CheckerContext &C) const
{
const clang::Decl *DC = C.getCurrentAnalysisDeclContext()->getDecl();
std::string caller = DC->getAsFunction()->getNameInfo().getAsString();
ProgramStateRef state = C.getState();
AnalyzedFuncsTy funcs = state->get<AnalyzedFuncs>();
if (funcs.isEmpty()) {
return;
}
const SymState sstate = funcs.getHead();
if (sstate.getFuncName() != caller) {
return;
}
printMsg("!!You should not be here..returning from function: " +
caller);
state = state->remove<AnalyzedFuncs>();
C.addTransition(state);
}
ProgramStateRef
ErrDocErrPath::isError(enum IsError *isErrorPathOut, StringRef name, SVal ret,
QualType ret_type, CheckerContext &C, ProgramStateRef old_state,
bool care_binary, int n_args) const
{
ProgramStateRef state = old_state;
ConstraintManager &CM = C.getConstraintManager();
enum IsError isErrorPath;
ProgramStateRef error, noerror;
FuncErrSpec *FES = fSpecs.findSpec(name);
SVal lbound, ubound, tVal;
*isErrorPathOut = NOT_ERROR;
if (FES == NULL) {
return NULL;
}
/* Just to be safe */
if ((FES->nparameters != DONT_CARE) && (n_args != DONT_CARE) &&
(n_args != FES->nparameters)) {
return NULL;
}
/* For integer, use specific error specification. */
if (ret_type->isIntegerType()) {
SValBuilder &SVB = C.getSValBuilder();
Optional<DefinedSVal> TV;
if (FES->ret_type != FuncErrSpec::INT_TYPE) {
return NULL;
}
if (!ret.getAs<NonLoc>()) {
return NULL;
}
/* Check first bound. */
if (FES->err_lbound_op != DONT_CARE) {
lbound = SVB.makeIntVal(FES->err_lbound, ret_type);
tVal = SVB.evalBinOpNN(state, (BinaryOperator::Opcode)
FES->err_lbound_op,
ret.castAs<NonLoc>(),
lbound.castAs<NonLoc>(),
ret_type);
TV = tVal.getAs<DefinedSVal>();
std::tie(error, noerror) = CM.assumeDual(state, *TV);
if (!error && noerror) {
isErrorPath = NOT_ERROR;
} else if (error && !noerror) {
isErrorPath = SURE_ERROR;
} else {
/* Force error path. */
isErrorPath = MAYBE_ERROR;
state = error;
}
}
/*
* Check second bound
* if there is still a chance of being an error.
*/
if ((isErrorPath >= MAYBE_ERROR) &&
(FES->err_ubound_op != DONT_CARE)) {
ubound = SVB.makeIntVal(FES->err_ubound, ret_type);
tVal = SVB.evalBinOpNN(state, (BinaryOperator::Opcode)
FES->err_ubound_op,
ret.castAs<NonLoc>(),
ubound.castAs<NonLoc>(),
ret_type);
TV = tVal.getAs<DefinedSVal>();
std::tie(error, noerror) = CM.assumeDual(state, *TV);
if (!error && noerror) {
isErrorPath = NOT_ERROR;
} else if (error && noerror) {
isErrorPath = MAYBE_ERROR;
/*
* Bad hack to avoid a Clang bug:
* handle the overflow case
*/
if (FES->err_ubound < 0) {
ProgramStateRef sane, insane;
SVal one;
one = SVB.makeIntVal(1, ret_type);
tVal =
SVB.evalBinOpNN(state, BO_LT,
ret.castAs<NonLoc>(),
one.castAs<NonLoc>(),
ret_type);
TV = tVal.getAs<DefinedSVal>();
std::tie(sane, insane) =
CM.assumeDual(state, *TV);
if (!sane || insane) {
isErrorPath = NOT_ERROR;
}
}
if (isErrorPath == MAYBE_ERROR) {
/* Force the error path. */
state = error;
}
}
/* No change if second bound must be true. */
}