2016-04-06 04:45:04 +08:00
|
|
|
//===-- sancov.cc --------------------------------------------===//
|
2015-11-12 04:58:20 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a command-line tool for reading and analyzing sanitizer
|
|
|
|
// coverage.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-09-29 05:39:28 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2016-02-12 08:29:07 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/DebugInfo/Symbolize/Symbolize.h"
|
2015-12-05 02:35:37 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2016-01-27 00:44:37 +08:00
|
|
|
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
|
2015-12-05 02:35:37 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
|
|
|
#include "llvm/MC/MCInstrAnalysis.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2015-12-19 06:02:08 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
2015-12-05 02:35:37 +08:00
|
|
|
#include "llvm/Object/Binary.h"
|
2016-08-11 04:08:19 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
2016-02-19 08:26:20 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2016-08-13 03:25:59 +08:00
|
|
|
#include "llvm/Object/MachO.h"
|
2015-12-05 02:35:37 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2016-02-19 08:26:20 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/LineIterator.h"
|
2016-03-12 07:28:28 +08:00
|
|
|
#include "llvm/Support/MD5.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2016-02-12 08:29:07 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2016-09-29 05:39:28 +08:00
|
|
|
#include "llvm/Support/SHA1.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2016-09-29 05:39:28 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2015-12-16 08:31:48 +08:00
|
|
|
#include "llvm/Support/SpecialCaseList.h"
|
2015-12-05 02:35:37 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2016-09-29 05:39:28 +08:00
|
|
|
#include "llvm/Support/YAMLParser.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2016-03-25 04:41:18 +08:00
|
|
|
#include <algorithm>
|
2015-11-12 04:58:20 +08:00
|
|
|
#include <set>
|
2016-04-06 04:45:04 +08:00
|
|
|
#include <stdio.h>
|
2015-12-05 02:35:37 +08:00
|
|
|
#include <string>
|
2016-02-12 08:29:07 +08:00
|
|
|
#include <utility>
|
2015-11-12 04:58:20 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// --------- COMMAND LINE FLAGS ---------
|
|
|
|
|
2015-12-05 02:35:37 +08:00
|
|
|
enum ActionType {
|
|
|
|
CoveredFunctionsAction,
|
2016-03-24 08:00:08 +08:00
|
|
|
HtmlReportAction,
|
2016-09-29 05:39:28 +08:00
|
|
|
MergeAction,
|
|
|
|
NotCoveredFunctionsAction,
|
|
|
|
PrintAction,
|
|
|
|
PrintCovPointsAction,
|
|
|
|
StatsAction,
|
|
|
|
SymbolizeAction
|
2015-12-05 02:35:37 +08:00
|
|
|
};
|
2015-11-12 04:58:20 +08:00
|
|
|
|
|
|
|
cl::opt<ActionType> Action(
|
|
|
|
cl::desc("Action (required)"), cl::Required,
|
2016-09-29 05:39:28 +08:00
|
|
|
cl::values(
|
|
|
|
clEnumValN(PrintAction, "print", "Print coverage addresses"),
|
|
|
|
clEnumValN(PrintCovPointsAction, "print-coverage-pcs",
|
|
|
|
"Print coverage instrumentation points addresses."),
|
|
|
|
clEnumValN(CoveredFunctionsAction, "covered-functions",
|
|
|
|
"Print all covered funcions."),
|
|
|
|
clEnumValN(NotCoveredFunctionsAction, "not-covered-functions",
|
|
|
|
"Print all not covered funcions."),
|
|
|
|
clEnumValN(StatsAction, "print-coverage-stats",
|
|
|
|
"Print coverage statistics."),
|
|
|
|
clEnumValN(HtmlReportAction, "html-report",
|
2016-10-05 03:18:23 +08:00
|
|
|
"REMOVED. Use -symbolize & coverage-report-server.py."),
|
2016-09-29 05:39:28 +08:00
|
|
|
clEnumValN(SymbolizeAction, "symbolize",
|
|
|
|
"Produces a symbolized JSON report from binary report."),
|
|
|
|
clEnumValN(MergeAction, "merge", "Merges reports."), clEnumValEnd));
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static cl::list<std::string>
|
2016-02-12 08:29:07 +08:00
|
|
|
ClInputFiles(cl::Positional, cl::OneOrMore,
|
|
|
|
cl::desc("(<binary file>|<.sancov file>)..."));
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static cl::opt<bool> ClDemangle("demangle", cl::init(true),
|
|
|
|
cl::desc("Print demangled function name."));
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static cl::opt<std::string> ClStripPathPrefix(
|
2015-12-05 02:35:37 +08:00
|
|
|
"strip_path_prefix", cl::init(""),
|
|
|
|
cl::desc("Strip this prefix from file paths in reports."));
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static cl::opt<std::string>
|
2015-12-16 08:31:48 +08:00
|
|
|
ClBlacklist("blacklist", cl::init(""),
|
|
|
|
cl::desc("Blacklist file (sanitizer blacklist format)."));
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static cl::opt<bool> ClUseDefaultBlacklist(
|
2015-12-16 08:31:48 +08:00
|
|
|
"use_default_blacklist", cl::init(true), cl::Hidden,
|
|
|
|
cl::desc("Controls if default blacklist should be used."));
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static const char *const DefaultBlacklistStr = "fun:__sanitizer_.*\n"
|
|
|
|
"src:/usr/include/.*\n"
|
|
|
|
"src:.*/libc\\+\\+/.*\n";
|
2015-12-16 08:31:48 +08:00
|
|
|
|
2015-11-12 04:58:20 +08:00
|
|
|
// --------- FORMAT SPECIFICATION ---------
|
|
|
|
|
|
|
|
struct FileHeader {
|
|
|
|
uint32_t Bitness;
|
|
|
|
uint32_t Magic;
|
|
|
|
};
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static const uint32_t BinCoverageMagic = 0xC0BFFFFF;
|
|
|
|
static const uint32_t Bitness32 = 0xFFFFFF32;
|
|
|
|
static const uint32_t Bitness64 = 0xFFFFFF64;
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static Regex SancovFileRegex("(.*)\\.[0-9]+\\.sancov");
|
|
|
|
static Regex SymcovFileRegex(".*\\.symcov");
|
|
|
|
|
|
|
|
// --------- MAIN DATASTRUCTURES ----------
|
|
|
|
|
|
|
|
// Contents of .sancov file: list of coverage point addresses that were
|
|
|
|
// executed.
|
|
|
|
struct RawCoverage {
|
|
|
|
explicit RawCoverage(std::unique_ptr<std::set<uint64_t>> Addrs)
|
|
|
|
: Addrs(std::move(Addrs)) {}
|
|
|
|
|
|
|
|
// Read binary .sancov file.
|
|
|
|
static ErrorOr<std::unique_ptr<RawCoverage>>
|
|
|
|
read(const std::string &FileName);
|
|
|
|
|
|
|
|
std::unique_ptr<std::set<uint64_t>> Addrs;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Coverage point has an opaque Id and corresponds to multiple source locations.
|
|
|
|
struct CoveragePoint {
|
|
|
|
explicit CoveragePoint(const std::string &Id) : Id(Id) {}
|
|
|
|
|
|
|
|
std::string Id;
|
|
|
|
SmallVector<DILineInfo, 1> Locs;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Symcov file content: set of covered Ids plus information about all available
|
|
|
|
// coverage points.
|
|
|
|
struct SymbolizedCoverage {
|
|
|
|
// Read json .symcov file.
|
|
|
|
static std::unique_ptr<SymbolizedCoverage> read(const std::string &InputFile);
|
|
|
|
|
|
|
|
std::set<std::string> CoveredIds;
|
|
|
|
std::string BinaryHash;
|
|
|
|
std::vector<CoveragePoint> Points;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CoverageStats {
|
|
|
|
size_t AllPoints;
|
|
|
|
size_t CovPoints;
|
|
|
|
size_t AllFns;
|
|
|
|
size_t CovFns;
|
|
|
|
};
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// --------- ERROR HANDLING ---------
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static void fail(const llvm::Twine &E) {
|
2016-02-12 08:29:07 +08:00
|
|
|
errs() << "Error: " << E << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static void failIf(bool B, const llvm::Twine &E) {
|
|
|
|
if (B)
|
|
|
|
fail(E);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void failIfError(std::error_code Error) {
|
2015-12-05 02:35:37 +08:00
|
|
|
if (!Error)
|
|
|
|
return;
|
|
|
|
errs() << "Error: " << Error.message() << "(" << Error.value() << ")\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
template <typename T> static void failIfError(const ErrorOr<T> &E) {
|
|
|
|
failIfError(E.getError());
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static void failIfError(Error Err) {
|
2016-07-14 10:24:01 +08:00
|
|
|
if (Err) {
|
|
|
|
logAllUnhandledErrors(std::move(Err), errs(), "Error: ");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
template <typename T> static void failIfError(Expected<T> &E) {
|
|
|
|
failIfError(E.takeError());
|
2016-06-04 04:25:09 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static void failIfNotEmpty(const llvm::Twine &E) {
|
2016-02-12 08:29:07 +08:00
|
|
|
if (E.str().empty())
|
2015-11-12 04:58:20 +08:00
|
|
|
return;
|
2016-09-29 05:39:28 +08:00
|
|
|
fail(E);
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2015-12-05 02:35:37 +08:00
|
|
|
template <typename T>
|
2016-09-29 05:39:28 +08:00
|
|
|
static void failIfEmpty(const std::unique_ptr<T> &Ptr,
|
2016-04-06 04:45:04 +08:00
|
|
|
const std::string &Message) {
|
2015-12-05 02:35:37 +08:00
|
|
|
if (Ptr.get())
|
|
|
|
return;
|
2016-09-29 05:39:28 +08:00
|
|
|
fail(Message);
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
// ----------- Coverage I/O ----------
|
2015-11-12 04:58:20 +08:00
|
|
|
template <typename T>
|
2016-04-06 04:45:04 +08:00
|
|
|
static void readInts(const char *Start, const char *End,
|
|
|
|
std::set<uint64_t> *Ints) {
|
2015-11-12 04:58:20 +08:00
|
|
|
const T *S = reinterpret_cast<const T *>(Start);
|
|
|
|
const T *E = reinterpret_cast<const T *>(End);
|
2015-12-05 02:35:37 +08:00
|
|
|
std::copy(S, E, std::inserter(*Ints, Ints->end()));
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
ErrorOr<std::unique_ptr<RawCoverage>>
|
|
|
|
RawCoverage::read(const std::string &FileName) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
|
|
|
|
MemoryBuffer::getFile(FileName);
|
|
|
|
if (!BufOrErr)
|
|
|
|
return BufOrErr.getError();
|
|
|
|
std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
|
|
|
|
if (Buf->getBufferSize() < 8) {
|
|
|
|
errs() << "File too small (<8): " << Buf->getBufferSize() << '\n';
|
|
|
|
return make_error_code(errc::illegal_byte_sequence);
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
const FileHeader *Header =
|
|
|
|
reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
if (Header->Magic != BinCoverageMagic) {
|
|
|
|
errs() << "Wrong magic: " << Header->Magic << '\n';
|
|
|
|
return make_error_code(errc::illegal_byte_sequence);
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
auto Addrs = llvm::make_unique<std::set<uint64_t>>();
|
|
|
|
|
|
|
|
switch (Header->Bitness) {
|
|
|
|
case Bitness64:
|
|
|
|
readInts<uint64_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),
|
|
|
|
Addrs.get());
|
|
|
|
break;
|
|
|
|
case Bitness32:
|
|
|
|
readInts<uint32_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),
|
|
|
|
Addrs.get());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errs() << "Unsupported bitness: " << Header->Bitness << '\n';
|
|
|
|
return make_error_code(errc::illegal_byte_sequence);
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
return std::unique_ptr<RawCoverage>(new RawCoverage(std::move(Addrs)));
|
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
// Print coverage addresses.
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const RawCoverage &CoverageData) {
|
|
|
|
for (auto Addr : *CoverageData.Addrs) {
|
|
|
|
OS << "0x";
|
|
|
|
OS.write_hex(Addr);
|
|
|
|
OS << "\n";
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
return OS;
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static raw_ostream &operator<<(raw_ostream &OS, const CoverageStats &Stats) {
|
|
|
|
OS << "all-edges: " << Stats.AllPoints << "\n";
|
|
|
|
OS << "cov-edges: " << Stats.CovPoints << "\n";
|
|
|
|
OS << "all-functions: " << Stats.AllFns << "\n";
|
|
|
|
OS << "cov-functions: " << Stats.CovFns << "\n";
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper for writing out JSON. Handles indents and commas using
|
|
|
|
// scope variables for objects and arrays.
|
|
|
|
class JSONWriter {
|
|
|
|
public:
|
|
|
|
JSONWriter(raw_ostream &Out) : OS(Out) {}
|
|
|
|
JSONWriter(const JSONWriter &) = delete;
|
|
|
|
~JSONWriter() { OS << "\n"; }
|
|
|
|
|
|
|
|
void operator<<(StringRef S) { printJSONStringLiteral(S, OS); }
|
|
|
|
|
|
|
|
// Helper RAII class to output JSON objects.
|
|
|
|
class Object {
|
|
|
|
public:
|
|
|
|
Object(JSONWriter *W, raw_ostream &OS) : W(W), OS(OS) {
|
|
|
|
OS << "{";
|
|
|
|
W->Indent++;
|
|
|
|
}
|
|
|
|
Object(const Object &) = delete;
|
|
|
|
~Object() {
|
|
|
|
W->Indent--;
|
|
|
|
OS << "\n";
|
|
|
|
W->indent();
|
|
|
|
OS << "}";
|
|
|
|
}
|
|
|
|
|
|
|
|
void key(StringRef Key) {
|
|
|
|
Index++;
|
|
|
|
if (Index > 0)
|
|
|
|
OS << ",";
|
|
|
|
OS << "\n";
|
|
|
|
W->indent();
|
|
|
|
printJSONStringLiteral(Key, OS);
|
|
|
|
OS << " : ";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSONWriter *W;
|
|
|
|
raw_ostream &OS;
|
|
|
|
int Index = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Object> object() { return make_unique<Object>(this, OS); }
|
|
|
|
|
|
|
|
// Helper RAII class to output JSON arrays.
|
|
|
|
class Array {
|
|
|
|
public:
|
|
|
|
Array(raw_ostream &OS) : OS(OS) { OS << "["; }
|
|
|
|
Array(const Array &) = delete;
|
|
|
|
~Array() { OS << "]"; }
|
|
|
|
void next() {
|
|
|
|
Index++;
|
|
|
|
if (Index > 0)
|
|
|
|
OS << ", ";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
raw_ostream &OS;
|
|
|
|
int Index = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Array> array() { return make_unique<Array>(OS); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void indent() { OS.indent(Indent * 2); }
|
|
|
|
|
|
|
|
static void printJSONStringLiteral(StringRef S, raw_ostream &OS) {
|
|
|
|
if (S.find('"') == std::string::npos) {
|
|
|
|
OS << "\"" << S << "\"";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
OS << "\"";
|
|
|
|
for (char Ch : S.bytes()) {
|
|
|
|
if (Ch == '"')
|
|
|
|
OS << "\\";
|
|
|
|
OS << Ch;
|
|
|
|
}
|
|
|
|
OS << "\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream &OS;
|
|
|
|
int Indent = 0;
|
2015-12-05 02:35:37 +08:00
|
|
|
};
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
// Output symbolized information for coverage points in JSON.
|
|
|
|
// Format:
|
|
|
|
// {
|
|
|
|
// '<file_name>' : {
|
|
|
|
// '<function_name>' : {
|
|
|
|
// '<point_id'> : '<line_number>:'<column_number'.
|
|
|
|
// ....
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
static void operator<<(JSONWriter &W,
|
|
|
|
const std::vector<CoveragePoint> &Points) {
|
|
|
|
// Group points by file.
|
|
|
|
auto ByFile(W.object());
|
|
|
|
std::map<std::string, std::vector<const CoveragePoint *>> PointsByFile;
|
|
|
|
for (const auto &Point : Points) {
|
|
|
|
for (const DILineInfo &Loc : Point.Locs) {
|
|
|
|
PointsByFile[Loc.FileName].push_back(&Point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &P : PointsByFile) {
|
|
|
|
std::string FileName = P.first;
|
|
|
|
ByFile->key(FileName);
|
|
|
|
|
|
|
|
// Group points by function.
|
|
|
|
auto ByFn(W.object());
|
|
|
|
std::map<std::string, std::vector<const CoveragePoint *>> PointsByFn;
|
|
|
|
for (auto PointPtr : P.second) {
|
|
|
|
for (const DILineInfo &Loc : PointPtr->Locs) {
|
|
|
|
PointsByFn[Loc.FunctionName].push_back(PointPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &P : PointsByFn) {
|
|
|
|
std::string FunctionName = P.first;
|
|
|
|
ByFn->key(FunctionName);
|
|
|
|
|
|
|
|
// Output <point_id> : "<line>:<col>".
|
|
|
|
auto ById(W.object());
|
|
|
|
for (const CoveragePoint *Point : P.second) {
|
|
|
|
for (const auto &Loc : Point->Locs) {
|
|
|
|
if (Loc.FileName != FileName || Loc.FunctionName != FunctionName)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ById->key(Point->Id);
|
|
|
|
W << (utostr(Loc.Line) + ":" + utostr(Loc.Column));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void operator<<(JSONWriter &W, const SymbolizedCoverage &C) {
|
|
|
|
auto O(W.object());
|
|
|
|
|
|
|
|
{
|
|
|
|
O->key("covered-points");
|
|
|
|
auto PointsArray(W.array());
|
|
|
|
|
|
|
|
for (const auto &P : C.CoveredIds) {
|
|
|
|
PointsArray->next();
|
|
|
|
W << P;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
if (!C.BinaryHash.empty()) {
|
|
|
|
O->key("binary-hash");
|
|
|
|
W << C.BinaryHash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
O->key("point-symbol-info");
|
|
|
|
W << C.Points;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string parseScalarString(yaml::Node *N) {
|
|
|
|
SmallString<64> StringStorage;
|
|
|
|
yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
|
|
|
|
failIf(!S, "expected string");
|
|
|
|
return S->getValue(StringStorage);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SymbolizedCoverage>
|
|
|
|
SymbolizedCoverage::read(const std::string &InputFile) {
|
|
|
|
auto Coverage(make_unique<SymbolizedCoverage>());
|
|
|
|
|
|
|
|
std::map<std::string, CoveragePoint> Points;
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
|
|
|
|
MemoryBuffer::getFile(InputFile);
|
|
|
|
failIfError(BufOrErr);
|
|
|
|
|
|
|
|
SourceMgr SM;
|
|
|
|
yaml::Stream S(**BufOrErr, SM);
|
|
|
|
|
|
|
|
yaml::document_iterator DI = S.begin();
|
|
|
|
failIf(DI == S.end(), "empty document: " + InputFile);
|
|
|
|
yaml::Node *Root = DI->getRoot();
|
|
|
|
failIf(!Root, "expecting root node: " + InputFile);
|
|
|
|
yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
|
|
|
|
failIf(!Top, "expecting mapping node: " + InputFile);
|
|
|
|
|
|
|
|
for (auto &KVNode : *Top) {
|
|
|
|
auto Key = parseScalarString(KVNode.getKey());
|
|
|
|
|
|
|
|
if (Key == "covered-points") {
|
|
|
|
yaml::SequenceNode *Points =
|
|
|
|
dyn_cast<yaml::SequenceNode>(KVNode.getValue());
|
|
|
|
failIf(!Points, "expected array: " + InputFile);
|
|
|
|
|
|
|
|
for (auto I = Points->begin(), E = Points->end(); I != E; ++I) {
|
|
|
|
Coverage->CoveredIds.insert(parseScalarString(&*I));
|
|
|
|
}
|
|
|
|
} else if (Key == "binary-hash") {
|
|
|
|
Coverage->BinaryHash = parseScalarString(KVNode.getValue());
|
|
|
|
} else if (Key == "point-symbol-info") {
|
|
|
|
yaml::MappingNode *PointSymbolInfo =
|
|
|
|
dyn_cast<yaml::MappingNode>(KVNode.getValue());
|
|
|
|
failIf(!PointSymbolInfo, "expected mapping node: " + InputFile);
|
|
|
|
|
|
|
|
for (auto &FileKVNode : *PointSymbolInfo) {
|
|
|
|
auto Filename = parseScalarString(FileKVNode.getKey());
|
|
|
|
|
|
|
|
yaml::MappingNode *FileInfo =
|
|
|
|
dyn_cast<yaml::MappingNode>(FileKVNode.getValue());
|
|
|
|
failIf(!FileInfo, "expected mapping node: " + InputFile);
|
|
|
|
|
|
|
|
for (auto &FunctionKVNode : *FileInfo) {
|
|
|
|
auto FunctionName = parseScalarString(FunctionKVNode.getKey());
|
|
|
|
|
|
|
|
yaml::MappingNode *FunctionInfo =
|
|
|
|
dyn_cast<yaml::MappingNode>(FunctionKVNode.getValue());
|
|
|
|
failIf(!FunctionInfo, "expected mapping node: " + InputFile);
|
|
|
|
|
|
|
|
for (auto &PointKVNode : *FunctionInfo) {
|
|
|
|
auto PointId = parseScalarString(PointKVNode.getKey());
|
|
|
|
auto Loc = parseScalarString(PointKVNode.getValue());
|
|
|
|
|
|
|
|
size_t ColonPos = Loc.find(':');
|
|
|
|
failIf(ColonPos == std::string::npos, "expected ':': " + InputFile);
|
|
|
|
|
|
|
|
auto LineStr = Loc.substr(0, ColonPos);
|
|
|
|
auto ColStr = Loc.substr(ColonPos + 1, Loc.size());
|
|
|
|
|
|
|
|
if (Points.find(PointId) == Points.end())
|
|
|
|
Points.insert(std::make_pair(PointId, CoveragePoint(PointId)));
|
|
|
|
|
|
|
|
DILineInfo LineInfo;
|
|
|
|
LineInfo.FileName = Filename;
|
|
|
|
LineInfo.FunctionName = FunctionName;
|
|
|
|
char *End;
|
|
|
|
LineInfo.Line = std::strtoul(LineStr.c_str(), &End, 10);
|
|
|
|
LineInfo.Column = std::strtoul(ColStr.c_str(), &End, 10);
|
|
|
|
|
|
|
|
CoveragePoint *CoveragePoint = &Points.find(PointId)->second;
|
|
|
|
CoveragePoint->Locs.push_back(LineInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errs() << "Ignoring unknown key: " << Key << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &KV : Points) {
|
|
|
|
Coverage->Points.push_back(KV.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Coverage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------- MAIN FUNCTIONALITY ----------
|
|
|
|
|
2015-12-05 02:35:37 +08:00
|
|
|
std::string stripPathPrefix(std::string Path) {
|
|
|
|
if (ClStripPathPrefix.empty())
|
|
|
|
return Path;
|
|
|
|
size_t Pos = Path.find(ClStripPathPrefix);
|
|
|
|
if (Pos == std::string::npos)
|
|
|
|
return Path;
|
|
|
|
return Path.substr(Pos + ClStripPathPrefix.size());
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static std::unique_ptr<symbolize::LLVMSymbolizer> createSymbolizer() {
|
2016-01-15 06:34:11 +08:00
|
|
|
symbolize::LLVMSymbolizer::Options SymbolizerOptions;
|
|
|
|
SymbolizerOptions.Demangle = ClDemangle;
|
|
|
|
SymbolizerOptions.UseSymbolTable = true;
|
|
|
|
return std::unique_ptr<symbolize::LLVMSymbolizer>(
|
|
|
|
new symbolize::LLVMSymbolizer(SymbolizerOptions));
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static std::string normalizeFilename(const std::string &FileName) {
|
|
|
|
SmallString<256> S(FileName);
|
|
|
|
sys::path::remove_dots(S, /* remove_dot_dot */ true);
|
|
|
|
return stripPathPrefix(S.str().str());
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
class Blacklists {
|
|
|
|
public:
|
|
|
|
Blacklists()
|
|
|
|
: DefaultBlacklist(createDefaultBlacklist()),
|
|
|
|
UserBlacklist(createUserBlacklist()) {}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
bool isBlacklisted(const DILineInfo &I) {
|
|
|
|
if (DefaultBlacklist && DefaultBlacklist->inSection("fun", I.FunctionName))
|
2016-02-19 08:26:20 +08:00
|
|
|
return true;
|
2016-09-29 05:39:28 +08:00
|
|
|
if (DefaultBlacklist && DefaultBlacklist->inSection("src", I.FileName))
|
2016-02-19 08:26:20 +08:00
|
|
|
return true;
|
2016-09-29 05:39:28 +08:00
|
|
|
if (UserBlacklist && UserBlacklist->inSection("fun", I.FunctionName))
|
2016-02-19 08:26:20 +08:00
|
|
|
return true;
|
2016-09-29 05:39:28 +08:00
|
|
|
if (UserBlacklist && UserBlacklist->inSection("src", I.FileName))
|
2016-02-19 08:26:20 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
private:
|
|
|
|
static std::unique_ptr<SpecialCaseList> createDefaultBlacklist() {
|
|
|
|
if (!ClUseDefaultBlacklist)
|
|
|
|
return std::unique_ptr<SpecialCaseList>();
|
|
|
|
std::unique_ptr<MemoryBuffer> MB =
|
|
|
|
MemoryBuffer::getMemBuffer(DefaultBlacklistStr);
|
|
|
|
std::string Error;
|
|
|
|
auto Blacklist = SpecialCaseList::create(MB.get(), Error);
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfNotEmpty(Error);
|
2016-02-19 08:26:20 +08:00
|
|
|
return Blacklist;
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
static std::unique_ptr<SpecialCaseList> createUserBlacklist() {
|
|
|
|
if (ClBlacklist.empty())
|
|
|
|
return std::unique_ptr<SpecialCaseList>();
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
return SpecialCaseList::createOrDie({{ClBlacklist}});
|
|
|
|
}
|
|
|
|
std::unique_ptr<SpecialCaseList> DefaultBlacklist;
|
|
|
|
std::unique_ptr<SpecialCaseList> UserBlacklist;
|
|
|
|
};
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static std::vector<CoveragePoint>
|
|
|
|
getCoveragePoints(const std::string &ObjectFile,
|
|
|
|
const std::set<uint64_t> &Addrs, bool InlinedCode) {
|
|
|
|
std::vector<CoveragePoint> Result;
|
2016-02-19 08:26:20 +08:00
|
|
|
auto Symbolizer(createSymbolizer());
|
|
|
|
Blacklists B;
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
for (auto Addr : Addrs) {
|
2016-09-29 05:39:28 +08:00
|
|
|
std::set<DILineInfo> Infos; // deduplicate debug info.
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
auto LineInfo = Symbolizer->symbolizeCode(ObjectFile, Addr);
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(LineInfo);
|
|
|
|
LineInfo->FileName = normalizeFilename(LineInfo->FileName);
|
|
|
|
if (B.isBlacklisted(*LineInfo))
|
2015-12-05 02:35:37 +08:00
|
|
|
continue;
|
2016-09-29 05:39:28 +08:00
|
|
|
|
|
|
|
auto Id = utohexstr(Addr, true);
|
|
|
|
auto Point = CoveragePoint(Id);
|
|
|
|
Infos.insert(*LineInfo);
|
|
|
|
Point.Locs.push_back(*LineInfo);
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
if (InlinedCode) {
|
|
|
|
auto InliningInfo = Symbolizer->symbolizeInlinedCode(ObjectFile, Addr);
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(InliningInfo);
|
2016-02-19 08:26:20 +08:00
|
|
|
for (uint32_t I = 0; I < InliningInfo->getNumberOfFrames(); ++I) {
|
|
|
|
auto FrameInfo = InliningInfo->getFrame(I);
|
2016-09-29 05:39:28 +08:00
|
|
|
FrameInfo.FileName = normalizeFilename(FrameInfo.FileName);
|
|
|
|
if (B.isBlacklisted(FrameInfo))
|
2016-02-19 08:26:20 +08:00
|
|
|
continue;
|
2016-09-29 05:39:28 +08:00
|
|
|
if (Infos.find(FrameInfo) == Infos.end()) {
|
|
|
|
Infos.insert(FrameInfo);
|
|
|
|
Point.Locs.push_back(FrameInfo);
|
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
|
|
|
|
Result.push_back(Point);
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 08:31:48 +08:00
|
|
|
return Result;
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
|
2016-08-11 04:08:19 +08:00
|
|
|
static bool isCoveragePointSymbol(StringRef Name) {
|
|
|
|
return Name == "__sanitizer_cov" || Name == "__sanitizer_cov_with_check" ||
|
2016-08-13 03:25:59 +08:00
|
|
|
Name == "__sanitizer_cov_trace_func_enter" ||
|
|
|
|
// Mac has '___' prefix
|
|
|
|
Name == "___sanitizer_cov" || Name == "___sanitizer_cov_with_check" ||
|
|
|
|
Name == "___sanitizer_cov_trace_func_enter";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Locate __sanitizer_cov* function addresses inside the stubs table on MachO.
|
|
|
|
static void findMachOIndirectCovFunctions(const object::MachOObjectFile &O,
|
|
|
|
std::set<uint64_t> *Result) {
|
|
|
|
MachO::dysymtab_command Dysymtab = O.getDysymtabLoadCommand();
|
|
|
|
MachO::symtab_command Symtab = O.getSymtabLoadCommand();
|
|
|
|
|
|
|
|
for (const auto &Load : O.load_commands()) {
|
|
|
|
if (Load.C.cmd == MachO::LC_SEGMENT_64) {
|
|
|
|
MachO::segment_command_64 Seg = O.getSegment64LoadCommand(Load);
|
|
|
|
for (unsigned J = 0; J < Seg.nsects; ++J) {
|
|
|
|
MachO::section_64 Sec = O.getSection64(Load, J);
|
|
|
|
|
|
|
|
uint32_t SectionType = Sec.flags & MachO::SECTION_TYPE;
|
|
|
|
if (SectionType == MachO::S_SYMBOL_STUBS) {
|
|
|
|
uint32_t Stride = Sec.reserved2;
|
|
|
|
uint32_t Cnt = Sec.size / Stride;
|
|
|
|
uint32_t N = Sec.reserved1;
|
|
|
|
for (uint32_t J = 0; J < Cnt && N + J < Dysymtab.nindirectsyms; J++) {
|
|
|
|
uint32_t IndirectSymbol =
|
|
|
|
O.getIndirectSymbolTableEntry(Dysymtab, N + J);
|
|
|
|
uint64_t Addr = Sec.addr + J * Stride;
|
|
|
|
if (IndirectSymbol < Symtab.nsyms) {
|
|
|
|
object::SymbolRef Symbol = *(O.getSymbolByIndex(IndirectSymbol));
|
|
|
|
Expected<StringRef> Name = Symbol.getName();
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(Name);
|
2016-08-13 03:25:59 +08:00
|
|
|
if (isCoveragePointSymbol(Name.get())) {
|
|
|
|
Result->insert(Addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Load.C.cmd == MachO::LC_SEGMENT) {
|
|
|
|
errs() << "ERROR: 32 bit MachO binaries not supported\n";
|
|
|
|
}
|
|
|
|
}
|
2016-08-11 04:08:19 +08:00
|
|
|
}
|
|
|
|
|
2015-12-15 07:55:04 +08:00
|
|
|
// Locate __sanitizer_cov* function addresses that are used for coverage
|
|
|
|
// reporting.
|
2016-04-06 04:45:04 +08:00
|
|
|
static std::set<uint64_t>
|
2015-12-15 07:55:04 +08:00
|
|
|
findSanitizerCovFunctions(const object::ObjectFile &O) {
|
|
|
|
std::set<uint64_t> Result;
|
|
|
|
|
2015-12-05 02:35:37 +08:00
|
|
|
for (const object::SymbolRef &Symbol : O.symbols()) {
|
2016-06-25 02:24:42 +08:00
|
|
|
Expected<uint64_t> AddressOrErr = Symbol.getAddress();
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(AddressOrErr);
|
2016-08-13 03:25:59 +08:00
|
|
|
uint64_t Address = AddressOrErr.get();
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> NameOrErr = Symbol.getName();
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(NameOrErr);
|
2015-12-15 07:55:04 +08:00
|
|
|
StringRef Name = NameOrErr.get();
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-08-13 03:25:59 +08:00
|
|
|
if (!(Symbol.getFlags() & object::BasicSymbolRef::SF_Undefined) &&
|
|
|
|
isCoveragePointSymbol(Name)) {
|
|
|
|
Result.insert(Address);
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 07:55:04 +08:00
|
|
|
|
2016-08-11 04:08:19 +08:00
|
|
|
if (const auto *CO = dyn_cast<object::COFFObjectFile>(&O)) {
|
|
|
|
for (const object::ExportDirectoryEntryRef &Export :
|
|
|
|
CO->export_directories()) {
|
|
|
|
uint32_t RVA;
|
|
|
|
std::error_code EC = Export.getExportRVA(RVA);
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(EC);
|
2016-08-11 04:08:19 +08:00
|
|
|
|
|
|
|
StringRef Name;
|
|
|
|
EC = Export.getSymbolName(Name);
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(EC);
|
2016-08-11 04:08:19 +08:00
|
|
|
|
|
|
|
if (isCoveragePointSymbol(Name))
|
|
|
|
Result.insert(CO->getImageBase() + RVA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 03:25:59 +08:00
|
|
|
if (const auto *MO = dyn_cast<object::MachOObjectFile>(&O)) {
|
|
|
|
findMachOIndirectCovFunctions(*MO, &Result);
|
|
|
|
}
|
|
|
|
|
2015-12-15 07:55:04 +08:00
|
|
|
return Result;
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Locate addresses of all coverage points in a file. Coverage point
|
|
|
|
// is defined as the 'address of instruction following __sanitizer_cov
|
|
|
|
// call - 1'.
|
2016-04-06 04:45:04 +08:00
|
|
|
static void getObjectCoveragePoints(const object::ObjectFile &O,
|
|
|
|
std::set<uint64_t> *Addrs) {
|
2015-12-05 02:35:37 +08:00
|
|
|
Triple TheTriple("unknown-unknown-unknown");
|
|
|
|
TheTriple.setArch(Triple::ArchType(O.getArch()));
|
|
|
|
auto TripleName = TheTriple.getTriple();
|
|
|
|
|
|
|
|
std::string Error;
|
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfNotEmpty(Error);
|
2015-12-05 02:35:37 +08:00
|
|
|
|
|
|
|
std::unique_ptr<const MCSubtargetInfo> STI(
|
|
|
|
TheTarget->createMCSubtargetInfo(TripleName, "", ""));
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfEmpty(STI, "no subtarget info for target " + TripleName);
|
2015-12-05 02:35:37 +08:00
|
|
|
|
|
|
|
std::unique_ptr<const MCRegisterInfo> MRI(
|
|
|
|
TheTarget->createMCRegInfo(TripleName));
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfEmpty(MRI, "no register info for target " + TripleName);
|
2015-12-05 02:35:37 +08:00
|
|
|
|
|
|
|
std::unique_ptr<const MCAsmInfo> AsmInfo(
|
|
|
|
TheTarget->createMCAsmInfo(*MRI, TripleName));
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfEmpty(AsmInfo, "no asm info for target " + TripleName);
|
2015-12-05 02:35:37 +08:00
|
|
|
|
|
|
|
std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
|
|
|
|
MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
|
|
|
|
std::unique_ptr<MCDisassembler> DisAsm(
|
|
|
|
TheTarget->createMCDisassembler(*STI, Ctx));
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfEmpty(DisAsm, "no disassembler info for target " + TripleName);
|
2015-12-05 02:35:37 +08:00
|
|
|
|
|
|
|
std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfEmpty(MII, "no instruction info for target " + TripleName);
|
2015-12-05 02:35:37 +08:00
|
|
|
|
|
|
|
std::unique_ptr<const MCInstrAnalysis> MIA(
|
|
|
|
TheTarget->createMCInstrAnalysis(MII.get()));
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfEmpty(MIA, "no instruction analysis info for target " + TripleName);
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2015-12-15 07:55:04 +08:00
|
|
|
auto SanCovAddrs = findSanitizerCovFunctions(O);
|
2016-02-19 08:26:20 +08:00
|
|
|
if (SanCovAddrs.empty())
|
2016-09-29 05:39:28 +08:00
|
|
|
fail("__sanitizer_cov* functions not found");
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-22 21:11:58 +08:00
|
|
|
for (object::SectionRef Section : O.sections()) {
|
2015-12-05 02:35:37 +08:00
|
|
|
if (Section.isVirtual() || !Section.isText()) // llvm-objdump does the same.
|
|
|
|
continue;
|
|
|
|
uint64_t SectionAddr = Section.getAddress();
|
|
|
|
uint64_t SectSize = Section.getSize();
|
|
|
|
if (!SectSize)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
StringRef BytesStr;
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(Section.getContents(BytesStr));
|
2015-12-05 02:35:37 +08:00
|
|
|
ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
|
|
|
|
BytesStr.size());
|
|
|
|
|
|
|
|
for (uint64_t Index = 0, Size = 0; Index < Section.getSize();
|
|
|
|
Index += Size) {
|
|
|
|
MCInst Inst;
|
|
|
|
if (!DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
|
|
|
|
SectionAddr + Index, nulls(), nulls())) {
|
|
|
|
if (Size == 0)
|
|
|
|
Size = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
uint64_t Addr = Index + SectionAddr;
|
|
|
|
// Sanitizer coverage uses the address of the next instruction - 1.
|
|
|
|
uint64_t CovPoint = Addr + Size - 1;
|
2015-12-05 02:35:37 +08:00
|
|
|
uint64_t Target;
|
|
|
|
if (MIA->isCall(Inst) &&
|
2016-02-19 08:26:20 +08:00
|
|
|
MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target) &&
|
|
|
|
SanCovAddrs.find(Target) != SanCovAddrs.end())
|
|
|
|
Addrs->insert(CovPoint);
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static void
|
2016-02-19 08:26:20 +08:00
|
|
|
visitObjectFiles(const object::Archive &A,
|
2016-06-18 04:41:14 +08:00
|
|
|
function_ref<void(const object::ObjectFile &)> Fn) {
|
2016-07-14 10:24:01 +08:00
|
|
|
Error Err;
|
|
|
|
for (auto &C : A.children(Err)) {
|
2016-05-18 01:10:12 +08:00
|
|
|
Expected<std::unique_ptr<object::Binary>> ChildOrErr = C.getAsBinary();
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(ChildOrErr);
|
2016-02-19 08:26:20 +08:00
|
|
|
if (auto *O = dyn_cast<object::ObjectFile>(&*ChildOrErr.get()))
|
|
|
|
Fn(*O);
|
2015-12-05 02:35:37 +08:00
|
|
|
else
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(object::object_error::invalid_file_type);
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(std::move(Err));
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static void
|
2016-06-18 04:41:14 +08:00
|
|
|
visitObjectFiles(const std::string &FileName,
|
|
|
|
function_ref<void(const object::ObjectFile &)> Fn) {
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<object::OwningBinary<object::Binary>> BinaryOrErr =
|
2015-12-05 02:35:37 +08:00
|
|
|
object::createBinary(FileName);
|
2016-04-07 06:14:09 +08:00
|
|
|
if (!BinaryOrErr)
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(BinaryOrErr);
|
2015-12-05 02:35:37 +08:00
|
|
|
|
|
|
|
object::Binary &Binary = *BinaryOrErr.get().getBinary();
|
|
|
|
if (object::Archive *A = dyn_cast<object::Archive>(&Binary))
|
2016-02-19 08:26:20 +08:00
|
|
|
visitObjectFiles(*A, Fn);
|
2015-12-05 02:35:37 +08:00
|
|
|
else if (object::ObjectFile *O = dyn_cast<object::ObjectFile>(&Binary))
|
2016-02-19 08:26:20 +08:00
|
|
|
Fn(*O);
|
2015-11-12 04:58:20 +08:00
|
|
|
else
|
2016-09-29 05:39:28 +08:00
|
|
|
failIfError(object::object_error::invalid_file_type);
|
2015-12-16 08:31:48 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static std::set<uint64_t>
|
|
|
|
findSanitizerCovFunctions(const std::string &FileName) {
|
2016-02-19 08:26:20 +08:00
|
|
|
std::set<uint64_t> Result;
|
|
|
|
visitObjectFiles(FileName, [&](const object::ObjectFile &O) {
|
|
|
|
auto Addrs = findSanitizerCovFunctions(O);
|
|
|
|
Result.insert(Addrs.begin(), Addrs.end());
|
|
|
|
});
|
|
|
|
return Result;
|
2015-12-16 08:31:48 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// Locate addresses of all coverage points in a file. Coverage point
|
|
|
|
// is defined as the 'address of instruction following __sanitizer_cov
|
|
|
|
// call - 1'.
|
2016-09-29 05:39:28 +08:00
|
|
|
static std::set<uint64_t> findCoveragePointAddrs(const std::string &FileName) {
|
2016-02-19 08:26:20 +08:00
|
|
|
std::set<uint64_t> Result;
|
|
|
|
visitObjectFiles(FileName, [&](const object::ObjectFile &O) {
|
|
|
|
getObjectCoveragePoints(O, &Result);
|
|
|
|
});
|
|
|
|
return Result;
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
|
2016-06-18 04:41:14 +08:00
|
|
|
static void printCovPoints(const std::string &ObjFile, raw_ostream &OS) {
|
2016-09-29 05:39:28 +08:00
|
|
|
for (uint64_t Addr : findCoveragePointAddrs(ObjFile)) {
|
2016-02-27 10:21:44 +08:00
|
|
|
OS << "0x";
|
|
|
|
OS.write_hex(Addr);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 04:41:14 +08:00
|
|
|
static ErrorOr<bool> isCoverageFile(const std::string &FileName) {
|
2016-09-29 05:39:28 +08:00
|
|
|
auto ShortFileName = llvm::sys::path::filename(FileName);
|
|
|
|
if (!SancovFileRegex.match(ShortFileName))
|
|
|
|
return false;
|
|
|
|
|
2016-02-12 08:29:07 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
|
|
|
|
MemoryBuffer::getFile(FileName);
|
2016-03-12 07:26:41 +08:00
|
|
|
if (!BufOrErr) {
|
|
|
|
errs() << "Warning: " << BufOrErr.getError().message() << "("
|
|
|
|
<< BufOrErr.getError().value()
|
|
|
|
<< "), filename: " << llvm::sys::path::filename(FileName) << "\n";
|
2016-02-12 08:29:07 +08:00
|
|
|
return BufOrErr.getError();
|
2016-03-12 07:26:41 +08:00
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
|
|
|
|
if (Buf->getBufferSize() < 8) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const FileHeader *Header =
|
|
|
|
reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
|
|
|
|
return Header->Magic == BinCoverageMagic;
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static bool isSymbolizedCoverageFile(const std::string &FileName) {
|
|
|
|
auto ShortFileName = llvm::sys::path::filename(FileName);
|
|
|
|
return SymcovFileRegex.match(ShortFileName);
|
2016-03-24 08:00:08 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static std::unique_ptr<SymbolizedCoverage>
|
|
|
|
symbolize(const RawCoverage &Data, const std::string ObjectFile) {
|
|
|
|
auto Coverage = make_unique<SymbolizedCoverage>();
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
|
|
|
|
MemoryBuffer::getFile(ObjectFile);
|
|
|
|
failIfError(BufOrErr);
|
|
|
|
SHA1 Hasher;
|
|
|
|
Hasher.update((*BufOrErr)->getBuffer());
|
|
|
|
Coverage->BinaryHash = toHex(Hasher.final());
|
|
|
|
|
|
|
|
for (uint64_t Addr : *Data.Addrs) {
|
|
|
|
Coverage->CoveredIds.insert(utohexstr(Addr, true));
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
std::set<uint64_t> AllAddrs = findCoveragePointAddrs(ObjectFile);
|
|
|
|
if (!std::includes(AllAddrs.begin(), AllAddrs.end(), Data.Addrs->begin(),
|
|
|
|
Data.Addrs->end())) {
|
|
|
|
fail("Coverage points in binary and .sancov file do not match.");
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
Coverage->Points = getCoveragePoints(ObjectFile, AllAddrs, true);
|
|
|
|
return Coverage;
|
|
|
|
}
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
struct FileFn {
|
|
|
|
bool operator<(const FileFn &RHS) const {
|
|
|
|
return std::tie(FileName, FunctionName) <
|
|
|
|
std::tie(RHS.FileName, RHS.FunctionName);
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
std::string FileName;
|
|
|
|
std::string FunctionName;
|
2016-02-19 08:26:20 +08:00
|
|
|
};
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static std::set<FileFn>
|
|
|
|
computeFunctions(const std::vector<CoveragePoint> &Points) {
|
|
|
|
std::set<FileFn> Fns;
|
|
|
|
for (const auto &Point : Points) {
|
|
|
|
for (const auto &Loc : Point.Locs) {
|
|
|
|
Fns.insert(FileFn{Loc.FileName, Loc.FunctionName});
|
2016-01-15 06:34:11 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
return Fns;
|
|
|
|
}
|
2016-01-15 06:34:11 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static std::set<FileFn>
|
|
|
|
computeNotCoveredFunctions(const SymbolizedCoverage &Coverage) {
|
|
|
|
auto Fns = computeFunctions(Coverage.Points);
|
2016-03-18 08:12:14 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
for (const auto &Point : Coverage.Points) {
|
|
|
|
if (Coverage.CoveredIds.find(Point.Id) == Coverage.CoveredIds.end())
|
|
|
|
continue;
|
2016-02-19 08:26:20 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
for (const auto &Loc : Point.Locs) {
|
|
|
|
Fns.erase(FileFn{Loc.FileName, Loc.FunctionName});
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
2015-11-13 03:34:21 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
return Fns;
|
|
|
|
}
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static std::set<FileFn>
|
|
|
|
computeCoveredFunctions(const SymbolizedCoverage &Coverage) {
|
|
|
|
auto AllFns = computeFunctions(Coverage.Points);
|
|
|
|
std::set<FileFn> Result;
|
2016-03-18 08:12:14 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
for (const auto &Point : Coverage.Points) {
|
|
|
|
if (Coverage.CoveredIds.find(Point.Id) == Coverage.CoveredIds.end())
|
|
|
|
continue;
|
2016-03-18 08:12:14 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
for (const auto &Loc : Point.Locs) {
|
|
|
|
Result.insert(FileFn{Loc.FileName, Loc.FunctionName});
|
2016-03-18 08:12:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
return Result;
|
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
typedef std::map<FileFn, std::pair<uint32_t, uint32_t>> FunctionLocs;
|
|
|
|
// finds first location in a file for each function.
|
|
|
|
static FunctionLocs resolveFunctions(const SymbolizedCoverage &Coverage,
|
|
|
|
const std::set<FileFn> &Fns) {
|
|
|
|
FunctionLocs Result;
|
|
|
|
for (const auto &Point : Coverage.Points) {
|
|
|
|
for (const auto &Loc : Point.Locs) {
|
|
|
|
FileFn Fn = FileFn{Loc.FileName, Loc.FunctionName};
|
|
|
|
if (Fns.find(Fn) == Fns.end())
|
2016-02-19 08:26:20 +08:00
|
|
|
continue;
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
auto P = std::make_pair(Loc.Line, Loc.Column);
|
|
|
|
auto I = Result.find(Fn);
|
|
|
|
if (I == Result.end() || I->second > P) {
|
|
|
|
Result[Fn] = P;
|
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
return Result;
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static void printFunctionLocs(const FunctionLocs &FnLocs, raw_ostream &OS) {
|
|
|
|
for (const auto &P : FnLocs) {
|
|
|
|
OS << stripPathPrefix(P.first.FileName) << ":" << P.second.first << " "
|
|
|
|
<< P.first.FunctionName << "\n";
|
2016-02-12 08:29:07 +08:00
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
}
|
|
|
|
CoverageStats computeStats(const SymbolizedCoverage &Coverage) {
|
|
|
|
CoverageStats Stats = {Coverage.Points.size(), Coverage.CoveredIds.size(),
|
|
|
|
computeFunctions(Coverage.Points).size(),
|
|
|
|
computeCoveredFunctions(Coverage).size()};
|
|
|
|
return Stats;
|
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
// Print list of covered functions.
|
|
|
|
// Line format: <file_name>:<line> <function_name>
|
|
|
|
static void printCoveredFunctions(const SymbolizedCoverage &CovData,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
auto CoveredFns = computeCoveredFunctions(CovData);
|
|
|
|
printFunctionLocs(resolveFunctions(CovData, CoveredFns), OS);
|
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
// Print list of not covered functions.
|
|
|
|
// Line format: <file_name>:<line> <function_name>
|
|
|
|
static void printNotCoveredFunctions(const SymbolizedCoverage &CovData,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
auto NotCoveredFns = computeNotCoveredFunctions(CovData);
|
|
|
|
printFunctionLocs(resolveFunctions(CovData, NotCoveredFns), OS);
|
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
// Read list of files and merges their coverage info.
|
|
|
|
static void readAndPrintRawCoverage(const std::vector<std::string> &FileNames,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
std::vector<std::unique_ptr<RawCoverage>> Covs;
|
|
|
|
for (const auto &FileName : FileNames) {
|
|
|
|
auto Cov = RawCoverage::read(FileName);
|
|
|
|
if (!Cov)
|
|
|
|
continue;
|
|
|
|
OS << *Cov.get();
|
2016-02-12 08:29:07 +08:00
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static std::unique_ptr<SymbolizedCoverage>
|
|
|
|
merge(const std::vector<std::unique_ptr<SymbolizedCoverage>> &Coverages) {
|
|
|
|
auto Result = make_unique<SymbolizedCoverage>();
|
|
|
|
|
|
|
|
for (size_t I = 0; I < Coverages.size(); ++I) {
|
|
|
|
const SymbolizedCoverage &Coverage = *Coverages[I];
|
|
|
|
std::string Prefix;
|
|
|
|
if (Coverages.size() > 1) {
|
|
|
|
// prefix is not needed when there's only one file.
|
|
|
|
Prefix =
|
|
|
|
(Coverage.BinaryHash.size() ? Coverage.BinaryHash : utostr(I)) + ":";
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
for (const auto &Id : Coverage.CoveredIds) {
|
|
|
|
Result->CoveredIds.insert(Prefix + Id);
|
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
for (const auto &CovPoint : Coverage.Points) {
|
|
|
|
CoveragePoint NewPoint(CovPoint);
|
|
|
|
NewPoint.Id = Prefix + CovPoint.Id;
|
|
|
|
Result->Points.push_back(NewPoint);
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
if (Coverages.size() == 1) {
|
|
|
|
Result->BinaryHash = Coverages[0]->BinaryHash;
|
2016-03-24 08:00:08 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
return Result;
|
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
static std::unique_ptr<SymbolizedCoverage>
|
|
|
|
readSymbolizeAndMergeCmdArguments(std::vector<std::string> FileNames) {
|
|
|
|
std::vector<std::unique_ptr<SymbolizedCoverage>> Coverages;
|
|
|
|
|
|
|
|
{
|
2016-02-12 08:29:07 +08:00
|
|
|
// Short name => file name.
|
|
|
|
std::map<std::string, std::string> ObjFiles;
|
|
|
|
std::string FirstObjFile;
|
|
|
|
std::set<std::string> CovFiles;
|
|
|
|
|
|
|
|
// Partition input values into coverage/object files.
|
|
|
|
for (const auto &FileName : FileNames) {
|
2016-09-29 05:39:28 +08:00
|
|
|
if (isSymbolizedCoverageFile(FileName)) {
|
|
|
|
Coverages.push_back(SymbolizedCoverage::read(FileName));
|
|
|
|
}
|
|
|
|
|
2016-02-12 08:29:07 +08:00
|
|
|
auto ErrorOrIsCoverage = isCoverageFile(FileName);
|
2016-03-12 07:26:41 +08:00
|
|
|
if (!ErrorOrIsCoverage)
|
|
|
|
continue;
|
2016-02-12 08:29:07 +08:00
|
|
|
if (ErrorOrIsCoverage.get()) {
|
|
|
|
CovFiles.insert(FileName);
|
|
|
|
} else {
|
|
|
|
auto ShortFileName = llvm::sys::path::filename(FileName);
|
|
|
|
if (ObjFiles.find(ShortFileName) != ObjFiles.end()) {
|
2016-09-29 05:39:28 +08:00
|
|
|
fail("Duplicate binary file with a short name: " + ShortFileName);
|
2016-02-12 08:29:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjFiles[ShortFileName] = FileName;
|
|
|
|
if (FirstObjFile.empty())
|
|
|
|
FirstObjFile = FileName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<StringRef, 2> Components;
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// Object file => list of corresponding coverage file names.
|
2016-09-29 05:39:28 +08:00
|
|
|
std::map<std::string, std::vector<std::string>> CoverageByObjFile;
|
|
|
|
for (const auto &FileName : CovFiles) {
|
2016-02-12 08:29:07 +08:00
|
|
|
auto ShortFileName = llvm::sys::path::filename(FileName);
|
2016-09-29 05:39:28 +08:00
|
|
|
auto Ok = SancovFileRegex.match(ShortFileName, &Components);
|
2016-02-12 08:29:07 +08:00
|
|
|
if (!Ok) {
|
2016-09-29 05:39:28 +08:00
|
|
|
fail("Can't match coverage file name against "
|
2016-02-12 08:29:07 +08:00
|
|
|
"<module_name>.<pid>.sancov pattern: " +
|
|
|
|
FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Iter = ObjFiles.find(Components[1]);
|
|
|
|
if (Iter == ObjFiles.end()) {
|
2016-09-29 05:39:28 +08:00
|
|
|
fail("Object file for coverage not found: " + FileName);
|
2016-02-12 08:29:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
CoverageByObjFile[Iter->second].push_back(FileName);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Read raw coverage and symbolize it.
|
2016-02-12 08:29:07 +08:00
|
|
|
for (const auto &Pair : CoverageByObjFile) {
|
2016-02-19 08:26:20 +08:00
|
|
|
if (findSanitizerCovFunctions(Pair.first).empty()) {
|
2016-05-27 20:30:51 +08:00
|
|
|
for (const auto &FileName : Pair.second) {
|
2016-02-19 08:26:20 +08:00
|
|
|
CovFiles.erase(FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
errs()
|
|
|
|
<< "Ignoring " << Pair.first
|
|
|
|
<< " and its coverage because __sanitizer_cov* functions were not "
|
|
|
|
"found.\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
for (const std::string &CoverageFile : Pair.second) {
|
|
|
|
auto DataOrError = RawCoverage::read(CoverageFile);
|
|
|
|
failIfError(DataOrError);
|
|
|
|
Coverages.push_back(symbolize(*DataOrError.get(), Pair.first));
|
2016-02-12 08:29:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
return merge(Coverages);
|
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
} // namespace
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
int main(int Argc, char **Argv) {
|
2015-11-12 04:58:20 +08:00
|
|
|
// Print stack trace if we signal out.
|
2016-09-29 05:39:28 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(Argv[0]);
|
|
|
|
PrettyStackTraceProgram X(Argc, Argv);
|
2015-11-12 04:58:20 +08:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
2015-12-05 02:35:37 +08:00
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllDisassemblers();
|
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
cl::ParseCommandLineOptions(Argc, Argv, "Sanitizer Coverage Processing Tool");
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-02-12 08:29:07 +08:00
|
|
|
// -print doesn't need object files.
|
|
|
|
if (Action == PrintAction) {
|
2016-09-29 05:39:28 +08:00
|
|
|
readAndPrintRawCoverage(ClInputFiles, outs());
|
2015-11-12 04:58:20 +08:00
|
|
|
return 0;
|
2016-02-27 10:21:44 +08:00
|
|
|
} else if (Action == PrintCovPointsAction) {
|
|
|
|
// -print-coverage-points doesn't need coverage files.
|
2016-05-27 20:30:51 +08:00
|
|
|
for (const std::string &ObjFile : ClInputFiles) {
|
2016-02-27 10:21:44 +08:00
|
|
|
printCovPoints(ObjFile, outs());
|
|
|
|
}
|
|
|
|
return 0;
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
|
2016-09-29 05:39:28 +08:00
|
|
|
auto Coverage = readSymbolizeAndMergeCmdArguments(ClInputFiles);
|
|
|
|
failIf(!Coverage, "No valid coverage files given.");
|
2016-02-12 08:29:07 +08:00
|
|
|
|
|
|
|
switch (Action) {
|
2015-11-12 04:58:20 +08:00
|
|
|
case CoveredFunctionsAction: {
|
2016-09-29 05:39:28 +08:00
|
|
|
printCoveredFunctions(*Coverage, outs());
|
2015-11-12 04:58:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
case NotCoveredFunctionsAction: {
|
2016-09-29 05:39:28 +08:00
|
|
|
printNotCoveredFunctions(*Coverage, outs());
|
2015-12-05 02:35:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
case StatsAction: {
|
|
|
|
outs() << computeStats(*Coverage);
|
2016-01-15 06:34:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
case MergeAction:
|
|
|
|
case SymbolizeAction: { // merge & symbolize are synonims.
|
|
|
|
JSONWriter W(outs());
|
|
|
|
W << *Coverage;
|
2016-03-24 08:00:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-09-29 05:39:28 +08:00
|
|
|
case HtmlReportAction:
|
|
|
|
errs() << "-html-report option is removed: "
|
2016-10-05 03:18:23 +08:00
|
|
|
"use -symbolize & coverage-report-server.py instead\n";
|
2016-09-29 05:39:28 +08:00
|
|
|
return 1;
|
2016-02-12 08:29:07 +08:00
|
|
|
case PrintAction:
|
2016-02-27 10:21:44 +08:00
|
|
|
case PrintCovPointsAction:
|
2016-02-12 08:29:07 +08:00
|
|
|
llvm_unreachable("unsupported action");
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
}
|