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