[PATCH] D117356: InstructionCombining: avoid eliding mismatched alloc/free pairs

Augie Fackler via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 14 13:18:20 PST 2022


durin42 created this revision.
durin42 added a reviewer: reames.
Herald added a subscriber: hiraditya.
durin42 requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: llvm-commits, sstefan1.
Herald added a project: LLVM.

Prior to this change LLVM would happily elide a call to any allocation
function and a call to any free function operating on the same unused
pointer. This can cause problems in some obscure cases, for example if
the body of operator::new can be inlined but the body of
operator::delete can't, as in this example from jyknight:

  #include <stdlib.h>
  #include <stdio.h>
  
  int allocs = 0;
  
  void *operator new(size_t n) {
      allocs++;
      void *mem = malloc(n);
      if (!mem) abort();
      return mem;
  }
  
  __attribute__((noinline)) void operator delete(void *mem) noexcept {
      allocs--;
      free(mem);
  }
  
  void deleteit(int*i) { delete i; }
  int main() {
      int*i = new int;
      deleteit(i);
      if (allocs != 0)
        printf("MEMORY LEAK! allocs: %d\n", allocs);
  }

This patch addresses the issue by introducing the concept of an
allocator function family and uses it to make sure that alloc/free
function pairs are only removed if they're in the same family.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117356

Files:
  llvm/include/llvm/Analysis/MemoryBuiltins.h
  llvm/lib/Analysis/MemoryBuiltins.cpp
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
  llvm/test/Transforms/InstCombine/malloc-free-mismatched.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117356.400129.patch
Type: text/x-patch
Size: 20683 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220114/c64c1902/attachment.bin>


More information about the llvm-commits mailing list