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 // Large Aarch64 binaries use thunks
633 Name == "__AArch64ADRPThunk___sanitizer_cov" ||
634 Name == "__AArch64ADRPThunk___sanitizer_cov_with_check" ||
635 Name == "__AArch64ADRPThunk___sanitizer_cov_trace_func_enter" ||
636 Name == "__AArch64ADRPThunk___sanitizer_cov_trace_pc_guard";
637}
638
639// Locate __sanitizer_cov* function addresses inside the stubs table on MachO.
640static void findMachOIndirectCovFunctions(const object::MachOObjectFile &O,
641 std::set<uint64_t> *Result) {
642 MachO::dysymtab_command Dysymtab = O.getDysymtabLoadCommand();
643 MachO::symtab_command Symtab = O.getSymtabLoadCommand();
644
645 for (const auto &Load : O.load_commands()) {
646 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
647 MachO::segment_command_64 Seg = O.getSegment64LoadCommand(L: Load);
648 for (unsigned J = 0; J < Seg.nsects; ++J) {
649 MachO::section_64 Sec = O.getSection64(L: Load, Index: J);
650
651 uint32_t SectionType = Sec.flags & MachO::SECTION_TYPE;
652 if (SectionType == MachO::S_SYMBOL_STUBS) {
653 uint32_t Stride = Sec.reserved2;
654 uint32_t Cnt = Sec.size / Stride;
655 uint32_t N = Sec.reserved1;
656 for (uint32_t J = 0; J < Cnt && N + J < Dysymtab.nindirectsyms; J++) {
657 uint32_t IndirectSymbol =
658 O.getIndirectSymbolTableEntry(DLC: Dysymtab, Index: N + J);
659 uint64_t Addr = Sec.addr + J * Stride;
660 if (IndirectSymbol < Symtab.nsyms) {
661 object::SymbolRef Symbol = *(O.getSymbolByIndex(Index: IndirectSymbol));
662 Expected<StringRef> Name = Symbol.getName();
663 failIfError(E&: Name);
664 if (isCoveragePointSymbol(Name: Name.get())) {
665 Result->insert(x: Addr);
666 }
667 }
668 }
669 }
670 }
671 }
672 if (Load.C.cmd == MachO::LC_SEGMENT) {
673 errs() << "ERROR: 32 bit MachO binaries not supported\n";
674 }
675 }
676}
677
678// Locate __sanitizer_cov* function addresses that are used for coverage
679// reporting.
680static std::set<uint64_t>
681findSanitizerCovFunctions(const object::ObjectFile &O) {
682 std::set<uint64_t> Result;
683
684 for (const object::SymbolRef &Symbol : O.symbols()) {
685 Expected<uint64_t> AddressOrErr = Symbol.getAddress();
686 failIfError(E&: AddressOrErr);
687 uint64_t Address = AddressOrErr.get();
688
689 Expected<StringRef> NameOrErr = Symbol.getName();
690 failIfError(E&: NameOrErr);
691 StringRef Name = NameOrErr.get();
692
693 Expected<uint32_t> FlagsOrErr = Symbol.getFlags();
694 // TODO: Test this error.
695 failIfError(E&: FlagsOrErr);
696 uint32_t Flags = FlagsOrErr.get();
697
698 if (!(Flags & object::BasicSymbolRef::SF_Undefined) &&
699 isCoveragePointSymbol(Name)) {
700 Result.insert(x: Address);
701 }
702 }
703
704 if (const auto *CO = dyn_cast<object::COFFObjectFile>(Val: &O)) {
705 for (const object::ExportDirectoryEntryRef &Export :
706 CO->export_directories()) {
707 uint32_t RVA;
708 failIfError(Err: Export.getExportRVA(Result&: RVA));
709
710 StringRef Name;
711 failIfError(Err: Export.getSymbolName(Result&: Name));
712
713 if (isCoveragePointSymbol(Name))
714 Result.insert(x: CO->getImageBase() + RVA);
715 }
716 }
717
718 if (const auto *MO = dyn_cast<object::MachOObjectFile>(Val: &O)) {
719 findMachOIndirectCovFunctions(O: *MO, Result: &Result);
720 }
721
722 return Result;
723}
724
725// Ported from
726// compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h:GetPreviousInstructionPc
727// GetPreviousInstructionPc.
728static uint64_t getPreviousInstructionPc(uint64_t PC, Triple TheTriple) {
729 if (TheTriple.isARM())
730 return (PC - 3) & (~1);
731 if (TheTriple.isMIPS() || TheTriple.isSPARC())
732 return PC - 8;
733 if (TheTriple.isRISCV())
734 return PC - 2;
735 if (TheTriple.isX86() || TheTriple.isSystemZ())
736 return PC - 1;
737 return PC - 4;
738}
739
740// Locate addresses of all coverage points in a file. Coverage point
741// is defined as the 'address of instruction following __sanitizer_cov
742// call - 1'.
743static void getObjectCoveragePoints(const object::ObjectFile &O,
744 std::set<uint64_t> *Addrs) {
745 Triple TheTriple("unknown-unknown-unknown");
746 TheTriple.setArch(Kind: Triple::ArchType(O.getArch()));
747 auto TripleName = TheTriple.getTriple();
748
749 std::string Error;
750 const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple, Error);
751 failIfNotEmpty(E: Error);
752
753 std::unique_ptr<const MCSubtargetInfo> STI(
754 TheTarget->createMCSubtargetInfo(TheTriple, CPU: "", Features: ""));
755 failIfEmpty(Ptr: STI, Message: "no subtarget info for target " + TripleName);
756
757 std::unique_ptr<const MCRegisterInfo> MRI(
758 TheTarget->createMCRegInfo(TT: TheTriple));
759 failIfEmpty(Ptr: MRI, Message: "no register info for target " + TripleName);
760
761 MCTargetOptions MCOptions;
762 std::unique_ptr<const MCAsmInfo> AsmInfo(
763 TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple, Options: MCOptions));
764 failIfEmpty(Ptr: AsmInfo, Message: "no asm info for target " + TripleName);
765
766 MCContext Ctx(TheTriple, AsmInfo.get(), MRI.get(), STI.get());
767 std::unique_ptr<MCDisassembler> DisAsm(
768 TheTarget->createMCDisassembler(STI: *STI, Ctx));
769 failIfEmpty(Ptr: DisAsm, Message: "no disassembler info for target " + TripleName);
770
771 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
772 failIfEmpty(Ptr: MII, Message: "no instruction info for target " + TripleName);
773
774 std::unique_ptr<MCInstrAnalysis> MIA(
775 TheTarget->createMCInstrAnalysis(Info: MII.get()));
776 failIfEmpty(Ptr: MIA, Message: "no instruction analysis info for target " + TripleName);
777
778 auto SanCovAddrs = findSanitizerCovFunctions(O);
779 if (SanCovAddrs.empty())
780 fail(E: "__sanitizer_cov* functions not found");
781
782 for (object::SectionRef Section : O.sections()) {
783 if (Section.isVirtual() || !Section.isText()) // llvm-objdump does the same.
784 continue;
785 uint64_t SectionAddr = Section.getAddress();
786 uint64_t SectSize = Section.getSize();
787 if (!SectSize)
788 continue;
789
790 Expected<StringRef> BytesStr = Section.getContents();
791 failIfError(E&: BytesStr);
792 ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(Input: *BytesStr);
793
794 if (MIA)
795 MIA->resetState();
796
797 for (uint64_t Index = 0, Size = 0; Index < Section.getSize();
798 Index += Size) {
799 MCInst Inst;
800 ArrayRef<uint8_t> ThisBytes = Bytes.slice(N: Index);
801 uint64_t ThisAddr = SectionAddr + Index;
802 if (!DisAsm->getInstruction(Instr&: Inst, Size, Bytes: ThisBytes, Address: ThisAddr, CStream&: nulls())) {
803 if (Size == 0)
804 Size = std::min<uint64_t>(
805 a: ThisBytes.size(),
806 b: DisAsm->suggestBytesToSkip(Bytes: ThisBytes, Address: ThisAddr));
807 MIA->resetState();
808 continue;
809 }
810 uint64_t Addr = Index + SectionAddr;
811 // Sanitizer coverage uses the address of the next instruction - 1.
812 uint64_t CovPoint = getPreviousInstructionPc(PC: Addr + Size, TheTriple);
813 uint64_t Target;
814 if (MIA->isCall(Inst) &&
815 MIA->evaluateBranch(Inst, Addr: SectionAddr + Index, Size, Target) &&
816 SanCovAddrs.find(x: Target) != SanCovAddrs.end())
817 Addrs->insert(x: CovPoint);
818 MIA->updateState(Inst, STI: STI.get(), Addr);
819 }
820 }
821}
822
823static void
824visitObjectFiles(const object::Archive &A,
825 function_ref<void(const object::ObjectFile &)> Fn) {
826 Error Err = Error::success();
827 for (auto &C : A.children(Err)) {
828 Expected<std::unique_ptr<object::Binary>> ChildOrErr = C.getAsBinary();
829 failIfError(E&: ChildOrErr);
830 if (auto *O = dyn_cast<object::ObjectFile>(Val: &*ChildOrErr.get()))
831 Fn(*O);
832 else
833 failIfError(Error: object::object_error::invalid_file_type);
834 }
835 failIfError(Err: std::move(Err));
836}
837
838static void
839visitObjectFiles(const std::string &FileName,
840 function_ref<void(const object::ObjectFile &)> Fn) {
841 Expected<object::OwningBinary<object::Binary>> BinaryOrErr =
842 object::createBinary(Path: FileName);
843 if (!BinaryOrErr)
844 failIfError(E&: BinaryOrErr);
845
846 object::Binary &Binary = *BinaryOrErr.get().getBinary();
847 if (object::Archive *A = dyn_cast<object::Archive>(Val: &Binary))
848 visitObjectFiles(A: *A, Fn);
849 else if (object::ObjectFile *O = dyn_cast<object::ObjectFile>(Val: &Binary))
850 Fn(*O);
851 else
852 failIfError(Error: object::object_error::invalid_file_type);
853}
854
855static std::set<uint64_t>
856findSanitizerCovFunctions(const std::string &FileName) {
857 std::set<uint64_t> Result;
858 visitObjectFiles(FileName, Fn: [&](const object::ObjectFile &O) {
859 auto Addrs = findSanitizerCovFunctions(O);
860 Result.insert(first: Addrs.begin(), last: Addrs.end());
861 });
862 return Result;
863}
864
865// Locate addresses of all coverage points in a file. Coverage point
866// is defined as the 'address of instruction following __sanitizer_cov
867// call - 1'.
868static std::set<uint64_t> findCoveragePointAddrs(const std::string &FileName) {
869 std::set<uint64_t> Result;
870 visitObjectFiles(FileName, Fn: [&](const object::ObjectFile &O) {
871 getObjectCoveragePoints(O, Addrs: &Result);
872 });
873 return Result;
874}
875
876static void printCovPoints(const std::string &ObjFile, raw_ostream &OS) {
877 for (uint64_t Addr : findCoveragePointAddrs(FileName: ObjFile)) {
878 OS << "0x";
879 OS.write_hex(N: Addr);
880 OS << "\n";
881 }
882}
883
884static ErrorOr<bool> isCoverageFile(const std::string &FileName) {
885 auto ShortFileName = llvm::sys::path::filename(path: FileName);
886 if (!SancovFileRegex.match(String: ShortFileName))
887 return false;
888
889 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
890 MemoryBuffer::getFile(Filename: FileName);
891 if (!BufOrErr) {
892 errs() << "Warning: " << BufOrErr.getError().message() << "("
893 << BufOrErr.getError().value()
894 << "), filename: " << llvm::sys::path::filename(path: FileName) << "\n";
895 return BufOrErr.getError();
896 }
897 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
898 if (Buf->getBufferSize() < 8) {
899 return false;
900 }
901 const FileHeader *Header =
902 reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
903 return Header->Magic == BinCoverageMagic;
904}
905
906static bool isSymbolizedCoverageFile(const std::string &FileName) {
907 auto ShortFileName = llvm::sys::path::filename(path: FileName);
908 return SymcovFileRegex.match(String: ShortFileName);
909}
910
911static std::unique_ptr<SymbolizedCoverage>
912symbolize(const RawCoverage &Data, const std::string ObjectFile) {
913 auto Coverage = std::make_unique<SymbolizedCoverage>();
914
915 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
916 MemoryBuffer::getFile(Filename: ObjectFile);
917 failIfError(E: BufOrErr);
918 SHA1 Hasher;
919 Hasher.update(Str: (*BufOrErr)->getBuffer());
920 Coverage->BinaryHash = toHex(Input: Hasher.final());
921
922 Ignorelists Ig;
923 auto Symbolizer(createSymbolizer());
924
925 for (uint64_t Addr : *Data.Addrs) {
926 // TODO: it would be neccessary to set proper section index here.
927 // object::SectionedAddress::UndefSection works for only absolute addresses.
928 auto LineInfo = Symbolizer->symbolizeCode(
929 ModuleName: ObjectFile, ModuleOffset: {.Address: Addr, .SectionIndex: object::SectionedAddress::UndefSection});
930 failIfError(E&: LineInfo);
931 if (Ig.isIgnorelisted(I: *LineInfo))
932 continue;
933
934 Coverage->CoveredIds.insert(x: utohexstr(X: Addr, LowerCase: true));
935 }
936
937 std::set<uint64_t> AllAddrs = findCoveragePointAddrs(FileName: ObjectFile);
938 if (!llvm::includes(Range1&: AllAddrs, Range2&: *Data.Addrs)) {
939 fail(E: "Coverage points in binary and .sancov file do not match.");
940 }
941 Coverage->Points = getCoveragePoints(ObjectFile, Addrs: AllAddrs, CoveredAddrs: *Data.Addrs);
942 return Coverage;
943}
944
945struct FileFn {
946 bool operator<(const FileFn &RHS) const {
947 return std::tie(args: FileName, args: FunctionName) <
948 std::tie(args: RHS.FileName, args: RHS.FunctionName);
949 }
950
951 std::string FileName;
952 std::string FunctionName;
953};
954
955static std::set<FileFn>
956computeFunctions(const std::vector<CoveragePoint> &Points) {
957 std::set<FileFn> Fns;
958 for (const auto &Point : Points) {
959 for (const auto &Loc : Point.Locs) {
960 Fns.insert(x: FileFn{.FileName: Loc.FileName, .FunctionName: Loc.FunctionName});
961 }
962 }
963 return Fns;
964}
965
966static std::set<FileFn>
967computeNotCoveredFunctions(const SymbolizedCoverage &Coverage) {
968 auto Fns = computeFunctions(Points: Coverage.Points);
969
970 for (const auto &Point : Coverage.Points) {
971 if (Coverage.CoveredIds.find(x: Point.Id) == Coverage.CoveredIds.end())
972 continue;
973
974 for (const auto &Loc : Point.Locs) {
975 Fns.erase(x: FileFn{.FileName: Loc.FileName, .FunctionName: Loc.FunctionName});
976 }
977 }
978
979 return Fns;
980}
981
982static std::set<FileFn>
983computeCoveredFunctions(const SymbolizedCoverage &Coverage) {
984 auto AllFns = computeFunctions(Points: Coverage.Points);
985 std::set<FileFn> Result;
986
987 for (const auto &Point : Coverage.Points) {
988 if (Coverage.CoveredIds.find(x: Point.Id) == Coverage.CoveredIds.end())
989 continue;
990
991 for (const auto &Loc : Point.Locs) {
992 Result.insert(x: FileFn{.FileName: Loc.FileName, .FunctionName: Loc.FunctionName});
993 }
994 }
995
996 return Result;
997}
998
999typedef std::map<FileFn, std::pair<uint32_t, uint32_t>> FunctionLocs;
1000// finds first location in a file for each function.
1001static FunctionLocs resolveFunctions(const SymbolizedCoverage &Coverage,
1002 const std::set<FileFn> &Fns) {
1003 FunctionLocs Result;
1004 for (const auto &Point : Coverage.Points) {
1005 for (const auto &Loc : Point.Locs) {
1006 FileFn Fn = FileFn{.FileName: Loc.FileName, .FunctionName: Loc.FunctionName};
1007 if (Fns.find(x: Fn) == Fns.end())
1008 continue;
1009
1010 auto P = std::make_pair(x: Loc.Line, y: Loc.Column);
1011 auto [It, Inserted] = Result.try_emplace(k: Fn, args&: P);
1012 if (!Inserted && It->second > P)
1013 It->second = P;
1014 }
1015 }
1016 return Result;
1017}
1018
1019static void printFunctionLocs(const FunctionLocs &FnLocs, raw_ostream &OS) {
1020 for (const auto &P : FnLocs) {
1021 OS << stripPathPrefix(Path: P.first.FileName) << ":" << P.second.first << " "
1022 << P.first.FunctionName << "\n";
1023 }
1024}
1025CoverageStats computeStats(const SymbolizedCoverage &Coverage) {
1026 CoverageStats Stats = {.AllPoints: Coverage.Points.size(), .CovPoints: Coverage.CoveredIds.size(),
1027 .AllFns: computeFunctions(Points: Coverage.Points).size(),
1028 .CovFns: computeCoveredFunctions(Coverage).size()};
1029 return Stats;
1030}
1031
1032// Print list of covered functions.
1033// Line format: <file_name>:<line> <function_name>
1034static void printCoveredFunctions(const SymbolizedCoverage &CovData,
1035 raw_ostream &OS) {
1036 auto CoveredFns = computeCoveredFunctions(Coverage: CovData);
1037 printFunctionLocs(FnLocs: resolveFunctions(Coverage: CovData, Fns: CoveredFns), OS);
1038}
1039
1040// Print list of not covered functions.
1041// Line format: <file_name>:<line> <function_name>
1042static void printNotCoveredFunctions(const SymbolizedCoverage &CovData,
1043 raw_ostream &OS) {
1044 auto NotCoveredFns = computeNotCoveredFunctions(Coverage: CovData);
1045 printFunctionLocs(FnLocs: resolveFunctions(Coverage: CovData, Fns: NotCoveredFns), OS);
1046}
1047
1048// Read list of files and merges their coverage info.
1049static void readAndPrintRawCoverage(const std::vector<std::string> &FileNames,
1050 raw_ostream &OS) {
1051 for (const auto &FileName : FileNames) {
1052 auto Cov = RawCoverage::read(FileName);
1053 if (!Cov)
1054 continue;
1055 OS << *Cov.get();
1056 }
1057}
1058
1059static const char *bitnessToString(uint32_t Bitness) {
1060 switch (Bitness) {
1061 case Bitness64:
1062 return "64-bit";
1063 case Bitness32:
1064 return "32-bit";
1065 default:
1066 fail(E: "Unsupported bitness: " + std::to_string(val: Bitness));
1067 return nullptr;
1068 }
1069}
1070
1071// Warn if two file headers have different bitness.
1072static void warnIfDifferentBitness(const FileHeader &Header1,
1073 const FileHeader &Header2,
1074 const std::string &File1Desc,
1075 const std::string &File2Desc) {
1076 if (Header1.Bitness != Header2.Bitness) {
1077 errs() << "WARNING: Input files have different bitness (" << File1Desc
1078 << ": " << bitnessToString(Bitness: Header1.Bitness) << ", " << File2Desc
1079 << ": " << bitnessToString(Bitness: Header2.Bitness)
1080 << "). Using bitness from " << File1Desc << ".\n";
1081
1082 if (Header1.Bitness == Bitness32 && Header2.Bitness == Bitness64) {
1083 errs() << "WARNING: 64-bit addresses will be truncated to 32 bits. "
1084 << "This may result in data loss.\n";
1085 }
1086 }
1087}
1088
1089// Compute difference between two coverage files (A - B) and write to output
1090// file.
1091static void diffRawCoverage(const std::string &FileA, const std::string &FileB,
1092 const std::string &OutputFile) {
1093 auto CovA = RawCoverage::read(FileName: FileA);
1094 failIfError(E: CovA);
1095
1096 auto CovB = RawCoverage::read(FileName: FileB);
1097 failIfError(E: CovB);
1098
1099 const FileHeader &HeaderA = CovA.get()->Header;
1100 const FileHeader &HeaderB = CovB.get()->Header;
1101
1102 warnIfDifferentBitness(Header1: HeaderA, Header2: HeaderB, File1Desc: FileA, File2Desc: FileB);
1103
1104 // Compute A - B
1105 auto DiffAddrs = std::make_unique<std::set<uint64_t>>();
1106 std::set_difference(first1: CovA.get()->Addrs->begin(), last1: CovA.get()->Addrs->end(),
1107 first2: CovB.get()->Addrs->begin(), last2: CovB.get()->Addrs->end(),
1108 result: std::inserter(x&: *DiffAddrs, i: DiffAddrs->end()));
1109
1110 RawCoverage DiffCov(std::move(DiffAddrs), HeaderA);
1111 RawCoverage::write(FileName: OutputFile, Coverage: DiffCov);
1112}
1113
1114// Compute union of multiple coverage files and write to output file.
1115static void unionRawCoverage(const std::vector<std::string> &InputFiles,
1116 const std::string &OutputFile) {
1117 failIf(B: InputFiles.empty(), E: "union action requires at least one input file");
1118
1119 // Read the first file to get the header and initial coverage
1120 auto UnionCov = RawCoverage::read(FileName: InputFiles[0]);
1121 failIfError(E: UnionCov);
1122
1123 const FileHeader &UnionHeader = UnionCov.get()->Header;
1124
1125 for (size_t I = 1; I < InputFiles.size(); ++I) {
1126 auto Cov = RawCoverage::read(FileName: InputFiles[I]);
1127 failIfError(E: Cov);
1128
1129 const FileHeader &CurHeader = Cov.get()->Header;
1130
1131 warnIfDifferentBitness(Header1: UnionHeader, Header2: CurHeader, File1Desc: InputFiles[0],
1132 File2Desc: InputFiles[I]);
1133
1134 UnionCov.get()->Addrs->insert(first: Cov.get()->Addrs->begin(),
1135 last: Cov.get()->Addrs->end());
1136 }
1137
1138 RawCoverage::write(FileName: OutputFile, Coverage: *UnionCov.get());
1139}
1140
1141static std::unique_ptr<SymbolizedCoverage>
1142merge(const std::vector<std::unique_ptr<SymbolizedCoverage>> &Coverages) {
1143 if (Coverages.empty())
1144 return nullptr;
1145
1146 auto Result = std::make_unique<SymbolizedCoverage>();
1147
1148 for (size_t I = 0; I < Coverages.size(); ++I) {
1149 const SymbolizedCoverage &Coverage = *Coverages[I];
1150 std::string Prefix;
1151 if (Coverages.size() > 1) {
1152 // prefix is not needed when there's only one file.
1153 Prefix = utostr(X: I);
1154 }
1155
1156 for (const auto &Id : Coverage.CoveredIds) {
1157 Result->CoveredIds.insert(x: Prefix + Id);
1158 }
1159
1160 for (const auto &CovPoint : Coverage.Points) {
1161 CoveragePoint NewPoint(CovPoint);
1162 NewPoint.Id = Prefix + CovPoint.Id;
1163 Result->Points.push_back(x: NewPoint);
1164 }
1165 }
1166
1167 if (Coverages.size() == 1) {
1168 Result->BinaryHash = Coverages[0]->BinaryHash;
1169 }
1170
1171 return Result;
1172}
1173
1174static std::unique_ptr<SymbolizedCoverage>
1175readSymbolizeAndMergeCmdArguments(std::vector<std::string> FileNames) {
1176 std::vector<std::unique_ptr<SymbolizedCoverage>> Coverages;
1177
1178 {
1179 // Short name => file name.
1180 std::map<std::string, std::string, std::less<>> ObjFiles;
1181 std::string FirstObjFile;
1182 std::set<std::string> CovFiles;
1183
1184 // Partition input values into coverage/object files.
1185 for (const auto &FileName : FileNames) {
1186 if (isSymbolizedCoverageFile(FileName)) {
1187 Coverages.push_back(x: SymbolizedCoverage::read(InputFile: FileName));
1188 }
1189
1190 auto ErrorOrIsCoverage = isCoverageFile(FileName);
1191 if (!ErrorOrIsCoverage)
1192 continue;
1193 if (ErrorOrIsCoverage.get()) {
1194 CovFiles.insert(x: FileName);
1195 } else {
1196 auto ShortFileName = llvm::sys::path::filename(path: FileName);
1197 if (ObjFiles.find(x: ShortFileName) != ObjFiles.end()) {
1198 fail(E: "Duplicate binary file with a short name: " + ShortFileName);
1199 }
1200
1201 ObjFiles[std::string(ShortFileName)] = FileName;
1202 if (FirstObjFile.empty())
1203 FirstObjFile = FileName;
1204 }
1205 }
1206
1207 SmallVector<StringRef, 2> Components;
1208
1209 // Object file => list of corresponding coverage file names.
1210 std::map<std::string, std::vector<std::string>> CoverageByObjFile;
1211 for (const auto &FileName : CovFiles) {
1212 auto ShortFileName = llvm::sys::path::filename(path: FileName);
1213 auto Ok = SancovFileRegex.match(String: ShortFileName, Matches: &Components);
1214 if (!Ok) {
1215 fail(E: "Can't match coverage file name against "
1216 "<module_name>.<pid>.sancov pattern: " +
1217 FileName);
1218 }
1219
1220 auto Iter = ObjFiles.find(x: Components[1]);
1221 if (Iter == ObjFiles.end()) {
1222 fail(E: "Object file for coverage not found: " + FileName);
1223 }
1224
1225 CoverageByObjFile[Iter->second].push_back(x: FileName);
1226 };
1227
1228 for (const auto &Pair : ObjFiles) {
1229 auto FileName = Pair.second;
1230 if (CoverageByObjFile.find(x: FileName) == CoverageByObjFile.end())
1231 errs() << "WARNING: No coverage file for " << FileName << "\n";
1232 }
1233
1234 // Read raw coverage and symbolize it.
1235 for (const auto &Pair : CoverageByObjFile) {
1236 if (findSanitizerCovFunctions(FileName: Pair.first).empty()) {
1237 errs()
1238 << "WARNING: Ignoring " << Pair.first
1239 << " and its coverage because __sanitizer_cov* functions were not "
1240 "found.\n";
1241 continue;
1242 }
1243
1244 for (const std::string &CoverageFile : Pair.second) {
1245 auto DataOrError = RawCoverage::read(FileName: CoverageFile);
1246 failIfError(E: DataOrError);
1247 Coverages.push_back(x: symbolize(Data: *DataOrError.get(), ObjectFile: Pair.first));
1248 }
1249 }
1250 }
1251
1252 return merge(Coverages);
1253}
1254
1255} // namespace
1256
1257static void parseArgs(int Argc, char **Argv) {
1258 SancovOptTable Tbl;
1259 llvm::BumpPtrAllocator A;
1260 llvm::StringSaver Saver{A};
1261 opt::InputArgList Args =
1262 Tbl.parseArgs(Argc, Argv, Unknown: OPT_UNKNOWN, Saver, ErrorFn: [&](StringRef Msg) {
1263 llvm::errs() << Msg << '\n';
1264 std::exit(status: 1);
1265 });
1266
1267 if (Args.hasArg(Ids: OPT_help)) {
1268 Tbl.printHelp(
1269 OS&: llvm::outs(),
1270 Usage: "sancov [options] <action> <binary files...> <.sancov files...> "
1271 "<.symcov files...>",
1272 Title: "Sanitizer Coverage Processing Tool (sancov)\n\n"
1273 " This tool can extract various coverage-related information from: \n"
1274 " coverage-instrumented binary files, raw .sancov files and their "
1275 "symbolized .symcov version.\n"
1276 " Depending on chosen action the tool expects different input files:\n"
1277 " -print-coverage-pcs - coverage-instrumented binary files\n"
1278 " -print-coverage - .sancov files\n"
1279 " -diff - two .sancov files & --output option\n"
1280 " -union - one or more .sancov files & --output "
1281 "option\n"
1282 " <other actions> - .sancov files & corresponding binary "
1283 "files, .symcov files\n");
1284 std::exit(status: 0);
1285 }
1286
1287 if (Args.hasArg(Ids: OPT_version)) {
1288 cl::PrintVersionMessage();
1289 std::exit(status: 0);
1290 }
1291
1292 if (Args.hasMultipleArgs(Id: OPT_action_grp)) {
1293 fail(E: "Only one action option is allowed");
1294 }
1295
1296 for (const opt::Arg *A : Args.filtered(Ids: OPT_INPUT)) {
1297 ClInputFiles.emplace_back(args: A->getValue());
1298 }
1299
1300 if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_action_grp)) {
1301 switch (A->getOption().getID()) {
1302 case OPT_print:
1303 Action = ActionType::PrintAction;
1304 break;
1305 case OPT_diff:
1306 Action = ActionType::DiffAction;
1307 break;
1308 case OPT_union_files:
1309 Action = ActionType::UnionAction;
1310 break;
1311 case OPT_printCoveragePcs:
1312 Action = ActionType::PrintCovPointsAction;
1313 break;
1314 case OPT_coveredFunctions:
1315 Action = ActionType::CoveredFunctionsAction;
1316 break;
1317 case OPT_notCoveredFunctions:
1318 Action = ActionType::NotCoveredFunctionsAction;
1319 break;
1320 case OPT_printCoverageStats:
1321 Action = ActionType::StatsAction;
1322 break;
1323 case OPT_htmlReport:
1324 Action = ActionType::HtmlReportAction;
1325 break;
1326 case OPT_symbolize:
1327 Action = ActionType::SymbolizeAction;
1328 break;
1329 case OPT_merge:
1330 Action = ActionType::MergeAction;
1331 break;
1332 default:
1333 fail(E: "Invalid Action");
1334 }
1335 }
1336
1337 ClDemangle = Args.hasFlag(Pos: OPT_demangle, Neg: OPT_no_demangle, Default: true);
1338 ClSkipDeadFiles = Args.hasFlag(Pos: OPT_skipDeadFiles, Neg: OPT_no_skipDeadFiles, Default: true);
1339 ClUseDefaultIgnorelist =
1340 Args.hasFlag(Pos: OPT_useDefaultIgnoreList, Neg: OPT_no_useDefaultIgnoreList, Default: true);
1341
1342 ClStripPathPrefix = Args.getLastArgValue(Id: OPT_stripPathPrefix_EQ);
1343 ClIgnorelist = Args.getLastArgValue(Id: OPT_ignorelist_EQ);
1344 ClOutputFile = Args.getLastArgValue(Id: OPT_output_EQ);
1345}
1346
1347int sancov_main(int Argc, char **Argv, const llvm::ToolContext &) {
1348 llvm::InitializeAllTargetInfos();
1349 llvm::InitializeAllTargetMCs();
1350 llvm::InitializeAllDisassemblers();
1351
1352 parseArgs(Argc, Argv);
1353
1354 // -print doesn't need object files.
1355 if (Action == PrintAction) {
1356 readAndPrintRawCoverage(FileNames: ClInputFiles, OS&: outs());
1357 return 0;
1358 }
1359 if (Action == DiffAction) {
1360 // -diff requires exactly 2 input files and an output file.
1361 failIf(B: ClInputFiles.size() != 2,
1362 E: "diff action requires exactly 2 input sancov files");
1363 failIf(
1364 B: ClOutputFile.empty(),
1365 E: "diff action requires --output option to specify output sancov file");
1366 diffRawCoverage(FileA: ClInputFiles[0], FileB: ClInputFiles[1], OutputFile: ClOutputFile);
1367 return 0;
1368 }
1369 if (Action == UnionAction) {
1370 // -union requires at least 1 input file and an output file.
1371 failIf(B: ClInputFiles.empty(),
1372 E: "union action requires at least one input sancov file");
1373 failIf(
1374 B: ClOutputFile.empty(),
1375 E: "union action requires --output option to specify output sancov file");
1376 unionRawCoverage(InputFiles: ClInputFiles, OutputFile: ClOutputFile);
1377 return 0;
1378 }
1379 if (Action == PrintCovPointsAction) {
1380 // -print-coverage-points doesn't need coverage files.
1381 for (const std::string &ObjFile : ClInputFiles) {
1382 printCovPoints(ObjFile, OS&: outs());
1383 }
1384 return 0;
1385 }
1386
1387 auto Coverage = readSymbolizeAndMergeCmdArguments(FileNames: ClInputFiles);
1388 failIf(B: !Coverage, E: "No valid coverage files given.");
1389
1390 switch (Action) {
1391 case CoveredFunctionsAction: {
1392 printCoveredFunctions(CovData: *Coverage, OS&: outs());
1393 return 0;
1394 }
1395 case NotCoveredFunctionsAction: {
1396 printNotCoveredFunctions(CovData: *Coverage, OS&: outs());
1397 return 0;
1398 }
1399 case StatsAction: {
1400 outs() << computeStats(Coverage: *Coverage);
1401 return 0;
1402 }
1403 case MergeAction:
1404 case SymbolizeAction: { // merge & symbolize are synonims.
1405 json::OStream W(outs(), 2);
1406 W << *Coverage;
1407 return 0;
1408 }
1409 case HtmlReportAction:
1410 errs() << "-html-report option is removed: "
1411 "use -symbolize & coverage-report-server.py instead\n";
1412 return 1;
1413 case DiffAction:
1414 case UnionAction:
1415 case PrintAction:
1416 case PrintCovPointsAction:
1417 llvm_unreachable("unsupported action");
1418 }
1419
1420 return 0;
1421}
1422