[PATCH] A new HeapToStack allocation promotion pass

Stephen Checkoway s at pahtak.org
Tue Sep 24 13:04:14 PDT 2013

On Sep 23, 2013, at 5:00 PM, hfinkel at anl.gov wrote:

> This adds a new optimization pass that can 'promote' malloc'd memory to stack-allocated memory when the lifetime of the allocation can be determined to be bounded by the execution of the function.

Is this transformation safe in general? Various libc implementations have functions for getting information about the allocated chunk. For example, malloc_size(3) on OS X, malloc_usable_size(3) in glibc. This patch seems to break valid code:

[secdev:~/m] s$ uname -a
Linux secdev 3.8.0-30-generic #44-Ubuntu SMP Thu Aug 22 20:52:24 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[secdev:~/m] s$ cat a.c
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>

int main()
  void *p = malloc(10);
  size_t s = malloc_usable_size(p);
  printf("%zu\n", s);
[secdev:~/m] s$ ../build-master/bin/clang -Wall a.c
[secdev:~/m] s$ ./a.out
[secdev:~/m] s$ ../build-master/bin/clang -Wall a.c -O1
[secdev:~/m] s$ ./a.out
Segmentation fault (core dumped)

Stephen Checkoway

More information about the llvm-commits mailing list