@@ -21,7 +21,12 @@ using namespace llvm;
2121namespace llvm {
2222namespace bolt {
2323
24+ static bool PassFailed = false ;
25+
2426void InsertNegateRAState::runOnFunction (BinaryFunction &BF) {
27+ if (PassFailed)
28+ return ;
29+
2530 BinaryContext &BC = BF.getBinaryContext ();
2631
2732 if (BF.getState () == BinaryFunction::State::Empty)
@@ -39,26 +44,31 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
3944 for (FunctionFragment &FF : BF.getLayout ().fragments ()) {
4045 coverFunctionFragmentStart (BF, FF);
4146 bool FirstIter = true ;
42- MCInst PrevInst ;
47+ bool PrevRAState = false ;
4348 // As this pass runs after function splitting, we should only check
4449 // consecutive instructions inside FunctionFragments.
4550 for (BinaryBasicBlock *BB : FF) {
4651 for (auto It = BB->begin (); It != BB->end (); ++It) {
4752 MCInst &Inst = *It;
4853 if (BC.MIB ->isCFI (Inst))
4954 continue ;
55+ auto RAState = BC.MIB ->getRAState (Inst);
56+ if (!RAState) {
57+ BC.errs () << " BOLT-ERROR: unknown RAState after inferUnknownStates "
58+ << " in function " << BF.getPrintName () << " \n " ;
59+ PassFailed = true ;
60+ return ;
61+ }
5062 if (!FirstIter) {
5163 // Consecutive instructions with different RAState means we need to
5264 // add a OpNegateRAState.
53- if ((BC.MIB ->isRASigned (PrevInst) && BC.MIB ->isRAUnsigned (Inst)) ||
54- (BC.MIB ->isRAUnsigned (PrevInst) && BC.MIB ->isRASigned (Inst))) {
65+ if (*RAState != PrevRAState)
5566 It = BF.addCFIInstruction (
5667 BB, It, MCCFIInstruction::createNegateRAState (nullptr ));
57- }
5868 } else {
5969 FirstIter = false ;
6070 }
61- PrevInst = *It ;
71+ PrevRAState = *RAState ;
6272 }
6373 }
6474 }
@@ -81,10 +91,17 @@ void InsertNegateRAState::coverFunctionFragmentStart(BinaryFunction &BF,
8191 });
8292 // If a function is already split in the input, the first FF can also start
8393 // with Signed state. This covers that scenario as well.
84- if (BC.MIB ->isRASigned (*((*FirstNonEmpty)->begin ()))) {
85- BF.addCFIInstruction (*FirstNonEmpty, (*FirstNonEmpty)->begin (),
86- MCCFIInstruction::createNegateRAState (nullptr ));
94+ auto II = (*FirstNonEmpty)->getFirstNonPseudo ();
95+ auto RAState = BC.MIB ->getRAState (*II);
96+ if (!RAState) {
97+ BC.errs () << " BOLT-ERROR: unknown RAState after inferUnknownStates "
98+ << " in function " << BF.getPrintName () << " \n " ;
99+ PassFailed = true ;
100+ return ;
87101 }
102+ if (*RAState)
103+ BF.addCFIInstruction (*FirstNonEmpty, II,
104+ MCCFIInstruction::createNegateRAState (nullptr ));
88105}
89106
90107void InsertNegateRAState::inferUnknownStates (BinaryFunction &BF) {
@@ -96,15 +113,21 @@ void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
96113 if (BC.MIB ->isCFI (Inst))
97114 continue ;
98115
99- if (!FirstIter && BC.MIB ->isRAStateUnknown (Inst)) {
100- if (BC.MIB ->isRASigned (PrevInst) || BC.MIB ->isPSignOnLR (PrevInst)) {
101- BC.MIB ->setRASigned (Inst);
102- } else if (BC.MIB ->isRAUnsigned (PrevInst) ||
103- BC.MIB ->isPAuthOnLR (PrevInst)) {
104- BC.MIB ->setRAUnsigned (Inst);
116+ auto RAState = BC.MIB ->getRAState (Inst);
117+ if (!FirstIter && !RAState) {
118+ if (BC.MIB ->isPSignOnLR (PrevInst))
119+ RAState = true ;
120+ else if (BC.MIB ->isPAuthOnLR (PrevInst))
121+ RAState = false ;
122+ else {
123+ auto PrevRAState = BC.MIB ->getRAState (PrevInst);
124+ RAState = PrevRAState ? *PrevRAState : false ;
105125 }
126+ BC.MIB ->setRAState (Inst, *RAState);
106127 } else {
107128 FirstIter = false ;
129+ if (!RAState)
130+ BC.MIB ->setRAState (Inst, BF.getInitialRAState ());
108131 }
109132 PrevInst = Inst;
110133 }
@@ -135,6 +158,8 @@ Error InsertNegateRAState::runOnFunctions(BinaryContext &BC) {
135158 << " functions "
136159 << format (" (%.2lf%%).\n " , (100.0 * FunctionsModified) /
137160 BC.getBinaryFunctions ().size ());
161+ if (PassFailed)
162+ return createFatalBOLTError (" " );
138163 return Error::success ();
139164}
140165
0 commit comments