forked from dyninst/dyninst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.C
More file actions
871 lines (766 loc) · 26.7 KB
/
Copy pathFunction.C
File metadata and controls
871 lines (766 loc) · 26.7 KB
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
/*
* See the dyninst/COPYRIGHT file for copyright information.
*
* We provide the Paradyn Tools (below described as "Paradyn")
* on an AS IS basis, and do not warrant its validity or performance.
* We reserve the right to update, modify, or discontinue this
* software at any time. We shall have no obligation to supply such
* updates or modifications or any other form of support to you.
*
* By your use of Paradyn, you understand and agree that we (or any
* other person or entity with proprietary rights in Paradyn) are
* under no obligation to provide either maintenance services,
* update services, notices of latent defects, or correction of
* defects for Paradyn.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Parser.h"
#include <algorithm>
#include "dyntypes.h"
#include "CodeObject.h"
#include "CFG.h"
#include "debug_parse.h"
#include "util.h"
#include "LoopAnalyzer.h"
#include "dominator.h"
#include "dataflowAPI/h/slicing.h"
#include "dataflowAPI/h/AbslocInterface.h"
#include "instructionAPI/h/InstructionDecoder.h"
#include "common/h/Graph.h"
#include "StackTamperVisitor.h"
#include "common/src/dthread.h"
#include <boost/thread/lock_guard.hpp>
using namespace std;
using namespace Dyninst;
using namespace Dyninst::ParseAPI;
Function::Function() :
_start(0),
_obj(NULL),
_region(NULL),
_isrc(NULL),
_cache_valid(false),
_src(RT),
_rs(UNSET),
_entry(NULL),
_is_leaf_function(true),
_ret_addr(0),
_parsed(false),
_no_stack_frame(true),
_saves_fp(false),
_cleans_stack(false),
_tamper(TAMPER_UNSET),
_tamper_addr(0),
_loop_analyzed(false),
_loop_root(NULL),
isDominatorInfoReady(false),
isPostDominatorInfoReady(false)
{
fprintf(stderr,"PROBABLE ERROR, default ParseAPI::Function constructor\n");
}
Function::Function(Address addr, string name, CodeObject * obj,
CodeRegion * region, InstructionSource * isrc) :
_start(addr),
_obj(obj),
_region(region),
_isrc(isrc),
_cache_valid(false),
_src(RT),
_rs(UNSET),
_name(name),
_entry(NULL),
_is_leaf_function(true),
_ret_addr(0),
_parsed(false),
_no_stack_frame(true),
_saves_fp(false),
_cleans_stack(false),
_tamper(TAMPER_UNSET),
_tamper_addr(0),
_loop_analyzed(false),
_loop_root(NULL),
isDominatorInfoReady(false),
isPostDominatorInfoReady(false)
{
if (obj->defensiveMode()) {
mal_printf("new funct at %lx\n",addr);
}
if (obj && obj->cs()) {
obj->cs()->incrementCounter(PARSE_FUNCTION_COUNT);
}
if (obj && obj->cs() && obj->cs()->nonReturning(name)) {
set_retstatus(NORETURN);
}
}
ParseAPI::Edge::~Edge() {
}
Function::~Function()
{
if (_obj && _obj->cs()) {
_obj->cs()->decrementCounter(PARSE_FUNCTION_COUNT);
}
vector<FuncExtent *>::iterator eit = _extents.begin();
for( ; eit != _extents.end(); ++eit) {
delete *eit;
}
for (auto lit = _loops.begin(); lit != _loops.end(); ++lit)
delete *lit;
}
Function::blocklist
Function::blocks()
{
boost::lock_guard<Function> g(*this);
if(!_cache_valid)
finalize();
return blocklist(blocks_begin(), blocks_end());
}
// Get the current set of blocks,
// as a const operation
Function::const_blocklist
Function::blocks() const
{
boost::lock_guard<const Function> g(*this);
assert(_cache_valid);
return const_blocklist(blocks_begin(), blocks_end());
}
const Function::edgelist &
Function::callEdges() {
boost::lock_guard<Function> g(*this);
if(!_cache_valid)
finalize();
return _call_edge_list;
}
Function::const_blocklist
Function::returnBlocks() {
boost::lock_guard<Function> g(*this);
if (!_cache_valid)
finalize();
return const_blocklist(ret_begin(), ret_end());
}
Function::const_blocklist
Function::exitBlocks() {
boost::lock_guard<Function> g(*this);
if (!_cache_valid)
finalize();
return const_blocklist(exit_begin(), exit_end());
}
Function::const_blocklist
Function::exitBlocks() const {
boost::lock_guard<const Function> g(*this);
assert(_cache_valid);
return const_blocklist(exit_begin(), exit_end());
}
vector<FuncExtent *> const&
Function::extents()
{
boost::lock_guard<Function> g(*this);
if(!_cache_valid)
finalize();
return _extents;
}
void
Function::finalize()
{
boost::lock_guard<Function> g(*this);
bool done;
do {
_extents.clear();
_exitBL.clear();
// for each block, decrement its refcount
for (auto blk = blocks_begin(); blk != blocks_end(); blk++) {
(*blk)->_func_cnt.fetch_add(-1);
}
_bmap.clear();
_retBL.clear();
_call_edge_list.clear();
_cache_valid = false;
// The Parser knows how to finalize
// a Function's parse data
done = _obj->parser->finalize(this);
} while (!done);
}
Function::blocklist
Function::blocks_int()
{
boost::lock_guard<Function> g(*this);
if(_cache_valid || !_entry)
return blocklist(blocks_begin(), blocks_end());
// overloaded map warning:
// visited[addr] == 1 means visited
// visited[addr] == 2 means already on the return list
dyn_hash_map<Address,short> visited;
vector<Block *> worklist;
bool need_entry = true;
for(auto bit=blocks_begin();
bit!=blocks_end();++bit)
{
Block * b = *bit;
visited[b->start()] = 1;
need_entry = need_entry && (b != _entry);
}
worklist.insert(worklist.begin(),blocks_begin(), blocks_end());
if(need_entry) {
worklist.push_back(_entry);
visited[_entry->start()] = 1;
add_block(_entry);
}
// We need to revalidate that the exit blocks we found before are still exit blocks
blockmap::iterator nextIt, curIt;
for (curIt = _exitBL.begin(); curIt != _exitBL.end(); ) {
Block *cur = curIt->second;
bool exit_func = false;
bool found_call = false;
bool found_call_ft = false;
boost::lock_guard<Block> blockGuard(*cur);
Block::edgelist targets;
cur->copy_targets(targets);
if (targets.empty()) exit_func = true;
for (auto e : targets) {
Block *t = e->trg();
if(e->type() == CALL || e->interproc()) {
found_call = true;
}
if (e->type() == CALL_FT) {
found_call_ft = true;
}
if (e->type() == RET || !t || t->obj() != cur->obj()) {
exit_func = true;
break;
}
}
if (found_call && !found_call_ft && !obj()->defensiveMode()) exit_func = true;
if (!exit_func) {
nextIt = curIt;
++nextIt;
_exitBL.erase(curIt);
curIt = nextIt;
} else ++curIt;
}
// avoid adding duplicate return blocks
for(auto bit=exit_begin();
bit!=exit_end();++bit)
{
Block * b = *bit;
visited[b->start()] = 2;
}
while(!worklist.empty()) {
Block * cur = worklist.back();
worklist.pop_back();
bool link_return = false;
bool exits_func = false;
bool found_call = false;
bool found_call_ft = false;
Block::edgelist trgs;
cur->copy_targets(trgs);
if (trgs.empty()) {
// Woo hlt!
parsing_printf("No targets, exits func\n");
exits_func = true;
}
for(auto e : trgs) {
Block * t = e->trg();
if(t) {
parsing_printf("\t Considering target block [0x%lx,0x%lx) from edge %p\n",
t->start(), t->end(), (void*)e);
}
if (e->type() == CALL_FT) {
found_call_ft = true;
}
if(e->type() == CALL) {
parsing_printf("\t Call typed\n");
_call_edge_list.insert(e);
found_call = true;
continue;
}
if(e->type() == RET) {
link_return = true;
exits_func = true;
parsing_printf("Block has return edge\n");
if (obj()->defensiveMode()) {
if (_tamper != TAMPER_UNSET && _tamper != TAMPER_NONE)
continue;
}
continue;
}
/* Handle tailcall edges */
if(e->interproc()) {
parsing_printf("\t Interprocedural\n");
_call_edge_list.insert(e);
found_call = true;
continue;
}
// If we are heading to a different CodeObject, call it a return
// and don't add target blocks.
if (t && (t->obj() != cur->obj())) {
// This is a jump to a different CodeObject; call it an exit
parsing_printf("Block exits object\n");
exits_func = true;
continue;
}
/* sink edges receive no further processing */
if(e->sinkEdge()) {
parsing_printf("\t Sink edge, skipping\n");
continue;
}
if(!HASHDEF(visited,e->trg_addr())) {
if(t) {
parsing_printf("\t Adding target block [%lx,%lx) to worklist according to edge from %lx, type %d\n", t->start(), t->end(), e->src()->last(), e->type());
worklist.push_back(t);
visited[e->trg_addr()] = 1;
add_block(t);
}
}
}
if (found_call && !found_call_ft && !obj()->defensiveMode()) {
parsing_printf("\t exits func\n");
exits_func = true;
}
if (link_return) assert(exits_func);
if(exits_func) {
if (link_return)
delayed_link_return(_obj,cur);
if(visited[cur->start()] <= 1) {
_exitBL[cur->start()] = cur;
parsing_printf("Adding block 0x%lx as exit\n", cur->start());
if (link_return) {
_retBL[cur->start()] = cur;
}
}
}
}
return blocklist(blocks_begin(), blocks_end());
}
/* Adds return edges to the CFG for a particular retblk, based
* on callers to this function. Handles case of return block
* that targets the entry block of a new function separately
* (ret to entry happens if the function tampers with its stack
* and maybe if this function is a signal handler?)
*/
void
Function::delayed_link_return(CodeObject * o, Block * retblk)
{
boost::lock_guard<Function> g(*this);
bool link_entry = false;
Block::edgelist::const_iterator eit;
dyn_hash_map<Address,bool> linked;
{
boost::lock_guard<Block> blockGuard(*retblk);
eit = retblk->targets().begin();
for( ; eit != retblk->targets().end(); ++eit) {
Edge * e = *eit;
linked[e->trg_addr()] = true;
}
}
boost::lock_guard<Block> g2(*_entry);
eit = _entry->sources().begin();
for( ; eit != _entry->sources().end(); ++eit) {
Edge * e = *eit;
if(e->type() == CALL) {
parsing_printf("[%s:%d] linking return edge %lx -> %lx\n",
FILE__,__LINE__,retblk->lastInsnAddr(),e->src()->end());
// XXX opportunity here to be more conservative about delayed
// determination of return status
Block * call_ft = _obj->findBlockByEntry(region(),e->src()->end());
if(!call_ft) {
parsing_printf("[%s:%d] no block found, error!\n",
FILE__,__LINE__);
}
else if(!HASHDEF(linked,call_ft->start())) {
if(call_ft == _entry)
link_entry = true;
else
o->add_edge(retblk,call_ft,RET);
linked[call_ft->start()] = true;
}
}
}
// can't do this during iteration
if(link_entry)
o->add_edge(retblk,_entry,RET);
}
void
Function::add_block(Block *b)
{
boost::lock_guard<Function> g(*this);
b->_func_cnt.fetch_add(1); // block counts references
_bmap[b->start()] = b;
}
const string &
Function::name() const
{
return _name;
}
bool
Function::contains(Block *b)
{
boost::lock_guard<Function> g(*this);
if (b == NULL) return false;
if(!_cache_valid)
finalize();
return HASHDEF(_bmap,b->start());
}
bool
Function::contains(Block *b) const
{
// boost::lock_guard<const Function> g(*this);
if (b == NULL) return false;
return HASHDEF(_bmap,b->start());
}
void Function::setEntryBlock(Block *new_entry)
{
boost::lock_guard<Function> g(*this);
obj()->parser->move_func(this, new_entry->start(), new_entry->region());
_region = new_entry->region();
_start = new_entry->start();
_entry = new_entry;
}
void Function::set_retstatus(FuncReturnStatus rs)
{
boost::lock_guard<Function> g(*this);
// If this function is a known non-returning function,
// we should ignore this result.
// A exmaple is .Unwind_Resume, which is non-returning.
// But on powerpc, the function contains a BLR instruction,
// looking like a return instruction, but actually is not.
if (obj()->cs()->nonReturning(_name) && rs != NORETURN) return;
parsing_printf("Set function %s at %lx ret status from %d to %d\n", _name.c_str(), addr(), _rs.load(), rs);
if (_rs == RETURN && rs == NORETURN) {
parsing_printf("\tERROR: ret status is already set to RETURN, now setting to NORETURN\n");
}
if (_rs == NORETURN && rs == RETURN) {
parsing_printf("\tERROR: ret status is already set to NORETURN, now setting to RETURN\n");
}
// If we are changing the return status, update prev counter
if (_rs != UNSET) {
if (_rs == NORETURN) {
_obj->cs()->decrementCounter(PARSE_NORETURN_COUNT);
} else if (_rs == RETURN) {
_obj->cs()->decrementCounter(PARSE_RETURN_COUNT);
} else if (_rs == UNKNOWN) {
_obj->cs()->decrementCounter(PARSE_UNKNOWN_COUNT);
}
}
// Update counter information
if (rs == NORETURN) {
_obj->cs()->incrementCounter(PARSE_NORETURN_COUNT);
} else if (rs == RETURN) {
_obj->cs()->incrementCounter(PARSE_RETURN_COUNT);
} else if (rs == UNKNOWN) {
_obj->cs()->incrementCounter(PARSE_UNKNOWN_COUNT);
}
// Write access is handled by the lock, so this should always work.
// Helgrind gets confused, so the cmp&swap hides the actual write.
FuncReturnStatus e = _rs;
assert(_rs.compare_exchange_strong(e, rs));
}
void
Function::removeBlock(Block* dead)
{
boost::lock_guard<Function> g(*this);
_cache_valid = false;
// specify replacement entry prior to deleting entry block, unless
// deleting all blocks
if (dead == _entry) {
mal_printf("Warning: removing entry block [%lx %lx) for function at "
"%lx\n", dead->start(), dead->end(), addr());
_entry = NULL;
assert(0);
}
// remove dead block from _retBL and _call_edge_list
boost::lock_guard<Block> g2(*dead);
const Block::edgelist & outs = dead->targets();
for (Block::edgelist::const_iterator oit = outs.begin();
outs.end() != oit;
oit++ )
{
switch((*oit)->type()) {
case CALL: {
bool foundEdge = false;
for (set<Edge*>::iterator cit = _call_edge_list.begin();
_call_edge_list.end() != cit;
cit++)
{
if (*oit == *cit) {
foundEdge = true;
_call_edge_list.erase(cit);
break;
}
}
assert(foundEdge || (*oit)->sinkEdge());
break;
}
case RET:
_retBL.erase(dead->start());
break;
default:
break;
}
}
// remove dead block from block map
_bmap.erase(dead->start());
_exitBL.erase(dead->start());
}
class ST_Predicates : public Slicer::Predicates {};
StackTamper
Function::tampersStack(bool recalculate)
{
boost::lock_guard<Function> g(*this);
using namespace SymbolicEvaluation;
using namespace InstructionAPI;
if ( ! obj()->defensiveMode() ) {
assert(0);
_tamper = TAMPER_NONE;
return _tamper;
}
// this is above the cond'n below b/c it finalizes the function,
// which could in turn call this function
Function::const_blocklist retblks(returnBlocks());
if ( retblks.begin() == retblks.end() ) {
_tamper = TAMPER_NONE;
return _tamper;
}
// The following line leads to dangling pointers, but leaving
// in until we understand why it was originally there.
//_cache_valid = false;
// if we want to re-calculate the tamper address
if (!recalculate && TAMPER_UNSET != _tamper) {
return _tamper;
}
assert(_cache_valid);
AssignmentConverter converter(true, true);
vector<Assignment::Ptr> assgns;
ST_Predicates preds;
_tamper = TAMPER_UNSET;
for (auto bit = retblks.begin(); retblks.end() != bit; ++bit) {
assert(_cache_valid);
Address retnAddr = (*bit)->lastInsnAddr();
InstructionDecoder retdec(this->isrc()->getPtrToInstruction(retnAddr),
InstructionDecoder::maxInstructionLength,
this->region()->getArch() );
Instruction retn = retdec.decode();
converter.convert(retn, retnAddr, this, *bit, assgns);
vector<Assignment::Ptr>::iterator ait;
AST::Ptr sliceAtRet;
for (ait = assgns.begin(); assgns.end() != ait; ait++) {
AbsRegion & outReg = (*ait)->out();
if ( outReg.absloc().isPC() ) {
// First check to see if an input is an unresolved stack slot
// (or worse, the heap) - since if that's the case there's no use
// in spending a lot of time slicing.
std::vector<AbsRegion>::const_iterator in_iter;
for (in_iter = (*ait)->inputs().begin();
in_iter != (*ait)->inputs().end(); ++in_iter) {
if (in_iter->type() != Absloc::Unknown) {
_tamper = TAMPER_NONZERO;
_tamper_addr = 0;
set_retstatus(NORETURN);
mal_printf("Stack tamper analysis for ret block at "
"%lx found unresolved stack slot or heap "
"addr, marking as TAMPER_NONZERO\n", retnAddr);
return _tamper;
}
}
Slicer slicer(*ait,*bit,this);
Graph::Ptr slGraph = slicer.backwardSlice(preds);
DataflowAPI::Result_t slRes;
DataflowAPI::SymEval::expand(slGraph,slRes);
sliceAtRet = slRes[*ait];
if (dyn_debug_malware && sliceAtRet != NULL) {
cerr << "assignment " << (*ait)->format() << " is "
<< sliceAtRet->format() << "\n";
}
break;
}
}
if (sliceAtRet == NULL) {
mal_printf("Failed to produce a slice for retn at %lx %s[%d]\n",
retnAddr, FILE__,__LINE__);
continue;
}
StackTamperVisitor vis(Absloc(-1 * isrc()->getAddressWidth(), 0, this));
Address curTamperAddr=0;
StackTamper curtamper = vis.tampersStack(sliceAtRet, curTamperAddr);
mal_printf("StackTamperVisitor for func at 0x%lx block[%lx %lx) w/ "
"lastInsn at 0x%lx returns tamper=%d tamperAddr=0x%lx\n",
_start, (*bit)->start(), (*bit)->end(), retnAddr,
curtamper, curTamperAddr);
if (TAMPER_UNSET == _tamper || TAMPER_NONE == _tamper ||
(TAMPER_NONZERO == _tamper &&
TAMPER_NONE != curtamper))
{
_tamper = curtamper;
_tamper_addr = curTamperAddr;
}
else if ((TAMPER_REL == _tamper || TAMPER_ABS == _tamper) &&
(TAMPER_REL == curtamper || TAMPER_ABS == curtamper))
{
if (_tamper != curtamper || _tamper_addr != curTamperAddr) {
fprintf(stderr, "WARNING! Unhandled case in stackTamper "
"analysis, func at %lx has distinct tamperAddrs "
"%d:%lx %d:%lx at different return instructions, "
"setting to TAMPER_NONZERO %s[%d]\n",
this->addr(), _tamper,_tamper_addr, curtamper,
curTamperAddr, FILE__, __LINE__);
_tamper = TAMPER_NONZERO; // let instrumentation take care of it
}
}
assgns.clear();
}
if ( TAMPER_UNSET == _tamper ) {
mal_printf("WARNING: we found no valid slices for function at %lx "
"%s[%d]\n", _start, FILE__,__LINE__);
_tamper = TAMPER_NONZERO;
}
if ( TAMPER_NONE != _tamper && TAMPER_REL != _tamper && RETURN == _rs ) {
set_retstatus(NORETURN);
}
return _tamper;
}
void Function::destroy(Function *f) {
f->obj()->destroy(f);
}
LoopTreeNode* Function::getLoopTree() const{
boost::lock_guard<const Function> g(*this);
if (_loop_root == NULL) {
LoopAnalyzer la(this);
la.createLoopHierarchy();
}
return _loop_root;
}
// this methods returns the loop objects that exist in the control flow
// grap. It returns a set. And if there are no loops, then it returns the empty
// set. not NULL.
void Function::getLoopsByNestingLevel(vector<Loop*>& lbb,
bool outerMostOnly) const
{
boost::lock_guard<const Function> g(*this);
if (_loop_analyzed == false) {
LoopAnalyzer la(this);
la.analyzeLoops();
_loop_analyzed = true;
}
for (std::set<Loop *>::iterator iter = _loops.begin();
iter != _loops.end(); ++iter) {
// if we are only getting the outermost loops
if (outerMostOnly &&
(*iter)->parentLoop() != NULL) continue;
lbb.push_back(*iter);
}
return;
}
// get all the loops in this flow graph
bool
Function::getLoops(vector<Loop*>& lbb) const
{
boost::lock_guard<const Function> g(*this);
getLoopsByNestingLevel(lbb, false);
return true;
}
// get the outermost loops in this flow graph
bool
Function::getOuterLoops(vector<Loop*>& lbb) const
{
boost::lock_guard<const Function> g(*this);
getLoopsByNestingLevel(lbb, true);
return true;
}
Loop *Function::findLoop(const char *name) const
{
boost::lock_guard<const Function> g(*this);
return getLoopTree()->findLoop(name);
}
//this method fill the dominator information of each basic block
//looking at the control flow edges. It uses a fixed point calculation
//to find the immediate dominator of the basic blocks and the set of
//basic blocks that are immediately dominated by this one.
//Before calling this method all the dominator information
//is going to give incorrect results. So first this function must
//be called to process dominator related fields and methods.
void Function::fillDominatorInfo() const
{
boost::lock_guard<const Function> g(*this);
if (!isDominatorInfoReady) {
dominatorCFG domcfg(this);
domcfg.calcDominators();
isDominatorInfoReady = true;
}
}
void Function::fillPostDominatorInfo() const
{
boost::lock_guard<const Function> g(*this);
if (!isPostDominatorInfoReady) {
dominatorCFG domcfg(this);
domcfg.calcPostDominators();
isPostDominatorInfoReady = true;
}
}
bool Function::dominates(Block* A, Block *B) const {
boost::lock_guard<const Function> g(*this);
if (A == NULL || B == NULL) return false;
if (A == B) return true;
fillDominatorInfo();
if (!immediateDominates[A]) return false;
for (auto bit = immediateDominates[A]->begin(); bit != immediateDominates[A]->end(); ++bit)
if (dominates(*bit, B)) return true;
return false;
}
Block* Function::getImmediateDominator(Block *A) const {
boost::lock_guard<const Function> g(*this);
fillDominatorInfo();
return immediateDominator[A];
}
void Function::getImmediateDominates(Block *A, set<Block*> &imd) const {
boost::lock_guard<const Function> g(*this);
fillDominatorInfo();
if (immediateDominates[A] != NULL)
imd.insert(immediateDominates[A]->begin(), immediateDominates[A]->end());
}
void Function::getAllDominates(Block *A, set<Block*> &d) const {
boost::lock_guard<const Function> g(*this);
fillDominatorInfo();
d.insert(A);
if (immediateDominates[A] == NULL) return;
for (auto bit = immediateDominates[A]->begin(); bit != immediateDominates[A]->end(); ++bit)
getAllDominates(*bit, d);
}
bool Function::postDominates(Block* A, Block *B) const {
boost::lock_guard<const Function> g(*this);
if (A == NULL || B == NULL) return false;
if (A == B) return true;
fillPostDominatorInfo();
if (!immediatePostDominates[A]) return false;
for (auto bit = immediatePostDominates[A]->begin(); bit != immediatePostDominates[A]->end(); ++bit)
if (postDominates(*bit, B)) return true;
return false;
}
Block* Function::getImmediatePostDominator(Block *A) const {
boost::lock_guard<const Function> g(*this);
fillPostDominatorInfo();
return immediatePostDominator[A];
}
void Function::getImmediatePostDominates(Block *A, set<Block*> &imd) const {
boost::lock_guard<const Function> g(*this);
fillPostDominatorInfo();
if (immediatePostDominates[A] != NULL)
imd.insert(immediatePostDominates[A]->begin(), immediatePostDominates[A]->end());
}
void Function::getAllPostDominates(Block *A, set<Block*> &d) const {
boost::lock_guard<const Function> g(*this);
fillPostDominatorInfo();
d.insert(A);
if (immediatePostDominates[A] == NULL) return;
for (auto bit = immediatePostDominates[A]->begin(); bit != immediatePostDominates[A]->end(); ++bit)
getAllPostDominates(*bit, d);
}