[PATCH] D20434: [MCExpr] avoid UB via negation of INT_MIN
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Thu May 19 15:11:09 PDT 2016
spatel updated this revision to Diff 57864.
spatel added a comment.
Patch updated:
1. Use David's suggestion of casting to uint64_t.
2. Fix the other similar code where this could happen too.
3. Rebased after http://reviews.llvm.org/rL270128.
http://reviews.llvm.org/D20434
Files:
lib/MC/MCExpr.cpp
test/MC/X86/imm-comments.s
Index: test/MC/X86/imm-comments.s
===================================================================
--- test/MC/X86/imm-comments.s
+++ test/MC/X86/imm-comments.s
@@ -11,8 +11,8 @@
movabsq $9223372036854775807, %rax
-# FIXME: This line causes UB failure.
-# movabsq $-9223372036854775808, %rax
+# This line should not induce undefined behavior via negation of INT64_MIN.
+movabsq $-9223372036854775808, %rax
# CHECK: movb $127, %al
# CHECK: movb $-128, %al
@@ -24,5 +24,5 @@
# CHECK: movl $-2147483648, %eax # imm = 0xFFFFFFFF80000000
# CHECK: movabsq $9223372036854775807, %rax # imm = 0x7FFFFFFFFFFFFFFF
-# FIXME-CHECK: movabsq $-9223372036854775808, %rax # imm = 0x8000000000000000
+# CHECK: movabsq $-9223372036854775808, %rax # imm = 0x8000000000000000
Index: lib/MC/MCExpr.cpp
===================================================================
--- lib/MC/MCExpr.cpp
+++ lib/MC/MCExpr.cpp
@@ -663,8 +663,10 @@
/// -(a - b + const) ==> (b - a - const)
if (Value.getSymA() && !Value.getSymB())
return false;
+
+ // The cast avoids undefined behavior if the constant is INT64_MIN.
Res = MCValue::get(Value.getSymB(), Value.getSymA(),
- -Value.getConstant());
+ -(uint64_t)Value.getConstant());
break;
case MCUnaryExpr::Not:
if (!Value.isAbsolute())
@@ -697,9 +699,10 @@
return false;
case MCBinaryExpr::Sub:
// Negate RHS and add.
+ // The cast avoids undefined behavior if the constant is INT64_MIN.
return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
RHSValue.getSymB(), RHSValue.getSymA(),
- -RHSValue.getConstant(), Res);
+ -(uint64_t)RHSValue.getConstant(), Res);
case MCBinaryExpr::Add:
return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20434.57864.patch
Type: text/x-patch
Size: 1967 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160519/5b91765b/attachment.bin>
More information about the llvm-commits
mailing list