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.
Differential Revision: https://reviews.llvm.org/D117356
31 lines
879 B
LLVM
31 lines
879 B
LLVM
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
|
|
; RUN: opt < %s -instcombine -S | FileCheck %s
|
|
|
|
define dso_local i32 @_Z6answeri(i32 %0) {
|
|
; CHECK-LABEL: @_Z6answeri(
|
|
; CHECK-NEXT: [[TMP2:%.*]] = call noalias nonnull dereferenceable(80) i8* @_Znam(i64 80) #[[ATTR1:[0-9]+]]
|
|
; CHECK-NEXT: call void @free(i8* [[TMP2]])
|
|
; CHECK-NEXT: ret i32 42
|
|
;
|
|
%2 = call noalias nonnull i8* @_Znam(i64 80) #0
|
|
call void @free(i8* %2)
|
|
ret i32 42
|
|
}
|
|
|
|
; All we care about with this function is that LLVM doesn't crash
|
|
; when optimizing it.
|
|
define void @test_alloca() {
|
|
%1 = alloca i8
|
|
call void @free(i8* %1)
|
|
ret void
|
|
}
|
|
|
|
; Function Attrs: nobuiltin allocsize(0)
|
|
declare dso_local nonnull i8* @_Znam(i64) #1
|
|
|
|
; Function Attrs: nounwind
|
|
declare dso_local void @free(i8*)
|
|
|
|
attributes #0 = { builtin allocsize(0) }
|
|
attributes #1 = { nobuiltin allocsize(0) }
|