[llvm] r270218 - [MCExpr] avoid UB via negation of INT_MIN
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Fri May 20 07:09:43 PDT 2016
Author: spatel
Date: Fri May 20 09:09:41 2016
New Revision: 270218
URL: http://llvm.org/viewvc/llvm-project?rev=270218&view=rev
Log:
[MCExpr] avoid UB via negation of INT_MIN
I accidentally exposed a bug in MCExpr::evaluateAsRelocatableImpl() with the test file added in:
http://reviews.llvm.org/rL269977
Differential Revision: http://reviews.llvm.org/D20434
Modified:
llvm/trunk/lib/MC/MCExpr.cpp
llvm/trunk/test/MC/X86/imm-comments.s
Modified: llvm/trunk/lib/MC/MCExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCExpr.cpp?rev=270218&r1=270217&r2=270218&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCExpr.cpp (original)
+++ llvm/trunk/lib/MC/MCExpr.cpp Fri May 20 09:09:41 2016
@@ -663,8 +663,10 @@ bool MCExpr::evaluateAsRelocatableImpl(M
/// -(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 @@ bool MCExpr::evaluateAsRelocatableImpl(M
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,
Modified: llvm/trunk/test/MC/X86/imm-comments.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/X86/imm-comments.s?rev=270218&r1=270217&r2=270218&view=diff
==============================================================================
--- llvm/trunk/test/MC/X86/imm-comments.s (original)
+++ llvm/trunk/test/MC/X86/imm-comments.s Fri May 20 09:09:41 2016
@@ -11,8 +11,8 @@ movl $-2147483648, %eax
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 @@ movabsq $9223372036854775807, %rax
# 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
More information about the llvm-commits
mailing list