LLVM  14.0.0git
RuntimeDyldChecker.h
Go to the documentation of this file.
1 //===---- RuntimeDyldChecker.h - RuntimeDyld tester framework -----*- C++ -*-=//
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 #ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLDCHECKER_H
10 #define LLVM_EXECUTIONENGINE_RUNTIMEDYLDCHECKER_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/Optional.h"
15 #include "llvm/Support/Endian.h"
16 
17 #include <cstdint>
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
22 namespace llvm {
23 
24 class StringRef;
25 class MCDisassembler;
26 class MemoryBuffer;
27 class MCInstPrinter;
28 class RuntimeDyld;
29 class RuntimeDyldCheckerImpl;
30 class raw_ostream;
31 
32 /// RuntimeDyld invariant checker for verifying that RuntimeDyld has
33 /// correctly applied relocations.
34 ///
35 /// The RuntimeDyldChecker class evaluates expressions against an attached
36 /// RuntimeDyld instance to verify that relocations have been applied
37 /// correctly.
38 ///
39 /// The expression language supports basic pointer arithmetic and bit-masking,
40 /// and has limited disassembler integration for accessing instruction
41 /// operands and the next PC (program counter) address for each instruction.
42 ///
43 /// The language syntax is:
44 ///
45 /// check = expr '=' expr
46 ///
47 /// expr = binary_expr
48 /// | sliceable_expr
49 ///
50 /// sliceable_expr = '*{' number '}' load_addr_expr [slice]
51 /// | '(' expr ')' [slice]
52 /// | ident_expr [slice]
53 /// | number [slice]
54 ///
55 /// slice = '[' high-bit-index ':' low-bit-index ']'
56 ///
57 /// load_addr_expr = symbol
58 /// | '(' symbol '+' number ')'
59 /// | '(' symbol '-' number ')'
60 ///
61 /// ident_expr = 'decode_operand' '(' symbol ',' operand-index ')'
62 /// | 'next_pc' '(' symbol ')'
63 /// | 'stub_addr' '(' stub-container-name ',' symbol ')'
64 /// | 'got_addr' '(' stub-container-name ',' symbol ')'
65 /// | symbol
66 ///
67 /// binary_expr = expr '+' expr
68 /// | expr '-' expr
69 /// | expr '&' expr
70 /// | expr '|' expr
71 /// | expr '<<' expr
72 /// | expr '>>' expr
73 ///
75 public:
77  public:
78  MemoryRegionInfo() = default;
79 
80  /// Constructor for symbols/sections with content.
82  : ContentPtr(Content.data()), Size(Content.size()),
83  TargetAddress(TargetAddress) {}
84 
85  /// Constructor for zero-fill symbols/sections.
87  : Size(Size), TargetAddress(TargetAddress) {}
88 
89  /// Returns true if this is a zero-fill symbol/section.
90  bool isZeroFill() const {
91  assert(Size && "setContent/setZeroFill must be called first");
92  return !ContentPtr;
93  }
94 
95  /// Set the content for this memory region.
97  assert(!ContentPtr && !Size && "Content/zero-fill already set");
98  ContentPtr = Content.data();
99  Size = Content.size();
100  }
101 
102  /// Set a zero-fill length for this memory region.
103  void setZeroFill(uint64_t Size) {
104  assert(!ContentPtr && !this->Size && "Content/zero-fill already set");
105  this->Size = Size;
106  }
107 
108  /// Returns the content for this section if there is any.
110  assert(!isZeroFill() && "Can't get content for a zero-fill section");
111  return {ContentPtr, static_cast<size_t>(Size)};
112  }
113 
114  /// Returns the zero-fill length for this section.
116  assert(isZeroFill() && "Can't get zero-fill length for content section");
117  return Size;
118  }
119 
120  /// Set the target address for this region.
121  void setTargetAddress(JITTargetAddress TargetAddress) {
122  assert(!this->TargetAddress && "TargetAddress already set");
123  this->TargetAddress = TargetAddress;
124  }
125 
126  /// Return the target address for this region.
127  JITTargetAddress getTargetAddress() const { return TargetAddress; }
128 
129  private:
130  const char *ContentPtr = 0;
131  uint64_t Size = 0;
132  JITTargetAddress TargetAddress = 0;
133  };
134 
136  using GetSymbolInfoFunction =
137  std::function<Expected<MemoryRegionInfo>(StringRef SymbolName)>;
138  using GetSectionInfoFunction = std::function<Expected<MemoryRegionInfo>(
140  using GetStubInfoFunction = std::function<Expected<MemoryRegionInfo>(
141  StringRef StubContainer, StringRef TargetName)>;
142  using GetGOTInfoFunction = std::function<Expected<MemoryRegionInfo>(
143  StringRef GOTContainer, StringRef TargetName)>;
144 
146  GetSymbolInfoFunction GetSymbolInfo,
147  GetSectionInfoFunction GetSectionInfo,
148  GetStubInfoFunction GetStubInfo,
149  GetGOTInfoFunction GetGOTInfo,
151  MCDisassembler *Disassembler, MCInstPrinter *InstPrinter,
152  raw_ostream &ErrStream);
154 
155  /// Check a single expression against the attached RuntimeDyld
156  /// instance.
157  bool check(StringRef CheckExpr) const;
158 
159  /// Scan the given memory buffer for lines beginning with the string
160  /// in RulePrefix. The remainder of the line is passed to the check
161  /// method to be evaluated as an expression.
162  bool checkAllRulesInBuffer(StringRef RulePrefix, MemoryBuffer *MemBuf) const;
163 
164  /// Returns the address of the requested section (or an error message
165  /// in the second element of the pair if the address cannot be found).
166  ///
167  /// if 'LocalAddress' is true, this returns the address of the section
168  /// within the linker's memory. If 'LocalAddress' is false it returns the
169  /// address within the target process (i.e. the load address).
170  std::pair<uint64_t, std::string> getSectionAddr(StringRef FileName,
172  bool LocalAddress);
173 
174  /// If there is a section at the given local address, return its load
175  /// address, otherwise return none.
176  Optional<uint64_t> getSectionLoadAddress(void *LocalAddress) const;
177 
178 private:
179  std::unique_ptr<RuntimeDyldCheckerImpl> Impl;
180 };
181 
182 } // end namespace llvm
183 
184 #endif
llvm::RuntimeDyldChecker::IsSymbolValidFunction
std::function< bool(StringRef Symbol)> IsSymbolValidFunction
Definition: RuntimeDyldChecker.h:135
llvm::RuntimeDyldChecker::MemoryRegionInfo::setZeroFill
void setZeroFill(uint64_t Size)
Set a zero-fill length for this memory region.
Definition: RuntimeDyldChecker.h:103
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::msgpack::Endianness
constexpr support::endianness Endianness
The endianness of all multi-byte encoded values in MessagePack.
Definition: MsgPack.h:24
llvm::RuntimeDyldChecker::MemoryRegionInfo::MemoryRegionInfo
MemoryRegionInfo(uint64_t Size, JITTargetAddress TargetAddress)
Constructor for zero-fill symbols/sections.
Definition: RuntimeDyldChecker.h:86
JITSymbol.h
Optional.h
llvm::RuntimeDyldChecker::check
bool check(StringRef CheckExpr) const
Check a single expression against the attached RuntimeDyld instance.
Definition: RuntimeDyldChecker.cpp:897
Content
T Content
Definition: ELFObjHandler.cpp:90
DenseMap.h
llvm::RuntimeDyldChecker::MemoryRegionInfo::getTargetAddress
JITTargetAddress getTargetAddress() const
Return the target address for this region.
Definition: RuntimeDyldChecker.h:127
llvm::Optional< uint64_t >
llvm::RuntimeDyldChecker::RuntimeDyldChecker
RuntimeDyldChecker(IsSymbolValidFunction IsSymbolValid, GetSymbolInfoFunction GetSymbolInfo, GetSectionInfoFunction GetSectionInfo, GetStubInfoFunction GetStubInfo, GetGOTInfoFunction GetGOTInfo, support::endianness Endianness, MCDisassembler *Disassembler, MCInstPrinter *InstPrinter, raw_ostream &ErrStream)
Definition: RuntimeDyldChecker.cpp:883
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::RuntimeDyldChecker::MemoryRegionInfo::isZeroFill
bool isZeroFill() const
Returns true if this is a zero-fill symbol/section.
Definition: RuntimeDyldChecker.h:90
llvm::RuntimeDyldChecker::MemoryRegionInfo::setContent
void setContent(ArrayRef< char > Content)
Set the content for this memory region.
Definition: RuntimeDyldChecker.h:96
llvm::RuntimeDyldChecker::GetGOTInfoFunction
std::function< Expected< MemoryRegionInfo >(StringRef GOTContainer, StringRef TargetName)> GetGOTInfoFunction
Definition: RuntimeDyldChecker.h:143
llvm::RuntimeDyldChecker::getSectionAddr
std::pair< uint64_t, std::string > getSectionAddr(StringRef FileName, StringRef SectionName, bool LocalAddress)
Returns the address of the requested section (or an error message in the second element of the pair i...
Definition: RuntimeDyldChecker.cpp:907
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::RuntimeDyldChecker::MemoryRegionInfo::MemoryRegionInfo
MemoryRegionInfo(ArrayRef< char > Content, JITTargetAddress TargetAddress)
Constructor for symbols/sections with content.
Definition: RuntimeDyldChecker.h:81
llvm::RuntimeDyldChecker::GetSectionInfoFunction
std::function< Expected< MemoryRegionInfo >(StringRef FileName, StringRef SectionName)> GetSectionInfoFunction
Definition: RuntimeDyldChecker.h:139
uint64_t
llvm::MCInstPrinter
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:43
llvm::MCDisassembler
Superclass for all disassemblers.
Definition: MCDisassembler.h:76
llvm::RuntimeDyldChecker::getSectionLoadAddress
Optional< uint64_t > getSectionLoadAddress(void *LocalAddress) const
If there is a section at the given local address, return its load address, otherwise return none.
llvm::RuntimeDyldChecker::GetSymbolInfoFunction
std::function< Expected< MemoryRegionInfo >(StringRef SymbolName)> GetSymbolInfoFunction
Definition: RuntimeDyldChecker.h:137
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::RuntimeDyldChecker::MemoryRegionInfo
Definition: RuntimeDyldChecker.h:76
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1532
llvm::ArrayRef< char >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::AMDGPU::HSAMD::Kernel::Key::SymbolName
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
Definition: AMDGPUMetadata.h:381
llvm::RuntimeDyldChecker::MemoryRegionInfo::getZeroFillLength
uint64_t getZeroFillLength() const
Returns the zero-fill length for this section.
Definition: RuntimeDyldChecker.h:115
llvm::SectionName
Definition: DWARFSection.h:21
llvm::RuntimeDyldChecker
RuntimeDyld invariant checker for verifying that RuntimeDyld has correctly applied relocations.
Definition: RuntimeDyldChecker.h:74
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
llvm::support::endianness
endianness
Definition: Endian.h:27
llvm::RuntimeDyldChecker::~RuntimeDyldChecker
~RuntimeDyldChecker()
Definition: RuntimeDyldChecker.cpp:895
llvm::RuntimeDyldChecker::checkAllRulesInBuffer
bool checkAllRulesInBuffer(StringRef RulePrefix, MemoryBuffer *MemBuf) const
Scan the given memory buffer for lines beginning with the string in RulePrefix.
Definition: RuntimeDyldChecker.cpp:901
llvm::RuntimeDyldChecker::MemoryRegionInfo::MemoryRegionInfo
MemoryRegionInfo()=default
Endian.h
llvm::RuntimeDyldChecker::GetStubInfoFunction
std::function< Expected< MemoryRegionInfo >(StringRef StubContainer, StringRef TargetName)> GetStubInfoFunction
Definition: RuntimeDyldChecker.h:141
llvm::RuntimeDyldChecker::MemoryRegionInfo::getContent
ArrayRef< char > getContent() const
Returns the content for this section if there is any.
Definition: RuntimeDyldChecker.h:109
llvm::RuntimeDyldChecker::MemoryRegionInfo::setTargetAddress
void setTargetAddress(JITTargetAddress TargetAddress)
Set the target address for this region.
Definition: RuntimeDyldChecker.h:121