-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
main.c
307 lines (256 loc) · 6.75 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* @file test dynamic memory allocation using C libraries
*
* This module verifies that the various dynamic memory allocation functions
* works fine with minimal C library and newlib C library.
*
* IMPORTANT: The module only ensures that each supported library is present,
* and that a bare minimum of its functionality is operating correctly. It does
* NOT guarantee that ALL standards-defined functionality is present, nor does
* it guarantee that ALL functionality provided is working correctly.
*/
#include <zephyr.h>
#include <ztest.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#define BUF_LEN 10
/* The access test allocates objects of this type and dereferences members. */
union aligntest {
long long thelonglong;
double thedouble;
uintmax_t theuintmax_t;
void (*thepfunc)(void);
time_t thetime_t;
};
/* Make sure we can access some built-in types. */
static void do_the_access(volatile union aligntest *aptr)
{
aptr->thelonglong = 2;
aptr->thelonglong;
if (IS_ENABLED(CONFIG_FPU)) {
aptr->thedouble = 3.0;
aptr->thedouble;
}
aptr->theuintmax_t = 4;
aptr->theuintmax_t;
aptr->thepfunc = test_main;
aptr->thepfunc;
aptr->thetime_t = 3;
aptr->thetime_t;
}
#define PRINT_TYPE_INFO(_t) \
TC_PRINT(" %-14s %4zu %5zu\n", #_t, sizeof(_t), __alignof__(_t))
void test_malloc_align(void)
{
char *ptr[64] = { NULL };
TC_PRINT(" Compiler type info for " CONFIG_ARCH " " CONFIG_BOARD "\n");
TC_PRINT(" TYPE SIZE ALIGN\n");
PRINT_TYPE_INFO(int);
PRINT_TYPE_INFO(long);
PRINT_TYPE_INFO(long long);
PRINT_TYPE_INFO(double);
PRINT_TYPE_INFO(size_t);
PRINT_TYPE_INFO(void *);
PRINT_TYPE_INFO(void (*)(void));
PRINT_TYPE_INFO(time_t);
/* Tries to use the malloc() implementation when in different states. */
for (size_t i = 0; i < ARRAY_SIZE(ptr); i++) {
union aligntest *aptr = NULL;
ptr[i] = malloc(sizeof *(ptr[i]));
aptr = malloc(sizeof(*aptr));
if (aptr) {
do_the_access(aptr);
}
free(aptr);
}
for (size_t i = 0; i < ARRAY_SIZE(ptr); i++) {
free(ptr[i]);
ptr[i] = NULL;
}
for (size_t n = 0; n < ARRAY_SIZE(ptr); n++) {
union aligntest *aptr = NULL;
for (size_t i = 0; i < n; i++) {
ptr[i] = malloc(sizeof *(ptr[i]));
}
aptr = malloc(sizeof(*aptr));
if (aptr) {
do_the_access(aptr);
}
free(aptr);
for (size_t i = 0; i < n; i++) {
free(ptr[i]);
ptr[i] = NULL;
}
}
for (size_t n = 0; n < ARRAY_SIZE(ptr); n++) {
union aligntest *aptr = NULL;
ptr[n] = malloc(n);
aptr = malloc(sizeof(*aptr));
if (aptr) {
do_the_access(aptr);
}
free(aptr);
free(ptr[n]);
ptr[n] = NULL;
}
}
/**
* @brief Test dynamic memory allocation using malloc
*
* @see malloc(), free()
*/
void test_malloc(void)
{
/* Initialize error number to avoid garbage value, in case of SUCCESS */
int *iptr = NULL;
iptr = malloc(BUF_LEN * sizeof(int));
zassert_not_null((iptr), "malloc failed, errno: %d", errno);
memset(iptr, 'p', BUF_LEN * sizeof(int));
free(iptr);
iptr = NULL;
}
/**
* @brief Test dynamic memory allocation free function
*
* @see free()
*/
void test_free(void)
{
/*
* In free, if ptr is passed as NULL, no operation is performed
* Just make sure, no exception occurs and test pass
*/
free(NULL);
}
/**
* @brief Test dynamic memory allocation using calloc
*
* @see calloc(), free()
*/
#define CALLOC_BUFLEN (200)
static ZTEST_BMEM unsigned char zerobuf[CALLOC_BUFLEN];
void test_calloc(void)
{
char *cptr = NULL;
cptr = calloc(CALLOC_BUFLEN, sizeof(char));
zassert_not_null((cptr), "calloc failed, errno: %d", errno);
zassert_true(((memcmp(cptr, zerobuf, CALLOC_BUFLEN)) == 0),
"calloc failed to set zero value, errno: %d", errno);
memset(cptr, 'p', CALLOC_BUFLEN);
free(cptr);
cptr = NULL;
}
/**
* @brief Test dynamic memory allocation using realloc
*
* @see malloc(), realloc(), free()
*/
ZTEST_BMEM unsigned char filled_buf[BUF_LEN];
void test_realloc(void)
{
char orig_size = BUF_LEN;
char new_size = BUF_LEN + BUF_LEN;
char *ptr = NULL;
char *reloc_ptr = NULL;
ptr = malloc(orig_size);
zassert_not_null((ptr), "malloc failed, errno: %d", errno);
(void)memset(ptr, 'p', orig_size);
reloc_ptr = realloc(ptr, new_size);
zassert_not_null(reloc_ptr, "realloc failed, errno: %d", errno);
ptr = reloc_ptr;
(void)memset(filled_buf, 'p', BUF_LEN);
zassert_true(((memcmp(ptr, filled_buf, BUF_LEN)) == 0),
"realloc failed to copy malloc data, errno: %d", errno);
free(ptr);
ptr = NULL;
}
/**
* @brief Test dynamic memory allocation using reallocarray
*
* @see malloc(), reallocarray(), free()
*/
#ifdef CONFIG_NEWLIB_LIBC
void test_reallocarray(void)
{
/* reallocarray not implemented for newlib */
ztest_test_skip();
}
#else
void test_reallocarray(void)
{
char orig_size = BUF_LEN;
char *ptr = NULL;
ptr = malloc(orig_size);
zassert_not_null((ptr), "malloc failed, errno: %d", errno);
(void)memset(ptr, 'p', orig_size);
char *reloc_ptr = reallocarray(ptr, 2, orig_size);
zassert_not_null(reloc_ptr, "reallocarray failed");
zassert_not_null((ptr), "malloc/reallocarray failed, errno: %d", errno);
ptr = reloc_ptr;
(void)memset(filled_buf, 'p', BUF_LEN);
zassert_true(((memcmp(ptr, filled_buf, BUF_LEN)) == 0),
"realloc failed to copy malloc data, errno: %d", errno);
free(ptr);
ptr = NULL;
}
#endif
#define MAX_LEN (10 * BUF_LEN)
/**
* @brief Test dynamic memory allocation functions
*
* @see malloc(), calloc(), realloc(), free()
*/
void test_memalloc_all(void)
{
char *mlc_ptr = NULL;
char *clc_ptr = NULL;
char *reloc_ptr = NULL;
int orig_size = BUF_LEN;
int new_size = MAX_LEN;
mlc_ptr = malloc(orig_size);
zassert_not_null((mlc_ptr), "malloc failed, errno: %d", errno);
clc_ptr = calloc(100, sizeof(char));
zassert_not_null((clc_ptr), "calloc failed, errno: %d", errno);
reloc_ptr = realloc(mlc_ptr, new_size);
zassert_not_null(reloc_ptr, "realloc failed, errno: %d", errno);
mlc_ptr = reloc_ptr;
free(mlc_ptr);
free(clc_ptr);
mlc_ptr = NULL;
clc_ptr = NULL;
reloc_ptr = NULL;
}
/**
*
* @brief Test dynamic memory allocation upto maximum size
* Negative test case
*
*/
__no_optimization void test_memalloc_max(void)
{
char *ptr = NULL;
ptr = malloc(0x7fffffff);
zassert_is_null(ptr, "malloc passed unexpectedly");
free(ptr);
ptr = NULL;
}
void test_main(void)
{
ztest_test_suite(test_c_lib_dynamic_memalloc,
ztest_user_unit_test(test_malloc_align),
ztest_user_unit_test(test_malloc),
ztest_user_unit_test(test_free),
ztest_user_unit_test(test_calloc),
ztest_user_unit_test(test_realloc),
ztest_user_unit_test(test_reallocarray),
ztest_user_unit_test(test_memalloc_all),
ztest_user_unit_test(test_memalloc_max)
);
ztest_run_test_suite(test_c_lib_dynamic_memalloc);
}