dlmalloc.c revision d648f557f116513263aa2f86bba552d2f72660f1
1/* 2 * Copyright (C) 2012 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#include "dlmalloc.h" 18 19#include "private/bionic_name_mem.h" 20#include "private/libc_logging.h" 21 22// Send dlmalloc errors to the log. 23static void __bionic_heap_corruption_error(const char* function); 24static void __bionic_heap_usage_error(const char* function, void* address); 25#define PROCEED_ON_ERROR 0 26#define CORRUPTION_ERROR_ACTION(m) __bionic_heap_corruption_error(__FUNCTION__) 27#define USAGE_ERROR_ACTION(m,p) __bionic_heap_usage_error(__FUNCTION__, p) 28 29// Bionic named anonymous memory declarations. 30static void* named_anonymous_mmap(size_t length); 31#define MMAP(s) named_anonymous_mmap(s) 32#define DIRECT_MMAP(s) named_anonymous_mmap(s) 33 34// Local definitions of custom prctl arguments to set a vma name in Android kernels. 35#include <sys/prctl.h> 36#define PR_SET_VMA 0x53564d41 37#define PR_SET_VMA_ANON_NAME 0 38 39// Ugly inclusion of C file so that bionic specific #defines configure dlmalloc. 40#include "../upstream-dlmalloc/malloc.c" 41 42static void __bionic_heap_corruption_error(const char* function) { 43 __libc_fatal("heap corruption detected by %s", function); 44} 45 46static void __bionic_heap_usage_error(const char* function, void* address) { 47 __libc_fatal_no_abort("invalid address or address of corrupt block %p passed to %s", 48 address, function); 49 // So that debuggerd gives us a memory dump around the specific address. 50 // TODO: improve the debuggerd protocol so we can tell it to dump an address when we abort. 51 *((int**) 0xdeadbaad) = (int*) address; 52} 53 54static void* named_anonymous_mmap(size_t length) { 55 void* map = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 56 if (map == MAP_FAILED) { 57 return map; 58 } 59 prctl(BIONIC_PR_SET_VMA, BIONIC_PR_SET_VMA_ANON_NAME, map, length, "libc_malloc"); 60 return map; 61} 62