This is an archive of the discontinued LLVM Phabricator instance.

[MCExpr] avoid UB via negation of INT_MIN
ClosedPublic

Authored by spatel on May 19 2016, 7:35 AM.

Details

Summary

I accidentally exposed a bug in MCExpr::evaluateAsRelocatableImpl() with the test file added in:
http://reviews.llvm.org/rL269977

I don't know anything about MC. Is this a viable fix?

Note that I see at least one other negation later in this file that seems like it could trigger the same UB:

return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
  RHSValue.getSymB(), RHSValue.getSymA(),
  -RHSValue.getConstant(), Res);

Diff Detail

Repository
rL LLVM

Event Timeline

spatel updated this revision to Diff 57789.May 19 2016, 7:35 AM
spatel retitled this revision from to [MCExpr] avoid UB via negation of INT_MIN.
spatel updated this object.
spatel added a subscriber: llvm-commits.

I've commented out the INT_MIN line in the test to unbreak sanitizer bots:
http://reviews.llvm.org/rL270128

For reference, the intended use of this test is for D20385 .

majnemer added inline comments.
lib/MC/MCExpr.cpp
669–673 ↗(On Diff #57789)

I'd just go with:

Res = MCValue::get(Value.getSymB(), Value.getSymA(),
                   -(uint64_t)Value.getConstant());
spatel updated this revision to Diff 57864.May 19 2016, 3:11 PM

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 rL270128.
majnemer accepted this revision.May 19 2016, 3:25 PM
majnemer added a reviewer: majnemer.

LGTM

This revision is now accepted and ready to land.May 19 2016, 3:25 PM
This revision was automatically updated to reflect the committed changes.