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