blob: 7d4ef0f71c66e0bcac6eac683a53ba121494af4e [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
Elliott Hughesce4cf902013-01-22 14:00:09 -080019#include <fcntl.h>
20#include <stdlib.h>
21#include <sys/mman.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
25#include <linux/ashmem.h>
26
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070027#include <private/libc_logging.h>
Elliott Hughesce4cf902013-01-22 14:00:09 -080028
29// Send dlmalloc errors to the log.
30static void __bionic_heap_corruption_error(const char* function);
31static void __bionic_heap_usage_error(const char* function, void* address);
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070032#define PROCEED_ON_ERROR 0
Elliott Hughesce4cf902013-01-22 14:00:09 -080033#define CORRUPTION_ERROR_ACTION(m) __bionic_heap_corruption_error(__FUNCTION__)
34#define USAGE_ERROR_ACTION(m,p) __bionic_heap_usage_error(__FUNCTION__, p)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070035
Elliott Hughesce4cf902013-01-22 14:00:09 -080036// We use ashmem to name the anonymous private regions created by dlmalloc.
37static void* __bionic_named_anonymous_mmap(size_t length);
38#define MMAP(s) __bionic_named_anonymous_mmap(s)
Ian Rogers89210602013-01-11 15:25:44 -080039
Elliott Hughesce4cf902013-01-22 14:00:09 -080040// Ugly inclusion of C file so that bionic specific #defines configure dlmalloc.
Ian Rogers2c344d32012-08-28 15:53:10 -070041#include "../upstream-dlmalloc/malloc.c"
Brian Carlstromf72ee262012-08-22 12:07:33 -070042
Elliott Hughesce4cf902013-01-22 14:00:09 -080043static void __bionic_heap_corruption_error(const char* function) {
44 __libc_format_log(ANDROID_LOG_FATAL, "libc", "@@@ ABORTING: heap corruption detected by %s",
45 function);
46 abort();
Ben Chengc84ff112012-05-24 16:56:53 -070047}
48
Elliott Hughesce4cf902013-01-22 14:00:09 -080049static void __bionic_heap_usage_error(const char* function, void* address) {
50 __libc_format_log(ANDROID_LOG_FATAL, "libc", "@@@ ABORTING: invalid address %p passed to %s",
51 address, function);
52 // So that we can get a memory dump around the specific address.
53 *((int**) 0xdeadbaad) = (int*) address;
David 'Digit' Turner7708a892011-06-30 18:32:03 +020054}
Ian Rogers89210602013-01-11 15:25:44 -080055
Elliott Hughesce4cf902013-01-22 14:00:09 -080056static int __ashmem_create_region(const char* name, size_t size) {
57 int fd = open("/dev/ashmem", O_RDWR);
58 if (fd == -1) {
Ian Rogers89210602013-01-11 15:25:44 -080059 return fd;
Elliott Hughesce4cf902013-01-22 14:00:09 -080060 }
61 int rc = ioctl(fd, ASHMEM_SET_NAME, name);
62 if (rc < 0) {
63 close(fd);
64 return rc;
65 }
66 rc = ioctl(fd, ASHMEM_SET_SIZE, size);
67 if (rc < 0) {
68 close(fd);
69 return rc;
70 }
71 return fd;
Ian Rogers89210602013-01-11 15:25:44 -080072}
73
Elliott Hughesce4cf902013-01-22 14:00:09 -080074static void* __bionic_named_anonymous_mmap(size_t length) {
75 int fd = __ashmem_create_region("libc malloc", length);
76 if (fd < 0) {
77 return MAP_FAILED;
78 }
79 void* result = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
80 close (fd);
81 return result;
Ian Rogers89210602013-01-11 15:25:44 -080082}