-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAST_Traversals.c
661 lines (590 loc) · 19.1 KB
/
AST_Traversals.c
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
/*
Stacy - A Static Code Analyzer
This file performs the core functionality of our tool.
Input source code to be analyzed is entered into the plug-in.
The output of the parser is used to form an Abstract Syntax Tree (AST) representation of code.
Core functions are carried out during a depth-first traversal of the AST.
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "graph.h"
#include "y.tab.h"
int stacktop=0;
int num_of_nodes=0;
int var_array_count = 0;
int init_array_count=0;
int var_array_stack_count=0;
int init_array_stack_count=0;
int init_var_used_stack_count=0;
int malloc_array_count=0;
int allocation_count=0;
int init_var_used_count=0;
int line_number = 1;
int final_line_count=0;
int first_print=0;
int prev_line = 0;
int mem_path_array_count=0;
int mem_freed_array_count=0;
int first_buff_print=0;
int first_mem_print=0;
//Create new node of the AST
void createNode(char *symbols, int type_of){
struct node *newNode,*switchNode;
int loopCounter=0;
newNode = (struct node*) malloc (sizeof(struct node));
/*Initialize new node*/
newNode->symbol = malloc(sizeof(char *)*10);
newNode->symbolCount = 0;
newNode->symbol[newNode->symbolCount++] = strdup(symbols);
newNode->type = type_of;
newNode->line = line_number;
newNode->index = malloc(sizeof(char)*10);
newNode->indexCount = 0;
final_line_count = line_number;
//Outward pointers of node
for(loopCounter=0;loopCounter<10;loopCounter++){
newNode->next[loopCounter] = NULL;
}
loopCounter=0;
//New node is the start node
if(startNode == NULL){
startNode = newNode;
}
else
//Encountered break in source code
if((strcmp(newNode->symbol[0],"break")==0)){
currentNode->next[0] = NULL;
}
else{
if((strcmp(newNode->symbol[0],"malloc")==0)){
currentNode->type = 9;
}
//Switch case
if(((strcmp(newNode->symbol[0],"case")==0)||(strcmp(newNode->symbol[0],"default")==0))&&(currentNode->type!=switch_node)){
if((strcmp(currentNode->symbol[0],"break")!=0)){
loopCounter=0;
}
switchNode = (struct node*) malloc (sizeof(struct node));
switchNode = stack[stacktop-1];
loopCounter=0;
while(switchNode->next[loopCounter]!=NULL){
loopCounter++;
}
switchNode->next[loopCounter] = newNode;
}
loopCounter=0;
while(currentNode->next[loopCounter]!=NULL)
loopCounter++;
currentNode->next[loopCounter] = malloc(sizeof(struct node*));
currentNode->next[loopCounter] = newNode;
}
num_of_nodes++;
currentNode = newNode;
//Initialization complete
}
//Push onto stack
void push(struct node *item){
stack[stacktop++] = item;
}
//Pop from stack
struct node* pop(){
return stack[--stacktop];
}
/*
Depth-first traversal of AST for detecting usage of uninitialized variables
Input argument: Each node of the AST
*/
int traverse_graph_for_init_var(struct node *graph_node){
int loopCounter;
int type,next_count=0,interim_count=0,check_for_common=0,common_array[10],common_array_count=0;
type = graph_node->type;
/*Identify type of input node*/
//If switch case is encountered
if(graph_node->type==switch_node){
push_var_array_stack(var_array_count);
push_init_array_stack(init_array_count);
push_init_var_used_stack(init_var_used_count);
}
//If node is encountered
if(graph_node->type==if_node){
push_var_array_stack(var_array_count);
push_init_array_stack(init_array_count);
push_init_var_used_stack(init_var_used_count);
}
//For loop is encountered
if(graph_node->type==for_node){
push_var_array_stack(var_array_count);
push_init_array_stack(init_array_count);
push_init_var_used_stack(init_var_used_count);
}
//While node is encountered
if(graph_node->type==while_node){
push_var_array_stack(var_array_count);
push_init_array_stack(init_array_count);
push_init_var_used_stack(init_var_used_count);
}
//Function parameter node
if(graph_node->type==function_parameter){
var_array_add(graph_node);
init_array[init_array_count] = var_array_count-1;
init_array_count++;
}
//Declaration node
if(graph_node->type==declaration){
var_array_add(graph_node);
}
//Node is the Right Hand Side (RHS) of an expression
if(graph_node->type==rhs){
int valid = check_rhs_validity(graph_node);
}
//Node is the Left Hand Side (LHS) of an expression
if(graph_node->type==lhs){
check_lhs_for_init_var(graph_node);
}
//End of scope
if((graph_node->next[0]==NULL)){
return 1;
}
//Recursive call
traverse_graph_for_init_var(graph_node->next[next_count++]);
//In case of branch at current node
while(graph_node->next[next_count+1]!=NULL){
interim_count = init_array_count;
check_for_common = init_array_stack[init_array_stack_count-1];
traverse_graph_for_init_var(graph_node->next[next_count++]);
}
//At final path of current node
if(graph_node->next[next_count]!=NULL){
int loopCounter,innerLoopCounter;
//Check for variables that have been initialized in all possible paths
for(loopCounter=check_for_common;loopCounter<interim_count;loopCounter++){
for(innerLoopCounter=interim_count;innerLoopCounter<init_array_count;innerLoopCounter++){
if(init_array[loopCounter]==init_array[innerLoopCounter]){
common_array[common_array_count++] = init_array[loopCounter];
}
}
}
//Return to previous state
var_array_count = pop_var_array_stack();
init_array_count = pop_init_array_stack();
init_var_used_count = pop_init_var_used_stack();
//If variables initialized in all possible paths
if(common_array_count>0){
for(loopCounter=0;loopCounter<common_array_count;loopCounter++){
init_array[init_array_count++] = common_array[loopCounter];
}
}
traverse_graph_for_init_var(graph_node->next[next_count++]);
}
}
/*
Depth-first traversal of AST for detecting presence of potential memory leaks
Input argument: Each node of the AST
*/
int traverse_graph_for_mem_leaks(struct node *graph_node){
int loopCounter;
int type,next_count=0,interim_count=0,check_for_common=0,common_array[10],common_array_count=0;
type = graph_node->type;
//Input node allocates memory dynamically
if(graph_node->type==malloc_node){
int loopCounter;
//Update the array that tracks memory allocations
allocation_node[allocation_count] = malloc(sizeof(struct malloc));
allocation_node[allocation_count]->index = malloc_array_count++;
allocation_node[allocation_count]->points = malloc(sizeof(int)*10);
allocation_node[allocation_count]->count = 0;
allocation_node[allocation_count]->line = graph_node->line;
allocation_node[allocation_count]->free = 0;
for(loopCounter=var_array_count-1;loopCounter>=0;loopCounter--){
if(strcmp(graph_node->symbol[0],var_array[loopCounter])==0)
break;
}
allocation_node[allocation_count]->points[allocation_node[allocation_count]->count++] = loopCounter;
allocation_count++;
}
//Branch in AST
if(graph_node->type==if_node||graph_node->type==switch_node){
push_mem_path_array(mem_freed_array_count);
}
//Node is on LHS of an expression
if(graph_node->type==lhs){
check_lhs_for_mem_leaks(graph_node);
}
//Dynamically allocated memory is freed by the current node
if(strcmp(graph_node->symbol[0],"free")==0){
int index,loopCounter,innerLoopCounter,flag=0;
//To determine which variable is freed
for(loopCounter=0;loopCounter<allocation_count;loopCounter++){
for(innerLoopCounter=0;innerLoopCounter<allocation_node[loopCounter]->count;innerLoopCounter++){
if(strcmp((graph_node->next[0])->symbol[0],var_array[allocation_node[loopCounter]->points[innerLoopCounter]])==0){
flag=1;
break;
}
}
if(flag==1)
break;
}
add_mem_freed_array(allocation_node[loopCounter]->index);
}
if((graph_node->next[0]==NULL)){
return 1;
}
//Recursive call
traverse_graph_for_mem_leaks(graph_node->next[next_count++]);
//In case of branch at current node
while(graph_node->next[next_count+1]!=NULL){
int loopCounter;
interim_count = mem_freed_array_count;
check_for_common = mem_path_array[mem_path_array_count-1];
traverse_graph_for_mem_leaks(graph_node->next[next_count++]);
}
//At final path of current node
if(graph_node->next[next_count]!=NULL){
int loopCounter,innerLoopCounter;
//Check for dynamically allocated variables that have been freed at all possible paths
for(loopCounter=check_for_common;loopCounter<interim_count;loopCounter++){
for(innerLoopCounter=interim_count;innerLoopCounter<mem_freed_array_count;innerLoopCounter++){
if(mem_freed_array[loopCounter]==mem_freed_array[innerLoopCounter]){
common_array[common_array_count++] = mem_freed_array[loopCounter];
}
}
}
//Return to previous state
mem_freed_array_count = pop_mem_path_array();
if(common_array_count>0){
for(loopCounter=0;loopCounter<common_array_count;loopCounter++){
mem_freed_array[mem_freed_array_count++] = common_array[loopCounter];
}
}
traverse_graph_for_mem_leaks(graph_node->next[next_count++]);
}
}
/*
Depth-first traversal of AST for detecting presence of potential buffer overflows
Input argument: Each node of the AST
*/
int traverse_graph_for_buffer_overflow(struct node *graph_node){
int loopCounter;
int type,next_count=0,interim_count=0,check_for_common=0,common_array[10],common_array_count=0;
type = graph_node->type;
//Determine type of input node
if(graph_node->type==if_node||graph_node->type==ifelse_node||graph_node->type==while_node||graph_node->type==switch_node||graph_node->type==for_node){
int loopCounter;
struct safeArray *newSafeArray; //Array of "safe" variables
struct unsafeArray *newUnsafeArray; //Array of "unsafe" variables
//Initialize safe array
newSafeArray = (struct safeArray*) malloc (sizeof(struct safeArray));
newSafeArray->count = 0;
for(loopCounter=0;loopCounter<graph_node->symbolCount;loopCounter++){
newSafeArray->safe[newSafeArray->count++] = strdup(graph_node->symbol[loopCounter]);
}
newSafeArray->nextScope = NULL;
if(currentSafeArray==NULL){
newSafeArray->prevScope = NULL;
}
else{
newSafeArray->prevScope = currentSafeArray;
}
currentSafeArray = newSafeArray;
//Initialize unsafe array
newUnsafeArray = (struct unsafeArray*) malloc (sizeof(struct unsafeArray));
newUnsafeArray->nextScope = NULL;
newUnsafeArray->count = 0;
if(currentUnsafeArray==NULL){
newUnsafeArray->prevScope = NULL;
}
else{
newUnsafeArray->prevScope = currentUnsafeArray;
}
currentUnsafeArray = newUnsafeArray;
}
//Declared but uninitialized variables are added to unsafe array
if(graph_node->type==declaration){
if(currentUnsafeArray==NULL){
struct unsafeArray *newUnsafeArray;
newUnsafeArray = (struct unsafeArray*) malloc (sizeof(struct unsafeArray));
newUnsafeArray->nextScope = NULL;
newUnsafeArray->count = 0;
newUnsafeArray->prevScope = NULL;
currentUnsafeArray = newUnsafeArray;
}
currentUnsafeArray->unsafe[currentUnsafeArray->count++] = strdup(graph_node->symbol[0]);
}
//Variables initialized and unchecked are added to the unsafe array
if(graph_node->type==lhs){
int loopCounter,flag=0;
if(currentSafeArray){
for(loopCounter=0;loopCounter<currentSafeArray->count;loopCounter++){
if(strcmp(currentSafeArray->safe[loopCounter],graph_node->symbol[0])==0){
currentUnsafeArray->unsafe[currentUnsafeArray->count] = malloc(sizeof(char));
strcpy(currentUnsafeArray->unsafe[currentUnsafeArray->count++],currentSafeArray->safe[loopCounter]);
flag=1;
break;
}
}
if(flag==1){
int innerLoopCounter;
for(innerLoopCounter=loopCounter;innerLoopCounter<currentSafeArray->count-1;innerLoopCounter++){
currentSafeArray->safe[innerLoopCounter] = currentSafeArray->safe[innerLoopCounter+1];
}
currentSafeArray->count--;
}
}
}
//Check for potential buffer overflows
if(graph_node->indexCount>0){
int loopCounter,innerLoopCounter,flag=0;
for(loopCounter=0;loopCounter<graph_node->indexCount;loopCounter++){
flag=0;
if(currentSafeArray){
for(innerLoopCounter=0;innerLoopCounter<currentSafeArray->count;innerLoopCounter++){
if(strcmp(graph_node->index[loopCounter],currentSafeArray->safe[innerLoopCounter])==0){
flag=1;
break;
}
}
}
// Potential buffer overflow
if(flag==0){
printf("potential buffer overflow line: %d\n",graph_node->line);
break;
}
}
}
if((graph_node->next[0]==NULL)){
return 1;
}
//Recursive call
traverse_graph_for_buffer_overflow(graph_node->next[next_count++]);
while(graph_node->next[next_count+1]!=NULL){
traverse_graph_for_buffer_overflow(graph_node->next[next_count++]);
}
//Return to previous state
if(graph_node->next[next_count]!=NULL){
currentSafeArray = currentSafeArray->prevScope;
currentUnsafeArray = currentUnsafeArray->prevScope;
traverse_graph_for_buffer_overflow(graph_node->next[next_count++]);
}
}
void var_array_add(struct node *graph_node){
var_array[var_array_count++]=graph_node->symbol[0];
}
void push_var_array_stack(int count){
var_array_stack[var_array_stack_count++] = count;
}
void push_init_array_stack(int count){
init_array_stack[init_array_stack_count++] = count;
}
void push_init_var_used_stack(int count){
init_var_used_stack[init_var_used_stack_count++] = count;
}
int pop_var_array_stack(){
--var_array_stack_count;
return var_array_stack[var_array_stack_count];
}
int pop_init_array_stack(){
--init_array_stack_count;
return init_array_stack[init_array_stack_count];
}
int pop_init_var_used_stack(){
--init_var_used_stack_count;
return init_var_used_stack[init_var_used_stack_count];
}
void print_init_array(struct node* graph_node){
int loopCounter;
FILE *fp;
if(first_print!=0){
fp = fopen("c:\\raunak\\static\\init_var.json","a");
fprintf(fp,",");
fclose(fp);
}
fp = fopen("c:\\raunak\\static\\init_var.json","a");
fprintf(fp,"\"%d\":[\"%s\"]", graph_node->line,var_array[init_var_used]);
prev_line = graph_node->line;
first_print++;
fclose(fp);
}
void print_mem_leaks(struct node* graph_node){
int loopCounter;
FILE *fp;
loopCounter=0;
for(loopCounter=0;loopCounter<allocation_count;loopCounter++){
int innerLoopCounter;
for(innerLoopCounter=0;innerLoopCounter<allocation_node[loopCounter]->count;innerLoopCounter++){
}
}
for(loopCounter=0;loopCounter<mem_freed_array_count;loopCounter++){
int innerLoopCounter;
for(innerLoopCounter=0;innerLoopCounter<allocation_count;innerLoopCounter++){
if(mem_freed_array[loopCounter]==allocation_node[innerLoopCounter]->index){ /* Map to variable array */
allocation_node[innerLoopCounter]->free = 1;
break;
}
}
}
int innerLoopCounter;
for(innerLoopCounter=0;innerLoopCounter<var_array_count;innerLoopCounter++){
if(strcmp(var_array[innerLoopCounter],"malloc")==0)
break;
}
fp = fopen("c:\\raunak\\static\\mem_leaks.json","w");
fprintf(fp,"{");
fclose(fp);
for(loopCounter=0;loopCounter<allocation_count;loopCounter++){
fp = fopen("c:\\raunak\\static\\mem_leaks.json","a");
if((allocation_node[loopCounter]->free==0)&&(allocation_node[loopCounter]->points[allocation_node[loopCounter]->count]!=innerLoopCounter)){
if(first_mem_print!=0)
fprintf(fp,",");
first_mem_print++;
fprintf(fp,"\"%d\":[\"%s\"]", allocation_node[loopCounter]->line, var_array[allocation_node[loopCounter]->points[0]]);
}
fclose(fp);
}
fp = fopen("c:\\raunak\\static\\mem_leaks.json","a");
fprintf(fp,"}");
fclose(fp);
}
void malloc_array_add(struct node* graph_node){
malloc_array[malloc_array_count++] = graph_node->symbol[0];
}
void remove_malloc_array(char* symbol){
int loopCounter,innerLoopCounter,k,index,flag=0;
for(loopCounter=var_array_count-1;loopCounter>=0;loopCounter--){
if(strcmp(var_array[loopCounter],symbol)==0)
break;
}
for(innerLoopCounter=0;innerLoopCounter<allocation_count;innerLoopCounter++){
for(k=0;k<allocation_node[innerLoopCounter]->count;k++){
if(allocation_node[innerLoopCounter]->points[k]==loopCounter){
flag = 1;
break;
}
}
if(flag==1)
break;
}
for(loopCounter=innerLoopCounter;loopCounter<allocation_count-1;loopCounter++){
allocation_node[loopCounter] = allocation_node[loopCounter+1];
}
free(allocation_node[loopCounter+1]);
allocation_count--;
}
void check_lhs_for_init_var(struct node * graph_node){
int loopCounter,index,flag=0,innerLoopCounter=0,repeat=0,valid;
int isAllocationNode=0;
if(var_array_count==0){
var_array_add(graph_node);
index = 0;
}
innerLoopCounter = var_array_count-1;
for(loopCounter=innerLoopCounter;loopCounter>=0;loopCounter--){
if(strcmp(var_array[loopCounter],graph_node->symbol[0])==0){
flag = 1;
index = loopCounter;
break;
}
}
if(flag==0){
var_array_add(graph_node);
index = var_array_count-1;
}
if(graph_node->next[0]){
if((graph_node->next[0])->type==3){
struct node *temp;
int loopCounter,innerLoopCounter,k,flag=0;
temp = graph_node->next[0];
for(k=var_array_count-1;k>=0;k--){
if(strcmp(var_array[k],temp->symbol[0])==0){
break;
}
}
}
}
for(loopCounter=0;loopCounter<init_array_count;loopCounter++){
if(init_array[loopCounter] == index){
break;
}
}
if(repeat==0){
init_array[init_array_count] = index;
init_array_count++;
}
}
int check_rhs_validity(struct node* graph_node){
if((strcmp(graph_node->symbol[0],"malloc")!=0)&&(strcmp(graph_node->symbol[0],"scanf")!=0)){
int loopCounter,index=101,flag=0;
for(loopCounter=var_array_count-1;loopCounter>=0;loopCounter--){
if(strcmp(var_array[loopCounter],graph_node->symbol[0])==0){
index = loopCounter;
break;
}
}
for(loopCounter=init_array_count-1;loopCounter>=0;loopCounter--){
if(init_array[loopCounter]==index){
flag=1;
break;
}
}
if(flag==0){
init_var_used = index;
print_init_array(graph_node);
return 0;
}
}
return 1;
}
void check_lhs_for_mem_leaks(struct node * graph_node){
int loopCounter,index,flag=0,innerLoopCounter=0,repeat=0,valid;
int isAllocationNode=0;
if(var_array_count==0){
var_array_add(graph_node);
index = 0;
}
innerLoopCounter = var_array_count-1;
for(loopCounter=innerLoopCounter;loopCounter>=0;loopCounter--){
if(strcmp(var_array[loopCounter],graph_node->symbol[0])==0){
flag = 1;
index = loopCounter;
break;
}
}
if(flag==0){
var_array_add(graph_node);
index = var_array_count-1;
}
if(graph_node->next[0]){
if((graph_node->next[0])->type==3){
struct node *temp;
int loopCounter,innerLoopCounter,k,flag=0;
temp = graph_node->next[0];
for(k=var_array_count-1;k>=0;k--){
if(strcmp(var_array[k],temp->symbol[0])==0){
break;
}
}
for(loopCounter=0;loopCounter<allocation_count;loopCounter++){
for(innerLoopCounter=0;innerLoopCounter<allocation_node[loopCounter]->count;innerLoopCounter++){
if(k == allocation_node[loopCounter]->points[innerLoopCounter]){ //
allocation_node[loopCounter]->points[allocation_node[loopCounter]->count] = index;
allocation_node[loopCounter]->count++;
flag = 1;
isAllocationNode=1;
break;
}
}
if(flag==1)
break;
}
}
}
}
void add_mem_freed_array(int index){
mem_freed_array[mem_freed_array_count++] = index;
}
void push_mem_path_array(int index){
mem_path_array[mem_path_array_count++] = index;
}
int pop_mem_path_array(){
return mem_path_array[--mem_path_array_count];
}