1 //===-- sancov.cc --------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a command-line tool for reading and analyzing sanitizer
12 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDisassembler.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstPrinter.h"
20 #include "llvm/MC/MCInstrAnalysis.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCObjectFileInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/Object/Binary.h"
26 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Errc.h"
29 #include "llvm/Support/ErrorOr.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/LineIterator.h"
32 #include "llvm/Support/ManagedStatic.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/Path.h"
35 #include "llvm/Support/PrettyStackTrace.h"
36 #include "llvm/Support/Signals.h"
37 #include "llvm/Support/TargetRegistry.h"
38 #include "llvm/Support/TargetSelect.h"
39 #include "llvm/Support/ToolOutputFile.h"
40 #include "llvm/Support/raw_ostream.h"
51 // --------- COMMAND LINE FLAGS ---------
55 CoveredFunctionsAction,
56 NotCoveredFunctionsAction
59 cl::opt<ActionType> Action(
60 cl::desc("Action (required)"), cl::Required,
61 cl::values(clEnumValN(PrintAction, "print", "Print coverage addresses"),
62 clEnumValN(CoveredFunctionsAction, "covered-functions",
63 "Print all covered funcions."),
64 clEnumValN(NotCoveredFunctionsAction, "not-covered-functions",
65 "Print all not covered funcions."),
68 static cl::list<std::string> ClInputFiles(cl::Positional, cl::OneOrMore,
69 cl::desc("<filenames...>"));
71 static cl::opt<std::string>
72 ClBinaryName("obj", cl::Required,
73 cl::desc("Path to object file to be symbolized"));
76 ClDemangle("demangle", cl::init(true),
77 cl::desc("Print demangled function name."));
79 static cl::opt<std::string> ClStripPathPrefix(
80 "strip_path_prefix", cl::init(""),
81 cl::desc("Strip this prefix from file paths in reports."));
83 // --------- FORMAT SPECIFICATION ---------
90 static const uint32_t BinCoverageMagic = 0xC0BFFFFF;
91 static const uint32_t Bitness32 = 0xFFFFFF32;
92 static const uint32_t Bitness64 = 0xFFFFFF64;
96 static void FailIfError(std::error_code Error) {
99 errs() << "Error: " << Error.message() << "(" << Error.value() << ")\n";
103 template <typename T> static void FailIfError(const ErrorOr<T> &E) {
104 FailIfError(E.getError());
107 static void FailIfNotEmpty(const std::string &E) {
110 errs() << "Error: " << E << "\n";
114 template <typename T>
115 static void FailIfEmpty(const std::unique_ptr<T> &Ptr,
116 const std::string &Message) {
119 errs() << "Error: " << Message << "\n";
123 template <typename T>
124 static void readInts(const char *Start, const char *End,
125 std::set<uint64_t> *Ints) {
126 const T *S = reinterpret_cast<const T *>(Start);
127 const T *E = reinterpret_cast<const T *>(End);
128 std::copy(S, E, std::inserter(*Ints, Ints->end()));
132 bool operator<(const FileLoc &RHS) const {
133 return std::tie(FileName, Line) < std::tie(RHS.FileName, RHS.Line);
136 std::string FileName;
141 bool operator<(const FunctionLoc &RHS) const {
142 return std::tie(Loc, FunctionName) < std::tie(RHS.Loc, RHS.FunctionName);
146 std::string FunctionName;
149 std::string stripPathPrefix(std::string Path) {
150 if (ClStripPathPrefix.empty())
152 size_t Pos = Path.find(ClStripPathPrefix);
153 if (Pos == std::string::npos)
155 return Path.substr(Pos + ClStripPathPrefix.size());
158 // Compute [FileLoc -> FunctionName] map for given addresses.
159 static std::map<FileLoc, std::string>
160 computeFunctionsMap(const std::set<uint64_t> &Addrs) {
161 std::map<FileLoc, std::string> Fns;
163 symbolize::LLVMSymbolizer::Options SymbolizerOptions;
164 SymbolizerOptions.Demangle = ClDemangle;
165 SymbolizerOptions.UseSymbolTable = true;
166 symbolize::LLVMSymbolizer Symbolizer(SymbolizerOptions);
169 for (auto Addr : Addrs) {
170 auto InliningInfo = Symbolizer.symbolizeInlinedCode(ClBinaryName, Addr);
171 FailIfError(InliningInfo);
172 for (uint32_t i = 0; i < InliningInfo->getNumberOfFrames(); ++i) {
173 auto FrameInfo = InliningInfo->getFrame(i);
174 SmallString<256> FileName(FrameInfo.FileName);
175 sys::path::remove_dots(FileName, /* remove_dot_dot */ true);
176 FileLoc Loc = {FileName.str(), FrameInfo.Line};
177 Fns[Loc] = FrameInfo.FunctionName;
184 // Compute functions for given addresses. It keeps only the first
185 // occurence of a function within a file.
186 std::set<FunctionLoc> computeFunctionLocs(const std::set<uint64_t> &Addrs) {
187 std::map<FileLoc, std::string> Fns = computeFunctionsMap(Addrs);
189 std::set<FunctionLoc> result;
190 std::string LastFileName;
191 std::set<std::string> ProcessedFunctions;
193 for (const auto &P : Fns) {
194 std::string FileName = P.first.FileName;
195 std::string FunctionName = P.second;
197 if (LastFileName != FileName)
198 ProcessedFunctions.clear();
199 LastFileName = FileName;
201 if (!ProcessedFunctions.insert(FunctionName).second)
204 result.insert(FunctionLoc{P.first, P.second});
210 // Locate __sanitizer_cov* function addresses that are used for coverage
212 static std::set<uint64_t>
213 findSanitizerCovFunctions(const object::ObjectFile &O) {
214 std::set<uint64_t> Result;
216 for (const object::SymbolRef &Symbol : O.symbols()) {
217 ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
218 FailIfError(AddressOrErr);
220 ErrorOr<StringRef> NameOrErr = Symbol.getName();
221 FailIfError(NameOrErr);
222 StringRef Name = NameOrErr.get();
224 if (Name == "__sanitizer_cov" || Name == "__sanitizer_cov_with_check" ||
225 Name == "__sanitizer_cov_trace_func_enter") {
226 Result.insert(AddressOrErr.get());
231 FailIfNotEmpty("__sanitizer_cov* functions not found");
236 // Locate addresses of all coverage points in a file. Coverage point
237 // is defined as the 'address of instruction following __sanitizer_cov
239 static void getObjectCoveragePoints(const object::ObjectFile &O,
240 std::set<uint64_t> *Addrs) {
241 Triple TheTriple("unknown-unknown-unknown");
242 TheTriple.setArch(Triple::ArchType(O.getArch()));
243 auto TripleName = TheTriple.getTriple();
246 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
247 FailIfNotEmpty(Error);
249 std::unique_ptr<const MCSubtargetInfo> STI(
250 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
251 FailIfEmpty(STI, "no subtarget info for target " + TripleName);
253 std::unique_ptr<const MCRegisterInfo> MRI(
254 TheTarget->createMCRegInfo(TripleName));
255 FailIfEmpty(MRI, "no register info for target " + TripleName);
257 std::unique_ptr<const MCAsmInfo> AsmInfo(
258 TheTarget->createMCAsmInfo(*MRI, TripleName));
259 FailIfEmpty(AsmInfo, "no asm info for target " + TripleName);
261 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
262 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
263 std::unique_ptr<MCDisassembler> DisAsm(
264 TheTarget->createMCDisassembler(*STI, Ctx));
265 FailIfEmpty(DisAsm, "no disassembler info for target " + TripleName);
267 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
268 FailIfEmpty(MII, "no instruction info for target " + TripleName);
270 std::unique_ptr<const MCInstrAnalysis> MIA(
271 TheTarget->createMCInstrAnalysis(MII.get()));
272 FailIfEmpty(MIA, "no instruction analysis info for target " + TripleName);
274 auto SanCovAddrs = findSanitizerCovFunctions(O);
276 for (const auto Section : O.sections()) {
277 if (Section.isVirtual() || !Section.isText()) // llvm-objdump does the same.
279 uint64_t SectionAddr = Section.getAddress();
280 uint64_t SectSize = Section.getSize();
284 StringRef SectionName;
285 FailIfError(Section.getName(SectionName));
288 FailIfError(Section.getContents(BytesStr));
289 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
292 for (uint64_t Index = 0, Size = 0; Index < Section.getSize();
295 if (!DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
296 SectionAddr + Index, nulls(), nulls())) {
302 if (MIA->isCall(Inst) &&
303 MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
304 if (SanCovAddrs.find(Target) != SanCovAddrs.end()) {
305 // Sanitizer coverage uses the address of the next instruction - 1.
306 Addrs->insert(Index + SectionAddr + Size - 1);
313 static void getArchiveCoveragePoints(const object::Archive &A,
314 std::set<uint64_t> *Addrs) {
315 for (auto &ErrorOrChild : A.children()) {
316 FailIfError(ErrorOrChild);
317 const object::Archive::Child &C = *ErrorOrChild;
318 ErrorOr<std::unique_ptr<object::Binary>> ChildOrErr = C.getAsBinary();
319 FailIfError(ChildOrErr);
320 if (object::ObjectFile *O =
321 dyn_cast<object::ObjectFile>(&*ChildOrErr.get()))
322 getObjectCoveragePoints(*O, Addrs);
324 FailIfError(object::object_error::invalid_file_type);
328 // Locate addresses of all coverage points in a file. Coverage point
329 // is defined as the 'address of instruction following __sanitizer_cov
331 std::set<uint64_t> getCoveragePoints(std::string FileName) {
332 std::set<uint64_t> Result;
334 ErrorOr<object::OwningBinary<object::Binary>> BinaryOrErr =
335 object::createBinary(FileName);
336 FailIfError(BinaryOrErr);
338 object::Binary &Binary = *BinaryOrErr.get().getBinary();
339 if (object::Archive *A = dyn_cast<object::Archive>(&Binary))
340 getArchiveCoveragePoints(*A, &Result);
341 else if (object::ObjectFile *O = dyn_cast<object::ObjectFile>(&Binary))
342 getObjectCoveragePoints(*O, &Result);
344 FailIfError(object::object_error::invalid_file_type);
349 static void printFunctionLocs(const std::set<FunctionLoc> &FnLocs,
351 for (const FunctionLoc &FnLoc : FnLocs) {
352 OS << stripPathPrefix(FnLoc.Loc.FileName) << ":" << FnLoc.Loc.Line << " "
353 << FnLoc.FunctionName << "\n";
359 // Read single file coverage data.
360 static ErrorOr<std::unique_ptr<CoverageData>> read(std::string FileName) {
361 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
362 MemoryBuffer::getFile(FileName);
364 return BufOrErr.getError();
365 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
366 if (Buf->getBufferSize() < 8) {
367 errs() << "File too small (<8): " << Buf->getBufferSize();
368 return make_error_code(errc::illegal_byte_sequence);
370 const FileHeader *Header =
371 reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
373 if (Header->Magic != BinCoverageMagic) {
374 errs() << "Wrong magic: " << Header->Magic;
375 return make_error_code(errc::illegal_byte_sequence);
378 auto Addrs = llvm::make_unique<std::set<uint64_t>>();
380 switch (Header->Bitness) {
382 readInts<uint64_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),
386 readInts<uint32_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),
390 errs() << "Unsupported bitness: " << Header->Bitness;
391 return make_error_code(errc::illegal_byte_sequence);
394 return std::unique_ptr<CoverageData>(new CoverageData(std::move(Addrs)));
397 // Merge multiple coverage data together.
398 static std::unique_ptr<CoverageData>
399 merge(const std::vector<std::unique_ptr<CoverageData>> &Covs) {
400 auto Addrs = llvm::make_unique<std::set<uint64_t>>();
402 for (const auto &Cov : Covs)
403 Addrs->insert(Cov->Addrs->begin(), Cov->Addrs->end());
405 return std::unique_ptr<CoverageData>(new CoverageData(std::move(Addrs)));
408 // Read list of files and merges their coverage info.
409 static ErrorOr<std::unique_ptr<CoverageData>>
410 readAndMerge(const std::vector<std::string> &FileNames) {
411 std::vector<std::unique_ptr<CoverageData>> Covs;
412 for (const auto &FileName : FileNames) {
413 auto Cov = read(FileName);
415 return Cov.getError();
416 Covs.push_back(std::move(Cov.get()));
421 // Print coverage addresses.
422 void printAddrs(raw_ostream &OS) {
423 for (auto Addr : *Addrs) {
430 // Print list of covered functions.
431 // Line format: <file_name>:<line> <function_name>
432 void printCoveredFunctions(raw_ostream &OS) {
433 printFunctionLocs(computeFunctionLocs(*Addrs), OS);
436 // Print list of not covered functions.
437 // Line format: <file_name>:<line> <function_name>
438 void printNotCoveredFunctions(raw_ostream &OS) {
439 std::set<FunctionLoc> AllFns =
440 computeFunctionLocs(getCoveragePoints(ClBinaryName));
441 std::set<FunctionLoc> CoveredFns = computeFunctionLocs(*Addrs);
443 std::set<FunctionLoc> NotCoveredFns;
444 std::set_difference(AllFns.begin(), AllFns.end(), CoveredFns.begin(),
446 std::inserter(NotCoveredFns, NotCoveredFns.end()));
447 printFunctionLocs(NotCoveredFns, OS);
451 explicit CoverageData(std::unique_ptr<std::set<uint64_t>> Addrs)
452 : Addrs(std::move(Addrs)) {}
454 std::unique_ptr<std::set<uint64_t>> Addrs;
458 int main(int argc, char **argv) {
459 // Print stack trace if we signal out.
460 sys::PrintStackTraceOnErrorSignal();
461 PrettyStackTraceProgram X(argc, argv);
462 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
464 llvm::InitializeAllTargetInfos();
465 llvm::InitializeAllTargetMCs();
466 llvm::InitializeAllDisassemblers();
468 cl::ParseCommandLineOptions(argc, argv, "Sanitizer Coverage Processing Tool");
470 auto CovData = CoverageData::readAndMerge(ClInputFiles);
471 FailIfError(CovData);
475 CovData.get()->printAddrs(outs());
478 case CoveredFunctionsAction: {
479 CovData.get()->printCoveredFunctions(outs());
482 case NotCoveredFunctionsAction: {
483 CovData.get()->printNotCoveredFunctions(outs());
488 llvm_unreachable("unsupported action");