Skip to content

Commit 80b9080

Browse files
tbr-ttnashif
authored andcommitted
tests/mem_alloc: check that malloc() objects can be accessed
Adds tests checking that we can use memory allocated by malloc(), calloc() and realloc() to access objects of some common types, including uint64_t and double. It works by doing a number of allocations of various sizes and dereferencing the returned pointer. The purpose is to catch cases where the application would trap if accessing the allocated memory. (The test does not check alignment against the ABI or alignof().) Signed-off-by: Martin Åberg <[email protected]>
1 parent 180ce49 commit 80b9080

File tree

1 file changed

+98
-2
lines changed

1 file changed

+98
-2
lines changed

tests/lib/mem_alloc/src/main.c

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,106 @@
2020
#include <ztest.h>
2121
#include <stdlib.h>
2222
#include <errno.h>
23+
#include <time.h>
2324

2425
#define BUF_LEN 10
2526

27+
28+
/* The access test allocates objects of this type and dereferences members. */
29+
union aligntest {
30+
long long thelonglong;
31+
double thedouble;
32+
uintmax_t theuintmax_t;
33+
void (*thepfunc)(void);
34+
time_t thetime_t;
35+
};
36+
37+
/* Make sure we can access some built-in types. */
38+
static void do_the_access(volatile union aligntest *aptr)
39+
{
40+
aptr->thelonglong = 2;
41+
aptr->thelonglong;
42+
43+
if (IS_ENABLED(CONFIG_FPU)) {
44+
aptr->thedouble = 3.0;
45+
aptr->thedouble;
46+
}
47+
48+
aptr->theuintmax_t = 4;
49+
aptr->theuintmax_t;
50+
51+
aptr->thepfunc = test_main;
52+
aptr->thepfunc;
53+
54+
aptr->thetime_t = 3;
55+
aptr->thetime_t;
56+
}
57+
58+
#define PRINT_TYPE_INFO(_t) \
59+
TC_PRINT(" %-14s %4zu %5zu\n", #_t, sizeof(_t), __alignof__(_t))
60+
61+
void test_malloc_align(void)
62+
{
63+
char *ptr[64] = { NULL };
64+
65+
TC_PRINT(" Compiler type info for " CONFIG_ARCH " " CONFIG_BOARD "\n");
66+
TC_PRINT(" TYPE SIZE ALIGN\n");
67+
PRINT_TYPE_INFO(int);
68+
PRINT_TYPE_INFO(long);
69+
PRINT_TYPE_INFO(long long);
70+
PRINT_TYPE_INFO(double);
71+
PRINT_TYPE_INFO(size_t);
72+
PRINT_TYPE_INFO(void *);
73+
PRINT_TYPE_INFO(void (*)(void));
74+
PRINT_TYPE_INFO(time_t);
75+
76+
/* Tries to use the malloc() implementation when in different states. */
77+
for (size_t i = 0; i < ARRAY_SIZE(ptr); i++) {
78+
union aligntest *aptr = NULL;
79+
80+
ptr[i] = malloc(sizeof *(ptr[i]));
81+
aptr = malloc(sizeof(*aptr));
82+
if (aptr) {
83+
do_the_access(aptr);
84+
}
85+
free(aptr);
86+
}
87+
for (size_t i = 0; i < ARRAY_SIZE(ptr); i++) {
88+
free(ptr[i]);
89+
ptr[i] = NULL;
90+
}
91+
92+
for (size_t n = 0; n < ARRAY_SIZE(ptr); n++) {
93+
union aligntest *aptr = NULL;
94+
95+
for (size_t i = 0; i < n; i++) {
96+
ptr[i] = malloc(sizeof *(ptr[i]));
97+
}
98+
aptr = malloc(sizeof(*aptr));
99+
if (aptr) {
100+
do_the_access(aptr);
101+
}
102+
free(aptr);
103+
for (size_t i = 0; i < n; i++) {
104+
free(ptr[i]);
105+
ptr[i] = NULL;
106+
}
107+
}
108+
109+
for (size_t n = 0; n < ARRAY_SIZE(ptr); n++) {
110+
union aligntest *aptr = NULL;
111+
112+
ptr[n] = malloc(n);
113+
aptr = malloc(sizeof(*aptr));
114+
if (aptr) {
115+
do_the_access(aptr);
116+
}
117+
free(aptr);
118+
free(ptr[n]);
119+
ptr[n] = NULL;
120+
}
121+
}
122+
26123
/**
27124
* @brief Test dynamic memory allocation using malloc
28125
*
@@ -97,7 +194,6 @@ void test_realloc(void)
97194
reloc_ptr = realloc(ptr, new_size);
98195

99196
zassert_not_null(reloc_ptr, "realloc failed, errno: %d", errno);
100-
zassert_not_null((ptr), "malloc/realloc failed, errno: %d", errno);
101197
ptr = reloc_ptr;
102198

103199
(void)memset(filled_buf, 'p', BUF_LEN);
@@ -169,7 +265,6 @@ void test_memalloc_all(void)
169265

170266
reloc_ptr = realloc(mlc_ptr, new_size);
171267
zassert_not_null(reloc_ptr, "realloc failed, errno: %d", errno);
172-
zassert_not_null((mlc_ptr), "malloc/realloc failed, errno: %d", errno);
173268
mlc_ptr = reloc_ptr;
174269

175270
free(mlc_ptr);
@@ -199,6 +294,7 @@ __no_optimization void test_memalloc_max(void)
199294
void test_main(void)
200295
{
201296
ztest_test_suite(test_c_lib_dynamic_memalloc,
297+
ztest_user_unit_test(test_malloc_align),
202298
ztest_user_unit_test(test_malloc),
203299
ztest_user_unit_test(test_free),
204300
ztest_user_unit_test(test_calloc),

0 commit comments

Comments
 (0)