Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76c300c

Browse files
authored
[BOLT][BTI] Fix assertions checking getNumOperands (#174600)
Several BTI-related functions are checking that a call MCInst has one non-annotation operand. This patch changes these checks to use MCPlus::getNumPrimeOperands, instead of getNumOperands. Testing: added annotations to existing gtests to serve as regression tests. These now also explicitly check getNumOperands and getNumPrimeOperands usage on the annotated MCInsts.
1 parent ad2c2b2 commit 76c300c

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

bolt/lib/Passes/PointerAuthCFIFixup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void PointerAuthCFIFixup::fillUnknownStateInBB(BinaryContext &BC,
183183
void PointerAuthCFIFixup::markUnknownBlock(BinaryContext &BC,
184184
BinaryBasicBlock &BB, bool State) {
185185
// If we call this when an Instruction has either kRASigned or kRAUnsigned
186-
// annotation, setRASigned or setRAUnsigned would fail.
186+
// annotation, setRAState would fail.
187187
assert(isUnknownBlock(BC, BB) &&
188188
"markUnknownBlock should only be called on unknown blocks");
189189
for (MCInst &Inst : BB) {

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,9 +2788,10 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
27882788
bool CallTarget = BTI == BTIKind::C || BTI == BTIKind::JC;
27892789
bool JumpTarget = BTI == BTIKind::J || BTI == BTIKind::JC;
27902790
unsigned HintNum = getBTIHintNum(CallTarget, JumpTarget);
2791-
bool IsExplicitBTI =
2792-
Inst.getOpcode() == AArch64::HINT && Inst.getNumOperands() == 1 &&
2793-
Inst.getOperand(0).isImm() && Inst.getOperand(0).getImm() == HintNum;
2791+
bool IsExplicitBTI = Inst.getOpcode() == AArch64::HINT &&
2792+
MCPlus::getNumPrimeOperands(Inst) == 1 &&
2793+
Inst.getOperand(0).isImm() &&
2794+
Inst.getOperand(0).getImm() == HintNum;
27942795

27952796
// Only "BTI C" can be implicit.
27962797
bool IsImplicitBTI =
@@ -2818,7 +2819,7 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
28182819
// x16 or x17. If the operand is not x16 or x17, it can be accepted by a BTI
28192820
// j or BTI jc (and not BTI c).
28202821
if (isIndirectBranch(Call)) {
2821-
assert(Call.getNumOperands() == 1 &&
2822+
assert(MCPlus::getNumPrimeOperands(Call) == 1 &&
28222823
"Indirect branch needs to have 1 operand.");
28232824
assert(Call.getOperand(0).isReg() &&
28242825
"Indirect branch does not have a register operand.");
@@ -2856,7 +2857,7 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
28562857
// x16 or x17. If the operand is not x16 or x17, it can be accepted by a
28572858
// BTI j or BTI jc (and not BTI c).
28582859
if (isIndirectBranch(Call)) {
2859-
assert(Call.getNumOperands() == 1 &&
2860+
assert(MCPlus::getNumPrimeOperands(Call) == 1 &&
28602861
"Indirect branch needs to have 1 operand.");
28612862
assert(Call.getOperand(0).isReg() &&
28622863
"Indirect branch does not have a register operand.");

bolt/unittests/Core/MCPlusBuilder.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,19 @@ TEST_P(MCPlusBuilderTester, AArch64_BTI) {
152152
MCInst BTIjc;
153153
BC->MIB->createBTI(BTIjc, BTIKind::JC);
154154
BB->addInstruction(BTIjc);
155+
BC->MIB->setRAState(BTIjc, true);
156+
ASSERT_NE(BTIjc.getNumOperands(), 1u);
157+
ASSERT_EQ(MCPlus::getNumPrimeOperands(BTIjc), 1u);
155158
auto II = BB->begin();
156159
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
157160
ASSERT_EQ(II->getOperand(0).getImm(), 38);
158161
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, BTIKind::JC));
159162

160163
MCInst BTIj;
161164
BC->MIB->createBTI(BTIj, BTIKind::J);
165+
BC->MIB->setRAState(BTIj, false);
166+
ASSERT_NE(BTIj.getNumOperands(), 1u);
167+
ASSERT_EQ(MCPlus::getNumPrimeOperands(BTIj), 1u);
162168
II = BB->addInstruction(BTIj);
163169
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
164170
ASSERT_EQ(II->getOperand(0).getImm(), 36);
@@ -206,6 +212,11 @@ TEST_P(MCPlusBuilderTester, AArch64_insertBTI_0) {
206212
BB->addInstruction(Inst);
207213
// BR x16 needs BTI c or BTI j. We prefer adding a BTI c.
208214
MCInst CallInst = MCInstBuilder(AArch64::BR).addReg(AArch64::X16);
215+
// Adding an annotation to the call, to check if param numbers are calculated
216+
// correctly. Could be any other annotation as well.
217+
BC->MIB->setRAState(CallInst, false);
218+
ASSERT_NE(CallInst.getNumOperands(), 1u);
219+
ASSERT_EQ(MCPlus::getNumPrimeOperands(CallInst), 1u);
209220
auto II = BB->begin();
210221
ASSERT_FALSE(BC->MIB->isCallCoveredByBTI(CallInst, *II));
211222
BC->MIB->insertBTI(*BB, CallInst);
@@ -223,6 +234,9 @@ TEST_P(MCPlusBuilderTester, AArch64_insertBTI_1) {
223234
BB->addInstruction(BTIc);
224235
// BR x16 needs BTI c or BTI j. We have a BTI c, no change is needed.
225236
MCInst CallInst = MCInstBuilder(AArch64::BR).addReg(AArch64::X16);
237+
BC->MIB->setRAState(CallInst, true);
238+
ASSERT_NE(CallInst.getNumOperands(), 1u);
239+
ASSERT_EQ(MCPlus::getNumPrimeOperands(CallInst), 1u);
226240
auto II = BB->begin();
227241
ASSERT_TRUE(BC->MIB->isCallCoveredByBTI(CallInst, *II));
228242
BC->MIB->insertBTI(*BB, CallInst);

0 commit comments

Comments
 (0)