1// Test that initially callocked memory is properly freed
2// (see https://github.com/google/sanitizers/issues/626).
3//
4// RUN: %clang %s -o %t
5// RUN: env LD_PRELOAD=%shared_libasan %run %t
6//
7// REQUIRES: asan-dynamic-runtime
8//
9// This way of setting LD_PRELOAD does not work with Android test runner.
10// REQUIRES: not-android
11
12#include <stdio.h>
13#include <stdlib.h>
14
15static void *ptr;
16
17// This constructor will run before __asan_init
18// so calloc will allocate memory from special pool.
19static void init() {
20  ptr = calloc(10, 1);
21}
22
23__attribute__((section(".preinit_array"), used))
24void *dummy = init;
25
26void free_memory() {
27  // This used to abort because
28  // Asan's free didn't recognize ptr.
29  free(ptr);
30}
31
32int main() {
33  free_memory();
34  return 0;
35}
36
37