1212#include " bolt/Core/BinaryContext.h"
1313#include " bolt/Core/BinaryFunction.h"
1414#include " bolt/Passes/BinaryPasses.h"
15- #include " llvm/ADT/SmallSet.h"
1615#include " llvm/Support/raw_ostream.h"
1716#include < memory>
1817
@@ -65,6 +64,14 @@ struct MCInstInBFReference {
6564 uint64_t Offset;
6665 MCInstInBFReference (BinaryFunction *BF, uint64_t Offset)
6766 : BF(BF), Offset(Offset) {}
67+
68+ static MCInstInBFReference get (const MCInst *Inst, BinaryFunction &BF) {
69+ for (auto &I : BF.instrs ())
70+ if (Inst == &I.second )
71+ return MCInstInBFReference (&BF, I.first );
72+ return {};
73+ }
74+
6875 MCInstInBFReference () : BF(nullptr ), Offset(0 ) {}
6976 bool operator ==(const MCInstInBFReference &RHS) const {
7077 return BF == RHS.BF && Offset == RHS.Offset ;
@@ -104,6 +111,12 @@ struct MCInstReference {
104111 MCInstReference (BinaryFunction *BF, uint32_t Offset)
105112 : MCInstReference(MCInstInBFReference(BF, Offset)) {}
106113
114+ static MCInstReference get (const MCInst *Inst, BinaryFunction &BF) {
115+ if (BF.hasCFG ())
116+ return MCInstInBBReference::get (Inst, BF);
117+ return MCInstInBFReference::get (Inst, BF);
118+ }
119+
107120 bool operator <(const MCInstReference &RHS) const {
108121 if (ParentKind != RHS.ParentKind )
109122 return ParentKind < RHS.ParentKind ;
@@ -138,6 +151,16 @@ struct MCInstReference {
138151 llvm_unreachable (" " );
139152 }
140153
154+ operator bool () const {
155+ switch (ParentKind) {
156+ case BasicBlockParent:
157+ return U.BBRef .BB != nullptr ;
158+ case FunctionParent:
159+ return U.BFRef .BF != nullptr ;
160+ }
161+ llvm_unreachable (" " );
162+ }
163+
141164 uint64_t getAddress () const {
142165 switch (ParentKind) {
143166 case BasicBlockParent:
@@ -173,9 +196,6 @@ raw_ostream &operator<<(raw_ostream &OS, const MCInstReference &);
173196
174197namespace PAuthGadgetScanner {
175198
176- class SrcSafetyAnalysis ;
177- struct SrcState ;
178-
179199// / Description of a gadget kind that can be detected. Intended to be
180200// / statically allocated to be attached to reports by reference.
181201class GadgetKind {
@@ -184,7 +204,7 @@ class GadgetKind {
184204public:
185205 GadgetKind (const char *Description) : Description(Description) {}
186206
187- const StringRef getDescription () const { return Description; }
207+ StringRef getDescription () const { return Description; }
188208};
189209
190210// / Base report located at some instruction, without any additional information.
@@ -199,8 +219,8 @@ struct Report {
199219
200220 // The two methods below are called by Analysis::computeDetailedInfo when
201221 // iterating over the reports.
202- virtual const ArrayRef<MCPhysReg> getAffectedRegisters () const { return {}; }
203- virtual void setOverwritingInstrs (const ArrayRef<MCInstReference> Instrs) {}
222+ virtual ArrayRef<MCPhysReg> getAffectedRegisters () const { return {}; }
223+ virtual void setOverwritingInstrs (ArrayRef<MCInstReference> Instrs) {}
204224
205225 void printBasicInfo (raw_ostream &OS, const BinaryContext &BC,
206226 StringRef IssueKind) const ;
@@ -223,19 +243,19 @@ struct GadgetReport : public Report {
223243
224244 void generateReport (raw_ostream &OS, const BinaryContext &BC) const override ;
225245
226- const ArrayRef<MCPhysReg> getAffectedRegisters () const override {
246+ ArrayRef<MCPhysReg> getAffectedRegisters () const override {
227247 return AffectedRegisters;
228248 }
229249
230- void setOverwritingInstrs (const ArrayRef<MCInstReference> Instrs) override {
250+ void setOverwritingInstrs (ArrayRef<MCInstReference> Instrs) override {
231251 OverwritingInstrs.assign (Instrs.begin (), Instrs.end ());
232252 }
233253};
234254
235255// / Report with a free-form message attached.
236256struct GenericReport : public Report {
237257 std::string Text;
238- GenericReport (MCInstReference Location, const std::string & Text)
258+ GenericReport (MCInstReference Location, StringRef Text)
239259 : Report(Location), Text(Text) {}
240260 virtual void generateReport (raw_ostream &OS,
241261 const BinaryContext &BC) const override ;
0 commit comments