blob: 6287549fe695539352f3edbc4cadda4ae63aa5cb [file] [log] [blame]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001/*
Ian Rogers2c344d32012-08-28 15:53:10 -07002 * Copyright (C) 2012 The Android Open Source Project
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07003 *
Ian Rogers2c344d32012-08-28 15:53:10 -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 Rogers2c344d32012-08-28 15:53:10 -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 Rogers2c344d32012-08-28 15:53:10 -070017#include "dlmalloc.h"
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070018
Ian Rogers2c344d32012-08-28 15:53:10 -070019/* Bionic error handling declarations */
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070020#define PROCEED_ON_ERROR 0
Ian Rogers2c344d32012-08-28 15:53:10 -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
Ian Rogers89210602013-01-11 15:25:44 -080027/* Bionic named anonymous memory declarations */
28static void* named_anonymous_mmap(size_t length);
29#define MMAP(s) named_anonymous_mmap(s)
30
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070031/*
Ian Rogers2c344d32012-08-28 15:53:10 -070032 * Ugly inclusion of C file so that bionic specific #defines configure
33 * dlmalloc.
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070034 */
Ian Rogers2c344d32012-08-28 15:53:10 -070035#include "../upstream-dlmalloc/malloc.c"
Brian Carlstromf72ee262012-08-22 12:07:33 -070036
Brian Carlstromf72ee262012-08-22 12:07:33 -070037
Ian Rogers2c344d32012-08-28 15:53:10 -070038/* Bionic error handling definitions */
Ben Chengc84ff112012-05-24 16:56:53 -070039/* Convert a pointer into hex string */
40static 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 Rogers2c344d32012-08-28 15:53:10 -070054#include <private/logd.h>
Ben Chengc84ff112012-05-24 16:56:53 -070055static void __bionic_heap_error(const char* msg, const char* function, void* p)
David 'Digit' Turner7708a892011-06-30 18:32:03 +020056{
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 Rogers2c344d32012-08-28 15:53:10 -070062 strlcpy(buffer, "@@@ ABORTING: LIBC: ", sizeof(buffer));
David 'Digit' Turner7708a892011-06-30 18:32:03 +020063 strlcat(buffer, msg, sizeof(buffer));
64 if (function != NULL) {
65 strlcat(buffer, " IN ", sizeof(buffer));
66 strlcat(buffer, function, sizeof(buffer));
67 }
Ben Chengc84ff112012-05-24 16:56:53 -070068
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 Rogers2c344d32012-08-28 15:53:10 -070076 __libc_android_log_write(ANDROID_LOG_FATAL, "libc", buffer);
Ben Chengec2ab732012-06-19 07:11:38 -070077
78 /* So that we can get a memory dump around p */
79 *((int **) 0xdeadbaad) = (int *) p;
David 'Digit' Turner7708a892011-06-30 18:32:03 +020080}
Ian Rogers89210602013-01-11 15:25:44 -080081
82/* Bionic named anonymous memory definitions */
83#include <linux/ashmem.h>
84static 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
108static 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}