blob: 938068085e0f2ce9632caa544a6726c8269855e9 [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 Ferris63619642014-06-16 23:35:53 -070036#include "private/bionic_config.h"
Ryan Savitskiecc37e32018-12-14 15:57:21 +000037#include "private/bionic_malloc.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) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -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);
315
Christopher Ferrisa4037802014-06-09 19:14:11 -0700316TEST(malloc, pvalloc_std) {
317 size_t pagesize = sysconf(_SC_PAGESIZE);
318 void* ptr = pvalloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700319 ASSERT_TRUE(ptr != nullptr);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700320 ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0);
321 ASSERT_LE(pagesize, malloc_usable_size(ptr));
322 free(ptr);
323}
324
325TEST(malloc, pvalloc_overflow) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700326 ASSERT_EQ(nullptr, pvalloc(SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700327}
328
329TEST(malloc, valloc_std) {
330 size_t pagesize = sysconf(_SC_PAGESIZE);
331 void* ptr = pvalloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700332 ASSERT_TRUE(ptr != nullptr);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700333 ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0);
334 free(ptr);
335}
336
337TEST(malloc, valloc_overflow) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700338 ASSERT_EQ(nullptr, valloc(SIZE_MAX));
Christopher Ferris72bbd422014-05-08 11:14:03 -0700339}
Dan Alberte5fdaa42014-06-14 01:04:31 +0000340#endif
Dan Albert4caa1f02014-08-20 09:16:57 -0700341
342TEST(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 Ferris6c619a02019-03-01 17:59:51 -0800357 if (std::string(root->Attribute("version")) == "jemalloc-1") {
358 // Verify jemalloc version of this data.
359 ASSERT_STREQ("jemalloc-1", root->Attribute("version"));
Dan Albert4caa1f02014-08-20 09:16:57 -0700360
Christopher Ferris6c619a02019-03-01 17:59:51 -0800361 auto arena = root->FirstChildElement();
362 for (; arena != nullptr; arena = arena->NextSiblingElement()) {
363 int val;
Dan Albert4caa1f02014-08-20 09:16:57 -0700364
Christopher Ferris6c619a02019-03-01 17:59:51 -0800365 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 Albert4caa1f02014-08-20 09:16:57 -0700375
Christopher Ferris6c619a02019-03-01 17:59:51 -0800376 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 Albert4caa1f02014-08-20 09:16:57 -0700387 }
388 }
Christopher Ferris6c619a02019-03-01 17:59:51 -0800389 } 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 Albert4caa1f02014-08-20 09:16:57 -0700393 }
394#endif
395}
Christopher Ferrisad33ebe2015-12-16 12:07:25 -0800396
397TEST(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 Hughes884f76e2016-02-10 20:43:22 -0800415
416TEST(malloc, malloc_0) {
417 void* p = malloc(0);
418 ASSERT_TRUE(p != nullptr);
419 free(p);
420}
421
422TEST(malloc, calloc_0_0) {
423 void* p = calloc(0, 0);
424 ASSERT_TRUE(p != nullptr);
425 free(p);
426}
427
428TEST(malloc, calloc_0_1) {
429 void* p = calloc(0, 1);
430 ASSERT_TRUE(p != nullptr);
431 free(p);
432}
433
434TEST(malloc, calloc_1_0) {
435 void* p = calloc(1, 0);
436 ASSERT_TRUE(p != nullptr);
437 free(p);
438}
439
440TEST(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
447TEST(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 Ferris72df6702016-02-11 15:51:31 -0800454
455constexpr size_t MAX_LOOPS = 200;
456
457// Make sure that memory returned by malloc is aligned to allow these data types.
458TEST(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 Ferrisa1c0d2f2017-05-15 15:50:19 -0700524
525TEST(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 Hughesb1770852018-09-18 12:52:42 -0700531
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800532TEST(malloc, mallopt_decay) {
533#if defined(__BIONIC__)
Evgenii Stepanov7cc67062019-02-05 18:43:34 -0800534 SKIP_WITH_HWASAN; // hwasan does not implement mallopt
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800535 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
545TEST(malloc, mallopt_purge) {
546#if defined(__BIONIC__)
Evgenii Stepanov7cc67062019-02-05 18:43:34 -0800547 SKIP_WITH_HWASAN; // hwasan does not implement mallopt
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800548 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 Hughesb1770852018-09-18 12:52:42 -0700555TEST(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
573TEST(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 Ferris09a19aa2018-11-16 13:28:56 -0800582
583TEST(malloc, mallinfo) {
584#if defined(__BIONIC__)
Evgenii Stepanov7cc67062019-02-05 18:43:34 -0800585 SKIP_WITH_HWASAN; // hwasan does not implement mallinfo
Christopher Ferris09a19aa2018-11-16 13:28:56 -0800586 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 Ferris4e562282019-02-07 14:20:03 -0800607 // 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 Ferris09a19aa2018-11-16 13:28:56 -0800614 }
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 Savitskiecc37e32018-12-14 15:57:21 +0000627
628TEST(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 Ferrise4cdbc42019-02-08 17:30:58 -0800639bool 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 Savitskiecc37e32018-12-14 15:57:21 +0000658TEST(android_mallopt, init_zygote_child_profiling) {
659#if defined(__BIONIC__)
660 // Successful call.
661 errno = 0;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800662 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 Savitskiecc37e32018-12-14 15:57:21 +0000670
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 Ferrise4cdbc42019-02-08 17:30:58 -0800675 if (IsDynamic()) {
676 EXPECT_EQ(EINVAL, errno);
677 } else {
678 EXPECT_EQ(ENOTSUP, errno);
679 }
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000680#else
681 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
682#endif
683}
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800684
685#if defined(__BIONIC__)
686template <typename FuncType>
687void 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
699TEST(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
734TEST(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__)
747static constexpr size_t kAllocationSize = 8 * 1024 * 1024;
748
749static 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
765static 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
781TEST(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
811TEST(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
839TEST(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__)
860static 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
871static 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
898TEST(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}