-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOracle.py
More file actions
333 lines (245 loc) · 11.8 KB
/
Oracle.py
File metadata and controls
333 lines (245 loc) · 11.8 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
import copy
import time
import logging
import numpy as np
import Mutator
from config import ORACLE_SEARCH_BUDGET, BUG_CONFIRMATION_BUDGET
from abc import ABC, abstractmethod
logger = logging.getLogger("fuzz_logger")
class Oracle(ABC):
def __init__(self, game, rand_seed, de_dup=None):
self.game = game
self.rng = np.random.default_rng(rand_seed)
self.de_dup = de_dup
if game.env_iden == "highway":
orcl_mut_bdgt = 2 # remove or add 2 cars
self.mutator = Mutator.HighwayOracleMutator(game, orcl_mut_bdgt)
elif game.env_iden == "lunar":
self.mutator = Mutator.LunarOracleMoonHeightMutator(game)
elif game.env_iden == "bipedal":
self.mutator = Mutator.BipedalEasyOracleMutator(game)
@abstractmethod
def explore(self, fuzz_seed):
pass
def setRandAndState(self, env_state, rand_state=None, rand_seed=None):
# random state is either restored to original or resetted with rand_seed
if rand_seed is not None:
if self.game.env_iden == "highway":
idl_rng = np.random.default_rng(rand_seed)
self.game.set_state(env_state, idl_rng)
else:
self.game.env.seed(rand_seed)
self.game.set_state(env_state) # highway: [street, v])
else:
self.game.set_state(env_state, rand_state)
class MMBugOracle(Oracle):
def __init__(self, game, rand_seed):
super().__init__(game, rand_seed)
def explore(self, fuzz_seed):
if self.game.env_iden == "bipedal":
print("This oracle is not suitable for BipedalWalker environment as it takes too much time. Thus, we did not include this experiment in the paper. If you are curious to try this, remove this condition and consider decreasing confirmation budget.")
exit()
org_w_cnt = 0
# below loop corresponds to line 2 in Algorithm 1
for rand_seed in range(BUG_CONFIRMATION_BUDGET):
self.setRandAndState(fuzz_seed.hi_lvl_state, rand_seed=rand_seed)
org_rew, _, _ = self.game.play(fuzz_seed.data)
if org_rew == 100: org_w_cnt += 1
if org_w_cnt == BUG_CONFIRMATION_BUDGET:
logger.info("Skipping seed %d as agent wins with every random seed on the current state (easier)." % fuzz_seed.identifier)
return 0
num_rejects = 0
for _ in range(ORACLE_SEARCH_BUDGET):
mut_state = self.mutator.mutate(fuzz_seed, self.rng, mode='unrelax')
if mut_state is None:
num_rejects += 1
continue
mut_w_cnt = 0
# below loop corresponds to line 5 in Algorithm 1
for rand_seed in range(BUG_CONFIRMATION_BUDGET):
self.setRandAndState(mut_state, rand_seed=rand_seed)
nn_state, _, _ = self.game.get_state()
mut_rew, _, _ = self.game.play(nn_state)
if mut_rew == 100: mut_w_cnt += 1
# below condition effectively corresponds to line 6 in Algorithm 1
if mut_w_cnt > org_w_cnt:
return 1 # bug found
# optimization
if (BUG_CONFIRMATION_BUDGET - rand_seed - 1) + mut_w_cnt <= org_w_cnt:
break
logger.info("%d out of %d (un)relaxation is rejected on fuzz seed %d." % (num_rejects, ORACLE_SEARCH_BUDGET, fuzz_seed.identifier))
return 0 # bug not found
class MMSeedBugBasicOracle(Oracle):
def __init__(self, game, rand_seed):
super().__init__(game, rand_seed)
def explore(self, fuzz_seed):
self.setRandAndState(fuzz_seed.hi_lvl_state, rand_state=fuzz_seed.rand_state)
org_reward, _, _ = self.game.play(fuzz_seed.data)
if org_reward == 0: fuzz_seed.is_crash = True
elif org_reward == 100:
logger.info("Skipping fuzz seed %d as agent wins on there (easier state)." % fuzz_seed.identifier)
return 0
num_rejects = 0
for _ in range(ORACLE_SEARCH_BUDGET):
mut_state = self.mutator.mutate(fuzz_seed, self.rng, mode='unrelax')
if mut_state is None:
num_rejects += 1
continue
self.setRandAndState(mut_state, rand_state=fuzz_seed.rand_state)
nn_state, _, _ = self.game.get_state()
mut_reward, _, _ = self.game.play(nn_state)
if mut_reward > org_reward:
return 1
logger.info("%d out of %d (un)relaxation is rejected on fuzz seed %d." % (num_rejects, ORACLE_SEARCH_BUDGET, fuzz_seed.identifier))
return 0
class MMSeedBugExtOracle(Oracle):
def __init__(self, game, rand_seed):
super().__init__(game, rand_seed)
def explore(self, fuzz_seed):
self.setRandAndState(fuzz_seed.hi_lvl_state, rand_state=fuzz_seed.rand_state)
org_reward, _, _ = self.game.play(fuzz_seed.data)
if org_reward == 0:
fuzz_seed.is_crash = True
logger.info("Skipping fuzz seed %d as agent loses on there (harder state)." % fuzz_seed.identifier)
return 0
num_bugs = 0
num_rejects = 0
for _ in range(ORACLE_SEARCH_BUDGET):
mut_state = self.mutator.mutate(fuzz_seed, self.rng, mode='relax')
if mut_state is None:
num_rejects += 1
continue
self.setRandAndState(mut_state, rand_state=fuzz_seed.rand_state)
nn_state, _, _ = self.game.get_state()
mut_reward, _, _ = self.game.play(nn_state)
if mut_reward < org_reward:
num_bugs += 1
logger.info("%d out of %d (un)relaxation is rejected on fuzz seed %d." % (num_rejects, ORACLE_SEARCH_BUDGET, fuzz_seed.identifier))
return num_bugs
class MMSeedBug2BugOracle(Oracle):
def __init__(self, game, rand_seed):
super().__init__(game, rand_seed)
self.rand_seed = rand_seed
def explore(self, fuzz_seed):
mmseedbugoracle = MMSeedBugBasicOracle(self.game, self.rand_seed)
is_seed_bug = mmseedbugoracle.explore(fuzz_seed)
if is_seed_bug == 1:
mmbugoracle = MMBugOracle(self.game, self.rand_seed)
is_bug = mmbugoracle.explore(fuzz_seed)
if is_bug == 1: return 1
return 0
class FailureSeedBugOracle(Oracle):
def __init__(self, game, rand_seed):
super().__init__(game, rand_seed)
def explore(self, fuzz_seed):
self.setRandAndState(fuzz_seed.hi_lvl_state, fuzz_seed.rand_state)
org_reward, _, _ = self.game.play(fuzz_seed.data)
if org_reward == 0:
fuzz_seed.is_crash = True
return 1
return 0
class RuleSeedBugOracle(Oracle):
def __init__(self, game, rand_seed):
super().__init__(game, rand_seed)
def explore(self, fuzz_seed):
self.setRandAndState(fuzz_seed.hi_lvl_state, fuzz_seed.rand_state)
org_reward, org_play, _ = self.game.play(fuzz_seed.data)
if org_reward == 100:
logger.info("Skipping fuzz seed %d as agent wins on there (easier state)." % fuzz_seed.identifier)
return 0
num_rejects = 0
for _ in range(ORACLE_SEARCH_BUDGET):
mut_state = self.mutator.mutate(fuzz_seed, self.rng, mode='unrelax')
if mut_state is None:
num_rejects += 1
continue
self.setRandAndState(mut_state, rand_state=fuzz_seed.rand_state)
nn_state, _, _ = self.game.get_state()
mut_reward, mut_play, _ = self.game.play(nn_state)
# should be winning the game. we return only true positives for this oracle
if mut_reward == 100 and mut_reward > org_reward:
# rule: if there is policy that can succeed in harder state, it should take the same action on the easier state
if self.game.env_iden == "bipedal":
if list(mut_play[0]) != list(org_play[0]):
return 1
else:
if mut_play[0] != org_play[0]:
return 1
logger.info("%d out of %d (un)relaxation is rejected on fuzz seed %d." % (num_rejects, ORACLE_SEARCH_BUDGET, fuzz_seed.identifier))
return 0
class PerfectOracle(Oracle):
def perf_oracle(self, env):
envs = [env]
while len(envs) != 0:
env = envs.pop()
for action in env.applicable_actions():
current_env = copy.deepcopy(env)
reward, _, done = current_env.step(action)
if done:
if reward > 0:
return True
else:
continue
envs.append(current_env)
return False
class PerfectSeedBugOracle(PerfectOracle):
def __init__(self, game, rand_seed):
super().__init__(game, rand_seed)
def explore(self, fuzz_seed):
if not self.game.env_iden == "highway":
print("This oracle is only suitable for Highway!")
exit()
self.setRandAndState(fuzz_seed.hi_lvl_state, fuzz_seed.rand_state)
org_reward, _, _ = self.game.play(fuzz_seed.data)
# if the agent is already winning then no need for exploration
if org_reward <= 0:
self.setRandAndState(fuzz_seed.hi_lvl_state, fuzz_seed.rand_state)
if self.perf_oracle(self.game.env):
return 1
return 0
class PerfectBugOracle(PerfectOracle):
def __init__(self, game, rand_seed):
super().__init__(game, rand_seed)
def explore(self, fuzz_seed):
if not self.game.env_iden == "highway":
print("This oracle is only suitable for Highway!")
exit()
num_perfect_fails = 0
for rs in range(BUG_CONFIRMATION_BUDGET):
self.setRandAndState(fuzz_seed.hi_lvl_state, rand_seed=rs)
if not self.perf_oracle(self.game.env): num_perfect_fails += 1
num_policy_fails = 0
for rs in range(BUG_CONFIRMATION_BUDGET):
self.setRandAndState(fuzz_seed.hi_lvl_state, rand_seed=rs)
rew, _, _ = self.game.play(fuzz_seed.data)
if rew == 0: num_policy_fails += 1
if num_policy_fails > num_perfect_fails:
return 1
return 0
class LunarApproxPerfectSeedBugOracle(PerfectOracle):
def __init__(self, game, rand_seed):
super().__init__(game, rand_seed)
def explore(self, fuzz_seed):
if not self.game.env_iden == "lunar":
print("This oracle implementation is only suitable for Lunar!")
exit()
env_states = [(fuzz_seed.hi_lvl_state, fuzz_seed.rand_state, 'dummy')]
winning_acts = []
start_time = time.perf_counter()
while len(env_states) > 0:
c_hls, rand_st, wa = env_states.pop()
winning_acts.append(wa)
for act in range(4): # there are 4 available actions in lunar
self.setRandAndState(c_hls, rand_state=rand_st)
_, _, done, info = self.game.env.step(act)
if done:
if info['leg_contact'][0] and info['leg_contact'][1]:
print(winning_acts)
return 0 # the state is winnable
else:
_, n_hls, rand_st = self.game.get_state()
env_states.append( (n_hls, rand_st, act) )
# 900 seconds is a hyperparameter
if (time.perf_counter() - start_time) > 900:
return 2 # timeout
return 1 # crash is inevitable