From: Siva Chandra Reddy Date: Thu, 18 May 2023 06:35:59 +0000 (+0000) Subject: [libc] Add a functioning realloc for hermetic tests. X-Git-Tag: upstream/17.0.6~7995 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d3b33042227019a82693733627973170d9760218;p=platform%2Fupstream%2Fllvm.git [libc] Add a functioning realloc for hermetic tests. Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D150846 --- diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp index 7117341..3d88b0e 100644 --- a/libc/test/UnitTest/HermeticTestUtils.cpp +++ b/libc/test/UnitTest/HermeticTestUtils.cpp @@ -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(malloc(s)); + if (newmem == nullptr) + return nullptr; + uint8_t *oldmem = reinterpret_cast(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 diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt index 154ebfe..ca75e7d 100644 --- a/libc/test/src/__support/CMakeLists.txt +++ b/libc/test/src/__support/CMakeLists.txt @@ -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 diff --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt index 28db8bd..04d128c 100644 --- a/libc/test/src/__support/CPP/CMakeLists.txt +++ b/libc/test/src/__support/CPP/CMakeLists.txt @@ -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