The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3 | * |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 7 | * |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 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. |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 15 | */ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 17 | #include "dlmalloc.h" |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 19 | #include "malloc.h" |
Dmitriy Ivanov | 51a22a1 | 2014-08-08 16:57:15 -0700 | [diff] [blame] | 20 | #include "private/bionic_prctl.h" |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 21 | #include "private/libc_logging.h" |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame] | 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); |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | #define PROCEED_ON_ERROR 0 |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame] | 27 | #define CORRUPTION_ERROR_ACTION(m) __bionic_heap_corruption_error(__FUNCTION__) |
| 28 | #define USAGE_ERROR_ACTION(m,p) __bionic_heap_usage_error(__FUNCTION__, p) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | d745386 | 2014-07-17 14:26:33 -0700 | [diff] [blame] | 30 | // Bionic named anonymous memory declarations. |
Colin Cross | 7f4074d | 2013-08-07 15:01:55 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame] | 35 | // Ugly inclusion of C file so that bionic specific #defines configure dlmalloc. |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 36 | #include "../upstream-dlmalloc/malloc.c" |
Brian Carlstrom | f72ee26 | 2012-08-22 12:07:33 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame] | 38 | static void __bionic_heap_corruption_error(const char* function) { |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 39 | __libc_fatal("heap corruption detected by %s", function); |
Ben Cheng | c84ff11 | 2012-05-24 16:56:53 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame] | 42 | static void __bionic_heap_usage_error(const char* function, void* address) { |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 43 | __libc_fatal_no_abort("invalid address or address of corrupt block %p passed to %s", |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 44 | address, function); |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 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. |
Elliott Hughes | ce4cf90 | 2013-01-22 14:00:09 -0800 | [diff] [blame] | 47 | *((int**) 0xdeadbaad) = (int*) address; |
David 'Digit' Turner | 7708a89 | 2011-06-30 18:32:03 +0200 | [diff] [blame] | 48 | } |
Colin Cross | 7f4074d | 2013-08-07 15:01:55 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 9b5235d | 2014-06-03 18:47:17 -0700 | [diff] [blame] | 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 | } |
Elliott Hughes | 2f9c6e3 | 2014-07-17 15:09:17 -0700 | [diff] [blame] | 55 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map, length, "libc_malloc"); |
Elliott Hughes | 9b5235d | 2014-06-03 18:47:17 -0700 | [diff] [blame] | 56 | return map; |
Colin Cross | 7f4074d | 2013-08-07 15:01:55 -0700 | [diff] [blame] | 57 | } |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 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. |
| 61 | size_t __mallinfo_narenas() { |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | size_t __mallinfo_nbins() { |
| 66 | return 0; |
| 67 | } |
| 68 | |
Dan Albert | ef619cc | 2014-08-22 11:05:48 -0700 | [diff] [blame] | 69 | struct mallinfo __mallinfo_arena_info(size_t aidx __unused) { |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 70 | struct mallinfo mi; |
| 71 | memset(&mi, 0, sizeof(mi)); |
| 72 | return mi; |
| 73 | } |
| 74 | |
Dan Albert | ef619cc | 2014-08-22 11:05:48 -0700 | [diff] [blame] | 75 | struct mallinfo __mallinfo_bin_info(size_t aidx __unused, size_t bidx __unused) { |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 76 | struct mallinfo mi; |
| 77 | memset(&mi, 0, sizeof(mi)); |
| 78 | return mi; |
| 79 | } |