malloc_test.cpp revision ad33ebead801f24d3197bc16f875501729e98485
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 103a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, posix_memalign_non_power2) { 104a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris void* ptr; 105a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_EQ(EINVAL, posix_memalign(&ptr, 17, 1024)); 106a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris} 107a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris 108a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, posix_memalign_overflow) { 109a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris void* ptr; 110a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX)); 111a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris} 112a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris 113885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, memalign_realloc) { 114885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Memalign and then realloc the pointer a couple of times. 115885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t alignment = 1; alignment <= 4096; alignment <<= 1) { 116885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char*)memalign(alignment, 100); 117885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 118885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 119885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0U, (intptr_t)ptr % alignment); 120885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0x23, 100); 121885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 122885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 200); 123885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 124885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 125885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 126885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 127885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x23, ptr[i]); 128885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 129885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0x45, 200); 130885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 131885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 300); 132885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 133885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(300U, malloc_usable_size(ptr)); 134885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 200; i++) { 135885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x45, ptr[i]); 136885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 137885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0x67, 300); 138885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 139885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 250); 140885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 141885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(250U, malloc_usable_size(ptr)); 142885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 250; i++) { 143885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x67, ptr[i]); 144885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 145885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 146885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 147885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 148885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 149885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_realloc_larger) { 150885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Realloc to a larger size, malloc is used for the original allocation. 151885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)malloc(100); 152885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 153885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 154885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 67, 100); 155885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 156885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 200); 157885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 158885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 159885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 160885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(67, ptr[i]); 161885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 162885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 163885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 164885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 165885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_realloc_smaller) { 166885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Realloc to a smaller size, malloc is used for the original allocation. 167885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)malloc(200); 168885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 169885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 170885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 67, 200); 171885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 172885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 100); 173885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 174885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 175885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 176885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(67, ptr[i]); 177885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 178885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 179885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 180885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 181885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_multiple_realloc) { 182885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Multiple reallocs, malloc is used for the original allocation. 183885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)malloc(200); 184885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 185885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 186885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0x23, 200); 187885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 188885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 100); 189885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 190885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 191885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 192885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x23, ptr[i]); 193885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 194885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 195885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 50); 196885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 197885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(50U, malloc_usable_size(ptr)); 198885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 50; i++) { 199885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x23, ptr[i]); 200885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 201885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 202885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 150); 203885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 204885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(150U, malloc_usable_size(ptr)); 205885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 50; i++) { 206885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x23, ptr[i]); 207885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 208885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0x23, 150); 209885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 210885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 425); 211885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 212885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(425U, malloc_usable_size(ptr)); 213885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 150; i++) { 214885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x23, ptr[i]); 215885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 216885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 217885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 218a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris 219885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_realloc_larger) { 220885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Realloc to a larger size, calloc is used for the original allocation. 221885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)calloc(1, 100); 222885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 223885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 224885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 225885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 200); 226885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 227885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 228885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 229885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 230885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 231885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 232885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 233885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 234885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_realloc_smaller) { 235885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Realloc to a smaller size, calloc is used for the original allocation. 236885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)calloc(1, 200); 237885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 238885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 239885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 240885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 100); 241885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 242885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 243885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 244885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 245885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 246885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 247885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 248885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 249885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_multiple_realloc) { 250885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Multiple reallocs, calloc is used for the original allocation. 251885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)calloc(1, 200); 252885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 253885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 254885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 255885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 100); 256885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 257885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 258885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 259885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 260885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 261885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 262885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 50); 263885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 264885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(50U, malloc_usable_size(ptr)); 265885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 50; i++) { 266885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 267885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 268885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 269885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 150); 270885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 271885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(150U, malloc_usable_size(ptr)); 272885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 50; i++) { 273885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 274885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 275885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0, 150); 276885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 277885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 425); 278885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 279885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(425U, malloc_usable_size(ptr)); 280885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 150; i++) { 281885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 282885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 283a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris free(ptr); 284a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris} 285885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 286a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, realloc_overflow) { 287a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris errno = 0; 288a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_EQ(NULL, realloc(NULL, SIZE_MAX)); 289a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_EQ(ENOMEM, errno); 290a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris void* ptr = malloc(100); 291a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_TRUE(ptr != NULL); 292a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris errno = 0; 293a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_EQ(NULL, realloc(ptr, SIZE_MAX)); 294a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_EQ(ENOMEM, errno); 295885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 296885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 29772bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris 298e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albert#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) 299e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albertextern "C" void* pvalloc(size_t); 300e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albertextern "C" void* valloc(size_t); 301e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albert 302a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, pvalloc_std) { 303a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris size_t pagesize = sysconf(_SC_PAGESIZE); 304a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris void* ptr = pvalloc(100); 305a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_TRUE(ptr != NULL); 306a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0); 307a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_LE(pagesize, malloc_usable_size(ptr)); 308a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris free(ptr); 309a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris} 31072bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris 311a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, pvalloc_overflow) { 312a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_EQ(NULL, pvalloc(SIZE_MAX)); 31372bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris} 31472bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris 315a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, valloc_std) { 316a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris size_t pagesize = sysconf(_SC_PAGESIZE); 317a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris void* ptr = pvalloc(100); 318a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_TRUE(ptr != NULL); 319a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_TRUE((reinterpret_cast<uintptr_t>(ptr) & (pagesize-1)) == 0); 320a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris free(ptr); 321a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris} 322a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris 323a403780538ac9d1a260e064df6599663f8cc4166Christopher FerrisTEST(malloc, valloc_overflow) { 324a403780538ac9d1a260e064df6599663f8cc4166Christopher Ferris ASSERT_EQ(NULL, valloc(SIZE_MAX)); 32572bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris} 326e5fdaa4f9d102461a4d8a865e6ca84666893b9e7Dan Albert#endif 3274caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert 3284caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan AlbertTEST(malloc, malloc_info) { 3294caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert#ifdef __BIONIC__ 3304caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert char* buf; 3314caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert size_t bufsize; 3324caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert FILE* memstream = open_memstream(&buf, &bufsize); 3334caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_NE(nullptr, memstream); 3344caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(0, malloc_info(0, memstream)); 3354caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(0, fclose(memstream)); 3364caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert 3374caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert tinyxml2::XMLDocument doc; 3384caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(buf)); 3394caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert 3404caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert auto root = doc.FirstChildElement(); 3414caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_NE(nullptr, root); 3424caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_STREQ("malloc", root->Name()); 3434caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_STREQ("jemalloc-1", root->Attribute("version")); 3444caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert 3454caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert auto arena = root->FirstChildElement(); 3464caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert for (; arena != nullptr; arena = arena->NextSiblingElement()) { 3474caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert int val; 3484caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert 3494caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_STREQ("heap", arena->Name()); 3504caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val)); 3514caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(tinyxml2::XML_SUCCESS, 3524caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert arena->FirstChildElement("allocated-large")->QueryIntText(&val)); 3534caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(tinyxml2::XML_SUCCESS, 3544caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert arena->FirstChildElement("allocated-huge")->QueryIntText(&val)); 3554caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(tinyxml2::XML_SUCCESS, 3564caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert arena->FirstChildElement("allocated-bins")->QueryIntText(&val)); 3574caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(tinyxml2::XML_SUCCESS, 3584caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert arena->FirstChildElement("bins-total")->QueryIntText(&val)); 3594caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert 3604caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert auto bin = arena->FirstChildElement("bin"); 3614caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert for (; bin != nullptr; bin = bin ->NextSiblingElement()) { 3624caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert if (strcmp(bin->Name(), "bin") == 0) { 3634caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(tinyxml2::XML_SUCCESS, bin->QueryIntAttribute("nr", &val)); 3644caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(tinyxml2::XML_SUCCESS, 3654caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert bin->FirstChildElement("allocated")->QueryIntText(&val)); 3664caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(tinyxml2::XML_SUCCESS, 3674caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert bin->FirstChildElement("nmalloc")->QueryIntText(&val)); 3684caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert ASSERT_EQ(tinyxml2::XML_SUCCESS, 3694caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert bin->FirstChildElement("ndalloc")->QueryIntText(&val)); 3704caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert } 3714caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert } 3724caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert } 3734caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert#endif 3744caa1f09770ea3e5ca22afbe8aa0900810a0dbfeDan Albert} 375ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris 376ad33ebead801f24d3197bc16f875501729e98485Christopher FerrisTEST(malloc, calloc_usable_size) { 377ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris for (size_t size = 1; size <= 2048; size++) { 378ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris void* pointer = malloc(size); 379ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris ASSERT_TRUE(pointer != nullptr); 380ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris memset(pointer, 0xeb, malloc_usable_size(pointer)); 381ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris free(pointer); 382ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris 383ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris // We should get a previous pointer that has been set to non-zero. 384ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris // If calloc does not zero out all of the data, this will fail. 385ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris uint8_t* zero_mem = reinterpret_cast<uint8_t*>(calloc(1, size)); 386ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris ASSERT_TRUE(pointer != nullptr); 387ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris size_t usable_size = malloc_usable_size(zero_mem); 388ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris for (size_t i = 0; i < usable_size; i++) { 389ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris ASSERT_EQ(0, zero_mem[i]) << "Failed at allocation size " << size << " at byte " << i; 390ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris } 391ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris free(zero_mem); 392ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris } 393ad33ebead801f24d3197bc16f875501729e98485Christopher Ferris} 394