@@ -46,14 +46,16 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
4646 bool FirstIter = true ;
4747 MCInst PrevInst;
4848 BinaryBasicBlock *PrevBB = nullptr ;
49+ // We need to iterate on BBs in the Layout order
50+ // not in the order they are stored in the BF class.
4951 auto *Begin = BF.getLayout ().block_begin ();
5052 auto *End = BF.getLayout ().block_end ();
5153 for (auto *BB = Begin; BB != End; BB++) {
5254
5355 // Support for function splitting:
54- // if two consecutive BBs are going to end up in different functions,
55- // we have to negate the RA State, so the new function starts with a Signed
56- // state.
56+ // if two consecutive BBs with Signed state are going to end up in different
57+ // functions, we have to add a OpNegateRAState to the beginning of the newly
58+ // split function, so it starts with a Signed state.
5759 if (PrevBB != nullptr &&
5860 PrevBB->getFragmentNum () != (*BB)->getFragmentNum () &&
5961 BC.MIB ->isRASigned (*((*BB)->begin ()))) {
@@ -68,6 +70,8 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
6870 continue ;
6971
7072 if (!FirstIter) {
73+ // Consecutive instructions with different RAState means we need to add
74+ // a OpNegateRAState.
7175 if ((BC.MIB ->isRASigned (PrevInst) && BC.MIB ->isRAUnsigned (Inst)) ||
7276 (BC.MIB ->isRAUnsigned (PrevInst) && BC.MIB ->isRASigned (Inst))) {
7377
@@ -90,7 +94,8 @@ bool InsertNegateRAState::addNegateRAStateAfterPacOrAuth(BinaryFunction &BF) {
9094 for (BinaryBasicBlock &BB : BF) {
9195 for (auto Iter = BB.begin (); Iter != BB.end (); ++Iter) {
9296 MCInst &Inst = *Iter;
93- if (BC.MIB ->isPSign (Inst) || BC.MIB ->isPAuth (Inst)) {
97+ if (BC.MIB ->isPSignOnLR (Inst) ||
98+ (BC.MIB ->isPAuthOnLR (Inst) && !BC.MIB ->isPAuthAndRet (Inst))) {
9499 Iter = BF.addCFIInstruction (
95100 &BB, Iter + 1 , MCCFIInstruction::createNegateRAState (nullptr ));
96101 FoundAny = true ;
0 commit comments