1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
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
7 *
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.
15 */
16
17 #include "dlmalloc.h"
18
19 /* Bionic error handling declarations */
20 #define PROCEED_ON_ERROR 0
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)
26
27 /*
28 * Ugly inclusion of C file so that bionic specific #defines configure
29 * dlmalloc.
30 */
31 #include "../upstream-dlmalloc/malloc.c"
32
33
34 /* Bionic error handling definitions */
35 /* Convert a pointer into hex string */
__bionic_itox(char * hex,void * ptr)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
50 #include <private/logd.h>
__bionic_heap_error(const char * msg,const char * function,void * p)51 static void __bionic_heap_error(const char* msg, const char* function, void* p)
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];
58 strlcpy(buffer, "@@@ ABORTING: LIBC: ", sizeof(buffer));
59 strlcat(buffer, msg, sizeof(buffer));
60 if (function != NULL) {
61 strlcat(buffer, " IN ", sizeof(buffer));
62 strlcat(buffer, function, sizeof(buffer));
63 }
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
72 __libc_android_log_write(ANDROID_LOG_FATAL, "libc", buffer);
73
74 /* So that we can get a memory dump around p */
75 *((int **) 0xdeadbaad) = (int *) p;
76 }
77