1 //===-- sancov.cpp --------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // This file is a command-line tool for reading and analyzing sanitizer
11 //===----------------------------------------------------------------------===//
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/Twine.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/Object/Archive.h"
26 #include "llvm/Object/Binary.h"
27 #include "llvm/Object/COFF.h"
28 #include "llvm/Object/MachO.h"
29 #include "llvm/Object/ObjectFile.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Errc.h"
33 #include "llvm/Support/ErrorOr.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/MD5.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/Path.h"
39 #include "llvm/Support/PrettyStackTrace.h"
40 #include "llvm/Support/Regex.h"
41 #include "llvm/Support/SHA1.h"
42 #include "llvm/Support/Signals.h"
43 #include "llvm/Support/SourceMgr.h"
44 #include "llvm/Support/SpecialCaseList.h"
45 #include "llvm/Support/TargetRegistry.h"
46 #include "llvm/Support/TargetSelect.h"
47 #include "llvm/Support/YAMLParser.h"
48 #include "llvm/Support/raw_ostream.h"
57 // --------- COMMAND LINE FLAGS ---------
60 CoveredFunctionsAction,
63 NotCoveredFunctionsAction,
70 cl::opt<ActionType> Action(
71 cl::desc("Action (required)"), cl::Required,
73 clEnumValN(PrintAction, "print", "Print coverage addresses"),
74 clEnumValN(PrintCovPointsAction, "print-coverage-pcs",
75 "Print coverage instrumentation points addresses."),
76 clEnumValN(CoveredFunctionsAction, "covered-functions",
77 "Print all covered funcions."),
78 clEnumValN(NotCoveredFunctionsAction, "not-covered-functions",
79 "Print all not covered funcions."),
80 clEnumValN(StatsAction, "print-coverage-stats",
81 "Print coverage statistics."),
82 clEnumValN(HtmlReportAction, "html-report",
83 "REMOVED. Use -symbolize & coverage-report-server.py."),
84 clEnumValN(SymbolizeAction, "symbolize",
85 "Produces a symbolized JSON report from binary report."),
86 clEnumValN(MergeAction, "merge", "Merges reports.")));
88 static cl::list<std::string>
89 ClInputFiles(cl::Positional, cl::OneOrMore,
90 cl::desc("<action> <binary files...> <.sancov files...> "
91 "<.symcov files...>"));
93 static cl::opt<bool> ClDemangle("demangle", cl::init(true),
94 cl::desc("Print demangled function name."));
97 ClSkipDeadFiles("skip-dead-files", cl::init(true),
98 cl::desc("Do not list dead source files in reports."));
100 static cl::opt<std::string> ClStripPathPrefix(
101 "strip_path_prefix", cl::init(""),
102 cl::desc("Strip this prefix from file paths in reports."));
104 static cl::opt<std::string>
105 ClBlacklist("blacklist", cl::init(""),
106 cl::desc("Blacklist file (sanitizer blacklist format)."));
108 static cl::opt<bool> ClUseDefaultBlacklist(
109 "use_default_blacklist", cl::init(true), cl::Hidden,
110 cl::desc("Controls if default blacklist should be used."));
112 static const char *const DefaultBlacklistStr = "fun:__sanitizer_.*\n"
113 "src:/usr/include/.*\n"
114 "src:.*/libc\\+\\+/.*\n";
116 // --------- FORMAT SPECIFICATION ---------
123 static const uint32_t BinCoverageMagic = 0xC0BFFFFF;
124 static const uint32_t Bitness32 = 0xFFFFFF32;
125 static const uint32_t Bitness64 = 0xFFFFFF64;
127 static Regex SancovFileRegex("(.*)\\.[0-9]+\\.sancov");
128 static Regex SymcovFileRegex(".*\\.symcov");
130 // --------- MAIN DATASTRUCTURES ----------
132 // Contents of .sancov file: list of coverage point addresses that were
135 explicit RawCoverage(std::unique_ptr<std::set<uint64_t>> Addrs)
136 : Addrs(std::move(Addrs)) {}
138 // Read binary .sancov file.
139 static ErrorOr<std::unique_ptr<RawCoverage>>
140 read(const std::string &FileName);
142 std::unique_ptr<std::set<uint64_t>> Addrs;
145 // Coverage point has an opaque Id and corresponds to multiple source locations.
146 struct CoveragePoint {
147 explicit CoveragePoint(const std::string &Id) : Id(Id) {}
150 SmallVector<DILineInfo, 1> Locs;
153 // Symcov file content: set of covered Ids plus information about all available
155 struct SymbolizedCoverage {
156 // Read json .symcov file.
157 static std::unique_ptr<SymbolizedCoverage> read(const std::string &InputFile);
159 std::set<std::string> CoveredIds;
160 std::string BinaryHash;
161 std::vector<CoveragePoint> Points;
164 struct CoverageStats {
171 // --------- ERROR HANDLING ---------
173 static void fail(const llvm::Twine &E) {
174 errs() << "ERROR: " << E << "\n";
178 static void failIf(bool B, const llvm::Twine &E) {
183 static void failIfError(std::error_code Error) {
186 errs() << "ERROR: " << Error.message() << "(" << Error.value() << ")\n";
190 template <typename T> static void failIfError(const ErrorOr<T> &E) {
191 failIfError(E.getError());
194 static void failIfError(Error Err) {
196 logAllUnhandledErrors(std::move(Err), errs(), "ERROR: ");
201 template <typename T> static void failIfError(Expected<T> &E) {
202 failIfError(E.takeError());
205 static void failIfNotEmpty(const llvm::Twine &E) {
211 template <typename T>
212 static void failIfEmpty(const std::unique_ptr<T> &Ptr,
213 const std::string &Message) {
219 // ----------- Coverage I/O ----------
220 template <typename T>
221 static void readInts(const char *Start, const char *End,
222 std::set<uint64_t> *Ints) {
223 const T *S = reinterpret_cast<const T *>(Start);
224 const T *E = reinterpret_cast<const T *>(End);
225 std::copy(S, E, std::inserter(*Ints, Ints->end()));
228 ErrorOr<std::unique_ptr<RawCoverage>>
229 RawCoverage::read(const std::string &FileName) {
230 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
231 MemoryBuffer::getFile(FileName);
233 return BufOrErr.getError();
234 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
235 if (Buf->getBufferSize() < 8) {
236 errs() << "File too small (<8): " << Buf->getBufferSize() << '\n';
237 return make_error_code(errc::illegal_byte_sequence);
239 const FileHeader *Header =
240 reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
242 if (Header->Magic != BinCoverageMagic) {
243 errs() << "Wrong magic: " << Header->Magic << '\n';
244 return make_error_code(errc::illegal_byte_sequence);
247 auto Addrs = llvm::make_unique<std::set<uint64_t>>();
249 switch (Header->Bitness) {
251 readInts<uint64_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),
255 readInts<uint32_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),
259 errs() << "Unsupported bitness: " << Header->Bitness << '\n';
260 return make_error_code(errc::illegal_byte_sequence);
263 return std::unique_ptr<RawCoverage>(new RawCoverage(std::move(Addrs)));
266 // Print coverage addresses.
267 raw_ostream &operator<<(raw_ostream &OS, const RawCoverage &CoverageData) {
268 for (auto Addr : *CoverageData.Addrs) {
276 static raw_ostream &operator<<(raw_ostream &OS, const CoverageStats &Stats) {
277 OS << "all-edges: " << Stats.AllPoints << "\n";
278 OS << "cov-edges: " << Stats.CovPoints << "\n";
279 OS << "all-functions: " << Stats.AllFns << "\n";
280 OS << "cov-functions: " << Stats.CovFns << "\n";
284 // Helper for writing out JSON. Handles indents and commas using
285 // scope variables for objects and arrays.
288 JSONWriter(raw_ostream &Out) : OS(Out) {}
289 JSONWriter(const JSONWriter &) = delete;
290 ~JSONWriter() { OS << "\n"; }
292 void operator<<(StringRef S) { printJSONStringLiteral(S, OS); }
294 // Helper RAII class to output JSON objects.
297 Object(JSONWriter *W, raw_ostream &OS) : W(W), OS(OS) {
301 Object(const Object &) = delete;
309 void key(StringRef Key) {
315 printJSONStringLiteral(Key, OS);
325 std::unique_ptr<Object> object() { return make_unique<Object>(this, OS); }
327 // Helper RAII class to output JSON arrays.
330 Array(raw_ostream &OS) : OS(OS) { OS << "["; }
331 Array(const Array &) = delete;
332 ~Array() { OS << "]"; }
344 std::unique_ptr<Array> array() { return make_unique<Array>(OS); }
347 void indent() { OS.indent(Indent * 2); }
349 static void printJSONStringLiteral(StringRef S, raw_ostream &OS) {
350 if (S.find('"') == std::string::npos) {
351 OS << "\"" << S << "\"";
355 for (char Ch : S.bytes()) {
367 // Output symbolized information for coverage points in JSON.
371 // '<function_name>' : {
372 // '<point_id'> : '<line_number>:'<column_number'.
377 static void operator<<(JSONWriter &W,
378 const std::vector<CoveragePoint> &Points) {
379 // Group points by file.
380 auto ByFile(W.object());
381 std::map<std::string, std::vector<const CoveragePoint *>> PointsByFile;
382 for (const auto &Point : Points) {
383 for (const DILineInfo &Loc : Point.Locs) {
384 PointsByFile[Loc.FileName].push_back(&Point);
388 for (const auto &P : PointsByFile) {
389 std::string FileName = P.first;
390 ByFile->key(FileName);
392 // Group points by function.
393 auto ByFn(W.object());
394 std::map<std::string, std::vector<const CoveragePoint *>> PointsByFn;
395 for (auto PointPtr : P.second) {
396 for (const DILineInfo &Loc : PointPtr->Locs) {
397 PointsByFn[Loc.FunctionName].push_back(PointPtr);
401 for (const auto &P : PointsByFn) {
402 std::string FunctionName = P.first;
403 std::set<std::string> WrittenIds;
405 ByFn->key(FunctionName);
407 // Output <point_id> : "<line>:<col>".
408 auto ById(W.object());
409 for (const CoveragePoint *Point : P.second) {
410 for (const auto &Loc : Point->Locs) {
411 if (Loc.FileName != FileName || Loc.FunctionName != FunctionName)
413 if (WrittenIds.find(Point->Id) != WrittenIds.end())
416 WrittenIds.insert(Point->Id);
417 ById->key(Point->Id);
418 W << (utostr(Loc.Line) + ":" + utostr(Loc.Column));
425 static void operator<<(JSONWriter &W, const SymbolizedCoverage &C) {
429 O->key("covered-points");
430 auto PointsArray(W.array());
432 for (const auto &P : C.CoveredIds) {
439 if (!C.BinaryHash.empty()) {
440 O->key("binary-hash");
446 O->key("point-symbol-info");
451 static std::string parseScalarString(yaml::Node *N) {
452 SmallString<64> StringStorage;
453 yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
454 failIf(!S, "expected string");
455 return S->getValue(StringStorage);
458 std::unique_ptr<SymbolizedCoverage>
459 SymbolizedCoverage::read(const std::string &InputFile) {
460 auto Coverage(make_unique<SymbolizedCoverage>());
462 std::map<std::string, CoveragePoint> Points;
463 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
464 MemoryBuffer::getFile(InputFile);
465 failIfError(BufOrErr);
468 yaml::Stream S(**BufOrErr, SM);
470 yaml::document_iterator DI = S.begin();
471 failIf(DI == S.end(), "empty document: " + InputFile);
472 yaml::Node *Root = DI->getRoot();
473 failIf(!Root, "expecting root node: " + InputFile);
474 yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
475 failIf(!Top, "expecting mapping node: " + InputFile);
477 for (auto &KVNode : *Top) {
478 auto Key = parseScalarString(KVNode.getKey());
480 if (Key == "covered-points") {
481 yaml::SequenceNode *Points =
482 dyn_cast<yaml::SequenceNode>(KVNode.getValue());
483 failIf(!Points, "expected array: " + InputFile);
485 for (auto I = Points->begin(), E = Points->end(); I != E; ++I) {
486 Coverage->CoveredIds.insert(parseScalarString(&*I));
488 } else if (Key == "binary-hash") {
489 Coverage->BinaryHash = parseScalarString(KVNode.getValue());
490 } else if (Key == "point-symbol-info") {
491 yaml::MappingNode *PointSymbolInfo =
492 dyn_cast<yaml::MappingNode>(KVNode.getValue());
493 failIf(!PointSymbolInfo, "expected mapping node: " + InputFile);
495 for (auto &FileKVNode : *PointSymbolInfo) {
496 auto Filename = parseScalarString(FileKVNode.getKey());
498 yaml::MappingNode *FileInfo =
499 dyn_cast<yaml::MappingNode>(FileKVNode.getValue());
500 failIf(!FileInfo, "expected mapping node: " + InputFile);
502 for (auto &FunctionKVNode : *FileInfo) {
503 auto FunctionName = parseScalarString(FunctionKVNode.getKey());
505 yaml::MappingNode *FunctionInfo =
506 dyn_cast<yaml::MappingNode>(FunctionKVNode.getValue());
507 failIf(!FunctionInfo, "expected mapping node: " + InputFile);
509 for (auto &PointKVNode : *FunctionInfo) {
510 auto PointId = parseScalarString(PointKVNode.getKey());
511 auto Loc = parseScalarString(PointKVNode.getValue());
513 size_t ColonPos = Loc.find(':');
514 failIf(ColonPos == std::string::npos, "expected ':': " + InputFile);
516 auto LineStr = Loc.substr(0, ColonPos);
517 auto ColStr = Loc.substr(ColonPos + 1, Loc.size());
519 if (Points.find(PointId) == Points.end())
520 Points.insert(std::make_pair(PointId, CoveragePoint(PointId)));
523 LineInfo.FileName = Filename;
524 LineInfo.FunctionName = FunctionName;
526 LineInfo.Line = std::strtoul(LineStr.c_str(), &End, 10);
527 LineInfo.Column = std::strtoul(ColStr.c_str(), &End, 10);
529 CoveragePoint *CoveragePoint = &Points.find(PointId)->second;
530 CoveragePoint->Locs.push_back(LineInfo);
535 errs() << "Ignoring unknown key: " << Key << "\n";
539 for (auto &KV : Points) {
540 Coverage->Points.push_back(KV.second);
546 // ---------- MAIN FUNCTIONALITY ----------
548 std::string stripPathPrefix(std::string Path) {
549 if (ClStripPathPrefix.empty())
551 size_t Pos = Path.find(ClStripPathPrefix);
552 if (Pos == std::string::npos)
554 return Path.substr(Pos + ClStripPathPrefix.size());
557 static std::unique_ptr<symbolize::LLVMSymbolizer> createSymbolizer() {
558 symbolize::LLVMSymbolizer::Options SymbolizerOptions;
559 SymbolizerOptions.Demangle = ClDemangle;
560 SymbolizerOptions.UseSymbolTable = true;
561 return std::unique_ptr<symbolize::LLVMSymbolizer>(
562 new symbolize::LLVMSymbolizer(SymbolizerOptions));
565 static std::string normalizeFilename(const std::string &FileName) {
566 SmallString<256> S(FileName);
567 sys::path::remove_dots(S, /* remove_dot_dot */ true);
568 return stripPathPrefix(S.str().str());
574 : DefaultBlacklist(createDefaultBlacklist()),
575 UserBlacklist(createUserBlacklist()) {}
577 bool isBlacklisted(const DILineInfo &I) {
578 if (DefaultBlacklist &&
579 DefaultBlacklist->inSection("sancov", "fun", I.FunctionName))
581 if (DefaultBlacklist &&
582 DefaultBlacklist->inSection("sancov", "src", I.FileName))
585 UserBlacklist->inSection("sancov", "fun", I.FunctionName))
587 if (UserBlacklist && UserBlacklist->inSection("sancov", "src", I.FileName))
593 static std::unique_ptr<SpecialCaseList> createDefaultBlacklist() {
594 if (!ClUseDefaultBlacklist)
595 return std::unique_ptr<SpecialCaseList>();
596 std::unique_ptr<MemoryBuffer> MB =
597 MemoryBuffer::getMemBuffer(DefaultBlacklistStr);
599 auto Blacklist = SpecialCaseList::create(MB.get(), Error);
600 failIfNotEmpty(Error);
604 static std::unique_ptr<SpecialCaseList> createUserBlacklist() {
605 if (ClBlacklist.empty())
606 return std::unique_ptr<SpecialCaseList>();
608 return SpecialCaseList::createOrDie({{ClBlacklist}});
610 std::unique_ptr<SpecialCaseList> DefaultBlacklist;
611 std::unique_ptr<SpecialCaseList> UserBlacklist;
614 static std::vector<CoveragePoint>
615 getCoveragePoints(const std::string &ObjectFile,
616 const std::set<uint64_t> &Addrs,
617 const std::set<uint64_t> &CoveredAddrs) {
618 std::vector<CoveragePoint> Result;
619 auto Symbolizer(createSymbolizer());
622 std::set<std::string> CoveredFiles;
623 if (ClSkipDeadFiles) {
624 for (auto Addr : CoveredAddrs) {
625 auto LineInfo = Symbolizer->symbolizeCode(ObjectFile, Addr);
626 failIfError(LineInfo);
627 CoveredFiles.insert(LineInfo->FileName);
628 auto InliningInfo = Symbolizer->symbolizeInlinedCode(ObjectFile, Addr);
629 failIfError(InliningInfo);
630 for (uint32_t I = 0; I < InliningInfo->getNumberOfFrames(); ++I) {
631 auto FrameInfo = InliningInfo->getFrame(I);
632 CoveredFiles.insert(FrameInfo.FileName);
637 for (auto Addr : Addrs) {
638 std::set<DILineInfo> Infos; // deduplicate debug info.
640 auto LineInfo = Symbolizer->symbolizeCode(ObjectFile, Addr);
641 failIfError(LineInfo);
642 if (ClSkipDeadFiles &&
643 CoveredFiles.find(LineInfo->FileName) == CoveredFiles.end())
645 LineInfo->FileName = normalizeFilename(LineInfo->FileName);
646 if (B.isBlacklisted(*LineInfo))
649 auto Id = utohexstr(Addr, true);
650 auto Point = CoveragePoint(Id);
651 Infos.insert(*LineInfo);
652 Point.Locs.push_back(*LineInfo);
654 auto InliningInfo = Symbolizer->symbolizeInlinedCode(ObjectFile, Addr);
655 failIfError(InliningInfo);
656 for (uint32_t I = 0; I < InliningInfo->getNumberOfFrames(); ++I) {
657 auto FrameInfo = InliningInfo->getFrame(I);
658 if (ClSkipDeadFiles &&
659 CoveredFiles.find(FrameInfo.FileName) == CoveredFiles.end())
661 FrameInfo.FileName = normalizeFilename(FrameInfo.FileName);
662 if (B.isBlacklisted(FrameInfo))
664 if (Infos.find(FrameInfo) == Infos.end()) {
665 Infos.insert(FrameInfo);
666 Point.Locs.push_back(FrameInfo);
670 Result.push_back(Point);
676 static bool isCoveragePointSymbol(StringRef Name) {
677 return Name == "__sanitizer_cov" || Name == "__sanitizer_cov_with_check" ||
678 Name == "__sanitizer_cov_trace_func_enter" ||
679 Name == "__sanitizer_cov_trace_pc_guard" ||
680 // Mac has '___' prefix
681 Name == "___sanitizer_cov" || Name == "___sanitizer_cov_with_check" ||
682 Name == "___sanitizer_cov_trace_func_enter" ||
683 Name == "___sanitizer_cov_trace_pc_guard";
686 // Locate __sanitizer_cov* function addresses inside the stubs table on MachO.
687 static void findMachOIndirectCovFunctions(const object::MachOObjectFile &O,
688 std::set<uint64_t> *Result) {
689 MachO::dysymtab_command Dysymtab = O.getDysymtabLoadCommand();
690 MachO::symtab_command Symtab = O.getSymtabLoadCommand();
692 for (const auto &Load : O.load_commands()) {
693 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
694 MachO::segment_command_64 Seg = O.getSegment64LoadCommand(Load);
695 for (unsigned J = 0; J < Seg.nsects; ++J) {
696 MachO::section_64 Sec = O.getSection64(Load, J);
698 uint32_t SectionType = Sec.flags & MachO::SECTION_TYPE;
699 if (SectionType == MachO::S_SYMBOL_STUBS) {
700 uint32_t Stride = Sec.reserved2;
701 uint32_t Cnt = Sec.size / Stride;
702 uint32_t N = Sec.reserved1;
703 for (uint32_t J = 0; J < Cnt && N + J < Dysymtab.nindirectsyms; J++) {
704 uint32_t IndirectSymbol =
705 O.getIndirectSymbolTableEntry(Dysymtab, N + J);
706 uint64_t Addr = Sec.addr + J * Stride;
707 if (IndirectSymbol < Symtab.nsyms) {
708 object::SymbolRef Symbol = *(O.getSymbolByIndex(IndirectSymbol));
709 Expected<StringRef> Name = Symbol.getName();
711 if (isCoveragePointSymbol(Name.get())) {
712 Result->insert(Addr);
719 if (Load.C.cmd == MachO::LC_SEGMENT) {
720 errs() << "ERROR: 32 bit MachO binaries not supported\n";
725 // Locate __sanitizer_cov* function addresses that are used for coverage
727 static std::set<uint64_t>
728 findSanitizerCovFunctions(const object::ObjectFile &O) {
729 std::set<uint64_t> Result;
731 for (const object::SymbolRef &Symbol : O.symbols()) {
732 Expected<uint64_t> AddressOrErr = Symbol.getAddress();
733 failIfError(AddressOrErr);
734 uint64_t Address = AddressOrErr.get();
736 Expected<StringRef> NameOrErr = Symbol.getName();
737 failIfError(NameOrErr);
738 StringRef Name = NameOrErr.get();
740 if (!(Symbol.getFlags() & object::BasicSymbolRef::SF_Undefined) &&
741 isCoveragePointSymbol(Name)) {
742 Result.insert(Address);
746 if (const auto *CO = dyn_cast<object::COFFObjectFile>(&O)) {
747 for (const object::ExportDirectoryEntryRef &Export :
748 CO->export_directories()) {
750 std::error_code EC = Export.getExportRVA(RVA);
754 EC = Export.getSymbolName(Name);
757 if (isCoveragePointSymbol(Name))
758 Result.insert(CO->getImageBase() + RVA);
762 if (const auto *MO = dyn_cast<object::MachOObjectFile>(&O)) {
763 findMachOIndirectCovFunctions(*MO, &Result);
769 // Locate addresses of all coverage points in a file. Coverage point
770 // is defined as the 'address of instruction following __sanitizer_cov
772 static void getObjectCoveragePoints(const object::ObjectFile &O,
773 std::set<uint64_t> *Addrs) {
774 Triple TheTriple("unknown-unknown-unknown");
775 TheTriple.setArch(Triple::ArchType(O.getArch()));
776 auto TripleName = TheTriple.getTriple();
779 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
780 failIfNotEmpty(Error);
782 std::unique_ptr<const MCSubtargetInfo> STI(
783 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
784 failIfEmpty(STI, "no subtarget info for target " + TripleName);
786 std::unique_ptr<const MCRegisterInfo> MRI(
787 TheTarget->createMCRegInfo(TripleName));
788 failIfEmpty(MRI, "no register info for target " + TripleName);
790 std::unique_ptr<const MCAsmInfo> AsmInfo(
791 TheTarget->createMCAsmInfo(*MRI, TripleName));
792 failIfEmpty(AsmInfo, "no asm info for target " + TripleName);
794 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
795 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
796 std::unique_ptr<MCDisassembler> DisAsm(
797 TheTarget->createMCDisassembler(*STI, Ctx));
798 failIfEmpty(DisAsm, "no disassembler info for target " + TripleName);
800 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
801 failIfEmpty(MII, "no instruction info for target " + TripleName);
803 std::unique_ptr<const MCInstrAnalysis> MIA(
804 TheTarget->createMCInstrAnalysis(MII.get()));
805 failIfEmpty(MIA, "no instruction analysis info for target " + TripleName);
807 auto SanCovAddrs = findSanitizerCovFunctions(O);
808 if (SanCovAddrs.empty())
809 fail("__sanitizer_cov* functions not found");
811 for (object::SectionRef Section : O.sections()) {
812 if (Section.isVirtual() || !Section.isText()) // llvm-objdump does the same.
814 uint64_t SectionAddr = Section.getAddress();
815 uint64_t SectSize = Section.getSize();
820 failIfError(Section.getContents(BytesStr));
821 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
824 for (uint64_t Index = 0, Size = 0; Index < Section.getSize();
827 if (!DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
828 SectionAddr + Index, nulls(), nulls())) {
833 uint64_t Addr = Index + SectionAddr;
834 // Sanitizer coverage uses the address of the next instruction - 1.
835 uint64_t CovPoint = Addr + Size - 1;
837 if (MIA->isCall(Inst) &&
838 MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target) &&
839 SanCovAddrs.find(Target) != SanCovAddrs.end())
840 Addrs->insert(CovPoint);
846 visitObjectFiles(const object::Archive &A,
847 function_ref<void(const object::ObjectFile &)> Fn) {
848 Error Err = Error::success();
849 for (auto &C : A.children(Err)) {
850 Expected<std::unique_ptr<object::Binary>> ChildOrErr = C.getAsBinary();
851 failIfError(ChildOrErr);
852 if (auto *O = dyn_cast<object::ObjectFile>(&*ChildOrErr.get()))
855 failIfError(object::object_error::invalid_file_type);
857 failIfError(std::move(Err));
861 visitObjectFiles(const std::string &FileName,
862 function_ref<void(const object::ObjectFile &)> Fn) {
863 Expected<object::OwningBinary<object::Binary>> BinaryOrErr =
864 object::createBinary(FileName);
866 failIfError(BinaryOrErr);
868 object::Binary &Binary = *BinaryOrErr.get().getBinary();
869 if (object::Archive *A = dyn_cast<object::Archive>(&Binary))
870 visitObjectFiles(*A, Fn);
871 else if (object::ObjectFile *O = dyn_cast<object::ObjectFile>(&Binary))
874 failIfError(object::object_error::invalid_file_type);
877 static std::set<uint64_t>
878 findSanitizerCovFunctions(const std::string &FileName) {
879 std::set<uint64_t> Result;
880 visitObjectFiles(FileName, [&](const object::ObjectFile &O) {
881 auto Addrs = findSanitizerCovFunctions(O);
882 Result.insert(Addrs.begin(), Addrs.end());
887 // Locate addresses of all coverage points in a file. Coverage point
888 // is defined as the 'address of instruction following __sanitizer_cov
890 static std::set<uint64_t> findCoveragePointAddrs(const std::string &FileName) {
891 std::set<uint64_t> Result;
892 visitObjectFiles(FileName, [&](const object::ObjectFile &O) {
893 getObjectCoveragePoints(O, &Result);
898 static void printCovPoints(const std::string &ObjFile, raw_ostream &OS) {
899 for (uint64_t Addr : findCoveragePointAddrs(ObjFile)) {
906 static ErrorOr<bool> isCoverageFile(const std::string &FileName) {
907 auto ShortFileName = llvm::sys::path::filename(FileName);
908 if (!SancovFileRegex.match(ShortFileName))
911 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
912 MemoryBuffer::getFile(FileName);
914 errs() << "Warning: " << BufOrErr.getError().message() << "("
915 << BufOrErr.getError().value()
916 << "), filename: " << llvm::sys::path::filename(FileName) << "\n";
917 return BufOrErr.getError();
919 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
920 if (Buf->getBufferSize() < 8) {
923 const FileHeader *Header =
924 reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
925 return Header->Magic == BinCoverageMagic;
928 static bool isSymbolizedCoverageFile(const std::string &FileName) {
929 auto ShortFileName = llvm::sys::path::filename(FileName);
930 return SymcovFileRegex.match(ShortFileName);
933 static std::unique_ptr<SymbolizedCoverage>
934 symbolize(const RawCoverage &Data, const std::string ObjectFile) {
935 auto Coverage = make_unique<SymbolizedCoverage>();
937 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
938 MemoryBuffer::getFile(ObjectFile);
939 failIfError(BufOrErr);
941 Hasher.update((*BufOrErr)->getBuffer());
942 Coverage->BinaryHash = toHex(Hasher.final());
945 auto Symbolizer(createSymbolizer());
947 for (uint64_t Addr : *Data.Addrs) {
948 auto LineInfo = Symbolizer->symbolizeCode(ObjectFile, Addr);
949 failIfError(LineInfo);
950 if (B.isBlacklisted(*LineInfo))
953 Coverage->CoveredIds.insert(utohexstr(Addr, true));
956 std::set<uint64_t> AllAddrs = findCoveragePointAddrs(ObjectFile);
957 if (!std::includes(AllAddrs.begin(), AllAddrs.end(), Data.Addrs->begin(),
958 Data.Addrs->end())) {
959 fail("Coverage points in binary and .sancov file do not match.");
961 Coverage->Points = getCoveragePoints(ObjectFile, AllAddrs, *Data.Addrs);
966 bool operator<(const FileFn &RHS) const {
967 return std::tie(FileName, FunctionName) <
968 std::tie(RHS.FileName, RHS.FunctionName);
971 std::string FileName;
972 std::string FunctionName;
975 static std::set<FileFn>
976 computeFunctions(const std::vector<CoveragePoint> &Points) {
977 std::set<FileFn> Fns;
978 for (const auto &Point : Points) {
979 for (const auto &Loc : Point.Locs) {
980 Fns.insert(FileFn{Loc.FileName, Loc.FunctionName});
986 static std::set<FileFn>
987 computeNotCoveredFunctions(const SymbolizedCoverage &Coverage) {
988 auto Fns = computeFunctions(Coverage.Points);
990 for (const auto &Point : Coverage.Points) {
991 if (Coverage.CoveredIds.find(Point.Id) == Coverage.CoveredIds.end())
994 for (const auto &Loc : Point.Locs) {
995 Fns.erase(FileFn{Loc.FileName, Loc.FunctionName});
1002 static std::set<FileFn>
1003 computeCoveredFunctions(const SymbolizedCoverage &Coverage) {
1004 auto AllFns = computeFunctions(Coverage.Points);
1005 std::set<FileFn> Result;
1007 for (const auto &Point : Coverage.Points) {
1008 if (Coverage.CoveredIds.find(Point.Id) == Coverage.CoveredIds.end())
1011 for (const auto &Loc : Point.Locs) {
1012 Result.insert(FileFn{Loc.FileName, Loc.FunctionName});
1019 typedef std::map<FileFn, std::pair<uint32_t, uint32_t>> FunctionLocs;
1020 // finds first location in a file for each function.
1021 static FunctionLocs resolveFunctions(const SymbolizedCoverage &Coverage,
1022 const std::set<FileFn> &Fns) {
1023 FunctionLocs Result;
1024 for (const auto &Point : Coverage.Points) {
1025 for (const auto &Loc : Point.Locs) {
1026 FileFn Fn = FileFn{Loc.FileName, Loc.FunctionName};
1027 if (Fns.find(Fn) == Fns.end())
1030 auto P = std::make_pair(Loc.Line, Loc.Column);
1031 auto I = Result.find(Fn);
1032 if (I == Result.end() || I->second > P) {
1040 static void printFunctionLocs(const FunctionLocs &FnLocs, raw_ostream &OS) {
1041 for (const auto &P : FnLocs) {
1042 OS << stripPathPrefix(P.first.FileName) << ":" << P.second.first << " "
1043 << P.first.FunctionName << "\n";
1046 CoverageStats computeStats(const SymbolizedCoverage &Coverage) {
1047 CoverageStats Stats = {Coverage.Points.size(), Coverage.CoveredIds.size(),
1048 computeFunctions(Coverage.Points).size(),
1049 computeCoveredFunctions(Coverage).size()};
1053 // Print list of covered functions.
1054 // Line format: <file_name>:<line> <function_name>
1055 static void printCoveredFunctions(const SymbolizedCoverage &CovData,
1057 auto CoveredFns = computeCoveredFunctions(CovData);
1058 printFunctionLocs(resolveFunctions(CovData, CoveredFns), OS);
1061 // Print list of not covered functions.
1062 // Line format: <file_name>:<line> <function_name>
1063 static void printNotCoveredFunctions(const SymbolizedCoverage &CovData,
1065 auto NotCoveredFns = computeNotCoveredFunctions(CovData);
1066 printFunctionLocs(resolveFunctions(CovData, NotCoveredFns), OS);
1069 // Read list of files and merges their coverage info.
1070 static void readAndPrintRawCoverage(const std::vector<std::string> &FileNames,
1072 std::vector<std::unique_ptr<RawCoverage>> Covs;
1073 for (const auto &FileName : FileNames) {
1074 auto Cov = RawCoverage::read(FileName);
1081 static std::unique_ptr<SymbolizedCoverage>
1082 merge(const std::vector<std::unique_ptr<SymbolizedCoverage>> &Coverages) {
1083 if (Coverages.empty())
1086 auto Result = make_unique<SymbolizedCoverage>();
1088 for (size_t I = 0; I < Coverages.size(); ++I) {
1089 const SymbolizedCoverage &Coverage = *Coverages[I];
1091 if (Coverages.size() > 1) {
1092 // prefix is not needed when there's only one file.
1096 for (const auto &Id : Coverage.CoveredIds) {
1097 Result->CoveredIds.insert(Prefix + Id);
1100 for (const auto &CovPoint : Coverage.Points) {
1101 CoveragePoint NewPoint(CovPoint);
1102 NewPoint.Id = Prefix + CovPoint.Id;
1103 Result->Points.push_back(NewPoint);
1107 if (Coverages.size() == 1) {
1108 Result->BinaryHash = Coverages[0]->BinaryHash;
1114 static std::unique_ptr<SymbolizedCoverage>
1115 readSymbolizeAndMergeCmdArguments(std::vector<std::string> FileNames) {
1116 std::vector<std::unique_ptr<SymbolizedCoverage>> Coverages;
1119 // Short name => file name.
1120 std::map<std::string, std::string> ObjFiles;
1121 std::string FirstObjFile;
1122 std::set<std::string> CovFiles;
1124 // Partition input values into coverage/object files.
1125 for (const auto &FileName : FileNames) {
1126 if (isSymbolizedCoverageFile(FileName)) {
1127 Coverages.push_back(SymbolizedCoverage::read(FileName));
1130 auto ErrorOrIsCoverage = isCoverageFile(FileName);
1131 if (!ErrorOrIsCoverage)
1133 if (ErrorOrIsCoverage.get()) {
1134 CovFiles.insert(FileName);
1136 auto ShortFileName = llvm::sys::path::filename(FileName);
1137 if (ObjFiles.find(ShortFileName) != ObjFiles.end()) {
1138 fail("Duplicate binary file with a short name: " + ShortFileName);
1141 ObjFiles[ShortFileName] = FileName;
1142 if (FirstObjFile.empty())
1143 FirstObjFile = FileName;
1147 SmallVector<StringRef, 2> Components;
1149 // Object file => list of corresponding coverage file names.
1150 std::map<std::string, std::vector<std::string>> CoverageByObjFile;
1151 for (const auto &FileName : CovFiles) {
1152 auto ShortFileName = llvm::sys::path::filename(FileName);
1153 auto Ok = SancovFileRegex.match(ShortFileName, &Components);
1155 fail("Can't match coverage file name against "
1156 "<module_name>.<pid>.sancov pattern: " +
1160 auto Iter = ObjFiles.find(Components[1]);
1161 if (Iter == ObjFiles.end()) {
1162 fail("Object file for coverage not found: " + FileName);
1165 CoverageByObjFile[Iter->second].push_back(FileName);
1168 for (const auto &Pair : ObjFiles) {
1169 auto FileName = Pair.second;
1170 if (CoverageByObjFile.find(FileName) == CoverageByObjFile.end())
1171 errs() << "WARNING: No coverage file for " << FileName << "\n";
1174 // Read raw coverage and symbolize it.
1175 for (const auto &Pair : CoverageByObjFile) {
1176 if (findSanitizerCovFunctions(Pair.first).empty()) {
1178 << "WARNING: Ignoring " << Pair.first
1179 << " and its coverage because __sanitizer_cov* functions were not "
1184 for (const std::string &CoverageFile : Pair.second) {
1185 auto DataOrError = RawCoverage::read(CoverageFile);
1186 failIfError(DataOrError);
1187 Coverages.push_back(symbolize(*DataOrError.get(), Pair.first));
1192 return merge(Coverages);
1197 int main(int Argc, char **Argv) {
1198 // Print stack trace if we signal out.
1199 sys::PrintStackTraceOnErrorSignal(Argv[0]);
1200 PrettyStackTraceProgram X(Argc, Argv);
1201 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1203 llvm::InitializeAllTargetInfos();
1204 llvm::InitializeAllTargetMCs();
1205 llvm::InitializeAllDisassemblers();
1207 cl::ParseCommandLineOptions(Argc, Argv,
1208 "Sanitizer Coverage Processing Tool (sancov)\n\n"
1209 " This tool can extract various coverage-related information from: \n"
1210 " coverage-instrumented binary files, raw .sancov files and their "
1211 "symbolized .symcov version.\n"
1212 " Depending on chosen action the tool expects different input files:\n"
1213 " -print-coverage-pcs - coverage-instrumented binary files\n"
1214 " -print-coverage - .sancov files\n"
1215 " <other actions> - .sancov files & corresponding binary "
1216 "files, .symcov files\n"
1219 // -print doesn't need object files.
1220 if (Action == PrintAction) {
1221 readAndPrintRawCoverage(ClInputFiles, outs());
1223 } else if (Action == PrintCovPointsAction) {
1224 // -print-coverage-points doesn't need coverage files.
1225 for (const std::string &ObjFile : ClInputFiles) {
1226 printCovPoints(ObjFile, outs());
1231 auto Coverage = readSymbolizeAndMergeCmdArguments(ClInputFiles);
1232 failIf(!Coverage, "No valid coverage files given.");
1235 case CoveredFunctionsAction: {
1236 printCoveredFunctions(*Coverage, outs());
1239 case NotCoveredFunctionsAction: {
1240 printNotCoveredFunctions(*Coverage, outs());
1244 outs() << computeStats(*Coverage);
1248 case SymbolizeAction: { // merge & symbolize are synonims.
1249 JSONWriter W(outs());
1253 case HtmlReportAction:
1254 errs() << "-html-report option is removed: "
1255 "use -symbolize & coverage-report-server.py instead\n";
1258 case PrintCovPointsAction:
1259 llvm_unreachable("unsupported action");