Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leal (%eax,%eax), %edx invalid operand. failure parsing operand (%eax,%eax), bad supporting to index addressing #575

Open
bbqz007 opened this issue Jan 6, 2024 · 4 comments

Comments

@bbqz007
Copy link

bbqz007 commented Jan 6, 2024

leal (%eax,%eax), %edx means edx = eax + eax,

@bbqz007 bbqz007 changed the title leal (%eax,%eax), %edx invalid operand. failure parsing operand (%eax,%eax) leal (%eax,%eax), %edx invalid operand. failure parsing operand (%eax,%eax), bad supporting to index addressing Jan 7, 2024
@bbqz007
Copy link
Author

bbqz007 commented Jan 7, 2024

operation displacement(base, index, scale) not ok

operation displacement(base, scale) ok

@bbqz007
Copy link
Author

bbqz007 commented Jan 7, 2024

X86AsmParser.cpp ::X86AsmParser::ParseMemOperand

  if (getLexer().is(AsmToken::Percent)) {
    SMLoc StartLoc, EndLoc;
    BaseLoc = Parser.getTok().getLoc();
    if (ParseRegister(BaseReg, StartLoc, EndLoc, ErrorCode)) {
        KsError = KS_ERR_ASM_INVALIDOPERAND;
        return nullptr;

X86AsmParser.cpp ::X86AsmParser::ParseRegister

  Parser.Lex(); // Eat identifier token.
  return false;
}

X86AsmParser.cpp ::X86AsmParser::ParseMemOperand

  if (getLexer().is(AsmToken::Comma)) {
    Parser.Lex(); // Eat the comma.
    IndexLoc = Parser.getTok().getLoc();

  • the correct logic
    • after BaseReg, enter try to Parse Loc,
    • if not Loc, and is Comma, may be IndexReg
    • after IndexReg, try to Parse Loc
  • problem
    • when parse Loc after BaseReg, the failure code path eat the comma then return
    • so skip the IndexReg branch.

@bbqz007
Copy link
Author

bbqz007 commented Jan 7, 2024

    // Following the comma we should have either an index register, or a scale
    // value. We don't support the later form, but we want to parse it
    // correctly.
    //
    // Not that even though it would be completely consistent to support syntax
    // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
    if (getLexer().is(AsmToken::Percent)) {
      SMLoc L;
      if (ParseRegister(IndexReg, L, L, ErrorCode)) {
          KsError = KS_ERR_ASM_INVALIDOPERAND;
          return nullptr;
      }

      if (ParseRegister(IndexReg, L, L, ErrorCode)) {
        KsError = KS_ERR_ASM_X86_INVALIDOPERAND;
        return nullptr;
      }

there are twice parsing IndexReg.

maybe the origin version is

    if (getLexer().is(AsmToken::Percent)) {
      SMLoc L;
      if (ParseRegister(BaseReg, L, L, ErrorCode)) {
          KsError = KS_ERR_ASM_INVALIDOPERAND;
          return nullptr;
      }

      if (ParseRegister(IndexReg, L, L, ErrorCode)) {
        KsError = KS_ERR_ASM_X86_INVALIDOPERAND;
        return nullptr;
      }

to solve like "1(%eax,,1)",
they move the BaseReg parsing code out of this if condition code path, and leave the un-debug codes where it was.

@bbqz007
Copy link
Author

bbqz007 commented Jan 7, 2024

Yes, it is.

the problem is solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant