The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -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 | 9990891 | 2012-08-17 17:28:15 -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 | 9990891 | 2012-08-17 17:28:15 -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 | 9990891 | 2012-08-17 17:28:15 -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 | 9990891 | 2012-08-17 17:28:15 -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 | 9990891 | 2012-08-17 17:28:15 -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 | |
| 27 | /* |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 28 | * Ugly inclusion of C file so that bionic specific #defines configure |
| 29 | * dlmalloc. |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | */ |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 31 | #include "../upstream-dlmalloc/malloc.c" |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 34 | /* Bionic error handling definitions */ |
Ben Cheng | c84ff11 | 2012-05-24 16:56:53 -0700 | [diff] [blame] | 35 | /* Convert a pointer into hex string */ |
| 36 | static void __bionic_itox(char* hex, void* ptr) |
| 37 | { |
| 38 | intptr_t val = (intptr_t) ptr; |
| 39 | /* Terminate with NULL */ |
| 40 | hex[8] = 0; |
| 41 | int i; |
| 42 | |
| 43 | for (i = 7; i >= 0; i--) { |
| 44 | int digit = val & 15; |
| 45 | hex[i] = (digit <= 9) ? digit + '0' : digit - 10 + 'a'; |
| 46 | val >>= 4; |
| 47 | } |
| 48 | } |
| 49 | |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 50 | #include <private/logd.h> |
Ben Cheng | c84ff11 | 2012-05-24 16:56:53 -0700 | [diff] [blame] | 51 | 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] | 52 | { |
| 53 | /* We format the buffer explicitely, i.e. without using snprintf() |
| 54 | * which may use malloc() internally. Not something we can trust |
| 55 | * if we just detected a corrupted heap. |
| 56 | */ |
| 57 | char buffer[256]; |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 58 | strlcpy(buffer, "@@@ ABORTING: LIBC: ", sizeof(buffer)); |
David 'Digit' Turner | 7708a89 | 2011-06-30 18:32:03 +0200 | [diff] [blame] | 59 | strlcat(buffer, msg, sizeof(buffer)); |
| 60 | if (function != NULL) { |
| 61 | strlcat(buffer, " IN ", sizeof(buffer)); |
| 62 | strlcat(buffer, function, sizeof(buffer)); |
| 63 | } |
Ben Cheng | c84ff11 | 2012-05-24 16:56:53 -0700 | [diff] [blame] | 64 | |
| 65 | if (p != NULL) { |
| 66 | char hexbuffer[9]; |
| 67 | __bionic_itox(hexbuffer, p); |
| 68 | strlcat(buffer, " addr=0x", sizeof(buffer)); |
| 69 | strlcat(buffer, hexbuffer, sizeof(buffer)); |
| 70 | } |
| 71 | |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 72 | __libc_android_log_write(ANDROID_LOG_FATAL, "libc", buffer); |
Ben Cheng | ec2ab73 | 2012-06-19 07:11:38 -0700 | [diff] [blame] | 73 | |
| 74 | /* So that we can get a memory dump around p */ |
| 75 | *((int **) 0xdeadbaad) = (int *) p; |
David 'Digit' Turner | 7708a89 | 2011-06-30 18:32:03 +0200 | [diff] [blame] | 76 | } |