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