LLVM 22.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
15#include "llvm/Support/Endian.h"
18#include <optional>
19
20#include <cstdint>
21#include <memory>
22#include <string>
23#include <utility>
24
25namespace llvm {
26
27class StringRef;
28class MCDisassembler;
29class MemoryBuffer;
30class MCInstPrinter;
31class RuntimeDyld;
33class raw_ostream;
34
35/// Holds target-specific properties for a symbol.
37
38/// RuntimeDyld invariant checker for verifying that RuntimeDyld has
39/// correctly applied relocations.
40///
41/// The RuntimeDyldChecker class evaluates expressions against an attached
42/// RuntimeDyld instance to verify that relocations have been applied
43/// correctly.
44///
45/// The expression language supports basic pointer arithmetic and bit-masking,
46/// and has limited disassembler integration for accessing instruction
47/// operands and the next PC (program counter) address for each instruction.
48///
49/// The language syntax is:
50///
51/// check = expr '=' expr
52///
53/// expr = binary_expr
54/// | sliceable_expr
55///
56/// sliceable_expr = '*{' number '}' load_addr_expr [slice]
57/// | '(' expr ')' [slice]
58/// | ident_expr [slice]
59/// | number [slice]
60///
61/// slice = '[' high-bit-index ':' low-bit-index ']'
62///
63/// load_addr_expr = symbol
64/// | '(' symbol '+' number ')'
65/// | '(' symbol '-' number ')'
66///
67/// ident_expr = 'decode_operand' '(' symbol ',' operand-index ')'
68/// | 'next_pc' '(' symbol ')'
69/// | 'stub_addr' '(' stub-container-name ',' symbol ')'
70/// | 'got_addr' '(' stub-container-name ',' symbol ')'
71/// | 'section_addr' '(' stub-container-name ',' symbol ')'
72/// | symbol
73///
74/// binary_expr = expr '+' expr
75/// | expr '-' expr
76/// | expr '&' expr
77/// | expr '|' expr
78/// | expr '<<' expr
79/// | expr '>>' expr
80///
82public:
84 public:
85 MemoryRegionInfo() : Size(0), Initialized(false) {}
86
87 /// Constructor for symbols/sections with content and TargetFlag.
89 TargetFlagsType TargetFlags)
90 : ContentPtr(Content.data()), Size(Content.size()),
91 TargetAddress(TargetAddress), TargetFlags(TargetFlags) {
92 Initialized = true;
93 }
94
95 /// Constructor for zero-fill symbols/sections.
97 : Size(Size), TargetAddress(TargetAddress) {
98 Initialized = true;
99 }
100
101 /// Returns true if this is a zero-fill symbol/section.
102 bool isZeroFill() const {
103 assert(Initialized && "setZeroFill / setContent not called");
104 return !ContentPtr;
105 }
106
107 /// Set the content for this memory region.
109 assert(!Initialized && "Content/zero-fill already set");
110 ContentPtr = Content.data();
111 Size = Content.size();
112 Initialized = true;
113 }
114
115 /// Set a zero-fill length for this memory region.
117 assert(!Initialized && "Content/zero-fill already set");
118 this->Size = Size;
119 Initialized = true;
120 }
121
122 /// Returns the content for this section if there is any.
124 assert(!isZeroFill() && "Can't get content for a zero-fill section");
125 return {ContentPtr, static_cast<size_t>(Size)};
126 }
127
128 /// Returns the zero-fill length for this section.
130 assert(isZeroFill() && "Can't get zero-fill length for content section");
131 return Size;
132 }
133
134 /// Set the target address for this region.
135 void setTargetAddress(JITTargetAddress TargetAddress) {
136 assert(!this->TargetAddress && "TargetAddress already set");
137 this->TargetAddress = TargetAddress;
138 }
139
140 /// Return the target address for this region.
141 JITTargetAddress getTargetAddress() const { return TargetAddress; }
142
143 /// Get the target flags for this Symbol.
144 TargetFlagsType getTargetFlags() const { return TargetFlags; }
145
146 /// Set the target flags for this Symbol.
148 assert(Flags <= 1 && "Add more bits to store more than one flag");
149 TargetFlags = Flags;
150 }
151
152 private:
153 const char *ContentPtr = nullptr;
154 uint64_t Size : 63;
155 uint64_t Initialized : 1;
156 JITTargetAddress TargetAddress = 0;
157 TargetFlagsType TargetFlags = 0;
158 };
159
160 using IsSymbolValidFunction = std::function<bool(StringRef Symbol)>;
162 std::function<Expected<MemoryRegionInfo>(StringRef SymbolName)>;
163 using GetSectionInfoFunction = std::function<Expected<MemoryRegionInfo>(
164 StringRef FileName, StringRef SectionName)>;
165 using GetStubInfoFunction = std::function<Expected<MemoryRegionInfo>(
166 StringRef StubContainer, StringRef TargetName, StringRef StubKindFilter)>;
167 using GetGOTInfoFunction = std::function<Expected<MemoryRegionInfo>(
168 StringRef GOTContainer, StringRef TargetName)>;
169
171 IsSymbolValidFunction IsSymbolValid, GetSymbolInfoFunction GetSymbolInfo,
172 GetSectionInfoFunction GetSectionInfo, GetStubInfoFunction GetStubInfo,
173 GetGOTInfoFunction GetGOTInfo, llvm::endianness Endianness, Triple TT,
174 StringRef CPU, SubtargetFeatures TF, raw_ostream &ErrStream);
176
177 /// Check a single expression against the attached RuntimeDyld
178 /// instance.
179 LLVM_ABI bool check(StringRef CheckExpr) const;
180
181 /// Scan the given memory buffer for lines beginning with the string
182 /// in RulePrefix. The remainder of the line is passed to the check
183 /// method to be evaluated as an expression.
185 MemoryBuffer *MemBuf) const;
186
187 /// Returns the address of the requested section (or an error message
188 /// in the second element of the pair if the address cannot be found).
189 ///
190 /// if 'LocalAddress' is true, this returns the address of the section
191 /// within the linker's memory. If 'LocalAddress' is false it returns the
192 /// address within the target process (i.e. the load address).
193 LLVM_ABI std::pair<uint64_t, std::string>
194 getSectionAddr(StringRef FileName, StringRef SectionName, bool LocalAddress);
195
196 /// If there is a section at the given local address, return its load
197 /// address, otherwise return std::nullopt.
198 LLVM_ABI std::optional<uint64_t>
199 getSectionLoadAddress(void *LocalAddress) const;
200
201private:
202 std::unique_ptr<RuntimeDyldCheckerImpl> Impl;
203};
204
205} // end namespace llvm
206
207#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
static Split data
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
const T * data() const
Definition ArrayRef.h:139
Superclass for all disassemblers.
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
This interface provides simple read-only access to a block of memory, and provides simple methods for...
void setContent(ArrayRef< char > Content)
Set the content for this memory region.
void setZeroFill(uint64_t Size)
Set a zero-fill length for this memory region.
void setTargetFlags(TargetFlagsType Flags)
Set the target flags for this Symbol.
JITTargetAddress getTargetAddress() const
Return the target address for this region.
MemoryRegionInfo(ArrayRef< char > Content, JITTargetAddress TargetAddress, TargetFlagsType TargetFlags)
Constructor for symbols/sections with content and TargetFlag.
void setTargetAddress(JITTargetAddress TargetAddress)
Set the target address for this region.
ArrayRef< char > getContent() const
Returns the content for this section if there is any.
uint64_t getZeroFillLength() const
Returns the zero-fill length for this section.
MemoryRegionInfo(uint64_t Size, JITTargetAddress TargetAddress)
Constructor for zero-fill symbols/sections.
TargetFlagsType getTargetFlags() const
Get the target flags for this Symbol.
bool isZeroFill() const
Returns true if this is a zero-fill symbol/section.
LLVM_ABI 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...
LLVM_ABI bool checkAllRulesInBuffer(StringRef RulePrefix, MemoryBuffer *MemBuf) const
Scan the given memory buffer for lines beginning with the string in RulePrefix.
LLVM_ABI bool check(StringRef CheckExpr) const
Check a single expression against the attached RuntimeDyld instance.
std::function< bool(StringRef Symbol)> IsSymbolValidFunction
LLVM_ABI ~RuntimeDyldChecker()
std::function< Expected< MemoryRegionInfo >( StringRef FileName, StringRef SectionName)> GetSectionInfoFunction
LLVM_ABI RuntimeDyldChecker(IsSymbolValidFunction IsSymbolValid, GetSymbolInfoFunction GetSymbolInfo, GetSectionInfoFunction GetSectionInfo, GetStubInfoFunction GetStubInfo, GetGOTInfoFunction GetGOTInfo, llvm::endianness Endianness, Triple TT, StringRef CPU, SubtargetFeatures TF, raw_ostream &ErrStream)
LLVM_ABI std::optional< uint64_t > getSectionLoadAddress(void *LocalAddress) const
If there is a section at the given local address, return its load address, otherwise return std::null...
std::function< Expected< MemoryRegionInfo >( StringRef GOTContainer, StringRef TargetName)> GetGOTInfoFunction
std::function< Expected< MemoryRegionInfo >(StringRef SymbolName)> GetSymbolInfoFunction
std::function< Expected< MemoryRegionInfo >( StringRef StubContainer, StringRef TargetName, StringRef StubKindFilter)> GetStubInfoFunction
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
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:1667
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Definition JITSymbol.h:43
endianness
Definition bit.h:71
uint8_t TargetFlagsType
Holds target-specific properties for a symbol.