[InstCombine] Avoid use after free in DenseMap, when built with GCC
commitd5972724450e37c578c91f40427b63e2468cdff3
authorMartin Storsjo <martin@martin.st>
Thu, 30 May 2019 20:53:21 +0000 (30 20:53 +0000)
committerMartin Storsjo <martin@martin.st>
Thu, 30 May 2019 20:53:21 +0000 (30 20:53 +0000)
treedad05c15c35acd0d61d94859c61c1c144d688862
parentf9fcbf5fb192c05d337f90cf383cdcedaa3aba0a
[InstCombine] Avoid use after free in DenseMap, when built with GCC

Previously, this used a statement like this:
    Map[A] = Map[B];

This is equivalent to the following:
    const auto &Src = Map[B];
    auto &Dest = Map[A];
    Dest = Src;

The second statement, "auto &Dest = Map[A];" can insert a new
element into the DenseMap, which can potentially grow and reallocate
the DenseMap's internal storage, which will invalidate the existing
reference to the source. When doing the actual assignment,
the Src reference is dereferenced, accessing memory that was
freed when the DenseMap grew.

This issue hasn't shown up when LLVM was built with Clang, because
the right hand side ended up dereferenced before evaulating the
left hand side. (If the value type is a larger data type, Clang doesn't
do this but behaves like GCC.)

With GCC, a cast to Value* isn't enough to make it dereference the
right hand side reference before invoking operator[] (while that is
enough to make Clang/LLVM do the right thing for larger types), but
storing it in an intermediate variable in a separate statement works.

This fixes PR42065.

Differential Revision: https://reviews.llvm.org/D62624

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362150 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/InstCombine/InstCombineCompares.cpp