-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[X86AsmParser] Check displacement overflow (#75747)
A displacement is an 8-, 16-, or 32-bit value. LLVM integrated assembler silently encodes an out-of-range displacement. GNU assembler checks the displacement and may report a warning or error (error is for 64-bit addressing, done as part of https://sourceware.org/PR10636). ``` movq 0x80000000(%rip), %rax Error: 0x80000000 out of range of signed 32bit displacement movq -0x080000001(%rax), %rax Error: 0xffffffff7fffffff out of range of signed 32bit displacement movl 0x100000001(%eax), %eax Warning: 0x100000001 shortened to 0x1 ``` For 32-bit addressing, GNU assembler gives no diagnostic when the displacement is within `[-2**32,2**32)`. 16-bit addressing is similar. ``` movl 0xffffffff(%eax), %eax # no diagnostic movl -0xffffffff(%eax), %eax # no diagnostic ``` Supporting a larger range is probably because wraparound using a large constant is more reasonable. E.g. Linux kernel arch/x86/kernel/head_32.S has `leal -__PAGE_OFFSET(%ecx),%esp` where `__PAGE_OFFSET` is 0xc0000000. This patch implements a similar behavior.
- Loading branch information
Showing
5 changed files
with
99 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# RUN: not llvm-mc -triple=x86_64 %s 2>&1 | FileCheck %s --check-prefixes=CHECK,64 --implicit-check-not=error: --implicit-check-not=warning: | ||
# RUN: llvm-mc -triple=i686 --defsym A16=1 %s 2>&1 | FileCheck %s --check-prefixes=CHECK,32 --implicit-check-not=error: --implicit-check-not=warning: | ||
|
||
.ifndef A16 | ||
movq 0x80000000-1(%rip), %rax | ||
leaq -0x80000000(%rip), %rax | ||
|
||
# 64: [[#@LINE+1]]:17: error: displacement 2147483648 is not within [-2147483648, 2147483647] | ||
movq 0x80000000(%rip), %rax | ||
|
||
# 64: [[#@LINE+1]]:18: error: displacement -2147483649 is not within [-2147483648, 2147483647] | ||
leaq -0x80000001(%rip), %rax | ||
.endif | ||
|
||
movl 0xffffffff(%eax), %eax | ||
leal -0xffffffff(%eax), %eax | ||
|
||
# CHECK: [[#@LINE+1]]:19: warning: displacement 4294967296 shortened to 32-bit signed 0 | ||
movl 0xffffffff+1(%eax), %eax | ||
|
||
# CHECK: [[#@LINE+1]]:20: warning: displacement -4294967296 shortened to 32-bit signed 0 | ||
leal -0xffffffff-1(%eax), %eax | ||
# CHECK: [[#@LINE+1]]:20: warning: displacement -4294967297 shortened to 32-bit signed -1 | ||
leal -0xffffffff-2(%eax), %eax | ||
|
||
{disp8} leal 0x100(%ebx), %eax | ||
{disp8} leal -0x100(%ebx), %eax | ||
|
||
.ifdef A16 | ||
.code16 | ||
movw $0, 0xffff(%bp) | ||
movw $0, -0xffff(%si) | ||
|
||
# 32: [[#@LINE+1]]:19: warning: displacement 65536 shortened to 16-bit signed 0 | ||
movw $0, 0xffff+1(%bp) | ||
# 32: [[#@LINE+1]]:20: warning: displacement -65536 shortened to 16-bit signed 0 | ||
movw $0, -0xffff-1(%si) | ||
.endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters