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> |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame^] | 21 | #include <pthread.h> |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 22 | #include <stdint.h> |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame^] | 25 | #include <string.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/wait.h> |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 28 | #include <malloc.h> |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 29 | #include <unistd.h> |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 30 | |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame^] | 31 | #include <atomic> |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 32 | #include <tinyxml2.h> |
| 33 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 34 | #include <android-base/file.h> |
| 35 | |
Christopher Ferris | 6361964 | 2014-06-16 23:35:53 -0700 | [diff] [blame] | 36 | #include "private/bionic_config.h" |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 37 | #include "private/bionic_malloc.h" |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 38 | #include "utils.h" |
Dan Albert | e5fdaa4 | 2014-06-14 01:04:31 +0000 | [diff] [blame] | 39 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 40 | #if defined(__BIONIC__) |
| 41 | #define HAVE_REALLOCARRAY 1 |
| 42 | #else |
| 43 | #define HAVE_REALLOCARRAY __GLIBC_PREREQ(2, 26) |
| 44 | #endif |
| 45 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 46 | TEST(malloc, malloc_std) { |
| 47 | // Simple malloc test. |
| 48 | void *ptr = malloc(100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 49 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 50 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 51 | free(ptr); |
| 52 | } |
| 53 | |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 54 | TEST(malloc, malloc_overflow) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 55 | SKIP_WITH_HWASAN; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 56 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 57 | ASSERT_EQ(nullptr, malloc(SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 58 | ASSERT_EQ(ENOMEM, errno); |
| 59 | } |
| 60 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 61 | TEST(malloc, calloc_std) { |
| 62 | // Simple calloc test. |
| 63 | size_t alloc_len = 100; |
| 64 | char *ptr = (char *)calloc(1, alloc_len); |
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(alloc_len, malloc_usable_size(ptr)); |
| 67 | for (size_t i = 0; i < alloc_len; i++) { |
| 68 | ASSERT_EQ(0, ptr[i]); |
| 69 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 70 | free(ptr); |
| 71 | } |
| 72 | |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 73 | TEST(malloc, calloc_illegal) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 74 | SKIP_WITH_HWASAN; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 75 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 76 | ASSERT_EQ(nullptr, calloc(-1, 100)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 77 | ASSERT_EQ(ENOMEM, errno); |
| 78 | } |
| 79 | |
| 80 | TEST(malloc, calloc_overflow) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 81 | SKIP_WITH_HWASAN; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 82 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 83 | ASSERT_EQ(nullptr, calloc(1, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 84 | ASSERT_EQ(ENOMEM, errno); |
| 85 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 86 | ASSERT_EQ(nullptr, calloc(SIZE_MAX, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 87 | ASSERT_EQ(ENOMEM, errno); |
| 88 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 89 | ASSERT_EQ(nullptr, calloc(2, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 90 | ASSERT_EQ(ENOMEM, errno); |
| 91 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 92 | ASSERT_EQ(nullptr, calloc(SIZE_MAX, 2)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 93 | ASSERT_EQ(ENOMEM, errno); |
| 94 | } |
| 95 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 96 | TEST(malloc, memalign_multiple) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 97 | SKIP_WITH_HWASAN; // hwasan requires power of 2 alignment. |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 98 | // Memalign test where the alignment is any value. |
| 99 | for (size_t i = 0; i <= 12; i++) { |
| 100 | for (size_t alignment = 1 << i; alignment < (1U << (i+1)); alignment++) { |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 101 | char *ptr = reinterpret_cast<char*>(memalign(alignment, 100)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 102 | ASSERT_TRUE(ptr != nullptr) << "Failed at alignment " << alignment; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 103 | ASSERT_LE(100U, malloc_usable_size(ptr)) << "Failed at alignment " << alignment; |
| 104 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) % ((1U << i))) |
| 105 | << "Failed at alignment " << alignment; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 106 | free(ptr); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 111 | TEST(malloc, memalign_overflow) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 112 | SKIP_WITH_HWASAN; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 113 | ASSERT_EQ(nullptr, memalign(4096, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST(malloc, memalign_non_power2) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 117 | SKIP_WITH_HWASAN; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 118 | void* ptr; |
| 119 | for (size_t align = 0; align <= 256; align++) { |
| 120 | ptr = memalign(align, 1024); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 121 | ASSERT_TRUE(ptr != nullptr) << "Failed at align " << align; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 122 | free(ptr); |
| 123 | } |
| 124 | } |
| 125 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 126 | TEST(malloc, memalign_realloc) { |
| 127 | // Memalign and then realloc the pointer a couple of times. |
| 128 | for (size_t alignment = 1; alignment <= 4096; alignment <<= 1) { |
| 129 | char *ptr = (char*)memalign(alignment, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 130 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 131 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 132 | ASSERT_EQ(0U, (intptr_t)ptr % alignment); |
| 133 | memset(ptr, 0x23, 100); |
| 134 | |
| 135 | ptr = (char*)realloc(ptr, 200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 136 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 137 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 138 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 139 | for (size_t i = 0; i < 100; i++) { |
| 140 | ASSERT_EQ(0x23, ptr[i]); |
| 141 | } |
| 142 | memset(ptr, 0x45, 200); |
| 143 | |
| 144 | ptr = (char*)realloc(ptr, 300); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 145 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 146 | ASSERT_LE(300U, malloc_usable_size(ptr)); |
| 147 | for (size_t i = 0; i < 200; i++) { |
| 148 | ASSERT_EQ(0x45, ptr[i]); |
| 149 | } |
| 150 | memset(ptr, 0x67, 300); |
| 151 | |
| 152 | ptr = (char*)realloc(ptr, 250); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 153 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 154 | ASSERT_LE(250U, malloc_usable_size(ptr)); |
| 155 | for (size_t i = 0; i < 250; i++) { |
| 156 | ASSERT_EQ(0x67, ptr[i]); |
| 157 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 158 | free(ptr); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | TEST(malloc, malloc_realloc_larger) { |
| 163 | // Realloc to a larger size, malloc is used for the original allocation. |
| 164 | char *ptr = (char *)malloc(100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 165 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 166 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 167 | memset(ptr, 67, 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)); |
| 172 | for (size_t i = 0; i < 100; i++) { |
| 173 | ASSERT_EQ(67, ptr[i]); |
| 174 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 175 | free(ptr); |
| 176 | } |
| 177 | |
| 178 | TEST(malloc, malloc_realloc_smaller) { |
| 179 | // Realloc to a smaller size, malloc is used for the original allocation. |
| 180 | char *ptr = (char *)malloc(200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 181 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 182 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 183 | memset(ptr, 67, 200); |
| 184 | |
| 185 | ptr = (char *)realloc(ptr, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 186 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 187 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 188 | for (size_t i = 0; i < 100; i++) { |
| 189 | ASSERT_EQ(67, ptr[i]); |
| 190 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 191 | free(ptr); |
| 192 | } |
| 193 | |
| 194 | TEST(malloc, malloc_multiple_realloc) { |
| 195 | // Multiple reallocs, malloc is used for the original allocation. |
| 196 | char *ptr = (char *)malloc(200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 197 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 198 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 199 | memset(ptr, 0x23, 200); |
| 200 | |
| 201 | ptr = (char *)realloc(ptr, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 202 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 203 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 204 | for (size_t i = 0; i < 100; i++) { |
| 205 | ASSERT_EQ(0x23, ptr[i]); |
| 206 | } |
| 207 | |
| 208 | ptr = (char*)realloc(ptr, 50); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 209 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 210 | ASSERT_LE(50U, malloc_usable_size(ptr)); |
| 211 | for (size_t i = 0; i < 50; i++) { |
| 212 | ASSERT_EQ(0x23, ptr[i]); |
| 213 | } |
| 214 | |
| 215 | ptr = (char*)realloc(ptr, 150); |
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(150U, malloc_usable_size(ptr)); |
| 218 | for (size_t i = 0; i < 50; i++) { |
| 219 | ASSERT_EQ(0x23, ptr[i]); |
| 220 | } |
| 221 | memset(ptr, 0x23, 150); |
| 222 | |
| 223 | ptr = (char*)realloc(ptr, 425); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 224 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 225 | ASSERT_LE(425U, malloc_usable_size(ptr)); |
| 226 | for (size_t i = 0; i < 150; i++) { |
| 227 | ASSERT_EQ(0x23, ptr[i]); |
| 228 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 229 | free(ptr); |
| 230 | } |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 231 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 232 | TEST(malloc, calloc_realloc_larger) { |
| 233 | // Realloc to a larger size, calloc is used for the original allocation. |
| 234 | char *ptr = (char *)calloc(1, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 235 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 236 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 237 | |
| 238 | ptr = (char *)realloc(ptr, 200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 239 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 240 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 241 | for (size_t i = 0; i < 100; i++) { |
| 242 | ASSERT_EQ(0, ptr[i]); |
| 243 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 244 | free(ptr); |
| 245 | } |
| 246 | |
| 247 | TEST(malloc, calloc_realloc_smaller) { |
| 248 | // Realloc to a smaller size, calloc is used for the original allocation. |
| 249 | char *ptr = (char *)calloc(1, 200); |
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(200U, malloc_usable_size(ptr)); |
| 252 | |
| 253 | ptr = (char *)realloc(ptr, 100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 254 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 255 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 256 | for (size_t i = 0; i < 100; i++) { |
| 257 | ASSERT_EQ(0, ptr[i]); |
| 258 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 259 | free(ptr); |
| 260 | } |
| 261 | |
| 262 | TEST(malloc, calloc_multiple_realloc) { |
| 263 | // Multiple reallocs, calloc is used for the original allocation. |
| 264 | char *ptr = (char *)calloc(1, 200); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 265 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 266 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 267 | |
| 268 | ptr = (char *)realloc(ptr, 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 | for (size_t i = 0; i < 100; i++) { |
| 272 | ASSERT_EQ(0, ptr[i]); |
| 273 | } |
| 274 | |
| 275 | ptr = (char*)realloc(ptr, 50); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 276 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 277 | ASSERT_LE(50U, malloc_usable_size(ptr)); |
| 278 | for (size_t i = 0; i < 50; i++) { |
| 279 | ASSERT_EQ(0, ptr[i]); |
| 280 | } |
| 281 | |
| 282 | ptr = (char*)realloc(ptr, 150); |
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(150U, malloc_usable_size(ptr)); |
| 285 | for (size_t i = 0; i < 50; i++) { |
| 286 | ASSERT_EQ(0, ptr[i]); |
| 287 | } |
| 288 | memset(ptr, 0, 150); |
| 289 | |
| 290 | ptr = (char*)realloc(ptr, 425); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 291 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 292 | ASSERT_LE(425U, malloc_usable_size(ptr)); |
| 293 | for (size_t i = 0; i < 150; i++) { |
| 294 | ASSERT_EQ(0, ptr[i]); |
| 295 | } |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 296 | free(ptr); |
| 297 | } |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 298 | |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 299 | TEST(malloc, realloc_overflow) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 300 | SKIP_WITH_HWASAN; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 301 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 302 | ASSERT_EQ(nullptr, realloc(nullptr, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 303 | ASSERT_EQ(ENOMEM, errno); |
| 304 | void* ptr = malloc(100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 305 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 306 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 307 | ASSERT_EQ(nullptr, realloc(ptr, SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 308 | ASSERT_EQ(ENOMEM, errno); |
| 309 | free(ptr); |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Dan Albert | e5fdaa4 | 2014-06-14 01:04:31 +0000 | [diff] [blame] | 312 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 313 | extern "C" void* pvalloc(size_t); |
| 314 | extern "C" void* valloc(size_t); |
| 315 | |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 316 | TEST(malloc, pvalloc_std) { |
| 317 | size_t pagesize = sysconf(_SC_PAGESIZE); |
| 318 | void* ptr = pvalloc(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 | ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0); |
| 321 | ASSERT_LE(pagesize, malloc_usable_size(ptr)); |
| 322 | free(ptr); |
| 323 | } |
| 324 | |
| 325 | TEST(malloc, pvalloc_overflow) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 326 | ASSERT_EQ(nullptr, pvalloc(SIZE_MAX)); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | TEST(malloc, valloc_std) { |
| 330 | size_t pagesize = sysconf(_SC_PAGESIZE); |
| 331 | void* ptr = pvalloc(100); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 332 | ASSERT_TRUE(ptr != nullptr); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 333 | ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0); |
| 334 | free(ptr); |
| 335 | } |
| 336 | |
| 337 | TEST(malloc, valloc_overflow) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 338 | ASSERT_EQ(nullptr, valloc(SIZE_MAX)); |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 339 | } |
Dan Albert | e5fdaa4 | 2014-06-14 01:04:31 +0000 | [diff] [blame] | 340 | #endif |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 341 | |
| 342 | TEST(malloc, malloc_info) { |
| 343 | #ifdef __BIONIC__ |
| 344 | char* buf; |
| 345 | size_t bufsize; |
| 346 | FILE* memstream = open_memstream(&buf, &bufsize); |
| 347 | ASSERT_NE(nullptr, memstream); |
| 348 | ASSERT_EQ(0, malloc_info(0, memstream)); |
| 349 | ASSERT_EQ(0, fclose(memstream)); |
| 350 | |
| 351 | tinyxml2::XMLDocument doc; |
| 352 | ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(buf)); |
| 353 | |
| 354 | auto root = doc.FirstChildElement(); |
| 355 | ASSERT_NE(nullptr, root); |
| 356 | ASSERT_STREQ("malloc", root->Name()); |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 357 | if (std::string(root->Attribute("version")) == "jemalloc-1") { |
| 358 | // Verify jemalloc version of this data. |
| 359 | ASSERT_STREQ("jemalloc-1", root->Attribute("version")); |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 360 | |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 361 | auto arena = root->FirstChildElement(); |
| 362 | for (; arena != nullptr; arena = arena->NextSiblingElement()) { |
| 363 | int val; |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 364 | |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 365 | ASSERT_STREQ("heap", arena->Name()); |
| 366 | ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val)); |
| 367 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 368 | arena->FirstChildElement("allocated-large")->QueryIntText(&val)); |
| 369 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 370 | arena->FirstChildElement("allocated-huge")->QueryIntText(&val)); |
| 371 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 372 | arena->FirstChildElement("allocated-bins")->QueryIntText(&val)); |
| 373 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 374 | arena->FirstChildElement("bins-total")->QueryIntText(&val)); |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 375 | |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 376 | auto bin = arena->FirstChildElement("bin"); |
| 377 | for (; bin != nullptr; bin = bin ->NextSiblingElement()) { |
| 378 | if (strcmp(bin->Name(), "bin") == 0) { |
| 379 | ASSERT_EQ(tinyxml2::XML_SUCCESS, bin->QueryIntAttribute("nr", &val)); |
| 380 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 381 | bin->FirstChildElement("allocated")->QueryIntText(&val)); |
| 382 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 383 | bin->FirstChildElement("nmalloc")->QueryIntText(&val)); |
| 384 | ASSERT_EQ(tinyxml2::XML_SUCCESS, |
| 385 | bin->FirstChildElement("ndalloc")->QueryIntText(&val)); |
| 386 | } |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 389 | } else { |
| 390 | // Only verify that this is debug-malloc-1, the malloc debug unit tests |
| 391 | // verify the output. |
| 392 | ASSERT_STREQ("debug-malloc-1", root->Attribute("version")); |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 393 | } |
| 394 | #endif |
| 395 | } |
Christopher Ferris | ad33ebe | 2015-12-16 12:07:25 -0800 | [diff] [blame] | 396 | |
| 397 | TEST(malloc, calloc_usable_size) { |
| 398 | for (size_t size = 1; size <= 2048; size++) { |
| 399 | void* pointer = malloc(size); |
| 400 | ASSERT_TRUE(pointer != nullptr); |
| 401 | memset(pointer, 0xeb, malloc_usable_size(pointer)); |
| 402 | free(pointer); |
| 403 | |
| 404 | // We should get a previous pointer that has been set to non-zero. |
| 405 | // If calloc does not zero out all of the data, this will fail. |
| 406 | uint8_t* zero_mem = reinterpret_cast<uint8_t*>(calloc(1, size)); |
| 407 | ASSERT_TRUE(pointer != nullptr); |
| 408 | size_t usable_size = malloc_usable_size(zero_mem); |
| 409 | for (size_t i = 0; i < usable_size; i++) { |
| 410 | ASSERT_EQ(0, zero_mem[i]) << "Failed at allocation size " << size << " at byte " << i; |
| 411 | } |
| 412 | free(zero_mem); |
| 413 | } |
| 414 | } |
Elliott Hughes | 884f76e | 2016-02-10 20:43:22 -0800 | [diff] [blame] | 415 | |
| 416 | TEST(malloc, malloc_0) { |
| 417 | void* p = malloc(0); |
| 418 | ASSERT_TRUE(p != nullptr); |
| 419 | free(p); |
| 420 | } |
| 421 | |
| 422 | TEST(malloc, calloc_0_0) { |
| 423 | void* p = calloc(0, 0); |
| 424 | ASSERT_TRUE(p != nullptr); |
| 425 | free(p); |
| 426 | } |
| 427 | |
| 428 | TEST(malloc, calloc_0_1) { |
| 429 | void* p = calloc(0, 1); |
| 430 | ASSERT_TRUE(p != nullptr); |
| 431 | free(p); |
| 432 | } |
| 433 | |
| 434 | TEST(malloc, calloc_1_0) { |
| 435 | void* p = calloc(1, 0); |
| 436 | ASSERT_TRUE(p != nullptr); |
| 437 | free(p); |
| 438 | } |
| 439 | |
| 440 | TEST(malloc, realloc_nullptr_0) { |
| 441 | // realloc(nullptr, size) is actually malloc(size). |
| 442 | void* p = realloc(nullptr, 0); |
| 443 | ASSERT_TRUE(p != nullptr); |
| 444 | free(p); |
| 445 | } |
| 446 | |
| 447 | TEST(malloc, realloc_0) { |
| 448 | void* p = malloc(1024); |
| 449 | ASSERT_TRUE(p != nullptr); |
| 450 | // realloc(p, 0) is actually free(p). |
| 451 | void* p2 = realloc(p, 0); |
| 452 | ASSERT_TRUE(p2 == nullptr); |
| 453 | } |
Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 454 | |
| 455 | constexpr size_t MAX_LOOPS = 200; |
| 456 | |
| 457 | // Make sure that memory returned by malloc is aligned to allow these data types. |
| 458 | TEST(malloc, verify_alignment) { |
| 459 | uint32_t** values_32 = new uint32_t*[MAX_LOOPS]; |
| 460 | uint64_t** values_64 = new uint64_t*[MAX_LOOPS]; |
| 461 | long double** values_ldouble = new long double*[MAX_LOOPS]; |
| 462 | // Use filler to attempt to force the allocator to get potentially bad alignments. |
| 463 | void** filler = new void*[MAX_LOOPS]; |
| 464 | |
| 465 | for (size_t i = 0; i < MAX_LOOPS; i++) { |
| 466 | // Check uint32_t pointers. |
| 467 | filler[i] = malloc(1); |
| 468 | ASSERT_TRUE(filler[i] != nullptr); |
| 469 | |
| 470 | values_32[i] = reinterpret_cast<uint32_t*>(malloc(sizeof(uint32_t))); |
| 471 | ASSERT_TRUE(values_32[i] != nullptr); |
| 472 | *values_32[i] = i; |
| 473 | ASSERT_EQ(*values_32[i], i); |
| 474 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_32[i]) & (sizeof(uint32_t) - 1)); |
| 475 | |
| 476 | free(filler[i]); |
| 477 | } |
| 478 | |
| 479 | for (size_t i = 0; i < MAX_LOOPS; i++) { |
| 480 | // Check uint64_t pointers. |
| 481 | filler[i] = malloc(1); |
| 482 | ASSERT_TRUE(filler[i] != nullptr); |
| 483 | |
| 484 | values_64[i] = reinterpret_cast<uint64_t*>(malloc(sizeof(uint64_t))); |
| 485 | ASSERT_TRUE(values_64[i] != nullptr); |
| 486 | *values_64[i] = 0x1000 + i; |
| 487 | ASSERT_EQ(*values_64[i], 0x1000 + i); |
| 488 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_64[i]) & (sizeof(uint64_t) - 1)); |
| 489 | |
| 490 | free(filler[i]); |
| 491 | } |
| 492 | |
| 493 | for (size_t i = 0; i < MAX_LOOPS; i++) { |
| 494 | // Check long double pointers. |
| 495 | filler[i] = malloc(1); |
| 496 | ASSERT_TRUE(filler[i] != nullptr); |
| 497 | |
| 498 | values_ldouble[i] = reinterpret_cast<long double*>(malloc(sizeof(long double))); |
| 499 | ASSERT_TRUE(values_ldouble[i] != nullptr); |
| 500 | *values_ldouble[i] = 5.5 + i; |
| 501 | ASSERT_DOUBLE_EQ(*values_ldouble[i], 5.5 + i); |
| 502 | // 32 bit glibc has a long double size of 12 bytes, so hardcode the |
| 503 | // required alignment to 0x7. |
| 504 | #if !defined(__BIONIC__) && !defined(__LP64__) |
| 505 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_ldouble[i]) & 0x7); |
| 506 | #else |
| 507 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_ldouble[i]) & (sizeof(long double) - 1)); |
| 508 | #endif |
| 509 | |
| 510 | free(filler[i]); |
| 511 | } |
| 512 | |
| 513 | for (size_t i = 0; i < MAX_LOOPS; i++) { |
| 514 | free(values_32[i]); |
| 515 | free(values_64[i]); |
| 516 | free(values_ldouble[i]); |
| 517 | } |
| 518 | |
| 519 | delete[] filler; |
| 520 | delete[] values_32; |
| 521 | delete[] values_64; |
| 522 | delete[] values_ldouble; |
| 523 | } |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 524 | |
| 525 | TEST(malloc, mallopt_smoke) { |
| 526 | errno = 0; |
| 527 | ASSERT_EQ(0, mallopt(-1000, 1)); |
| 528 | // mallopt doesn't set errno. |
| 529 | ASSERT_EQ(0, errno); |
| 530 | } |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 531 | |
Christopher Ferris | af1b8dd | 2018-11-07 15:28:16 -0800 | [diff] [blame] | 532 | TEST(malloc, mallopt_decay) { |
| 533 | #if defined(__BIONIC__) |
Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 534 | SKIP_WITH_HWASAN; // hwasan does not implement mallopt |
Christopher Ferris | af1b8dd | 2018-11-07 15:28:16 -0800 | [diff] [blame] | 535 | errno = 0; |
| 536 | ASSERT_EQ(1, mallopt(M_DECAY_TIME, 1)); |
| 537 | ASSERT_EQ(1, mallopt(M_DECAY_TIME, 0)); |
| 538 | ASSERT_EQ(1, mallopt(M_DECAY_TIME, 1)); |
| 539 | ASSERT_EQ(1, mallopt(M_DECAY_TIME, 0)); |
| 540 | #else |
| 541 | GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n"; |
| 542 | #endif |
| 543 | } |
| 544 | |
| 545 | TEST(malloc, mallopt_purge) { |
| 546 | #if defined(__BIONIC__) |
Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 547 | SKIP_WITH_HWASAN; // hwasan does not implement mallopt |
Christopher Ferris | af1b8dd | 2018-11-07 15:28:16 -0800 | [diff] [blame] | 548 | errno = 0; |
| 549 | ASSERT_EQ(1, mallopt(M_PURGE, 0)); |
| 550 | #else |
| 551 | GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n"; |
| 552 | #endif |
| 553 | } |
| 554 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 555 | TEST(malloc, reallocarray_overflow) { |
| 556 | #if HAVE_REALLOCARRAY |
| 557 | // Values that cause overflow to a result small enough (8 on LP64) that malloc would "succeed". |
| 558 | size_t a = static_cast<size_t>(INTPTR_MIN + 4); |
| 559 | size_t b = 2; |
| 560 | |
| 561 | errno = 0; |
| 562 | ASSERT_TRUE(reallocarray(nullptr, a, b) == nullptr); |
| 563 | ASSERT_EQ(ENOMEM, errno); |
| 564 | |
| 565 | errno = 0; |
| 566 | ASSERT_TRUE(reallocarray(nullptr, b, a) == nullptr); |
| 567 | ASSERT_EQ(ENOMEM, errno); |
| 568 | #else |
| 569 | GTEST_LOG_(INFO) << "This test requires a C library with reallocarray.\n"; |
| 570 | #endif |
| 571 | } |
| 572 | |
| 573 | TEST(malloc, reallocarray) { |
| 574 | #if HAVE_REALLOCARRAY |
| 575 | void* p = reallocarray(nullptr, 2, 32); |
| 576 | ASSERT_TRUE(p != nullptr); |
| 577 | ASSERT_GE(malloc_usable_size(p), 64U); |
| 578 | #else |
| 579 | GTEST_LOG_(INFO) << "This test requires a C library with reallocarray.\n"; |
| 580 | #endif |
| 581 | } |
Christopher Ferris | 09a19aa | 2018-11-16 13:28:56 -0800 | [diff] [blame] | 582 | |
| 583 | TEST(malloc, mallinfo) { |
| 584 | #if defined(__BIONIC__) |
Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 585 | SKIP_WITH_HWASAN; // hwasan does not implement mallinfo |
Christopher Ferris | 09a19aa | 2018-11-16 13:28:56 -0800 | [diff] [blame] | 586 | static size_t sizes[] = { |
| 587 | 8, 32, 128, 4096, 32768, 131072, 1024000, 10240000, 20480000, 300000000 |
| 588 | }; |
| 589 | |
| 590 | constexpr static size_t kMaxAllocs = 50; |
| 591 | |
| 592 | for (size_t size : sizes) { |
| 593 | // If some of these allocations are stuck in a thread cache, then keep |
| 594 | // looping until we make an allocation that changes the total size of the |
| 595 | // memory allocated. |
| 596 | // jemalloc implementations counts the thread cache allocations against |
| 597 | // total memory allocated. |
| 598 | void* ptrs[kMaxAllocs] = {}; |
| 599 | bool pass = false; |
| 600 | for (size_t i = 0; i < kMaxAllocs; i++) { |
| 601 | size_t allocated = mallinfo().uordblks; |
| 602 | ptrs[i] = malloc(size); |
| 603 | ASSERT_TRUE(ptrs[i] != nullptr); |
| 604 | size_t new_allocated = mallinfo().uordblks; |
| 605 | if (allocated != new_allocated) { |
| 606 | size_t usable_size = malloc_usable_size(ptrs[i]); |
Christopher Ferris | 4e56228 | 2019-02-07 14:20:03 -0800 | [diff] [blame] | 607 | // Only check if the total got bigger by at least allocation size. |
| 608 | // Sometimes the mallinfo numbers can go backwards due to compaction |
| 609 | // and/or freeing of cached data. |
| 610 | if (new_allocated >= allocated + usable_size) { |
| 611 | pass = true; |
| 612 | break; |
| 613 | } |
Christopher Ferris | 09a19aa | 2018-11-16 13:28:56 -0800 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | for (void* ptr : ptrs) { |
| 617 | free(ptr); |
| 618 | } |
| 619 | ASSERT_TRUE(pass) |
| 620 | << "For size " << size << " allocated bytes did not increase after " |
| 621 | << kMaxAllocs << " allocations."; |
| 622 | } |
| 623 | #else |
| 624 | GTEST_LOG_(INFO) << "Host glibc does not pass this test, skipping.\n"; |
| 625 | #endif |
| 626 | } |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 627 | |
| 628 | TEST(android_mallopt, error_on_unexpected_option) { |
| 629 | #if defined(__BIONIC__) |
| 630 | const int unrecognized_option = -1; |
| 631 | errno = 0; |
| 632 | EXPECT_EQ(false, android_mallopt(unrecognized_option, nullptr, 0)); |
| 633 | EXPECT_EQ(ENOTSUP, errno); |
| 634 | #else |
| 635 | GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n"; |
| 636 | #endif |
| 637 | } |
| 638 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 639 | bool IsDynamic() { |
| 640 | #if defined(__LP64__) |
| 641 | Elf64_Ehdr ehdr; |
| 642 | #else |
| 643 | Elf32_Ehdr ehdr; |
| 644 | #endif |
| 645 | std::string path(android::base::GetExecutablePath()); |
| 646 | |
| 647 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 648 | if (fd == -1) { |
| 649 | // Assume dynamic on error. |
| 650 | return true; |
| 651 | } |
| 652 | bool read_completed = android::base::ReadFully(fd, &ehdr, sizeof(ehdr)); |
| 653 | close(fd); |
| 654 | // Assume dynamic in error cases. |
| 655 | return !read_completed || ehdr.e_type == ET_DYN; |
| 656 | } |
| 657 | |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 658 | TEST(android_mallopt, init_zygote_child_profiling) { |
| 659 | #if defined(__BIONIC__) |
| 660 | // Successful call. |
| 661 | errno = 0; |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 662 | if (IsDynamic()) { |
| 663 | EXPECT_EQ(true, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0)); |
| 664 | EXPECT_EQ(0, errno); |
| 665 | } else { |
| 666 | // Not supported in static executables. |
| 667 | EXPECT_EQ(false, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0)); |
| 668 | EXPECT_EQ(ENOTSUP, errno); |
| 669 | } |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 670 | |
| 671 | // Unexpected arguments rejected. |
| 672 | errno = 0; |
| 673 | char unexpected = 0; |
| 674 | EXPECT_EQ(false, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, &unexpected, 1)); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 675 | if (IsDynamic()) { |
| 676 | EXPECT_EQ(EINVAL, errno); |
| 677 | } else { |
| 678 | EXPECT_EQ(ENOTSUP, errno); |
| 679 | } |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 680 | #else |
| 681 | GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n"; |
| 682 | #endif |
| 683 | } |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame^] | 684 | |
| 685 | #if defined(__BIONIC__) |
| 686 | template <typename FuncType> |
| 687 | void CheckAllocationFunction(FuncType func) { |
| 688 | // Assumes that no more than 108MB of memory is allocated before this. |
| 689 | size_t limit = 128 * 1024 * 1024; |
| 690 | ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 691 | if (!func(20 * 1024 * 1024)) |
| 692 | exit(1); |
| 693 | if (func(128 * 1024 * 1024)) |
| 694 | exit(1); |
| 695 | exit(0); |
| 696 | } |
| 697 | #endif |
| 698 | |
| 699 | TEST(android_mallopt, set_allocation_limit) { |
| 700 | #if defined(__BIONIC__) |
| 701 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return calloc(bytes, 1) != nullptr; }), |
| 702 | testing::ExitedWithCode(0), ""); |
| 703 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return calloc(1, bytes) != nullptr; }), |
| 704 | testing::ExitedWithCode(0), ""); |
| 705 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return malloc(bytes) != nullptr; }), |
| 706 | testing::ExitedWithCode(0), ""); |
| 707 | EXPECT_EXIT(CheckAllocationFunction( |
| 708 | [](size_t bytes) { return memalign(sizeof(void*), bytes) != nullptr; }), |
| 709 | testing::ExitedWithCode(0), ""); |
| 710 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { |
| 711 | void* ptr; |
| 712 | return posix_memalign(&ptr, sizeof(void *), bytes) == 0; |
| 713 | }), |
| 714 | testing::ExitedWithCode(0), ""); |
| 715 | EXPECT_EXIT(CheckAllocationFunction( |
| 716 | [](size_t bytes) { return aligned_alloc(sizeof(void*), bytes) != nullptr; }), |
| 717 | testing::ExitedWithCode(0), ""); |
| 718 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { |
| 719 | void* p = malloc(1024 * 1024); |
| 720 | return realloc(p, bytes) != nullptr; |
| 721 | }), |
| 722 | testing::ExitedWithCode(0), ""); |
| 723 | #if !defined(__LP64__) |
| 724 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return pvalloc(bytes) != nullptr; }), |
| 725 | testing::ExitedWithCode(0), ""); |
| 726 | EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return valloc(bytes) != nullptr; }), |
| 727 | testing::ExitedWithCode(0), ""); |
| 728 | #endif |
| 729 | #else |
| 730 | GTEST_LOG_(INFO) << "This tests a bionic extension.\n"; |
| 731 | #endif |
| 732 | } |
| 733 | |
| 734 | TEST(android_mallopt, set_allocation_limit_multiple) { |
| 735 | #if defined(__BIONIC__) |
| 736 | // Only the first set should work. |
| 737 | size_t limit = 256 * 1024 * 1024; |
| 738 | ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 739 | limit = 32 * 1024 * 1024; |
| 740 | ASSERT_FALSE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 741 | #else |
| 742 | GTEST_LOG_(INFO) << "This tests a bionic extension.\n"; |
| 743 | #endif |
| 744 | } |
| 745 | |
| 746 | #if defined(__BIONIC__) |
| 747 | static constexpr size_t kAllocationSize = 8 * 1024 * 1024; |
| 748 | |
| 749 | static size_t GetMaxAllocations() { |
| 750 | size_t max_pointers = 0; |
| 751 | void* ptrs[20]; |
| 752 | for (size_t i = 0; i < sizeof(ptrs) / sizeof(void*); i++) { |
| 753 | ptrs[i] = malloc(kAllocationSize); |
| 754 | if (ptrs[i] == nullptr) { |
| 755 | max_pointers = i; |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | for (size_t i = 0; i < max_pointers; i++) { |
| 760 | free(ptrs[i]); |
| 761 | } |
| 762 | return max_pointers; |
| 763 | } |
| 764 | |
| 765 | static void VerifyMaxPointers(size_t max_pointers) { |
| 766 | // Now verify that we can allocate the same number as before. |
| 767 | void* ptrs[20]; |
| 768 | for (size_t i = 0; i < max_pointers; i++) { |
| 769 | ptrs[i] = malloc(kAllocationSize); |
| 770 | ASSERT_TRUE(ptrs[i] != nullptr) << "Failed to allocate on iteration " << i; |
| 771 | } |
| 772 | |
| 773 | // Make sure the next allocation still fails. |
| 774 | ASSERT_TRUE(malloc(kAllocationSize) == nullptr); |
| 775 | for (size_t i = 0; i < max_pointers; i++) { |
| 776 | free(ptrs[i]); |
| 777 | } |
| 778 | } |
| 779 | #endif |
| 780 | |
| 781 | TEST(android_mallopt, set_allocation_limit_realloc_increase) { |
| 782 | #if defined(__BIONIC__) |
| 783 | size_t limit = 128 * 1024 * 1024; |
| 784 | ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 785 | |
| 786 | size_t max_pointers = GetMaxAllocations(); |
| 787 | ASSERT_TRUE(max_pointers != 0) << "Limit never reached."; |
| 788 | |
| 789 | void* memory = malloc(10 * 1024 * 1024); |
| 790 | ASSERT_TRUE(memory != nullptr); |
| 791 | |
| 792 | // Increase size. |
| 793 | memory = realloc(memory, 20 * 1024 * 1024); |
| 794 | ASSERT_TRUE(memory != nullptr); |
| 795 | memory = realloc(memory, 40 * 1024 * 1024); |
| 796 | ASSERT_TRUE(memory != nullptr); |
| 797 | memory = realloc(memory, 60 * 1024 * 1024); |
| 798 | ASSERT_TRUE(memory != nullptr); |
| 799 | memory = realloc(memory, 80 * 1024 * 1024); |
| 800 | ASSERT_TRUE(memory != nullptr); |
| 801 | // Now push past limit. |
| 802 | memory = realloc(memory, 130 * 1024 * 1024); |
| 803 | ASSERT_TRUE(memory == nullptr); |
| 804 | |
| 805 | VerifyMaxPointers(max_pointers); |
| 806 | #else |
| 807 | GTEST_LOG_(INFO) << "This tests a bionic extension.\n"; |
| 808 | #endif |
| 809 | } |
| 810 | |
| 811 | TEST(android_mallopt, set_allocation_limit_realloc_decrease) { |
| 812 | #if defined(__BIONIC__) |
| 813 | size_t limit = 100 * 1024 * 1024; |
| 814 | ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 815 | |
| 816 | size_t max_pointers = GetMaxAllocations(); |
| 817 | ASSERT_TRUE(max_pointers != 0) << "Limit never reached."; |
| 818 | |
| 819 | void* memory = malloc(80 * 1024 * 1024); |
| 820 | ASSERT_TRUE(memory != nullptr); |
| 821 | |
| 822 | // Decrease size. |
| 823 | memory = realloc(memory, 60 * 1024 * 1024); |
| 824 | ASSERT_TRUE(memory != nullptr); |
| 825 | memory = realloc(memory, 40 * 1024 * 1024); |
| 826 | ASSERT_TRUE(memory != nullptr); |
| 827 | memory = realloc(memory, 20 * 1024 * 1024); |
| 828 | ASSERT_TRUE(memory != nullptr); |
| 829 | memory = realloc(memory, 10 * 1024 * 1024); |
| 830 | ASSERT_TRUE(memory != nullptr); |
| 831 | free(memory); |
| 832 | |
| 833 | VerifyMaxPointers(max_pointers); |
| 834 | #else |
| 835 | GTEST_LOG_(INFO) << "This tests a bionic extension.\n"; |
| 836 | #endif |
| 837 | } |
| 838 | |
| 839 | TEST(android_mallopt, set_allocation_limit_realloc_free) { |
| 840 | #if defined(__BIONIC__) |
| 841 | size_t limit = 100 * 1024 * 1024; |
| 842 | ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))); |
| 843 | |
| 844 | size_t max_pointers = GetMaxAllocations(); |
| 845 | ASSERT_TRUE(max_pointers != 0) << "Limit never reached."; |
| 846 | |
| 847 | void* memory = malloc(60 * 1024 * 1024); |
| 848 | ASSERT_TRUE(memory != nullptr); |
| 849 | |
| 850 | memory = realloc(memory, 0); |
| 851 | ASSERT_TRUE(memory == nullptr); |
| 852 | |
| 853 | VerifyMaxPointers(max_pointers); |
| 854 | #else |
| 855 | GTEST_LOG_(INFO) << "This tests a bionic extension.\n"; |
| 856 | #endif |
| 857 | } |
| 858 | |
| 859 | #if defined(__BIONIC__) |
| 860 | static void* SetAllocationLimit(void* data) { |
| 861 | std::atomic_bool* go = reinterpret_cast<std::atomic_bool*>(data); |
| 862 | while (!go->load()) { |
| 863 | } |
| 864 | size_t limit = 500 * 1024 * 1024; |
| 865 | if (android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))) { |
| 866 | return reinterpret_cast<void*>(-1); |
| 867 | } |
| 868 | return nullptr; |
| 869 | } |
| 870 | |
| 871 | static void SetAllocationLimitMultipleThreads() { |
| 872 | std::atomic_bool go; |
| 873 | go = false; |
| 874 | |
| 875 | static constexpr size_t kNumThreads = 4; |
| 876 | pthread_t threads[kNumThreads]; |
| 877 | for (size_t i = 0; i < kNumThreads; i++) { |
| 878 | ASSERT_EQ(0, pthread_create(&threads[i], nullptr, SetAllocationLimit, &go)); |
| 879 | } |
| 880 | |
| 881 | // Let them go all at once. |
| 882 | go = true; |
| 883 | ASSERT_EQ(0, kill(getpid(), __SIGRTMIN + 4)); |
| 884 | |
| 885 | size_t num_successful = 0; |
| 886 | for (size_t i = 0; i < kNumThreads; i++) { |
| 887 | void* result; |
| 888 | ASSERT_EQ(0, pthread_join(threads[i], &result)); |
| 889 | if (result != nullptr) { |
| 890 | num_successful++; |
| 891 | } |
| 892 | } |
| 893 | ASSERT_EQ(1U, num_successful); |
| 894 | exit(0); |
| 895 | } |
| 896 | #endif |
| 897 | |
| 898 | TEST(android_mallopt, set_allocation_limit_multiple_threads) { |
| 899 | #if defined(__BIONIC__) |
| 900 | if (IsDynamic()) { |
| 901 | ASSERT_TRUE(android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0)); |
| 902 | } |
| 903 | |
| 904 | // Run this a number of times as a stress test. |
| 905 | for (size_t i = 0; i < 100; i++) { |
| 906 | // Not using ASSERT_EXIT because errors messages are not displayed. |
| 907 | pid_t pid; |
| 908 | if ((pid = fork()) == 0) { |
| 909 | ASSERT_NO_FATAL_FAILURE(SetAllocationLimitMultipleThreads()); |
| 910 | } |
| 911 | ASSERT_NE(-1, pid); |
| 912 | int status; |
| 913 | ASSERT_EQ(pid, wait(&status)); |
| 914 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 915 | } |
| 916 | #else |
| 917 | GTEST_LOG_(INFO) << "This tests a bionic extension.\n"; |
| 918 | #endif |
| 919 | } |