1885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris/*
2885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * Copyright (C) 2013 The Android Open Source Project
3885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris *
4885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * Licensed under the Apache License, Version 2.0 (the "License");
5885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * you may not use this file except in compliance with the License.
6885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * You may obtain a copy of the License at
7885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris *
8885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris *      http://www.apache.org/licenses/LICENSE-2.0
9885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris *
10885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * Unless required by applicable law or agreed to in writing, software
11885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * distributed under the License is distributed on an "AS IS" BASIS,
12885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * See the License for the specific language governing permissions and
14885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * limitations under the License.
15885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris */
16885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
17885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris#include <gtest/gtest.h>
18885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
19a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris#include <limits.h>
20a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris#include <stdint.h>
21885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris#include <stdlib.h>
22885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris#include <malloc.h>
23a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris#include <unistd.h>
24885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
254caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert#include <tinyxml2.h>
264caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert
27636196438180fd37027bf7b7119a436169b6923eChristopher Ferris#include "private/bionic_config.h"
28e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albert
29885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_std) {
30885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  // Simple malloc test.
31885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  void *ptr = malloc(100);
32885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
33885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(100U, malloc_usable_size(ptr));
34885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  free(ptr);
35885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris}
36885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
37a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, malloc_overflow) {
38a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  errno = 0;
39a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, malloc(SIZE_MAX));
40a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(ENOMEM, errno);
41a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris}
42a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris
43885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_std) {
44885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  // Simple calloc test.
45885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  size_t alloc_len = 100;
46885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  char *ptr = (char *)calloc(1, alloc_len);
47885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
48885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(alloc_len, malloc_usable_size(ptr));
49885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < alloc_len; i++) {
50885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0, ptr[i]);
51885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
52885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  free(ptr);
53885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris}
54885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
55a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, calloc_illegal) {
56a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  errno = 0;
57a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, calloc(-1, 100));
58a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(ENOMEM, errno);
59a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris}
60a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris
61a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, calloc_overflow) {
62a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  errno = 0;
63a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, calloc(1, SIZE_MAX));
64a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(ENOMEM, errno);
65a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  errno = 0;
66a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, calloc(SIZE_MAX, SIZE_MAX));
67a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(ENOMEM, errno);
68a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  errno = 0;
69a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, calloc(2, SIZE_MAX));
70a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(ENOMEM, errno);
71a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  errno = 0;
72a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, calloc(SIZE_MAX, 2));
73a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(ENOMEM, errno);
74a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris}
75a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris
76885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, memalign_multiple) {
77885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  // Memalign test where the alignment is any value.
78885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i <= 12; i++) {
79885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    for (size_t alignment = 1 << i; alignment < (1U << (i+1)); alignment++) {
80a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris      char *ptr = reinterpret_cast<char*>(memalign(alignment, 100));
81a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris      ASSERT_TRUE(ptr != NULL) << "Failed at alignment " << alignment;
82a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris      ASSERT_LE(100U, malloc_usable_size(ptr)) << "Failed at alignment " << alignment;
83a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris      ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) % ((1U << i)))
84a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris          << "Failed at alignment " << alignment;
85885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris      free(ptr);
86885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    }
87885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
88885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris}
89885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
90a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, memalign_overflow) {
91a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, memalign(4096, SIZE_MAX));
92a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris}
93a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris
94a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, memalign_non_power2) {
95a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  void* ptr;
96a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  for (size_t align = 0; align <= 256; align++) {
97a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris    ptr = memalign(align, 1024);
98a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris    ASSERT_TRUE(ptr != NULL) << "Failed at align " << align;
99a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris    free(ptr);
100a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  }
101a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris}
102a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris
103885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, memalign_realloc) {
104885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  // Memalign and then realloc the pointer a couple of times.
105885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t alignment = 1; alignment <= 4096; alignment <<= 1) {
106885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    char *ptr = (char*)memalign(alignment, 100);
107885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_TRUE(ptr != NULL);
108885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_LE(100U, malloc_usable_size(ptr));
109885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0U, (intptr_t)ptr % alignment);
110885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    memset(ptr, 0x23, 100);
111885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
112885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ptr = (char*)realloc(ptr, 200);
113885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_TRUE(ptr != NULL);
114885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_LE(200U, malloc_usable_size(ptr));
115885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_TRUE(ptr != NULL);
116885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    for (size_t i = 0; i < 100; i++) {
117885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris      ASSERT_EQ(0x23, ptr[i]);
118885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    }
119885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    memset(ptr, 0x45, 200);
120885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
121885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ptr = (char*)realloc(ptr, 300);
122885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_TRUE(ptr != NULL);
123885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_LE(300U, malloc_usable_size(ptr));
124885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    for (size_t i = 0; i < 200; i++) {
125885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris      ASSERT_EQ(0x45, ptr[i]);
126885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    }
127885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    memset(ptr, 0x67, 300);
128885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
129885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ptr = (char*)realloc(ptr, 250);
130885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_TRUE(ptr != NULL);
131885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_LE(250U, malloc_usable_size(ptr));
132885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    for (size_t i = 0; i < 250; i++) {
133885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris      ASSERT_EQ(0x67, ptr[i]);
134885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    }
135885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    free(ptr);
136885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
137885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris}
138885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
139885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_realloc_larger) {
140885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  // Realloc to a larger size, malloc is used for the original allocation.
141885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  char *ptr = (char *)malloc(100);
142885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
143885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(100U, malloc_usable_size(ptr));
144885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  memset(ptr, 67, 100);
145885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
146885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char *)realloc(ptr, 200);
147885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
148885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(200U, malloc_usable_size(ptr));
149885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 100; i++) {
150885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(67, ptr[i]);
151885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
152885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  free(ptr);
153885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris}
154885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
155885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_realloc_smaller) {
156885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  // Realloc to a smaller size, malloc is used for the original allocation.
157885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  char *ptr = (char *)malloc(200);
158885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
159885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(200U, malloc_usable_size(ptr));
160885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  memset(ptr, 67, 200);
161885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
162885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char *)realloc(ptr, 100);
163885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
164885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(100U, malloc_usable_size(ptr));
165885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 100; i++) {
166885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(67, ptr[i]);
167885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
168885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  free(ptr);
169885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris}
170885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
171885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_multiple_realloc) {
172885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  // Multiple reallocs, malloc is used for the original allocation.
173885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  char *ptr = (char *)malloc(200);
174885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
175885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(200U, malloc_usable_size(ptr));
176885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  memset(ptr, 0x23, 200);
177885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
178885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char *)realloc(ptr, 100);
179885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
180885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(100U, malloc_usable_size(ptr));
181885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 100; i++) {
182885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0x23, ptr[i]);
183885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
184885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
185885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char*)realloc(ptr, 50);
186885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
187885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(50U, malloc_usable_size(ptr));
188885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 50; i++) {
189885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0x23, ptr[i]);
190885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
191885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
192885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char*)realloc(ptr, 150);
193885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
194885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(150U, malloc_usable_size(ptr));
195885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 50; i++) {
196885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0x23, ptr[i]);
197885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
198885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  memset(ptr, 0x23, 150);
199885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
200885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char*)realloc(ptr, 425);
201885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
202885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(425U, malloc_usable_size(ptr));
203885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 150; i++) {
204885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0x23, ptr[i]);
205885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
206885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  free(ptr);
207885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris}
208a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris
209885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_realloc_larger) {
210885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  // Realloc to a larger size, calloc is used for the original allocation.
211885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  char *ptr = (char *)calloc(1, 100);
212885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
213885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(100U, malloc_usable_size(ptr));
214885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
215885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char *)realloc(ptr, 200);
216885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
217885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(200U, malloc_usable_size(ptr));
218885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 100; i++) {
219885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0, ptr[i]);
220885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
221885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  free(ptr);
222885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris}
223885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
224885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_realloc_smaller) {
225885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  // Realloc to a smaller size, calloc is used for the original allocation.
226885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  char *ptr = (char *)calloc(1, 200);
227885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
228885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(200U, malloc_usable_size(ptr));
229885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
230885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char *)realloc(ptr, 100);
231885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
232885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(100U, malloc_usable_size(ptr));
233885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 100; i++) {
234885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0, ptr[i]);
235885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
236885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  free(ptr);
237885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris}
238885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
239885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_multiple_realloc) {
240885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  // Multiple reallocs, calloc is used for the original allocation.
241885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  char *ptr = (char *)calloc(1, 200);
242885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
243885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(200U, malloc_usable_size(ptr));
244885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
245885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char *)realloc(ptr, 100);
246885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
247885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(100U, malloc_usable_size(ptr));
248885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 100; i++) {
249885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0, ptr[i]);
250885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
251885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
252885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char*)realloc(ptr, 50);
253885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
254885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(50U, malloc_usable_size(ptr));
255885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 50; i++) {
256885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0, ptr[i]);
257885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
258885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
259885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char*)realloc(ptr, 150);
260885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
261885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(150U, malloc_usable_size(ptr));
262885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 50; i++) {
263885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0, ptr[i]);
264885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
265885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  memset(ptr, 0, 150);
266885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
267885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ptr = (char*)realloc(ptr, 425);
268885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_TRUE(ptr != NULL);
269885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  ASSERT_LE(425U, malloc_usable_size(ptr));
270885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  for (size_t i = 0; i < 150; i++) {
271885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris    ASSERT_EQ(0, ptr[i]);
272885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  }
273a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  free(ptr);
274a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris}
275885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris
276a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, realloc_overflow) {
277a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  errno = 0;
278a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, realloc(NULL, SIZE_MAX));
279a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(ENOMEM, errno);
280a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  void* ptr = malloc(100);
281a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_TRUE(ptr != NULL);
282a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  errno = 0;
283a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, realloc(ptr, SIZE_MAX));
284a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(ENOMEM, errno);
285885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris  free(ptr);
286885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris}
28772bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris
288e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albert#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
289e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albertextern "C" void* pvalloc(size_t);
290e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albertextern "C" void* valloc(size_t);
291e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albert
292a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, pvalloc_std) {
293a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  size_t pagesize = sysconf(_SC_PAGESIZE);
294a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  void* ptr = pvalloc(100);
295a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_TRUE(ptr != NULL);
296a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0);
297a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_LE(pagesize, malloc_usable_size(ptr));
298a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  free(ptr);
299a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris}
30072bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris
301a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, pvalloc_overflow) {
302a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, pvalloc(SIZE_MAX));
30372bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris}
30472bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris
305a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, valloc_std) {
306a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  size_t pagesize = sysconf(_SC_PAGESIZE);
307a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  void* ptr = pvalloc(100);
308a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_TRUE(ptr != NULL);
309a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0);
310a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  free(ptr);
311a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris}
312a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris
313a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, valloc_overflow) {
314a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris  ASSERT_EQ(NULL, valloc(SIZE_MAX));
31572bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris}
316e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albert#endif
3174caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert
3184caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan AlbertTEST(malloc, malloc_info) {
3194caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert#ifdef __BIONIC__
3204caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  char* buf;
3214caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  size_t bufsize;
3224caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  FILE* memstream = open_memstream(&buf, &bufsize);
3234caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  ASSERT_NE(nullptr, memstream);
3244caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  ASSERT_EQ(0, malloc_info(0, memstream));
3254caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  ASSERT_EQ(0, fclose(memstream));
3264caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert
3274caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  tinyxml2::XMLDocument doc;
3284caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(buf));
3294caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert
3304caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  auto root = doc.FirstChildElement();
3314caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  ASSERT_NE(nullptr, root);
3324caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  ASSERT_STREQ("malloc", root->Name());
3334caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  ASSERT_STREQ("jemalloc-1", root->Attribute("version"));
3344caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert
3354caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  auto arena = root->FirstChildElement();
3364caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  for (; arena != nullptr; arena = arena->NextSiblingElement()) {
3374caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert    int val;
3384caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert
3394caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert    ASSERT_STREQ("heap", arena->Name());
3404caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert    ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val));
3414caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert    ASSERT_EQ(tinyxml2::XML_SUCCESS,
3424caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert              arena->FirstChildElement("allocated-large")->QueryIntText(&val));
3434caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert    ASSERT_EQ(tinyxml2::XML_SUCCESS,
3444caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert              arena->FirstChildElement("allocated-huge")->QueryIntText(&val));
3454caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert    ASSERT_EQ(tinyxml2::XML_SUCCESS,
3464caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert              arena->FirstChildElement("allocated-bins")->QueryIntText(&val));
3474caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert    ASSERT_EQ(tinyxml2::XML_SUCCESS,
3484caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert              arena->FirstChildElement("bins-total")->QueryIntText(&val));
3494caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert
3504caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert    auto bin = arena->FirstChildElement("bin");
3514caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert    for (; bin != nullptr; bin = bin ->NextSiblingElement()) {
3524caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert      if (strcmp(bin->Name(), "bin") == 0) {
3534caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert        ASSERT_EQ(tinyxml2::XML_SUCCESS, bin->QueryIntAttribute("nr", &val));
3544caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert        ASSERT_EQ(tinyxml2::XML_SUCCESS,
3554caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert                  bin->FirstChildElement("allocated")->QueryIntText(&val));
3564caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert        ASSERT_EQ(tinyxml2::XML_SUCCESS,
3574caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert                  bin->FirstChildElement("nmalloc")->QueryIntText(&val));
3584caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert        ASSERT_EQ(tinyxml2::XML_SUCCESS,
3594caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert                  bin->FirstChildElement("ndalloc")->QueryIntText(&val));
3604caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert      }
3614caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert    }
3624caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert  }
3634caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert#endif
3644caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert}
365ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris
366ad33ebead801f24d3197bc16f875501729e98485Christopher FerrisTEST(malloc, calloc_usable_size) {
367ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris  for (size_t size = 1; size <= 2048; size++) {
368ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    void* pointer = malloc(size);
369ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    ASSERT_TRUE(pointer != nullptr);
370ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    memset(pointer, 0xeb, malloc_usable_size(pointer));
371ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    free(pointer);
372ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris
373ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    // We should get a previous pointer that has been set to non-zero.
374ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    // If calloc does not zero out all of the data, this will fail.
375ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    uint8_t* zero_mem = reinterpret_cast<uint8_t*>(calloc(1, size));
376ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    ASSERT_TRUE(pointer != nullptr);
377ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    size_t usable_size = malloc_usable_size(zero_mem);
378ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    for (size_t i = 0; i < usable_size; i++) {
379ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris      ASSERT_EQ(0, zero_mem[i]) << "Failed at allocation size " << size << " at byte " << i;
380ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    }
381ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris    free(zero_mem);
382ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris  }
383ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris}
384884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes
385884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott HughesTEST(malloc, malloc_0) {
386884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  void* p = malloc(0);
387884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  ASSERT_TRUE(p != nullptr);
388884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  free(p);
389884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes}
390884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes
391884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott HughesTEST(malloc, calloc_0_0) {
392884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  void* p = calloc(0, 0);
393884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  ASSERT_TRUE(p != nullptr);
394884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  free(p);
395884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes}
396884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes
397884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott HughesTEST(malloc, calloc_0_1) {
398884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  void* p = calloc(0, 1);
399884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  ASSERT_TRUE(p != nullptr);
400884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  free(p);
401884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes}
402884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes
403884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott HughesTEST(malloc, calloc_1_0) {
404884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  void* p = calloc(1, 0);
405884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  ASSERT_TRUE(p != nullptr);
406884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  free(p);
407884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes}
408884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes
409884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott HughesTEST(malloc, realloc_nullptr_0) {
410884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  // realloc(nullptr, size) is actually malloc(size).
411884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  void* p = realloc(nullptr, 0);
412884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  ASSERT_TRUE(p != nullptr);
413884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  free(p);
414884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes}
415884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes
416884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott HughesTEST(malloc, realloc_0) {
417884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  void* p = malloc(1024);
418884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  ASSERT_TRUE(p != nullptr);
419884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  // realloc(p, 0) is actually free(p).
420884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  void* p2 = realloc(p, 0);
421884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes  ASSERT_TRUE(p2 == nullptr);
422884f76e3aa081740f7cfe582b11af7446bf77bd9Elliott Hughes}
42372df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
42472df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferrisconstexpr size_t MAX_LOOPS = 200;
42572df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
42672df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris// Make sure that memory returned by malloc is aligned to allow these data types.
42772df6708c829a4c6494936fdfbda6dc7e68e647bChristopher FerrisTEST(malloc, verify_alignment) {
42872df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  uint32_t** values_32 = new uint32_t*[MAX_LOOPS];
42972df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  uint64_t** values_64 = new uint64_t*[MAX_LOOPS];
43072df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  long double** values_ldouble = new long double*[MAX_LOOPS];
43172df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  // Use filler to attempt to force the allocator to get potentially bad alignments.
43272df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  void** filler = new void*[MAX_LOOPS];
43372df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
43472df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  for (size_t i = 0; i < MAX_LOOPS; i++) {
43572df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    // Check uint32_t pointers.
43672df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    filler[i] = malloc(1);
43772df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_TRUE(filler[i] != nullptr);
43872df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
43972df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    values_32[i] = reinterpret_cast<uint32_t*>(malloc(sizeof(uint32_t)));
44072df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_TRUE(values_32[i] != nullptr);
44172df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    *values_32[i] = i;
44272df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_EQ(*values_32[i], i);
44372df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_32[i]) & (sizeof(uint32_t) - 1));
44472df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
44572df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    free(filler[i]);
44672df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  }
44772df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
44872df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  for (size_t i = 0; i < MAX_LOOPS; i++) {
44972df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    // Check uint64_t pointers.
45072df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    filler[i] = malloc(1);
45172df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_TRUE(filler[i] != nullptr);
45272df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
45372df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    values_64[i] = reinterpret_cast<uint64_t*>(malloc(sizeof(uint64_t)));
45472df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_TRUE(values_64[i] != nullptr);
45572df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    *values_64[i] = 0x1000 + i;
45672df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_EQ(*values_64[i], 0x1000 + i);
45772df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_64[i]) & (sizeof(uint64_t) - 1));
45872df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
45972df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    free(filler[i]);
46072df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  }
46172df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
46272df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  for (size_t i = 0; i < MAX_LOOPS; i++) {
46372df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    // Check long double pointers.
46472df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    filler[i] = malloc(1);
46572df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_TRUE(filler[i] != nullptr);
46672df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
46772df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    values_ldouble[i] = reinterpret_cast<long double*>(malloc(sizeof(long double)));
46872df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_TRUE(values_ldouble[i] != nullptr);
46972df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    *values_ldouble[i] = 5.5 + i;
47072df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_DOUBLE_EQ(*values_ldouble[i], 5.5 + i);
47172df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    // 32 bit glibc has a long double size of 12 bytes, so hardcode the
47272df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    // required alignment to 0x7.
47372df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris#if !defined(__BIONIC__) && !defined(__LP64__)
47472df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_ldouble[i]) & 0x7);
47572df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris#else
47672df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(values_ldouble[i]) & (sizeof(long double) - 1));
47772df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris#endif
47872df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
47972df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    free(filler[i]);
48072df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  }
48172df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
48272df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  for (size_t i = 0; i < MAX_LOOPS; i++) {
48372df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    free(values_32[i]);
48472df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    free(values_64[i]);
48572df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris    free(values_ldouble[i]);
48672df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  }
48772df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris
48872df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  delete[] filler;
48972df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  delete[] values_32;
49072df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  delete[] values_64;
49172df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris  delete[] values_ldouble;
49272df6708c829a4c6494936fdfbda6dc7e68e647bChristopher Ferris}
493a1c0d2fd4ce96e123c4ae6506c9d637d747e1fe2Christopher Ferris
494a1c0d2fd4ce96e123c4ae6506c9d637d747e1fe2Christopher FerrisTEST(malloc, mallopt_smoke) {
495a1c0d2fd4ce96e123c4ae6506c9d637d747e1fe2Christopher Ferris  errno = 0;
496a1c0d2fd4ce96e123c4ae6506c9d637d747e1fe2Christopher Ferris  ASSERT_EQ(0, mallopt(-1000, 1));
497a1c0d2fd4ce96e123c4ae6506c9d637d747e1fe2Christopher Ferris  // mallopt doesn't set errno.
498a1c0d2fd4ce96e123c4ae6506c9d637d747e1fe2Christopher Ferris  ASSERT_EQ(0, errno);
499a1c0d2fd4ce96e123c4ae6506c9d637d747e1fe2Christopher Ferris}
500