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 #include "malloc.h"
20 #include "private/bionic_prctl.h"
21 #include "private/libc_logging.h"
22
23 // Send dlmalloc errors to the log.
24 static void __bionic_heap_corruption_error(const char* function);
25 static void __bionic_heap_usage_error(const char* function, void* address);
26 #define PROCEED_ON_ERROR 0
27 #define CORRUPTION_ERROR_ACTION(m) __bionic_heap_corruption_error(__FUNCTION__)
28 #define USAGE_ERROR_ACTION(m,p) __bionic_heap_usage_error(__FUNCTION__, p)
29
30 // Bionic named anonymous memory declarations.
31 static void* named_anonymous_mmap(size_t length);
32 #define MMAP(s) named_anonymous_mmap(s)
33 #define DIRECT_MMAP(s) named_anonymous_mmap(s)
34
35 // Ugly inclusion of C file so that bionic specific #defines configure dlmalloc.
36 #include "../upstream-dlmalloc/malloc.c"
37
__bionic_heap_corruption_error(const char * function)38 static void __bionic_heap_corruption_error(const char* function) {
39 __libc_fatal("heap corruption detected by %s", function);
40 }
41
__bionic_heap_usage_error(const char * function,void * address)42 static void __bionic_heap_usage_error(const char* function, void* address) {
43 __libc_fatal_no_abort("invalid address or address of corrupt block %p passed to %s",
44 address, function);
45 // So that debuggerd gives us a memory dump around the specific address.
46 // TODO: improve the debuggerd protocol so we can tell it to dump an address when we abort.
47 *((int**) 0xdeadbaad) = (int*) address;
48 }
49
named_anonymous_mmap(size_t length)50 static void* named_anonymous_mmap(size_t length) {
51 void* map = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
52 if (map == MAP_FAILED) {
53 return map;
54 }
55 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map, length, "libc_malloc");
56 return map;
57 }
58
59 // Since dlmalloc isn't the default, we'll leave this unimplemented for now. If
60 // we decide we need it later, we can fill it in.
__mallinfo_narenas()61 size_t __mallinfo_narenas() {
62 return 0;
63 }
64
__mallinfo_nbins()65 size_t __mallinfo_nbins() {
66 return 0;
67 }
68
__mallinfo_arena_info(size_t aidx __unused)69 struct mallinfo __mallinfo_arena_info(size_t aidx __unused) {
70 struct mallinfo mi;
71 memset(&mi, 0, sizeof(mi));
72 return mi;
73 }
74
__mallinfo_bin_info(size_t aidx __unused,size_t bidx __unused)75 struct mallinfo __mallinfo_bin_info(size_t aidx __unused, size_t bidx __unused) {
76 struct mallinfo mi;
77 memset(&mi, 0, sizeof(mi));
78 return mi;
79 }
80