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