1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifdef HOOK_ENABLE
17 #include "musl_preinit_common.h"
18 #include "musl_malloc.h"
19 #include <stdatomic.h>
20 #include <malloc.h>
21 #include <stdlib.h>
22 
23 #ifdef USE_GWP_ASAN
24 extern void* libc_gwp_asan_malloc(size_t bytes);
25 extern void libc_gwp_asan_free(void *mem);
26 extern size_t libc_gwp_asan_malloc_usable_size(void *mem);
27 extern void* libc_gwp_asan_realloc(void *ptr, size_t size);
28 extern void* libc_gwp_asan_calloc(size_t nmemb, size_t size);
29 #endif
30 
31 struct musl_libc_globals __musl_libc_globals;
32 #ifdef USE_GWP_ASAN
33 struct MallocDispatchType __libc_malloc_default_dispatch = {
34 	.malloc = libc_gwp_asan_malloc,
35 	.free = libc_gwp_asan_free,
36 	.mmap = MuslMalloc(mmap),
37 	.munmap = MuslMalloc(munmap),
38 	.calloc = libc_gwp_asan_calloc,
39 	.realloc = libc_gwp_asan_realloc,
40 	.malloc_usable_size = libc_gwp_asan_malloc_usable_size,
41 	.prctl = MuslMalloc(prctl),
42 	.aligned_alloc = MuslMalloc(aligned_alloc),
43 };
44 #else
45 struct MallocDispatchType __libc_malloc_default_dispatch = {
46 	.malloc = MuslFunc(malloc),
47 	.free = MuslFunc(free),
48 	.mmap = MuslMalloc(mmap),
49 	.munmap = MuslMalloc(munmap),
50 	.calloc = MuslFunc(calloc),
51 	.realloc = MuslFunc(realloc),
52 	.malloc_usable_size = MuslMalloc(malloc_usable_size),
53 	.prctl = MuslMalloc(prctl),
54 	.aligned_alloc = MuslMalloc(aligned_alloc),
55 };
56 #endif
57 
58 volatile atomic_bool __hook_enable_hook_flag;
59 volatile atomic_bool __memleak_hook_flag;
60 
61 #endif
62