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