1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4"""
5This takes a crashing qtest trace and tries to remove superflous operations
6"""
7
8import sys
9import os
10import subprocess
11import time
12import struct
13
14QEMU_ARGS = None
15QEMU_PATH = None
16TIMEOUT = 5
17CRASH_TOKEN = None
18
19write_suffix_lookup = {"b": (1, "B"),
20                       "w": (2, "H"),
21                       "l": (4, "L"),
22                       "q": (8, "Q")}
23
24def usage():
25    sys.exit("""\
26Usage: QEMU_PATH="/path/to/qemu" QEMU_ARGS="args" {} input_trace output_trace
27By default, will try to use the second-to-last line in the output to identify
28whether the crash occred. Optionally, manually set a string that idenitifes the
29crash by setting CRASH_TOKEN=
30""".format((sys.argv[0])))
31
32deduplication_note = """\n\
33Note: While trimming the input, sometimes the mutated trace triggers a different
34type crash but indicates the same bug. Under this situation, our minimizer is
35incapable of recognizing and stopped from removing it. In the future, we may
36use a more sophisticated crash case deduplication method.
37\n"""
38
39def check_if_trace_crashes(trace, path):
40    with open(path, "w") as tracefile:
41        tracefile.write("".join(trace))
42
43    rc = subprocess.Popen("timeout -s 9 {timeout}s {qemu_path} {qemu_args} 2>&1\
44    < {trace_path}".format(timeout=TIMEOUT,
45                           qemu_path=QEMU_PATH,
46                           qemu_args=QEMU_ARGS,
47                           trace_path=path),
48                          shell=True,
49                          stdin=subprocess.PIPE,
50                          stdout=subprocess.PIPE,
51                          encoding="utf-8")
52    global CRASH_TOKEN
53    if CRASH_TOKEN is None:
54        try:
55            outs, _ = rc.communicate(timeout=5)
56            CRASH_TOKEN = " ".join(outs.splitlines()[-2].split()[0:3])
57        except subprocess.TimeoutExpired:
58            print("subprocess.TimeoutExpired")
59            return False
60        print("Identifying Crashes by this string: {}".format(CRASH_TOKEN))
61        global deduplication_note
62        print(deduplication_note)
63        return True
64
65    for line in iter(rc.stdout.readline, ""):
66        if "CLOSED" in line:
67            return False
68        if CRASH_TOKEN in line:
69            return True
70
71    print("\nWarning:")
72    print("  There is no 'CLOSED'or CRASH_TOKEN in the stdout of subprocess.")
73    print("  Usually this indicates a different type of crash.\n")
74    return False
75
76
77def minimize_trace(inpath, outpath):
78    global TIMEOUT
79    with open(inpath) as f:
80        trace = f.readlines()
81    start = time.time()
82    if not check_if_trace_crashes(trace, outpath):
83        sys.exit("The input qtest trace didn't cause a crash...")
84    end = time.time()
85    print("Crashed in {} seconds".format(end-start))
86    TIMEOUT = (end-start)*5
87    print("Setting the timeout for {} seconds".format(TIMEOUT))
88
89    i = 0
90    newtrace = trace[:]
91    remove_step = 1
92    while i < len(newtrace):
93        # 1.) Try to remove lines completely and reproduce the crash.
94        # If it works, we're done.
95        if (i+remove_step) >= len(newtrace):
96            remove_step = 1
97        prior = newtrace[i:i+remove_step]
98        for j in range(i, i+remove_step):
99            newtrace[j] = ""
100        print("Removing {lines} ...".format(lines=prior))
101        if check_if_trace_crashes(newtrace, outpath):
102            i += remove_step
103            # Double the number of lines to remove for next round
104            remove_step *= 2
105            continue
106        # Failed to remove multiple IOs, fast recovery
107        if remove_step > 1:
108            for j in range(i, i+remove_step):
109                newtrace[j] = prior[j-i]
110            remove_step = 1
111            continue
112        newtrace[i] = prior[0] # remove_step = 1
113        # 2.) Try to replace write{bwlq} commands with a write addr, len
114        # command. Since this can require swapping endianness, try both LE and
115        # BE options. We do this, so we can "trim" the writes in (3)
116        if (newtrace[i].startswith("write") and not
117            newtrace[i].startswith("write ")):
118            suffix = newtrace[i].split()[0][-1]
119            assert(suffix in write_suffix_lookup)
120            addr = int(newtrace[i].split()[1], 16)
121            value = int(newtrace[i].split()[2], 16)
122            for endianness in ['<', '>']:
123                data = struct.pack("{end}{size}".format(end=endianness,
124                                   size=write_suffix_lookup[suffix][1]),
125                                   value)
126                newtrace[i] = "write {addr} {size} 0x{data}\n".format(
127                    addr=hex(addr),
128                    size=hex(write_suffix_lookup[suffix][0]),
129                    data=data.hex())
130                if(check_if_trace_crashes(newtrace, outpath)):
131                    break
132            else:
133                newtrace[i] = prior[0]
134
135        # 3.) If it is a qtest write command: write addr len data, try to split
136        # it into two separate write commands. If splitting the write down the
137        # middle does not work, try to move the pivot "left" and retry, until
138        # there is no space left. The idea is to prune unneccessary bytes from
139        # long writes, while accommodating arbitrary MemoryRegion access sizes
140        # and alignments.
141        if newtrace[i].startswith("write "):
142            addr = int(newtrace[i].split()[1], 16)
143            length = int(newtrace[i].split()[2], 16)
144            data = newtrace[i].split()[3][2:]
145            if length > 1:
146                leftlength = int(length/2)
147                rightlength = length - leftlength
148                newtrace.insert(i+1, "")
149                while leftlength > 0:
150                    newtrace[i] = "write {addr} {size} 0x{data}\n".format(
151                            addr=hex(addr),
152                            size=hex(leftlength),
153                            data=data[:leftlength*2])
154                    newtrace[i+1] = "write {addr} {size} 0x{data}\n".format(
155                            addr=hex(addr+leftlength),
156                            size=hex(rightlength),
157                            data=data[leftlength*2:])
158                    if check_if_trace_crashes(newtrace, outpath):
159                        break
160                    else:
161                        leftlength -= 1
162                        rightlength += 1
163                if check_if_trace_crashes(newtrace, outpath):
164                    i -= 1
165                else:
166                    newtrace[i] = prior[0]
167                    del newtrace[i+1]
168        i += 1
169    check_if_trace_crashes(newtrace, outpath)
170
171
172if __name__ == '__main__':
173    if len(sys.argv) < 3:
174        usage()
175
176    QEMU_PATH = os.getenv("QEMU_PATH")
177    QEMU_ARGS = os.getenv("QEMU_ARGS")
178    if QEMU_PATH is None or QEMU_ARGS is None:
179        usage()
180    # if "accel" not in QEMU_ARGS:
181    #     QEMU_ARGS += " -accel qtest"
182    CRASH_TOKEN = os.getenv("CRASH_TOKEN")
183    QEMU_ARGS += " -qtest stdio -monitor none -serial none "
184    minimize_trace(sys.argv[1], sys.argv[2])
185