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 | |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 19 | /* Bionic error handling declarations */ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | #define PROCEED_ON_ERROR 0 |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 21 | static void __bionic_heap_error(const char* msg, const char* function, void* p); |
| 22 | #define CORRUPTION_ERROR_ACTION(m) \ |
| 23 | __bionic_heap_error("HEAP MEMORY CORRUPTION", __FUNCTION__, NULL) |
| 24 | #define USAGE_ERROR_ACTION(m,p) \ |
| 25 | __bionic_heap_error("ARGUMENT IS INVALID HEAP ADDRESS", __FUNCTION__, p) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | |
Ian Rogers | 8921060 | 2013-01-11 15:25:44 -0800 | [diff] [blame^] | 27 | /* Bionic named anonymous memory declarations */ |
| 28 | static void* named_anonymous_mmap(size_t length); |
| 29 | #define MMAP(s) named_anonymous_mmap(s) |
| 30 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | /* |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 32 | * Ugly inclusion of C file so that bionic specific #defines configure |
| 33 | * dlmalloc. |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | */ |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 35 | #include "../upstream-dlmalloc/malloc.c" |
Brian Carlstrom | f72ee26 | 2012-08-22 12:07:33 -0700 | [diff] [blame] | 36 | |
Brian Carlstrom | f72ee26 | 2012-08-22 12:07:33 -0700 | [diff] [blame] | 37 | |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 38 | /* Bionic error handling definitions */ |
Ben Cheng | c84ff11 | 2012-05-24 16:56:53 -0700 | [diff] [blame] | 39 | /* Convert a pointer into hex string */ |
| 40 | static void __bionic_itox(char* hex, void* ptr) |
| 41 | { |
| 42 | intptr_t val = (intptr_t) ptr; |
| 43 | /* Terminate with NULL */ |
| 44 | hex[8] = 0; |
| 45 | int i; |
| 46 | |
| 47 | for (i = 7; i >= 0; i--) { |
| 48 | int digit = val & 15; |
| 49 | hex[i] = (digit <= 9) ? digit + '0' : digit - 10 + 'a'; |
| 50 | val >>= 4; |
| 51 | } |
| 52 | } |
| 53 | |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 54 | #include <private/logd.h> |
Ben Cheng | c84ff11 | 2012-05-24 16:56:53 -0700 | [diff] [blame] | 55 | static void __bionic_heap_error(const char* msg, const char* function, void* p) |
David 'Digit' Turner | 7708a89 | 2011-06-30 18:32:03 +0200 | [diff] [blame] | 56 | { |
| 57 | /* We format the buffer explicitely, i.e. without using snprintf() |
| 58 | * which may use malloc() internally. Not something we can trust |
| 59 | * if we just detected a corrupted heap. |
| 60 | */ |
| 61 | char buffer[256]; |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 62 | strlcpy(buffer, "@@@ ABORTING: LIBC: ", sizeof(buffer)); |
David 'Digit' Turner | 7708a89 | 2011-06-30 18:32:03 +0200 | [diff] [blame] | 63 | strlcat(buffer, msg, sizeof(buffer)); |
| 64 | if (function != NULL) { |
| 65 | strlcat(buffer, " IN ", sizeof(buffer)); |
| 66 | strlcat(buffer, function, sizeof(buffer)); |
| 67 | } |
Ben Cheng | c84ff11 | 2012-05-24 16:56:53 -0700 | [diff] [blame] | 68 | |
| 69 | if (p != NULL) { |
| 70 | char hexbuffer[9]; |
| 71 | __bionic_itox(hexbuffer, p); |
| 72 | strlcat(buffer, " addr=0x", sizeof(buffer)); |
| 73 | strlcat(buffer, hexbuffer, sizeof(buffer)); |
| 74 | } |
| 75 | |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 76 | __libc_android_log_write(ANDROID_LOG_FATAL, "libc", buffer); |
Ben Cheng | ec2ab73 | 2012-06-19 07:11:38 -0700 | [diff] [blame] | 77 | |
| 78 | /* So that we can get a memory dump around p */ |
| 79 | *((int **) 0xdeadbaad) = (int *) p; |
David 'Digit' Turner | 7708a89 | 2011-06-30 18:32:03 +0200 | [diff] [blame] | 80 | } |
Ian Rogers | 8921060 | 2013-01-11 15:25:44 -0800 | [diff] [blame^] | 81 | |
| 82 | /* Bionic named anonymous memory definitions */ |
| 83 | #include <linux/ashmem.h> |
| 84 | static int __ashmem_create_region(const char* name, size_t size) |
| 85 | { |
| 86 | int fd, ret; |
| 87 | fd = open("/dev/ashmem", O_RDWR); |
| 88 | if (fd < 0) |
| 89 | return fd; |
| 90 | if (name != NULL) { |
| 91 | char buf[ASHMEM_NAME_LEN]; |
| 92 | |
| 93 | strlcpy(buf, name, sizeof(buf)); |
| 94 | ret = ioctl(fd, ASHMEM_SET_NAME, buf); |
| 95 | if (ret < 0) { /* error */ |
| 96 | close(fd); |
| 97 | return ret; |
| 98 | } |
| 99 | } |
| 100 | ret = ioctl(fd, ASHMEM_SET_SIZE, size); |
| 101 | if (ret < 0) { /* error */ |
| 102 | close(fd); |
| 103 | return ret; |
| 104 | } |
| 105 | return fd; |
| 106 | } |
| 107 | |
| 108 | static void* named_anonymous_mmap(size_t length) |
| 109 | { |
| 110 | void* ret; |
| 111 | int fd = __ashmem_create_region("libc malloc", length); |
| 112 | if (fd < 0) |
| 113 | return MAP_FAILED; |
| 114 | ret = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 115 | close (fd); |
| 116 | return ret; |
| 117 | } |