-
Notifications
You must be signed in to change notification settings - Fork 8
/
EPEx.cpp
913 lines (802 loc) · 23.1 KB
/
EPEx.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
/*
* The Clang checker implementation of EPEx.
*/
#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>
using namespace clang;
using namespace ento;
/* placeholder value for wild-card parameter counts and bounds */
#define DONT_CARE -1
namespace {
/* 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 EPEx checker */
class EPEx : public Checker<check::PreCall, check::PostCall, check::EndFunction,
check::PreStmt<ReturnStmt>> {
struct FuncSpecs fSpecs; /* the error specifications */
/* the error logging functions */
mutable std::map<std::string, int> loggers;
/*
* 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;
/*
* Add a logging function.
* rest_line: the part of the line after the logging function marker.
* note that it may be changed to remove newlines
*/
void addLogger(char *rest_line);
/*
* 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:
/*
* Load the configuration file,
* and divert the output to a randomly-named logging file.
*/
EPEx();
/*
* Print a single message line, with this checker's prefix.
* str: the message to print
*/
void printMsg(std::string str) 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_LIST_WITH_PROGRAMSTATE(AnalyzedFuncs, SymState)
/*
* the suffix of the randomly-named output log,
* so that they can be identified and gathered
*/
#define LOG_SUFFIX ".e.log"
#define LOG_SUFFIX_LEN (strlen(LOG_SUFFIX) + 1)
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <time.h>
#define N_HEX_IN_BYTE 2
#define URANDOM_PATH "/dev/urandom"
/*
* Divert stderr to a randomly-named file.
*/
void divertToRandom()
{
int random_file = open(URANDOM_PATH, O_RDONLY);
size_t fname_size;
unsigned random_bytes;
time_t creation_time;
char *fname;
/* Try using urandom. If that fails, use random. */
if (random_file >= 0) {
read(random_file, &random_bytes, sizeof(random_bytes));
close(random_file);
} else {
srandom(time(NULL));
random_bytes = random() % INT_MAX;
}
/* Ensure uniqueness by using time, too. */
creation_time = time(NULL);
fname_size = (sizeof(random_bytes) + sizeof(creation_time)) *
N_HEX_IN_BYTE + 1 +
LOG_SUFFIX_LEN;
fname = (char *) alloca(fname_size);
snprintf(fname, fname_size, "%08x_%016lx" LOG_SUFFIX, random_bytes,
creation_time);
freopen(fname, "w", stderr);
}
void EPEx::addLogger(char *rest_line)
{
size_t real_end = strlen(rest_line) - 1;
size_t effective_end = real_end;
/* Remove the new line character at the end, if it's there. */
if (rest_line[real_end] == '\n') {
rest_line[real_end] = '\0';
real_end--;
}
if (effective_end > 0) {
loggers[rest_line] = 1;
}
}
/* the prefix for logger function lines */
#define LOGGER_MARKER '1'
/* 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 EPEx::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;
/* Process lines starting with the logger prefix. */
if (buf[0] == LOGGER_MARKER) {
addLogger(buf + 1);
return 0;
}
/* 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 = fSpecs.addSpec(func_name, nargs, lbound, lboundop,
ubound, uboundop, ret_type);
assert(success);
count++;
}
return count;
}
#define ERROR_SPEC_NAME "error_spec.txt"
EPEx::EPEx()
{
size_t count = 0;
FILE *fp = fopen(ERROR_SPEC_NAME, "r");
char path[PATH_MAX];
char buf[2048];
/* Find and parse specification. */
if (fp == NULL) {
/*
* No error spec in the current directory,
* look in the top-level directory.
*/
size_t found;
std::string cur_dir = std::string(getcwd(path, sizeof(path)));
if (((found=cur_dir.find("openssl-")) != std::string::npos) ||
((found=cur_dir.find("mbedtls-")) != std::string::npos) ||
((found=cur_dir.find("gnutls-")) != std::string::npos) ||
((found=cur_dir.find("wolfssl-")) != std::string::npos)) {
cur_dir.append("/");
if ((found = cur_dir.find("/", found+1)) !=
std::string::npos) {
fp = fopen((cur_dir.substr(0, found) +
"/" + ERROR_SPEC_NAME).c_str(), "r");
}
}
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 " +
std::string(getcwd(path, sizeof(path))) +
"/" + ERROR_SPEC_NAME << "\n";
llvm::errs() << "Loaded " << loggers.size() << " logging functions\n";
/* Redirect output. */
divertToRandom();
}
void EPEx::checkPostCall(const CallEvent &Call, CheckerContext &C) const
{
std::string last_err_call = "";
ProgramStateRef state = C.getState(), new_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();
/* 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;
}
/* Get the name of the called function. */
StringRef FName = id_info->getName();
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();
/* Check if the function's return value makes the path an error path. */
new_state = isError(&isErrorPath, FName, ret, ret_type, C, state, true,
(int) Call.getNumArgs());
if (isErrorPath >= MAYBE_ERROR) {
std::string line;
if (new_state == NULL) {
new_state = state;
}
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()));
}
if (new_state != NULL) {
C.addTransition(new_state);
}
}
bool EPEx::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 EPEx::checkPreCall(const CallEvent &Call, CheckerContext &C) const
{
if (Call.getCalleeIdentifier() == NULL) {
return;
}
StringRef name = Call.getCalleeIdentifier()->getName();
bool custom_loggers = loggers.size() != 0;
bool logging = custom_loggers ? loggers.find(name) != loggers.end() :
name == "_gnutls_asn2err";
/* Check logging. */
if (logging) {
const clang::Decl *
DC = C.getCurrentAnalysisDeclContext()->getDecl();
std::string
s = DC->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() != s) {
return;
}
printMsg(sstate.getErrorFuncNames() + " " + loc + " "+ s +
" Return=error");
state = state->remove<AnalyzedFuncs>();
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";
}
printMsg(sstate.getErrorFuncNames() + " " + loc+ " " +
caller + " Return=" + return_status);
state = state->remove<AnalyzedFuncs>();
C.addTransition(state);
}
}
void EPEx::printMsg(std::string str) const {
llvm::errs() << "EPEx: " << str << "\n";
}
void EPEx::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;
}
if (status.length() > 0) {
printMsg(sstate.getErrorFuncNames() + " " + loc +
" " + caller + " Return=" + status);
}
need_printing = false;
}
cleanup:
state = state->remove<AnalyzedFuncs>();
C.addTransition(state);
}
void EPEx::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
EPEx::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. */
}
} else {
/* Check type of error spec only if we need to. */
if (care_binary) {
if (ret_type->isBooleanType()) {
if (FES->ret_type != FuncErrSpec::BOOL_TYPE) {
return NULL;
}
} else if (ret_type->isPointerType()) {
if (FES->ret_type != FuncErrSpec::PTR_TYPE) {
return NULL;
}
}
}
/* Check that we can still handle the type. */
if (!(ret_type->isBooleanType() || ret_type->isPointerType())) {
return NULL;
}
if (!ret.getAs<DefinedOrUnknownSVal>()) {
return NULL;
} else {
/* 0 (NULL or false) is error. */
std::tie(noerror, error) =
state->assume(ret.castAs<DefinedOrUnknownSVal>());
if (error && !noerror) {
isErrorPath = SURE_ERROR;
} else if (!error && noerror) {
isErrorPath = NOT_ERROR;
} else {
isErrorPath = MAYBE_ERROR;
/* Force the error path. */
state = error;
}
}
}
*isErrorPathOut = isErrorPath;
if (state == old_state) {
return NULL;
}
return state;
}
/*
* Perform standard Clang checker registration.
*/
void ento::registerEPEx(CheckerManager &mgr)
{
mgr.registerChecker<EPEx>();
}