1 /**
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif /* __cplusplus */
20 #define MAX_ENTRIES               (32 * 1024)
21 #define INITIAL_VAL               0xBE
22 
23 #define ENABLE_NONE               0x00
24 #define ENABLE_MEMALIGN_CHECK     0x01
25 #define ENABLE_MALLOC_CHECK       0x02
26 #define ENABLE_CALLOC_CHECK       0x04
27 #define ENABLE_ALL                ENABLE_MEMALIGN_CHECK | ENABLE_MALLOC_CHECK |\
28     ENABLE_CALLOC_CHECK
29 
30 typedef struct {
31     void *mem_ptr;
32     size_t mem_size;
33 } allocated_memory_struct;
34 
35 static bool s_memutils_initialized = false;
36 static int s_allocation_index = 0;
37 static allocated_memory_struct s_allocation_list[MAX_ENTRIES] = { { 0, 0 } };
38 
39 extern bool is_tracking_required(size_t size);
40 static void* (*real_memalign)(size_t, size_t) = NULL;
41 static void* (*real_malloc)(size_t) = NULL;
42 static void (*real_free)(void *) = NULL;
43 
44 #ifdef ENABLE_SELECTIVE_OVERLOADING
45 extern char enable_selective_overload;
46 #endif /* ENABLE_SELECTIVE_OVERLOADING */
47 
48 #ifdef CHECK_MEMORY_LEAK
49 static void* (*real_calloc)(size_t, size_t) = NULL;
50 void exit_vulnerable_if_memory_leak_detected(void);
51 #endif
52 
53 #ifdef CHECK_UNINITIALIZED_MEMORY
54 extern bool is_memory_uninitialized();
55 #endif
56 
57 #ifdef __cplusplus
58 }
59 #endif /* __cplusplus */
60