-
Notifications
You must be signed in to change notification settings - Fork 4
/
check_error_handling.py
240 lines (212 loc) · 7.68 KB
/
check_error_handling.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
# -*- coding: utf-8 -*-
"""Tool for automated testing of applications using Error Sanitizer."""
# Copyright (c) 2020, Samsung Electronics Co., Ltd. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Author: Jakub Botwicz <j.botwicz@samsung.com>
#
import argparse
import os
from subprocess import Popen, PIPE
import sys
MAIN_LINE_SEPARATOR = "\n" + 80 * "=" + "\n"
"""String used as major separator."""
LINE_SEPARATOR = "\n" + 80 * "-" + "\n"
"""String used as minor separator."""
MAX_FAIL_NR = 9999999
"""Maximum number of failures in ESAN map."""
MAP_SIZE_BYTES = int(MAX_FAIL_NR / 8)
"""Size of ESAN failure map in bytes."""
def create_map_file(map_size, fail_num, data, filename):
"""Create file with generated ESAN map."""
byte_nr = int(fail_num / 8)
byte_pos = int(fail_num % 8)
result = 1 << (byte_pos)
output = (
b"\x00" * (byte_nr)
+ result.to_bytes(1, "big")
+ b"\x00" * (map_size - byte_nr)
+ bytes("XXXX", "ascii")
+ data
)
with open(filename, "wb") as the_file:
the_file.write(output)
def run_process_popen_gdb(esan_path, app_path, test_map, before_args="", after_args=""):
"""Run gdb of tested application with given parameters."""
app_string = (
"gdb -batch -ex='run' -ex='backtrace' -ex='backtrace full' --args env LD_PRELOAD="
+ esan_path
+ " "
+ app_path
+ " "
+ before_args
+ " "
+ test_map
+ " "
+ after_args
)
app_string = app_string.replace(" ", " ").strip()
print(f"\nReproduction string: {app_string}\n")
os.system(app_string + " > gdb_dump.txt")
with open("gdb_dump.txt", "r") as the_file:
gdb_result = the_file.read()
os.remove("gdb_dump.txt")
print(f"gdb output: {LINE_SEPARATOR}{gdb_result}{LINE_SEPARATOR}")
return (None, None, 0)
def run_process_popen(esan_path, app_path, test_map, before_args="", after_args=""):
"""Run tested application with given parameters."""
app_string = app_path + " " + before_args + " " + test_map + " " + after_args
app_string = app_string.replace(" ", " ").strip()
print(f"Reproduction string: LD_PRELOAD={esan_path} {app_string}\n")
process = Popen(
app_string.split(" "),
bufsize=0,
stdout=PIPE,
stderr=PIPE,
shell=False,
env={"LD_PRELOAD": esan_path},
)
(stdout_pipe, stderr_pipe) = process.communicate()
exit_code = process.wait()
if stdout_pipe:
print(f"stdout:{LINE_SEPARATOR}{stdout_pipe.decode('ascii')}{LINE_SEPARATOR}")
if stderr_pipe:
print(f"stderr:{LINE_SEPARATOR}{stderr_pipe.decode('ascii')}{LINE_SEPARATOR}")
print(f"Exit_code: {exit_code}\n")
return (stdout_pipe, stderr_pipe, exit_code)
def perform_testing(
esan_path,
app_path,
testfile_path,
before_args="",
after_args="",
start_iteration=0,
finish_iteration=sys.maxsize,
):
"""Perform testing of application using ESAN."""
print(f"\nApplication in test: {app_path}\n{MAIN_LINE_SEPARATOR}")
with open(testfile_path, "rb") as the_file:
test_payload = the_file.read()
print("Starting dry run (with always_succeed map):\n")
create_map_file(MAX_FAIL_NR, MAX_FAIL_NR + 1, test_payload, "temp.cur_input")
(_, stderr_pipe, exit_code) = run_process_popen(
esan_path,
app_path,
"temp.cur_input",
after_args=after_args,
before_args=before_args,
)
output_str = str(stderr_pipe)
total_index = output_str.rfind("TOTAL")
if total_index == -1:
print("Could not find ESAN stats!")
sys.exit(-1)
total_str = output_str[total_index:]
total_value = int(total_str.split("|")[1].strip())
print(f"Total numbers of executions to test: {total_value}\n{MAIN_LINE_SEPARATOR}")
for i in range(start_iteration, min(total_value, finish_iteration)):
print(f"{MAIN_LINE_SEPARATOR}\nStarting run nr {i}:\n")
test_map = "temp_" + str(i) + ".cur_input"
create_map_file(10 * total_value, i, test_payload, test_map)
(_, stderr_pipe, exit_code) = run_process_popen(
esan_path,
app_path,
test_map,
after_args=after_args,
before_args=before_args,
)
if exit_code != 0:
print(
f"Tested application failed with map nr {i} - exit code: {exit_code}."
)
(_, stderr_pipe, exit_code) = run_process_popen_gdb(
esan_path,
app_path,
test_map,
after_args=after_args,
before_args=before_args,
)
def main(args):
"""Get input parameters and initiate testing."""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description="Tool for checking error handling using the Error Sanitizer.\n\n"
+ "At the beginning the tool verifies number of executions of hooked "
+ "functions using the always_succeed map.\nAfterwards it checks single "
+ "failure maps starting from the first occurence to the last one.\n"
+ "In case of a crash, the application is executed again using gdb, "
+ "to receive full stack dump before the crash.\n\n"
+ "For example, if you want to check execution of the following application: "
+ "'diff -a -d test_000.xml test_001.xml -l'\n"
+ "You should use: python check_error_handling.py $(ESAN_PATH)/error_sanitizer_preload.so "
+ "diff test_000.xml -B '-a -d' -A 'test_001.xml -l'",
)
parser.add_argument(
"esan_path",
metavar="ESAN_PATH",
type=str,
help="Full path to ESAN executable (error_sanitizer_preload.so)",
)
parser.add_argument(
"app_path", metavar="APP_PATH", type=str, help="Path to tested executable"
)
parser.add_argument(
"test_file",
metavar="TESTFILE_PATH",
type=str,
help="Path to main test file (file that will be used for provided failure map)",
)
parser.add_argument(
"--before_args",
"-B",
type=str,
default="",
help="Parameters for tested executable provided BEFORE main test file",
)
parser.add_argument(
"--after_args",
"-A",
type=str,
default="",
help="Parameters for tested executable provided AFTER main test file",
)
parser.add_argument(
"--start",
"-S",
metavar="START_NR",
type=int,
default=0,
help="Start value for failure map iteration (nr of hook that will fail) - default: 0",
)
parser.add_argument(
"--final",
"-F",
metavar="FINAL_NR",
type=int,
default=MAX_FAIL_NR,
help=f"Final value for failure map iteration (see: --start) - default: {MAX_FAIL_NR}",
)
args = parser.parse_args()
perform_testing(
args.esan_path,
args.app_path,
args.test_file,
args.before_args,
args.after_args,
args.start,
args.final,
)
if __name__ == "__main__":
main(sys.argv[1:])