llvm-project
155 строк · 5.1 Кб
1//===- sanstats.cpp - Sanitizer statistics dumper -------------------------===//
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//
9// This tool dumps statistics information from files in the format produced
10// by clang's -fsanitize-stats feature.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
15#include "llvm/DebugInfo/Symbolize/Symbolize.h"
16#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/ErrorOr.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/Path.h"
21#include "llvm/Transforms/Utils/SanitizerStats.h"
22#include <stdint.h>
23
24using namespace llvm;
25
26static cl::OptionCategory Cat("sanstats Options");
27
28static cl::opt<std::string> ClInputFile(cl::Positional, cl::Required,
29cl::desc("<filename>"));
30
31static cl::opt<bool> ClDemangle("demangle", cl::init(false),
32cl::desc("Print demangled function name"),
33cl::cat(Cat));
34
35inline uint64_t KindFromData(uint64_t Data, char SizeofPtr) {
36return Data >> (SizeofPtr * 8 - kSanitizerStatKindBits);
37}
38
39inline uint64_t CountFromData(uint64_t Data, char SizeofPtr) {
40return Data & ((1ull << (SizeofPtr * 8 - kSanitizerStatKindBits)) - 1);
41}
42
43static uint64_t ReadLE(char Size, const char *Begin, const char *End) {
44uint64_t Result = 0;
45char Pos = 0;
46while (Begin < End && Pos != Size) {
47Result |= uint64_t(uint8_t(*Begin)) << (Pos * 8);
48++Begin;
49++Pos;
50}
51return Result;
52}
53
54static const char *ReadModule(char SizeofPtr, const char *Begin,
55const char *End) {
56const char *FilenameBegin = Begin;
57while (Begin != End && *Begin)
58++Begin;
59if (Begin == End)
60return nullptr;
61std::string Filename(FilenameBegin, Begin - FilenameBegin);
62
63if (!llvm::sys::fs::exists(Filename))
64Filename = std::string(llvm::sys::path::parent_path(ClInputFile)) +
65std::string(llvm::sys::path::filename(Filename));
66
67++Begin;
68if (Begin == End)
69return nullptr;
70
71symbolize::LLVMSymbolizer::Options SymbolizerOptions;
72SymbolizerOptions.Demangle = ClDemangle;
73SymbolizerOptions.UseSymbolTable = true;
74symbolize::LLVMSymbolizer Symbolizer(SymbolizerOptions);
75
76while (true) {
77uint64_t Addr = ReadLE(SizeofPtr, Begin, End);
78Begin += SizeofPtr;
79uint64_t Data = ReadLE(SizeofPtr, Begin, End);
80Begin += SizeofPtr;
81
82if (Begin > End)
83return nullptr;
84if (Addr == 0 && Data == 0)
85return Begin;
86if (Begin == End)
87return nullptr;
88
89// As the instrumentation tracks the return address and not
90// the address of the call to `__sanitizer_stat_report` we
91// remove one from the address to get the correct DI.
92// TODO: it would be neccessary to set proper section index here.
93// object::SectionedAddress::UndefSection works for only absolute addresses.
94if (Expected<DILineInfo> LineInfo = Symbolizer.symbolizeCode(
95Filename, {Addr - 1, object::SectionedAddress::UndefSection})) {
96llvm::outs() << format_hex(Addr - 1, 18) << ' ' << LineInfo->FileName
97<< ':' << LineInfo->Line << ' ' << LineInfo->FunctionName
98<< ' ';
99} else {
100logAllUnhandledErrors(LineInfo.takeError(), llvm::outs(), "<error> ");
101}
102
103switch (KindFromData(Data, SizeofPtr)) {
104case SanStat_CFI_VCall:
105llvm::outs() << "cfi-vcall";
106break;
107case SanStat_CFI_NVCall:
108llvm::outs() << "cfi-nvcall";
109break;
110case SanStat_CFI_DerivedCast:
111llvm::outs() << "cfi-derived-cast";
112break;
113case SanStat_CFI_UnrelatedCast:
114llvm::outs() << "cfi-unrelated-cast";
115break;
116case SanStat_CFI_ICall:
117llvm::outs() << "cfi-icall";
118break;
119default:
120llvm::outs() << "<unknown>";
121break;
122}
123
124llvm::outs() << " " << CountFromData(Data, SizeofPtr) << '\n';
125}
126}
127
128int main(int argc, char **argv) {
129cl::HideUnrelatedOptions(Cat);
130cl::ParseCommandLineOptions(argc, argv,
131"Sanitizer Statistics Processing Tool");
132
133ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(
134ClInputFile, /*IsText=*/false, /*RequiresNullTerminator=*/false);
135if (!MBOrErr) {
136errs() << argv[0] << ": " << ClInputFile << ": "
137<< MBOrErr.getError().message() << '\n';
138return 1;
139}
140std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
141const char *Begin = MB->getBufferStart(), *End = MB->getBufferEnd();
142if (Begin == End) {
143errs() << argv[0] << ": " << ClInputFile << ": short read\n";
144return 1;
145}
146char SizeofPtr = *Begin++;
147while (Begin != End) {
148Begin = ReadModule(SizeofPtr, Begin, End);
149if (Begin == nullptr) {
150errs() << argv[0] << ": " << ClInputFile << ": short read\n";
151return 1;
152}
153assert(Begin <= End);
154}
155}
156