Skip to content

Commit

Permalink
[libc] Add a functioning realloc for hermetic tests.
Browse files Browse the repository at this point in the history
Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D150846
  • Loading branch information
Siva Chandra Reddy committed May 18, 2023
1 parent 625d692 commit d3b3304
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
17 changes: 14 additions & 3 deletions libc/test/UnitTest/HermeticTestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,20 @@ void *malloc(size_t s) {

void free(void *) {}

void *realloc(void *ptr, size_t s) {
free(ptr);
return malloc(s);
void *realloc(void *mem, size_t s) {
if (mem == nullptr)
return malloc(s);
uint8_t *newmem = reinterpret_cast<uint8_t *>(malloc(s));
if (newmem == nullptr)
return nullptr;
uint8_t *oldmem = reinterpret_cast<uint8_t *>(mem);
// We use a simple for loop to copy the data over.
// If |s| is less the previous alloc size, the copy works as expected.
// If |s| is greater than the previous alloc size, then garbage is copied
// over to the additional part in the new memory block.
for (size_t i = 0; i < s; ++i)
newmem[i] = oldmem[i];
return newmem;
}

// The unit test framework uses pure virtual functions. Since hermetic tests
Expand Down
2 changes: 0 additions & 2 deletions libc/test/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ add_libc_test(

add_libc_test(
char_vector_test
# This test relies on 'realloc' which is not implemented for hermetic tests.
UNIT_TEST_ONLY
SUITE
libc-support-tests
SRCS
Expand Down
2 changes: 0 additions & 2 deletions libc/test/src/__support/CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ add_libc_test(

add_libc_test(
string_test
# This test relies on 'realloc' which is not implemented for hermetic tests.
UNIT_TEST_ONLY
SUITE
libc-cpp-utils-tests
SRCS
Expand Down

0 comments on commit d3b3304

Please sign in to comment.