1 /** 2 * Copyright (C) 2019 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 (1024 * 1024) 21 #define INITIAL_VAL (0xBE) 22 23 #define DISABLE_MEM_ACCESS(mem, size)\ 24 mprotect((char *) mem, size, PROT_NONE); 25 26 #define ENABLE_MEM_ACCESS(mem, size)\ 27 mprotect((char *) mem, size, PROT_READ | PROT_WRITE); 28 29 #define ENABLE_NONE 0x00 30 #define ENABLE_MEMALIGN_CHECK 0x01 31 #define ENABLE_MALLOC_CHECK 0x02 32 #define ENABLE_CALLOC_CHECK 0x04 33 #define ENABLE_REALLOC_CHECK 0x08 34 #define ENABLE_FREE_CHECK 0x10 35 #define ENABLE_ALL ENABLE_MEMALIGN_CHECK | ENABLE_MALLOC_CHECK |\ 36 ENABLE_CALLOC_CHECK | ENABLE_REALLOC_CHECK | ENABLE_FREE_CHECK 37 38 typedef struct _map_struct_t { 39 void *start_ptr; 40 void *mem_ptr; 41 int num_pages; 42 size_t mem_size; 43 } map_struct_t; 44 45 static void* (*real_memalign)(size_t, size_t) = NULL; 46 static void* (*real_calloc)(size_t, size_t) = NULL; 47 static void* (*real_malloc)(size_t) = NULL; 48 static void* (*real_realloc)(void *ptr, size_t size) = NULL; 49 static void (*real_free)(void *) = NULL; 50 static int s_memutils_initialized = 0; 51 static int s_mem_map_index = 0; 52 static struct sigaction new_sa, old_sa; 53 #ifdef ENABLE_SELECTIVE_OVERLOADING 54 extern char enable_selective_overload; 55 #endif /* ENABLE_SELECTIVE_OVERLOADING */ 56 #ifdef CHECK_USE_AFTER_FREE_WITH_WINDOW_SIZE 57 static int s_free_write_index = 0; 58 static int s_free_read_index = 0; 59 static int s_free_list_size = 0; 60 map_struct_t s_free_list[MAX_ENTRIES]; 61 #endif /* CHECK_USE_AFTER_FREE_WITH_WINDOW_SIZE */ 62 map_struct_t s_mem_map[MAX_ENTRIES]; 63 #if (!(defined CHECK_OVERFLOW) && !(defined CHECK_UNDERFLOW)) 64 #error "CHECK MACROS NOT DEFINED" 65 #endif 66 #ifdef __cplusplus 67 } 68 #endif /* __cplusplus */ 69