-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tabulation.cpp
135 lines (114 loc) · 2.85 KB
/
Tabulation.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
/* Copyright (C), 2016-present, Sourcebrella, Inc Ltd - All rights reserved.
* Unauthorized copying, using, modifying of this file, via any medium is
* strictly prohibited, proprietary, and confidential.
*
* Author:
* Qingkai Shi <[email protected]>
*
* File Description:
*
*
* Creation Date: 2021/7/10
* Modification History:
**/
#include "Tabulation.h"
#include "ProgressBar.h"
#include <csignal>
#include <unistd.h>
static bool timeout = false;
static void alarm_handler(int param) {
timeout = true;
}
Tabulation::Tabulation(Graph &g) : vfg(g) {
}
bool Tabulation::reach(int s, int t) {
if (visited.count(s))
return false;
if (s == t)
return true;
visited.insert(s);
auto& edges = vfg.out_edges(s);
for (auto successor : edges) {
if (is_call(s, successor)) {
// visit the func body
if (reach_func(successor, t))
return true;
} else {
if (reach(successor, t))
return true;
}
}
return false;
}
bool Tabulation::reach_func(int s, int t) {
if (func_visited.count(s))
return false;
if (s == t)
return true;
func_visited.insert(s);
auto& edges = vfg.out_edges(s);
for (auto successor : edges) {
if (is_return(s, successor)) {
continue;
} else {
if (reach_func(successor, t))
return true;
}
}
return false;
}
bool Tabulation::is_call(int s, int t) {
return vfg.label(s, t) > 0;
}
bool Tabulation::is_return(int s, int t) {
return vfg.label(s, t) < 0;
}
void Tabulation::traverse(int s, std::set<int>& tc) {
if (visited.count(s))
return;
if (timeout)
return;
visited.insert(s);
tc.insert(s);
auto& edges = vfg.out_edges(s);
for (auto successor : edges) {
if (is_call(s, successor)) {
// visit the func body
traverse_func(successor, tc);
} else {
traverse(successor, tc);
}
}
}
void Tabulation::traverse_func(int s, std::set<int>& tc) {
if (func_visited.count(s))
return;
if (timeout)
return;
func_visited.insert(s);
tc.insert(s);
auto& edges = vfg.out_edges(s);
for (auto successor : edges) {
if (is_return(s, successor)) {
continue;
} else {
traverse_func(successor, tc);
}
}
}
double Tabulation::tc() {
signal(SIGALRM, alarm_handler);
timeout = false;
alarm(3600 * 6);
ProgressBar bar(vfg.num_vertices());
double ret = 0;
std::map<int, std::set<int>> tc;
for (int i = 0; i < vfg.num_vertices(); ++i) {
visited.clear();
func_visited.clear();
traverse(i, tc[i]);
ret += (tc[i].size()) * sizeof(int);
bar.update();
}
return ret / 1024.0 / 1024.0;
}