Skip to content
Snippets Groups Projects
Commit b648ea82 authored by Nguyen Anh Quynh's avatar Nguyen Anh Quynh
Browse files

x86: sanity check on Scale of memory operand. this fixes issue #154

parent 4d8f1962
No related branches found
No related tags found
No related merge requests found
...@@ -1263,7 +1263,8 @@ static unsigned getIntelMemOperandSize(StringRef OpStr) { ...@@ -1263,7 +1263,8 @@ static unsigned getIntelMemOperandSize(StringRef OpStr) {
std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm( std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm(
unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg, unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg,
unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier, unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier,
InlineAsmIdentifierInfo &Info) { InlineAsmIdentifierInfo &Info)
{
// If we found a decl other than a VarDecl, then assume it is a FuncDecl or // If we found a decl other than a VarDecl, then assume it is a FuncDecl or
// some other label reference. // some other label reference.
if (isa<MCSymbolRefExpr>(Disp) && Info.OpDecl && !Info.IsVarDecl) { if (isa<MCSymbolRefExpr>(Disp) && Info.OpDecl && !Info.IsVarDecl) {
...@@ -1588,6 +1589,18 @@ X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start, ...@@ -1588,6 +1589,18 @@ X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
int IndexReg = SM.getIndexReg(); int IndexReg = SM.getIndexReg();
//printf("--- BaseReg = %u, IndexReg = %u, SegReg = %u\n", BaseReg, IndexReg, SegReg); //printf("--- BaseReg = %u, IndexReg = %u, SegReg = %u\n", BaseReg, IndexReg, SegReg);
int Scale = SM.getScale(); int Scale = SM.getScale();
if (IndexReg !=0 && !Scale) {
// Scale must go with Index register
KsError = KS_ERR_ASM_INVALIDOPERAND;
return nullptr;
}
if (Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
// invalid Scale
KsError = KS_ERR_ASM_INVALIDOPERAND;
return nullptr;
}
if (!isParsingInlineAsm()) { if (!isParsingInlineAsm()) {
// handle [-42] // handle [-42]
if (!BaseReg && !IndexReg) { if (!BaseReg && !IndexReg) {
...@@ -2381,6 +2394,18 @@ std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg, ...@@ -2381,6 +2394,18 @@ std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
return nullptr; return nullptr;
} }
if (IndexReg !=0 && !Scale) {
// Scale must go with Index register
KsError = KS_ERR_ASM_INVALIDOPERAND;
return nullptr;
}
if (Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
// invalid Scale
KsError = KS_ERR_ASM_INVALIDOPERAND;
return nullptr;
}
if (SegReg || BaseReg || IndexReg) if (SegReg || BaseReg || IndexReg)
return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg, return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
IndexReg, Scale, MemStart, MemEnd); IndexReg, Scale, MemStart, MemEnd);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment