History

x86-asm: Reject some invalid arith imm8 instruction

There were two errors in the arithmetic imm8 instruction. They accept only REGW, and in case the user write a xxxb opcode that variant needs to be rejected as well (it's not automatically rejected by REGW in case the destination is memory).

Michael Matz committed 10 years ago

Commit f2a4cb01 parent 0f36f604 files · +25 −2Browse files

Unified · Split

4 files
  1. i386-asm.c +7 −0
  2. i386-asm.h +1 −1
  3. tests/asmtest.S +16 −0
  4. x86_64-asm.h +1 −1
i386-asm.c+7−0
@@ -552,7 +552,14 @@ ST_FUNC void asm_opcode(TCCState *s1, int opcode) · 551 unmodified lines
552552 continue;
553553 s = (opcode - pa->sym) % NBWLX;
554554 if ((pa->instr_type & OPC_BWLX) == OPC_WLX)
555+ {
556+ /* We need to reject the xxxb opcodes that we accepted above.
557+ Note that pa->sym for WLX opcodes is the 'w' token,
558+ to get the 'b' token subtract one. */
559+ if (((opcode - pa->sym + 1) % NBWLX) == 0)
560+ continue;
555561 s++;
562+ }
556563 } else if (pa->instr_type & OPC_SHIFT) {
557564 if (!(opcode >= pa->sym && opcode < pa->sym + 7*NBWLX))
558565 continue;
i386-asm.h+1−1
@@ -157,7 +157,7 @@ ALT(DEF_ASM_OP2(lgs, 0x0fb5, 0, OPC_MODRM, OPT_EA, OPT_REG32)) · 156 unmodified lines
157157 ALT(DEF_ASM_OP2(addb, 0x00, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) /* XXX: use D bit ? */
158158 ALT(DEF_ASM_OP2(addb, 0x02, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG))
159159 ALT(DEF_ASM_OP2(addb, 0x04, 0, OPC_ARITH | OPC_BWLX, OPT_IM, OPT_EAX))
160−ALT(DEF_ASM_OP2(addw, 0x83, 0, OPC_ARITH | OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_EA | OPT_REG))
160+ALT(DEF_ASM_OP2(addw, 0x83, 0, OPC_ARITH | OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_EA | OPT_REGW))
161161 ALT(DEF_ASM_OP2(addb, 0x80, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_IM, OPT_EA | OPT_REG))
162162
163163 ALT(DEF_ASM_OP2(testb, 0x84, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG))
tests/asmtest.S+16−0
@@ -158,6 +158,22 @@ addl $0x123, %eax · 157 unmodified lines
158158 add $0x123, %ebx
159159 add $-16, %ecx
160160 add $-0x123, %esi
161+add $1, %bx
162+add $1, %ebx
163+add $-1, %bx
164+add $-1, %ebx
165+add $127, %bx
166+addl $127, %ebx
167+addl $-128, %ebx
168+addl $-128, %ebx
169+addl $-129, %ebx
170+addl $128, %ebx
171+addl $255, %ebx
172+addl $256, %ebx
173+andb $0xf, %ah
174+andb $-15, %cl
175+xorb $127, %dh
176+cmpb $42, (%eax)
161177 addl $0x123, 0x100
162178 addl $0x123, 0x100(%ebx)
163179 addl $0x123, 0x100(%ebx,%edx,2)
x86_64-asm.h+1−1
@@ -166,7 +166,7 @@ ALT(DEF_ASM_OP2(lgs, 0x0fb5, 0, OPC_MODRM, OPT_EA, OPT_REG32)) · 165 unmodified lines
166166 ALT(DEF_ASM_OP2(addb, 0x00, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) /* XXX: use D bit ? */
167167 ALT(DEF_ASM_OP2(addb, 0x02, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG))
168168 ALT(DEF_ASM_OP2(addb, 0x04, 0, OPC_ARITH | OPC_BWLX, OPT_IM, OPT_EAX))
169−ALT(DEF_ASM_OP2(addw, 0x83, 0, OPC_ARITH | OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_EA | OPT_REG))
169+ALT(DEF_ASM_OP2(addw, 0x83, 0, OPC_ARITH | OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_EA | OPT_REGW))
170170 ALT(DEF_ASM_OP2(addb, 0x80, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_IM, OPT_EA | OPT_REG))
171171
172172 ALT(DEF_ASM_OP2(testb, 0x84, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG))

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t