blob: dc5f8ab020dd87e4ad7baaa0cc220c201d95de7c [file] [log] [blame]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001/*
Ian Rogers99908912012-08-17 17:28:15 -07002 * Copyright (C) 2012 The Android Open Source Project
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07003 *
Ian Rogers99908912012-08-17 17:28:15 -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 Rogers99908912012-08-17 17:28:15 -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 Rogers99908912012-08-17 17:28:15 -070017#include "dlmalloc.h"
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070018
Ian Rogers99908912012-08-17 17:28:15 -070019/* Bionic error handling declarations */
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070020#define PROCEED_ON_ERROR 0
Ian Rogers99908912012-08-17 17:28:15 -070021static 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 Projecta27d2ba2008-10-21 07:00:00 -070026
27/*
Ian Rogers99908912012-08-17 17:28:15 -070028 * Ugly inclusion of C file so that bionic specific #defines configure
29 * dlmalloc.
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070030 */
Ian Rogers99908912012-08-17 17:28:15 -070031#include "../upstream-dlmalloc/malloc.c"
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070032
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070033
Ian Rogers99908912012-08-17 17:28:15 -070034/* Bionic error handling definitions */
Ben Chengc84ff112012-05-24 16:56:53 -070035/* Convert a pointer into hex string */
36static 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 Rogers99908912012-08-17 17:28:15 -070050#include <private/logd.h>
Ben Chengc84ff112012-05-24 16:56:53 -070051static void __bionic_heap_error(const char* msg, const char* function, void* p)
David 'Digit' Turner7708a892011-06-30 18:32:03 +020052{
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 Rogers99908912012-08-17 17:28:15 -070058 strlcpy(buffer, "@@@ ABORTING: LIBC: ", sizeof(buffer));
David 'Digit' Turner7708a892011-06-30 18:32:03 +020059 strlcat(buffer, msg, sizeof(buffer));
60 if (function != NULL) {
61 strlcat(buffer, " IN ", sizeof(buffer));
62 strlcat(buffer, function, sizeof(buffer));
63 }
Ben Chengc84ff112012-05-24 16:56:53 -070064
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 Rogers99908912012-08-17 17:28:15 -070072 __libc_android_log_write(ANDROID_LOG_FATAL, "libc", buffer);
Ben Chengec2ab732012-06-19 07:11:38 -070073
74 /* So that we can get a memory dump around p */
75 *((int **) 0xdeadbaad) = (int *) p;
David 'Digit' Turner7708a892011-06-30 18:32:03 +020076}