Skip to content

Commit 9ad144c

Browse files
committed
fixed calloc tests
1 parent deea28e commit 9ad144c

File tree

2 files changed

+97
-36
lines changed

2 files changed

+97
-36
lines changed

test/standalone/asprintf_fprintf/src/main.c

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ __attribute__((__unused__)) static void print_string(const char* str) {
9191
// prevents Clang from replacing function calls with builtins
9292
#if RENAME_FUNCTIONS
9393

94+
// this does not have __attribute__((malloc)) so we can test aliasing
95+
void *T_malloc(size_t size);
96+
97+
// this does not have __attribute__((malloc)) so we can test aliasing
98+
void *T_calloc(size_t nmemb, size_t size);
99+
94100
void T_bzero(void* s, size_t n);
95101

96102
void *T_memccpy(void *__restrict dest, const void *__restrict src, int c, size_t n)
@@ -182,6 +188,8 @@ char *T_strtok_r(char *__restrict s, const char *__restrict delim, char **__rest
182188

183189
#else
184190

191+
#define T_malloc malloc
192+
#define T_calloc calloc
185193
#define T_bzero bzero
186194
#define T_memccpy memccpy
187195
#define T_memchr memchr
@@ -571,12 +579,6 @@ int memccpy_tests(void) {
571579
return __LINE__;
572580
}
573581

574-
/* check that no crashes occur with small calloc sizes */
575-
buf = (char*)calloc(1, sizeof(char));
576-
free(buf);
577-
buf = (char*)calloc(0, sizeof(char));
578-
free(buf);
579-
580582
buf = (char*)calloc(file_size + 1, sizeof(char));
581583
if (buf == NULL) {
582584
perror("calloc failure");
@@ -662,7 +664,7 @@ int bzero_test(void) {
662664
if (T_strlen(&data[2]) != 0) {
663665
return __LINE__;
664666
}
665-
T_bzero(NULL, 0);
667+
666668
T_bzero(&data[8], 17);
667669
int cmp = T_memcmp(data, truth, 32);
668670
if (cmp != 0) {
@@ -1520,8 +1522,57 @@ int mem65536_test(void) {
15201522
return 0;
15211523
}
15221524

1525+
int alloc_test(void) {
1526+
/* test that malloc works and returns mutable memory */
1527+
1528+
buf = (char*)T_malloc(1);
1529+
C(buf != NULL);
1530+
*buf = 0xFF;
1531+
C(T_memcmp(buf, SINK, 1) > 0);
1532+
*buf = 0x00;
1533+
C(T_memcmp(buf, SINK, 1) == 0);
1534+
free(buf);
1535+
1536+
/* check that no crashes occur with small calloc sizes */
1537+
1538+
buf = (char*)T_calloc(1, sizeof(char));
1539+
C(buf != NULL && *buf == 0x00);
1540+
free(buf);
1541+
1542+
buf = (char*)T_calloc(2, sizeof(char));
1543+
C(buf != NULL && buf[0] == 0x00 && buf[1] == 0x00);
1544+
free(buf);
1545+
1546+
/* test allocating zero bytes */
1547+
1548+
/**
1549+
* malloc(0) is implementation defined, but it should be safe to assume
1550+
* that it does not return `buf`
1551+
*/
1552+
char * const Pointer_Not_From_Malloc = (char*)&buf;
1553+
1554+
buf = (char*)T_malloc(0);
1555+
C(buf != Pointer_Not_From_Malloc);
1556+
free(buf);
1557+
1558+
buf = (char*)T_calloc(0, sizeof(char));
1559+
C(buf != Pointer_Not_From_Malloc);
1560+
free(buf);
1561+
1562+
// ensure that we do not free twice
1563+
buf = NULL;
1564+
return 0;
1565+
}
1566+
15231567
int run_tests(void) {
15241568
int ret = 0;
1569+
buf = NULL;
1570+
1571+
/* malloc and calloc */
1572+
ret = alloc_test();
1573+
free(buf); buf = NULL;
1574+
if (ret != 0) { return ret; }
1575+
15251576
/* boot_asprintf */
15261577
ret = boot_sprintf_tests();
15271578
free(buf); buf = NULL;

test/standalone/asprintf_fprintf/src/rename.s

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ _NULL_ptr:
1010

1111
.section .text
1212

13+
.global _T_malloc
14+
.global _T_calloc
1315
.global _T_bzero
1416
.global _T_memccpy
1517
.global _T_memchr
@@ -42,6 +44,12 @@ _NULL_ptr:
4244

4345
;-------------------------------------------------------------------------------
4446

47+
_T_malloc:
48+
jp _malloc
49+
50+
_T_calloc:
51+
jp _calloc
52+
4553
_T_bzero:
4654
jp _bzero
4755

@@ -131,32 +139,34 @@ _T_strtok_r:
131139

132140
;-------------------------------------------------------------------------------
133141

134-
.global _bzero
135-
.global _memccpy
136-
.global _memchr
137-
.global _memcmp
138-
.global _memcpy
139-
.global _memmem
140-
.global _memmove
141-
.global _mempcpy
142-
.global _memrchr
143-
.global _memrmem
144-
.global _memset
145-
.global _stpcpy
146-
.global _stpncpy
147-
.global _strcat
148-
.global _strchr
149-
.global _strchrnul
150-
.global _strcmp
151-
.global _strcpy
152-
.global _strlcat
153-
.global _strlen
154-
.global _strncat
155-
.global _strncmp
156-
.global _strncpy
157-
.global _strnlen
158-
.global _strrchr
159-
.global _strrstr
160-
.global _strstr
161-
.global _strtok
162-
.global _strtok_r
142+
.extern _malloc
143+
.extern _calloc
144+
.extern _bzero
145+
.extern _memccpy
146+
.extern _memchr
147+
.extern _memcmp
148+
.extern _memcpy
149+
.extern _memmem
150+
.extern _memmove
151+
.extern _mempcpy
152+
.extern _memrchr
153+
.extern _memrmem
154+
.extern _memset
155+
.extern _stpcpy
156+
.extern _stpncpy
157+
.extern _strcat
158+
.extern _strchr
159+
.extern _strchrnul
160+
.extern _strcmp
161+
.extern _strcpy
162+
.extern _strlcat
163+
.extern _strlen
164+
.extern _strncat
165+
.extern _strncmp
166+
.extern _strncpy
167+
.extern _strnlen
168+
.extern _strrchr
169+
.extern _strrstr
170+
.extern _strstr
171+
.extern _strtok
172+
.extern _strtok_r

0 commit comments

Comments
 (0)