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