File: alloc-api-tests.c

package info (click to toggle)
mmlib 1.4.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,716 kB
  • sloc: ansic: 18,071; makefile: 431; sh: 135; python: 63
file content (170 lines) | stat: -rw-r--r-- 3,951 bytes parent folder | download
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
/*
   @mindmaze_header@
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <check.h>
#include <stdint.h>

#include "api-testcases.h"
#include "mmlib.h"
#include "mmlog.h"
#include "mmerrno.h"
#include "mmpredefs.h"

#define NUM_ALLOC	30

static int prev_max_loglvl;
static void setup(void)
{
	prev_max_loglvl = mm_log_set_maxlvl(MM_LOG_NONE);
}

static void teardown(void)
{
	mm_log_set_maxlvl(prev_max_loglvl);
}

START_TEST(aligned_heap_allocation)
{
	void* ptr;
	size_t align, size;
	int i;

	for (i = 1; i < NUM_ALLOC; i++) {
		for (align = sizeof(void*); align <= MM_PAGESZ; align *= 2) {
			size = i * align;
			ptr = mm_aligned_alloc(align, size);
			ck_assert(ptr != NULL);
			ck_assert_int_eq((uintptr_t)ptr & (align-1), 0);
			memset(ptr, 'x', size);
			mm_aligned_free(ptr);
		}
	}
}
END_TEST


START_TEST(aligned_heap_allocation_error)
{
#if !defined(__SANITIZE_ADDRESS__)
	void* ptr;
	size_t align;
	struct mm_error_state errstate;

	mm_save_errorstate(&errstate);

	// Test error if alignment is not power of 2 of pointer size
	for (align = 0; align < MM_PAGESZ; align++) {
		if (align >= sizeof(void*) && MM_IS_POW2(align))
			continue;

		ptr = mm_aligned_alloc(align, 4*MM_PAGESZ);
		ck_assert(ptr == NULL);
		ck_assert(mm_get_lasterror_number() == EINVAL);
	}

	// Test error is size is too big
	ptr = mm_aligned_alloc(sizeof(void*), SIZE_MAX);
	ck_assert(ptr == NULL);
	ck_assert(mm_get_lasterror_number() == ENOMEM);

	mm_set_errorstate(&errstate);
#endif /* !__SANITIZE_ADDRESS__ */
}
END_TEST


static size_t stack_alloc_sizes[] = {
	1, 3, sizeof(double), 64, 57, 256, 950, 2044, 2048, 2056, 4032,
};

START_TEST(aligned_stack_allocation)
{
	void* ptr;
	size_t align, size;

	size = stack_alloc_sizes[_i];

	for (align = sizeof(void*); align <= 2048; align *= 2) {
		ptr = mm_aligned_alloca(align, size);
		ck_assert(ptr != NULL);
		ck_assert_int_eq((uintptr_t)ptr & (align-1), 0);
		memset(ptr, 'x', size);
	}
}
END_TEST


static size_t malloca_sizes[] = {
	1, 3, sizeof(double), 64, 57, 256, 950, 2044, 2048, 2056, 4032,
	4096, 6*4091, 6*4096, 10000000, 100000000
};

START_TEST(safe_stack_allocation)
{
	void* ptr;
	size_t size = malloca_sizes[_i];

	ptr = mm_malloca(size);
	ck_assert(ptr != NULL);
	memset(ptr, 'x', size);

	ck_assert_int_eq((uintptr_t)ptr & (sizeof(char)-1), 0);
	ck_assert_int_eq((uintptr_t)ptr & (sizeof(short)-1), 0);
	ck_assert_int_eq((uintptr_t)ptr & (sizeof(int)-1), 0);
	ck_assert_int_eq((uintptr_t)ptr & (sizeof(long)-1), 0);
	ck_assert_int_eq((uintptr_t)ptr & (sizeof(float)-1), 0);
	ck_assert_int_eq((uintptr_t)ptr & (sizeof(double)-1), 0);

	mm_freea(ptr);
}
END_TEST


START_TEST(safe_stack_allocation_error)
{
#if !defined(__SANITIZE_ADDRESS__)
	void* ptr;
	size_t rem_sz;
	struct mm_error_state errstate;

	mm_save_errorstate(&errstate);

	for (rem_sz = 0; rem_sz < 4*MM_STK_ALIGN; rem_sz++) {
		ptr = mm_malloca(SIZE_MAX - rem_sz);
		ck_assert(ptr == NULL);
		ck_assert(mm_get_lasterror_number() == ENOMEM);
	}

	mm_set_errorstate(&errstate);
#endif /* !__SANITIZE_ADDRESS__ */
}
END_TEST


/**************************************************************************
 *                                                                        *
 *                          Test suite setup                              *
 *                                                                        *
 **************************************************************************/
LOCAL_SYMBOL
TCase* create_allocation_tcase(void)
{
	TCase *tc = tcase_create("allocation");
	tcase_add_checked_fixture(tc, setup, teardown);

	tcase_add_test(tc, aligned_heap_allocation);
	tcase_add_test(tc, aligned_heap_allocation_error);
	tcase_add_loop_test(tc, aligned_stack_allocation,
	                    0, MM_NELEM(stack_alloc_sizes));
	tcase_add_loop_test(tc, safe_stack_allocation,
	                    0, MM_NELEM(malloca_sizes));
	tcase_add_test(tc, safe_stack_allocation_error);

	return tc;
}