Skip to content

Commit a6678e3

Browse files
committed
Hook realloc for test harness
This commit implements test_realloc to support dynamic adjustment of memory sizes allocated by test_malloc or test_calloc. In this commit, when shrinking an allocated block (e.g., from 100 bytes to 10), memory corruption beyond the new size (such as m[i] for 10 <= i <= 99) cannot be detected. This limitation stems from the fact that the original memory block is retained after shrinking. While this approach helps avoid unnecessary memory copies, especially in cases where memory is often expanded and reduced, it can leave unused memory behind. Although this may improve performance when memory is plentiful, it may also lead to wasted space. There is still room for improvement in future versions to better manage memory use and detect corruption when shrinking blocks. Change-Id: Iebf68ea810c73549d966cb2305a5470863dacde5
1 parent 9563d40 commit a6678e3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Diff for: harness.c

+25
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static bool time_limited = false;
6363
typedef enum {
6464
TEST_MALLOC,
6565
TEST_CALLOC,
66+
TEST_REALLOC,
6667
} alloc_t;
6768

6869
/* Internal functions */
@@ -128,6 +129,7 @@ static void *alloc(alloc_t alloc_type, size_t size)
128129
char *msg_alloc_forbidden[] = {
129130
"Calls to malloc are disallowed",
130131
"Calls to calloc are disallowed",
132+
"Calls to realloc are disallowed",
131133
};
132134
report_event(MSG_FATAL, "%s", msg_alloc_forbidden[alloc_type]);
133135
return NULL;
@@ -137,6 +139,7 @@ static void *alloc(alloc_t alloc_type, size_t size)
137139
char *msg_alloc_failure[] = {
138140
"Malloc returning NULL",
139141
"Calloc returning NULL",
142+
"Realloc returning NULL",
140143
};
141144
report_event(MSG_WARN, "%s", msg_alloc_failure[alloc_type]);
142145
return NULL;
@@ -187,6 +190,28 @@ void *test_calloc(size_t nelem, size_t elsize)
187190
return alloc(TEST_CALLOC, nelem * elsize);
188191
}
189192

193+
/*
194+
* Implementation of adjusting the size of the memory allocated
195+
* by test_malloc or test_calloc.
196+
*/
197+
void *test_realloc(void *p, size_t new_size)
198+
{
199+
if (!p)
200+
return alloc(TEST_REALLOC, new_size);
201+
202+
const block_element_t *b = find_header(p);
203+
if (b->payload_size >= new_size)
204+
return p;
205+
206+
void *new_ptr = alloc(TEST_REALLOC, new_size);
207+
if (!new_ptr)
208+
return NULL;
209+
memcpy(new_ptr, p, b->payload_size);
210+
test_free(p);
211+
212+
return new_ptr;
213+
}
214+
190215
void test_free(void *p)
191216
{
192217
if (noallocate_mode) {

Diff for: harness.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
void *test_malloc(size_t size);
1414
void *test_calloc(size_t nmemb, size_t size);
15+
void *test_realloc(void *p, size_t new_size);
1516
void test_free(void *p);
1617
char *test_strdup(const char *s);
17-
/* FIXME: provide test_realloc as well */
1818

1919
#ifdef INTERNAL
2020

@@ -56,6 +56,7 @@ void trigger_exception(char *msg);
5656
/* Tested program use our versions of malloc and free */
5757
#define malloc test_malloc
5858
#define calloc test_calloc
59+
#define realloc test_realloc
5960
#define free test_free
6061

6162
/* Use undef to avoid strdup redefined error */

0 commit comments

Comments
 (0)