Skip to content

Commit

Permalink
[libc] Make the bump pointer explicitly return null on buffer oveerrun
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jhuber6 committed May 15, 2023
1 parent 1a42f79 commit 9417d9f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions libc/test/IntegrationTest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ int atexit(void (*func)(void)) { return __llvm_libc::atexit(func); }
// which just hands out continuous blocks from a statically allocated chunk of
// memory.

static uint8_t memory[16384];
static constexpr uint64_t MEMORY_SIZE = 16384;
static uint8_t memory[MEMORY_SIZE];
static uint8_t *ptr = memory;

extern "C" {

void *malloc(size_t s) {
void *mem = ptr;
ptr += s;
return mem;
return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
}

void free(void *) {}
Expand Down
5 changes: 3 additions & 2 deletions libc/test/UnitTest/HermeticTestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace {
// 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
// memory.
static uint8_t memory[16384];
static constexpr uint64_t MEMORY_SIZE = 16384;
static uint8_t memory[MEMORY_SIZE];
static uint8_t *ptr = memory;

} // anonymous namespace
Expand Down Expand Up @@ -68,7 +69,7 @@ void *malloc(size_t s) {
s = ((s + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
void *mem = ptr;
ptr += s;
return mem;
return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
}

void free(void *) {}
Expand Down

0 comments on commit 9417d9f

Please sign in to comment.