-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchecker9.py
executable file
·192 lines (128 loc) · 5.74 KB
/
checker9.py
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
#!/usr/bin/python3
'''SOSP23-Paper21: '''
'''Checker9: F_start --> S_G|O --> F_end'''
import os
import configparser
import json
from utils import *
g_record_file = 'checker9.log'
def checker9(func_str, args_apis, global_apis, file_name, g_logger):
ast_f = build_ast_from_str(func_str)
if ast_f=='': return
has_arg = 0
has_global = 0
arg_var_type = {}
global_var_type = {}
for n in ast_f.node_set.keys():
cur_node = ast_f.node_set[n]
src = cur_node.dot_src
if cur_node.type==AST_NODE_TYPE_PARAM:
struct = src.split(',')[1].split(')')[0].strip()
if struct.find('struct')!=-1 and len(struct.split(' '))>=2:
#print('Find ARG Node: %s'%(struct))
struct_type = struct.split(' ')[1]
var = struct.split(' ')[-1]
if var[0]=='*':
var = var[1:]
if struct_type in args_apis.keys():
#print('Target Func: %s'%(src))
has_arg = 1
if not (var in arg_var_type.keys()):
arg_var_type[var] = struct_type
for k in global_apis.keys():
if src.find(k)!=-1:
has_global = 1
global_var_type[k] = k
if has_arg == 1 or has_global==1:
#print('Begin Analyzing...')
for n in ast_f.node_set.keys():
cur_node = ast_f.node_set[n]
src = cur_node.dot_src
if cur_node.type==AST_NODE_TYPE_OP_ASSIGN:
if len(src.split('='))!=3:
continue
#print(src)
right_hand = src.split('=')[2].strip().split(')')[0].strip()
#print('RIGHT-HAND: %s'%(right_hand))
if (right_hand in arg_var_type.keys()) or (right_hand in global_var_type.keys()):
#print('%s %s %s'%('+'*10, file_name, '+'*10))
#print('AS: %s'%(cur_node.dot_src))
if right_hand in arg_var_type.keys():
api = args_apis[arg_var_type[right_hand]]
if right_hand in global_var_type.keys():
api = global_apis[global_var_type[right_hand]]
root_node = cur_node
while root_node.type!=AST_NODE_TYPE_METHOD:
root_node = root_node.parent[0]
node_set = [root_node]
has_get = 0
while len(node_set)!=0:
t = node_set.pop()
for nb in t.nb:
node_set.append(nb)
if t.line<cur_node.line:
if t.dot_src.find(api)!=-1:
has_get = 1
break
if has_get == 0:
print('%s %s %s'%('+'*10, file_name, '+'*10))
g_logger.write('%s %s %s\n'%('+'*10, file_name, '+'*10))
print('Find Bug: %s'%(cur_node.dot_src))
g_logger.write('Find Bug: %s\n'%(cur_node.dot_src))
g_logger.flush()
def run_checker():
g_logger = open(g_record_file, 'w')
config = configparser.ConfigParser()
config.read('checkers.ini', encoding='utf-8')
#secs = config.sections()
#print(secs)
apis = ''
ast_kernel_functions_file = config.get('global', 'ast_kernel_functions')
if config.has_section('future_escape'):
#print('yes')
apis = config.get('future_escape', 'apis')[1:-1].split(',')
else:
print('no section *future_escape* for checker9')
return -1
args_apis = {}
global_apis = {}
for api in apis:
api = api.strip()
struct = api.split(':')[1].strip()
get_api = api.split(':')[2].strip()
if api[0]=='a':
if struct in args_apis.keys():
print(' Has Already This ARG Key: %s'%(struct))
exit(-1)
else:
print('ARG: %s - %s'%(struct, get_api))
args_apis[struct]=get_api
if api[0]=='g':
if struct in global_apis.keys():
print(' Has Already This GLOBAL Key: %s'%(struct))
exit(-1)
else:
print('GLOBAL: %s - %s'%(struct, get_api))
#global_apis[struct]=get_api
#return
i = 0
with open(ast_kernel_functions_file) as f:
while True:
l = f.readline()
if l=='': break
l = l.strip()
if l=='': continue
dot_file = l.split(' ')[0].split(':')[1]
#dot_file = '/data1/linux-ast/0/+data1+struck-linux+arch+powerpc+platforms+powernv+opal.c+outdir/45-ast.dot'
#print('Process --- %s'%dot_file)
with open(dot_file) as df:
file_str = df.read()
checker9(file_str, args_apis, global_apis, l, g_logger)
#break
i+=1
if i%10000==0:
print(i)
#if i>50000: break
g_logger.close()
if __name__ == '__main__':
run_checker()