Skip to content

Commit a073cbb

Browse files
authored
[X86] Fix misassemble due to not storing registers to state machine on RParen (#150252)
This fixes #116883. The x86 parser saves any register it encounters to a TmpReg field in its state machine, then on encountering the next valid token immediately afterwards saves it to either BaseReg, or IndexReg if BaseReg was already filled. However, this saving logic was missing on the RParen token handler, causing the parser to "forget" the register immediately beforehand. This also would prevent later validation logic from detecting the addressing mode as invalid, leading to a silent misassembly rather than an error.
1 parent 3d8db8e commit a073cbb

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,8 +1042,8 @@ class X86AsmParser : public MCTargetAsmParser {
10421042
}
10431043
PrevState = CurrState;
10441044
}
1045-
void onRParen() {
1046-
PrevState = State;
1045+
bool onRParen(StringRef &ErrMsg) {
1046+
IntelExprState CurrState = State;
10471047
switch (State) {
10481048
default:
10491049
State = IES_ERROR;
@@ -1054,9 +1054,27 @@ class X86AsmParser : public MCTargetAsmParser {
10541054
case IES_RBRAC:
10551055
case IES_RPAREN:
10561056
State = IES_RPAREN;
1057+
// In the case of a multiply, onRegister has already set IndexReg
1058+
// directly, with appropriate scale.
1059+
// Otherwise if we just saw a register it has only been stored in
1060+
// TmpReg, so we need to store it into the state machine.
1061+
if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
1062+
// If we already have a BaseReg, then assume this is the IndexReg with
1063+
// no explicit scale.
1064+
if (!BaseReg) {
1065+
BaseReg = TmpReg;
1066+
} else {
1067+
if (IndexReg)
1068+
return regsUseUpError(ErrMsg);
1069+
IndexReg = TmpReg;
1070+
Scale = 0;
1071+
}
1072+
}
10571073
IC.pushOperator(IC_RPAREN);
10581074
break;
10591075
}
1076+
PrevState = CurrState;
1077+
return false;
10601078
}
10611079
bool onOffset(const MCExpr *Val, SMLoc OffsetLoc, StringRef ID,
10621080
const InlineAsmIdentifierInfo &IDInfo,
@@ -2172,7 +2190,11 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
21722190
}
21732191
break;
21742192
case AsmToken::LParen: SM.onLParen(); break;
2175-
case AsmToken::RParen: SM.onRParen(); break;
2193+
case AsmToken::RParen:
2194+
if (SM.onRParen(ErrMsg)) {
2195+
return Error(Tok.getLoc(), ErrMsg);
2196+
}
2197+
break;
21762198
}
21772199
if (SM.hadError())
21782200
return Error(Tok.getLoc(), "unknown token in expression");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: not llvm-mc -triple x86_64-unknown-unknown %s 2>&1 | FileCheck %s
2+
3+
.intel_syntax
4+
5+
// CHECK: error: invalid base+index expression
6+
lea rdi, [(label + rsi) + rip]
7+
// CHECK: leaq 1(%rax,%rdi), %rdi
8+
lea rdi, [(rax + rdi) + 1]
9+
label:
10+
.quad 42

0 commit comments

Comments
 (0)