Skip to content

Commit

Permalink
Merge pull request #167 from Prof9/fix-shift-edge-cases
Browse files Browse the repository at this point in the history
THUMB: Fix negative branches not getting truncated anymore and change…
  • Loading branch information
Kingcom committed May 2, 2020
2 parents 17952fa + 7e4730f commit 5b88a04
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 12 additions & 3 deletions Archs/ARM/CThumbInstruction.cpp
Expand Up @@ -138,15 +138,24 @@ bool CThumbInstruction::Validate()
}
Vars.Immediate = pos >> 2;
}

if (Vars.ImmediateBitLen != 32)

if (Opcode.type == THUMB_TYPE1)
{
int max = (Opcode.flags & THUMB_RIGHTSHIFT_IMMEDIATE) ? 32 : 31;
if (Vars.Immediate < 0 || Vars.Immediate > max)
{
Logger::queueError(Logger::Error, L"Shift amount 0x%02X out of range",Vars.Immediate);
return false;
}
} else if (Vars.ImmediateBitLen != 32)
{
int max = (1 << Vars.ImmediateBitLen) - ((Opcode.flags & THUMB_RIGHTSHIFT_IMMEDIATE) ? 0 : 1);
int max = (1 << Vars.ImmediateBitLen) - 1;
if (abs(Vars.Immediate) > max)
{
Logger::queueError(Logger::Error,L"Immediate value 0x%02X out of range",Vars.Immediate);
return false;
}
Vars.Immediate &= max;
}
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/ARM/ShiftInvalid/expected.txt
@@ -1,7 +1,7 @@
ShiftInvalid.asm(5) error: Immediate value 0x20 out of range
ShiftInvalid.asm(6) error: Immediate value 0x20 out of range
ShiftInvalid.asm(7) error: Immediate value 0x21 out of range
ShiftInvalid.asm(8) error: Immediate value 0x21 out of range
ShiftInvalid.asm(5) error: Shift amount 0x20 out of range
ShiftInvalid.asm(6) error: Shift amount 0x20 out of range
ShiftInvalid.asm(7) error: Shift amount 0x21 out of range
ShiftInvalid.asm(8) error: Shift amount 0x21 out of range
ShiftInvalid.asm(11) error: Shift amount 0x20 out of range
ShiftInvalid.asm(12) error: Shift amount 0x20 out of range
ShiftInvalid.asm(13) error: Shift amount 0x21 out of range
Expand Down

0 comments on commit 5b88a04

Please sign in to comment.