@@ -157,9 +157,10 @@ void InsertNegateRAState::fillUnknownStateInBB(BinaryContext &BC,
157157 if (BC.MIB ->isCFI (Inst))
158158 continue ;
159159
160+ // No need to check for nullopt: we only entered this loop after the first
161+ // instruction had its RAState set, and RAState is always set for the
162+ // previous instruction in the previous iteration of the loop.
160163 auto PrevRAState = BC.MIB ->getRAState (Prev);
161- if (!PrevRAState)
162- llvm_unreachable (" Previous Instruction has no RAState." );
163164
164165 auto RAState = BC.MIB ->getRAState (Inst);
165166 if (!RAState) {
@@ -198,6 +199,47 @@ void InsertNegateRAState::markUnknownBlock(BinaryContext &BC,
198199 }
199200}
200201
202+ void InsertNegateRAState::fillUnknownStubs (BinaryFunction &BF) {
203+ BinaryContext &BC = BF.getBinaryContext ();
204+ bool FirstIter = true ;
205+ MCInst PrevInst;
206+ for (FunctionFragment &FF : BF.getLayout ().fragments ()) {
207+ for (BinaryBasicBlock *BB : FF) {
208+ if (!FirstIter && isUnknownBlock (BC, *BB)) {
209+ // If we have no predecessors or successors, current BB is a Stub called
210+ // from another BinaryFunction. As of #160989, we have to copy the
211+ // PrevInst's RAState, because CFIs are already incorrect here.
212+ if (BB->pred_size () == 0 && BB->succ_size () == 0 ) {
213+ auto PrevRAState = BC.MIB ->getRAState (PrevInst);
214+ if (!PrevRAState) {
215+ llvm_unreachable (
216+ " Previous Instruction has no RAState in fillUnknownStubs." );
217+ continue ;
218+ }
219+
220+ if (BC.MIB ->isPSignOnLR (PrevInst)) {
221+ PrevRAState = true ;
222+ } else if (BC.MIB ->isPAuthOnLR (PrevInst)) {
223+ PrevRAState = false ;
224+ }
225+ markUnknownBlock (BC, *BB, *PrevRAState);
226+ }
227+ }
228+ if (FirstIter) {
229+ FirstIter = false ;
230+ if (isUnknownBlock (BC, *BB))
231+ // Set block to unsigned, because this is the first block of the
232+ // function, and all instruction in it are new instructions generated
233+ // by BOLT optimizations.
234+ markUnknownBlock (BC, *BB, false );
235+ }
236+ auto Last = BB->getLastNonPseudo ();
237+ if (Last != BB->rend ())
238+ PrevInst = *Last;
239+ }
240+ }
241+ }
242+
201243std::optional<bool > InsertNegateRAState::getRAStateByCFG (BinaryBasicBlock &BB,
202244 BinaryFunction &BF) {
203245 BinaryContext &BC = BF.getBinaryContext ();
@@ -244,43 +286,6 @@ std::optional<bool> InsertNegateRAState::getRAStateByCFG(BinaryBasicBlock &BB,
244286 return NeighborRAState;
245287}
246288
247- void InsertNegateRAState::fillUnknownStubs (BinaryFunction &BF) {
248- BinaryContext &BC = BF.getBinaryContext ();
249- bool FirstIter = true ;
250- MCInst PrevInst;
251- for (FunctionFragment &FF : BF.getLayout ().fragments ()) {
252- for (BinaryBasicBlock *BB : FF) {
253- if (!FirstIter && isUnknownBlock (BC, *BB)) {
254- // If we have no predecessors or successors, current BB is a Stub called
255- // from another BinaryFunction. As of #160989, we have to copy the
256- // PrevInst's RAState, because CFIs are already incorrect here.
257- if (BB->pred_size () == 0 && BB->succ_size () == 0 ) {
258- auto PrevRAState = BC.MIB ->getRAState (PrevInst);
259- if (!PrevRAState) {
260- llvm_unreachable (
261- " Previous Instruction has no RAState in fillUnknownStubs." );
262- continue ;
263- }
264-
265- if (BC.MIB ->isPSignOnLR (PrevInst)) {
266- PrevRAState = true ;
267- } else if (BC.MIB ->isPAuthOnLR (PrevInst)) {
268- PrevRAState = false ;
269- }
270- markUnknownBlock (BC, *BB, *PrevRAState);
271- }
272- }
273- if (FirstIter) {
274- FirstIter = false ;
275- if (isUnknownBlock (BC, *BB))
276- markUnknownBlock (BC, *BB, false );
277- }
278- auto Last = BB->getLastNonPseudo ();
279- if (Last != BB->rend ())
280- PrevInst = *Last;
281- }
282- }
283- }
284289void InsertNegateRAState::fillUnknownBlocksInCFG (BinaryFunction &BF) {
285290 BinaryContext &BC = BF.getBinaryContext ();
286291
0 commit comments