blob: 0407553b2f5899fab47cd4508559390a28ca52b7 [file] [log] [blame]
Christopher Ferris885f3b92013-05-21 17:48:01 -07001/*
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 Ferrise4cdbc42019-02-08 17:30:58 -080019#include <elf.h>
Christopher Ferrisa4037802014-06-09 19:14:11 -070020#include <limits.h>
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080021#include <pthread.h>
Christopher Ferrisa4037802014-06-09 19:14:11 -070022#include <stdint.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080023#include <stdio.h>
Christopher Ferris885f3b92013-05-21 17:48:01 -070024#include <stdlib.h>
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080025#include <string.h>
26#include <sys/types.h>
27#include <sys/wait.h>
Christopher Ferris885f3b92013-05-21 17:48:01 -070028#include <malloc.h>
Christopher Ferrisa4037802014-06-09 19:14:11 -070029#include <unistd.h>
Christopher Ferris885f3b92013-05-21 17:48:01 -070030
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080031#include <atomic>
Dan Albert4caa1f02014-08-20 09:16:57 -070032#include <tinyxml2.h>
33
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080034#include <android-base/file.h>
35
Christopher Ferris2b0638e2019-09-11 19:05:29 -070036#include "platform/bionic/malloc.h"
Christopher Ferris63619642014-06-16 23:35:53 -070037#include "private/bionic_config.h"
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080038#include "utils.h"
Dan Alberte5fdaa42014-06-14 01:04:31 +000039
Elliott Hughesb1770852018-09-18 12:52:42 -070040#if defined(__BIONIC__)
41#define HAVE_REALLOCARRAY 1
42#else
43#define HAVE_REALLOCARRAY __GLIBC_PREREQ(2, 26)
44#endif
45
Christopher Ferris885f3b92013-05-21 17:48:01 -070046TEST(malloc, malloc_std) {
47 // Simple malloc test.
48 void *ptr = malloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -070049 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -070050 ASSERT_LE(100U, malloc_usable_size(ptr));
Christopher Ferris885f3b92013-05-21 17:48:01 -070051 free(ptr);
52}
53
Christopher Ferrisa4037802014-06-09 19:14:11 -070054TEST(malloc, malloc_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080055 SKIP_WITH_HWASAN;
Christopher Ferrisa4037802014-06-09 19:14:11 -070056 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070057 ASSERT_EQ(nullptr, malloc(SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -070058 ASSERT_EQ(ENOMEM, errno);
59}
60
Christopher Ferris885f3b92013-05-21 17:48:01 -070061TEST(malloc, calloc_std) {
62 // Simple calloc test.
63 size_t alloc_len = 100;
64 char *ptr = (char *)calloc(1, alloc_len);
Yi Kong32bc0fc2018-08-02 17:31:13 -070065 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -070066 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 Ferris885f3b92013-05-21 17:48:01 -070070 free(ptr);
71}
72
Christopher Ferrisa4037802014-06-09 19:14:11 -070073TEST(malloc, calloc_illegal) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080074 SKIP_WITH_HWASAN;
Christopher Ferrisa4037802014-06-09 19:14:11 -070075 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070076 ASSERT_EQ(nullptr, calloc(-1, 100));
Christopher Ferrisa4037802014-06-09 19:14:11 -070077 ASSERT_EQ(ENOMEM, errno);
78}
79
80TEST(malloc, calloc_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080081 SKIP_WITH_HWASAN;
Christopher Ferrisa4037802014-06-09 19:14:11 -070082 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070083 ASSERT_EQ(nullptr, calloc(1, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -070084 ASSERT_EQ(ENOMEM, errno);
85 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070086 ASSERT_EQ(nullptr, calloc(SIZE_MAX, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -070087 ASSERT_EQ(ENOMEM, errno);
88 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070089 ASSERT_EQ(nullptr, calloc(2, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -070090 ASSERT_EQ(ENOMEM, errno);
91 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070092 ASSERT_EQ(nullptr, calloc(SIZE_MAX, 2));
Christopher Ferrisa4037802014-06-09 19:14:11 -070093 ASSERT_EQ(ENOMEM, errno);
94}
95
Christopher Ferris885f3b92013-05-21 17:48:01 -070096TEST(malloc, memalign_multiple) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -080097 SKIP_WITH_HWASAN << "hwasan requires power of 2 alignment";
Christopher Ferris885f3b92013-05-21 17:48:01 -070098 // 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 Ferrisa4037802014-06-09 19:14:11 -0700101 char *ptr = reinterpret_cast<char*>(memalign(alignment, 100));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700102 ASSERT_TRUE(ptr != nullptr) << "Failed at alignment " << alignment;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700103 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 Ferris885f3b92013-05-21 17:48:01 -0700106 free(ptr);
107 }
108 }
109}
110
Christopher Ferrisa4037802014-06-09 19:14:11 -0700111TEST(malloc, memalign_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800112 SKIP_WITH_HWASAN;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700113 ASSERT_EQ(nullptr, memalign(4096, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700114}
115
116TEST(malloc, memalign_non_power2) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800117 SKIP_WITH_HWASAN;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700118 void* ptr;
119 for (size_t align = 0; align <= 256; align++) {
120 ptr = memalign(align, 1024);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700121 ASSERT_TRUE(ptr != nullptr) << "Failed at align " << align;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700122 free(ptr);
123 }
124}
125
Christopher Ferris885f3b92013-05-21 17:48:01 -0700126TEST(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 Kong32bc0fc2018-08-02 17:31:13 -0700130 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700131 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 Kong32bc0fc2018-08-02 17:31:13 -0700136 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700137 ASSERT_LE(200U, malloc_usable_size(ptr));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700138 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700139 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 Kong32bc0fc2018-08-02 17:31:13 -0700145 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700146 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 Kong32bc0fc2018-08-02 17:31:13 -0700153 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700154 ASSERT_LE(250U, malloc_usable_size(ptr));
155 for (size_t i = 0; i < 250; i++) {
156 ASSERT_EQ(0x67, ptr[i]);
157 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700158 free(ptr);
159 }
160}
161
162TEST(malloc, malloc_realloc_larger) {
163 // Realloc to a larger size, malloc is used for the original allocation.
164 char *ptr = (char *)malloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700165 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700166 ASSERT_LE(100U, malloc_usable_size(ptr));
167 memset(ptr, 67, 100);
168
169 ptr = (char *)realloc(ptr, 200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700170 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700171 ASSERT_LE(200U, malloc_usable_size(ptr));
172 for (size_t i = 0; i < 100; i++) {
173 ASSERT_EQ(67, ptr[i]);
174 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700175 free(ptr);
176}
177
178TEST(malloc, malloc_realloc_smaller) {
179 // Realloc to a smaller size, malloc is used for the original allocation.
180 char *ptr = (char *)malloc(200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700181 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700182 ASSERT_LE(200U, malloc_usable_size(ptr));
183 memset(ptr, 67, 200);
184
185 ptr = (char *)realloc(ptr, 100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700186 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700187 ASSERT_LE(100U, malloc_usable_size(ptr));
188 for (size_t i = 0; i < 100; i++) {
189 ASSERT_EQ(67, ptr[i]);
190 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700191 free(ptr);
192}
193
194TEST(malloc, malloc_multiple_realloc) {
195 // Multiple reallocs, malloc is used for the original allocation.
196 char *ptr = (char *)malloc(200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700197 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700198 ASSERT_LE(200U, malloc_usable_size(ptr));
199 memset(ptr, 0x23, 200);
200
201 ptr = (char *)realloc(ptr, 100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700202 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700203 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 Kong32bc0fc2018-08-02 17:31:13 -0700209 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700210 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 Kong32bc0fc2018-08-02 17:31:13 -0700216 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700217 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 Kong32bc0fc2018-08-02 17:31:13 -0700224 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700225 ASSERT_LE(425U, malloc_usable_size(ptr));
226 for (size_t i = 0; i < 150; i++) {
227 ASSERT_EQ(0x23, ptr[i]);
228 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700229 free(ptr);
230}
Christopher Ferrisa4037802014-06-09 19:14:11 -0700231
Christopher Ferris885f3b92013-05-21 17:48:01 -0700232TEST(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 Kong32bc0fc2018-08-02 17:31:13 -0700235 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700236 ASSERT_LE(100U, malloc_usable_size(ptr));
237
238 ptr = (char *)realloc(ptr, 200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700239 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700240 ASSERT_LE(200U, malloc_usable_size(ptr));
241 for (size_t i = 0; i < 100; i++) {
242 ASSERT_EQ(0, ptr[i]);
243 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700244 free(ptr);
245}
246
247TEST(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 Kong32bc0fc2018-08-02 17:31:13 -0700250 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700251 ASSERT_LE(200U, malloc_usable_size(ptr));
252
253 ptr = (char *)realloc(ptr, 100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700254 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700255 ASSERT_LE(100U, malloc_usable_size(ptr));
256 for (size_t i = 0; i < 100; i++) {
257 ASSERT_EQ(0, ptr[i]);
258 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700259 free(ptr);
260}
261
262TEST(malloc, calloc_multiple_realloc) {
263 // Multiple reallocs, calloc is used for the original allocation.
264 char *ptr = (char *)calloc(1, 200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700265 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700266 ASSERT_LE(200U, malloc_usable_size(ptr));
267
268 ptr = (char *)realloc(ptr, 100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700269 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700270 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 Kong32bc0fc2018-08-02 17:31:13 -0700276 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700277 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 Kong32bc0fc2018-08-02 17:31:13 -0700283 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700284 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 Kong32bc0fc2018-08-02 17:31:13 -0700291 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700292 ASSERT_LE(425U, malloc_usable_size(ptr));
293 for (size_t i = 0; i < 150; i++) {
294 ASSERT_EQ(0, ptr[i]);
295 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700296 free(ptr);
297}
Christopher Ferris72bbd422014-05-08 11:14:03 -0700298
Christopher Ferrisa4037802014-06-09 19:14:11 -0700299TEST(malloc, realloc_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800300 SKIP_WITH_HWASAN;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700301 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700302 ASSERT_EQ(nullptr, realloc(nullptr, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700303 ASSERT_EQ(ENOMEM, errno);
304 void* ptr = malloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700305 ASSERT_TRUE(ptr != nullptr);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700306 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700307 ASSERT_EQ(nullptr, realloc(ptr, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700308 ASSERT_EQ(ENOMEM, errno);
309 free(ptr);
Christopher Ferris72bbd422014-05-08 11:14:03 -0700310}
311
Dan Alberte5fdaa42014-06-14 01:04:31 +0000312#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
313extern "C" void* pvalloc(size_t);
314extern "C" void* valloc(size_t);
Christopher Ferris804cebe2019-06-20 08:50:23 -0700315#endif
Dan Alberte5fdaa42014-06-14 01:04:31 +0000316
Christopher Ferrisa4037802014-06-09 19:14:11 -0700317TEST(malloc, pvalloc_std) {
Christopher Ferris804cebe2019-06-20 08:50:23 -0700318#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700319 size_t pagesize = sysconf(_SC_PAGESIZE);
320 void* ptr = pvalloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700321 ASSERT_TRUE(ptr != nullptr);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700322 ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0);
323 ASSERT_LE(pagesize, malloc_usable_size(ptr));
324 free(ptr);
Christopher Ferris804cebe2019-06-20 08:50:23 -0700325#else
326 GTEST_SKIP() << "pvalloc not supported.";
327#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700328}
329
330TEST(malloc, pvalloc_overflow) {
Christopher Ferris804cebe2019-06-20 08:50:23 -0700331#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700332 ASSERT_EQ(nullptr, pvalloc(SIZE_MAX));
Christopher Ferris804cebe2019-06-20 08:50:23 -0700333#else
334 GTEST_SKIP() << "pvalloc not supported.";
335#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700336}
337
338TEST(malloc, valloc_std) {
Christopher Ferris804cebe2019-06-20 08:50:23 -0700339#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700340 size_t pagesize = sysconf(_SC_PAGESIZE);
Christopher Ferrisd5ab0a52019-06-19 12:03:57 -0700341 void* ptr = valloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700342 ASSERT_TRUE(ptr != nullptr);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700343 ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0);
344 free(ptr);
Christopher Ferris804cebe2019-06-20 08:50:23 -0700345#else
346 GTEST_SKIP() << "valloc not supported.";
347#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700348}
349
350TEST(malloc, valloc_overflow) {
Christopher Ferris804cebe2019-06-20 08:50:23 -0700351#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700352 ASSERT_EQ(nullptr, valloc(SIZE_MAX));
Christopher Ferris804cebe2019-06-20 08:50:23 -0700353#else
354 GTEST_SKIP() << "valloc not supported.";
Dan Alberte5fdaa42014-06-14 01:04:31 +0000355#endif
Christopher Ferris804cebe2019-06-20 08:50:23 -0700356}
Dan Albert4caa1f02014-08-20 09:16:57 -0700357
358TEST(malloc, malloc_info) {
359#ifdef __BIONIC__
Evgenii Stepanov8de6b462019-03-22 13:22:28 -0700360 SKIP_WITH_HWASAN; // hwasan does not implement malloc_info
Dan Albert4caa1f02014-08-20 09:16:57 -0700361 char* buf;
362 size_t bufsize;
363 FILE* memstream = open_memstream(&buf, &bufsize);
364 ASSERT_NE(nullptr, memstream);
365 ASSERT_EQ(0, malloc_info(0, memstream));
366 ASSERT_EQ(0, fclose(memstream));
367
368 tinyxml2::XMLDocument doc;
369 ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(buf));
370
371 auto root = doc.FirstChildElement();
372 ASSERT_NE(nullptr, root);
373 ASSERT_STREQ("malloc", root->Name());
Christopher Ferris85169652019-10-09 18:41:55 -0700374 std::string version(root->Attribute("version"));
375 if (version == "jemalloc-1") {
Christopher Ferris6c619a02019-03-01 17:59:51 -0800376 // Verify jemalloc version of this data.
377 ASSERT_STREQ("jemalloc-1", root->Attribute("version"));
Dan Albert4caa1f02014-08-20 09:16:57 -0700378
Christopher Ferris6c619a02019-03-01 17:59:51 -0800379 auto arena = root->FirstChildElement();
380 for (; arena != nullptr; arena = arena->NextSiblingElement()) {
381 int val;
Dan Albert4caa1f02014-08-20 09:16:57 -0700382
Christopher Ferris6c619a02019-03-01 17:59:51 -0800383 ASSERT_STREQ("heap", arena->Name());
384 ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val));
385 ASSERT_EQ(tinyxml2::XML_SUCCESS,
386 arena->FirstChildElement("allocated-large")->QueryIntText(&val));
387 ASSERT_EQ(tinyxml2::XML_SUCCESS,
388 arena->FirstChildElement("allocated-huge")->QueryIntText(&val));
389 ASSERT_EQ(tinyxml2::XML_SUCCESS,
390 arena->FirstChildElement("allocated-bins")->QueryIntText(&val));
391 ASSERT_EQ(tinyxml2::XML_SUCCESS,
392 arena->FirstChildElement("bins-total")->QueryIntText(&val));
Dan Albert4caa1f02014-08-20 09:16:57 -0700393
Christopher Ferris6c619a02019-03-01 17:59:51 -0800394 auto bin = arena->FirstChildElement("bin");
395 for (; bin != nullptr; bin = bin ->NextSiblingElement()) {
396 if (strcmp(bin->Name(), "bin") == 0) {
397 ASSERT_EQ(tinyxml2::XML_SUCCESS, bin->QueryIntAttribute("nr", &val));
398 ASSERT_EQ(tinyxml2::XML_SUCCESS,
399 bin->FirstChildElement("allocated")->QueryIntText(&val));
400 ASSERT_EQ(tinyxml2::XML_SUCCESS,
401 bin->FirstChildElement("nmalloc")->QueryIntText(&val));
402 ASSERT_EQ(tinyxml2::XML_SUCCESS,
403 bin->FirstChildElement("ndalloc")->QueryIntText(&val));
404 }
Dan Albert4caa1f02014-08-20 09:16:57 -0700405 }
406 }
Christopher Ferris6c619a02019-03-01 17:59:51 -0800407 } else {
Christopher Ferris85169652019-10-09 18:41:55 -0700408 // Do not verify output for scudo or debug malloc.
409 ASSERT_TRUE(version == "scudo-1" || version == "debug-malloc-1")
410 << "Unknown version: " << version;
Dan Albert4caa1f02014-08-20 09:16:57 -0700411 }
412#endif
413}
Christopher Ferrisad33ebe2015-12-16 12:07:25 -0800414
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700415TEST(malloc, malloc_info_matches_mallinfo) {
416#ifdef __BIONIC__
417 SKIP_WITH_HWASAN; // hwasan does not implement malloc_info
418
419 char* buf;
420 size_t bufsize;
421 FILE* memstream = open_memstream(&buf, &bufsize);
422 ASSERT_NE(nullptr, memstream);
423 size_t mallinfo_before_allocated_bytes = mallinfo().uordblks;
424 ASSERT_EQ(0, malloc_info(0, memstream));
425 size_t mallinfo_after_allocated_bytes = mallinfo().uordblks;
426 ASSERT_EQ(0, fclose(memstream));
427
428 tinyxml2::XMLDocument doc;
429 ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(buf));
430
431 size_t total_allocated_bytes = 0;
432 auto root = doc.FirstChildElement();
433 ASSERT_NE(nullptr, root);
434 ASSERT_STREQ("malloc", root->Name());
Christopher Ferris85169652019-10-09 18:41:55 -0700435 std::string version(root->Attribute("version"));
436 if (version == "jemalloc-1") {
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700437 // Verify jemalloc version of this data.
438 ASSERT_STREQ("jemalloc-1", root->Attribute("version"));
439
440 auto arena = root->FirstChildElement();
441 for (; arena != nullptr; arena = arena->NextSiblingElement()) {
442 int val;
443
444 ASSERT_STREQ("heap", arena->Name());
445 ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val));
446 ASSERT_EQ(tinyxml2::XML_SUCCESS,
447 arena->FirstChildElement("allocated-large")->QueryIntText(&val));
448 total_allocated_bytes += val;
449 ASSERT_EQ(tinyxml2::XML_SUCCESS,
450 arena->FirstChildElement("allocated-huge")->QueryIntText(&val));
451 total_allocated_bytes += val;
452 ASSERT_EQ(tinyxml2::XML_SUCCESS,
453 arena->FirstChildElement("allocated-bins")->QueryIntText(&val));
454 total_allocated_bytes += val;
455 ASSERT_EQ(tinyxml2::XML_SUCCESS,
456 arena->FirstChildElement("bins-total")->QueryIntText(&val));
457 }
458 // The total needs to be between the mallinfo call before and after
459 // since malloc_info allocates some memory.
460 EXPECT_LE(mallinfo_before_allocated_bytes, total_allocated_bytes);
461 EXPECT_GE(mallinfo_after_allocated_bytes, total_allocated_bytes);
462 } else {
Christopher Ferris85169652019-10-09 18:41:55 -0700463 // Do not verify output for scudo or debug malloc.
464 ASSERT_TRUE(version == "scudo-1" || version == "debug-malloc-1")
465 << "Unknown version: " << version;
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700466 }
467#endif
468}
469
Christopher Ferrisad33ebe2015-12-16 12:07:25 -0800470TEST(malloc, calloc_usable_size) {
471 for (size_t size = 1; size <= 2048; size++) {
472 void* pointer = malloc(size);
473 ASSERT_TRUE(pointer != nullptr);
474 memset(pointer, 0xeb, malloc_usable_size(pointer));
475 free(pointer);
476
477 // We should get a previous pointer that has been set to non-zero.
478 // If calloc does not zero out all of the data, this will fail.
479 uint8_t* zero_mem = reinterpret_cast<uint8_t*>(calloc(1, size));
480 ASSERT_TRUE(pointer != nullptr);
481 size_t usable_size = malloc_usable_size(zero_mem);
482 for (size_t i = 0; i < usable_size; i++) {
483 ASSERT_EQ(0, zero_mem[i]) << "Failed at allocation size " << size << " at byte " << i;
484 }
485 free(zero_mem);
486 }
487}
Elliott Hughes884f76e2016-02-10 20:43:22 -0800488
489TEST(malloc, malloc_0) {
490 void* p = malloc(0);
491 ASSERT_TRUE(p != nullptr);
492 free(p);
493}
494
495TEST(malloc, calloc_0_0) {
496 void* p = calloc(0, 0);
497 ASSERT_TRUE(p != nullptr);
498 free(p);
499}
500
501TEST(malloc, calloc_0_1) {
502 void* p = calloc(0, 1);
503 ASSERT_TRUE(p != nullptr);
504 free(p);
505}
506
507TEST(malloc, calloc_1_0) {
508 void* p = calloc(1, 0);
509 ASSERT_TRUE(p != nullptr);
510 free(p);
511}
512
513TEST(malloc, realloc_nullptr_0) {
514 // realloc(nullptr, size) is actually malloc(size).
515 void* p = realloc(nullptr, 0);
516 ASSERT_TRUE(p != nullptr);
517 free(p);
518}
519
520TEST(malloc, realloc_0) {
521 void* p = malloc(1024);
522 ASSERT_TRUE(p != nullptr);
523 // realloc(p, 0) is actually free(p).
524 void* p2 = realloc(p, 0);
525 ASSERT_TRUE(p2 == nullptr);
526}
Christopher Ferris72df6702016-02-11 15:51:31 -0800527
528constexpr size_t MAX_LOOPS = 200;
529
530// Make sure that memory returned by malloc is aligned to allow these data types.
531TEST(malloc, verify_alignment) {
532 uint32_t** values_32 = new uint32_t*[MAX_LOOPS];
533 uint64_t** values_64 = new uint64_t*[MAX_LOOPS];
534 long double** values_ldouble = new long double*[MAX_LOOPS];
535 // Use filler to attempt to force the allocator to get potentially bad alignments.
536 void** filler = new void*[MAX_LOOPS];
537
538 for (size_t i = 0; i < MAX_LOOPS; i++) {
539 // Check uint32_t pointers.
540 filler[i] = malloc(1);
541 ASSERT_TRUE(filler[i] != nullptr);
542
543 values_32[i] = reinterpret_cast<uint32_t*>(malloc(sizeof(uint32_t)));
544 ASSERT_TRUE(values_32[i] != nullptr);
545 *values_32[i] = i;
546 ASSERT_EQ(*values_32[i], i);
547 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_32[i]) & (sizeof(uint32_t) - 1));
548
549 free(filler[i]);
550 }
551
552 for (size_t i = 0; i < MAX_LOOPS; i++) {
553 // Check uint64_t pointers.
554 filler[i] = malloc(1);
555 ASSERT_TRUE(filler[i] != nullptr);
556
557 values_64[i] = reinterpret_cast<uint64_t*>(malloc(sizeof(uint64_t)));
558 ASSERT_TRUE(values_64[i] != nullptr);
559 *values_64[i] = 0x1000 + i;
560 ASSERT_EQ(*values_64[i], 0x1000 + i);
561 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_64[i]) & (sizeof(uint64_t) - 1));
562
563 free(filler[i]);
564 }
565
566 for (size_t i = 0; i < MAX_LOOPS; i++) {
567 // Check long double pointers.
568 filler[i] = malloc(1);
569 ASSERT_TRUE(filler[i] != nullptr);
570
571 values_ldouble[i] = reinterpret_cast<long double*>(malloc(sizeof(long double)));
572 ASSERT_TRUE(values_ldouble[i] != nullptr);
573 *values_ldouble[i] = 5.5 + i;
574 ASSERT_DOUBLE_EQ(*values_ldouble[i], 5.5 + i);
575 // 32 bit glibc has a long double size of 12 bytes, so hardcode the
576 // required alignment to 0x7.
577#if !defined(__BIONIC__) && !defined(__LP64__)
578 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_ldouble[i]) & 0x7);
579#else
580 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_ldouble[i]) & (sizeof(long double) - 1));
581#endif
582
583 free(filler[i]);
584 }
585
586 for (size_t i = 0; i < MAX_LOOPS; i++) {
587 free(values_32[i]);
588 free(values_64[i]);
589 free(values_ldouble[i]);
590 }
591
592 delete[] filler;
593 delete[] values_32;
594 delete[] values_64;
595 delete[] values_ldouble;
596}
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700597
598TEST(malloc, mallopt_smoke) {
599 errno = 0;
600 ASSERT_EQ(0, mallopt(-1000, 1));
601 // mallopt doesn't set errno.
602 ASSERT_EQ(0, errno);
603}
Elliott Hughesb1770852018-09-18 12:52:42 -0700604
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800605TEST(malloc, mallopt_decay) {
606#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800607 SKIP_WITH_HWASAN << "hwasan does not implement mallopt";
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800608 errno = 0;
609 ASSERT_EQ(1, mallopt(M_DECAY_TIME, 1));
610 ASSERT_EQ(1, mallopt(M_DECAY_TIME, 0));
611 ASSERT_EQ(1, mallopt(M_DECAY_TIME, 1));
612 ASSERT_EQ(1, mallopt(M_DECAY_TIME, 0));
613#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800614 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800615#endif
616}
617
618TEST(malloc, mallopt_purge) {
619#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800620 SKIP_WITH_HWASAN << "hwasan does not implement mallopt";
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800621 errno = 0;
622 ASSERT_EQ(1, mallopt(M_PURGE, 0));
623#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800624 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800625#endif
626}
627
Elliott Hughesb1770852018-09-18 12:52:42 -0700628TEST(malloc, reallocarray_overflow) {
629#if HAVE_REALLOCARRAY
630 // Values that cause overflow to a result small enough (8 on LP64) that malloc would "succeed".
631 size_t a = static_cast<size_t>(INTPTR_MIN + 4);
632 size_t b = 2;
633
634 errno = 0;
635 ASSERT_TRUE(reallocarray(nullptr, a, b) == nullptr);
636 ASSERT_EQ(ENOMEM, errno);
637
638 errno = 0;
639 ASSERT_TRUE(reallocarray(nullptr, b, a) == nullptr);
640 ASSERT_EQ(ENOMEM, errno);
641#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800642 GTEST_SKIP() << "reallocarray not available";
Elliott Hughesb1770852018-09-18 12:52:42 -0700643#endif
644}
645
646TEST(malloc, reallocarray) {
647#if HAVE_REALLOCARRAY
648 void* p = reallocarray(nullptr, 2, 32);
649 ASSERT_TRUE(p != nullptr);
650 ASSERT_GE(malloc_usable_size(p), 64U);
651#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800652 GTEST_SKIP() << "reallocarray not available";
Elliott Hughesb1770852018-09-18 12:52:42 -0700653#endif
654}
Christopher Ferris09a19aa2018-11-16 13:28:56 -0800655
656TEST(malloc, mallinfo) {
657#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800658 SKIP_WITH_HWASAN << "hwasan does not implement mallinfo";
Christopher Ferris09a19aa2018-11-16 13:28:56 -0800659 static size_t sizes[] = {
660 8, 32, 128, 4096, 32768, 131072, 1024000, 10240000, 20480000, 300000000
661 };
662
663 constexpr static size_t kMaxAllocs = 50;
664
665 for (size_t size : sizes) {
666 // If some of these allocations are stuck in a thread cache, then keep
667 // looping until we make an allocation that changes the total size of the
668 // memory allocated.
669 // jemalloc implementations counts the thread cache allocations against
670 // total memory allocated.
671 void* ptrs[kMaxAllocs] = {};
672 bool pass = false;
673 for (size_t i = 0; i < kMaxAllocs; i++) {
674 size_t allocated = mallinfo().uordblks;
675 ptrs[i] = malloc(size);
676 ASSERT_TRUE(ptrs[i] != nullptr);
677 size_t new_allocated = mallinfo().uordblks;
678 if (allocated != new_allocated) {
679 size_t usable_size = malloc_usable_size(ptrs[i]);
Christopher Ferris4e562282019-02-07 14:20:03 -0800680 // Only check if the total got bigger by at least allocation size.
681 // Sometimes the mallinfo numbers can go backwards due to compaction
682 // and/or freeing of cached data.
683 if (new_allocated >= allocated + usable_size) {
684 pass = true;
685 break;
686 }
Christopher Ferris09a19aa2018-11-16 13:28:56 -0800687 }
688 }
689 for (void* ptr : ptrs) {
690 free(ptr);
691 }
692 ASSERT_TRUE(pass)
693 << "For size " << size << " allocated bytes did not increase after "
694 << kMaxAllocs << " allocations.";
695 }
696#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800697 GTEST_SKIP() << "glibc is broken";
Christopher Ferris09a19aa2018-11-16 13:28:56 -0800698#endif
699}
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000700
701TEST(android_mallopt, error_on_unexpected_option) {
702#if defined(__BIONIC__)
703 const int unrecognized_option = -1;
704 errno = 0;
705 EXPECT_EQ(false, android_mallopt(unrecognized_option, nullptr, 0));
706 EXPECT_EQ(ENOTSUP, errno);
707#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800708 GTEST_SKIP() << "bionic-only test";
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000709#endif
710}
711
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800712bool IsDynamic() {
713#if defined(__LP64__)
714 Elf64_Ehdr ehdr;
715#else
716 Elf32_Ehdr ehdr;
717#endif
718 std::string path(android::base::GetExecutablePath());
719
720 int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
721 if (fd == -1) {
722 // Assume dynamic on error.
723 return true;
724 }
725 bool read_completed = android::base::ReadFully(fd, &ehdr, sizeof(ehdr));
726 close(fd);
727 // Assume dynamic in error cases.
728 return !read_completed || ehdr.e_type == ET_DYN;
729}
730
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000731TEST(android_mallopt, init_zygote_child_profiling) {
732#if defined(__BIONIC__)
733 // Successful call.
734 errno = 0;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800735 if (IsDynamic()) {
736 EXPECT_EQ(true, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0));
737 EXPECT_EQ(0, errno);
738 } else {
739 // Not supported in static executables.
740 EXPECT_EQ(false, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0));
741 EXPECT_EQ(ENOTSUP, errno);
742 }
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000743
744 // Unexpected arguments rejected.
745 errno = 0;
746 char unexpected = 0;
747 EXPECT_EQ(false, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, &unexpected, 1));
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800748 if (IsDynamic()) {
749 EXPECT_EQ(EINVAL, errno);
750 } else {
751 EXPECT_EQ(ENOTSUP, errno);
752 }
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000753#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800754 GTEST_SKIP() << "bionic-only test";
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000755#endif
756}
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800757
758#if defined(__BIONIC__)
759template <typename FuncType>
760void CheckAllocationFunction(FuncType func) {
761 // Assumes that no more than 108MB of memory is allocated before this.
762 size_t limit = 128 * 1024 * 1024;
763 ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
764 if (!func(20 * 1024 * 1024))
765 exit(1);
766 if (func(128 * 1024 * 1024))
767 exit(1);
768 exit(0);
769}
770#endif
771
772TEST(android_mallopt, set_allocation_limit) {
773#if defined(__BIONIC__)
774 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return calloc(bytes, 1) != nullptr; }),
775 testing::ExitedWithCode(0), "");
776 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return calloc(1, bytes) != nullptr; }),
777 testing::ExitedWithCode(0), "");
778 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return malloc(bytes) != nullptr; }),
779 testing::ExitedWithCode(0), "");
780 EXPECT_EXIT(CheckAllocationFunction(
781 [](size_t bytes) { return memalign(sizeof(void*), bytes) != nullptr; }),
782 testing::ExitedWithCode(0), "");
783 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) {
784 void* ptr;
785 return posix_memalign(&ptr, sizeof(void *), bytes) == 0;
786 }),
787 testing::ExitedWithCode(0), "");
788 EXPECT_EXIT(CheckAllocationFunction(
789 [](size_t bytes) { return aligned_alloc(sizeof(void*), bytes) != nullptr; }),
790 testing::ExitedWithCode(0), "");
791 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) {
792 void* p = malloc(1024 * 1024);
793 return realloc(p, bytes) != nullptr;
794 }),
795 testing::ExitedWithCode(0), "");
796#if !defined(__LP64__)
797 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return pvalloc(bytes) != nullptr; }),
798 testing::ExitedWithCode(0), "");
799 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return valloc(bytes) != nullptr; }),
800 testing::ExitedWithCode(0), "");
801#endif
802#else
Elliott Hughes10907202019-03-27 08:51:02 -0700803 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800804#endif
805}
806
807TEST(android_mallopt, set_allocation_limit_multiple) {
808#if defined(__BIONIC__)
809 // Only the first set should work.
810 size_t limit = 256 * 1024 * 1024;
811 ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
812 limit = 32 * 1024 * 1024;
813 ASSERT_FALSE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
814#else
Elliott Hughes10907202019-03-27 08:51:02 -0700815 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800816#endif
817}
818
819#if defined(__BIONIC__)
820static constexpr size_t kAllocationSize = 8 * 1024 * 1024;
821
822static size_t GetMaxAllocations() {
823 size_t max_pointers = 0;
824 void* ptrs[20];
825 for (size_t i = 0; i < sizeof(ptrs) / sizeof(void*); i++) {
826 ptrs[i] = malloc(kAllocationSize);
827 if (ptrs[i] == nullptr) {
828 max_pointers = i;
829 break;
830 }
831 }
832 for (size_t i = 0; i < max_pointers; i++) {
833 free(ptrs[i]);
834 }
835 return max_pointers;
836}
837
838static void VerifyMaxPointers(size_t max_pointers) {
839 // Now verify that we can allocate the same number as before.
840 void* ptrs[20];
841 for (size_t i = 0; i < max_pointers; i++) {
842 ptrs[i] = malloc(kAllocationSize);
843 ASSERT_TRUE(ptrs[i] != nullptr) << "Failed to allocate on iteration " << i;
844 }
845
846 // Make sure the next allocation still fails.
847 ASSERT_TRUE(malloc(kAllocationSize) == nullptr);
848 for (size_t i = 0; i < max_pointers; i++) {
849 free(ptrs[i]);
850 }
851}
852#endif
853
854TEST(android_mallopt, set_allocation_limit_realloc_increase) {
855#if defined(__BIONIC__)
856 size_t limit = 128 * 1024 * 1024;
857 ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
858
859 size_t max_pointers = GetMaxAllocations();
860 ASSERT_TRUE(max_pointers != 0) << "Limit never reached.";
861
862 void* memory = malloc(10 * 1024 * 1024);
863 ASSERT_TRUE(memory != nullptr);
864
865 // Increase size.
866 memory = realloc(memory, 20 * 1024 * 1024);
867 ASSERT_TRUE(memory != nullptr);
868 memory = realloc(memory, 40 * 1024 * 1024);
869 ASSERT_TRUE(memory != nullptr);
870 memory = realloc(memory, 60 * 1024 * 1024);
871 ASSERT_TRUE(memory != nullptr);
872 memory = realloc(memory, 80 * 1024 * 1024);
873 ASSERT_TRUE(memory != nullptr);
874 // Now push past limit.
875 memory = realloc(memory, 130 * 1024 * 1024);
876 ASSERT_TRUE(memory == nullptr);
877
878 VerifyMaxPointers(max_pointers);
879#else
Elliott Hughes10907202019-03-27 08:51:02 -0700880 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800881#endif
882}
883
884TEST(android_mallopt, set_allocation_limit_realloc_decrease) {
885#if defined(__BIONIC__)
886 size_t limit = 100 * 1024 * 1024;
887 ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
888
889 size_t max_pointers = GetMaxAllocations();
890 ASSERT_TRUE(max_pointers != 0) << "Limit never reached.";
891
892 void* memory = malloc(80 * 1024 * 1024);
893 ASSERT_TRUE(memory != nullptr);
894
895 // Decrease size.
896 memory = realloc(memory, 60 * 1024 * 1024);
897 ASSERT_TRUE(memory != nullptr);
898 memory = realloc(memory, 40 * 1024 * 1024);
899 ASSERT_TRUE(memory != nullptr);
900 memory = realloc(memory, 20 * 1024 * 1024);
901 ASSERT_TRUE(memory != nullptr);
902 memory = realloc(memory, 10 * 1024 * 1024);
903 ASSERT_TRUE(memory != nullptr);
904 free(memory);
905
906 VerifyMaxPointers(max_pointers);
907#else
Elliott Hughes10907202019-03-27 08:51:02 -0700908 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800909#endif
910}
911
912TEST(android_mallopt, set_allocation_limit_realloc_free) {
913#if defined(__BIONIC__)
914 size_t limit = 100 * 1024 * 1024;
915 ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
916
917 size_t max_pointers = GetMaxAllocations();
918 ASSERT_TRUE(max_pointers != 0) << "Limit never reached.";
919
920 void* memory = malloc(60 * 1024 * 1024);
921 ASSERT_TRUE(memory != nullptr);
922
923 memory = realloc(memory, 0);
924 ASSERT_TRUE(memory == nullptr);
925
926 VerifyMaxPointers(max_pointers);
927#else
Elliott Hughes10907202019-03-27 08:51:02 -0700928 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800929#endif
930}
931
932#if defined(__BIONIC__)
933static void* SetAllocationLimit(void* data) {
934 std::atomic_bool* go = reinterpret_cast<std::atomic_bool*>(data);
935 while (!go->load()) {
936 }
937 size_t limit = 500 * 1024 * 1024;
938 if (android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))) {
939 return reinterpret_cast<void*>(-1);
940 }
941 return nullptr;
942}
943
944static void SetAllocationLimitMultipleThreads() {
945 std::atomic_bool go;
946 go = false;
947
948 static constexpr size_t kNumThreads = 4;
949 pthread_t threads[kNumThreads];
950 for (size_t i = 0; i < kNumThreads; i++) {
951 ASSERT_EQ(0, pthread_create(&threads[i], nullptr, SetAllocationLimit, &go));
952 }
953
954 // Let them go all at once.
955 go = true;
956 ASSERT_EQ(0, kill(getpid(), __SIGRTMIN + 4));
957
958 size_t num_successful = 0;
959 for (size_t i = 0; i < kNumThreads; i++) {
960 void* result;
961 ASSERT_EQ(0, pthread_join(threads[i], &result));
962 if (result != nullptr) {
963 num_successful++;
964 }
965 }
966 ASSERT_EQ(1U, num_successful);
967 exit(0);
968}
969#endif
970
971TEST(android_mallopt, set_allocation_limit_multiple_threads) {
972#if defined(__BIONIC__)
973 if (IsDynamic()) {
974 ASSERT_TRUE(android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0));
975 }
976
977 // Run this a number of times as a stress test.
978 for (size_t i = 0; i < 100; i++) {
979 // Not using ASSERT_EXIT because errors messages are not displayed.
980 pid_t pid;
981 if ((pid = fork()) == 0) {
982 ASSERT_NO_FATAL_FAILURE(SetAllocationLimitMultipleThreads());
983 }
984 ASSERT_NE(-1, pid);
985 int status;
986 ASSERT_EQ(pid, wait(&status));
987 ASSERT_EQ(0, WEXITSTATUS(status));
988 }
989#else
Elliott Hughes10907202019-03-27 08:51:02 -0700990 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800991#endif
992}