-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.c
746 lines (657 loc) · 22.1 KB
/
function.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
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
/***********************************************************************************
Copyright 2014 Arizona Board of Regents; all rights reserved.
This software is being provided by the copyright holders under the
following license. By obtaining, using and/or copying this software, you
agree that you have read, understood, and will comply with the following
terms and conditions:
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee or royalty is hereby granted,
provided that the full text of this notice appears on all copies of the
software and documentation or portions thereof, including modifications,
that you make.
This software is provided "as is," and copyright holders make no
representations or warranties, expressed or implied. By way of example, but
not limitation, copyright holders make no representations or warranties of
merchantability or fitness for any particular purpose or that the use of the
software or documentation will not infringe any third party patents,
copyrights, trademarks or other rights. Copyright holders will bear no
liability for any use of this software or documentation.
The name and trademarks of copyright holders may not be used in advertising
or publicity pertaining to the software without specific, written prior
permission. Title to copyright in this software and any associated
documentation will at all times remain with copyright holders.
***********************************************************************************/
/*
* function.c
*
* Created on: May 16, 2011
* Author: genlu
*/
#include <stdlib.h>
#include <assert.h>
#include "al_helper.h"
#include "cfg.h"
#include "instr.h"
#include "function.h"
static int anonfunobjctl=0;
static int funobjtableentryctl=0;
char *anonfunobj_str = "anonfunobj";
int functionCFGProcessed = 0;
int FuncEntryBlkIDCompare(void *i1, void *i2){
assert(i1 && i2);
return ((Function *)i1)->funcEntryBlock->id- ((Function *)i2)->funcEntryBlock->id;
}
int FuncEntryAddrCompare(void *i1, void *i2){
assert(i1 && i2);
return ((Function *)i1)->funcEntryAddr - ((Function *)i2)->funcEntryAddr;
}
void destroyFunctionNode(void *func){
if(func!=NULL){
if(((Function *)func)->funcObj)
al_free(((Function *)func)->funcObj);
free((Function *)func);
}
}
Function *createFunctionNode(void){
Function *func = (Function *)malloc(sizeof(Function));
assert(func);
func->args = 0;
func->loc_vars = 0;
func->flags = 0;
func->funcEntryAddr = 0;
func->funcObj = al_newInt(AL_LIST_SET);;
func->funcEntryBlock = NULL;
//func->augmentedExit = NULL;
func->funcName = NULL;
func->funcBody = al_newGeneric(AL_LIST_SORTED, blockIdCompare, NULL, NULL);
return func;
}
void destroyFunctionCFGs(ArrayList *funcCFGs){
if(funcCFGs){
//printf("freeing %d\n", al_size(funcCFGs));
al_freeWithElements(funcCFGs);
}
}
/*
* the recursive function used by buildFunctionCFGs
*/
static void doSearch(ArrayList *blockList, BasicBlock *n, uint32_t flag, ArrayList *functionCFGs){
int k, i, j;
BlockEdge *edge;
BasicBlock *block1, *block2, *block3, *block4;
Function *func;
if(!n)return;
assert(n);
BBL_SET_FLAG(n, flag);
if(n->type==BT_SCRIPT_INVOKE){
//assert(al_size(n->succs)==1);
for(k=0;k<al_size(n->succs);k++)
{
//get a pointer to the callee
edge = (BlockEdge *)al_get(n->succs, k);
if(EDGE_HAS_FLAG(edge, EDGE_IS_CALLSITE))
continue;
block1 = edge->head;
BBL_SET_FLAG(block1, BBL_IS_FUNC_ENTRY);
edge = NULL;
//n = NULL; //FXXX!!!
/*
* Now block1 is the function entry block, use block1->preds to find all caller blocks
* and concat them with their next block.
* cut out the function from CFG, create a function node and insert it to functionCFGs
* XXX: this will fail if caller and returned block are not adjacent address-wise (i.e. in a slice/simpllified trace)
* need to change this later!
*/
//printf("@@b1:%d\n", block1->id);
for(i=0;i<al_size(block1->preds);i++){
edge = (BlockEdge *)al_get(block1->preds, i);
block2 = edge->tail;
//printf("@@ b2:%d\n", block2->id);
//printBasicBlock(block2); //////////////////////
//assert(block2->type==BT_SCRIPT_INVOKE);
//assert(al_size(block2->succs)==1);
//xxx this is probably wrong, check it! maybe search for edge in block2->succs that edge->head==block1
edge = (BlockEdge *)al_get(block2->succs, 0);
//assert(EDGE_HAS_FLAG(edge, EDGE_IS_SCRIPT_INVOKE));///////////////////////
//remove this edge from block2->succs and block1->preds
al_remove(block2->succs, (void *)edge);
al_remove(block1->preds, (void *)edge);
//hack
i--;
if(n->id==block2->id)
k--;
destroyBlockEdge((void *)edge);
//assert(al_size(block2->succs)==0);
block3 = findBasicBlockFromListByAddr(blockList, block2->end_addr + 3);
if(!block3){
//printf("block2:%d\t%lx\n",block2->id, block2->end_addr + 3);
//assert(block3);
continue;
}
//printf(" @@b3:%d\n", block3->id);
for(j=0;j<al_size(block3->preds);j++){
edge = (BlockEdge *)al_get(block3->preds, j);
if(!EDGE_HAS_FLAG(edge, EDGE_IS_RET))
continue;
else{
block4 = edge->tail;
BBL_SET_FLAG(block4, BBL_IS_FUNC_RET);
//printf("@@ b4:%d\n", block4->id);
al_remove(block4->succs, (void *)edge);
al_remove(block3->preds, (void *)edge);
//printf("@@@@@ b1:%d\tb2:%d\tb3:%d\tb4:%d\n", block1->id, block2->id, block3->id, block4->id);
//hack
j--;
destroyBlockEdge((void *)edge);
}
}
edge = createBlockEdge();
edge->tail = block2;
edge->head = block3;
edge->count = block2->count;
EDGE_SET_FLAG(edge, EDGE_IS_CALLSITE);
int added = 0;
if(!al_contains(block2->succs, edge)){
al_add(block2->succs, edge);
added++;
}else{
EDGE_SET_FLAG(((BlockEdge *)al_get(block2->succs, al_indexOf(block2->succs, edge))), EDGE_IS_CALLSITE);
}
if(!al_contains(block3->preds, edge)){
al_add(block3->preds, edge);
added++;
}else{
EDGE_SET_FLAG(((BlockEdge *)al_get(block3->preds, al_indexOf(block3->preds, edge))), EDGE_IS_CALLSITE);
}
if(!added){
destroyBlockEdge(edge);
}
//printf("dealing: %d\t block1:%d\t block3:%d\n", n->id, block1->id, block3->id);
doSearch(blockList, block3, flag, functionCFGs);
}
assert(al_size(block1->preds)==0);
func = createFunctionNode();
func->funcEntryAddr = block1->addr;
func->funcEntryBlock = block1;
if(al_contains(functionCFGs, (void *)func))
destroyFunctionNode((void *)func);
else
al_add(functionCFGs, (void *)func);
//printBasicBlockList(blockList);
doSearch(blockList, block1, flag, functionCFGs);
//doSearch(blockList, block3, flag, functionCFGs);
}
}
else{
for(i=0;i<al_size(n->succs);i++){
edge = (BlockEdge *)al_get(n->succs, i);
block1 = edge->head;
if(!BBL_HAS_FLAG(block1, flag)){
//EDGE_SET_FLAG(edge, EDGE_IN_DFST);
doSearch(blockList, block1, flag, functionCFGs);
}
}
}
}
void concatCallSiteBlocks(BlockEdge *edge){
int i;
BasicBlock *block1, *block2;
BlockEdge *e;
Instruction *instr;
block1 = edge->tail;
block2 = edge->head;
block1->type = block2->type;
block1->lastInstr = block2->lastInstr;
al_remove(block1->succs, edge);
for(i=0;i<al_size(block2->succs);i++){
e = al_get(block2->succs, i);
e->tail = block1;
al_add(block1->succs, e);
}
for(i=0;i<al_size(block2->instrs);i++){
instr = al_get(block2->instrs, i);
instr->inBlock = block1;
al_add(block1->instrs, instr);
}
block1->end_addr = block2->end_addr;
if(BBL_HAS_FLAG(block2, BBL_IS_HALT_NODE))
BBL_SET_FLAG(block1, BBL_IS_HALT_NODE);
if(BBL_HAS_FLAG(block2, BBL_IS_FUNC_RET))
BBL_SET_FLAG(block1, BBL_IS_FUNC_RET);
al_free(block2->instrs);
al_free(block2->preds);
al_free(block2->succs);
}
void doSearchFuncBody(Function *func, BasicBlock *n, ArrayList *funcBody, uint32_t flag){
int i;
BlockEdge *edge;
BasicBlock *block;
assert(n);
BBL_SET_FLAG(n, flag);
al_add(funcBody, n);
for(i=0;i<al_size(n->succs);i++){
edge = al_get(n->succs, i);
block = edge->head;
if(!BBL_HAS_FLAG(block, flag)){
doSearchFuncBody(func, block, funcBody, flag);
}
}
}
void traverseFuncBody(Function *func, BasicBlock *n, uint32_t flag){
int i;
BlockEdge *edge;
BasicBlock *block;
assert(n);
BBL_SET_FLAG(n, flag);
n->inFunction = func->funcEntryAddr;
for(i=0;i<al_size(n->succs);i++){
edge = al_get(n->succs, i);
block = edge->head;
if(!BBL_HAS_FLAG(block, flag)){
traverseFuncBody(func, block, flag);
}
}
}
/*
* buildFunctionCFGs() rearrange CFG specified by 'blockList',
* "cut out" all the functions, and connect call-site and return-site (adjacent instructions)
* with an 'EDGE_IS_CALLSITE' edge. it also mark entry and return node of functions
* Finally, a list of Function nodes is returned (program entry is not considered as a function)
*/
ArrayList *buildFunctionCFGs(InstrList *iList, ArrayList *blockList, ArrayList **funcObjTable){
int i,j,k;
BasicBlock *block;
BlockEdge *edge;
BasicBlock *n0;
ArrayList *functionCFGs;
Instruction *instr;
functionCFGProcessed++;
functionCFGs = al_newGeneric(AL_LIST_SET, FuncEntryBlkIDCompare, NULL, destroyFunctionNode);
assert(functionCFGs);
n0=NULL;
//make sure flag TMP0 is cleared
for(i=0;i<al_size(blockList);i++){
block = al_get(blockList, i);
BBL_CLR_FLAG(block, BBL_FLAG_TMP0);
if(BBL_HAS_FLAG(block, BBL_IS_ENTRY_NODE))
n0 = block;
}
//printf("n0: %lx\n", n0);
//printf("n0:%p\n", n0);
doSearch(blockList, n0, BBL_FLAG_TMP0, functionCFGs);
//clear flag TMP0
for(i=0;i<al_size(blockList);i++){
block = al_get(blockList, i);
BBL_CLR_FLAG(block, BBL_FLAG_TMP0);
}
//concat all script call and returned block as one block
int change;
while(1){
change=0;
for(i=0;i<al_size(blockList);i++){
block = al_get(blockList, i);
for(j=0;j<al_size(block->succs);j++){
edge = al_get(block->succs, j);
if(EDGE_HAS_FLAG(edge, EDGE_IS_CALLSITE)){
//assert(0);
printf("concat callsite: %d->%d\n", edge->tail->id, edge->head->id);
assert(al_size(edge->tail->succs)==1);
assert(al_size(edge->head->preds)==1);
concatCallSiteBlocks(edge);
al_remove(blockList, edge->head);
for(k=0;k<InstrListLength(iList);k++){
instr = getInstruction(iList, k);
if(instr->inBlock==edge->head){
assert(0); //this should be done in concatCallsite(already)
instr->inBlock = edge->tail;
}
}
destroyBlockEdge(edge);
edge=NULL;
change++;
break;
}
}//end inner for-loop
if(change)
break;
}//end outer for-loop
if(!change)
break;
}
Function *func;
//find all the function bodies
for(i=0;i<al_size(functionCFGs);i++){
func = al_get(functionCFGs, i);
doSearchFuncBody(func, func->funcEntryBlock, func->funcBody, BBL_FLAG_TMP0);
}
//print all function info
printf("\nFunctionCFGs:\t");
printf("%d\n", al_size(functionCFGs));
for(i=0;i<al_size(functionCFGs);i++){
func = al_get(functionCFGs, i);
printf("func %s\t", func->funcName);
printf("func_entry_addr %lx\t", func->funcEntryAddr);
for(j=0;j<al_size(func->funcBody);j++){
printf("%d ",((BasicBlock*)al_get(func->funcBody, j))->id);
}
printf("\n");
}
printf("**********************\n");
//clear flag TMP0
for(i=0;i<al_size(blockList);i++){
block = al_get(blockList, i);
BBL_CLR_FLAG(block, BBL_FLAG_TMP0);
}
//try to find the total number of arguments of each function
for(i=0;i<al_size(functionCFGs);i++){
int args = 0;
func = al_get(functionCFGs, i);
for(j=0;j<al_size(func->funcBody);j++){
block = al_get(func->funcBody, j);
for(k=0;k<al_size(block->instrs);k++){
instr = al_get(block->instrs, k);
if(instr->opCode==JSOP_GETARG || instr->opCode==JSOP_SETARG || instr->opCode==JSOP_CALLARG ||
instr->opCode==JSOP_ARGINC || instr->opCode==JSOP_INCARG || instr->opCode==JSOP_ARGDEC || instr->opCode==JSOP_DECARG ||
INSTR_HAS_FLAG(instr, INSTR_IS_LARG_ACCESS)){
if(instr->operand.i > args)
args = instr->operand.i;
}
}
}
func->args = args;
}
//create funcObjTable first, this is needed for slice processing
*funcObjTable = constructFuncObjTable(iList, functionCFGs);
printFuncObjTable(*funcObjTable);
//fill the block->inFunction field (which is from func_obj)
for(i=0;i<al_size(functionCFGs);i++){
func = al_get(functionCFGs, i);
traverseFuncBody(func, func->funcEntryBlock, BBL_FLAG_TMP0);
}
for(i=0;i<al_size(blockList);i++){
block = al_get(blockList, i);
BBL_CLR_FLAG(block, BBL_FLAG_TMP0);
}
//for each instr, find which function this instr is in. (0 means not in any function, or in main procedure )
for(i=0;i<InstrListLength(iList);i++){
instr = getInstruction(iList, i);
instr->inFunction = instr->inBlock->inFunction;
}
return functionCFGs;
}
Function *findFunctionByObjAddress(ArrayList *functionCFGs, ADDRESS funcObjAddr){
assert(functionCFGs);
Function *func;
ADDRESS funcObj;
int i,j;
for(i=0;i<al_size(functionCFGs);i++){
func = al_get(functionCFGs, i);
for(j=0;j<al_size(func->funcObj);j++){
funcObj = (ADDRESS) al_get(func->funcObj, j);
if(funcObj==funcObjAddr){
return func;
}
}
}
printf("Error: cannot find function object with id %lx\n", funcObjAddr);
return NULL;
}
Function *findFunctionByEntryAddress(ArrayList *functionCFGs, ADDRESS addr){
assert(functionCFGs);
Function *func;
int i;
for(i=0;i<al_size(functionCFGs);i++){
func = al_get(functionCFGs, i);
if(func->funcEntryBlock->addr==addr){
return func;
}
}
printf("Error: cannot find function object with address %lx\n", addr);
return NULL;
}
Function *findFunctionByEntryBlockID(ArrayList *functionCFGs, int id){
assert(functionCFGs);
Function *func;
int i;
for(i=0;i<al_size(functionCFGs);i++){
func = al_get(functionCFGs, i);
if(func->funcEntryBlock->id==id){
return func;
}
}
printf("Error: cannot find function object with id %d\n", id);
return NULL;
}
BasicBlock *isBlockInTheFunction(Function *func, int id){
int i;
BasicBlock *block;
for(i=0;i<al_size(func->funcBody);i++){
block = al_get(func->funcBody, i);
if(block->id == id)
return block;
}
return NULL;
}
FuncObjTableEntry *createFuncObjTableEntry(void){
FuncObjTableEntry *entry;
entry = (FuncObjTableEntry *)malloc(sizeof(FuncObjTableEntry));
assert(entry);
entry->id = funobjtableentryctl++;
entry->flags = 0;
entry->func_name = NULL;
entry->func_addr = 0;
entry->anonfunobj_instr_addr = 0;
entry->func_struct = NULL;
entry->func_objs = al_newInt(AL_LIST_SET);
return entry;
}
void destroyFuncObjTableEntry(void *e){
assert(e);
FuncObjTableEntry *entry = (FuncObjTableEntry *)e;
if(entry->func_name){
free(entry->func_name);
entry->func_name = NULL;
}
if(entry->func_objs){
al_free(entry->func_objs);
entry->func_objs=NULL;
}
free(entry);
}
//function's starting address is unique, rather than the function's obj address (i.e. anonymous functions)
int FuncObjTableEntryCompare(void *a1, void *a2){
assert(a1 && a2);
return ((FuncObjTableEntry *)a1)->func_addr - ((FuncObjTableEntry *)a2)->func_addr;
}
//find function table entry by address
FuncObjTableEntry *findFuncObjTableEntryByFuncAddr(ArrayList *FuncObjTable, ADDRESS funcAddr){
assert(FuncObjTable);
assert(funcAddr);
int i;
FuncObjTableEntry *entry;
for(i=0;i<al_size(FuncObjTable);i++){
entry = (FuncObjTableEntry*)al_get(FuncObjTable, i);
if(entry->func_addr == funcAddr){
return entry;
}
}
return NULL;
}
//find function table entry by address
FuncObjTableEntry *findFuncObjTableEntryByFuncObj(ArrayList *FuncObjTable, ADDRESS funcObj){
assert(FuncObjTable);
assert(funcObj);
int i,j;
ADDRESS tempAddr = 0;
FuncObjTableEntry *entry = NULL;
for(i=0;i<al_size(FuncObjTable);i++){
entry = al_get(FuncObjTable, i);
for(j=0;j<al_size(entry->func_objs);j++){
tempAddr = (ADDRESS)al_get(entry->func_objs, j);
if(tempAddr == funcObj){
return entry;
}
}
}
return NULL;
}
void printFuncObjTableEntry(FuncObjTableEntry *entry){
int i;
printf("id: %d\tfuncName: %16s\tfunc_addr:%lx\ttargetBlock:%d\n", entry->id,
entry->func_name?entry->func_name:"(null)", entry->func_addr, entry->func_struct?entry->func_struct->funcEntryBlock->id:-1);
printf("\t-- func_objs:\t");
for(i=0;i<al_size(entry->func_objs);i++){
printf(" %lx ", (ADDRESS)al_get(entry->func_objs, i));
}
printf("\n");
}
void printFuncObjTable(ArrayList *funcObjTable){
assert(funcObjTable);
int i;
FuncObjTableEntry *entry;
printf("\nFuncObj Table:\n");
for(i=0;i<al_size(funcObjTable);i++){
entry = al_get(funcObjTable, i);
printFuncObjTableEntry(entry);
}
printf("\n");
}
/*******************************************************************/
/*
* go though the trace to create functionObjTable.
* function def instructions (JSOP_DEFFUN, JSOP_CLOSURE) will add a new entry in table, fill in the name and funcObj
* JSOP_ANONFUNOBJ will check if the function is already added (by using instr's addr) before create a new entry
* Function calling instructions (JSOP_NEW & JSOP_CALL) will find the existing function entry,
* and fill in the the address of function's first instruction ()
* JSOP_STOP/RETURN will set the func->loc_vars by looking at instr->nvars
*/
ArrayList *constructFuncObjTable(InstrList *iList, ArrayList *funcCFGs){
anonfunobjctl = 0;
#define TABLE_DEBUG 0
int i,j, size;
char *str=NULL;
Instruction *instr=NULL;
Instruction *next=NULL;
Function *funcStruct=NULL;
FuncObjTableEntry *funcObjTableEntry1=NULL;
ArrayList *funcObjTable;
funcObjTable = al_newGeneric(AL_LIST_UNSORTED, NULL, NULL, destroyFuncObjTableEntry);
size = InstrListLength(iList);
for(i=0;i<size;i++){
instr = getInstruction(iList, i);
if(i<size-1)
next = getInstruction(iList, i+1);
else
next = NULL;
//function calling instrs
if(INSTR_HAS_FLAG(instr, INSTR_IS_SCRIPT_INVOKE) && (instr->opCode == JSOP_NEW ||
(instr->opCode == JSOP_CALL&&!INSTR_HAS_FLAG(instr, INSTR_IS_DOC_WRITE)))){
assert(next);
#if TABLE_DEBUG
fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~instr: %lx\tfind func instr->objRef: %lx\n", instr->addr, instr->objRef);
#endif
funcStruct = findFunctionByEntryAddress(funcCFGs, next->addr);
funcObjTableEntry1 = findFuncObjTableEntryByFuncObj(funcObjTable, instr->objRef);
assert(funcObjTableEntry1);
assert(funcStruct);
assert(funcObjTableEntry1->func_name);
if(funcObjTableEntry1->func_struct!=NULL){
assert(funcObjTableEntry1->func_struct==funcStruct);
}else{
funcObjTableEntry1->func_struct = funcStruct;
}
if(funcObjTableEntry1->func_addr!=0){
assert(funcObjTableEntry1->func_addr==next->addr);
}else{
funcObjTableEntry1->func_addr = next->addr;
}
if(funcStruct->funcName==NULL){
funcStruct->funcName = (char *)malloc(strlen(funcObjTableEntry1->func_name)+1);
assert(funcStruct->funcName);
memcpy(funcStruct->funcName, funcObjTableEntry1->func_name, strlen(funcObjTableEntry1->func_name)+1);
al_add(funcStruct->funcObj,(void *)instr->objRef);
}else{
assert(!strcmp(funcStruct->funcName, funcObjTableEntry1->func_name));
}
}
//eval, do nothing, just set flag (since eval'ed code doesn't need a name)
else if(INSTR_HAS_FLAG(instr, INSTR_IS_SCRIPT_INVOKE) &&
((instr->opCode == JSOP_EVAL)||(instr->opCode == JSOP_CALL&&INSTR_HAS_FLAG(instr, INSTR_IS_DOC_WRITE)))){
assert(next);
funcStruct = findFunctionByEntryAddress(funcCFGs, next->addr);
if(funcStruct)
BBL_SET_FLAG(funcStruct->funcEntryBlock, BBL_IS_EVAL_ENTRY);
else
assert(0);
}
//named function def
else if(instr->opCode==JSOP_DEFFUN || instr->opCode==JSOP_CLOSURE){
#if TABLE_DEBUG
fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~instr: %lx\tadd func %s instr->objRef: %lx\n", instr->addr,
instr->str?instr->str:"(null)", instr->objRef);
#endif
str = (char *)malloc(strlen(instr->str)+1);
assert(str);
memcpy(str, instr->str, strlen(instr->str)+1);
funcObjTableEntry1 = createFuncObjTableEntry();
funcObjTableEntry1->func_name = str;
al_add(funcObjTableEntry1->func_objs, (void *)(instr->objRef));
assert(!findFuncObjTableEntryByFuncObj(funcObjTable, instr->objRef));
al_add(funcObjTable, funcObjTableEntry1);
str=NULL;
#if TABLE_DEBUG
printFuncObjTable(funcObjTable);
#endif
}
//anonymous
else if(instr->opCode==JSOP_ANONFUNOBJ){
//first, check if there's any entry for the same anonymous function already been added
//we have to use anonfunobj instruction's addr since every time this instr will use a different obj_ref
for(j=0;j<al_size(funcObjTable);j++){
funcObjTableEntry1 = (FuncObjTableEntry *)al_get(funcObjTable, j);
if(FE_HAS_FLAG(funcObjTableEntry1, FE_IS_ANONFUNOBJ) && funcObjTableEntry1->anonfunobj_instr_addr==instr->addr){
break;
}
funcObjTableEntry1=NULL;
}
//if we find one, then just add the obj_entry in to the list
if(funcObjTableEntry1){
#if TABLE_DEBUG
fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~instr: %lx\tadd anon-func instr->objRef: %lx(only obj_ref)\n", instr->addr, instr->objRef);
fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~ to entry %d\n", funcObjTableEntry1->id);
#endif
al_add(funcObjTableEntry1->func_objs, (void *)(instr->objRef));
#if TABLE_DEBUG
printFuncObjTable(funcObjTable);
#endif
}
//otherwise, create and add the new entry (and create a name for the function)
else{
#if TABLE_DEBUG
fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~instr: %lx\tadd anon-func #%d instr->objRef: %lx\n", instr->addr, anonfunobjctl, instr->objRef);
#endif
str = (char *)malloc(strlen(anonfunobj_str)+5);
assert(str);
sprintf(str, "%s%d", anonfunobj_str, anonfunobjctl++);
funcObjTableEntry1 = createFuncObjTableEntry();
funcObjTableEntry1->func_name = str;
funcObjTableEntry1->anonfunobj_instr_addr = instr->addr;
al_add(funcObjTableEntry1->func_objs, (void *)(instr->objRef));
assert(!findFuncObjTableEntryByFuncObj(funcObjTable, instr->objRef));
FE_SET_FLAG(funcObjTableEntry1, FE_IS_ANONFUNOBJ);
al_add(funcObjTable, funcObjTableEntry1);
str=NULL;
#if TABLE_DEBUG
printFuncObjTable(funcObjTable);
#endif
}
}
//return op, set func->loc_vars
else if(INSTR_HAS_FLAG(instr,INSTR_IS_RET) && instr->inCallee){
funcStruct = findFunctionByObjAddress(funcCFGs, instr->inCallee);
assert(funcStruct);
funcStruct->loc_vars = instr->nvars;
}
}//end for-loop
return funcObjTable;
}