[libc] Make the bump pointer explicitly return null on buffer oveerrun
We use a simple bump ptr in the `libc` tests. If we run out of data we can currently return other static memory and have weird failure cases. We should fail more explicitly here by returning a null pointer instead. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D150529
This commit is contained in:
parent
1a42f79558
commit
9417d9fc38
@ -57,7 +57,8 @@ int atexit(void (*func)(void)) { return __llvm_libc::atexit(func); }
|
|||||||
// which just hands out continuous blocks from a statically allocated chunk of
|
// which just hands out continuous blocks from a statically allocated chunk of
|
||||||
// memory.
|
// memory.
|
||||||
|
|
||||||
static uint8_t memory[16384];
|
static constexpr uint64_t MEMORY_SIZE = 16384;
|
||||||
|
static uint8_t memory[MEMORY_SIZE];
|
||||||
static uint8_t *ptr = memory;
|
static uint8_t *ptr = memory;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -65,7 +66,7 @@ extern "C" {
|
|||||||
void *malloc(size_t s) {
|
void *malloc(size_t s) {
|
||||||
void *mem = ptr;
|
void *mem = ptr;
|
||||||
ptr += s;
|
ptr += s;
|
||||||
return mem;
|
return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free(void *) {}
|
void free(void *) {}
|
||||||
|
@ -29,7 +29,8 @@ namespace {
|
|||||||
// requires. Hence, as a work around for this problem, we use a simple allocator
|
// requires. Hence, as a work around for this problem, we use a simple allocator
|
||||||
// which just hands out continuous blocks from a statically allocated chunk of
|
// which just hands out continuous blocks from a statically allocated chunk of
|
||||||
// memory.
|
// memory.
|
||||||
static uint8_t memory[16384];
|
static constexpr uint64_t MEMORY_SIZE = 16384;
|
||||||
|
static uint8_t memory[MEMORY_SIZE];
|
||||||
static uint8_t *ptr = memory;
|
static uint8_t *ptr = memory;
|
||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
@ -68,7 +69,7 @@ void *malloc(size_t s) {
|
|||||||
s = ((s + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
|
s = ((s + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
|
||||||
void *mem = ptr;
|
void *mem = ptr;
|
||||||
ptr += s;
|
ptr += s;
|
||||||
return mem;
|
return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free(void *) {}
|
void free(void *) {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user