blob: 2d67392c098f8299f007a04d5fe9d28c21ba1665 [file] [log] [blame]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001/*
Ian Rogers2c344d32012-08-28 15:53:10 -07002 * Copyright (C) 2012 The Android Open Source Project
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07003 *
Ian Rogers2c344d32012-08-28 15:53:10 -07004 * 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
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07007 *
Ian Rogers2c344d32012-08-28 15:53:10 -07008 * 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.
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070015 */
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070016
Ian Rogers2c344d32012-08-28 15:53:10 -070017#include "dlmalloc.h"
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070018
Colin Cross7f4074d2013-08-07 15:01:55 -070019#include "private/bionic_name_mem.h"
Elliott Hughes0d787c12013-04-04 13:46:46 -070020#include "private/libc_logging.h"
Elliott Hughesce4cf902013-01-22 14:00:09 -080021
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);
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070025#define PROCEED_ON_ERROR 0
Elliott Hughesce4cf902013-01-22 14:00:09 -080026#define CORRUPTION_ERROR_ACTION(m) __bionic_heap_corruption_error(__FUNCTION__)
27#define USAGE_ERROR_ACTION(m,p) __bionic_heap_usage_error(__FUNCTION__, p)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070028
Colin Cross7f4074d2013-08-07 15:01:55 -070029/* 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
Elliott Hughesce4cf902013-01-22 14:00:09 -080034// Ugly inclusion of C file so that bionic specific #defines configure dlmalloc.
Ian Rogers2c344d32012-08-28 15:53:10 -070035#include "../upstream-dlmalloc/malloc.c"
Brian Carlstromf72ee262012-08-22 12:07:33 -070036
Elliott Hughesce4cf902013-01-22 14:00:09 -080037static void __bionic_heap_corruption_error(const char* function) {
Elliott Hughes61e699a2013-06-12 14:05:46 -070038 __libc_fatal("heap corruption detected by %s", function);
Ben Chengc84ff112012-05-24 16:56:53 -070039}
40
Elliott Hughesce4cf902013-01-22 14:00:09 -080041static void __bionic_heap_usage_error(const char* function, void* address) {
Elliott Hughes61e699a2013-06-12 14:05:46 -070042 __libc_fatal_no_abort("invalid address or address of corrupt block %p passed to %s",
Elliott Hughes0d787c12013-04-04 13:46:46 -070043 address, function);
Elliott Hughes61e699a2013-06-12 14:05:46 -070044 // So that debuggerd gives us a memory dump around the specific address.
45 // TODO: improve the debuggerd protocol so we can tell it to dump an address when we abort.
Elliott Hughesce4cf902013-01-22 14:00:09 -080046 *((int**) 0xdeadbaad) = (int*) address;
David 'Digit' Turner7708a892011-06-30 18:32:03 +020047}
Colin Cross7f4074d2013-08-07 15:01:55 -070048
Elliott Hughes9b5235d2014-06-03 18:47:17 -070049static void* named_anonymous_mmap(size_t length) {
50 void* map = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
51 if (map == MAP_FAILED) {
52 return map;
53 }
54 __bionic_name_mem(map, length, "libc_malloc");
55 return map;
Colin Cross7f4074d2013-08-07 15:01:55 -070056}