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