]>
git.proxmox.com Git - rustc.git/blob - src/compiler-rt/lib/sanitizer_common/scripts/sancov.py
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.
17 print >> sys
.stderr
, "Usage: \n" + \
18 " " + 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"
26 if bits
!= 32 and bits
!= 64:
27 raise Exception("Wrong bitness: %d" % bits
)
29 def TypeCodeForBits(bits
):
31 return 'L' if bits
== 64 else 'I'
33 kMagic32SecondHalf
= 0xFFFFFF32;
34 kMagic64SecondHalf
= 0xFFFFFF64;
35 kMagicFirstHalf
= 0xC0BFFFFF;
37 def MagicForBits(bits
):
39 if sys
.byteorder
== 'little':
40 return [kMagic64SecondHalf
if bits
== 64 else kMagic32SecondHalf
, kMagicFirstHalf
]
42 return [kMagicFirstHalf
, kMagic64SecondHalf
if bits
== 64 else kMagic32SecondHalf
]
44 def ReadMagicAndReturnBitness(f
, path
):
45 magic_bytes
= f
.read(8)
46 magic_words
= struct
.unpack('II', magic_bytes
);
48 idx
= 1 if sys
.byteorder
== 'little' else 0
49 if magic_words
[idx
] == kMagicFirstHalf
:
50 if magic_words
[1-idx
] == kMagic64SecondHalf
:
52 elif magic_words
[1-idx
] == kMagic32SecondHalf
:
55 raise Exception('Bad magic word in %s' % path
)
58 def ReadOneFile(path
):
59 with
open(path
, mode
="rb") as f
:
64 raise Exception('File %s is short (< 8 bytes)' % path
)
65 bits
= ReadMagicAndReturnBitness(f
, path
)
67 s
= array
.array(TypeCodeForBits(bits
), f
.read(size
))
68 print >>sys
.stderr
, "%s: read %d %d-bit PCs from %s" % (prog_name
, size
* 8 / bits
, bits
, path
)
74 s
= s
.union(set(ReadOneFile(f
)))
75 print >> sys
.stderr
, "%s: %d files merged; %d PCs total" % \
76 (prog_name
, len(files
), len(s
))
79 def PrintFiles(files
):
82 else: # If there is just on file, print the PCs in order.
83 s
= ReadOneFile(files
[0])
84 print >> sys
.stderr
, "%s: 1 file merged; %d PCs total" % \
89 def MergeAndPrint(files
):
90 if sys
.stdout
.isatty():
94 if max(s
) > 0xFFFFFFFF:
96 array
.array('I', MagicForBits(bits
)).tofile(sys
.stdout
)
97 a
= array
.array(TypeCodeForBits(bits
), s
)
101 def UnpackOneFile(path
):
102 with
open(path
, mode
="rb") as f
:
103 print >> sys
.stderr
, "%s: unpacking %s" % (prog_name
, path
)
106 if not header
: return
109 pid
, module_length
, blob_size
= struct
.unpack('iII', header
)
110 module
= f
.read(module_length
)
111 blob
= f
.read(blob_size
)
112 assert(len(module
) == module_length
)
113 assert(len(blob
) == blob_size
)
114 extracted_file
= "%s.%d.sancov" % (module
, pid
)
115 print >> sys
.stderr
, "%s: extracting %s" % \
116 (prog_name
, extracted_file
)
117 # The packed file may contain multiple blobs for the same pid/module
118 # pair. Append to the end of the file instead of overwriting.
119 with
open(extracted_file
, 'ab') as f2
:
122 raise Exception('Error reading file %s' % path
)
129 def UnpackOneRawFile(path
, map_path
):
131 with
open(map_path
, mode
="rt") as f_map
:
132 print >> sys
.stderr
, "%s: reading map %s" % (prog_name
, map_path
)
133 bits
= int(f_map
.readline())
134 if bits
!= 32 and bits
!= 64:
135 raise Exception('Wrong bits size in the map')
137 parts
= line
.rstrip().split()
138 mem_map
.append((int(parts
[0], 16),
141 ' '.join(parts
[3:])))
142 mem_map
.sort(key
=lambda m
: m
[0])
143 mem_map_keys
= [m
[0] for m
in mem_map
]
145 with
open(path
, mode
="rb") as f
:
146 print >> sys
.stderr
, "%s: unpacking %s" % (prog_name
, path
)
151 pcs
= array
.array(TypeCodeForBits(bits
), f
.read(size
))
152 mem_map_pcs
= [[] for i
in range(0, len(mem_map
))]
156 map_idx
= bisect
.bisect(mem_map_keys
, pc
) - 1
157 (start
, end
, base
, module_path
) = mem_map
[map_idx
]
160 print >> sys
.stderr
, "warning: %s: pc %x outside of any known mapping" % (prog_name
, pc
)
162 mem_map_pcs
[map_idx
].append(pc
- base
)
164 for ((start
, end
, base
, module_path
), pc_list
) in zip(mem_map
, mem_map_pcs
):
165 if len(pc_list
) == 0: continue
166 assert path
.endswith('.sancov.raw')
167 dst_path
= module_path
+ '.' + os
.path
.basename(path
)[:-4]
168 print >> sys
.stderr
, "%s: writing %d PCs to %s" % (prog_name
, len(pc_list
), dst_path
)
169 arr
= array
.array(TypeCodeForBits(bits
))
170 arr
.fromlist(sorted(pc_list
))
171 with
open(dst_path
, 'ab') as f2
:
172 array
.array('I', MagicForBits(bits
)).tofile(f2
)
175 def RawUnpack(files
):
177 if not f
.endswith('.sancov.raw'):
178 raise Exception('Unexpected raw file name %s' % f
)
179 f_map
= f
[:-3] + 'map'
180 UnpackOneRawFile(f
, f_map
)
182 def GetInstrumentedPCs(binary
):
183 # This looks scary, but all it does is extract all offsets where we call:
184 # - __sanitizer_cov() or __sanitizer_cov_with_check(),
185 # - with call or callq,
186 # - directly or via PLT.
187 cmd
= "objdump -d %s | " \
188 "grep '^\s\+[0-9a-f]\+:.*\scall\(q\|\)\s\+[0-9a-f]\+ <__sanitizer_cov\(_with_check\|\)\(@plt\|\)>' | " \
189 "grep '^\s\+[0-9a-f]\+' -o" % binary
190 proc
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
,
193 # The PCs we get from objdump are off by 4 bytes, as they point to the
194 # beginning of the callq instruction. Empirically this is true on x86 and
196 return set(int(line
.strip(), 16) + 4 for line
in proc
.stdout
)
198 def PrintMissing(binary
):
199 if not os
.path
.isfile(binary
):
200 raise Exception('File not found: %s' % binary
)
201 instrumented
= GetInstrumentedPCs(binary
)
202 print >> sys
.stderr
, "%s: found %d instrumented PCs in %s" % (prog_name
,
205 covered
= set(int(line
, 16) for line
in sys
.stdin
)
206 print >> sys
.stderr
, "%s: read %d PCs from stdin" % (prog_name
, len(covered
))
207 missing
= instrumented
- covered
208 print >> sys
.stderr
, "%s: %d PCs missing from coverage" % (prog_name
, len(missing
))
209 if (len(missing
) > len(instrumented
) - len(covered
)):
210 print >> sys
.stderr
, \
211 "%s: WARNING: stdin contains PCs not found in binary" % prog_name
212 for pc
in sorted(missing
):
215 if __name__
== '__main__':
216 prog_name
= sys
.argv
[0]
217 if len(sys
.argv
) <= 2:
220 if sys
.argv
[1] == "missing":
221 if len(sys
.argv
) != 3:
223 PrintMissing(sys
.argv
[2])
227 for f
in sys
.argv
[2:]:
228 file_list
+= glob
.glob(f
)
232 if sys
.argv
[1] == "print":
233 PrintFiles(file_list
)
234 elif sys
.argv
[1] == "merge":
235 MergeAndPrint(file_list
)
236 elif sys
.argv
[1] == "unpack":
238 elif sys
.argv
[1] == "rawunpack":