1//===-- sancov.cpp --------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8// This file is a command-line tool for reading and analyzing sanitizer
9// coverage.
10//===----------------------------------------------------------------------===//
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/StringExtras.h"
13#include "llvm/ADT/Twine.h"
14#include "llvm/DebugInfo/Symbolize/Symbolize.h"
15#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCDisassembler/MCDisassembler.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCInstrAnalysis.h"
20#include "llvm/MC/MCInstrInfo.h"
21#include "llvm/MC/MCObjectFileInfo.h"
22#include "llvm/MC/MCRegisterInfo.h"
23#include "llvm/MC/MCSubtargetInfo.h"
24#include "llvm/MC/MCTargetOptions.h"
25#include "llvm/MC/TargetRegistry.h"
26#include "llvm/Object/Archive.h"
27#include "llvm/Object/Binary.h"
28#include "llvm/Object/COFF.h"
29#include "llvm/Object/MachO.h"
30#include "llvm/Object/ObjectFile.h"
31#include "llvm/Object/XCOFFObjectFile.h"
32#include "llvm/Option/ArgList.h"
33#include "llvm/Option/Option.h"
34#include "llvm/Support/Casting.h"
35#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Driver.h"
37#include "llvm/Support/Errc.h"
38#include "llvm/Support/ErrorOr.h"
39#include "llvm/Support/FileSystem.h"
40#include "llvm/Support/JSON.h"
41#include "llvm/Support/MD5.h"
42#include "llvm/Support/MemoryBuffer.h"
43#include "llvm/Support/Path.h"
44#include "llvm/Support/Regex.h"
45#include "llvm/Support/SHA1.h"
46#include "llvm/Support/SourceMgr.h"
47#include "llvm/Support/SpecialCaseList.h"
48#include "llvm/Support/TargetSelect.h"
49#include "llvm/Support/VirtualFileSystem.h"
50#include "llvm/Support/YAMLParser.h"
51#include "llvm/Support/raw_ostream.h"
52
53#include <set>
54#include <vector>
55
56using namespace llvm;
57
58namespace {
59
60// Command-line option boilerplate.
61namespace {
62using namespace llvm::opt;
63enum ID {
64 OPT_INVALID = 0, // This is not an option ID.
65#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
66#include "Opts.inc"
67#undef OPTION
68};
69
70#define OPTTABLE_CODE
71#include "Opts.inc"
72
73class SancovOptTable : public opt::OptTable {
74public:
75 SancovOptTable() : OptTable(optionTables()) {}
76};
77} // namespace
78
79// --------- COMMAND LINE FLAGS ---------
80
81enum ActionType {
82 CoveredFunctionsAction,
83 DiffAction,
84 HtmlReportAction,
85 MergeAction,
86 NotCoveredFunctionsAction,
87 PrintAction,
88 PrintCovPointsAction,
89 StatsAction,
90 SymbolizeAction,
91 UnionAction
92};
93
94static ActionType Action;
95static std::vector<std::string> ClInputFiles;
96static bool ClDemangle;
97static bool ClSkipDeadFiles;
98static bool ClUseDefaultIgnorelist;
99static std::string ClStripPathPrefix;
100static std::string ClIgnorelist;
101static std::string ClOutputFile;
102
103static const char *const DefaultIgnorelistStr = "fun:__sanitizer_.*\n"
104 "src:/usr/include/.*\n"
105 "src:.*/libc\\+\\+/.*\n";
106
107// --------- FORMAT SPECIFICATION ---------
108
109struct FileHeader {
110 uint32_t Bitness;
111 uint32_t Magic;
112};
113
114static const uint32_t BinCoverageMagic = 0xC0BFFFFF;
115static const uint32_t Bitness32 = 0xFFFFFF32;
116static const uint32_t Bitness64 = 0xFFFFFF64;
117
118static const Regex SancovFileRegex("(.*)\\.[0-9]+\\.sancov");
119static const Regex SymcovFileRegex(".*\\.symcov");
120
121// --------- MAIN DATASTRUCTURES ----------
122
123// Contents of .sancov file: list of coverage point addresses that were
124// executed.
125struct RawCoverage {
126 explicit RawCoverage(std::unique_ptr<std::set<uint64_t>> Addrs,
127 FileHeader Header)
128 : Addrs(std::move(Addrs)), Header(Header) {}
129
130 // Read binary .sancov file.
131 static ErrorOr<std::unique_ptr<RawCoverage>>
132 read(const std::string &FileName);
133
134 // Write binary .sancov file.
135 static void write(const std::string &FileName, const RawCoverage &Coverage);
136
137 std::unique_ptr<std::set<uint64_t>> Addrs;
138 FileHeader Header;
139};
140
141// Coverage point has an opaque Id and corresponds to multiple source locations.
142struct CoveragePoint {
143 explicit CoveragePoint(const std::string &Id) : Id(Id) {}
144
145 std::string Id;
146 SmallVector<DILineInfo, 1> Locs;
147};
148
149// Symcov file content: set of covered Ids plus information about all available
150// coverage points.
151struct SymbolizedCoverage {
152 // Read json .symcov file.
153 static std::unique_ptr<SymbolizedCoverage> read(const std::string &InputFile);
154
155 std::set<std::string> CoveredIds;
156 std::string BinaryHash;
157 std::vector<CoveragePoint> Points;
158};
159
160struct CoverageStats {
161 size_t AllPoints;
162 size_t CovPoints;
163 size_t AllFns;
164 size_t CovFns;
165};
166
167// --------- ERROR HANDLING ---------
168
169static void fail(const llvm::Twine &E) {
170 errs() << "ERROR: " << E << "\n";
171 exit(status: 1);
172}
173
174static void failIf(bool B, const llvm::Twine &E) {
175 if (B)
176 fail(E);
177}
178
179static void failIfError(std::error_code Error) {
180 if (!Error)
181 return;
182 errs() << "ERROR: " << Error.message() << "(" << Error.value() << ")\n";
183 exit(status: 1);
184}
185
186template <typename T> static void failIfError(const ErrorOr<T> &E) {
187 failIfError(E.getError());
188}
189
190static void failIfError(Error Err) {
191 if (Err) {
192 logAllUnhandledErrors(E: std::move(Err), OS&: errs(), ErrorBanner: "ERROR: ");
193 exit(status: 1);
194 }
195}
196
197template <typename T> static void failIfError(Expected<T> &E) {
198 failIfError(E.takeError());
199}
200
201static void failIfNotEmpty(const llvm::Twine &E) {
202 if (E.str().empty())
203 return;
204 fail(E);
205}
206
207template <typename T>
208static void failIfEmpty(const std::unique_ptr<T> &Ptr,
209 const std::string &Message) {
210 if (Ptr.get())
211 return;
212 fail(E: Message);
213}
214
215// ----------- Coverage I/O ----------
216template <typename T>
217static void readInts(const char *Start, const char *End,
218 std::set<uint64_t> *Ints) {
219 const T *S = reinterpret_cast<const T *>(Start);
220 const T *E = reinterpret_cast<const T *>(End);
221 std::copy(S, E, std::inserter(x&: *Ints, i: Ints->end()));
222}
223
224ErrorOr<std::unique_ptr<RawCoverage>>
225RawCoverage::read(const std::string &FileName) {
226 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
227 MemoryBuffer::getFile(Filename: FileName);
228 if (!BufOrErr)
229 return BufOrErr.getError();
230 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
231 if (Buf->getBufferSize() < 8) {
232 errs() << "File too small (<8): " << Buf->getBufferSize() << '\n';
233 return make_error_code(E: errc::illegal_byte_sequence);
234 }
235 const FileHeader *Header =
236 reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
237
238 if (Header->Magic != BinCoverageMagic) {
239 errs() << "Wrong magic: " << Header->Magic << '\n';
240 return make_error_code(E: errc::illegal_byte_sequence);
241 }
242
243 auto Addrs = std::make_unique<std::set<uint64_t>>();
244
245 switch (Header->Bitness) {
246 case Bitness64:
247 readInts<uint64_t>(Start: Buf->getBufferStart() + 8, End: Buf->getBufferEnd(),
248 Ints: Addrs.get());
249 break;
250 case Bitness32:
251 readInts<uint32_t>(Start: Buf->getBufferStart() + 8, End: Buf->getBufferEnd(),
252 Ints: Addrs.get());
253 break;
254 default:
255 errs() << "Unsupported bitness: " << Header->Bitness << '\n';
256 return make_error_code(E: errc::illegal_byte_sequence);
257 }
258
259 // Ignore slots that are zero, so a runtime implementation is not required
260 // to compactify the data.
261 Addrs->erase(x: 0);
262
263 return std::make_unique<RawCoverage>(args: std::move(Addrs), args: *Header);
264}
265
266// Print coverage addresses.
267raw_ostream &operator<<(raw_ostream &OS, const RawCoverage &CoverageData) {
268 for (auto Addr : *CoverageData.Addrs) {
269 OS << "0x";
270 OS.write_hex(N: Addr);
271 OS << "\n";
272 }
273 return OS;
274}
275
276// Write coverage addresses in binary format.
277void RawCoverage::write(const std::string &FileName,
278 const RawCoverage &Coverage) {
279 std::error_code EC;
280 raw_fd_ostream OS(FileName, EC, sys::fs::OF_None);
281 failIfError(Error: EC);
282
283 OS.write(Ptr: reinterpret_cast<const char *>(&Coverage.Header),
284 Size: sizeof(Coverage.Header));
285
286 switch (Coverage.Header.Bitness) {
287 case Bitness64:
288 for (auto Addr : *Coverage.Addrs) {
289 uint64_t Addr64 = Addr;
290 OS.write(Ptr: reinterpret_cast<const char *>(&Addr64), Size: sizeof(Addr64));
291 }
292 break;
293 case Bitness32:
294 for (auto Addr : *Coverage.Addrs) {
295 uint32_t Addr32 = static_cast<uint32_t>(Addr);
296 OS.write(Ptr: reinterpret_cast<const char *>(&Addr32), Size: sizeof(Addr32));
297 }
298 break;
299 default:
300 fail(E: "Unsupported bitness: " + std::to_string(val: Coverage.Header.Bitness));
301 }
302}
303
304static raw_ostream &operator<<(raw_ostream &OS, const CoverageStats &Stats) {
305 OS << "all-edges: " << Stats.AllPoints << "\n";
306 OS << "cov-edges: " << Stats.CovPoints << "\n";
307 OS << "all-functions: " << Stats.AllFns << "\n";
308 OS << "cov-functions: " << Stats.CovFns << "\n";
309 return OS;
310}
311
312// Output symbolized information for coverage points in JSON.
313// Format:
314// {
315// '<file_name>' : {
316// '<function_name>' : {
317// '<point_id'> : '<line_number>:'<column_number'.
318// ....
319// }
320// }
321// }
322static void operator<<(json::OStream &W,
323 const std::vector<CoveragePoint> &Points) {
324 // Group points by file.
325 std::map<std::string, std::vector<const CoveragePoint *>> PointsByFile;
326 for (const auto &Point : Points) {
327 for (const DILineInfo &Loc : Point.Locs) {
328 PointsByFile[Loc.FileName].push_back(x: &Point);
329 }
330 }
331
332 for (const auto &P : PointsByFile) {
333 std::string FileName = P.first;
334 std::map<std::string, std::vector<const CoveragePoint *>> PointsByFn;
335 for (auto PointPtr : P.second) {
336 for (const DILineInfo &Loc : PointPtr->Locs) {
337 PointsByFn[Loc.FunctionName].push_back(x: PointPtr);
338 }
339 }
340
341 W.attributeObject(Key: P.first, Contents: [&] {
342 // Group points by function.
343 for (const auto &P : PointsByFn) {
344 std::string FunctionName = P.first;
345 std::set<std::string> WrittenIds;
346
347 W.attributeObject(Key: FunctionName, Contents: [&] {
348 for (const CoveragePoint *Point : P.second) {
349 for (const auto &Loc : Point->Locs) {
350 if (Loc.FileName != FileName || Loc.FunctionName != FunctionName)
351 continue;
352 if (!WrittenIds.insert(x: Point->Id).second)
353 continue;
354
355 // Output <point_id> : "<line>:<col>".
356 W.attribute(Key: Point->Id,
357 Contents: (utostr(X: Loc.Line) + ":" + utostr(X: Loc.Column)));
358 }
359 }
360 });
361 }
362 });
363 }
364}
365
366static void operator<<(json::OStream &W, const SymbolizedCoverage &C) {
367 W.object(Contents: [&] {
368 W.attributeArray(Key: "covered-points", Contents: [&] {
369 for (const std::string &P : C.CoveredIds) {
370 W.value(V: P);
371 }
372 });
373 W.attribute(Key: "binary-hash", Contents: C.BinaryHash);
374 W.attributeObject(Key: "point-symbol-info", Contents: [&] { W << C.Points; });
375 });
376}
377
378static std::string parseScalarString(yaml::Node *N) {
379 SmallString<64> StringStorage;
380 yaml::ScalarNode *S = dyn_cast_if_present<yaml::ScalarNode>(Val: N);
381 failIf(B: !S, E: "expected string");
382 return std::string(S->getValue(Storage&: StringStorage));
383}
384
385std::unique_ptr<SymbolizedCoverage>
386SymbolizedCoverage::read(const std::string &InputFile) {
387 auto Coverage(std::make_unique<SymbolizedCoverage>());
388
389 std::map<std::string, CoveragePoint> Points;
390 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
391 MemoryBuffer::getFile(Filename: InputFile);
392 failIfError(E: BufOrErr);
393
394 SourceMgr SM;
395 yaml::Stream S(**BufOrErr, SM);
396
397 yaml::document_iterator DI = S.begin();
398 failIf(B: DI == S.end(), E: "empty document: " + InputFile);
399 yaml::Node *Root = DI->getRoot();
400 failIf(B: !Root, E: "expecting root node: " + InputFile);
401 yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Val: Root);
402 failIf(B: !Top, E: "expecting mapping node: " + InputFile);
403
404 for (auto &KVNode : *Top) {
405 auto Key = parseScalarString(N: KVNode.getKey());
406
407 if (Key == "covered-points") {
408 yaml::SequenceNode *Points =
409 dyn_cast_if_present<yaml::SequenceNode>(Val: KVNode.getValue());
410 failIf(B: !Points, E: "expected array: " + InputFile);
411
412 for (auto I = Points->begin(), E = Points->end(); I != E; ++I) {
413 Coverage->CoveredIds.insert(x: parseScalarString(N: &*I));
414 }
415 } else if (Key == "binary-hash") {
416 Coverage->BinaryHash = parseScalarString(N: KVNode.getValue());
417 } else if (Key == "point-symbol-info") {
418 yaml::MappingNode *PointSymbolInfo =
419 dyn_cast_if_present<yaml::MappingNode>(Val: KVNode.getValue());
420 failIf(B: !PointSymbolInfo, E: "expected mapping node: " + InputFile);
421
422 for (auto &FileKVNode : *PointSymbolInfo) {
423 auto Filename = parseScalarString(N: FileKVNode.getKey());
424
425 yaml::MappingNode *FileInfo =
426 dyn_cast_if_present<yaml::MappingNode>(Val: FileKVNode.getValue());
427 failIf(B: !FileInfo, E: "expected mapping node: " + InputFile);
428
429 for (auto &FunctionKVNode : *FileInfo) {
430 auto FunctionName = parseScalarString(N: FunctionKVNode.getKey());
431
432 yaml::MappingNode *FunctionInfo =
433 dyn_cast_if_present<yaml::MappingNode>(Val: FunctionKVNode.getValue());
434 failIf(B: !FunctionInfo, E: "expected mapping node: " + InputFile);
435
436 for (auto &PointKVNode : *FunctionInfo) {
437 auto PointId = parseScalarString(N: PointKVNode.getKey());
438 auto Loc = parseScalarString(N: PointKVNode.getValue());
439
440 size_t ColonPos = Loc.find(c: ':');
441 failIf(B: ColonPos == std::string::npos, E: "expected ':': " + InputFile);
442
443 auto LineStr = Loc.substr(pos: 0, n: ColonPos);
444 auto ColStr = Loc.substr(pos: ColonPos + 1, n: Loc.size());
445
446 DILineInfo LineInfo;
447 LineInfo.FileName = Filename;
448 LineInfo.FunctionName = FunctionName;
449 char *End;
450 LineInfo.Line = std::strtoul(nptr: LineStr.c_str(), endptr: &End, base: 10);
451 LineInfo.Column = std::strtoul(nptr: ColStr.c_str(), endptr: &End, base: 10);
452
453 CoveragePoint *CoveragePoint =
454 &Points.try_emplace(k: PointId, args&: PointId).first->second;
455 CoveragePoint->Locs.push_back(Elt: LineInfo);
456 }
457 }
458 }
459 } else {
460 errs() << "Ignoring unknown key: " << Key << "\n";
461 }
462 }
463
464 for (auto &KV : Points) {
465 Coverage->Points.push_back(x: KV.second);
466 }
467
468 return Coverage;
469}
470
471// ---------- MAIN FUNCTIONALITY ----------
472
473std::string stripPathPrefix(std::string Path) {
474 if (ClStripPathPrefix.empty())
475 return Path;
476 size_t Pos = Path.find(str: ClStripPathPrefix);
477 if (Pos == std::string::npos)
478 return Path;
479 return Path.substr(pos: Pos + ClStripPathPrefix.size());
480}
481
482static std::unique_ptr<symbolize::LLVMSymbolizer> createSymbolizer() {
483 symbolize::LLVMSymbolizer::Options SymbolizerOptions;
484 SymbolizerOptions.Demangle = ClDemangle;
485 SymbolizerOptions.UseSymbolTable = true;
486 return std::make_unique<symbolize::LLVMSymbolizer>(args&: SymbolizerOptions);
487}
488
489static std::string normalizeFilename(const std::string &FileName) {
490 SmallString<256> S(FileName);
491 sys::path::remove_dots(path&: S, /* remove_dot_dot */ true);
492 return stripPathPrefix(Path: sys::path::convert_to_slash(path: std::string(S)));
493}
494
495class Ignorelists {
496public:
497 Ignorelists()
498 : DefaultIgnorelist(createDefaultIgnorelist()),
499 UserIgnorelist(createUserIgnorelist()) {}
500
501 bool isIgnorelisted(const DILineInfo &I) {
502 if (DefaultIgnorelist &&
503 DefaultIgnorelist->inSection(Section: "sancov", Prefix: "fun", Query: I.FunctionName))
504 return true;
505 if (DefaultIgnorelist &&
506 DefaultIgnorelist->inSection(Section: "sancov", Prefix: "src", Query: I.FileName))
507 return true;
508 if (UserIgnorelist &&
509 UserIgnorelist->inSection(Section: "sancov", Prefix: "fun", Query: I.FunctionName))
510 return true;
511 if (UserIgnorelist &&
512 UserIgnorelist->inSection(Section: "sancov", Prefix: "src", Query: I.FileName))
513 return true;
514 return false;
515 }
516
517private:
518 static std::unique_ptr<SpecialCaseList> createDefaultIgnorelist() {
519 if (!ClUseDefaultIgnorelist)
520 return std::unique_ptr<SpecialCaseList>();
521 std::unique_ptr<MemoryBuffer> MB =
522 MemoryBuffer::getMemBuffer(InputData: DefaultIgnorelistStr);
523 std::string Error;
524 auto Ignorelist = SpecialCaseList::create(MB: MB.get(), Error);
525 failIfNotEmpty(E: Error);
526 return Ignorelist;
527 }
528
529 static std::unique_ptr<SpecialCaseList> createUserIgnorelist() {
530 if (ClIgnorelist.empty())
531 return std::unique_ptr<SpecialCaseList>();
532 return SpecialCaseList::createOrDie(Paths: {{ClIgnorelist}},
533 FS&: *vfs::getRealFileSystem());
534 }
535 std::unique_ptr<SpecialCaseList> DefaultIgnorelist;
536 std::unique_ptr<SpecialCaseList> UserIgnorelist;
537};
538
539static std::vector<CoveragePoint>
540getCoveragePoints(const std::string &ObjectFile,
541 const std::set<uint64_t> &Addrs,
542 const std::set<uint64_t> &CoveredAddrs,
543 symbolize::LLVMSymbolizer &Symbolizer) {
544 std::vector<CoveragePoint> Result;
545 Ignorelists Ig;
546
547 std::set<std::string> CoveredFiles;
548 if (ClSkipDeadFiles) {
549 for (auto Addr : CoveredAddrs) {
550 // TODO: it would be neccessary to set proper section index here.
551 // object::SectionedAddress::UndefSection works for only absolute
552 // addresses.
553 object::SectionedAddress ModuleAddress = {
554 .Address: Addr, .SectionIndex: object::SectionedAddress::UndefSection};
555
556 auto LineInfo = Symbolizer.symbolizeCode(ModuleName: ObjectFile, ModuleOffset: ModuleAddress);
557 failIfError(E&: LineInfo);
558 CoveredFiles.insert(x: LineInfo->FileName);
559 auto InliningInfo =
560 Symbolizer.symbolizeInlinedCode(ModuleName: ObjectFile, ModuleOffset: ModuleAddress);
561 failIfError(E&: InliningInfo);
562 for (uint32_t I = 0; I < InliningInfo->getNumberOfFrames(); ++I) {
563 auto FrameInfo = InliningInfo->getFrame(Index: I);
564 CoveredFiles.insert(x: FrameInfo.FileName);
565 }
566 }
567 }
568
569 for (auto Addr : Addrs) {
570 std::set<DILineInfo> Infos; // deduplicate debug info.
571
572 // TODO: it would be neccessary to set proper section index here.
573 // object::SectionedAddress::UndefSection works for only absolute addresses.
574 object::SectionedAddress ModuleAddress = {
575 .Address: Addr, .SectionIndex: object::SectionedAddress::UndefSection};
576
577 auto LineInfo = Symbolizer.symbolizeCode(ModuleName: ObjectFile, ModuleOffset: ModuleAddress);
578 failIfError(E&: LineInfo);
579 if (ClSkipDeadFiles &&
580 CoveredFiles.find(x: LineInfo->FileName) == CoveredFiles.end())
581 continue;
582 LineInfo->FileName = normalizeFilename(FileName: LineInfo->FileName);
583 if (Ig.isIgnorelisted(I: *LineInfo))
584 continue;
585
586 auto Id = utohexstr(X: Addr, LowerCase: true);
587 auto Point = CoveragePoint(Id);
588 Infos.insert(x: *LineInfo);
589 Point.Locs.push_back(Elt: *LineInfo);
590
591 auto InliningInfo =
592 Symbolizer.symbolizeInlinedCode(ModuleName: ObjectFile, ModuleOffset: ModuleAddress);
593 failIfError(E&: InliningInfo);
594 for (uint32_t I = 0; I < InliningInfo->getNumberOfFrames(); ++I) {
595 auto FrameInfo = InliningInfo->getFrame(Index: I);
596 if (ClSkipDeadFiles &&
597 CoveredFiles.find(x: FrameInfo.FileName) == CoveredFiles.end())
598 continue;
599 FrameInfo.FileName = normalizeFilename(FileName: FrameInfo.FileName);
600 if (Ig.isIgnorelisted(I: FrameInfo))
601 continue;
602 if (Infos.insert(x: FrameInfo).second)
603 Point.Locs.push_back(Elt: FrameInfo);
604 }
605
606 Result.push_back(x: Point);
607 }
608
609 return Result;
610}
611
612static bool isCoveragePointSymbol(StringRef Name) {
613 return Name == "__sanitizer_cov" || Name == "__sanitizer_cov_with_check" ||
614 Name == "__sanitizer_cov_trace_func_enter" ||
615 Name == "__sanitizer_cov_trace_pc_guard" ||
616 // Mac has '___' prefix
617 Name == "___sanitizer_cov" || Name == "___sanitizer_cov_with_check" ||
618 Name == "___sanitizer_cov_trace_func_enter" ||
619 Name == "___sanitizer_cov_trace_pc_guard" ||
620 // Large Aarch64 binaries use thunks
621 Name == "__AArch64ADRPThunk___sanitizer_cov" ||
622 Name == "__AArch64ADRPThunk___sanitizer_cov_with_check" ||
623 Name == "__AArch64ADRPThunk___sanitizer_cov_trace_func_enter" ||
624 Name == "__AArch64ADRPThunk___sanitizer_cov_trace_pc_guard";
625}
626
627// Locate __sanitizer_cov* function addresses inside the stubs table on MachO.
628static void findMachOIndirectCovFunctions(const object::MachOObjectFile &O,
629 std::set<uint64_t> *Result) {
630 MachO::dysymtab_command Dysymtab = O.getDysymtabLoadCommand();
631 MachO::symtab_command Symtab = O.getSymtabLoadCommand();
632
633 for (const auto &Load : O.load_commands()) {
634 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
635 MachO::segment_command_64 Seg = O.getSegment64LoadCommand(L: Load);
636 for (unsigned J = 0; J < Seg.nsects; ++J) {
637 MachO::section_64 Sec = O.getSection64(L: Load, Index: J);
638
639 uint32_t SectionType = Sec.flags & MachO::SECTION_TYPE;
640 if (SectionType == MachO::S_SYMBOL_STUBS) {
641 uint32_t Stride = Sec.reserved2;
642 uint32_t Cnt = Sec.size / Stride;
643 uint32_t N = Sec.reserved1;
644 for (uint32_t J = 0; J < Cnt && N + J < Dysymtab.nindirectsyms; J++) {
645 uint32_t IndirectSymbol =
646 O.getIndirectSymbolTableEntry(DLC: Dysymtab, Index: N + J);
647 uint64_t Addr = Sec.addr + J * Stride;
648 if (IndirectSymbol < Symtab.nsyms) {
649 object::SymbolRef Symbol = *(O.getSymbolByIndex(Index: IndirectSymbol));
650 Expected<StringRef> Name = Symbol.getName();
651 failIfError(E&: Name);
652 if (isCoveragePointSymbol(Name: Name.get())) {
653 Result->insert(x: Addr);
654 }
655 }
656 }
657 }
658 }
659 }
660 if (Load.C.cmd == MachO::LC_SEGMENT) {
661 errs() << "ERROR: 32 bit MachO binaries not supported\n";
662 }
663 }
664}
665
666// Locate __sanitizer_cov* function addresses that are used for coverage
667// reporting.
668static std::set<uint64_t>
669findSanitizerCovFunctions(const object::ObjectFile &O) {
670 std::set<uint64_t> Result;
671
672 for (const object::SymbolRef &Symbol : O.symbols()) {
673 Expected<uint64_t> AddressOrErr = Symbol.getAddress();
674 failIfError(E&: AddressOrErr);
675 uint64_t Address = AddressOrErr.get();
676
677 Expected<StringRef> NameOrErr = Symbol.getName();
678 failIfError(E&: NameOrErr);
679 StringRef Name = NameOrErr.get();
680
681 Expected<uint32_t> FlagsOrErr = Symbol.getFlags();
682 // TODO: Test this error.
683 failIfError(E&: FlagsOrErr);
684 uint32_t Flags = FlagsOrErr.get();
685
686 // XCOFF uses "." prefix for function entry point symbols.
687 StringRef EffectiveName =
688 (isa<object::XCOFFObjectFile>(Val: &O) && Name.starts_with(Prefix: "."))
689 ? Name.drop_front(N: 1)
690 : Name;
691 if (!(Flags & object::BasicSymbolRef::SF_Undefined) &&
692 isCoveragePointSymbol(Name: EffectiveName)) {
693 Result.insert(x: Address);
694 }
695 }
696
697 if (const auto *CO = dyn_cast<object::COFFObjectFile>(Val: &O)) {
698 for (const object::ExportDirectoryEntryRef &Export :
699 CO->export_directories()) {
700 uint32_t RVA;
701 failIfError(Err: Export.getExportRVA(Result&: RVA));
702
703 StringRef Name;
704 failIfError(Err: Export.getSymbolName(Result&: Name));
705
706 if (isCoveragePointSymbol(Name))
707 Result.insert(x: CO->getImageBase() + RVA);
708 }
709 }
710
711 if (const auto *MO = dyn_cast<object::MachOObjectFile>(Val: &O)) {
712 findMachOIndirectCovFunctions(O: *MO, Result: &Result);
713 }
714
715 return Result;
716}
717
718// Ported from
719// compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h:GetPreviousInstructionPc
720// GetPreviousInstructionPc.
721static uint64_t getPreviousInstructionPc(uint64_t PC, Triple TheTriple) {
722 if (TheTriple.isARM())
723 return (PC - 3) & (~1);
724 if (TheTriple.isMIPS() || TheTriple.isSPARC())
725 return PC - 8;
726 if (TheTriple.isRISCV())
727 return PC - 2;
728 if (TheTriple.isX86() || TheTriple.isSystemZ())
729 return PC - 1;
730 return PC - 4;
731}
732
733// Locate addresses of all coverage points in a file. Coverage point
734// is defined as the 'address of instruction following __sanitizer_cov
735// call - 1'.
736static void getObjectCoveragePoints(const object::ObjectFile &O,
737 std::set<uint64_t> *Addrs) {
738 Triple TheTriple("unknown-unknown-unknown");
739 TheTriple.setArch(Kind: Triple::ArchType(O.getArch()));
740 auto TripleName = TheTriple.getTriple();
741
742 std::string Error;
743 const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple, Error);
744 failIfNotEmpty(E: Error);
745
746 std::unique_ptr<const MCSubtargetInfo> STI(
747 TheTarget->createMCSubtargetInfo(TheTriple, CPU: "", Features: ""));
748 failIfEmpty(Ptr: STI, Message: "no subtarget info for target " + TripleName);
749
750 std::unique_ptr<const MCRegisterInfo> MRI(
751 TheTarget->createMCRegInfo(TT: TheTriple));
752 failIfEmpty(Ptr: MRI, Message: "no register info for target " + TripleName);
753
754 MCTargetOptions MCOptions;
755 std::unique_ptr<const MCAsmInfo> AsmInfo(
756 TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple, Options: MCOptions));
757 failIfEmpty(Ptr: AsmInfo, Message: "no asm info for target " + TripleName);
758
759 MCContext Ctx(TheTriple, *AsmInfo, *MRI, *STI);
760 std::unique_ptr<MCDisassembler> DisAsm(
761 TheTarget->createMCDisassembler(STI: *STI, Ctx));
762 failIfEmpty(Ptr: DisAsm, Message: "no disassembler info for target " + TripleName);
763
764 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
765 failIfEmpty(Ptr: MII, Message: "no instruction info for target " + TripleName);
766
767 std::unique_ptr<MCInstrAnalysis> MIA(
768 TheTarget->createMCInstrAnalysis(Info: MII.get()));
769 failIfEmpty(Ptr: MIA, Message: "no instruction analysis info for target " + TripleName);
770
771 auto SanCovAddrs = findSanitizerCovFunctions(O);
772 if (SanCovAddrs.empty())
773 fail(E: "__sanitizer_cov* functions not found");
774
775 for (object::SectionRef Section : O.sections()) {
776 if (Section.isVirtual() || !Section.isText()) // llvm-objdump does the same.
777 continue;
778 uint64_t SectionAddr = Section.getAddress();
779 uint64_t SectSize = Section.getSize();
780 if (!SectSize)
781 continue;
782
783 Expected<StringRef> BytesStr = Section.getContents();
784 failIfError(E&: BytesStr);
785 ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(Input: *BytesStr);
786
787 if (MIA)
788 MIA->resetState();
789
790 for (uint64_t Index = 0, Size = 0; Index < Section.getSize();
791 Index += Size) {
792 MCInst Inst;
793 ArrayRef<uint8_t> ThisBytes = Bytes.slice(N: Index);
794 uint64_t ThisAddr = SectionAddr + Index;
795 if (!DisAsm->getInstruction(Instr&: Inst, Size, Bytes: ThisBytes, Address: ThisAddr, CStream&: nulls())) {
796 if (Size == 0)
797 Size = std::min<uint64_t>(
798 a: ThisBytes.size(),
799 b: DisAsm->suggestBytesToSkip(Bytes: ThisBytes, Address: ThisAddr));
800 MIA->resetState();
801 continue;
802 }
803 uint64_t Addr = Index + SectionAddr;
804 // Sanitizer coverage uses the address of the next instruction - 1.
805 uint64_t CovPoint = getPreviousInstructionPc(PC: Addr + Size, TheTriple);
806 uint64_t Target;
807 if (MIA->isCall(Inst) &&
808 MIA->evaluateBranch(Inst, Addr: SectionAddr + Index, Size, Target) &&
809 SanCovAddrs.find(x: Target) != SanCovAddrs.end())
810 Addrs->insert(x: CovPoint);
811 MIA->updateState(Inst, STI: STI.get(), Addr);
812 }
813 }
814}
815
816static void
817visitObjectFiles(const object::Archive &A,
818 function_ref<void(const object::ObjectFile &)> Fn) {
819 Error Err = Error::success();
820 for (auto &C : A.children(Err)) {
821 Expected<std::unique_ptr<object::Binary>> ChildOrErr = C.getAsBinary();
822 failIfError(E&: ChildOrErr);
823 if (auto *O = dyn_cast<object::ObjectFile>(Val: &*ChildOrErr.get()))
824 Fn(*O);
825 else
826 failIfError(Error: object::object_error::invalid_file_type);
827 }
828 failIfError(Err: std::move(Err));
829}
830
831static void
832visitObjectFiles(const std::string &FileName,
833 function_ref<void(const object::ObjectFile &)> Fn) {
834 Expected<object::OwningBinary<object::Binary>> BinaryOrErr =
835 object::createBinary(Path: FileName);
836 if (!BinaryOrErr)
837 failIfError(E&: BinaryOrErr);
838
839 object::Binary &Binary = *BinaryOrErr.get().getBinary();
840 if (object::Archive *A = dyn_cast<object::Archive>(Val: &Binary))
841 visitObjectFiles(A: *A, Fn);
842 else if (object::ObjectFile *O = dyn_cast<object::ObjectFile>(Val: &Binary))
843 Fn(*O);
844 else
845 failIfError(Error: object::object_error::invalid_file_type);
846}
847
848static std::set<uint64_t>
849findSanitizerCovFunctions(const std::string &FileName) {
850 std::set<uint64_t> Result;
851 visitObjectFiles(FileName, Fn: [&](const object::ObjectFile &O) {
852 auto Addrs = findSanitizerCovFunctions(O);
853 Result.insert(first: Addrs.begin(), last: Addrs.end());
854 });
855 return Result;
856}
857
858// Locate addresses of all coverage points in a file. Coverage point
859// is defined as the 'address of instruction following __sanitizer_cov
860// call - 1'.
861static std::set<uint64_t> findCoveragePointAddrs(const std::string &FileName) {
862 std::set<uint64_t> Result;
863 visitObjectFiles(FileName, Fn: [&](const object::ObjectFile &O) {
864 getObjectCoveragePoints(O, Addrs: &Result);
865 });
866 return Result;
867}
868
869static void printCovPoints(const std::string &ObjFile, raw_ostream &OS) {
870 for (uint64_t Addr : findCoveragePointAddrs(FileName: ObjFile)) {
871 OS << "0x";
872 OS.write_hex(N: Addr);
873 OS << "\n";
874 }
875}
876
877static ErrorOr<bool> isCoverageFile(const std::string &FileName) {
878 auto ShortFileName = llvm::sys::path::filename(path: FileName);
879 if (!SancovFileRegex.match(String: ShortFileName))
880 return false;
881
882 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
883 MemoryBuffer::getFile(Filename: FileName);
884 if (!BufOrErr) {
885 errs() << "Warning: " << BufOrErr.getError().message() << "("
886 << BufOrErr.getError().value()
887 << "), filename: " << llvm::sys::path::filename(path: FileName) << "\n";
888 return BufOrErr.getError();
889 }
890 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
891 if (Buf->getBufferSize() < 8) {
892 return false;
893 }
894 const FileHeader *Header =
895 reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
896 return Header->Magic == BinCoverageMagic;
897}
898
899static bool isSymbolizedCoverageFile(const std::string &FileName) {
900 auto ShortFileName = llvm::sys::path::filename(path: FileName);
901 return SymcovFileRegex.match(String: ShortFileName);
902}
903
904static std::unique_ptr<SymbolizedCoverage>
905symbolize(const RawCoverage &Data, const std::string ObjectFile) {
906 auto Coverage = std::make_unique<SymbolizedCoverage>();
907
908 // Release the mapped file before symbolizing.
909 {
910 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
911 MemoryBuffer::getFile(Filename: ObjectFile);
912 failIfError(E: BufOrErr);
913 SHA1 Hasher;
914 Hasher.update(Str: (*BufOrErr)->getBuffer());
915 Coverage->BinaryHash = toHex(Input: Hasher.final());
916 }
917
918 Ignorelists Ig;
919 auto Symbolizer(createSymbolizer());
920
921 for (uint64_t Addr : *Data.Addrs) {
922 // TODO: it would be neccessary to set proper section index here.
923 // object::SectionedAddress::UndefSection works for only absolute
924 // addresses.
925 auto LineInfo = Symbolizer->symbolizeCode(
926 ModuleName: ObjectFile, ModuleOffset: {.Address: Addr, .SectionIndex: object::SectionedAddress::UndefSection});
927 failIfError(E&: LineInfo);
928 if (Ig.isIgnorelisted(I: *LineInfo))
929 continue;
930
931 Coverage->CoveredIds.insert(x: utohexstr(X: Addr, LowerCase: true));
932 }
933
934 std::set<uint64_t> AllAddrs = findCoveragePointAddrs(FileName: ObjectFile);
935 if (!llvm::includes(Range1&: AllAddrs, Range2&: *Data.Addrs)) {
936 fail(E: "Coverage points in binary and .sancov file do not match.");
937 }
938 Coverage->Points =
939 getCoveragePoints(ObjectFile, Addrs: AllAddrs, CoveredAddrs: *Data.Addrs, Symbolizer&: *Symbolizer);
940 return Coverage;
941}
942
943struct FileFn {
944 bool operator<(const FileFn &RHS) const {
945 return std::tie(args: FileName, args: FunctionName) <
946 std::tie(args: RHS.FileName, args: RHS.FunctionName);
947 }
948
949 std::string FileName;
950 std::string FunctionName;
951};
952
953static std::set<FileFn>
954computeFunctions(const std::vector<CoveragePoint> &Points) {
955 std::set<FileFn> Fns;
956 for (const auto &Point : Points) {
957 for (const auto &Loc : Point.Locs) {
958 Fns.insert(x: FileFn{.FileName: Loc.FileName, .FunctionName: Loc.FunctionName});
959 }
960 }
961 return Fns;
962}
963
964static std::set<FileFn>
965computeNotCoveredFunctions(const SymbolizedCoverage &Coverage) {
966 auto Fns = computeFunctions(Points: Coverage.Points);
967
968 for (const auto &Point : Coverage.Points) {
969 if (Coverage.CoveredIds.find(x: Point.Id) == Coverage.CoveredIds.end())
970 continue;
971
972 for (const auto &Loc : Point.Locs) {
973 Fns.erase(x: FileFn{.FileName: Loc.FileName, .FunctionName: Loc.FunctionName});
974 }
975 }
976
977 return Fns;
978}
979
980static std::set<FileFn>
981computeCoveredFunctions(const SymbolizedCoverage &Coverage) {
982 std::set<FileFn> Result;
983
984 for (const auto &Point : Coverage.Points) {
985 if (Coverage.CoveredIds.find(x: Point.Id) == Coverage.CoveredIds.end())
986 continue;
987
988 for (const auto &Loc : Point.Locs) {
989 Result.insert(x: FileFn{.FileName: Loc.FileName, .FunctionName: Loc.FunctionName});
990 }
991 }
992
993 return Result;
994}
995
996typedef std::map<FileFn, std::pair<uint32_t, uint32_t>> FunctionLocs;
997// finds first location in a file for each function.
998static FunctionLocs resolveFunctions(const SymbolizedCoverage &Coverage,
999 const std::set<FileFn> &Fns) {
1000 FunctionLocs Result;
1001 for (const auto &Point : Coverage.Points) {
1002 for (const auto &Loc : Point.Locs) {
1003 FileFn Fn = FileFn{.FileName: Loc.FileName, .FunctionName: Loc.FunctionName};
1004 if (Fns.find(x: Fn) == Fns.end())
1005 continue;
1006
1007 auto P = std::make_pair(x: Loc.Line, y: Loc.Column);
1008 auto [It, Inserted] = Result.try_emplace(k: Fn, args&: P);
1009 if (!Inserted && It->second > P)
1010 It->second = P;
1011 }
1012 }
1013 return Result;
1014}
1015
1016static void printFunctionLocs(const FunctionLocs &FnLocs, raw_ostream &OS) {
1017 for (const auto &P : FnLocs) {
1018 OS << stripPathPrefix(Path: P.first.FileName) << ":" << P.second.first << " "
1019 << P.first.FunctionName << "\n";
1020 }
1021}
1022CoverageStats computeStats(const SymbolizedCoverage &Coverage) {
1023 CoverageStats Stats = {.AllPoints: Coverage.Points.size(), .CovPoints: Coverage.CoveredIds.size(),
1024 .AllFns: computeFunctions(Points: Coverage.Points).size(),
1025 .CovFns: computeCoveredFunctions(Coverage).size()};
1026 return Stats;
1027}
1028
1029// Print list of covered functions.
1030// Line format: <file_name>:<line> <function_name>
1031static void printCoveredFunctions(const SymbolizedCoverage &CovData,
1032 raw_ostream &OS) {
1033 auto CoveredFns = computeCoveredFunctions(Coverage: CovData);
1034 printFunctionLocs(FnLocs: resolveFunctions(Coverage: CovData, Fns: CoveredFns), OS);
1035}
1036
1037// Print list of not covered functions.
1038// Line format: <file_name>:<line> <function_name>
1039static void printNotCoveredFunctions(const SymbolizedCoverage &CovData,
1040 raw_ostream &OS) {
1041 auto NotCoveredFns = computeNotCoveredFunctions(Coverage: CovData);
1042 printFunctionLocs(FnLocs: resolveFunctions(Coverage: CovData, Fns: NotCoveredFns), OS);
1043}
1044
1045// Read list of files and merges their coverage info.
1046static void readAndPrintRawCoverage(const std::vector<std::string> &FileNames,
1047 raw_ostream &OS) {
1048 for (const auto &FileName : FileNames) {
1049 auto Cov = RawCoverage::read(FileName);
1050 if (!Cov)
1051 continue;
1052 OS << *Cov.get();
1053 }
1054}
1055
1056static const char *bitnessToString(uint32_t Bitness) {
1057 switch (Bitness) {
1058 case Bitness64:
1059 return "64-bit";
1060 case Bitness32:
1061 return "32-bit";
1062 default:
1063 fail(E: "Unsupported bitness: " + std::to_string(val: Bitness));
1064 return nullptr;
1065 }
1066}
1067
1068// Warn if two file headers have different bitness.
1069static void warnIfDifferentBitness(const FileHeader &Header1,
1070 const FileHeader &Header2,
1071 const std::string &File1Desc,
1072 const std::string &File2Desc) {
1073 if (Header1.Bitness != Header2.Bitness) {
1074 errs() << "WARNING: Input files have different bitness (" << File1Desc
1075 << ": " << bitnessToString(Bitness: Header1.Bitness) << ", " << File2Desc
1076 << ": " << bitnessToString(Bitness: Header2.Bitness)
1077 << "). Using bitness from " << File1Desc << ".\n";
1078
1079 if (Header1.Bitness == Bitness32 && Header2.Bitness == Bitness64) {
1080 errs() << "WARNING: 64-bit addresses will be truncated to 32 bits. "
1081 << "This may result in data loss.\n";
1082 }
1083 }
1084}
1085
1086// Compute difference between two coverage files (A - B) and write to output
1087// file.
1088static void diffRawCoverage(const std::string &FileA, const std::string &FileB,
1089 const std::string &OutputFile) {
1090 auto CovA = RawCoverage::read(FileName: FileA);
1091 failIfError(E: CovA);
1092
1093 auto CovB = RawCoverage::read(FileName: FileB);
1094 failIfError(E: CovB);
1095
1096 const FileHeader &HeaderA = CovA.get()->Header;
1097 const FileHeader &HeaderB = CovB.get()->Header;
1098
1099 warnIfDifferentBitness(Header1: HeaderA, Header2: HeaderB, File1Desc: FileA, File2Desc: FileB);
1100
1101 // Compute A - B
1102 auto DiffAddrs = std::make_unique<std::set<uint64_t>>();
1103 std::set_difference(first1: CovA.get()->Addrs->begin(), last1: CovA.get()->Addrs->end(),
1104 first2: CovB.get()->Addrs->begin(), last2: CovB.get()->Addrs->end(),
1105 result: std::inserter(x&: *DiffAddrs, i: DiffAddrs->end()));
1106
1107 RawCoverage DiffCov(std::move(DiffAddrs), HeaderA);
1108 RawCoverage::write(FileName: OutputFile, Coverage: DiffCov);
1109}
1110
1111// Compute union of multiple coverage files and write to output file.
1112static void unionRawCoverage(const std::vector<std::string> &InputFiles,
1113 const std::string &OutputFile) {
1114 failIf(B: InputFiles.empty(), E: "union action requires at least one input file");
1115
1116 // Read the first file to get the header and initial coverage
1117 auto UnionCov = RawCoverage::read(FileName: InputFiles[0]);
1118 failIfError(E: UnionCov);
1119
1120 const FileHeader &UnionHeader = UnionCov.get()->Header;
1121
1122 for (size_t I = 1; I < InputFiles.size(); ++I) {
1123 auto Cov = RawCoverage::read(FileName: InputFiles[I]);
1124 failIfError(E: Cov);
1125
1126 const FileHeader &CurHeader = Cov.get()->Header;
1127
1128 warnIfDifferentBitness(Header1: UnionHeader, Header2: CurHeader, File1Desc: InputFiles[0],
1129 File2Desc: InputFiles[I]);
1130
1131 UnionCov.get()->Addrs->insert(first: Cov.get()->Addrs->begin(),
1132 last: Cov.get()->Addrs->end());
1133 }
1134
1135 RawCoverage::write(FileName: OutputFile, Coverage: *UnionCov.get());
1136}
1137
1138static std::unique_ptr<SymbolizedCoverage>
1139merge(const std::vector<std::unique_ptr<SymbolizedCoverage>> &Coverages) {
1140 if (Coverages.empty())
1141 return nullptr;
1142
1143 auto Result = std::make_unique<SymbolizedCoverage>();
1144
1145 for (size_t I = 0; I < Coverages.size(); ++I) {
1146 const SymbolizedCoverage &Coverage = *Coverages[I];
1147 std::string Prefix;
1148 if (Coverages.size() > 1) {
1149 // prefix is not needed when there's only one file.
1150 Prefix = utostr(X: I);
1151 }
1152
1153 for (const auto &Id : Coverage.CoveredIds) {
1154 Result->CoveredIds.insert(x: Prefix + Id);
1155 }
1156
1157 for (const auto &CovPoint : Coverage.Points) {
1158 CoveragePoint NewPoint(CovPoint);
1159 NewPoint.Id = Prefix + CovPoint.Id;
1160 Result->Points.push_back(x: NewPoint);
1161 }
1162 }
1163
1164 if (Coverages.size() == 1) {
1165 Result->BinaryHash = Coverages[0]->BinaryHash;
1166 }
1167
1168 return Result;
1169}
1170
1171static std::unique_ptr<SymbolizedCoverage>
1172readSymbolizeAndMergeCmdArguments(std::vector<std::string> FileNames) {
1173 std::vector<std::unique_ptr<SymbolizedCoverage>> Coverages;
1174
1175 {
1176 // Short name => file name.
1177 std::map<std::string, std::string, std::less<>> ObjFiles;
1178 std::string FirstObjFile;
1179 std::set<std::string> CovFiles;
1180
1181 // Partition input values into coverage/object files.
1182 for (const auto &FileName : FileNames) {
1183 if (isSymbolizedCoverageFile(FileName)) {
1184 Coverages.push_back(x: SymbolizedCoverage::read(InputFile: FileName));
1185 }
1186
1187 auto ErrorOrIsCoverage = isCoverageFile(FileName);
1188 if (!ErrorOrIsCoverage)
1189 continue;
1190 if (ErrorOrIsCoverage.get()) {
1191 CovFiles.insert(x: FileName);
1192 } else {
1193 auto ShortFileName = llvm::sys::path::filename(path: FileName);
1194 if (ObjFiles.find(x: ShortFileName) != ObjFiles.end()) {
1195 fail(E: "Duplicate binary file with a short name: " + ShortFileName);
1196 }
1197
1198 ObjFiles[std::string(ShortFileName)] = FileName;
1199 if (FirstObjFile.empty())
1200 FirstObjFile = FileName;
1201 }
1202 }
1203
1204 SmallVector<StringRef, 2> Components;
1205
1206 // Object file => list of corresponding coverage file names.
1207 std::map<std::string, std::vector<std::string>> CoverageByObjFile;
1208 for (const auto &FileName : CovFiles) {
1209 auto ShortFileName = llvm::sys::path::filename(path: FileName);
1210 auto Ok = SancovFileRegex.match(String: ShortFileName, Matches: &Components);
1211 if (!Ok) {
1212 fail(E: "Can't match coverage file name against "
1213 "<module_name>.<pid>.sancov pattern: " +
1214 FileName);
1215 }
1216
1217 auto Iter = ObjFiles.find(x: Components[1]);
1218 if (Iter == ObjFiles.end()) {
1219 fail(E: "Object file for coverage not found: " + FileName);
1220 }
1221
1222 CoverageByObjFile[Iter->second].push_back(x: FileName);
1223 };
1224
1225 for (const auto &Pair : ObjFiles) {
1226 auto FileName = Pair.second;
1227 if (CoverageByObjFile.find(x: FileName) == CoverageByObjFile.end())
1228 errs() << "WARNING: No coverage file for " << FileName << "\n";
1229 }
1230
1231 // Read raw coverage and symbolize it.
1232 for (const auto &Pair : CoverageByObjFile) {
1233 if (findSanitizerCovFunctions(FileName: Pair.first).empty()) {
1234 errs()
1235 << "WARNING: Ignoring " << Pair.first
1236 << " and its coverage because __sanitizer_cov* functions were not "
1237 "found.\n";
1238 continue;
1239 }
1240
1241 for (const std::string &CoverageFile : Pair.second) {
1242 auto DataOrError = RawCoverage::read(FileName: CoverageFile);
1243 failIfError(E: DataOrError);
1244 Coverages.push_back(x: symbolize(Data: *DataOrError.get(), ObjectFile: Pair.first));
1245 }
1246 }
1247 }
1248
1249 return merge(Coverages);
1250}
1251
1252} // namespace
1253
1254static void parseArgs(int Argc, char **Argv) {
1255 SancovOptTable Tbl;
1256 llvm::BumpPtrAllocator A;
1257 llvm::StringSaver Saver{A};
1258 opt::InputArgList Args =
1259 Tbl.parseArgs(Argc, Argv, Unknown: OPT_UNKNOWN, Saver, ErrorFn: [&](StringRef Msg) {
1260 llvm::errs() << Msg << '\n';
1261 std::exit(status: 1);
1262 });
1263
1264 if (Args.hasArg(Ids: OPT_help)) {
1265 Tbl.printHelp(
1266 OS&: llvm::outs(),
1267 Usage: "sancov [options] <action> <binary files...> <.sancov files...> "
1268 "<.symcov files...>",
1269 Title: "Sanitizer Coverage Processing Tool (sancov)\n\n"
1270 " This tool can extract various coverage-related information from: \n"
1271 " coverage-instrumented binary files, raw .sancov files and their "
1272 "symbolized .symcov version.\n"
1273 " Depending on chosen action the tool expects different input files:\n"
1274 " -print-coverage-pcs - coverage-instrumented binary files\n"
1275 " -print-coverage - .sancov files\n"
1276 " -diff - two .sancov files & --output option\n"
1277 " -union - one or more .sancov files & --output "
1278 "option\n"
1279 " <other actions> - .sancov files & corresponding binary "
1280 "files, .symcov files\n");
1281 std::exit(status: 0);
1282 }
1283
1284 if (Args.hasArg(Ids: OPT_version)) {
1285 cl::PrintVersionMessage();
1286 std::exit(status: 0);
1287 }
1288
1289 if (Args.hasMultipleArgs(Id: OPT_action_grp)) {
1290 fail(E: "Only one action option is allowed");
1291 }
1292
1293 for (const opt::Arg *A : Args.filtered(Ids: OPT_INPUT)) {
1294 ClInputFiles.emplace_back(args: A->getValue());
1295 }
1296
1297 if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_action_grp)) {
1298 switch (A->getOption().getID()) {
1299 case OPT_print:
1300 Action = ActionType::PrintAction;
1301 break;
1302 case OPT_diff:
1303 Action = ActionType::DiffAction;
1304 break;
1305 case OPT_union_files:
1306 Action = ActionType::UnionAction;
1307 break;
1308 case OPT_printCoveragePcs:
1309 Action = ActionType::PrintCovPointsAction;
1310 break;
1311 case OPT_coveredFunctions:
1312 Action = ActionType::CoveredFunctionsAction;
1313 break;
1314 case OPT_notCoveredFunctions:
1315 Action = ActionType::NotCoveredFunctionsAction;
1316 break;
1317 case OPT_printCoverageStats:
1318 Action = ActionType::StatsAction;
1319 break;
1320 case OPT_htmlReport:
1321 Action = ActionType::HtmlReportAction;
1322 break;
1323 case OPT_symbolize:
1324 Action = ActionType::SymbolizeAction;
1325 break;
1326 case OPT_merge:
1327 Action = ActionType::MergeAction;
1328 break;
1329 default:
1330 fail(E: "Invalid Action");
1331 }
1332 }
1333
1334 ClDemangle = Args.hasFlag(Pos: OPT_demangle, Neg: OPT_no_demangle, Default: true);
1335 ClSkipDeadFiles = Args.hasFlag(Pos: OPT_skipDeadFiles, Neg: OPT_no_skipDeadFiles, Default: true);
1336 ClUseDefaultIgnorelist =
1337 Args.hasFlag(Pos: OPT_useDefaultIgnoreList, Neg: OPT_no_useDefaultIgnoreList, Default: true);
1338
1339 ClStripPathPrefix = Args.getLastArgValue(Id: OPT_stripPathPrefix_EQ);
1340 ClIgnorelist = Args.getLastArgValue(Id: OPT_ignorelist_EQ);
1341 ClOutputFile = Args.getLastArgValue(Id: OPT_output_EQ);
1342}
1343
1344int sancov_main(int Argc, char **Argv, const llvm::ToolContext &) {
1345 llvm::InitializeAllTargetInfos();
1346 llvm::InitializeAllTargetMCs();
1347 llvm::InitializeAllDisassemblers();
1348
1349 parseArgs(Argc, Argv);
1350
1351 // -print doesn't need object files.
1352 if (Action == PrintAction) {
1353 readAndPrintRawCoverage(FileNames: ClInputFiles, OS&: outs());
1354 return 0;
1355 }
1356 if (Action == DiffAction) {
1357 // -diff requires exactly 2 input files and an output file.
1358 failIf(B: ClInputFiles.size() != 2,
1359 E: "diff action requires exactly 2 input sancov files");
1360 failIf(
1361 B: ClOutputFile.empty(),
1362 E: "diff action requires --output option to specify output sancov file");
1363 diffRawCoverage(FileA: ClInputFiles[0], FileB: ClInputFiles[1], OutputFile: ClOutputFile);
1364 return 0;
1365 }
1366 if (Action == UnionAction) {
1367 // -union requires at least 1 input file and an output file.
1368 failIf(B: ClInputFiles.empty(),
1369 E: "union action requires at least one input sancov file");
1370 failIf(
1371 B: ClOutputFile.empty(),
1372 E: "union action requires --output option to specify output sancov file");
1373 unionRawCoverage(InputFiles: ClInputFiles, OutputFile: ClOutputFile);
1374 return 0;
1375 }
1376 if (Action == PrintCovPointsAction) {
1377 // -print-coverage-points doesn't need coverage files.
1378 for (const std::string &ObjFile : ClInputFiles) {
1379 printCovPoints(ObjFile, OS&: outs());
1380 }
1381 return 0;
1382 }
1383
1384 auto Coverage = readSymbolizeAndMergeCmdArguments(FileNames: ClInputFiles);
1385 failIf(B: !Coverage, E: "No valid coverage files given.");
1386
1387 switch (Action) {
1388 case CoveredFunctionsAction: {
1389 printCoveredFunctions(CovData: *Coverage, OS&: outs());
1390 return 0;
1391 }
1392 case NotCoveredFunctionsAction: {
1393 printNotCoveredFunctions(CovData: *Coverage, OS&: outs());
1394 return 0;
1395 }
1396 case StatsAction: {
1397 outs() << computeStats(Coverage: *Coverage);
1398 return 0;
1399 }
1400 case MergeAction:
1401 case SymbolizeAction: { // merge & symbolize are synonims.
1402 json::OStream W(outs(), 2);
1403 W << *Coverage;
1404 return 0;
1405 }
1406 case HtmlReportAction:
1407 errs() << "-html-report option is removed: "
1408 "use -symbolize & coverage-report-server.py instead\n";
1409 return 1;
1410 case DiffAction:
1411 case UnionAction:
1412 case PrintAction:
1413 case PrintCovPointsAction:
1414 llvm_unreachable("unsupported action");
1415 }
1416
1417 return 0;
1418}
1419