#include "dlmalloc.h"
-/* Bionic error handling declarations */
-#define PROCEED_ON_ERROR 0
-static void __bionic_heap_error(const char* msg, const char* function, void* p);
-#define CORRUPTION_ERROR_ACTION(m) \
- __bionic_heap_error("HEAP MEMORY CORRUPTION", __FUNCTION__, NULL)
-#define USAGE_ERROR_ACTION(m,p) \
- __bionic_heap_error("ARGUMENT IS INVALID HEAP ADDRESS", __FUNCTION__, p)
-
-/* Bionic named anonymous memory declarations */
-static void* named_anonymous_mmap(size_t length);
-#define MMAP(s) named_anonymous_mmap(s)
-
-/*
- * Ugly inclusion of C file so that bionic specific #defines configure
- * dlmalloc.
- */
-#include "../upstream-dlmalloc/malloc.c"
-
-
-/* Bionic error handling definitions */
-/* Convert a pointer into hex string */
-static void __bionic_itox(char* hex, void* ptr)
-{
- intptr_t val = (intptr_t) ptr;
- /* Terminate with NULL */
- hex[8] = 0;
- int i;
+#include <fcntl.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
- for (i = 7; i >= 0; i--) {
- int digit = val & 15;
- hex[i] = (digit <= 9) ? digit + '0' : digit - 10 + 'a';
- val >>= 4;
- }
-}
+#include <linux/ashmem.h>
+#include <private/debug_format.h>
#include <private/logd.h>
-static void __bionic_heap_error(const char* msg, const char* function, void* p)
-{
- /* We format the buffer explicitely, i.e. without using snprintf()
- * which may use malloc() internally. Not something we can trust
- * if we just detected a corrupted heap.
- */
- char buffer[256];
- strlcpy(buffer, "@@@ ABORTING: LIBC: ", sizeof(buffer));
- strlcat(buffer, msg, sizeof(buffer));
- if (function != NULL) {
- strlcat(buffer, " IN ", sizeof(buffer));
- strlcat(buffer, function, sizeof(buffer));
- }
- if (p != NULL) {
- char hexbuffer[9];
- __bionic_itox(hexbuffer, p);
- strlcat(buffer, " addr=0x", sizeof(buffer));
- strlcat(buffer, hexbuffer, sizeof(buffer));
- }
+// Send dlmalloc errors to the log.
+static void __bionic_heap_corruption_error(const char* function);
+static void __bionic_heap_usage_error(const char* function, void* address);
+#define PROCEED_ON_ERROR 0
+#define CORRUPTION_ERROR_ACTION(m) __bionic_heap_corruption_error(__FUNCTION__)
+#define USAGE_ERROR_ACTION(m,p) __bionic_heap_usage_error(__FUNCTION__, p)
- __libc_android_log_write(ANDROID_LOG_FATAL, "libc", buffer);
+// We use ashmem to name the anonymous private regions created by dlmalloc.
+static void* __bionic_named_anonymous_mmap(size_t length);
+#define MMAP(s) __bionic_named_anonymous_mmap(s)
- /* So that we can get a memory dump around p */
- *((int **) 0xdeadbaad) = (int *) p;
+// Ugly inclusion of C file so that bionic specific #defines configure dlmalloc.
+#include "../upstream-dlmalloc/malloc.c"
+
+static void __bionic_heap_corruption_error(const char* function) {
+ __libc_format_log(ANDROID_LOG_FATAL, "libc", "@@@ ABORTING: heap corruption detected by %s",
+ function);
+ abort();
}
-/* Bionic named anonymous memory definitions */
-#include <linux/ashmem.h>
-static int __ashmem_create_region(const char* name, size_t size)
-{
- int fd, ret;
- fd = open("/dev/ashmem", O_RDWR);
- if (fd < 0)
- return fd;
- if (name != NULL) {
- char buf[ASHMEM_NAME_LEN];
+static void __bionic_heap_usage_error(const char* function, void* address) {
+ __libc_format_log(ANDROID_LOG_FATAL, "libc", "@@@ ABORTING: invalid address %p passed to %s",
+ address, function);
+ // So that we can get a memory dump around the specific address.
+ *((int**) 0xdeadbaad) = (int*) address;
+}
- strlcpy(buf, name, sizeof(buf));
- ret = ioctl(fd, ASHMEM_SET_NAME, buf);
- if (ret < 0) { /* error */
- close(fd);
- return ret;
- }
- }
- ret = ioctl(fd, ASHMEM_SET_SIZE, size);
- if (ret < 0) { /* error */
- close(fd);
- return ret;
- }
+static int __ashmem_create_region(const char* name, size_t size) {
+ int fd = open("/dev/ashmem", O_RDWR);
+ if (fd == -1) {
return fd;
+ }
+ int rc = ioctl(fd, ASHMEM_SET_NAME, name);
+ if (rc < 0) {
+ close(fd);
+ return rc;
+ }
+ rc = ioctl(fd, ASHMEM_SET_SIZE, size);
+ if (rc < 0) {
+ close(fd);
+ return rc;
+ }
+ return fd;
}
-static void* named_anonymous_mmap(size_t length)
-{
- void* ret;
- int fd = __ashmem_create_region("libc malloc", length);
- if (fd < 0)
- return MAP_FAILED;
- ret = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
- close (fd);
- return ret;
+static void* __bionic_named_anonymous_mmap(size_t length) {
+ int fd = __ashmem_create_region("libc malloc", length);
+ if (fd < 0) {
+ return MAP_FAILED;
+ }
+ void* result = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
+ close (fd);
+ return result;
}