The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3 | * |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 7 | * |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 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. |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 15 | */ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 17 | #include "dlmalloc.h" |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame^] | 19 | #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 | |
| 27 | #include <private/debug_format.h> |
| 28 | #include <private/logd.h> |
| 29 | |
| 30 | // Send dlmalloc errors to the log. |
| 31 | static void __bionic_heap_corruption_error(const char* function); |
| 32 | static void __bionic_heap_usage_error(const char* function, void* address); |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | #define PROCEED_ON_ERROR 0 |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame^] | 34 | #define CORRUPTION_ERROR_ACTION(m) __bionic_heap_corruption_error(__FUNCTION__) |
| 35 | #define USAGE_ERROR_ACTION(m,p) __bionic_heap_usage_error(__FUNCTION__, p) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame^] | 37 | // We use ashmem to name the anonymous private regions created by dlmalloc. |
| 38 | static void* __bionic_named_anonymous_mmap(size_t length); |
| 39 | #define MMAP(s) __bionic_named_anonymous_mmap(s) |
Ian Rogers | 8921060 | 2013-01-11 15:25:44 -0800 | [diff] [blame] | 40 | |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame^] | 41 | // Ugly inclusion of C file so that bionic specific #defines configure dlmalloc. |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 42 | #include "../upstream-dlmalloc/malloc.c" |
Brian Carlstrom | f72ee26 | 2012-08-22 12:07:33 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame^] | 44 | static void __bionic_heap_corruption_error(const char* function) { |
| 45 | __libc_format_log(ANDROID_LOG_FATAL, "libc", "@@@ ABORTING: heap corruption detected by %s", |
| 46 | function); |
| 47 | abort(); |
Ben Cheng | c84ff11 | 2012-05-24 16:56:53 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame^] | 50 | static void __bionic_heap_usage_error(const char* function, void* address) { |
| 51 | __libc_format_log(ANDROID_LOG_FATAL, "libc", "@@@ ABORTING: invalid address %p passed to %s", |
| 52 | address, function); |
| 53 | // So that we can get a memory dump around the specific address. |
| 54 | *((int**) 0xdeadbaad) = (int*) address; |
David 'Digit' Turner | 7708a89 | 2011-06-30 18:32:03 +0200 | [diff] [blame] | 55 | } |
Ian Rogers | 8921060 | 2013-01-11 15:25:44 -0800 | [diff] [blame] | 56 | |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame^] | 57 | static int __ashmem_create_region(const char* name, size_t size) { |
| 58 | int fd = open("/dev/ashmem", O_RDWR); |
| 59 | if (fd == -1) { |
Ian Rogers | 8921060 | 2013-01-11 15:25:44 -0800 | [diff] [blame] | 60 | return fd; |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame^] | 61 | } |
| 62 | int rc = ioctl(fd, ASHMEM_SET_NAME, name); |
| 63 | if (rc < 0) { |
| 64 | close(fd); |
| 65 | return rc; |
| 66 | } |
| 67 | rc = ioctl(fd, ASHMEM_SET_SIZE, size); |
| 68 | if (rc < 0) { |
| 69 | close(fd); |
| 70 | return rc; |
| 71 | } |
| 72 | return fd; |
Ian Rogers | 8921060 | 2013-01-11 15:25:44 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame^] | 75 | static void* __bionic_named_anonymous_mmap(size_t length) { |
| 76 | int fd = __ashmem_create_region("libc malloc", length); |
| 77 | if (fd < 0) { |
| 78 | return MAP_FAILED; |
| 79 | } |
| 80 | void* result = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 81 | close (fd); |
| 82 | return result; |
Ian Rogers | 8921060 | 2013-01-11 15:25:44 -0800 | [diff] [blame] | 83 | } |