Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 <gtest/gtest.h> |
| 18 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 19 | #include <elf.h> |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 20 | #include <limits.h> |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 21 | #include <malloc.h> |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 22 | #include <pthread.h> |
Peter Collingbourne | 5d3aa86 | 2020-09-11 15:05:17 -0700 | [diff] [blame] | 23 | #include <semaphore.h> |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 24 | #include <signal.h> |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 25 | #include <stdint.h> |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 27 | #include <stdlib.h> |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 28 | #include <string.h> |
Peter Collingbourne | 45819dd | 2020-01-09 11:00:43 -0800 | [diff] [blame] | 29 | #include <sys/auxv.h> |
| 30 | #include <sys/prctl.h> |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/wait.h> |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 33 | #include <unistd.h> |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 34 | |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 35 | #include <atomic> |
Christopher Ferris | f32494c | 2020-01-08 14:19:10 -0800 | [diff] [blame] | 36 | #include <thread> |
| 37 | |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 38 | #include <tinyxml2.h> |
| 39 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 40 | #include <android-base/file.h> |
| 41 | |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 42 | #include "utils.h" |
Dan Albert | e5fdaa4 | 2014-06-14 01:04:31 +0000 | [diff] [blame] | 43 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 44 | #if defined(__BIONIC__) |
Christopher Ferris | b874c33 | 2020-01-21 16:39:05 -0800 | [diff] [blame] | 45 | |
Peter Collingbourne | 45819dd | 2020-01-09 11:00:43 -0800 | [diff] [blame] | 46 | #include "SignalUtils.h" |
| 47 | |
Christopher Ferris | b874c33 | 2020-01-21 16:39:05 -0800 | [diff] [blame] | 48 | #include "platform/bionic/malloc.h" |
Peter Collingbourne | 5d3aa86 | 2020-09-11 15:05:17 -0700 | [diff] [blame] | 49 | #include "platform/bionic/mte.h" |
Peter Collingbourne | 45819dd | 2020-01-09 11:00:43 -0800 | [diff] [blame] | 50 | #include "platform/bionic/mte_kernel.h" |
Christopher Ferris | b874c33 | 2020-01-21 16:39:05 -0800 | [diff] [blame] | 51 | #include "platform/bionic/reserved_signals.h" |
| 52 | #include "private/bionic_config.h" |
| 53 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 54 | #define HAVE_REALLOCARRAY 1 |
Christopher Ferris | b874c33 | 2020-01-21 16:39:05 -0800 | [diff] [blame] | 55 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 56 | #else |
Christopher Ferris | b874c33 | 2020-01-21 16:39:05 -0800 | [diff] [blame] | 57 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 58 | #define HAVE_REALLOCARRAY __GLIBC_PREREQ(2, 26) |
Christopher Ferris | b874c33 | 2020-01-21 16:39:05 -0800 | [diff] [blame] | 59 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 62 | TEST(malloc, malloc_std) { |
| 63 | // Simple malloc test. |
| 64 | void *ptr = malloc(100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 66 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 67 | free(ptr); |
| 68 | } |
| 69 | |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 70 | TEST(malloc, malloc_overflow) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 71 | SKIP_WITH_HWASAN; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 72 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 73 | ASSERT_EQ(nullptr, malloc(SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 74 | ASSERT_EQ(ENOMEM, errno); |
| 75 | } |
| 76 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 77 | TEST(malloc, calloc_std) { |
| 78 | // Simple calloc test. |
| 79 | size_t alloc_len = 100; |
| 80 | char *ptr = (char *)calloc(1, alloc_len); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 81 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 82 | ASSERT_LE(alloc_len, malloc_usable_size(ptr)); |
| 83 | for (size_t i = 0; i < alloc_len; i++) { |
| 84 | ASSERT_EQ(0, ptr[i]); |
| 85 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 86 | free(ptr); |
| 87 | } |
| 88 | |
Peter Collingbourne | 978eb16 | 2020-09-21 15:26:02 -0700 | [diff] [blame] | 89 | TEST(malloc, calloc_mem_init_disabled) { |
| 90 | #if defined(__BIONIC__) |
| 91 | // calloc should still zero memory if mem-init is disabled. |
| 92 | // With jemalloc the mallopts will fail but that shouldn't affect the |
| 93 | // execution of the test. |
| 94 | mallopt(M_THREAD_DISABLE_MEM_INIT, 1); |
| 95 | size_t alloc_len = 100; |
| 96 | char *ptr = reinterpret_cast<char*>(calloc(1, alloc_len)); |
| 97 | for (size_t i = 0; i < alloc_len; i++) { |
| 98 | ASSERT_EQ(0, ptr[i]); |
| 99 | } |
| 100 | free(ptr); |
| 101 | mallopt(M_THREAD_DISABLE_MEM_INIT, 0); |
| 102 | #else |
| 103 | GTEST_SKIP() << "bionic-only test"; |
| 104 | #endif |
| 105 | } |
| 106 | |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 107 | TEST(malloc, calloc_illegal) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 108 | SKIP_WITH_HWASAN; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 109 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 110 | ASSERT_EQ(nullptr, calloc(-1, 100)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 111 | ASSERT_EQ(ENOMEM, errno); |
| 112 | } |
| 113 | |
| 114 | TEST(malloc, calloc_overflow) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 115 | SKIP_WITH_HWASAN; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 116 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 117 | ASSERT_EQ(nullptr, calloc(1, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 118 | ASSERT_EQ(ENOMEM, errno); |
| 119 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 120 | ASSERT_EQ(nullptr, calloc(SIZE_MAX, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 121 | ASSERT_EQ(ENOMEM, errno); |
| 122 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 123 | ASSERT_EQ(nullptr, calloc(2, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 124 | ASSERT_EQ(ENOMEM, errno); |
| 125 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 126 | ASSERT_EQ(nullptr, calloc(SIZE_MAX, 2)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 127 | ASSERT_EQ(ENOMEM, errno); |
| 128 | } |
| 129 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 130 | TEST(malloc, memalign_multiple) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 131 | SKIP_WITH_HWASAN << "hwasan requires power of 2 alignment"; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 132 | // Memalign test where the alignment is any value. |
| 133 | for (size_t i = 0; i <= 12; i++) { |
| 134 | for (size_t alignment = 1 << i; alignment < (1U << (i+1)); alignment++) { |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 135 | char *ptr = reinterpret_cast<char*>(memalign(alignment, 100)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 136 | ASSERT_TRUE(ptr != nullptr) << "Failed at alignment " << alignment; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 137 | ASSERT_LE(100U, malloc_usable_size(ptr)) << "Failed at alignment " << alignment; |
| 138 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) % ((1U << i))) |
| 139 | << "Failed at alignment " << alignment; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 140 | free(ptr); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 145 | TEST(malloc, memalign_overflow) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 146 | SKIP_WITH_HWASAN; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 147 | ASSERT_EQ(nullptr, memalign(4096, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | TEST(malloc, memalign_non_power2) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 151 | SKIP_WITH_HWASAN; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 152 | void* ptr; |
| 153 | for (size_t align = 0; align <= 256; align++) { |
| 154 | ptr = memalign(align, 1024); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 155 | ASSERT_TRUE(ptr != nullptr) << "Failed at align " << align; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 156 | free(ptr); |
| 157 | } |
| 158 | } |
| 159 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 160 | TEST(malloc, memalign_realloc) { |
| 161 | // Memalign and then realloc the pointer a couple of times. |
| 162 | for (size_t alignment = 1; alignment <= 4096; alignment <<= 1) { |
| 163 | char *ptr = (char*)memalign(alignment, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 164 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 165 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 166 | ASSERT_EQ(0U, (intptr_t)ptr % alignment); |
| 167 | memset(ptr, 0x23, 100); |
| 168 | |
| 169 | ptr = (char*)realloc(ptr, 200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 170 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 171 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 172 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 173 | for (size_t i = 0; i < 100; i++) { |
| 174 | ASSERT_EQ(0x23, ptr[i]); |
| 175 | } |
| 176 | memset(ptr, 0x45, 200); |
| 177 | |
| 178 | ptr = (char*)realloc(ptr, 300); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 179 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 180 | ASSERT_LE(300U, malloc_usable_size(ptr)); |
| 181 | for (size_t i = 0; i < 200; i++) { |
| 182 | ASSERT_EQ(0x45, ptr[i]); |
| 183 | } |
| 184 | memset(ptr, 0x67, 300); |
| 185 | |
| 186 | ptr = (char*)realloc(ptr, 250); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 187 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 188 | ASSERT_LE(250U, malloc_usable_size(ptr)); |
| 189 | for (size_t i = 0; i < 250; i++) { |
| 190 | ASSERT_EQ(0x67, ptr[i]); |
| 191 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 192 | free(ptr); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | TEST(malloc, malloc_realloc_larger) { |
| 197 | // Realloc to a larger size, malloc is used for the original allocation. |
| 198 | char *ptr = (char *)malloc(100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 199 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 200 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 201 | memset(ptr, 67, 100); |
| 202 | |
| 203 | ptr = (char *)realloc(ptr, 200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 204 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 205 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 206 | for (size_t i = 0; i < 100; i++) { |
| 207 | ASSERT_EQ(67, ptr[i]); |
| 208 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 209 | free(ptr); |
| 210 | } |
| 211 | |
| 212 | TEST(malloc, malloc_realloc_smaller) { |
| 213 | // Realloc to a smaller size, malloc is used for the original allocation. |
| 214 | char *ptr = (char *)malloc(200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 215 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 216 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 217 | memset(ptr, 67, 200); |
| 218 | |
| 219 | ptr = (char *)realloc(ptr, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 220 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 221 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 222 | for (size_t i = 0; i < 100; i++) { |
| 223 | ASSERT_EQ(67, ptr[i]); |
| 224 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 225 | free(ptr); |
| 226 | } |
| 227 | |
| 228 | TEST(malloc, malloc_multiple_realloc) { |
| 229 | // Multiple reallocs, malloc is used for the original allocation. |
| 230 | char *ptr = (char *)malloc(200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 231 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 232 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 233 | memset(ptr, 0x23, 200); |
| 234 | |
| 235 | ptr = (char *)realloc(ptr, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 236 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 237 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 238 | for (size_t i = 0; i < 100; i++) { |
| 239 | ASSERT_EQ(0x23, ptr[i]); |
| 240 | } |
| 241 | |
| 242 | ptr = (char*)realloc(ptr, 50); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 243 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 244 | ASSERT_LE(50U, malloc_usable_size(ptr)); |
| 245 | for (size_t i = 0; i < 50; i++) { |
| 246 | ASSERT_EQ(0x23, ptr[i]); |
| 247 | } |
| 248 | |
| 249 | ptr = (char*)realloc(ptr, 150); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 250 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 251 | ASSERT_LE(150U, malloc_usable_size(ptr)); |
| 252 | for (size_t i = 0; i < 50; i++) { |
| 253 | ASSERT_EQ(0x23, ptr[i]); |
| 254 | } |
| 255 | memset(ptr, 0x23, 150); |
| 256 | |
| 257 | ptr = (char*)realloc(ptr, 425); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 258 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 259 | ASSERT_LE(425U, malloc_usable_size(ptr)); |
| 260 | for (size_t i = 0; i < 150; i++) { |
| 261 | ASSERT_EQ(0x23, ptr[i]); |
| 262 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 263 | free(ptr); |
| 264 | } |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 265 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 266 | TEST(malloc, calloc_realloc_larger) { |
| 267 | // Realloc to a larger size, calloc is used for the original allocation. |
| 268 | char *ptr = (char *)calloc(1, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 269 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 270 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 271 | |
| 272 | ptr = (char *)realloc(ptr, 200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 273 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 274 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 275 | for (size_t i = 0; i < 100; i++) { |
| 276 | ASSERT_EQ(0, ptr[i]); |
| 277 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 278 | free(ptr); |
| 279 | } |
| 280 | |
| 281 | TEST(malloc, calloc_realloc_smaller) { |
| 282 | // Realloc to a smaller size, calloc is used for the original allocation. |
| 283 | char *ptr = (char *)calloc(1, 200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 284 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 285 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 286 | |
| 287 | ptr = (char *)realloc(ptr, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 288 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 289 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 290 | for (size_t i = 0; i < 100; i++) { |
| 291 | ASSERT_EQ(0, ptr[i]); |
| 292 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 293 | free(ptr); |
| 294 | } |
| 295 | |
| 296 | TEST(malloc, calloc_multiple_realloc) { |
| 297 | // Multiple reallocs, calloc is used for the original allocation. |
| 298 | char *ptr = (char *)calloc(1, 200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 299 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 300 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 301 | |
| 302 | ptr = (char *)realloc(ptr, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 303 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 304 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 305 | for (size_t i = 0; i < 100; i++) { |
| 306 | ASSERT_EQ(0, ptr[i]); |
| 307 | } |
| 308 | |
| 309 | ptr = (char*)realloc(ptr, 50); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 310 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 311 | ASSERT_LE(50U, malloc_usable_size(ptr)); |
| 312 | for (size_t i = 0; i < 50; i++) { |
| 313 | ASSERT_EQ(0, ptr[i]); |
| 314 | } |
| 315 | |
| 316 | ptr = (char*)realloc(ptr, 150); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 317 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 318 | ASSERT_LE(150U, malloc_usable_size(ptr)); |
| 319 | for (size_t i = 0; i < 50; i++) { |
| 320 | ASSERT_EQ(0, ptr[i]); |
| 321 | } |
| 322 | memset(ptr, 0, 150); |
| 323 | |
| 324 | ptr = (char*)realloc(ptr, 425); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 325 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 326 | ASSERT_LE(425U, malloc_usable_size(ptr)); |
| 327 | for (size_t i = 0; i < 150; i++) { |
| 328 | ASSERT_EQ(0, ptr[i]); |
| 329 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 330 | free(ptr); |
| 331 | } |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 332 | |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 333 | TEST(malloc, realloc_overflow) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 334 | SKIP_WITH_HWASAN; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 335 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 336 | ASSERT_EQ(nullptr, realloc(nullptr, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 337 | ASSERT_EQ(ENOMEM, errno); |
| 338 | void* ptr = malloc(100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 339 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 340 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 341 | ASSERT_EQ(nullptr, realloc(ptr, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 342 | ASSERT_EQ(ENOMEM, errno); |
| 343 | free(ptr); |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Dan Albert | e5fdaa4 | 2014-06-14 01:04:31 +0000 | [diff] [blame] | 346 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 347 | extern "C" void* pvalloc(size_t); |
| 348 | extern "C" void* valloc(size_t); |
Christopher Ferris | 804cebe | 2019-06-20 08:50:23 -0700 | [diff] [blame] | 349 | #endif |
Dan Albert | e5fdaa4 | 2014-06-14 01:04:31 +0000 | [diff] [blame] | 350 | |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 351 | TEST(malloc, pvalloc_std) { |
Christopher Ferris | 804cebe | 2019-06-20 08:50:23 -0700 | [diff] [blame] | 352 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 353 | size_t pagesize = sysconf(_SC_PAGESIZE); |
| 354 | void* ptr = pvalloc(100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 355 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 356 | ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0); |
| 357 | ASSERT_LE(pagesize, malloc_usable_size(ptr)); |
| 358 | free(ptr); |
Christopher Ferris | 804cebe | 2019-06-20 08:50:23 -0700 | [diff] [blame] | 359 | #else |
| 360 | GTEST_SKIP() << "pvalloc not supported."; |
| 361 | #endif |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | TEST(malloc, pvalloc_overflow) { |
Christopher Ferris | 804cebe | 2019-06-20 08:50:23 -0700 | [diff] [blame] | 365 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 366 | ASSERT_EQ(nullptr, pvalloc(SIZE_MAX)); |
Christopher Ferris | 804cebe | 2019-06-20 08:50:23 -0700 | [diff] [blame] | 367 | #else |
| 368 | GTEST_SKIP() << "pvalloc not supported."; |
| 369 | #endif |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | TEST(malloc, valloc_std) { |
Christopher Ferris | 804cebe | 2019-06-20 08:50:23 -0700 | [diff] [blame] | 373 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 374 | size_t pagesize = sysconf(_SC_PAGESIZE); |
Christopher Ferris | d5ab0a5 | 2019-06-19 12:03:57 -0700 | [diff] [blame] | 375 | void* ptr = valloc(100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 376 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 377 | ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0); |
| 378 | free(ptr); |
Christopher Ferris | 804cebe | 2019-06-20 08:50:23 -0700 | [diff] [blame] | 379 | #else |
| 380 | GTEST_SKIP() << "valloc not supported."; |
| 381 | #endif |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | TEST(malloc, valloc_overflow) { |
Christopher Ferris | 804cebe | 2019-06-20 08:50:23 -0700 | [diff] [blame] | 385 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 386 | ASSERT_EQ(nullptr, valloc(SIZE_MAX)); |
Christopher Ferris | 804cebe | 2019-06-20 08:50:23 -0700 | [diff] [blame] | 387 | #else |
| 388 | GTEST_SKIP() << "valloc not supported."; |
Dan Albert | e5fdaa4 | 2014-06-14 01:04:31 +0000 | [diff] [blame] | 389 | #endif |
Christopher Ferris | 804cebe | 2019-06-20 08:50:23 -0700 | [diff] [blame] | 390 | } |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 391 | |
| 392 | TEST(malloc, malloc_info) { |
| 393 | #ifdef __BIONIC__ |
Evgenii Stepanov | 8de6b46 | 2019-03-22 13:22:28 -0700 | [diff] [blame] | 394 | SKIP_WITH_HWASAN; // hwasan does not implement malloc_info |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 395 | |
| 396 | TemporaryFile tf; |
| 397 | ASSERT_TRUE(tf.fd != -1); |
| 398 | FILE* fp = fdopen(tf.fd, "w+"); |
| 399 | tf.release(); |
| 400 | ASSERT_TRUE(fp != nullptr); |
| 401 | ASSERT_EQ(0, malloc_info(0, fp)); |
| 402 | ASSERT_EQ(0, fclose(fp)); |
| 403 | |
| 404 | std::string contents; |
| 405 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &contents)); |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 406 | |
| 407 | tinyxml2::XMLDocument doc; |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 408 | ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(contents.c_str())); |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 409 | |
| 410 | auto root = doc.FirstChildElement(); |
| 411 | ASSERT_NE(nullptr, root); |
| 412 | ASSERT_STREQ("malloc", root->Name()); |
Christopher Ferris | 8516965 | 2019-10-09 18:41:55 -0700 | [diff] [blame] | 413 | std::string version(root->Attribute("version")); |
| 414 | if (version == "jemalloc-1") { |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 415 | auto arena = root->FirstChildElement(); |
| 416 | for (; arena != nullptr; arena = arena->NextSiblingElement()) { |
| 417 | int val; |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 418 | |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 419 | ASSERT_STREQ("heap", arena->Name()); |
| 420 | ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val)); |
| 421 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 422 | arena->FirstChildElement("allocated-large")->QueryIntText(&val)); |
| 423 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 424 | arena->FirstChildElement("allocated-huge")->QueryIntText(&val)); |
| 425 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 426 | arena->FirstChildElement("allocated-bins")->QueryIntText(&val)); |
| 427 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 428 | arena->FirstChildElement("bins-total")->QueryIntText(&val)); |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 429 | |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 430 | auto bin = arena->FirstChildElement("bin"); |
| 431 | for (; bin != nullptr; bin = bin ->NextSiblingElement()) { |
| 432 | if (strcmp(bin->Name(), "bin") == 0) { |
| 433 | ASSERT_EQ(tinyxml2::XML_SUCCESS, bin->QueryIntAttribute("nr", &val)); |
| 434 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 435 | bin->FirstChildElement("allocated")->QueryIntText(&val)); |
| 436 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 437 | bin->FirstChildElement("nmalloc")->QueryIntText(&val)); |
| 438 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 439 | bin->FirstChildElement("ndalloc")->QueryIntText(&val)); |
| 440 | } |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
Christopher Ferris | cce88c0 | 2020-02-12 17:41:01 -0800 | [diff] [blame] | 443 | } else if (version == "scudo-1") { |
| 444 | auto element = root->FirstChildElement(); |
| 445 | for (; element != nullptr; element = element->NextSiblingElement()) { |
| 446 | int val; |
| 447 | |
| 448 | ASSERT_STREQ("alloc", element->Name()); |
| 449 | ASSERT_EQ(tinyxml2::XML_SUCCESS, element->QueryIntAttribute("size", &val)); |
| 450 | ASSERT_EQ(tinyxml2::XML_SUCCESS, element->QueryIntAttribute("count", &val)); |
| 451 | } |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 452 | } else { |
Christopher Ferris | cce88c0 | 2020-02-12 17:41:01 -0800 | [diff] [blame] | 453 | // Do not verify output for debug malloc. |
| 454 | ASSERT_TRUE(version == "debug-malloc-1") << "Unknown version: " << version; |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 455 | } |
| 456 | #endif |
| 457 | } |
Christopher Ferris | ad33ebe | 2015-12-16 12:07:25 -0800 | [diff] [blame] | 458 | |
Christopher Ferris | db9706a | 2019-05-02 18:33:11 -0700 | [diff] [blame] | 459 | TEST(malloc, malloc_info_matches_mallinfo) { |
| 460 | #ifdef __BIONIC__ |
| 461 | SKIP_WITH_HWASAN; // hwasan does not implement malloc_info |
| 462 | |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 463 | TemporaryFile tf; |
| 464 | ASSERT_TRUE(tf.fd != -1); |
| 465 | FILE* fp = fdopen(tf.fd, "w+"); |
| 466 | tf.release(); |
| 467 | ASSERT_TRUE(fp != nullptr); |
Christopher Ferris | db9706a | 2019-05-02 18:33:11 -0700 | [diff] [blame] | 468 | size_t mallinfo_before_allocated_bytes = mallinfo().uordblks; |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 469 | ASSERT_EQ(0, malloc_info(0, fp)); |
Christopher Ferris | db9706a | 2019-05-02 18:33:11 -0700 | [diff] [blame] | 470 | size_t mallinfo_after_allocated_bytes = mallinfo().uordblks; |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 471 | ASSERT_EQ(0, fclose(fp)); |
| 472 | |
| 473 | std::string contents; |
| 474 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &contents)); |
Christopher Ferris | db9706a | 2019-05-02 18:33:11 -0700 | [diff] [blame] | 475 | |
| 476 | tinyxml2::XMLDocument doc; |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 477 | ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(contents.c_str())); |
Christopher Ferris | db9706a | 2019-05-02 18:33:11 -0700 | [diff] [blame] | 478 | |
| 479 | size_t total_allocated_bytes = 0; |
| 480 | auto root = doc.FirstChildElement(); |
| 481 | ASSERT_NE(nullptr, root); |
| 482 | ASSERT_STREQ("malloc", root->Name()); |
Christopher Ferris | 8516965 | 2019-10-09 18:41:55 -0700 | [diff] [blame] | 483 | std::string version(root->Attribute("version")); |
| 484 | if (version == "jemalloc-1") { |
Christopher Ferris | db9706a | 2019-05-02 18:33:11 -0700 | [diff] [blame] | 485 | auto arena = root->FirstChildElement(); |
| 486 | for (; arena != nullptr; arena = arena->NextSiblingElement()) { |
| 487 | int val; |
| 488 | |
| 489 | ASSERT_STREQ("heap", arena->Name()); |
| 490 | ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val)); |
| 491 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 492 | arena->FirstChildElement("allocated-large")->QueryIntText(&val)); |
| 493 | total_allocated_bytes += val; |
| 494 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 495 | arena->FirstChildElement("allocated-huge")->QueryIntText(&val)); |
| 496 | total_allocated_bytes += val; |
| 497 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 498 | arena->FirstChildElement("allocated-bins")->QueryIntText(&val)); |
| 499 | total_allocated_bytes += val; |
| 500 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 501 | arena->FirstChildElement("bins-total")->QueryIntText(&val)); |
| 502 | } |
| 503 | // The total needs to be between the mallinfo call before and after |
| 504 | // since malloc_info allocates some memory. |
| 505 | EXPECT_LE(mallinfo_before_allocated_bytes, total_allocated_bytes); |
| 506 | EXPECT_GE(mallinfo_after_allocated_bytes, total_allocated_bytes); |
Christopher Ferris | cce88c0 | 2020-02-12 17:41:01 -0800 | [diff] [blame] | 507 | } else if (version == "scudo-1") { |
| 508 | auto element = root->FirstChildElement(); |
| 509 | for (; element != nullptr; element = element->NextSiblingElement()) { |
| 510 | ASSERT_STREQ("alloc", element->Name()); |
| 511 | int size; |
| 512 | ASSERT_EQ(tinyxml2::XML_SUCCESS, element->QueryIntAttribute("size", &size)); |
| 513 | int count; |
| 514 | ASSERT_EQ(tinyxml2::XML_SUCCESS, element->QueryIntAttribute("count", &count)); |
| 515 | total_allocated_bytes += size * count; |
| 516 | } |
| 517 | // Scudo only gives the information on the primary, so simply make |
| 518 | // sure that the value is non-zero. |
| 519 | EXPECT_NE(0U, total_allocated_bytes); |
Christopher Ferris | db9706a | 2019-05-02 18:33:11 -0700 | [diff] [blame] | 520 | } else { |
Christopher Ferris | cce88c0 | 2020-02-12 17:41:01 -0800 | [diff] [blame] | 521 | // Do not verify output for debug malloc. |
| 522 | ASSERT_TRUE(version == "debug-malloc-1") << "Unknown version: " << version; |
Christopher Ferris | db9706a | 2019-05-02 18:33:11 -0700 | [diff] [blame] | 523 | } |
| 524 | #endif |
| 525 | } |
| 526 | |
Christopher Ferris | ad33ebe | 2015-12-16 12:07:25 -0800 | [diff] [blame] | 527 | TEST(malloc, calloc_usable_size) { |
| 528 | for (size_t size = 1; size <= 2048; size++) { |
| 529 | void* pointer = malloc(size); |
| 530 | ASSERT_TRUE(pointer != nullptr); |
| 531 | memset(pointer, 0xeb, malloc_usable_size(pointer)); |
| 532 | free(pointer); |
| 533 | |
| 534 | // We should get a previous pointer that has been set to non-zero. |
| 535 | // If calloc does not zero out all of the data, this will fail. |
| 536 | uint8_t* zero_mem = reinterpret_cast<uint8_t*>(calloc(1, size)); |
| 537 | ASSERT_TRUE(pointer != nullptr); |
| 538 | size_t usable_size = malloc_usable_size(zero_mem); |
| 539 | for (size_t i = 0; i < usable_size; i++) { |
| 540 | ASSERT_EQ(0, zero_mem[i]) << "Failed at allocation size " << size << " at byte " << i; |
| 541 | } |
| 542 | free(zero_mem); |
| 543 | } |
| 544 | } |
Elliott Hughes | 884f76e | 2016-02-10 20:43:22 -0800 | [diff] [blame] | 545 | |
| 546 | TEST(malloc, malloc_0) { |
| 547 | void* p = malloc(0); |
| 548 | ASSERT_TRUE(p != nullptr); |
| 549 | free(p); |
| 550 | } |
| 551 | |
| 552 | TEST(malloc, calloc_0_0) { |
| 553 | void* p = calloc(0, 0); |
| 554 | ASSERT_TRUE(p != nullptr); |
| 555 | free(p); |
| 556 | } |
| 557 | |
| 558 | TEST(malloc, calloc_0_1) { |
| 559 | void* p = calloc(0, 1); |
| 560 | ASSERT_TRUE(p != nullptr); |
| 561 | free(p); |
| 562 | } |
| 563 | |
| 564 | TEST(malloc, calloc_1_0) { |
| 565 | void* p = calloc(1, 0); |
| 566 | ASSERT_TRUE(p != nullptr); |
| 567 | free(p); |
| 568 | } |
| 569 | |
| 570 | TEST(malloc, realloc_nullptr_0) { |
| 571 | // realloc(nullptr, size) is actually malloc(size). |
| 572 | void* p = realloc(nullptr, 0); |
| 573 | ASSERT_TRUE(p != nullptr); |
| 574 | free(p); |
| 575 | } |
| 576 | |
| 577 | TEST(malloc, realloc_0) { |
| 578 | void* p = malloc(1024); |
| 579 | ASSERT_TRUE(p != nullptr); |
| 580 | // realloc(p, 0) is actually free(p). |
| 581 | void* p2 = realloc(p, 0); |
| 582 | ASSERT_TRUE(p2 == nullptr); |
| 583 | } |
Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 584 | |
| 585 | constexpr size_t MAX_LOOPS = 200; |
| 586 | |
| 587 | // Make sure that memory returned by malloc is aligned to allow these data types. |
| 588 | TEST(malloc, verify_alignment) { |
| 589 | uint32_t** values_32 = new uint32_t*[MAX_LOOPS]; |
| 590 | uint64_t** values_64 = new uint64_t*[MAX_LOOPS]; |
| 591 | long double** values_ldouble = new long double*[MAX_LOOPS]; |
| 592 | // Use filler to attempt to force the allocator to get potentially bad alignments. |
| 593 | void** filler = new void*[MAX_LOOPS]; |
| 594 | |
| 595 | for (size_t i = 0; i < MAX_LOOPS; i++) { |
| 596 | // Check uint32_t pointers. |
| 597 | filler[i] = malloc(1); |
| 598 | ASSERT_TRUE(filler[i] != nullptr); |
| 599 | |
| 600 | values_32[i] = reinterpret_cast<uint32_t*>(malloc(sizeof(uint32_t))); |
| 601 | ASSERT_TRUE(values_32[i] != nullptr); |
| 602 | *values_32[i] = i; |
| 603 | ASSERT_EQ(*values_32[i], i); |
| 604 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_32[i]) & (sizeof(uint32_t) - 1)); |
| 605 | |
| 606 | free(filler[i]); |
| 607 | } |
| 608 | |
| 609 | for (size_t i = 0; i < MAX_LOOPS; i++) { |
| 610 | // Check uint64_t pointers. |
| 611 | filler[i] = malloc(1); |
| 612 | ASSERT_TRUE(filler[i] != nullptr); |
| 613 | |
| 614 | values_64[i] = reinterpret_cast<uint64_t*>(malloc(sizeof(uint64_t))); |
| 615 | ASSERT_TRUE(values_64[i] != nullptr); |
| 616 | *values_64[i] = 0x1000 + i; |
| 617 | ASSERT_EQ(*values_64[i], 0x1000 + i); |
| 618 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_64[i]) & (sizeof(uint64_t) - 1)); |
| 619 | |
| 620 | free(filler[i]); |
| 621 | } |
| 622 | |
| 623 | for (size_t i = 0; i < MAX_LOOPS; i++) { |
| 624 | // Check long double pointers. |
| 625 | filler[i] = malloc(1); |
| 626 | ASSERT_TRUE(filler[i] != nullptr); |
| 627 | |
| 628 | values_ldouble[i] = reinterpret_cast<long double*>(malloc(sizeof(long double))); |
| 629 | ASSERT_TRUE(values_ldouble[i] != nullptr); |
| 630 | *values_ldouble[i] = 5.5 + i; |
| 631 | ASSERT_DOUBLE_EQ(*values_ldouble[i], 5.5 + i); |
| 632 | // 32 bit glibc has a long double size of 12 bytes, so hardcode the |
| 633 | // required alignment to 0x7. |
| 634 | #if !defined(__BIONIC__) && !defined(__LP64__) |
| 635 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_ldouble[i]) & 0x7); |
| 636 | #else |
| 637 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_ldouble[i]) & (sizeof(long double) - 1)); |
| 638 | #endif |
| 639 | |
| 640 | free(filler[i]); |
| 641 | } |
| 642 | |
| 643 | for (size_t i = 0; i < MAX_LOOPS; i++) { |
| 644 | free(values_32[i]); |
| 645 | free(values_64[i]); |
| 646 | free(values_ldouble[i]); |
| 647 | } |
| 648 | |
| 649 | delete[] filler; |
| 650 | delete[] values_32; |
| 651 | delete[] values_64; |
| 652 | delete[] values_ldouble; |
| 653 | } |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 654 | |
| 655 | TEST(malloc, mallopt_smoke) { |
| 656 | errno = 0; |
| 657 | ASSERT_EQ(0, mallopt(-1000, 1)); |
| 658 | // mallopt doesn't set errno. |
| 659 | ASSERT_EQ(0, errno); |
| 660 | } |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 661 | |
Christopher Ferris | af1b8dd | 2018-11-07 15:28:16 -0800 | [diff] [blame] | 662 | TEST(malloc, mallopt_decay) { |
| 663 | #if defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 664 | SKIP_WITH_HWASAN << "hwasan does not implement mallopt"; |
Christopher Ferris | af1b8dd | 2018-11-07 15:28:16 -0800 | [diff] [blame] | 665 | errno = 0; |
| 666 | ASSERT_EQ(1, mallopt(M_DECAY_TIME, 1)); |
| 667 | ASSERT_EQ(1, mallopt(M_DECAY_TIME, 0)); |
| 668 | ASSERT_EQ(1, mallopt(M_DECAY_TIME, 1)); |
| 669 | ASSERT_EQ(1, mallopt(M_DECAY_TIME, 0)); |
| 670 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 671 | GTEST_SKIP() << "bionic-only test"; |
Christopher Ferris | af1b8dd | 2018-11-07 15:28:16 -0800 | [diff] [blame] | 672 | #endif |
| 673 | } |
| 674 | |
| 675 | TEST(malloc, mallopt_purge) { |
| 676 | #if defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 677 | SKIP_WITH_HWASAN << "hwasan does not implement mallopt"; |
Christopher Ferris | af1b8dd | 2018-11-07 15:28:16 -0800 | [diff] [blame] | 678 | errno = 0; |
| 679 | ASSERT_EQ(1, mallopt(M_PURGE, 0)); |
| 680 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 681 | GTEST_SKIP() << "bionic-only test"; |
Christopher Ferris | af1b8dd | 2018-11-07 15:28:16 -0800 | [diff] [blame] | 682 | #endif |
| 683 | } |
| 684 | |
Christopher Ferris | 8844879 | 2020-07-28 14:15:31 -0700 | [diff] [blame] | 685 | #if defined(__BIONIC__) |
| 686 | static void GetAllocatorVersion(bool* allocator_scudo) { |
| 687 | TemporaryFile tf; |
| 688 | ASSERT_TRUE(tf.fd != -1); |
| 689 | FILE* fp = fdopen(tf.fd, "w+"); |
| 690 | tf.release(); |
| 691 | ASSERT_TRUE(fp != nullptr); |
| 692 | ASSERT_EQ(0, malloc_info(0, fp)); |
| 693 | ASSERT_EQ(0, fclose(fp)); |
| 694 | |
| 695 | std::string contents; |
| 696 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &contents)); |
| 697 | |
| 698 | tinyxml2::XMLDocument doc; |
| 699 | ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(contents.c_str())); |
| 700 | |
| 701 | auto root = doc.FirstChildElement(); |
| 702 | ASSERT_NE(nullptr, root); |
| 703 | ASSERT_STREQ("malloc", root->Name()); |
| 704 | std::string version(root->Attribute("version")); |
| 705 | *allocator_scudo = (version == "scudo-1"); |
| 706 | } |
| 707 | #endif |
| 708 | |
| 709 | TEST(malloc, mallopt_scudo_only_options) { |
| 710 | #if defined(__BIONIC__) |
| 711 | SKIP_WITH_HWASAN << "hwasan does not implement mallopt"; |
| 712 | bool allocator_scudo; |
| 713 | GetAllocatorVersion(&allocator_scudo); |
| 714 | if (!allocator_scudo) { |
| 715 | GTEST_SKIP() << "scudo allocator only test"; |
| 716 | } |
| 717 | ASSERT_EQ(1, mallopt(M_CACHE_COUNT_MAX, 100)); |
| 718 | ASSERT_EQ(1, mallopt(M_CACHE_SIZE_MAX, 1024 * 1024 * 2)); |
| 719 | ASSERT_EQ(1, mallopt(M_TSDS_COUNT_MAX, 8)); |
| 720 | #else |
| 721 | GTEST_SKIP() << "bionic-only test"; |
| 722 | #endif |
| 723 | } |
| 724 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 725 | TEST(malloc, reallocarray_overflow) { |
| 726 | #if HAVE_REALLOCARRAY |
| 727 | // Values that cause overflow to a result small enough (8 on LP64) that malloc would "succeed". |
| 728 | size_t a = static_cast<size_t>(INTPTR_MIN + 4); |
| 729 | size_t b = 2; |
| 730 | |
| 731 | errno = 0; |
| 732 | ASSERT_TRUE(reallocarray(nullptr, a, b) == nullptr); |
| 733 | ASSERT_EQ(ENOMEM, errno); |
| 734 | |
| 735 | errno = 0; |
| 736 | ASSERT_TRUE(reallocarray(nullptr, b, a) == nullptr); |
| 737 | ASSERT_EQ(ENOMEM, errno); |
| 738 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 739 | GTEST_SKIP() << "reallocarray not available"; |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 740 | #endif |
| 741 | } |
| 742 | |
| 743 | TEST(malloc, reallocarray) { |
| 744 | #if HAVE_REALLOCARRAY |
| 745 | void* p = reallocarray(nullptr, 2, 32); |
| 746 | ASSERT_TRUE(p != nullptr); |
| 747 | ASSERT_GE(malloc_usable_size(p), 64U); |
| 748 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 749 | GTEST_SKIP() << "reallocarray not available"; |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 750 | #endif |
| 751 | } |
Christopher Ferris | 09a19aa | 2018-11-16 13:28:56 -0800 | [diff] [blame] | 752 | |
| 753 | TEST(malloc, mallinfo) { |
| 754 | #if defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 755 | SKIP_WITH_HWASAN << "hwasan does not implement mallinfo"; |
Christopher Ferris | 09a19aa | 2018-11-16 13:28:56 -0800 | [diff] [blame] | 756 | static size_t sizes[] = { |
| 757 | 8, 32, 128, 4096, 32768, 131072, 1024000, 10240000, 20480000, 300000000 |
| 758 | }; |
| 759 | |
| 760 | constexpr static size_t kMaxAllocs = 50; |
| 761 | |
| 762 | for (size_t size : sizes) { |
| 763 | // If some of these allocations are stuck in a thread cache, then keep |
| 764 | // looping until we make an allocation that changes the total size of the |
| 765 | // memory allocated. |
| 766 | // jemalloc implementations counts the thread cache allocations against |
| 767 | // total memory allocated. |
| 768 | void* ptrs[kMaxAllocs] = {}; |
| 769 | bool pass = false; |
| 770 | for (size_t i = 0; i < kMaxAllocs; i++) { |
| 771 | size_t allocated = mallinfo().uordblks; |
| 772 | ptrs[i] = malloc(size); |
| 773 | ASSERT_TRUE(ptrs[i] != nullptr); |
| 774 | size_t new_allocated = mallinfo().uordblks; |
| 775 | if (allocated != new_allocated) { |
| 776 | size_t usable_size = malloc_usable_size(ptrs[i]); |
Christopher Ferris | 4e56228 | 2019-02-07 14:20:03 -0800 | [diff] [blame] | 777 | // Only check if the total got bigger by at least allocation size. |
| 778 | // Sometimes the mallinfo numbers can go backwards due to compaction |
| 779 | // and/or freeing of cached data. |
| 780 | if (new_allocated >= allocated + usable_size) { |
| 781 | pass = true; |
| 782 | break; |
| 783 | } |
Christopher Ferris | 09a19aa | 2018-11-16 13:28:56 -0800 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | for (void* ptr : ptrs) { |
| 787 | free(ptr); |
| 788 | } |
| 789 | ASSERT_TRUE(pass) |
| 790 | << "For size " << size << " allocated bytes did not increase after " |
| 791 | << kMaxAllocs << " allocations."; |
| 792 | } |
| 793 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 794 | GTEST_SKIP() << "glibc is broken"; |
Christopher Ferris | 09a19aa | 2018-11-16 13:28:56 -0800 | [diff] [blame] | 795 | #endif |
| 796 | } |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 797 | |
Christopher Ferris | f32494c | 2020-01-08 14:19:10 -0800 | [diff] [blame] | 798 | template <typename Type> |
| 799 | void __attribute__((optnone)) VerifyAlignment(Type* floating) { |
| 800 | size_t expected_alignment = alignof(Type); |
| 801 | if (expected_alignment != 0) { |
| 802 | ASSERT_EQ(0U, (expected_alignment - 1) & reinterpret_cast<uintptr_t>(floating)) |
| 803 | << "Expected alignment " << expected_alignment << " ptr value " << floating; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | template <typename Type> |
| 808 | void __attribute__((optnone)) TestAllocateType() { |
| 809 | // The number of allocations to do in a row. This is to attempt to |
| 810 | // expose the worst case alignment for native allocators that use |
| 811 | // bins. |
| 812 | static constexpr size_t kMaxConsecutiveAllocs = 100; |
| 813 | |
| 814 | // Verify using new directly. |
| 815 | Type* types[kMaxConsecutiveAllocs]; |
| 816 | for (size_t i = 0; i < kMaxConsecutiveAllocs; i++) { |
| 817 | types[i] = new Type; |
| 818 | VerifyAlignment(types[i]); |
| 819 | if (::testing::Test::HasFatalFailure()) { |
| 820 | return; |
| 821 | } |
| 822 | } |
| 823 | for (size_t i = 0; i < kMaxConsecutiveAllocs; i++) { |
| 824 | delete types[i]; |
| 825 | } |
| 826 | |
| 827 | // Verify using malloc. |
| 828 | for (size_t i = 0; i < kMaxConsecutiveAllocs; i++) { |
| 829 | types[i] = reinterpret_cast<Type*>(malloc(sizeof(Type))); |
| 830 | ASSERT_TRUE(types[i] != nullptr); |
| 831 | VerifyAlignment(types[i]); |
| 832 | if (::testing::Test::HasFatalFailure()) { |
| 833 | return; |
| 834 | } |
| 835 | } |
| 836 | for (size_t i = 0; i < kMaxConsecutiveAllocs; i++) { |
| 837 | free(types[i]); |
| 838 | } |
| 839 | |
| 840 | // Verify using a vector. |
| 841 | std::vector<Type> type_vector(kMaxConsecutiveAllocs); |
| 842 | for (size_t i = 0; i < type_vector.size(); i++) { |
| 843 | VerifyAlignment(&type_vector[i]); |
| 844 | if (::testing::Test::HasFatalFailure()) { |
| 845 | return; |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | #if defined(__ANDROID__) |
| 851 | static void __attribute__((optnone)) AndroidVerifyAlignment(size_t alloc_size, size_t aligned_bytes) { |
| 852 | void* ptrs[100]; |
| 853 | uintptr_t mask = aligned_bytes - 1; |
| 854 | for (size_t i = 0; i < sizeof(ptrs) / sizeof(void*); i++) { |
| 855 | ptrs[i] = malloc(alloc_size); |
| 856 | ASSERT_TRUE(ptrs[i] != nullptr); |
| 857 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptrs[i]) & mask) |
| 858 | << "Expected at least " << aligned_bytes << " byte alignment: size " |
| 859 | << alloc_size << " actual ptr " << ptrs[i]; |
| 860 | } |
| 861 | } |
| 862 | #endif |
| 863 | |
| 864 | TEST(malloc, align_check) { |
| 865 | // See http://www.open-std.org/jtc1/sc22/wg14/www/docs/summary.htm#dr_445 |
| 866 | // for a discussion of type alignment. |
| 867 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<float>()); |
| 868 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<double>()); |
| 869 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<long double>()); |
| 870 | |
| 871 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<char>()); |
| 872 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<char16_t>()); |
| 873 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<char32_t>()); |
| 874 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<wchar_t>()); |
| 875 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<signed char>()); |
| 876 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<short int>()); |
| 877 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<int>()); |
| 878 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<long int>()); |
| 879 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<long long int>()); |
| 880 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<unsigned char>()); |
| 881 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<unsigned short int>()); |
| 882 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<unsigned int>()); |
| 883 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<unsigned long int>()); |
| 884 | ASSERT_NO_FATAL_FAILURE(TestAllocateType<unsigned long long int>()); |
| 885 | |
| 886 | #if defined(__ANDROID__) |
| 887 | // On Android, there is a lot of code that expects certain alignments: |
| 888 | // - Allocations of a size that rounds up to a multiple of 16 bytes |
| 889 | // must have at least 16 byte alignment. |
| 890 | // - Allocations of a size that rounds up to a multiple of 8 bytes and |
| 891 | // not 16 bytes, are only required to have at least 8 byte alignment. |
| 892 | // This is regardless of whether it is in a 32 bit or 64 bit environment. |
| 893 | |
| 894 | // See http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2293.htm for |
| 895 | // a discussion of this alignment mess. The code below is enforcing |
| 896 | // strong-alignment, since who knows what code depends on this behavior now. |
| 897 | for (size_t i = 1; i <= 128; i++) { |
| 898 | size_t rounded = (i + 7) & ~7; |
| 899 | if ((rounded % 16) == 0) { |
| 900 | AndroidVerifyAlignment(i, 16); |
| 901 | } else { |
| 902 | AndroidVerifyAlignment(i, 8); |
| 903 | } |
| 904 | if (::testing::Test::HasFatalFailure()) { |
| 905 | return; |
| 906 | } |
| 907 | } |
| 908 | #endif |
| 909 | } |
| 910 | |
Christopher Ferris | 201dcf4 | 2020-01-29 13:09:31 -0800 | [diff] [blame] | 911 | // Jemalloc doesn't pass this test right now, so leave it as disabled. |
| 912 | TEST(malloc, DISABLED_alloc_after_fork) { |
| 913 | // Both of these need to be a power of 2. |
| 914 | static constexpr size_t kMinAllocationSize = 8; |
| 915 | static constexpr size_t kMaxAllocationSize = 2097152; |
| 916 | |
| 917 | static constexpr size_t kNumAllocatingThreads = 5; |
| 918 | static constexpr size_t kNumForkLoops = 100; |
| 919 | |
| 920 | std::atomic_bool stop; |
| 921 | |
| 922 | // Create threads that simply allocate and free different sizes. |
| 923 | std::vector<std::thread*> threads; |
| 924 | for (size_t i = 0; i < kNumAllocatingThreads; i++) { |
| 925 | std::thread* t = new std::thread([&stop] { |
| 926 | while (!stop) { |
| 927 | for (size_t size = kMinAllocationSize; size <= kMaxAllocationSize; size <<= 1) { |
| 928 | void* ptr = malloc(size); |
| 929 | if (ptr == nullptr) { |
| 930 | return; |
| 931 | } |
| 932 | // Make sure this value is not optimized away. |
| 933 | asm volatile("" : : "r,m"(ptr) : "memory"); |
| 934 | free(ptr); |
| 935 | } |
| 936 | } |
| 937 | }); |
| 938 | threads.push_back(t); |
| 939 | } |
| 940 | |
| 941 | // Create a thread to fork and allocate. |
| 942 | for (size_t i = 0; i < kNumForkLoops; i++) { |
| 943 | pid_t pid; |
| 944 | if ((pid = fork()) == 0) { |
| 945 | for (size_t size = kMinAllocationSize; size <= kMaxAllocationSize; size <<= 1) { |
| 946 | void* ptr = malloc(size); |
| 947 | ASSERT_TRUE(ptr != nullptr); |
| 948 | // Make sure this value is not optimized away. |
| 949 | asm volatile("" : : "r,m"(ptr) : "memory"); |
| 950 | // Make sure we can touch all of the allocation. |
| 951 | memset(ptr, 0x1, size); |
| 952 | ASSERT_LE(size, malloc_usable_size(ptr)); |
| 953 | free(ptr); |
| 954 | } |
| 955 | _exit(10); |
| 956 | } |
| 957 | ASSERT_NE(-1, pid); |
| 958 | AssertChildExited(pid, 10); |
| 959 | } |
| 960 | |
| 961 | stop = true; |
| 962 | for (auto thread : threads) { |
| 963 | thread->join(); |
| 964 | delete thread; |
| 965 | } |
| 966 | } |
| 967 | |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 968 | TEST(android_mallopt, error_on_unexpected_option) { |
| 969 | #if defined(__BIONIC__) |
| 970 | const int unrecognized_option = -1; |
| 971 | errno = 0; |
| 972 | EXPECT_EQ(false, android_mallopt(unrecognized_option, nullptr, 0)); |
| 973 | EXPECT_EQ(ENOTSUP, errno); |
| 974 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 975 | GTEST_SKIP() << "bionic-only test"; |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 976 | #endif |
| 977 | } |
| 978 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 979 | bool IsDynamic() { |
| 980 | #if defined(__LP64__) |
| 981 | Elf64_Ehdr ehdr; |
| 982 | #else |
| 983 | Elf32_Ehdr ehdr; |
| 984 | #endif |
| 985 | std::string path(android::base::GetExecutablePath()); |
| 986 | |
| 987 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 988 | if (fd == -1) { |
| 989 | // Assume dynamic on error. |
| 990 | return true; |
| 991 | } |
| 992 | bool read_completed = android::base::ReadFully(fd, &ehdr, sizeof(ehdr)); |
| 993 | close(fd); |
| 994 | // Assume dynamic in error cases. |
| 995 | return !read_completed || ehdr.e_type == ET_DYN; |
| 996 | } |
| 997 | |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 998 | TEST(android_mallopt, init_zygote_child_profiling) { |
| 999 | #if defined(__BIONIC__) |
| 1000 | // Successful call. |
| 1001 | errno = 0; |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 1002 | if (IsDynamic()) { |
| 1003 | EXPECT_EQ(true, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0)); |
| 1004 | EXPECT_EQ(0, errno); |
| 1005 | } else { |
| 1006 | // Not supported in static executables. |
| 1007 | EXPECT_EQ(false, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0)); |
| 1008 | EXPECT_EQ(ENOTSUP, errno); |
| 1009 | } |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Unexpected arguments rejected. |
| 1012 | errno = 0; |
| 1013 | char unexpected = 0; |
| 1014 | EXPECT_EQ(false, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, &unexpected, 1)); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 1015 | if (IsDynamic()) { |
| 1016 | EXPECT_EQ(EINVAL, errno); |
| 1017 | } else { |
| 1018 | EXPECT_EQ(ENOTSUP, errno); |
| 1019 | } |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 1020 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1021 | GTEST_SKIP() << "bionic-only test"; |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 1022 | #endif |
| 1023 | } |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 1024 | |
| 1025 | #if defined(__BIONIC__) |
| 1026 | template <typename FuncType> |
| 1027 | void CheckAllocationFunction(FuncType func) { |
| 1028 | // Assumes that no more than 108MB of memory is allocated before this. |
| 1029 | size_t limit = 128 * 1024 * 1024; |
| 1030 | ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 1031 | if (!func(20 * 1024 * 1024)) |
| 1032 | exit(1); |
| 1033 | if (func(128 * 1024 * 1024)) |
| 1034 | exit(1); |
| 1035 | exit(0); |
| 1036 | } |
| 1037 | #endif |
| 1038 | |
| 1039 | TEST(android_mallopt, set_allocation_limit) { |
| 1040 | #if defined(__BIONIC__) |
| 1041 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return calloc(bytes, 1) != nullptr; }), |
| 1042 | testing::ExitedWithCode(0), ""); |
| 1043 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return calloc(1, bytes) != nullptr; }), |
| 1044 | testing::ExitedWithCode(0), ""); |
| 1045 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return malloc(bytes) != nullptr; }), |
| 1046 | testing::ExitedWithCode(0), ""); |
| 1047 | EXPECT_EXIT(CheckAllocationFunction( |
| 1048 | [](size_t bytes) { return memalign(sizeof(void*), bytes) != nullptr; }), |
| 1049 | testing::ExitedWithCode(0), ""); |
| 1050 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { |
| 1051 | void* ptr; |
| 1052 | return posix_memalign(&ptr, sizeof(void *), bytes) == 0; |
| 1053 | }), |
| 1054 | testing::ExitedWithCode(0), ""); |
| 1055 | EXPECT_EXIT(CheckAllocationFunction( |
| 1056 | [](size_t bytes) { return aligned_alloc(sizeof(void*), bytes) != nullptr; }), |
| 1057 | testing::ExitedWithCode(0), ""); |
| 1058 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { |
| 1059 | void* p = malloc(1024 * 1024); |
| 1060 | return realloc(p, bytes) != nullptr; |
| 1061 | }), |
| 1062 | testing::ExitedWithCode(0), ""); |
| 1063 | #if !defined(__LP64__) |
| 1064 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return pvalloc(bytes) != nullptr; }), |
| 1065 | testing::ExitedWithCode(0), ""); |
| 1066 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return valloc(bytes) != nullptr; }), |
| 1067 | testing::ExitedWithCode(0), ""); |
| 1068 | #endif |
| 1069 | #else |
Elliott Hughes | 1090720 | 2019-03-27 08:51:02 -0700 | [diff] [blame] | 1070 | GTEST_SKIP() << "bionic extension"; |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 1071 | #endif |
| 1072 | } |
| 1073 | |
| 1074 | TEST(android_mallopt, set_allocation_limit_multiple) { |
| 1075 | #if defined(__BIONIC__) |
| 1076 | // Only the first set should work. |
| 1077 | size_t limit = 256 * 1024 * 1024; |
| 1078 | ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 1079 | limit = 32 * 1024 * 1024; |
| 1080 | ASSERT_FALSE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 1081 | #else |
Elliott Hughes | 1090720 | 2019-03-27 08:51:02 -0700 | [diff] [blame] | 1082 | GTEST_SKIP() << "bionic extension"; |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 1083 | #endif |
| 1084 | } |
| 1085 | |
| 1086 | #if defined(__BIONIC__) |
| 1087 | static constexpr size_t kAllocationSize = 8 * 1024 * 1024; |
| 1088 | |
| 1089 | static size_t GetMaxAllocations() { |
| 1090 | size_t max_pointers = 0; |
| 1091 | void* ptrs[20]; |
| 1092 | for (size_t i = 0; i < sizeof(ptrs) / sizeof(void*); i++) { |
| 1093 | ptrs[i] = malloc(kAllocationSize); |
| 1094 | if (ptrs[i] == nullptr) { |
| 1095 | max_pointers = i; |
| 1096 | break; |
| 1097 | } |
| 1098 | } |
| 1099 | for (size_t i = 0; i < max_pointers; i++) { |
| 1100 | free(ptrs[i]); |
| 1101 | } |
| 1102 | return max_pointers; |
| 1103 | } |
| 1104 | |
| 1105 | static void VerifyMaxPointers(size_t max_pointers) { |
| 1106 | // Now verify that we can allocate the same number as before. |
| 1107 | void* ptrs[20]; |
| 1108 | for (size_t i = 0; i < max_pointers; i++) { |
| 1109 | ptrs[i] = malloc(kAllocationSize); |
| 1110 | ASSERT_TRUE(ptrs[i] != nullptr) << "Failed to allocate on iteration " << i; |
| 1111 | } |
| 1112 | |
| 1113 | // Make sure the next allocation still fails. |
| 1114 | ASSERT_TRUE(malloc(kAllocationSize) == nullptr); |
| 1115 | for (size_t i = 0; i < max_pointers; i++) { |
| 1116 | free(ptrs[i]); |
| 1117 | } |
| 1118 | } |
| 1119 | #endif |
| 1120 | |
| 1121 | TEST(android_mallopt, set_allocation_limit_realloc_increase) { |
| 1122 | #if defined(__BIONIC__) |
| 1123 | size_t limit = 128 * 1024 * 1024; |
| 1124 | ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 1125 | |
| 1126 | size_t max_pointers = GetMaxAllocations(); |
| 1127 | ASSERT_TRUE(max_pointers != 0) << "Limit never reached."; |
| 1128 | |
| 1129 | void* memory = malloc(10 * 1024 * 1024); |
| 1130 | ASSERT_TRUE(memory != nullptr); |
| 1131 | |
| 1132 | // Increase size. |
| 1133 | memory = realloc(memory, 20 * 1024 * 1024); |
| 1134 | ASSERT_TRUE(memory != nullptr); |
| 1135 | memory = realloc(memory, 40 * 1024 * 1024); |
| 1136 | ASSERT_TRUE(memory != nullptr); |
| 1137 | memory = realloc(memory, 60 * 1024 * 1024); |
| 1138 | ASSERT_TRUE(memory != nullptr); |
| 1139 | memory = realloc(memory, 80 * 1024 * 1024); |
| 1140 | ASSERT_TRUE(memory != nullptr); |
| 1141 | // Now push past limit. |
| 1142 | memory = realloc(memory, 130 * 1024 * 1024); |
| 1143 | ASSERT_TRUE(memory == nullptr); |
| 1144 | |
| 1145 | VerifyMaxPointers(max_pointers); |
| 1146 | #else |
Elliott Hughes | 1090720 | 2019-03-27 08:51:02 -0700 | [diff] [blame] | 1147 | GTEST_SKIP() << "bionic extension"; |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 1148 | #endif |
| 1149 | } |
| 1150 | |
| 1151 | TEST(android_mallopt, set_allocation_limit_realloc_decrease) { |
| 1152 | #if defined(__BIONIC__) |
| 1153 | size_t limit = 100 * 1024 * 1024; |
| 1154 | ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 1155 | |
| 1156 | size_t max_pointers = GetMaxAllocations(); |
| 1157 | ASSERT_TRUE(max_pointers != 0) << "Limit never reached."; |
| 1158 | |
| 1159 | void* memory = malloc(80 * 1024 * 1024); |
| 1160 | ASSERT_TRUE(memory != nullptr); |
| 1161 | |
| 1162 | // Decrease size. |
| 1163 | memory = realloc(memory, 60 * 1024 * 1024); |
| 1164 | ASSERT_TRUE(memory != nullptr); |
| 1165 | memory = realloc(memory, 40 * 1024 * 1024); |
| 1166 | ASSERT_TRUE(memory != nullptr); |
| 1167 | memory = realloc(memory, 20 * 1024 * 1024); |
| 1168 | ASSERT_TRUE(memory != nullptr); |
| 1169 | memory = realloc(memory, 10 * 1024 * 1024); |
| 1170 | ASSERT_TRUE(memory != nullptr); |
| 1171 | free(memory); |
| 1172 | |
| 1173 | VerifyMaxPointers(max_pointers); |
| 1174 | #else |
Elliott Hughes | 1090720 | 2019-03-27 08:51:02 -0700 | [diff] [blame] | 1175 | GTEST_SKIP() << "bionic extension"; |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 1176 | #endif |
| 1177 | } |
| 1178 | |
| 1179 | TEST(android_mallopt, set_allocation_limit_realloc_free) { |
| 1180 | #if defined(__BIONIC__) |
| 1181 | size_t limit = 100 * 1024 * 1024; |
| 1182 | ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 1183 | |
| 1184 | size_t max_pointers = GetMaxAllocations(); |
| 1185 | ASSERT_TRUE(max_pointers != 0) << "Limit never reached."; |
| 1186 | |
| 1187 | void* memory = malloc(60 * 1024 * 1024); |
| 1188 | ASSERT_TRUE(memory != nullptr); |
| 1189 | |
| 1190 | memory = realloc(memory, 0); |
| 1191 | ASSERT_TRUE(memory == nullptr); |
| 1192 | |
| 1193 | VerifyMaxPointers(max_pointers); |
| 1194 | #else |
Elliott Hughes | 1090720 | 2019-03-27 08:51:02 -0700 | [diff] [blame] | 1195 | GTEST_SKIP() << "bionic extension"; |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 1196 | #endif |
| 1197 | } |
| 1198 | |
| 1199 | #if defined(__BIONIC__) |
| 1200 | static void* SetAllocationLimit(void* data) { |
| 1201 | std::atomic_bool* go = reinterpret_cast<std::atomic_bool*>(data); |
| 1202 | while (!go->load()) { |
| 1203 | } |
| 1204 | size_t limit = 500 * 1024 * 1024; |
| 1205 | if (android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))) { |
| 1206 | return reinterpret_cast<void*>(-1); |
| 1207 | } |
| 1208 | return nullptr; |
| 1209 | } |
| 1210 | |
| 1211 | static void SetAllocationLimitMultipleThreads() { |
| 1212 | std::atomic_bool go; |
| 1213 | go = false; |
| 1214 | |
| 1215 | static constexpr size_t kNumThreads = 4; |
| 1216 | pthread_t threads[kNumThreads]; |
| 1217 | for (size_t i = 0; i < kNumThreads; i++) { |
| 1218 | ASSERT_EQ(0, pthread_create(&threads[i], nullptr, SetAllocationLimit, &go)); |
| 1219 | } |
| 1220 | |
| 1221 | // Let them go all at once. |
| 1222 | go = true; |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 1223 | // Send hardcoded signal (BIONIC_SIGNAL_PROFILER with value 0) to trigger |
| 1224 | // heapprofd handler. |
| 1225 | union sigval signal_value; |
| 1226 | signal_value.sival_int = 0; |
Christopher Ferris | b874c33 | 2020-01-21 16:39:05 -0800 | [diff] [blame] | 1227 | ASSERT_EQ(0, sigqueue(getpid(), BIONIC_SIGNAL_PROFILER, signal_value)); |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 1228 | |
| 1229 | size_t num_successful = 0; |
| 1230 | for (size_t i = 0; i < kNumThreads; i++) { |
| 1231 | void* result; |
| 1232 | ASSERT_EQ(0, pthread_join(threads[i], &result)); |
| 1233 | if (result != nullptr) { |
| 1234 | num_successful++; |
| 1235 | } |
| 1236 | } |
| 1237 | ASSERT_EQ(1U, num_successful); |
| 1238 | exit(0); |
| 1239 | } |
| 1240 | #endif |
| 1241 | |
| 1242 | TEST(android_mallopt, set_allocation_limit_multiple_threads) { |
| 1243 | #if defined(__BIONIC__) |
| 1244 | if (IsDynamic()) { |
| 1245 | ASSERT_TRUE(android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0)); |
| 1246 | } |
| 1247 | |
| 1248 | // Run this a number of times as a stress test. |
| 1249 | for (size_t i = 0; i < 100; i++) { |
| 1250 | // Not using ASSERT_EXIT because errors messages are not displayed. |
| 1251 | pid_t pid; |
| 1252 | if ((pid = fork()) == 0) { |
| 1253 | ASSERT_NO_FATAL_FAILURE(SetAllocationLimitMultipleThreads()); |
| 1254 | } |
| 1255 | ASSERT_NE(-1, pid); |
| 1256 | int status; |
| 1257 | ASSERT_EQ(pid, wait(&status)); |
| 1258 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 1259 | } |
| 1260 | #else |
Elliott Hughes | 1090720 | 2019-03-27 08:51:02 -0700 | [diff] [blame] | 1261 | GTEST_SKIP() << "bionic extension"; |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 1262 | #endif |
| 1263 | } |
Peter Collingbourne | 5d3aa86 | 2020-09-11 15:05:17 -0700 | [diff] [blame] | 1264 | |
| 1265 | TEST(android_mallopt, disable_memory_mitigations) { |
| 1266 | #if defined(__BIONIC__) |
| 1267 | if (!mte_supported()) { |
| 1268 | GTEST_SKIP() << "This function can only be tested with MTE"; |
| 1269 | } |
| 1270 | |
| 1271 | #ifdef ANDROID_EXPERIMENTAL_MTE |
| 1272 | sem_t sem; |
| 1273 | ASSERT_EQ(0, sem_init(&sem, 0, 0)); |
| 1274 | |
| 1275 | pthread_t thread; |
| 1276 | ASSERT_EQ(0, pthread_create( |
| 1277 | &thread, nullptr, |
| 1278 | [](void* ptr) -> void* { |
| 1279 | auto* sem = reinterpret_cast<sem_t*>(ptr); |
| 1280 | sem_wait(sem); |
| 1281 | return reinterpret_cast<void*>(prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0)); |
| 1282 | }, |
| 1283 | &sem)); |
| 1284 | |
| 1285 | ASSERT_TRUE(android_mallopt(M_DISABLE_MEMORY_MITIGATIONS, nullptr, 0)); |
| 1286 | ASSERT_EQ(0, sem_post(&sem)); |
| 1287 | |
| 1288 | int my_tagged_addr_ctrl = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0); |
| 1289 | ASSERT_EQ(PR_MTE_TCF_NONE, my_tagged_addr_ctrl & PR_MTE_TCF_MASK); |
| 1290 | |
| 1291 | void* retval; |
| 1292 | ASSERT_EQ(0, pthread_join(thread, &retval)); |
| 1293 | int thread_tagged_addr_ctrl = reinterpret_cast<uintptr_t>(retval); |
| 1294 | ASSERT_EQ(my_tagged_addr_ctrl, thread_tagged_addr_ctrl); |
| 1295 | #endif |
| 1296 | #else |
| 1297 | GTEST_SKIP() << "bionic extension"; |
| 1298 | #endif |
| 1299 | } |