blob: e19afdb717ece9d9dc454db687cbc3e490fb7598 [file] [log] [blame]
Kostya Serebryanyb4150322013-11-15 11:51:08 +00001#!/usr/bin/env python
2# Merge or print the coverage data collected by asan's coverage.
3# Input files are sequences of 4-byte integers.
4# We need to merge these integers into a set and then
5# either print them (as hex) or dump them into another file.
6import array
Timur Iskhodzhanov882bc562015-04-01 14:46:10 +00007import bisect
8import glob
9import os.path
Sergey Matveev6cb47a082014-05-19 12:53:03 +000010import struct
Sergey Matveevc2de3462015-05-06 20:48:29 +000011import subprocess
Kostya Serebryanyb4150322013-11-15 11:51:08 +000012import sys
13
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000014prog_name = ""
Kostya Serebryanyb4150322013-11-15 11:51:08 +000015
16def Usage():
17 print >> sys.stderr, "Usage: \n" + \
Sergey Matveevc2de3462015-05-06 20:48:29 +000018 " " + prog_name + " merge FILE [FILE...] > OUTPUT\n" \
19 " " + prog_name + " print FILE [FILE...]\n" \
20 " " + prog_name + " unpack FILE [FILE...]\n" \
21 " " + prog_name + " rawunpack FILE [FILE ...]\n" \
22 " " + prog_name + " missing BINARY < LIST_OF_PCS\n"
Kostya Serebryanyb4150322013-11-15 11:51:08 +000023 exit(1)
24
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000025def CheckBits(bits):
26 if bits != 32 and bits != 64:
Bill Seurer92760a32015-03-25 14:56:02 +000027 raise Exception("Wrong bitness: %d" % bits)
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000028
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000029def TypeCodeForBits(bits):
30 CheckBits(bits)
31 return 'L' if bits == 64 else 'I'
32
Sagar Thakurb1c51f62016-04-22 09:20:22 +000033def TypeCodeForStruct(bits):
34 CheckBits(bits)
35 return 'Q' if bits == 64 else 'I'
36
Kostya Serebryanyeaec5b62015-03-19 19:52:30 +000037kMagic32SecondHalf = 0xFFFFFF32;
38kMagic64SecondHalf = 0xFFFFFF64;
39kMagicFirstHalf = 0xC0BFFFFF;
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000040
41def MagicForBits(bits):
42 CheckBits(bits)
Bill Seurer92760a32015-03-25 14:56:02 +000043 if sys.byteorder == 'little':
44 return [kMagic64SecondHalf if bits == 64 else kMagic32SecondHalf, kMagicFirstHalf]
45 else:
46 return [kMagicFirstHalf, kMagic64SecondHalf if bits == 64 else kMagic32SecondHalf]
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000047
Kostya Serebryanydcb54db2015-03-19 21:01:27 +000048def ReadMagicAndReturnBitness(f, path):
Kostya Serebryanyeaec5b62015-03-19 19:52:30 +000049 magic_bytes = f.read(8)
50 magic_words = struct.unpack('II', magic_bytes);
51 bits = 0
Bill Seurer92760a32015-03-25 14:56:02 +000052 idx = 1 if sys.byteorder == 'little' else 0
53 if magic_words[idx] == kMagicFirstHalf:
54 if magic_words[1-idx] == kMagic64SecondHalf:
Kostya Serebryanyeaec5b62015-03-19 19:52:30 +000055 bits = 64
Bill Seurer92760a32015-03-25 14:56:02 +000056 elif magic_words[1-idx] == kMagic32SecondHalf:
Kostya Serebryanyeaec5b62015-03-19 19:52:30 +000057 bits = 32
58 if bits == 0:
Kostya Serebryanydcb54db2015-03-19 21:01:27 +000059 raise Exception('Bad magic word in %s' % path)
Kostya Serebryanyeaec5b62015-03-19 19:52:30 +000060 return bits
61
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000062def ReadOneFile(path):
Sergey Matveev6cb47a082014-05-19 12:53:03 +000063 with open(path, mode="rb") as f:
64 f.seek(0, 2)
65 size = f.tell()
66 f.seek(0, 0)
Kostya Serebryanydcb54db2015-03-19 21:01:27 +000067 if size < 8:
68 raise Exception('File %s is short (< 8 bytes)' % path)
69 bits = ReadMagicAndReturnBitness(f, path)
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000070 size -= 8
Sagar Thakurb1c51f62016-04-22 09:20:22 +000071 s = struct.unpack_from(TypeCodeForStruct(bits) * (size * 8 / bits), f.read(size))
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000072 print >>sys.stderr, "%s: read %d %d-bit PCs from %s" % (prog_name, size * 8 / bits, bits, path)
Kostya Serebryanyb4150322013-11-15 11:51:08 +000073 return s
74
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000075def Merge(files):
Kostya Serebryanyb4150322013-11-15 11:51:08 +000076 s = set()
77 for f in files:
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000078 s = s.union(set(ReadOneFile(f)))
Kostya Serebryanyb4150322013-11-15 11:51:08 +000079 print >> sys.stderr, "%s: %d files merged; %d PCs total" % \
80 (prog_name, len(files), len(s))
81 return sorted(s)
82
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000083def PrintFiles(files):
Kostya Serebryanycba49d42015-03-18 00:23:44 +000084 if len(files) > 1:
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000085 s = Merge(files)
Kostya Serebryanycba49d42015-03-18 00:23:44 +000086 else: # If there is just on file, print the PCs in order.
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000087 s = ReadOneFile(files[0])
Kostya Serebryanydcb54db2015-03-19 21:01:27 +000088 print >> sys.stderr, "%s: 1 file merged; %d PCs total" % \
89 (prog_name, len(s))
Kostya Serebryanyb4150322013-11-15 11:51:08 +000090 for i in s:
91 print "0x%x" % i
92
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000093def MergeAndPrint(files):
Kostya Serebryanyb4150322013-11-15 11:51:08 +000094 if sys.stdout.isatty():
95 Usage()
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000096 s = Merge(files)
97 bits = 32
Kostya Serebryany2d56aba2015-03-18 22:03:39 +000098 if max(s) > 0xFFFFFFFF:
99 bits = 64
Kostya Serebryanydcb54db2015-03-19 21:01:27 +0000100 array.array('I', MagicForBits(bits)).tofile(sys.stdout)
Sagar Thakurb1c51f62016-04-22 09:20:22 +0000101 a = struct.pack(TypeCodeForStruct(bits) * len(s), *s)
102 sys.stdout.write(a)
Kostya Serebryanyb4150322013-11-15 11:51:08 +0000103
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000104
105def UnpackOneFile(path):
106 with open(path, mode="rb") as f:
107 print >> sys.stderr, "%s: unpacking %s" % (prog_name, path)
108 while True:
109 header = f.read(12)
110 if not header: return
111 if len(header) < 12:
112 break
113 pid, module_length, blob_size = struct.unpack('iII', header)
114 module = f.read(module_length)
115 blob = f.read(blob_size)
116 assert(len(module) == module_length)
117 assert(len(blob) == blob_size)
118 extracted_file = "%s.%d.sancov" % (module, pid)
119 print >> sys.stderr, "%s: extracting %s" % \
120 (prog_name, extracted_file)
121 # The packed file may contain multiple blobs for the same pid/module
122 # pair. Append to the end of the file instead of overwriting.
123 with open(extracted_file, 'ab') as f2:
124 f2.write(blob)
125 # fail
126 raise Exception('Error reading file %s' % path)
127
128
129def Unpack(files):
130 for f in files:
131 UnpackOneFile(f)
132
Kostya Serebryany2d56aba2015-03-18 22:03:39 +0000133def UnpackOneRawFile(path, map_path):
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000134 mem_map = []
135 with open(map_path, mode="rt") as f_map:
136 print >> sys.stderr, "%s: reading map %s" % (prog_name, map_path)
Kostya Serebryany2d56aba2015-03-18 22:03:39 +0000137 bits = int(f_map.readline())
138 if bits != 32 and bits != 64:
Kostya Serebryany9f1243e2015-03-17 22:09:19 +0000139 raise Exception('Wrong bits size in the map')
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000140 for line in f_map:
141 parts = line.rstrip().split()
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000142 mem_map.append((int(parts[0], 16),
143 int(parts[1], 16),
144 int(parts[2], 16),
Evgeniy Stepanov937afa12014-06-03 15:25:43 +0000145 ' '.join(parts[3:])))
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000146 mem_map.sort(key=lambda m : m[0])
147 mem_map_keys = [m[0] for m in mem_map]
148
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000149 with open(path, mode="rb") as f:
150 print >> sys.stderr, "%s: unpacking %s" % (prog_name, path)
151
152 f.seek(0, 2)
153 size = f.tell()
154 f.seek(0, 0)
Sagar Thakurb1c51f62016-04-22 09:20:22 +0000155 pcs = struct.unpack_from(TypeCodeForStruct(bits) * (size * 8 / bits), f.read(size))
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000156 mem_map_pcs = [[] for i in range(0, len(mem_map))]
157
158 for pc in pcs:
159 if pc == 0: continue
160 map_idx = bisect.bisect(mem_map_keys, pc) - 1
161 (start, end, base, module_path) = mem_map[map_idx]
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000162 assert pc >= start
163 if pc >= end:
164 print >> sys.stderr, "warning: %s: pc %x outside of any known mapping" % (prog_name, pc)
165 continue
166 mem_map_pcs[map_idx].append(pc - base)
167
168 for ((start, end, base, module_path), pc_list) in zip(mem_map, mem_map_pcs):
169 if len(pc_list) == 0: continue
170 assert path.endswith('.sancov.raw')
171 dst_path = module_path + '.' + os.path.basename(path)[:-4]
Evgeniy Stepanovb7238342014-12-25 16:03:24 +0000172 print >> sys.stderr, "%s: writing %d PCs to %s" % (prog_name, len(pc_list), dst_path)
Sagar Thakurb1c51f62016-04-22 09:20:22 +0000173 sorted_pc_list = sorted(pc_list)
174 pc_buffer = struct.pack(TypeCodeForStruct(bits) * len(pc_list), *sorted_pc_list)
175 with open(dst_path, 'ab+') as f2:
Kostya Serebryanydcb54db2015-03-19 21:01:27 +0000176 array.array('I', MagicForBits(bits)).tofile(f2)
Sagar Thakurb1c51f62016-04-22 09:20:22 +0000177 f2.seek(0, 2)
178 f2.write(pc_buffer)
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000179
Kostya Serebryany2d56aba2015-03-18 22:03:39 +0000180def RawUnpack(files):
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000181 for f in files:
182 if not f.endswith('.sancov.raw'):
183 raise Exception('Unexpected raw file name %s' % f)
184 f_map = f[:-3] + 'map'
Kostya Serebryany2d56aba2015-03-18 22:03:39 +0000185 UnpackOneRawFile(f, f_map)
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000186
Sergey Matveevc2de3462015-05-06 20:48:29 +0000187def GetInstrumentedPCs(binary):
Sergey Matveevf57fb6c2015-05-12 16:46:54 +0000188 # This looks scary, but all it does is extract all offsets where we call:
189 # - __sanitizer_cov() or __sanitizer_cov_with_check(),
190 # - with call or callq,
191 # - directly or via PLT.
Sergey Matveevc2de3462015-05-06 20:48:29 +0000192 cmd = "objdump -d %s | " \
Sergey Matveevf57fb6c2015-05-12 16:46:54 +0000193 "grep '^\s\+[0-9a-f]\+:.*\scall\(q\|\)\s\+[0-9a-f]\+ <__sanitizer_cov\(_with_check\|\)\(@plt\|\)>' | " \
Sergey Matveevc2de3462015-05-06 20:48:29 +0000194 "grep '^\s\+[0-9a-f]\+' -o" % binary
195 proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
196 shell=True)
197 proc.stdin.close()
198 # The PCs we get from objdump are off by 4 bytes, as they point to the
199 # beginning of the callq instruction. Empirically this is true on x86 and
200 # x86_64.
201 return set(int(line.strip(), 16) + 4 for line in proc.stdout)
202
203def PrintMissing(binary):
204 if not os.path.isfile(binary):
205 raise Exception('File not found: %s' % binary)
206 instrumented = GetInstrumentedPCs(binary)
207 print >> sys.stderr, "%s: found %d instrumented PCs in %s" % (prog_name,
208 len(instrumented),
209 binary)
210 covered = set(int(line, 16) for line in sys.stdin)
211 print >> sys.stderr, "%s: read %d PCs from stdin" % (prog_name, len(covered))
212 missing = instrumented - covered
213 print >> sys.stderr, "%s: %d PCs missing from coverage" % (prog_name, len(missing))
214 if (len(missing) > len(instrumented) - len(covered)):
215 print >> sys.stderr, \
216 "%s: WARNING: stdin contains PCs not found in binary" % prog_name
217 for pc in sorted(missing):
218 print "0x%x" % pc
219
Kostya Serebryanyb4150322013-11-15 11:51:08 +0000220if __name__ == '__main__':
221 prog_name = sys.argv[0]
Kostya Serebryany2d56aba2015-03-18 22:03:39 +0000222 if len(sys.argv) <= 2:
Kostya Serebryanyb4150322013-11-15 11:51:08 +0000223 Usage();
Kostya Serebryany9f1243e2015-03-17 22:09:19 +0000224
Sergey Matveevc2de3462015-05-06 20:48:29 +0000225 if sys.argv[1] == "missing":
226 if len(sys.argv) != 3:
227 Usage()
228 PrintMissing(sys.argv[2])
229 exit(0)
230
Timur Iskhodzhanov882bc562015-04-01 14:46:10 +0000231 file_list = []
232 for f in sys.argv[2:]:
233 file_list += glob.glob(f)
234 if not file_list:
235 Usage()
236
Kostya Serebryany2d56aba2015-03-18 22:03:39 +0000237 if sys.argv[1] == "print":
Timur Iskhodzhanov882bc562015-04-01 14:46:10 +0000238 PrintFiles(file_list)
Kostya Serebryany2d56aba2015-03-18 22:03:39 +0000239 elif sys.argv[1] == "merge":
Timur Iskhodzhanov882bc562015-04-01 14:46:10 +0000240 MergeAndPrint(file_list)
Kostya Serebryany2d56aba2015-03-18 22:03:39 +0000241 elif sys.argv[1] == "unpack":
Timur Iskhodzhanov882bc562015-04-01 14:46:10 +0000242 Unpack(file_list)
Kostya Serebryany2d56aba2015-03-18 22:03:39 +0000243 elif sys.argv[1] == "rawunpack":
Timur Iskhodzhanov882bc562015-04-01 14:46:10 +0000244 RawUnpack(file_list)
Kostya Serebryanyb4150322013-11-15 11:51:08 +0000245 else:
246 Usage()