forked from Rhapsod/sapienz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
380 lines (305 loc) · 12.8 KB
/
main.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
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
# Copyright (c) 2016-present, Ke Mao. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# * The names of the contributors may not be used to endorse or
# promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import random
import os
import sys
import pickle
import datetime
import subprocess
import platform
import numpy
from deap import creator, base, tools
from algorithms import eaMuPlusLambdaParallel
import settings
from coverages import emma_coverage
from coverages import ella_coverage
from coverages import act_coverage
from plot import two_d_line
from devices import emulator
from crashes import crash_handler
from analysers import static_analyser
from init import initRepeatParallel
class CanNotInitSeqException(Exception):
pass
# get one test suite by running multiple times of MotifCore
def get_suite(device, apk_dir, package_name):
ret = []
unique_crashes = set()
for i in range(0, settings.SUITE_SIZE):
# get_sequence may return empty sequence
seq = []
repeated = 0
while len(seq) <= 2:
seq = get_sequence(device, apk_dir, package_name, i, unique_crashes)
repeated += 1
if repeated > 20:
raise CanNotInitSeqException("Cannot get sequence via MotifCore.")
ret.append(seq)
return ret
### helper functions
# get one event sequence by running revised motifcore
# note: the luanch activity is started by emma instrument
def get_sequence(device, apk_dir, package_name, index, unique_crashes):
std_out_file = apk_dir + "/intermediate/" + "output.stdout"
random.seed()
motifcore_events = random.randint(settings.SEQUENCE_LENGTH_MIN, settings.SEQUENCE_LENGTH_MAX)
ret = []
# clear data
os.system("adb -s " + device + " shell pm clear " + package_name)
# start motifcore
print "... Start generating a sequence"
# command = Command("adb -s " + device + " shell motifcore -p " + package_name + " -v --throttle " + str(
# settings.THROTTLE) + " " + str(motifcore_events))
# command.run(timeout=600)
cmd = "adb -s " + device + " shell motifcore -p " + package_name + " --ignore-crashes --ignore-security-exceptions --ignore-timeouts --bugreport --string-seeding /mnt/sdcard/" + package_name + "_strings.xml -v " + str(
motifcore_events)
os.system(settings.TIMEOUT_CMD + " " + str(settings.EVAL_TIMEOUT) + " " + cmd)
# need to kill motifcore when timeout
kill_motifcore_cmd = "shell ps | awk '/com\.android\.commands\.motifcore/ { system(\"adb -s " + device + " shell kill \" $2) }'"
os.system("adb -s " + device + " " + kill_motifcore_cmd)
print "... Finish generating a sequence"
# access the generated script, should ignore the first launch activity
script_name = settings.MOTIFCORE_SCRIPT_PATH.split("/")[-1]
ts = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S.%f")[:-3]
os.system(
"adb -s " + device + " pull " + settings.MOTIFCORE_SCRIPT_PATH + " " + apk_dir + "/intermediate/" + script_name + ".init." + ts + "." + str(
index))
script = open(apk_dir + "/intermediate/" + script_name + ".init." + ts + "." + str(index))
is_content = False
is_skipped_first = False
for line in script:
line = line.strip()
if line.find("start data >>") != -1:
is_content = True
continue
if is_content and line != "":
if is_skipped_first == False:
is_skipped_first = True
continue
if is_skipped_first:
ret.append(line)
script.close()
# deal with crash
crash_handler.handle(device, apk_dir, apk_dir + "/intermediate/" + script_name + ".init." + ts + "." + str(index),
"init", ts, index, unique_crashes)
return ret
# generate individual by running motifcore
def gen_individual(device, apk_dir, package_name):
if settings.DEBUG:
print "Generate Individual on device, ", device
suite = get_suite(device, apk_dir, package_name)
return (creator.Individual(suite), device)
# the suite coverage is accumulated
def eval_suite(individual, device, apk_dir, package_name, gen, pop):
# for get_motifcore_suite_coverage
script_path = []
# for length objective
suite_lengths = []
for index, seq in enumerate(individual):
# generate script file list
script = open(apk_dir + "/intermediate/motifcore.evo.script." + str(gen) + "." + str(pop) + "." + str(index), "w")
script.write(settings.MOTIFCORE_SCRIPT_HEADER)
length = 0
for line in seq:
script.write(line + "\n")
length += 1
suite_lengths.append(length)
script.close()
script_path.append(os.path.abspath(
apk_dir + "/intermediate/motifcore.evo.script." + str(gen) + "." + str(pop) + "." + str(index)))
# give a script and package, return the coverage by running all seqs
if apk_dir.endswith(".apk_output"):
coverage, num_crashes = act_coverage.get_suite_coverage(script_path, device, apk_dir, package_name, gen, pop)
else:
coverage, num_crashes = emma_coverage.get_suite_coverage(script_path, device, apk_dir, package_name, gen, pop)
print "### Coverage = ", coverage
print "### Lengths = ", suite_lengths
print "### #Crashes = ", num_crashes
# 1st obj: coverage, 2nd: average seq length of the suite, 3nd: #crashes
return pop, (coverage, numpy.mean(suite_lengths), num_crashes), device
def mut_suite(individual, indpb):
# shuffle seq
individual, = tools.mutShuffleIndexes(individual, indpb)
# crossover inside the suite
for i in range(1, len(individual), 2):
if random.random() < settings.MUTPB:
if len(individual[i - 1]) <= 2:
print "\n\n### Indi Length =", len(individual[i - 1]), " ith = ", i - 1, individual[i - 1]
continue # sys.exit(1)
if len(individual[i]) <= 2:
print "\n\n### Indi Length =", len(individual[i]), "ith = ", i, individual[i]
continue # sys.exit(1)
individual[i - 1], individual[i] = tools.cxOnePoint(individual[i - 1], individual[i])
# shuffle events
for i in range(len(individual)):
if random.random() < settings.MUTPB:
if len(individual[i]) <= 2:
print "\n\n### Indi Length =", len(individual[i]), "ith = ", i, individual[i]
continue # sys.exit(1)
individual[i], = tools.mutShuffleIndexes(individual[i], indpb)
return individual,
def return_as_is(a):
return a
def initRepeat(container, func, n, device, apk_dir, package_name):
return container(func(device, apk_dir, package_name) for _ in xrange(n))
### deap framework setup
creator.create("FitnessCovLen", base.Fitness, weights=(10.0, -0.5, 1000.0))
creator.create("Individual", list, fitness=creator.FitnessCovLen)
toolbox = base.Toolbox()
toolbox.register("individual", gen_individual)
toolbox.register("population", initRepeatParallel.initPop, list, toolbox.individual)
toolbox.register("evaluate", eval_suite)
# mate crossover two suites
toolbox.register("mate", tools.cxUniform, indpb=0.5)
# mutate should change seq order in the suite as well
toolbox.register("mutate", mut_suite, indpb=0.5)
# toolbox.register("select", tools.selTournament, tournsize=5)
toolbox.register("select", tools.selNSGA2)
# log the history
history = tools.History()
# Decorate the variation operators
toolbox.decorate("mate", history.decorator)
toolbox.decorate("mutate", history.decorator)
def get_package_name(path):
apk_path = None
if path.endswith(".apk"):
apk_path = path
else:
for file_name in os.listdir(path + "/bin"):
if file_name == "bugroid-instrumented.apk":
apk_path = path + "/bin/bugroid-instrumented.apk"
break
elif file_name.endswith("-debug.apk"):
apk_path = path + "/bin/" + file_name
assert apk_path is not None
get_package_cmd = "aapt d xmltree " + apk_path + " AndroidManifest.xml | grep package= | awk 'BEGIN {FS=\"\\\"\"}{print $2}'"
# print get_package_cmd
package_name = subprocess.Popen(get_package_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0].strip()
return package_name, apk_path
def main(instrumented_app_dir):
"""
Test one apk
:param instrumented_app_dir: The instrumentation folder of the app | apk file path for closed-source app
"""
host_system = platform.system()
if host_system == "Darwin":
print "Running on Mac OS"
settings.TIMEOUT_CMD = "gtimeout"
elif host_system == "Linux":
print "Running on Linux"
else:
print "Runnning on unknown OS"
package_name, apk_path = get_package_name(instrumented_app_dir)
# for css subjects
if instrumented_app_dir.endswith(".apk"):
instrumented_app_dir += "_output"
os.system("mkdir " + instrumented_app_dir)
print "### Working on apk:", package_name
# get emulator device
print "Preparing devices ..."
emulator.boot_devices()
emulator.prepare_motifcore()
emulator.clean_sdcard()
# log the devices
devices = emulator.get_devices()
# static analysis
if settings.ENABLE_STRING_SEEDING:
output_dir = None
if instrumented_app_dir.endswith(".apk_output"):
output_dir = instrumented_app_dir
else:
output_dir = instrumented_app_dir + "/bin"
static_analyser.decode_apk(apk_path, output_dir)
# will use dummy 0 if disabled
for device in devices:
decoded_dir = None
if instrumented_app_dir.endswith(".apk_output"):
decoded_dir = instrumented_app_dir + "/" + apk_path.split("/")[-1].split(".apk")[0]
else:
decoded_dir = instrumented_app_dir + "/bin/" + apk_path.split("/")[-1].split(".apk")[0]
static_analyser.upload_string_xml(device, decoded_dir, package_name)
os.system("adb -s " + device + " shell rm /mnt/sdcard/bugreport.crash")
os.system("adb -s " + device + " uninstall " + package_name)
os.system("adb -s " + device + " install " + apk_path)
# intermediate should be in app folder
os.system("rm -rf " + instrumented_app_dir + "/intermediate")
os.system("mkdir " + instrumented_app_dir + "/intermediate")
os.system("rm -rf " + instrumented_app_dir + "/crashes")
os.system("mkdir " + instrumented_app_dir + "/crashes")
os.system("rm -rf " + instrumented_app_dir + "/coverages")
os.system("mkdir " + instrumented_app_dir + "/coverages")
# generate initial population
print "### Initialising population ...."
population = toolbox.population(n=settings.POPULATION_SIZE, apk_dir=instrumented_app_dir,
package_name=package_name)
print "### Individual Lengths: "
for indi in population:
for seq in indi:
print len(seq),
print ""
history.update(population)
# hof = tools.HallOfFame(6)
# pareto front can be large, there is a similarity option parameter
hof = tools.ParetoFront()
stats = tools.Statistics(lambda ind: ind.fitness.values)
# axis = 0, the numpy.mean will return an array of results
stats.register("avg", numpy.mean, axis=0)
stats.register("std", numpy.std, axis=0)
stats.register("min", numpy.min, axis=0)
stats.register("max", numpy.max, axis=0)
stats.register("pop_fitness", return_as_is)
# evolve
print "\n\n\n### Start to Evolve"
population, logbook = eaMuPlusLambdaParallel.evolve(population, toolbox, settings.POPULATION_SIZE,
settings.OFFSPRING_SIZE,
cxpb=settings.CXPB, mutpb=settings.MUTPB,
ngen=settings.GENERATION,
apk_dir=instrumented_app_dir,
package_name=package_name,
stats=stats, halloffame=hof, verbose=True)
# persistent
logbook_file = open(instrumented_app_dir + "/intermediate/logbook.pickle", 'wb')
pickle.dump(logbook, logbook_file)
logbook_file.close()
hof_file = open(instrumented_app_dir + "/intermediate/hof.pickle", 'wb')
pickle.dump(hof, hof_file)
hof_file.close()
history_file = open(instrumented_app_dir + "/intermediate/history.pickle", 'wb')
pickle.dump(history, history_file)
history_file.close()
# draw graph
two_d_line.plot(logbook, 0, instrumented_app_dir)
two_d_line.plot(logbook, 1, instrumented_app_dir)
two_d_line.plot(logbook, 2, instrumented_app_dir)
# draw history network
# history_network.plot(history, instrumented_app_dir)
if __name__ == "__main__":
app_dir = sys.argv[1]
main(app_dir)