blob: eae44ceef17152d60b36a5131dda8bc073e254b0 [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>
Ryan Savitski175c8862020-01-02 19:54:57 +000021#include <malloc.h>
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080022#include <pthread.h>
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070023#include <semaphore.h>
Ryan Savitski175c8862020-01-02 19:54:57 +000024#include <signal.h>
Christopher Ferrisa4037802014-06-09 19:14:11 -070025#include <stdint.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080026#include <stdio.h>
Christopher Ferris885f3b92013-05-21 17:48:01 -070027#include <stdlib.h>
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080028#include <string.h>
Peter Collingbourne45819dd2020-01-09 11:00:43 -080029#include <sys/auxv.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070030#include <sys/cdefs.h>
Peter Collingbourne45819dd2020-01-09 11:00:43 -080031#include <sys/prctl.h>
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080032#include <sys/types.h>
33#include <sys/wait.h>
Christopher Ferrisa4037802014-06-09 19:14:11 -070034#include <unistd.h>
Christopher Ferris885f3b92013-05-21 17:48:01 -070035
Mitch Phillips9cad8422021-01-20 16:03:27 -080036#include <algorithm>
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080037#include <atomic>
Christopher Ferrisf32494c2020-01-08 14:19:10 -080038#include <thread>
Mitch Phillips9cad8422021-01-20 16:03:27 -080039#include <vector>
Christopher Ferrisf32494c2020-01-08 14:19:10 -080040
Dan Albert4caa1f02014-08-20 09:16:57 -070041#include <tinyxml2.h>
42
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080043#include <android-base/file.h>
44
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080045#include "utils.h"
Dan Alberte5fdaa42014-06-14 01:04:31 +000046
Elliott Hughesb1770852018-09-18 12:52:42 -070047#if defined(__BIONIC__)
Christopher Ferrisb874c332020-01-21 16:39:05 -080048
Peter Collingbourne45819dd2020-01-09 11:00:43 -080049#include "SignalUtils.h"
Peter Collingbourne2659d7b2021-03-05 13:31:41 -080050#include "dlext_private.h"
Peter Collingbourne45819dd2020-01-09 11:00:43 -080051
Christopher Ferrisb874c332020-01-21 16:39:05 -080052#include "platform/bionic/malloc.h"
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070053#include "platform/bionic/mte.h"
Christopher Ferrisb874c332020-01-21 16:39:05 -080054#include "platform/bionic/reserved_signals.h"
55#include "private/bionic_config.h"
56
Elliott Hughesb1770852018-09-18 12:52:42 -070057#define HAVE_REALLOCARRAY 1
Christopher Ferrisb874c332020-01-21 16:39:05 -080058
Colin Cross7da20342021-07-28 11:18:11 -070059#elif defined(__GLIBC__)
Christopher Ferrisb874c332020-01-21 16:39:05 -080060
Elliott Hughesb1770852018-09-18 12:52:42 -070061#define HAVE_REALLOCARRAY __GLIBC_PREREQ(2, 26)
Christopher Ferrisb874c332020-01-21 16:39:05 -080062
Colin Cross4c5595c2021-08-16 15:51:59 -070063#elif defined(ANDROID_HOST_MUSL)
Colin Cross7da20342021-07-28 11:18:11 -070064
65#define HAVE_REALLOCARRAY 1
66
Elliott Hughesb1770852018-09-18 12:52:42 -070067#endif
68
Christopher Ferris885f3b92013-05-21 17:48:01 -070069TEST(malloc, malloc_std) {
70 // Simple malloc test.
71 void *ptr = malloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -070072 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -070073 ASSERT_LE(100U, malloc_usable_size(ptr));
Christopher Ferris885f3b92013-05-21 17:48:01 -070074 free(ptr);
75}
76
Christopher Ferrisa4037802014-06-09 19:14:11 -070077TEST(malloc, malloc_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080078 SKIP_WITH_HWASAN;
Christopher Ferrisa4037802014-06-09 19:14:11 -070079 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070080 ASSERT_EQ(nullptr, malloc(SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -070081 ASSERT_EQ(ENOMEM, errno);
82}
83
Christopher Ferris885f3b92013-05-21 17:48:01 -070084TEST(malloc, calloc_std) {
85 // Simple calloc test.
86 size_t alloc_len = 100;
87 char *ptr = (char *)calloc(1, alloc_len);
Yi Kong32bc0fc2018-08-02 17:31:13 -070088 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -070089 ASSERT_LE(alloc_len, malloc_usable_size(ptr));
90 for (size_t i = 0; i < alloc_len; i++) {
91 ASSERT_EQ(0, ptr[i]);
92 }
Christopher Ferris885f3b92013-05-21 17:48:01 -070093 free(ptr);
94}
95
Peter Collingbourne978eb162020-09-21 15:26:02 -070096TEST(malloc, calloc_mem_init_disabled) {
97#if defined(__BIONIC__)
98 // calloc should still zero memory if mem-init is disabled.
99 // With jemalloc the mallopts will fail but that shouldn't affect the
100 // execution of the test.
101 mallopt(M_THREAD_DISABLE_MEM_INIT, 1);
102 size_t alloc_len = 100;
103 char *ptr = reinterpret_cast<char*>(calloc(1, alloc_len));
104 for (size_t i = 0; i < alloc_len; i++) {
105 ASSERT_EQ(0, ptr[i]);
106 }
107 free(ptr);
108 mallopt(M_THREAD_DISABLE_MEM_INIT, 0);
109#else
110 GTEST_SKIP() << "bionic-only test";
111#endif
112}
113
Christopher Ferrisa4037802014-06-09 19:14:11 -0700114TEST(malloc, calloc_illegal) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800115 SKIP_WITH_HWASAN;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700116 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700117 ASSERT_EQ(nullptr, calloc(-1, 100));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700118 ASSERT_EQ(ENOMEM, errno);
119}
120
121TEST(malloc, calloc_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800122 SKIP_WITH_HWASAN;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700123 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700124 ASSERT_EQ(nullptr, calloc(1, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700125 ASSERT_EQ(ENOMEM, errno);
126 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700127 ASSERT_EQ(nullptr, calloc(SIZE_MAX, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700128 ASSERT_EQ(ENOMEM, errno);
129 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700130 ASSERT_EQ(nullptr, calloc(2, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700131 ASSERT_EQ(ENOMEM, errno);
132 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700133 ASSERT_EQ(nullptr, calloc(SIZE_MAX, 2));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700134 ASSERT_EQ(ENOMEM, errno);
135}
136
Christopher Ferris885f3b92013-05-21 17:48:01 -0700137TEST(malloc, memalign_multiple) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800138 SKIP_WITH_HWASAN << "hwasan requires power of 2 alignment";
Christopher Ferris885f3b92013-05-21 17:48:01 -0700139 // Memalign test where the alignment is any value.
140 for (size_t i = 0; i <= 12; i++) {
141 for (size_t alignment = 1 << i; alignment < (1U << (i+1)); alignment++) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700142 char *ptr = reinterpret_cast<char*>(memalign(alignment, 100));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700143 ASSERT_TRUE(ptr != nullptr) << "Failed at alignment " << alignment;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700144 ASSERT_LE(100U, malloc_usable_size(ptr)) << "Failed at alignment " << alignment;
145 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) % ((1U << i)))
146 << "Failed at alignment " << alignment;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700147 free(ptr);
148 }
149 }
150}
151
Christopher Ferrisa4037802014-06-09 19:14:11 -0700152TEST(malloc, memalign_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800153 SKIP_WITH_HWASAN;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700154 ASSERT_EQ(nullptr, memalign(4096, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700155}
156
157TEST(malloc, memalign_non_power2) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800158 SKIP_WITH_HWASAN;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700159 void* ptr;
160 for (size_t align = 0; align <= 256; align++) {
161 ptr = memalign(align, 1024);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700162 ASSERT_TRUE(ptr != nullptr) << "Failed at align " << align;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700163 free(ptr);
164 }
165}
166
Christopher Ferris885f3b92013-05-21 17:48:01 -0700167TEST(malloc, memalign_realloc) {
168 // Memalign and then realloc the pointer a couple of times.
169 for (size_t alignment = 1; alignment <= 4096; alignment <<= 1) {
170 char *ptr = (char*)memalign(alignment, 100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700171 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700172 ASSERT_LE(100U, malloc_usable_size(ptr));
173 ASSERT_EQ(0U, (intptr_t)ptr % alignment);
174 memset(ptr, 0x23, 100);
175
176 ptr = (char*)realloc(ptr, 200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700177 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700178 ASSERT_LE(200U, malloc_usable_size(ptr));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700179 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700180 for (size_t i = 0; i < 100; i++) {
181 ASSERT_EQ(0x23, ptr[i]);
182 }
183 memset(ptr, 0x45, 200);
184
185 ptr = (char*)realloc(ptr, 300);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700186 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700187 ASSERT_LE(300U, malloc_usable_size(ptr));
188 for (size_t i = 0; i < 200; i++) {
189 ASSERT_EQ(0x45, ptr[i]);
190 }
191 memset(ptr, 0x67, 300);
192
193 ptr = (char*)realloc(ptr, 250);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700194 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700195 ASSERT_LE(250U, malloc_usable_size(ptr));
196 for (size_t i = 0; i < 250; i++) {
197 ASSERT_EQ(0x67, ptr[i]);
198 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700199 free(ptr);
200 }
201}
202
203TEST(malloc, malloc_realloc_larger) {
204 // Realloc to a larger size, malloc is used for the original allocation.
205 char *ptr = (char *)malloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700206 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700207 ASSERT_LE(100U, malloc_usable_size(ptr));
208 memset(ptr, 67, 100);
209
210 ptr = (char *)realloc(ptr, 200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700211 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700212 ASSERT_LE(200U, malloc_usable_size(ptr));
213 for (size_t i = 0; i < 100; i++) {
214 ASSERT_EQ(67, ptr[i]);
215 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700216 free(ptr);
217}
218
219TEST(malloc, malloc_realloc_smaller) {
220 // Realloc to a smaller size, malloc is used for the original allocation.
221 char *ptr = (char *)malloc(200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700222 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700223 ASSERT_LE(200U, malloc_usable_size(ptr));
224 memset(ptr, 67, 200);
225
226 ptr = (char *)realloc(ptr, 100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700227 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700228 ASSERT_LE(100U, malloc_usable_size(ptr));
229 for (size_t i = 0; i < 100; i++) {
230 ASSERT_EQ(67, ptr[i]);
231 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700232 free(ptr);
233}
234
235TEST(malloc, malloc_multiple_realloc) {
236 // Multiple reallocs, malloc is used for the original allocation.
237 char *ptr = (char *)malloc(200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700238 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700239 ASSERT_LE(200U, malloc_usable_size(ptr));
240 memset(ptr, 0x23, 200);
241
242 ptr = (char *)realloc(ptr, 100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700243 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700244 ASSERT_LE(100U, malloc_usable_size(ptr));
245 for (size_t i = 0; i < 100; i++) {
246 ASSERT_EQ(0x23, ptr[i]);
247 }
248
249 ptr = (char*)realloc(ptr, 50);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700250 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700251 ASSERT_LE(50U, malloc_usable_size(ptr));
252 for (size_t i = 0; i < 50; i++) {
253 ASSERT_EQ(0x23, ptr[i]);
254 }
255
256 ptr = (char*)realloc(ptr, 150);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700257 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700258 ASSERT_LE(150U, malloc_usable_size(ptr));
259 for (size_t i = 0; i < 50; i++) {
260 ASSERT_EQ(0x23, ptr[i]);
261 }
262 memset(ptr, 0x23, 150);
263
264 ptr = (char*)realloc(ptr, 425);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700265 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700266 ASSERT_LE(425U, malloc_usable_size(ptr));
267 for (size_t i = 0; i < 150; i++) {
268 ASSERT_EQ(0x23, ptr[i]);
269 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700270 free(ptr);
271}
Christopher Ferrisa4037802014-06-09 19:14:11 -0700272
Christopher Ferris885f3b92013-05-21 17:48:01 -0700273TEST(malloc, calloc_realloc_larger) {
274 // Realloc to a larger size, calloc is used for the original allocation.
275 char *ptr = (char *)calloc(1, 100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700276 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700277 ASSERT_LE(100U, malloc_usable_size(ptr));
278
279 ptr = (char *)realloc(ptr, 200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700280 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700281 ASSERT_LE(200U, malloc_usable_size(ptr));
282 for (size_t i = 0; i < 100; i++) {
283 ASSERT_EQ(0, ptr[i]);
284 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700285 free(ptr);
286}
287
288TEST(malloc, calloc_realloc_smaller) {
289 // Realloc to a smaller size, calloc is used for the original allocation.
290 char *ptr = (char *)calloc(1, 200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700291 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700292 ASSERT_LE(200U, malloc_usable_size(ptr));
293
294 ptr = (char *)realloc(ptr, 100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700295 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700296 ASSERT_LE(100U, malloc_usable_size(ptr));
297 for (size_t i = 0; i < 100; i++) {
298 ASSERT_EQ(0, ptr[i]);
299 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700300 free(ptr);
301}
302
303TEST(malloc, calloc_multiple_realloc) {
304 // Multiple reallocs, calloc is used for the original allocation.
305 char *ptr = (char *)calloc(1, 200);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700306 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700307 ASSERT_LE(200U, malloc_usable_size(ptr));
308
309 ptr = (char *)realloc(ptr, 100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700310 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700311 ASSERT_LE(100U, malloc_usable_size(ptr));
312 for (size_t i = 0; i < 100; i++) {
313 ASSERT_EQ(0, ptr[i]);
314 }
315
316 ptr = (char*)realloc(ptr, 50);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700317 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700318 ASSERT_LE(50U, malloc_usable_size(ptr));
319 for (size_t i = 0; i < 50; i++) {
320 ASSERT_EQ(0, ptr[i]);
321 }
322
323 ptr = (char*)realloc(ptr, 150);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700324 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700325 ASSERT_LE(150U, malloc_usable_size(ptr));
326 for (size_t i = 0; i < 50; i++) {
327 ASSERT_EQ(0, ptr[i]);
328 }
329 memset(ptr, 0, 150);
330
331 ptr = (char*)realloc(ptr, 425);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700332 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700333 ASSERT_LE(425U, malloc_usable_size(ptr));
334 for (size_t i = 0; i < 150; i++) {
335 ASSERT_EQ(0, ptr[i]);
336 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700337 free(ptr);
338}
Christopher Ferris72bbd422014-05-08 11:14:03 -0700339
Christopher Ferrisa4037802014-06-09 19:14:11 -0700340TEST(malloc, realloc_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800341 SKIP_WITH_HWASAN;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700342 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700343 ASSERT_EQ(nullptr, realloc(nullptr, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700344 ASSERT_EQ(ENOMEM, errno);
345 void* ptr = malloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700346 ASSERT_TRUE(ptr != nullptr);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700347 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700348 ASSERT_EQ(nullptr, realloc(ptr, SIZE_MAX));
Christopher Ferrisa4037802014-06-09 19:14:11 -0700349 ASSERT_EQ(ENOMEM, errno);
350 free(ptr);
Christopher Ferris72bbd422014-05-08 11:14:03 -0700351}
352
Dan Alberte5fdaa42014-06-14 01:04:31 +0000353#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
354extern "C" void* pvalloc(size_t);
355extern "C" void* valloc(size_t);
Christopher Ferris804cebe2019-06-20 08:50:23 -0700356#endif
Dan Alberte5fdaa42014-06-14 01:04:31 +0000357
Christopher Ferrisa4037802014-06-09 19:14:11 -0700358TEST(malloc, pvalloc_std) {
Christopher Ferris804cebe2019-06-20 08:50:23 -0700359#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700360 size_t pagesize = sysconf(_SC_PAGESIZE);
361 void* ptr = pvalloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700362 ASSERT_TRUE(ptr != nullptr);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700363 ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0);
364 ASSERT_LE(pagesize, malloc_usable_size(ptr));
365 free(ptr);
Christopher Ferris804cebe2019-06-20 08:50:23 -0700366#else
367 GTEST_SKIP() << "pvalloc not supported.";
368#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700369}
370
371TEST(malloc, pvalloc_overflow) {
Christopher Ferris804cebe2019-06-20 08:50:23 -0700372#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700373 ASSERT_EQ(nullptr, pvalloc(SIZE_MAX));
Christopher Ferris804cebe2019-06-20 08:50:23 -0700374#else
375 GTEST_SKIP() << "pvalloc not supported.";
376#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700377}
378
379TEST(malloc, valloc_std) {
Christopher Ferris804cebe2019-06-20 08:50:23 -0700380#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700381 size_t pagesize = sysconf(_SC_PAGESIZE);
Christopher Ferrisd5ab0a52019-06-19 12:03:57 -0700382 void* ptr = valloc(100);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700383 ASSERT_TRUE(ptr != nullptr);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700384 ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0);
385 free(ptr);
Christopher Ferris804cebe2019-06-20 08:50:23 -0700386#else
387 GTEST_SKIP() << "valloc not supported.";
388#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700389}
390
391TEST(malloc, valloc_overflow) {
Christopher Ferris804cebe2019-06-20 08:50:23 -0700392#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700393 ASSERT_EQ(nullptr, valloc(SIZE_MAX));
Christopher Ferris804cebe2019-06-20 08:50:23 -0700394#else
395 GTEST_SKIP() << "valloc not supported.";
Dan Alberte5fdaa42014-06-14 01:04:31 +0000396#endif
Christopher Ferris804cebe2019-06-20 08:50:23 -0700397}
Dan Albert4caa1f02014-08-20 09:16:57 -0700398
399TEST(malloc, malloc_info) {
400#ifdef __BIONIC__
Evgenii Stepanov8de6b462019-03-22 13:22:28 -0700401 SKIP_WITH_HWASAN; // hwasan does not implement malloc_info
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800402
403 TemporaryFile tf;
404 ASSERT_TRUE(tf.fd != -1);
405 FILE* fp = fdopen(tf.fd, "w+");
406 tf.release();
407 ASSERT_TRUE(fp != nullptr);
408 ASSERT_EQ(0, malloc_info(0, fp));
409 ASSERT_EQ(0, fclose(fp));
410
411 std::string contents;
412 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &contents));
Dan Albert4caa1f02014-08-20 09:16:57 -0700413
414 tinyxml2::XMLDocument doc;
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800415 ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(contents.c_str()));
Dan Albert4caa1f02014-08-20 09:16:57 -0700416
417 auto root = doc.FirstChildElement();
418 ASSERT_NE(nullptr, root);
419 ASSERT_STREQ("malloc", root->Name());
Christopher Ferris85169652019-10-09 18:41:55 -0700420 std::string version(root->Attribute("version"));
421 if (version == "jemalloc-1") {
Christopher Ferris6c619a02019-03-01 17:59:51 -0800422 auto arena = root->FirstChildElement();
423 for (; arena != nullptr; arena = arena->NextSiblingElement()) {
424 int val;
Dan Albert4caa1f02014-08-20 09:16:57 -0700425
Christopher Ferris6c619a02019-03-01 17:59:51 -0800426 ASSERT_STREQ("heap", arena->Name());
427 ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val));
428 ASSERT_EQ(tinyxml2::XML_SUCCESS,
429 arena->FirstChildElement("allocated-large")->QueryIntText(&val));
430 ASSERT_EQ(tinyxml2::XML_SUCCESS,
431 arena->FirstChildElement("allocated-huge")->QueryIntText(&val));
432 ASSERT_EQ(tinyxml2::XML_SUCCESS,
433 arena->FirstChildElement("allocated-bins")->QueryIntText(&val));
434 ASSERT_EQ(tinyxml2::XML_SUCCESS,
435 arena->FirstChildElement("bins-total")->QueryIntText(&val));
Dan Albert4caa1f02014-08-20 09:16:57 -0700436
Christopher Ferris6c619a02019-03-01 17:59:51 -0800437 auto bin = arena->FirstChildElement("bin");
438 for (; bin != nullptr; bin = bin ->NextSiblingElement()) {
439 if (strcmp(bin->Name(), "bin") == 0) {
440 ASSERT_EQ(tinyxml2::XML_SUCCESS, bin->QueryIntAttribute("nr", &val));
441 ASSERT_EQ(tinyxml2::XML_SUCCESS,
442 bin->FirstChildElement("allocated")->QueryIntText(&val));
443 ASSERT_EQ(tinyxml2::XML_SUCCESS,
444 bin->FirstChildElement("nmalloc")->QueryIntText(&val));
445 ASSERT_EQ(tinyxml2::XML_SUCCESS,
446 bin->FirstChildElement("ndalloc")->QueryIntText(&val));
447 }
Dan Albert4caa1f02014-08-20 09:16:57 -0700448 }
449 }
Christopher Ferriscce88c02020-02-12 17:41:01 -0800450 } else if (version == "scudo-1") {
451 auto element = root->FirstChildElement();
452 for (; element != nullptr; element = element->NextSiblingElement()) {
453 int val;
454
455 ASSERT_STREQ("alloc", element->Name());
456 ASSERT_EQ(tinyxml2::XML_SUCCESS, element->QueryIntAttribute("size", &val));
457 ASSERT_EQ(tinyxml2::XML_SUCCESS, element->QueryIntAttribute("count", &val));
458 }
Christopher Ferris6c619a02019-03-01 17:59:51 -0800459 } else {
Christopher Ferriscce88c02020-02-12 17:41:01 -0800460 // Do not verify output for debug malloc.
461 ASSERT_TRUE(version == "debug-malloc-1") << "Unknown version: " << version;
Dan Albert4caa1f02014-08-20 09:16:57 -0700462 }
463#endif
464}
Christopher Ferrisad33ebe2015-12-16 12:07:25 -0800465
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700466TEST(malloc, malloc_info_matches_mallinfo) {
467#ifdef __BIONIC__
468 SKIP_WITH_HWASAN; // hwasan does not implement malloc_info
469
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800470 TemporaryFile tf;
471 ASSERT_TRUE(tf.fd != -1);
472 FILE* fp = fdopen(tf.fd, "w+");
473 tf.release();
474 ASSERT_TRUE(fp != nullptr);
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700475 size_t mallinfo_before_allocated_bytes = mallinfo().uordblks;
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800476 ASSERT_EQ(0, malloc_info(0, fp));
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700477 size_t mallinfo_after_allocated_bytes = mallinfo().uordblks;
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800478 ASSERT_EQ(0, fclose(fp));
479
480 std::string contents;
481 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &contents));
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700482
483 tinyxml2::XMLDocument doc;
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800484 ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(contents.c_str()));
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700485
486 size_t total_allocated_bytes = 0;
487 auto root = doc.FirstChildElement();
488 ASSERT_NE(nullptr, root);
489 ASSERT_STREQ("malloc", root->Name());
Christopher Ferris85169652019-10-09 18:41:55 -0700490 std::string version(root->Attribute("version"));
491 if (version == "jemalloc-1") {
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700492 auto arena = root->FirstChildElement();
493 for (; arena != nullptr; arena = arena->NextSiblingElement()) {
494 int val;
495
496 ASSERT_STREQ("heap", arena->Name());
497 ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val));
498 ASSERT_EQ(tinyxml2::XML_SUCCESS,
499 arena->FirstChildElement("allocated-large")->QueryIntText(&val));
500 total_allocated_bytes += val;
501 ASSERT_EQ(tinyxml2::XML_SUCCESS,
502 arena->FirstChildElement("allocated-huge")->QueryIntText(&val));
503 total_allocated_bytes += val;
504 ASSERT_EQ(tinyxml2::XML_SUCCESS,
505 arena->FirstChildElement("allocated-bins")->QueryIntText(&val));
506 total_allocated_bytes += val;
507 ASSERT_EQ(tinyxml2::XML_SUCCESS,
508 arena->FirstChildElement("bins-total")->QueryIntText(&val));
509 }
510 // The total needs to be between the mallinfo call before and after
511 // since malloc_info allocates some memory.
512 EXPECT_LE(mallinfo_before_allocated_bytes, total_allocated_bytes);
513 EXPECT_GE(mallinfo_after_allocated_bytes, total_allocated_bytes);
Christopher Ferriscce88c02020-02-12 17:41:01 -0800514 } else if (version == "scudo-1") {
515 auto element = root->FirstChildElement();
516 for (; element != nullptr; element = element->NextSiblingElement()) {
517 ASSERT_STREQ("alloc", element->Name());
518 int size;
519 ASSERT_EQ(tinyxml2::XML_SUCCESS, element->QueryIntAttribute("size", &size));
520 int count;
521 ASSERT_EQ(tinyxml2::XML_SUCCESS, element->QueryIntAttribute("count", &count));
522 total_allocated_bytes += size * count;
523 }
524 // Scudo only gives the information on the primary, so simply make
525 // sure that the value is non-zero.
526 EXPECT_NE(0U, total_allocated_bytes);
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700527 } else {
Christopher Ferriscce88c02020-02-12 17:41:01 -0800528 // Do not verify output for debug malloc.
529 ASSERT_TRUE(version == "debug-malloc-1") << "Unknown version: " << version;
Christopher Ferrisdb9706a2019-05-02 18:33:11 -0700530 }
531#endif
532}
533
Christopher Ferrisad33ebe2015-12-16 12:07:25 -0800534TEST(malloc, calloc_usable_size) {
535 for (size_t size = 1; size <= 2048; size++) {
536 void* pointer = malloc(size);
537 ASSERT_TRUE(pointer != nullptr);
538 memset(pointer, 0xeb, malloc_usable_size(pointer));
539 free(pointer);
540
541 // We should get a previous pointer that has been set to non-zero.
542 // If calloc does not zero out all of the data, this will fail.
543 uint8_t* zero_mem = reinterpret_cast<uint8_t*>(calloc(1, size));
544 ASSERT_TRUE(pointer != nullptr);
545 size_t usable_size = malloc_usable_size(zero_mem);
546 for (size_t i = 0; i < usable_size; i++) {
547 ASSERT_EQ(0, zero_mem[i]) << "Failed at allocation size " << size << " at byte " << i;
548 }
549 free(zero_mem);
550 }
551}
Elliott Hughes884f76e2016-02-10 20:43:22 -0800552
553TEST(malloc, malloc_0) {
554 void* p = malloc(0);
555 ASSERT_TRUE(p != nullptr);
556 free(p);
557}
558
559TEST(malloc, calloc_0_0) {
560 void* p = calloc(0, 0);
561 ASSERT_TRUE(p != nullptr);
562 free(p);
563}
564
565TEST(malloc, calloc_0_1) {
566 void* p = calloc(0, 1);
567 ASSERT_TRUE(p != nullptr);
568 free(p);
569}
570
571TEST(malloc, calloc_1_0) {
572 void* p = calloc(1, 0);
573 ASSERT_TRUE(p != nullptr);
574 free(p);
575}
576
577TEST(malloc, realloc_nullptr_0) {
578 // realloc(nullptr, size) is actually malloc(size).
579 void* p = realloc(nullptr, 0);
580 ASSERT_TRUE(p != nullptr);
581 free(p);
582}
583
584TEST(malloc, realloc_0) {
585 void* p = malloc(1024);
586 ASSERT_TRUE(p != nullptr);
587 // realloc(p, 0) is actually free(p).
588 void* p2 = realloc(p, 0);
589 ASSERT_TRUE(p2 == nullptr);
590}
Christopher Ferris72df6702016-02-11 15:51:31 -0800591
592constexpr size_t MAX_LOOPS = 200;
593
594// Make sure that memory returned by malloc is aligned to allow these data types.
595TEST(malloc, verify_alignment) {
596 uint32_t** values_32 = new uint32_t*[MAX_LOOPS];
597 uint64_t** values_64 = new uint64_t*[MAX_LOOPS];
598 long double** values_ldouble = new long double*[MAX_LOOPS];
599 // Use filler to attempt to force the allocator to get potentially bad alignments.
600 void** filler = new void*[MAX_LOOPS];
601
602 for (size_t i = 0; i < MAX_LOOPS; i++) {
603 // Check uint32_t pointers.
604 filler[i] = malloc(1);
605 ASSERT_TRUE(filler[i] != nullptr);
606
607 values_32[i] = reinterpret_cast<uint32_t*>(malloc(sizeof(uint32_t)));
608 ASSERT_TRUE(values_32[i] != nullptr);
609 *values_32[i] = i;
610 ASSERT_EQ(*values_32[i], i);
611 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_32[i]) & (sizeof(uint32_t) - 1));
612
613 free(filler[i]);
614 }
615
616 for (size_t i = 0; i < MAX_LOOPS; i++) {
617 // Check uint64_t pointers.
618 filler[i] = malloc(1);
619 ASSERT_TRUE(filler[i] != nullptr);
620
621 values_64[i] = reinterpret_cast<uint64_t*>(malloc(sizeof(uint64_t)));
622 ASSERT_TRUE(values_64[i] != nullptr);
623 *values_64[i] = 0x1000 + i;
624 ASSERT_EQ(*values_64[i], 0x1000 + i);
625 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_64[i]) & (sizeof(uint64_t) - 1));
626
627 free(filler[i]);
628 }
629
630 for (size_t i = 0; i < MAX_LOOPS; i++) {
631 // Check long double pointers.
632 filler[i] = malloc(1);
633 ASSERT_TRUE(filler[i] != nullptr);
634
635 values_ldouble[i] = reinterpret_cast<long double*>(malloc(sizeof(long double)));
636 ASSERT_TRUE(values_ldouble[i] != nullptr);
637 *values_ldouble[i] = 5.5 + i;
638 ASSERT_DOUBLE_EQ(*values_ldouble[i], 5.5 + i);
639 // 32 bit glibc has a long double size of 12 bytes, so hardcode the
640 // required alignment to 0x7.
641#if !defined(__BIONIC__) && !defined(__LP64__)
642 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_ldouble[i]) & 0x7);
643#else
644 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_ldouble[i]) & (sizeof(long double) - 1));
645#endif
646
647 free(filler[i]);
648 }
649
650 for (size_t i = 0; i < MAX_LOOPS; i++) {
651 free(values_32[i]);
652 free(values_64[i]);
653 free(values_ldouble[i]);
654 }
655
656 delete[] filler;
657 delete[] values_32;
658 delete[] values_64;
659 delete[] values_ldouble;
660}
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700661
662TEST(malloc, mallopt_smoke) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700663#if !defined(ANDROID_HOST_MUSL)
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700664 errno = 0;
665 ASSERT_EQ(0, mallopt(-1000, 1));
666 // mallopt doesn't set errno.
667 ASSERT_EQ(0, errno);
Colin Cross7da20342021-07-28 11:18:11 -0700668#else
669 GTEST_SKIP() << "musl doesn't have mallopt";
670#endif
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700671}
Elliott Hughesb1770852018-09-18 12:52:42 -0700672
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800673TEST(malloc, mallopt_decay) {
674#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800675 SKIP_WITH_HWASAN << "hwasan does not implement mallopt";
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800676 errno = 0;
677 ASSERT_EQ(1, mallopt(M_DECAY_TIME, 1));
678 ASSERT_EQ(1, mallopt(M_DECAY_TIME, 0));
679 ASSERT_EQ(1, mallopt(M_DECAY_TIME, 1));
680 ASSERT_EQ(1, mallopt(M_DECAY_TIME, 0));
681#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800682 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800683#endif
684}
685
686TEST(malloc, mallopt_purge) {
687#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800688 SKIP_WITH_HWASAN << "hwasan does not implement mallopt";
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800689 errno = 0;
690 ASSERT_EQ(1, mallopt(M_PURGE, 0));
691#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800692 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisaf1b8dd2018-11-07 15:28:16 -0800693#endif
694}
695
Christopher Ferris88448792020-07-28 14:15:31 -0700696#if defined(__BIONIC__)
697static void GetAllocatorVersion(bool* allocator_scudo) {
698 TemporaryFile tf;
699 ASSERT_TRUE(tf.fd != -1);
700 FILE* fp = fdopen(tf.fd, "w+");
701 tf.release();
702 ASSERT_TRUE(fp != nullptr);
703 ASSERT_EQ(0, malloc_info(0, fp));
704 ASSERT_EQ(0, fclose(fp));
705
706 std::string contents;
707 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &contents));
708
709 tinyxml2::XMLDocument doc;
710 ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(contents.c_str()));
711
712 auto root = doc.FirstChildElement();
713 ASSERT_NE(nullptr, root);
714 ASSERT_STREQ("malloc", root->Name());
715 std::string version(root->Attribute("version"));
716 *allocator_scudo = (version == "scudo-1");
717}
718#endif
719
720TEST(malloc, mallopt_scudo_only_options) {
721#if defined(__BIONIC__)
722 SKIP_WITH_HWASAN << "hwasan does not implement mallopt";
723 bool allocator_scudo;
724 GetAllocatorVersion(&allocator_scudo);
725 if (!allocator_scudo) {
726 GTEST_SKIP() << "scudo allocator only test";
727 }
728 ASSERT_EQ(1, mallopt(M_CACHE_COUNT_MAX, 100));
729 ASSERT_EQ(1, mallopt(M_CACHE_SIZE_MAX, 1024 * 1024 * 2));
730 ASSERT_EQ(1, mallopt(M_TSDS_COUNT_MAX, 8));
731#else
732 GTEST_SKIP() << "bionic-only test";
733#endif
734}
735
Elliott Hughesb1770852018-09-18 12:52:42 -0700736TEST(malloc, reallocarray_overflow) {
737#if HAVE_REALLOCARRAY
738 // Values that cause overflow to a result small enough (8 on LP64) that malloc would "succeed".
739 size_t a = static_cast<size_t>(INTPTR_MIN + 4);
740 size_t b = 2;
741
742 errno = 0;
743 ASSERT_TRUE(reallocarray(nullptr, a, b) == nullptr);
744 ASSERT_EQ(ENOMEM, errno);
745
746 errno = 0;
747 ASSERT_TRUE(reallocarray(nullptr, b, a) == nullptr);
748 ASSERT_EQ(ENOMEM, errno);
749#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800750 GTEST_SKIP() << "reallocarray not available";
Elliott Hughesb1770852018-09-18 12:52:42 -0700751#endif
752}
753
754TEST(malloc, reallocarray) {
755#if HAVE_REALLOCARRAY
756 void* p = reallocarray(nullptr, 2, 32);
757 ASSERT_TRUE(p != nullptr);
758 ASSERT_GE(malloc_usable_size(p), 64U);
759#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800760 GTEST_SKIP() << "reallocarray not available";
Elliott Hughesb1770852018-09-18 12:52:42 -0700761#endif
762}
Christopher Ferris09a19aa2018-11-16 13:28:56 -0800763
764TEST(malloc, mallinfo) {
765#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800766 SKIP_WITH_HWASAN << "hwasan does not implement mallinfo";
Christopher Ferris09a19aa2018-11-16 13:28:56 -0800767 static size_t sizes[] = {
768 8, 32, 128, 4096, 32768, 131072, 1024000, 10240000, 20480000, 300000000
769 };
770
771 constexpr static size_t kMaxAllocs = 50;
772
773 for (size_t size : sizes) {
774 // If some of these allocations are stuck in a thread cache, then keep
775 // looping until we make an allocation that changes the total size of the
776 // memory allocated.
777 // jemalloc implementations counts the thread cache allocations against
778 // total memory allocated.
779 void* ptrs[kMaxAllocs] = {};
780 bool pass = false;
781 for (size_t i = 0; i < kMaxAllocs; i++) {
782 size_t allocated = mallinfo().uordblks;
783 ptrs[i] = malloc(size);
784 ASSERT_TRUE(ptrs[i] != nullptr);
785 size_t new_allocated = mallinfo().uordblks;
786 if (allocated != new_allocated) {
787 size_t usable_size = malloc_usable_size(ptrs[i]);
Christopher Ferris4e562282019-02-07 14:20:03 -0800788 // Only check if the total got bigger by at least allocation size.
789 // Sometimes the mallinfo numbers can go backwards due to compaction
790 // and/or freeing of cached data.
791 if (new_allocated >= allocated + usable_size) {
792 pass = true;
793 break;
794 }
Christopher Ferris09a19aa2018-11-16 13:28:56 -0800795 }
796 }
797 for (void* ptr : ptrs) {
798 free(ptr);
799 }
800 ASSERT_TRUE(pass)
801 << "For size " << size << " allocated bytes did not increase after "
802 << kMaxAllocs << " allocations.";
803 }
804#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800805 GTEST_SKIP() << "glibc is broken";
Christopher Ferris09a19aa2018-11-16 13:28:56 -0800806#endif
807}
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000808
Christopher Ferrisf32494c2020-01-08 14:19:10 -0800809template <typename Type>
810void __attribute__((optnone)) VerifyAlignment(Type* floating) {
811 size_t expected_alignment = alignof(Type);
812 if (expected_alignment != 0) {
813 ASSERT_EQ(0U, (expected_alignment - 1) & reinterpret_cast<uintptr_t>(floating))
814 << "Expected alignment " << expected_alignment << " ptr value " << floating;
815 }
816}
817
818template <typename Type>
819void __attribute__((optnone)) TestAllocateType() {
820 // The number of allocations to do in a row. This is to attempt to
821 // expose the worst case alignment for native allocators that use
822 // bins.
823 static constexpr size_t kMaxConsecutiveAllocs = 100;
824
825 // Verify using new directly.
826 Type* types[kMaxConsecutiveAllocs];
827 for (size_t i = 0; i < kMaxConsecutiveAllocs; i++) {
828 types[i] = new Type;
829 VerifyAlignment(types[i]);
830 if (::testing::Test::HasFatalFailure()) {
831 return;
832 }
833 }
834 for (size_t i = 0; i < kMaxConsecutiveAllocs; i++) {
835 delete types[i];
836 }
837
838 // Verify using malloc.
839 for (size_t i = 0; i < kMaxConsecutiveAllocs; i++) {
840 types[i] = reinterpret_cast<Type*>(malloc(sizeof(Type)));
841 ASSERT_TRUE(types[i] != nullptr);
842 VerifyAlignment(types[i]);
843 if (::testing::Test::HasFatalFailure()) {
844 return;
845 }
846 }
847 for (size_t i = 0; i < kMaxConsecutiveAllocs; i++) {
848 free(types[i]);
849 }
850
851 // Verify using a vector.
852 std::vector<Type> type_vector(kMaxConsecutiveAllocs);
853 for (size_t i = 0; i < type_vector.size(); i++) {
854 VerifyAlignment(&type_vector[i]);
855 if (::testing::Test::HasFatalFailure()) {
856 return;
857 }
858 }
859}
860
861#if defined(__ANDROID__)
862static void __attribute__((optnone)) AndroidVerifyAlignment(size_t alloc_size, size_t aligned_bytes) {
863 void* ptrs[100];
864 uintptr_t mask = aligned_bytes - 1;
865 for (size_t i = 0; i < sizeof(ptrs) / sizeof(void*); i++) {
866 ptrs[i] = malloc(alloc_size);
867 ASSERT_TRUE(ptrs[i] != nullptr);
868 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptrs[i]) & mask)
869 << "Expected at least " << aligned_bytes << " byte alignment: size "
870 << alloc_size << " actual ptr " << ptrs[i];
871 }
872}
873#endif
874
875TEST(malloc, align_check) {
876 // See http://www.open-std.org/jtc1/sc22/wg14/www/docs/summary.htm#dr_445
877 // for a discussion of type alignment.
878 ASSERT_NO_FATAL_FAILURE(TestAllocateType<float>());
879 ASSERT_NO_FATAL_FAILURE(TestAllocateType<double>());
880 ASSERT_NO_FATAL_FAILURE(TestAllocateType<long double>());
881
882 ASSERT_NO_FATAL_FAILURE(TestAllocateType<char>());
883 ASSERT_NO_FATAL_FAILURE(TestAllocateType<char16_t>());
884 ASSERT_NO_FATAL_FAILURE(TestAllocateType<char32_t>());
885 ASSERT_NO_FATAL_FAILURE(TestAllocateType<wchar_t>());
886 ASSERT_NO_FATAL_FAILURE(TestAllocateType<signed char>());
887 ASSERT_NO_FATAL_FAILURE(TestAllocateType<short int>());
888 ASSERT_NO_FATAL_FAILURE(TestAllocateType<int>());
889 ASSERT_NO_FATAL_FAILURE(TestAllocateType<long int>());
890 ASSERT_NO_FATAL_FAILURE(TestAllocateType<long long int>());
891 ASSERT_NO_FATAL_FAILURE(TestAllocateType<unsigned char>());
892 ASSERT_NO_FATAL_FAILURE(TestAllocateType<unsigned short int>());
893 ASSERT_NO_FATAL_FAILURE(TestAllocateType<unsigned int>());
894 ASSERT_NO_FATAL_FAILURE(TestAllocateType<unsigned long int>());
895 ASSERT_NO_FATAL_FAILURE(TestAllocateType<unsigned long long int>());
896
897#if defined(__ANDROID__)
898 // On Android, there is a lot of code that expects certain alignments:
899 // - Allocations of a size that rounds up to a multiple of 16 bytes
900 // must have at least 16 byte alignment.
901 // - Allocations of a size that rounds up to a multiple of 8 bytes and
902 // not 16 bytes, are only required to have at least 8 byte alignment.
903 // This is regardless of whether it is in a 32 bit or 64 bit environment.
904
905 // See http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2293.htm for
906 // a discussion of this alignment mess. The code below is enforcing
907 // strong-alignment, since who knows what code depends on this behavior now.
908 for (size_t i = 1; i <= 128; i++) {
909 size_t rounded = (i + 7) & ~7;
910 if ((rounded % 16) == 0) {
911 AndroidVerifyAlignment(i, 16);
912 } else {
913 AndroidVerifyAlignment(i, 8);
914 }
915 if (::testing::Test::HasFatalFailure()) {
916 return;
917 }
918 }
919#endif
920}
921
Christopher Ferris201dcf42020-01-29 13:09:31 -0800922// Jemalloc doesn't pass this test right now, so leave it as disabled.
923TEST(malloc, DISABLED_alloc_after_fork) {
924 // Both of these need to be a power of 2.
925 static constexpr size_t kMinAllocationSize = 8;
926 static constexpr size_t kMaxAllocationSize = 2097152;
927
928 static constexpr size_t kNumAllocatingThreads = 5;
929 static constexpr size_t kNumForkLoops = 100;
930
931 std::atomic_bool stop;
932
933 // Create threads that simply allocate and free different sizes.
934 std::vector<std::thread*> threads;
935 for (size_t i = 0; i < kNumAllocatingThreads; i++) {
936 std::thread* t = new std::thread([&stop] {
937 while (!stop) {
938 for (size_t size = kMinAllocationSize; size <= kMaxAllocationSize; size <<= 1) {
Elliott Hughes7cda75f2020-10-22 13:22:35 -0700939 void* ptr;
940 DoNotOptimize(ptr = malloc(size));
Christopher Ferris201dcf42020-01-29 13:09:31 -0800941 free(ptr);
942 }
943 }
944 });
945 threads.push_back(t);
946 }
947
948 // Create a thread to fork and allocate.
949 for (size_t i = 0; i < kNumForkLoops; i++) {
950 pid_t pid;
951 if ((pid = fork()) == 0) {
952 for (size_t size = kMinAllocationSize; size <= kMaxAllocationSize; size <<= 1) {
Elliott Hughes7cda75f2020-10-22 13:22:35 -0700953 void* ptr;
954 DoNotOptimize(ptr = malloc(size));
Christopher Ferris201dcf42020-01-29 13:09:31 -0800955 ASSERT_TRUE(ptr != nullptr);
Christopher Ferris201dcf42020-01-29 13:09:31 -0800956 // Make sure we can touch all of the allocation.
957 memset(ptr, 0x1, size);
958 ASSERT_LE(size, malloc_usable_size(ptr));
959 free(ptr);
960 }
961 _exit(10);
962 }
963 ASSERT_NE(-1, pid);
964 AssertChildExited(pid, 10);
965 }
966
967 stop = true;
968 for (auto thread : threads) {
969 thread->join();
970 delete thread;
971 }
972}
973
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000974TEST(android_mallopt, error_on_unexpected_option) {
975#if defined(__BIONIC__)
976 const int unrecognized_option = -1;
977 errno = 0;
978 EXPECT_EQ(false, android_mallopt(unrecognized_option, nullptr, 0));
979 EXPECT_EQ(ENOTSUP, errno);
980#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800981 GTEST_SKIP() << "bionic-only test";
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000982#endif
983}
984
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800985bool IsDynamic() {
986#if defined(__LP64__)
987 Elf64_Ehdr ehdr;
988#else
989 Elf32_Ehdr ehdr;
990#endif
991 std::string path(android::base::GetExecutablePath());
992
993 int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
994 if (fd == -1) {
995 // Assume dynamic on error.
996 return true;
997 }
998 bool read_completed = android::base::ReadFully(fd, &ehdr, sizeof(ehdr));
999 close(fd);
1000 // Assume dynamic in error cases.
1001 return !read_completed || ehdr.e_type == ET_DYN;
1002}
1003
Ryan Savitskiecc37e32018-12-14 15:57:21 +00001004TEST(android_mallopt, init_zygote_child_profiling) {
1005#if defined(__BIONIC__)
1006 // Successful call.
1007 errno = 0;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -08001008 if (IsDynamic()) {
1009 EXPECT_EQ(true, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0));
1010 EXPECT_EQ(0, errno);
1011 } else {
1012 // Not supported in static executables.
1013 EXPECT_EQ(false, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0));
1014 EXPECT_EQ(ENOTSUP, errno);
1015 }
Ryan Savitskiecc37e32018-12-14 15:57:21 +00001016
1017 // Unexpected arguments rejected.
1018 errno = 0;
1019 char unexpected = 0;
1020 EXPECT_EQ(false, android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, &unexpected, 1));
Christopher Ferrise4cdbc42019-02-08 17:30:58 -08001021 if (IsDynamic()) {
1022 EXPECT_EQ(EINVAL, errno);
1023 } else {
1024 EXPECT_EQ(ENOTSUP, errno);
1025 }
Ryan Savitskiecc37e32018-12-14 15:57:21 +00001026#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001027 GTEST_SKIP() << "bionic-only test";
Ryan Savitskiecc37e32018-12-14 15:57:21 +00001028#endif
1029}
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -08001030
1031#if defined(__BIONIC__)
1032template <typename FuncType>
1033void CheckAllocationFunction(FuncType func) {
1034 // Assumes that no more than 108MB of memory is allocated before this.
1035 size_t limit = 128 * 1024 * 1024;
1036 ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
1037 if (!func(20 * 1024 * 1024))
1038 exit(1);
1039 if (func(128 * 1024 * 1024))
1040 exit(1);
1041 exit(0);
1042}
1043#endif
1044
1045TEST(android_mallopt, set_allocation_limit) {
1046#if defined(__BIONIC__)
1047 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return calloc(bytes, 1) != nullptr; }),
1048 testing::ExitedWithCode(0), "");
1049 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return calloc(1, bytes) != nullptr; }),
1050 testing::ExitedWithCode(0), "");
1051 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return malloc(bytes) != nullptr; }),
1052 testing::ExitedWithCode(0), "");
1053 EXPECT_EXIT(CheckAllocationFunction(
1054 [](size_t bytes) { return memalign(sizeof(void*), bytes) != nullptr; }),
1055 testing::ExitedWithCode(0), "");
1056 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) {
1057 void* ptr;
1058 return posix_memalign(&ptr, sizeof(void *), bytes) == 0;
1059 }),
1060 testing::ExitedWithCode(0), "");
1061 EXPECT_EXIT(CheckAllocationFunction(
1062 [](size_t bytes) { return aligned_alloc(sizeof(void*), bytes) != nullptr; }),
1063 testing::ExitedWithCode(0), "");
1064 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) {
1065 void* p = malloc(1024 * 1024);
1066 return realloc(p, bytes) != nullptr;
1067 }),
1068 testing::ExitedWithCode(0), "");
1069#if !defined(__LP64__)
1070 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return pvalloc(bytes) != nullptr; }),
1071 testing::ExitedWithCode(0), "");
1072 EXPECT_EXIT(CheckAllocationFunction([](size_t bytes) { return valloc(bytes) != nullptr; }),
1073 testing::ExitedWithCode(0), "");
1074#endif
1075#else
Elliott Hughes10907202019-03-27 08:51:02 -07001076 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -08001077#endif
1078}
1079
1080TEST(android_mallopt, set_allocation_limit_multiple) {
1081#if defined(__BIONIC__)
1082 // Only the first set should work.
1083 size_t limit = 256 * 1024 * 1024;
1084 ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
1085 limit = 32 * 1024 * 1024;
1086 ASSERT_FALSE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
1087#else
Elliott Hughes10907202019-03-27 08:51:02 -07001088 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -08001089#endif
1090}
1091
1092#if defined(__BIONIC__)
1093static constexpr size_t kAllocationSize = 8 * 1024 * 1024;
1094
1095static size_t GetMaxAllocations() {
1096 size_t max_pointers = 0;
1097 void* ptrs[20];
1098 for (size_t i = 0; i < sizeof(ptrs) / sizeof(void*); i++) {
1099 ptrs[i] = malloc(kAllocationSize);
1100 if (ptrs[i] == nullptr) {
1101 max_pointers = i;
1102 break;
1103 }
1104 }
1105 for (size_t i = 0; i < max_pointers; i++) {
1106 free(ptrs[i]);
1107 }
1108 return max_pointers;
1109}
1110
1111static void VerifyMaxPointers(size_t max_pointers) {
1112 // Now verify that we can allocate the same number as before.
1113 void* ptrs[20];
1114 for (size_t i = 0; i < max_pointers; i++) {
1115 ptrs[i] = malloc(kAllocationSize);
1116 ASSERT_TRUE(ptrs[i] != nullptr) << "Failed to allocate on iteration " << i;
1117 }
1118
1119 // Make sure the next allocation still fails.
1120 ASSERT_TRUE(malloc(kAllocationSize) == nullptr);
1121 for (size_t i = 0; i < max_pointers; i++) {
1122 free(ptrs[i]);
1123 }
1124}
1125#endif
1126
1127TEST(android_mallopt, set_allocation_limit_realloc_increase) {
1128#if defined(__BIONIC__)
1129 size_t limit = 128 * 1024 * 1024;
1130 ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
1131
1132 size_t max_pointers = GetMaxAllocations();
1133 ASSERT_TRUE(max_pointers != 0) << "Limit never reached.";
1134
1135 void* memory = malloc(10 * 1024 * 1024);
1136 ASSERT_TRUE(memory != nullptr);
1137
1138 // Increase size.
1139 memory = realloc(memory, 20 * 1024 * 1024);
1140 ASSERT_TRUE(memory != nullptr);
1141 memory = realloc(memory, 40 * 1024 * 1024);
1142 ASSERT_TRUE(memory != nullptr);
1143 memory = realloc(memory, 60 * 1024 * 1024);
1144 ASSERT_TRUE(memory != nullptr);
1145 memory = realloc(memory, 80 * 1024 * 1024);
1146 ASSERT_TRUE(memory != nullptr);
1147 // Now push past limit.
1148 memory = realloc(memory, 130 * 1024 * 1024);
1149 ASSERT_TRUE(memory == nullptr);
1150
1151 VerifyMaxPointers(max_pointers);
1152#else
Elliott Hughes10907202019-03-27 08:51:02 -07001153 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -08001154#endif
1155}
1156
1157TEST(android_mallopt, set_allocation_limit_realloc_decrease) {
1158#if defined(__BIONIC__)
1159 size_t limit = 100 * 1024 * 1024;
1160 ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
1161
1162 size_t max_pointers = GetMaxAllocations();
1163 ASSERT_TRUE(max_pointers != 0) << "Limit never reached.";
1164
1165 void* memory = malloc(80 * 1024 * 1024);
1166 ASSERT_TRUE(memory != nullptr);
1167
1168 // Decrease size.
1169 memory = realloc(memory, 60 * 1024 * 1024);
1170 ASSERT_TRUE(memory != nullptr);
1171 memory = realloc(memory, 40 * 1024 * 1024);
1172 ASSERT_TRUE(memory != nullptr);
1173 memory = realloc(memory, 20 * 1024 * 1024);
1174 ASSERT_TRUE(memory != nullptr);
1175 memory = realloc(memory, 10 * 1024 * 1024);
1176 ASSERT_TRUE(memory != nullptr);
1177 free(memory);
1178
1179 VerifyMaxPointers(max_pointers);
1180#else
Elliott Hughes10907202019-03-27 08:51:02 -07001181 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -08001182#endif
1183}
1184
1185TEST(android_mallopt, set_allocation_limit_realloc_free) {
1186#if defined(__BIONIC__)
1187 size_t limit = 100 * 1024 * 1024;
1188 ASSERT_TRUE(android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit)));
1189
1190 size_t max_pointers = GetMaxAllocations();
1191 ASSERT_TRUE(max_pointers != 0) << "Limit never reached.";
1192
1193 void* memory = malloc(60 * 1024 * 1024);
1194 ASSERT_TRUE(memory != nullptr);
1195
1196 memory = realloc(memory, 0);
1197 ASSERT_TRUE(memory == nullptr);
1198
1199 VerifyMaxPointers(max_pointers);
1200#else
Elliott Hughes10907202019-03-27 08:51:02 -07001201 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -08001202#endif
1203}
1204
1205#if defined(__BIONIC__)
1206static void* SetAllocationLimit(void* data) {
1207 std::atomic_bool* go = reinterpret_cast<std::atomic_bool*>(data);
1208 while (!go->load()) {
1209 }
1210 size_t limit = 500 * 1024 * 1024;
1211 if (android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &limit, sizeof(limit))) {
1212 return reinterpret_cast<void*>(-1);
1213 }
1214 return nullptr;
1215}
1216
1217static void SetAllocationLimitMultipleThreads() {
1218 std::atomic_bool go;
1219 go = false;
1220
1221 static constexpr size_t kNumThreads = 4;
1222 pthread_t threads[kNumThreads];
1223 for (size_t i = 0; i < kNumThreads; i++) {
1224 ASSERT_EQ(0, pthread_create(&threads[i], nullptr, SetAllocationLimit, &go));
1225 }
1226
1227 // Let them go all at once.
1228 go = true;
Ryan Savitski175c8862020-01-02 19:54:57 +00001229 // Send hardcoded signal (BIONIC_SIGNAL_PROFILER with value 0) to trigger
1230 // heapprofd handler.
1231 union sigval signal_value;
1232 signal_value.sival_int = 0;
Christopher Ferrisb874c332020-01-21 16:39:05 -08001233 ASSERT_EQ(0, sigqueue(getpid(), BIONIC_SIGNAL_PROFILER, signal_value));
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -08001234
1235 size_t num_successful = 0;
1236 for (size_t i = 0; i < kNumThreads; i++) {
1237 void* result;
1238 ASSERT_EQ(0, pthread_join(threads[i], &result));
1239 if (result != nullptr) {
1240 num_successful++;
1241 }
1242 }
1243 ASSERT_EQ(1U, num_successful);
1244 exit(0);
1245}
1246#endif
1247
1248TEST(android_mallopt, set_allocation_limit_multiple_threads) {
1249#if defined(__BIONIC__)
1250 if (IsDynamic()) {
1251 ASSERT_TRUE(android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0));
1252 }
1253
1254 // Run this a number of times as a stress test.
1255 for (size_t i = 0; i < 100; i++) {
1256 // Not using ASSERT_EXIT because errors messages are not displayed.
1257 pid_t pid;
1258 if ((pid = fork()) == 0) {
1259 ASSERT_NO_FATAL_FAILURE(SetAllocationLimitMultipleThreads());
1260 }
1261 ASSERT_NE(-1, pid);
1262 int status;
1263 ASSERT_EQ(pid, wait(&status));
1264 ASSERT_EQ(0, WEXITSTATUS(status));
1265 }
1266#else
Elliott Hughes10907202019-03-27 08:51:02 -07001267 GTEST_SKIP() << "bionic extension";
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -08001268#endif
1269}
Peter Collingbourne5d3aa862020-09-11 15:05:17 -07001270
Mitch Phillips9cad8422021-01-20 16:03:27 -08001271void TestHeapZeroing(int num_iterations, int (*get_alloc_size)(int iteration)) {
1272 std::vector<void*> allocs;
1273 constexpr int kMaxBytesToCheckZero = 64;
1274 const char kBlankMemory[kMaxBytesToCheckZero] = {};
1275
1276 for (int i = 0; i < num_iterations; ++i) {
1277 int size = get_alloc_size(i);
1278 allocs.push_back(malloc(size));
1279 memset(allocs.back(), 'X', std::min(size, kMaxBytesToCheckZero));
1280 }
1281
1282 for (void* alloc : allocs) {
1283 free(alloc);
1284 }
1285 allocs.clear();
1286
1287 for (int i = 0; i < num_iterations; ++i) {
1288 int size = get_alloc_size(i);
1289 allocs.push_back(malloc(size));
1290 ASSERT_EQ(0, memcmp(allocs.back(), kBlankMemory, std::min(size, kMaxBytesToCheckZero)));
1291 }
1292
1293 for (void* alloc : allocs) {
1294 free(alloc);
1295 }
1296}
1297
1298TEST(malloc, zero_init) {
1299#if defined(__BIONIC__)
1300 SKIP_WITH_HWASAN << "hwasan does not implement mallopt";
1301 bool allocator_scudo;
1302 GetAllocatorVersion(&allocator_scudo);
1303 if (!allocator_scudo) {
1304 GTEST_SKIP() << "scudo allocator only test";
1305 }
1306
1307 mallopt(M_BIONIC_ZERO_INIT, 1);
1308
1309 // Test using a block of 4K small (1-32 byte) allocations.
1310 TestHeapZeroing(/* num_iterations */ 0x1000, [](int iteration) -> int {
1311 return 1 + iteration % 32;
1312 });
1313
1314 // Also test large allocations that land in the scudo secondary, as this is
1315 // the only part of Scudo that's changed by enabling zero initialization with
1316 // MTE. Uses 32 allocations, totalling 60MiB memory. Decay time (time to
1317 // release secondary allocations back to the OS) was modified to 0ms/1ms by
1318 // mallopt_decay. Ensure that we delay for at least a second before releasing
1319 // pages to the OS in order to avoid implicit zeroing by the kernel.
1320 mallopt(M_DECAY_TIME, 1000);
1321 TestHeapZeroing(/* num_iterations */ 32, [](int iteration) -> int {
1322 return 1 << (19 + iteration % 4);
1323 });
1324
1325#else
1326 GTEST_SKIP() << "bionic-only test";
1327#endif
1328}
1329
1330// Note that MTE is enabled on cc_tests on devices that support MTE.
1331TEST(malloc, disable_mte) {
Peter Collingbourne5d3aa862020-09-11 15:05:17 -07001332#if defined(__BIONIC__)
1333 if (!mte_supported()) {
1334 GTEST_SKIP() << "This function can only be tested with MTE";
1335 }
1336
Peter Collingbourne5d3aa862020-09-11 15:05:17 -07001337 sem_t sem;
1338 ASSERT_EQ(0, sem_init(&sem, 0, 0));
1339
1340 pthread_t thread;
1341 ASSERT_EQ(0, pthread_create(
1342 &thread, nullptr,
1343 [](void* ptr) -> void* {
1344 auto* sem = reinterpret_cast<sem_t*>(ptr);
1345 sem_wait(sem);
1346 return reinterpret_cast<void*>(prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0));
1347 },
1348 &sem));
1349
Mitch Phillips9cad8422021-01-20 16:03:27 -08001350 ASSERT_EQ(1, mallopt(M_BIONIC_SET_HEAP_TAGGING_LEVEL, M_HEAP_TAGGING_LEVEL_NONE));
Peter Collingbourne5d3aa862020-09-11 15:05:17 -07001351 ASSERT_EQ(0, sem_post(&sem));
1352
1353 int my_tagged_addr_ctrl = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0);
1354 ASSERT_EQ(PR_MTE_TCF_NONE, my_tagged_addr_ctrl & PR_MTE_TCF_MASK);
1355
1356 void* retval;
1357 ASSERT_EQ(0, pthread_join(thread, &retval));
1358 int thread_tagged_addr_ctrl = reinterpret_cast<uintptr_t>(retval);
1359 ASSERT_EQ(my_tagged_addr_ctrl, thread_tagged_addr_ctrl);
Peter Collingbourne5d3aa862020-09-11 15:05:17 -07001360#else
1361 GTEST_SKIP() << "bionic extension";
1362#endif
1363}
Peter Collingbourne2659d7b2021-03-05 13:31:41 -08001364
1365TEST(malloc, allocation_slack) {
1366#if defined(__BIONIC__)
Christopher Ferris7c0ce862021-06-08 15:33:22 -07001367 SKIP_WITH_NATIVE_BRIDGE; // http://b/189606147
1368
Peter Collingbourne2659d7b2021-03-05 13:31:41 -08001369 bool allocator_scudo;
1370 GetAllocatorVersion(&allocator_scudo);
1371 if (!allocator_scudo) {
1372 GTEST_SKIP() << "scudo allocator only test";
1373 }
1374
1375 // Test that older target SDK levels let you access a few bytes off the end of
1376 // a large allocation.
1377 android_set_application_target_sdk_version(29);
1378 auto p = std::make_unique<char[]>(131072);
1379 volatile char *vp = p.get();
1380 volatile char oob ATTRIBUTE_UNUSED = vp[131072];
1381#else
1382 GTEST_SKIP() << "bionic extension";
1383#endif
1384}