2015-11-17 02:30:32 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Contains a thin layer that calls whatever real native allocator
|
|
|
|
// has been defined. For the libc shared library, this allows the
|
|
|
|
// implementation of a debug malloc that can intercept all of the allocation
|
|
|
|
// calls and add special debugging code to attempt to catch allocation
|
|
|
|
// errors. All of the debugging code is implemented in a separate shared
|
|
|
|
// library that is only loaded when the property "libc.debug.malloc.options"
|
2019-04-16 04:01:08 +02:00
|
|
|
// is set to a non-zero value.
|
2015-11-17 02:30:32 +01:00
|
|
|
|
2019-03-08 19:56:17 +01:00
|
|
|
#include <errno.h>
|
2019-02-09 02:30:58 +01:00
|
|
|
#include <stdint.h>
|
2019-03-02 02:59:51 +01:00
|
|
|
#include <stdio.h>
|
2016-01-29 21:48:18 +01:00
|
|
|
|
2019-09-12 04:05:29 +02:00
|
|
|
#include <platform/bionic/malloc.h>
|
2020-11-26 01:48:54 +01:00
|
|
|
#include <private/ScopedPthreadMutexLocker.h>
|
|
|
|
#include <private/bionic_config.h>
|
2015-11-17 02:30:32 +01:00
|
|
|
|
2020-02-01 04:57:04 +01:00
|
|
|
#include "gwp_asan_wrappers.h"
|
2020-01-09 19:48:22 +01:00
|
|
|
#include "heap_tagging.h"
|
2021-01-21 01:03:27 +01:00
|
|
|
#include "heap_zero_init.h"
|
2019-02-09 02:30:58 +01:00
|
|
|
#include "malloc_common.h"
|
2019-02-16 03:06:15 +01:00
|
|
|
#include "malloc_limit.h"
|
2020-01-08 00:47:47 +01:00
|
|
|
#include "malloc_tagged_pointers.h"
|
2018-09-01 00:36:48 +02:00
|
|
|
|
2019-02-09 02:30:58 +01:00
|
|
|
// =============================================================================
|
|
|
|
// Global variables instantations.
|
|
|
|
// =============================================================================
|
2015-11-17 02:30:32 +01:00
|
|
|
|
2019-02-09 02:30:58 +01:00
|
|
|
// Malloc hooks globals.
|
2018-02-08 03:42:14 +01:00
|
|
|
void* (*volatile __malloc_hook)(size_t, const void*);
|
|
|
|
void* (*volatile __realloc_hook)(void*, size_t, const void*);
|
|
|
|
void (*volatile __free_hook)(void*, const void*);
|
|
|
|
void* (*volatile __memalign_hook)(size_t, size_t, const void*);
|
2019-02-09 02:30:58 +01:00
|
|
|
// =============================================================================
|
2015-11-17 02:30:32 +01:00
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
// Allocation functions
|
|
|
|
// =============================================================================
|
|
|
|
extern "C" void* calloc(size_t n_elements, size_t elem_size) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(dispatch_table->calloc(n_elements, elem_size));
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
Log when malloc functions fail.
This shouldn't happen often, and resulting failures can be hard to debug.
From the bionic unit tests now:
W libc : malloc(18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 100) failed: returning null pointer
W libc : calloc(1, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 18446744073709551615) failed: returning null pointer
W libc : calloc(2, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 2) failed: returning null pointer
W libc : memalign(4096, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x0, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x75d7526070, 18446744073709551615) failed: returning null pointer
W libc : reallocaray(0x0, 9223372036854775812, 2) failed: returning null pointer
W libc : reallocaray(0x0, 2, 9223372036854775812) failed: returning null pointer
Bug: http://b/12821450
Test: ran tests
Change-Id: Ib176814404f4ba1297416dd3e1edd721bf59aeed
2019-02-25 22:21:04 +01:00
|
|
|
void* result = Malloc(calloc)(n_elements, elem_size);
|
|
|
|
if (__predict_false(result == nullptr)) {
|
|
|
|
warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size);
|
|
|
|
}
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(result);
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void free(void* mem) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
2020-01-08 00:47:47 +01:00
|
|
|
mem = MaybeUntagAndCheckPointer(mem);
|
2019-02-04 21:26:02 +01:00
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
|
|
|
dispatch_table->free(mem);
|
2015-11-17 02:30:32 +01:00
|
|
|
} else {
|
|
|
|
Malloc(free)(mem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" struct mallinfo mallinfo() {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
|
|
|
return dispatch_table->mallinfo();
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
|
|
|
return Malloc(mallinfo)();
|
|
|
|
}
|
|
|
|
|
2019-03-02 02:59:51 +01:00
|
|
|
extern "C" int malloc_info(int options, FILE* fp) {
|
|
|
|
auto dispatch_table = GetDispatchTable();
|
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
|
|
|
return dispatch_table->malloc_info(options, fp);
|
|
|
|
}
|
|
|
|
return Malloc(malloc_info)(options, fp);
|
|
|
|
}
|
|
|
|
|
2017-05-16 00:50:19 +02:00
|
|
|
extern "C" int mallopt(int param, int value) {
|
2021-01-14 22:34:20 +01:00
|
|
|
// Some are handled by libc directly rather than by the allocator.
|
|
|
|
if (param == M_BIONIC_SET_HEAP_TAGGING_LEVEL) {
|
|
|
|
ScopedPthreadMutexLocker locker(&g_heap_tagging_lock);
|
|
|
|
return SetHeapTaggingLevel(static_cast<HeapTaggingLevel>(value));
|
|
|
|
}
|
2021-01-21 01:03:27 +01:00
|
|
|
if (param == M_BIONIC_ZERO_INIT) {
|
|
|
|
return SetHeapZeroInitialize(value);
|
2021-01-14 22:34:20 +01:00
|
|
|
}
|
|
|
|
// The rest we pass on...
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
|
|
|
return dispatch_table->mallopt(param, value);
|
2017-05-16 00:50:19 +02:00
|
|
|
}
|
|
|
|
return Malloc(mallopt)(param, value);
|
|
|
|
}
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
extern "C" void* malloc(size_t bytes) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
2020-01-08 00:47:47 +01:00
|
|
|
void *result;
|
2019-02-04 21:26:02 +01:00
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
2020-01-08 00:47:47 +01:00
|
|
|
result = dispatch_table->malloc(bytes);
|
|
|
|
} else {
|
|
|
|
result = Malloc(malloc)(bytes);
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
Log when malloc functions fail.
This shouldn't happen often, and resulting failures can be hard to debug.
From the bionic unit tests now:
W libc : malloc(18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 100) failed: returning null pointer
W libc : calloc(1, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 18446744073709551615) failed: returning null pointer
W libc : calloc(2, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 2) failed: returning null pointer
W libc : memalign(4096, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x0, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x75d7526070, 18446744073709551615) failed: returning null pointer
W libc : reallocaray(0x0, 9223372036854775812, 2) failed: returning null pointer
W libc : reallocaray(0x0, 2, 9223372036854775812) failed: returning null pointer
Bug: http://b/12821450
Test: ran tests
Change-Id: Ib176814404f4ba1297416dd3e1edd721bf59aeed
2019-02-25 22:21:04 +01:00
|
|
|
if (__predict_false(result == nullptr)) {
|
|
|
|
warning_log("malloc(%zu) failed: returning null pointer", bytes);
|
2020-01-08 00:47:47 +01:00
|
|
|
return nullptr;
|
Log when malloc functions fail.
This shouldn't happen often, and resulting failures can be hard to debug.
From the bionic unit tests now:
W libc : malloc(18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 100) failed: returning null pointer
W libc : calloc(1, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 18446744073709551615) failed: returning null pointer
W libc : calloc(2, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 2) failed: returning null pointer
W libc : memalign(4096, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x0, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x75d7526070, 18446744073709551615) failed: returning null pointer
W libc : reallocaray(0x0, 9223372036854775812, 2) failed: returning null pointer
W libc : reallocaray(0x0, 2, 9223372036854775812) failed: returning null pointer
Bug: http://b/12821450
Test: ran tests
Change-Id: Ib176814404f4ba1297416dd3e1edd721bf59aeed
2019-02-25 22:21:04 +01:00
|
|
|
}
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(result);
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" size_t malloc_usable_size(const void* mem) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
2020-01-08 00:47:47 +01:00
|
|
|
mem = MaybeUntagAndCheckPointer(mem);
|
2019-02-04 21:26:02 +01:00
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
|
|
|
return dispatch_table->malloc_usable_size(mem);
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
|
|
|
return Malloc(malloc_usable_size)(mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void* memalign(size_t alignment, size_t bytes) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(dispatch_table->memalign(alignment, bytes));
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
Log when malloc functions fail.
This shouldn't happen often, and resulting failures can be hard to debug.
From the bionic unit tests now:
W libc : malloc(18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 100) failed: returning null pointer
W libc : calloc(1, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 18446744073709551615) failed: returning null pointer
W libc : calloc(2, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 2) failed: returning null pointer
W libc : memalign(4096, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x0, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x75d7526070, 18446744073709551615) failed: returning null pointer
W libc : reallocaray(0x0, 9223372036854775812, 2) failed: returning null pointer
W libc : reallocaray(0x0, 2, 9223372036854775812) failed: returning null pointer
Bug: http://b/12821450
Test: ran tests
Change-Id: Ib176814404f4ba1297416dd3e1edd721bf59aeed
2019-02-25 22:21:04 +01:00
|
|
|
void* result = Malloc(memalign)(alignment, bytes);
|
|
|
|
if (__predict_false(result == nullptr)) {
|
|
|
|
warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes);
|
|
|
|
}
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(result);
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
2020-01-08 00:47:47 +01:00
|
|
|
int result;
|
2019-02-04 21:26:02 +01:00
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
2020-01-08 00:47:47 +01:00
|
|
|
result = dispatch_table->posix_memalign(memptr, alignment, size);
|
|
|
|
} else {
|
|
|
|
result = Malloc(posix_memalign)(memptr, alignment, size);
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
2020-01-08 00:47:47 +01:00
|
|
|
if (result == 0) {
|
|
|
|
*memptr = MaybeTagPointer(*memptr);
|
|
|
|
}
|
|
|
|
return result;
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 03:14:55 +01:00
|
|
|
extern "C" void* aligned_alloc(size_t alignment, size_t size) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(dispatch_table->aligned_alloc(alignment, size));
|
2018-02-06 03:14:55 +01:00
|
|
|
}
|
Log when malloc functions fail.
This shouldn't happen often, and resulting failures can be hard to debug.
From the bionic unit tests now:
W libc : malloc(18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 100) failed: returning null pointer
W libc : calloc(1, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 18446744073709551615) failed: returning null pointer
W libc : calloc(2, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 2) failed: returning null pointer
W libc : memalign(4096, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x0, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x75d7526070, 18446744073709551615) failed: returning null pointer
W libc : reallocaray(0x0, 9223372036854775812, 2) failed: returning null pointer
W libc : reallocaray(0x0, 2, 9223372036854775812) failed: returning null pointer
Bug: http://b/12821450
Test: ran tests
Change-Id: Ib176814404f4ba1297416dd3e1edd721bf59aeed
2019-02-25 22:21:04 +01:00
|
|
|
void* result = Malloc(aligned_alloc)(alignment, size);
|
|
|
|
if (__predict_false(result == nullptr)) {
|
|
|
|
warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size);
|
|
|
|
}
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(result);
|
2018-02-06 03:14:55 +01:00
|
|
|
}
|
|
|
|
|
2019-04-21 07:18:49 +02:00
|
|
|
extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
2020-01-08 00:47:47 +01:00
|
|
|
old_mem = MaybeUntagAndCheckPointer(old_mem);
|
2019-02-04 21:26:02 +01:00
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(dispatch_table->realloc(old_mem, bytes));
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
Log when malloc functions fail.
This shouldn't happen often, and resulting failures can be hard to debug.
From the bionic unit tests now:
W libc : malloc(18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 100) failed: returning null pointer
W libc : calloc(1, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 18446744073709551615) failed: returning null pointer
W libc : calloc(2, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 2) failed: returning null pointer
W libc : memalign(4096, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x0, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x75d7526070, 18446744073709551615) failed: returning null pointer
W libc : reallocaray(0x0, 9223372036854775812, 2) failed: returning null pointer
W libc : reallocaray(0x0, 2, 9223372036854775812) failed: returning null pointer
Bug: http://b/12821450
Test: ran tests
Change-Id: Ib176814404f4ba1297416dd3e1edd721bf59aeed
2019-02-25 22:21:04 +01:00
|
|
|
void* result = Malloc(realloc)(old_mem, bytes);
|
|
|
|
if (__predict_false(result == nullptr && bytes != 0)) {
|
|
|
|
warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes);
|
|
|
|
}
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(result);
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 21:52:42 +02:00
|
|
|
extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
|
|
|
|
size_t new_size;
|
|
|
|
if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
|
Log when malloc functions fail.
This shouldn't happen often, and resulting failures can be hard to debug.
From the bionic unit tests now:
W libc : malloc(18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 100) failed: returning null pointer
W libc : calloc(1, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 18446744073709551615) failed: returning null pointer
W libc : calloc(2, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 2) failed: returning null pointer
W libc : memalign(4096, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x0, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x75d7526070, 18446744073709551615) failed: returning null pointer
W libc : reallocaray(0x0, 9223372036854775812, 2) failed: returning null pointer
W libc : reallocaray(0x0, 2, 9223372036854775812) failed: returning null pointer
Bug: http://b/12821450
Test: ran tests
Change-Id: Ib176814404f4ba1297416dd3e1edd721bf59aeed
2019-02-25 22:21:04 +01:00
|
|
|
warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
|
|
|
|
old_mem, item_count, item_size);
|
2018-09-18 21:52:42 +02:00
|
|
|
errno = ENOMEM;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return realloc(old_mem, new_size);
|
|
|
|
}
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
|
|
|
|
extern "C" void* pvalloc(size_t bytes) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(dispatch_table->pvalloc(bytes));
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
Log when malloc functions fail.
This shouldn't happen often, and resulting failures can be hard to debug.
From the bionic unit tests now:
W libc : malloc(18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 100) failed: returning null pointer
W libc : calloc(1, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 18446744073709551615) failed: returning null pointer
W libc : calloc(2, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 2) failed: returning null pointer
W libc : memalign(4096, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x0, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x75d7526070, 18446744073709551615) failed: returning null pointer
W libc : reallocaray(0x0, 9223372036854775812, 2) failed: returning null pointer
W libc : reallocaray(0x0, 2, 9223372036854775812) failed: returning null pointer
Bug: http://b/12821450
Test: ran tests
Change-Id: Ib176814404f4ba1297416dd3e1edd721bf59aeed
2019-02-25 22:21:04 +01:00
|
|
|
void* result = Malloc(pvalloc)(bytes);
|
|
|
|
if (__predict_false(result == nullptr)) {
|
|
|
|
warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
|
|
|
|
}
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(result);
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void* valloc(size_t bytes) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(dispatch_table->valloc(bytes));
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
Log when malloc functions fail.
This shouldn't happen often, and resulting failures can be hard to debug.
From the bionic unit tests now:
W libc : malloc(18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 100) failed: returning null pointer
W libc : calloc(1, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 18446744073709551615) failed: returning null pointer
W libc : calloc(2, 18446744073709551615) failed: returning null pointer
W libc : calloc(18446744073709551615, 2) failed: returning null pointer
W libc : memalign(4096, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x0, 18446744073709551615) failed: returning null pointer
W libc : realloc(0x75d7526070, 18446744073709551615) failed: returning null pointer
W libc : reallocaray(0x0, 9223372036854775812, 2) failed: returning null pointer
W libc : reallocaray(0x0, 2, 9223372036854775812) failed: returning null pointer
Bug: http://b/12821450
Test: ran tests
Change-Id: Ib176814404f4ba1297416dd3e1edd721bf59aeed
2019-02-25 22:21:04 +01:00
|
|
|
void* result = Malloc(valloc)(bytes);
|
|
|
|
if (__predict_false(result == nullptr)) {
|
|
|
|
warning_log("valloc(%zu) failed: returning null pointer", bytes);
|
|
|
|
}
|
2020-01-08 00:47:47 +01:00
|
|
|
return MaybeTagPointer(result);
|
2015-11-17 02:30:32 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// =============================================================================
|
2018-12-14 16:57:21 +01:00
|
|
|
|
2020-01-08 00:47:47 +01:00
|
|
|
struct CallbackWrapperArg {
|
|
|
|
void (*callback)(uintptr_t base, size_t size, void* arg);
|
|
|
|
void* arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
void CallbackWrapper(uintptr_t base, size_t size, void* arg) {
|
|
|
|
CallbackWrapperArg* wrapper_arg = reinterpret_cast<CallbackWrapperArg*>(arg);
|
|
|
|
wrapper_arg->callback(
|
|
|
|
reinterpret_cast<uintptr_t>(MaybeTagPointer(reinterpret_cast<void*>(base))),
|
|
|
|
size, wrapper_arg->arg);
|
|
|
|
}
|
|
|
|
|
2016-01-29 21:48:18 +01:00
|
|
|
// =============================================================================
|
|
|
|
// Exported for use by libmemunreachable.
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
// Calls callback for every allocation in the anonymous heap mapping
|
2020-01-08 00:47:47 +01:00
|
|
|
// [base, base+size). Must be called between malloc_disable and malloc_enable.
|
|
|
|
// `base` in this can take either a tagged or untagged pointer, but we always
|
|
|
|
// provide a tagged pointer to the `base` argument of `callback` if the kernel
|
|
|
|
// supports tagged pointers.
|
2016-01-29 21:48:18 +01:00
|
|
|
extern "C" int malloc_iterate(uintptr_t base, size_t size,
|
|
|
|
void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
2020-01-08 00:47:47 +01:00
|
|
|
// Wrap the malloc_iterate callback we were provided, in order to provide
|
|
|
|
// pointer tagging support.
|
|
|
|
CallbackWrapperArg wrapper_arg;
|
|
|
|
wrapper_arg.callback = callback;
|
|
|
|
wrapper_arg.arg = arg;
|
|
|
|
uintptr_t untagged_base =
|
|
|
|
reinterpret_cast<uintptr_t>(UntagPointer(reinterpret_cast<void*>(base)));
|
2019-02-04 21:26:02 +01:00
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
2020-01-08 00:47:47 +01:00
|
|
|
return dispatch_table->malloc_iterate(
|
|
|
|
untagged_base, size, CallbackWrapper, &wrapper_arg);
|
2016-01-29 21:48:18 +01:00
|
|
|
}
|
2020-01-08 00:47:47 +01:00
|
|
|
return Malloc(malloc_iterate)(
|
|
|
|
untagged_base, size, CallbackWrapper, &wrapper_arg);
|
2016-01-29 21:48:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disable calls to malloc so malloc_iterate gets a consistent view of
|
|
|
|
// allocated memory.
|
|
|
|
extern "C" void malloc_disable() {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
|
|
|
return dispatch_table->malloc_disable();
|
2016-01-29 21:48:18 +01:00
|
|
|
}
|
|
|
|
return Malloc(malloc_disable)();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-enable calls to malloc after a previous call to malloc_disable.
|
|
|
|
extern "C" void malloc_enable() {
|
2019-02-04 21:26:02 +01:00
|
|
|
auto dispatch_table = GetDispatchTable();
|
|
|
|
if (__predict_false(dispatch_table != nullptr)) {
|
|
|
|
return dispatch_table->malloc_enable();
|
2016-01-29 21:48:18 +01:00
|
|
|
}
|
|
|
|
return Malloc(malloc_enable)();
|
|
|
|
}
|
2016-02-02 20:57:54 +01:00
|
|
|
|
2019-02-09 02:30:58 +01:00
|
|
|
#if defined(LIBC_STATIC)
|
2016-02-02 20:57:54 +01:00
|
|
|
extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2019-02-09 02:30:58 +01:00
|
|
|
|
|
|
|
#if __has_feature(hwaddress_sanitizer)
|
|
|
|
// FIXME: implement these in HWASan allocator.
|
2019-11-08 20:28:38 +01:00
|
|
|
extern "C" int __sanitizer_malloc_iterate(uintptr_t base __unused, size_t size __unused,
|
|
|
|
void (*callback)(uintptr_t base, size_t size, void* arg)
|
|
|
|
__unused,
|
|
|
|
void* arg __unused) {
|
2019-02-09 02:30:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void __sanitizer_malloc_disable() {
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void __sanitizer_malloc_enable() {
|
|
|
|
}
|
2019-03-08 19:56:17 +01:00
|
|
|
|
|
|
|
extern "C" int __sanitizer_malloc_info(int, FILE*) {
|
|
|
|
errno = ENOTSUP;
|
|
|
|
return -1;
|
|
|
|
}
|
2019-02-09 02:30:58 +01:00
|
|
|
#endif
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
// Platform-internal mallopt variant.
|
|
|
|
// =============================================================================
|
|
|
|
#if defined(LIBC_STATIC)
|
2019-02-16 03:06:15 +01:00
|
|
|
extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
|
|
|
|
if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
|
|
|
|
return LimitEnable(arg, arg_size);
|
|
|
|
}
|
2020-02-01 04:57:04 +01:00
|
|
|
if (opcode == M_INITIALIZE_GWP_ASAN) {
|
2020-12-01 00:04:14 +01:00
|
|
|
if (arg == nullptr || arg_size != sizeof(android_mallopt_gwp_asan_options_t)) {
|
2020-02-01 04:57:04 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-21 02:25:46 +02:00
|
|
|
|
2020-12-01 00:04:14 +01:00
|
|
|
return EnableGwpAsan(*reinterpret_cast<android_mallopt_gwp_asan_options_t*>(arg));
|
2020-02-01 04:57:04 +01:00
|
|
|
}
|
2019-02-09 02:30:58 +01:00
|
|
|
errno = ENOTSUP;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// =============================================================================
|
2020-02-01 04:57:04 +01:00
|
|
|
|
|
|
|
static constexpr MallocDispatch __libc_malloc_default_dispatch __attribute__((unused)) = {
|
|
|
|
Malloc(calloc),
|
|
|
|
Malloc(free),
|
|
|
|
Malloc(mallinfo),
|
|
|
|
Malloc(malloc),
|
|
|
|
Malloc(malloc_usable_size),
|
|
|
|
Malloc(memalign),
|
|
|
|
Malloc(posix_memalign),
|
|
|
|
#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
|
|
|
|
Malloc(pvalloc),
|
|
|
|
#endif
|
|
|
|
Malloc(realloc),
|
|
|
|
#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
|
|
|
|
Malloc(valloc),
|
|
|
|
#endif
|
|
|
|
Malloc(malloc_iterate),
|
|
|
|
Malloc(malloc_disable),
|
|
|
|
Malloc(malloc_enable),
|
|
|
|
Malloc(mallopt),
|
|
|
|
Malloc(aligned_alloc),
|
|
|
|
Malloc(malloc_info),
|
|
|
|
};
|
|
|
|
|
|
|
|
const MallocDispatch* NativeAllocatorDispatch() {
|
|
|
|
return &__libc_malloc_default_dispatch;
|
|
|
|
}
|