@@ -401,11 +401,11 @@ class PacRetAnalysis
401401
402402public: 
403403  std::vector<MCInstReference>
404-   getLastClobberingInsts (const  MCInst Ret , BinaryFunction &BF,
405-                          const  ArrayRef<MCPhysReg> UsedDirtyRegs) const   {
404+   getLastClobberingInsts (const  MCInst &Inst , BinaryFunction &BF,
405+                          const  ArrayRef<MCPhysReg> UsedDirtyRegs) {
406406    if  (RegsToTrackInstsFor.empty ())
407407      return  {};
408-     auto  MaybeState = getStateAt (Ret );
408+     auto  MaybeState = getStateBefore (Inst );
409409    if  (!MaybeState)
410410      llvm_unreachable (" Expected State to be present" 
411411    const  State &S = *MaybeState;
@@ -453,6 +453,29 @@ shouldReportReturnGadget(const BinaryContext &BC, const MCInstReference &Inst,
453453  return  std::make_shared<GadgetReport>(RetKind, Inst, RetReg);
454454}
455455
456+ static  std::shared_ptr<Report>
457+ shouldReportCallGadget (const  BinaryContext &BC, const  MCInstReference &Inst,
458+                        const  State &S) {
459+   static  const  GadgetKind CallKind (" non-protected call found" 
460+   if  (!BC.MIB ->isCall (Inst) && !BC.MIB ->isBranch (Inst))
461+     return  nullptr ;
462+ 
463+   bool  IsAuthenticated = false ;
464+   MCPhysReg DestReg = BC.MIB ->getRegUsedAsCallDest (Inst, IsAuthenticated);
465+   if  (IsAuthenticated || DestReg == BC.MIB ->getNoRegister ())
466+     return  nullptr ;
467+ 
468+   LLVM_DEBUG ({
469+     traceInst (BC, " Found call inst" 
470+     traceReg (BC, " Call destination reg" 
471+     traceRegMask (BC, " SafeToDerefRegs" SafeToDerefRegs );
472+   });
473+   if  (S.SafeToDerefRegs [DestReg])
474+     return  nullptr ;
475+ 
476+   return  std::make_shared<GadgetReport>(CallKind, Inst, DestReg);
477+ }
478+ 
456479FunctionAnalysisResult
457480Analysis::findGadgets (BinaryFunction &BF,
458481                      MCPlusBuilder::AllocatorIdTy AllocatorId) {
@@ -469,7 +492,7 @@ Analysis::findGadgets(BinaryFunction &BF,
469492  for  (BinaryBasicBlock &BB : BF) {
470493    for  (int64_t  I = 0 , E = BB.size (); I < E; ++I) {
471494      MCInstReference Inst (&BB, I);
472-       const  State &S = *PRA.getStateAt (Inst);
495+       const  State &S = *PRA.getStateBefore (Inst);
473496
474497      //  If non-empty state was never propagated from the entry basic block
475498      //  to Inst, assume it to be unreachable and report a warning.
@@ -481,6 +504,12 @@ Analysis::findGadgets(BinaryFunction &BF,
481504
482505      if  (auto  Report = shouldReportReturnGadget (BC, Inst, S))
483506        Result.Diagnostics .push_back (Report);
507+ 
508+       if  (PacRetGadgetsOnly)
509+         continue ;
510+ 
511+       if  (auto  Report = shouldReportCallGadget (BC, Inst, S))
512+         Result.Diagnostics .push_back (Report);
484513    }
485514  }
486515  return  Result;
0 commit comments