summaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
authorJoseph Huber <jhuber6@vols.utk.edu>2023-04-24 14:35:38 -0500
committerJoseph Huber <jhuber6@vols.utk.edu>2023-05-15 06:15:52 -0500
commit9417d9fc38c81e1c13aec49f81fa7bf93c9a4bb6 (patch)
tree62175c99be1d2260925d1dbd72515005475fef1c /libc
parent1a42f795587b9d57291d009989aace6efd0a7a7f (diff)
downloadllvm-9417d9fc38c81e1c13aec49f81fa7bf93c9a4bb6.tar.gz
[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
Diffstat (limited to 'libc')
-rw-r--r--libc/test/IntegrationTest/test.cpp5
-rw-r--r--libc/test/UnitTest/HermeticTestUtils.cpp5
2 files changed, 6 insertions, 4 deletions
diff --git a/libc/test/IntegrationTest/test.cpp b/libc/test/IntegrationTest/test.cpp
index e86e0a8d22c8..8e2feccd0dac 100644
--- a/libc/test/IntegrationTest/test.cpp
+++ b/libc/test/IntegrationTest/test.cpp
@@ -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
// 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" {
@@ -65,7 +66,7 @@ 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 *) {}
diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp
index c8279e54e15d..04ec22f165b8 100644
--- a/libc/test/UnitTest/HermeticTestUtils.cpp
+++ b/libc/test/UnitTest/HermeticTestUtils.cpp
@@ -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
@@ -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 *) {}