2016-01-16 08:31:11 +08:00
|
|
|
//===- sanstats.cpp - Sanitizer statistics dumper -------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-01-16 08:31:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This tool dumps statistics information from files in the format produced
|
|
|
|
// by clang's -fsanitize-stats feature.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/DebugInfo/Symbolize/Symbolize.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2018-11-27 05:48:55 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-01-16 08:31:11 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2018-11-27 05:48:55 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-01-16 08:31:11 +08:00
|
|
|
#include "llvm/Transforms/Utils/SanitizerStats.h"
|
2016-04-06 04:45:04 +08:00
|
|
|
#include <stdint.h>
|
2016-01-16 08:31:11 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static cl::opt<std::string> ClInputFile(cl::Positional, cl::Required,
|
|
|
|
cl::desc("<filename>"));
|
2016-01-16 08:31:11 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static cl::opt<bool> ClDemangle("demangle", cl::init(false),
|
|
|
|
cl::desc("Print demangled function name."));
|
2016-01-16 08:31:11 +08:00
|
|
|
|
|
|
|
inline uint64_t KindFromData(uint64_t Data, char SizeofPtr) {
|
|
|
|
return Data >> (SizeofPtr * 8 - kSanitizerStatKindBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint64_t CountFromData(uint64_t Data, char SizeofPtr) {
|
|
|
|
return Data & ((1ull << (SizeofPtr * 8 - kSanitizerStatKindBits)) - 1);
|
|
|
|
}
|
|
|
|
|
2020-09-27 06:57:09 +08:00
|
|
|
static uint64_t ReadLE(char Size, const char *Begin, const char *End) {
|
2016-01-16 08:31:11 +08:00
|
|
|
uint64_t Result = 0;
|
|
|
|
char Pos = 0;
|
|
|
|
while (Begin < End && Pos != Size) {
|
|
|
|
Result |= uint64_t(uint8_t(*Begin)) << (Pos * 8);
|
|
|
|
++Begin;
|
|
|
|
++Pos;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2020-09-27 06:57:09 +08:00
|
|
|
static const char *ReadModule(char SizeofPtr, const char *Begin,
|
|
|
|
const char *End) {
|
2016-01-16 08:31:11 +08:00
|
|
|
const char *FilenameBegin = Begin;
|
|
|
|
while (Begin != End && *Begin)
|
|
|
|
++Begin;
|
|
|
|
if (Begin == End)
|
2016-01-27 02:48:36 +08:00
|
|
|
return nullptr;
|
2018-11-27 05:48:55 +08:00
|
|
|
std::string Filename(FilenameBegin, Begin - FilenameBegin);
|
|
|
|
|
|
|
|
if (!llvm::sys::fs::exists(Filename))
|
|
|
|
Filename = std::string(llvm::sys::path::parent_path(ClInputFile)) +
|
|
|
|
std::string(llvm::sys::path::filename(Filename));
|
2016-01-16 08:31:11 +08:00
|
|
|
|
|
|
|
++Begin;
|
|
|
|
if (Begin == End)
|
2016-01-27 02:48:36 +08:00
|
|
|
return nullptr;
|
2016-01-16 08:31:11 +08:00
|
|
|
|
|
|
|
symbolize::LLVMSymbolizer::Options SymbolizerOptions;
|
|
|
|
SymbolizerOptions.Demangle = ClDemangle;
|
|
|
|
SymbolizerOptions.UseSymbolTable = true;
|
|
|
|
symbolize::LLVMSymbolizer Symbolizer(SymbolizerOptions);
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
while (1) {
|
2016-01-16 08:31:11 +08:00
|
|
|
uint64_t Addr = ReadLE(SizeofPtr, Begin, End);
|
|
|
|
Begin += SizeofPtr;
|
|
|
|
uint64_t Data = ReadLE(SizeofPtr, Begin, End);
|
|
|
|
Begin += SizeofPtr;
|
|
|
|
|
|
|
|
if (Begin > End)
|
2016-01-27 02:48:36 +08:00
|
|
|
return nullptr;
|
2016-01-16 08:31:11 +08:00
|
|
|
if (Addr == 0 && Data == 0)
|
|
|
|
return Begin;
|
|
|
|
if (Begin == End)
|
2016-01-27 02:48:36 +08:00
|
|
|
return nullptr;
|
2016-01-16 08:31:11 +08:00
|
|
|
|
2017-07-13 08:49:03 +08:00
|
|
|
// As the instrumentation tracks the return address and not
|
2017-09-12 08:19:11 +08:00
|
|
|
// the address of the call to `__sanitizer_stat_report` we
|
2017-07-13 08:49:03 +08:00
|
|
|
// remove one from the address to get the correct DI.
|
2019-02-27 21:17:36 +08:00
|
|
|
// TODO: it would be neccessary to set proper section index here.
|
|
|
|
// object::SectionedAddress::UndefSection works for only absolute addresses.
|
|
|
|
if (Expected<DILineInfo> LineInfo = Symbolizer.symbolizeCode(
|
|
|
|
Filename, {Addr - 1, object::SectionedAddress::UndefSection})) {
|
2018-11-27 05:48:45 +08:00
|
|
|
llvm::outs() << format_hex(Addr - 1, 18) << ' ' << LineInfo->FileName
|
|
|
|
<< ':' << LineInfo->Line << ' ' << LineInfo->FunctionName
|
|
|
|
<< ' ';
|
2016-01-16 08:31:11 +08:00
|
|
|
} else {
|
2016-06-04 04:25:09 +08:00
|
|
|
logAllUnhandledErrors(LineInfo.takeError(), llvm::outs(), "<error> ");
|
2016-01-16 08:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (KindFromData(Data, SizeofPtr)) {
|
|
|
|
case SanStat_CFI_VCall:
|
|
|
|
llvm::outs() << "cfi-vcall";
|
|
|
|
break;
|
|
|
|
case SanStat_CFI_NVCall:
|
|
|
|
llvm::outs() << "cfi-nvcall";
|
|
|
|
break;
|
|
|
|
case SanStat_CFI_DerivedCast:
|
|
|
|
llvm::outs() << "cfi-derived-cast";
|
|
|
|
break;
|
|
|
|
case SanStat_CFI_UnrelatedCast:
|
|
|
|
llvm::outs() << "cfi-unrelated-cast";
|
|
|
|
break;
|
|
|
|
case SanStat_CFI_ICall:
|
|
|
|
llvm::outs() << "cfi-icall";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm::outs() << "<unknown>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::outs() << " " << CountFromData(Data, SizeofPtr) << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
"Sanitizer Statistics Processing Tool");
|
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
|
|
|
|
MemoryBuffer::getFile(ClInputFile, -1, false);
|
|
|
|
if (!MBOrErr) {
|
|
|
|
errs() << argv[0] << ": " << ClInputFile << ": "
|
|
|
|
<< MBOrErr.getError().message() << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
|
|
|
|
const char *Begin = MB->getBufferStart(), *End = MB->getBufferEnd();
|
|
|
|
if (Begin == End) {
|
|
|
|
errs() << argv[0] << ": " << ClInputFile << ": short read\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char SizeofPtr = *Begin++;
|
|
|
|
while (Begin != End) {
|
|
|
|
Begin = ReadModule(SizeofPtr, Begin, End);
|
2016-01-27 02:48:36 +08:00
|
|
|
if (Begin == nullptr) {
|
2016-01-16 08:31:11 +08:00
|
|
|
errs() << argv[0] << ": " << ClInputFile << ": short read\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
assert(Begin <= End);
|
|
|
|
}
|
|
|
|
}
|