[llvm-commits] [PATCH] MemCpyOptimizer: fix invalid reads shown by valgrind

Török Edwin edwintorok at gmail.com
Sat May 3 06:24:23 PDT 2008


Hi,

Valgrind has shown that MemCpyOptimizer is accessing freed memory, see
relevant portions below.
The instruction is freed in processStore using eraseFromParent here:
if (StoreInst *SI = dyn_cast<StoreInst>(I))
        changed_function |= processStore(SI, BI);

But dyn_cast<> is accessing the freed instruction here:
    if (MemCpyInst* M = dyn_cast<MemCpyInst>(I)) {

Since StoreInst can never be a MemCpyInst a simple solution is to use
'else'.
Attached patch fixes this, ok to commit?

Invalid read of size 2
==17137==    at 0x7159F2: llvm::Value::getValueID() const (Value.h:212)
[...]
==17137==    by 0x877698: (anonymous
namespace)::MemCpyOpt::iterateOnFunction(llvm::Function&)
(MemCpyOptimizer.cpp:722)
[...]
==17137==  Address 0x57e9780 is 8 bytes inside a block of size 144 free'd
[...]
==17137==    by 0x877557: (anonymous
namespace)::MemCpyOpt::processStore(llvm::StoreInst*,
llvm::ilist_iterator<llvm::Instruction>&) (MemCpyOptimizer.cpp:461)

--Edwin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: memcpy.patch
Type: text/x-diff
Size: 549 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20080503/6fb21f3b/attachment.patch>


More information about the llvm-commits mailing list