@@ -40,25 +40,30 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
4040 coverFunctionFragmentStart (BF, FF);
4141 bool FirstIter = true ;
4242 MCInst PrevInst;
43+ bool PrevRAState = false ;
4344 // As this pass runs after function splitting, we should only check
4445 // consecutive instructions inside FunctionFragments.
4546 for (BinaryBasicBlock *BB : FF) {
4647 for (auto It = BB->begin (); It != BB->end (); ++It) {
4748 MCInst &Inst = *It;
4849 if (BC.MIB ->isCFI (Inst))
4950 continue ;
51+ auto RAState = BC.MIB ->getRAState (Inst);
52+ if (!RAState) {
53+ BC.errs () << " BOLT-ERROR: unknown RAState after inferUnknownStates "
54+ << " in function " << BF.getPrintName () << " \n " ;
55+ }
5056 if (!FirstIter) {
5157 // Consecutive instructions with different RAState means we need to
5258 // add a OpNegateRAState.
53- if ((BC.MIB ->isRASigned (PrevInst) && BC.MIB ->isRAUnsigned (Inst)) ||
54- (BC.MIB ->isRAUnsigned (PrevInst) && BC.MIB ->isRASigned (Inst))) {
59+ if (*RAState != PrevRAState)
5560 It = BF.addCFIInstruction (
5661 BB, It, MCCFIInstruction::createNegateRAState (nullptr ));
57- }
5862 } else {
5963 FirstIter = false ;
6064 }
6165 PrevInst = *It;
66+ PrevRAState = *RAState;
6267 }
6368 }
6469 }
@@ -81,10 +86,15 @@ void InsertNegateRAState::coverFunctionFragmentStart(BinaryFunction &BF,
8186 });
8287 // If a function is already split in the input, the first FF can also start
8388 // with Signed state. This covers that scenario as well.
84- if (BC.MIB ->isRASigned (*((*FirstNonEmpty)->begin ()))) {
89+ auto RAState = BC.MIB ->getRAState (*(*FirstNonEmpty)->begin ());
90+ if (!RAState) {
91+ BC.errs () << " BOLT-ERROR: unknown RAState after inferUnknownStates "
92+ << " in function " << BF.getPrintName () << " \n " ;
93+ return ;
94+ }
95+ if (*RAState)
8596 BF.addCFIInstruction (*FirstNonEmpty, (*FirstNonEmpty)->begin (),
8697 MCCFIInstruction::createNegateRAState (nullptr ));
87- }
8898}
8999
90100void InsertNegateRAState::inferUnknownStates (BinaryFunction &BF) {
@@ -96,15 +106,21 @@ void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
96106 if (BC.MIB ->isCFI (Inst))
97107 continue ;
98108
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);
109+ auto RAState = BC.MIB ->getRAState (Inst);
110+ if (!FirstIter && !RAState) {
111+ if (BC.MIB ->isPSignOnLR (PrevInst))
112+ RAState = true ;
113+ else if (BC.MIB ->isPAuthOnLR (PrevInst))
114+ RAState = false ;
115+ else {
116+ auto PrevRAState = BC.MIB ->getRAState (PrevInst);
117+ RAState = PrevRAState ? *PrevRAState : false ;
105118 }
119+ BC.MIB ->setRAState (Inst, *RAState);
106120 } else {
107121 FirstIter = false ;
122+ if (!RAState)
123+ BC.MIB ->setRAState (Inst, BF.getInitialRAState ());
108124 }
109125 PrevInst = Inst;
110126 }
0 commit comments