summaryrefslogtreecommitdiff
path: root/testsuites/libtests/heapwalk/init.c
blob: cd5caaa852c85351e9dd307e29af0c796cef7d0d (plain) (blame)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 *  Test of Heap Walker
 *
 *  COPYRIGHT (c) 1989-2009.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  Copyright (c) 2009 embedded brains GmbH.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define CONFIGURE_INIT
#include "system.h"

#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <rtems/score/heapimpl.h>
#include <rtems/score/sysstate.h>
#include <rtems/dumpbuf.h>

const char rtems_test_name[] = "HEAPWALK";

#define TEST_HEAP_SIZE 1024
#define TEST_DEFAULT_PAGESIZE 128
#define DUMP false

unsigned TestHeapMemory[TEST_HEAP_SIZE];
Heap_Control TestHeap;

static void test_heap_init_with_page_size( uintptr_t page_size )
{
  memset( TestHeapMemory, 0xFF, sizeof(TestHeapMemory) );
  _Heap_Initialize( &TestHeap, TestHeapMemory, sizeof(TestHeapMemory), page_size );
}

static void test_heap_init_default(void)
{
  test_heap_init_with_page_size( 0 );
}

static void test_heap_init_custom(void)
{
  test_heap_init_with_page_size( TEST_DEFAULT_PAGESIZE );
}

static void test_call_heap_walk( bool expectet_retval )
{
  bool retval = _Heap_Walk( &TestHeap, 0, DUMP );
  rtems_test_assert( retval == expectet_retval );
}

static void *test_allocate_block(void)
{
  return _Heap_Allocate_aligned_with_boundary( &TestHeap, 1, 0, 0 );
}

static void test_create_heap_with_gaps(void)
{
  void *p1 = test_allocate_block();
  /* void *p2 = */ test_allocate_block();
  void *p3 = test_allocate_block();
  /* void *p4 = */ test_allocate_block();
  void *p5 = test_allocate_block();
  /* void *p6 = */ test_allocate_block();
  _Heap_Free( &TestHeap, p1 );
  _Heap_Protection_free_all_delayed_blocks( &TestHeap );
  _Heap_Free( &TestHeap, p3 );
  _Heap_Protection_free_all_delayed_blocks( &TestHeap );
  _Heap_Free( &TestHeap, p5 );
  _Heap_Protection_free_all_delayed_blocks( &TestHeap );
}

static void *test_fill_heap(void)
{
  void *p1 = NULL;
  void *p2 = NULL;

  do {
    p2 = p1;
    p1 = test_allocate_block();
  }while( p1 != NULL );

  return p2;
}

static void test_system_not_up(void)
{
  rtems_interrupt_level level;

  puts( "start with a system state != SYSTEM_STATE_UP" );

  rtems_interrupt_local_disable( level );
  System_state_Codes state = _System_state_Get();
  _System_state_Set( SYSTEM_STATE_TERMINATED );
  test_call_heap_walk( true );
  _System_state_Set( state );
  rtems_interrupt_local_enable( level );
}

static void test_check_control(void)
{
  puts( "testing the _Heap_Walk_check_control() function" );

  puts( "\ttest what happens if page size = 0" );
  test_heap_init_default();
  TestHeap.page_size = 0;
  test_call_heap_walk( false );

  puts( "\tset page size to a not CPU-aligned value" );
  test_heap_init_default();
  TestHeap.page_size = 3 * (CPU_ALIGNMENT) / 2;
  test_call_heap_walk( false );

  puts( "\tset minimal block size to a not page aligned value" );
  test_heap_init_custom();
  TestHeap.min_block_size = TEST_DEFAULT_PAGESIZE / 2;
  test_call_heap_walk( false );

  puts( "\tset allocation area of the first block to be non page-aligned" );
  test_heap_init_custom();
  TestHeap.first_block = (Heap_Block *) ((char *) TestHeap.first_block + CPU_ALIGNMENT);
  test_call_heap_walk( false );

  puts( "\tclear the previous used flag of the first block" );
  test_heap_init_default();
  TestHeap.first_block->size_and_flag &= ~HEAP_PREV_BLOCK_USED;
  test_call_heap_walk( false );

  puts( "\tmark last block as free" );
  test_heap_init_custom();
  TestHeap.last_block->size_and_flag = 0;
  test_call_heap_walk( false );

  puts( "\tset invalid next block for last block" );
  test_heap_init_custom();
  TestHeap.last_block->size_and_flag = HEAP_PREV_BLOCK_USED;
  test_call_heap_walk( false );
}

static void test_check_free_list(void)
{
  void *p1 = NULL;
  Heap_Block *first_free_block = NULL;
  Heap_Block *secound_free_block = NULL;
  Heap_Block *third_free_block = NULL;
  Heap_Block *used_block = NULL;

  puts( "testing the _Heap_Walk_check_free_list() function" );

  puts( "\tno free blocks" );
  test_heap_init_custom();
  test_fill_heap();
  test_call_heap_walk( true );

  puts( "\tcreate a loop in the free list" );
  test_heap_init_default();
  test_create_heap_with_gaps();
  /* find free blocks */
  first_free_block = _Heap_Free_list_first( &TestHeap );
  secound_free_block = first_free_block->next;
  third_free_block = secound_free_block->next;
  /* create a loop */
  third_free_block->next = secound_free_block;
  secound_free_block->prev = third_free_block;
  test_call_heap_walk( false );

  puts( "\tput a block outside the heap to the free list" );
  test_heap_init_default();
  first_free_block = _Heap_Free_list_first( &TestHeap );
  first_free_block->next = TestHeap.first_block - 1;
  test_call_heap_walk( false );

  puts( "\tput a block on the free list, which is not page-aligned" );
  test_heap_init_custom();
  test_create_heap_with_gaps();
  first_free_block = _Heap_Free_list_first( &TestHeap );
  first_free_block->next = (Heap_Block *) ((uintptr_t) first_free_block->next + CPU_ALIGNMENT);
  first_free_block->next->prev = first_free_block;
  test_call_heap_walk( false );

  puts( "\tput a used block on the free list" );
  test_heap_init_custom();
  test_create_heap_with_gaps();
  p1 = test_allocate_block();
  first_free_block = _Heap_Free_list_first( &TestHeap );
  used_block = _Heap_Block_of_alloc_area( (uintptr_t) p1, TestHeap.page_size );
  _Heap_Free_list_insert_after( first_free_block, used_block );
  test_call_heap_walk( false );
}

static void test_freshly_initialized(void)
{
  puts( "Walk freshly initialized heap" );
  test_heap_init_default();
  test_call_heap_walk( true );
}

static void test_main_loop(void)
{
  void *p1 = NULL;
  void *p2 = NULL;
  Heap_Block *block = NULL;
  Heap_Block *next_block = NULL;

  puts( "Test the main loop" );

  puts( "\tset the blocksize so, that the next block is outside the heap" );
  test_heap_init_custom();
  /* use all blocks */
  p1 = test_fill_heap();
  block = _Heap_Block_of_alloc_area( (uintptr_t) p1, TestHeap.page_size );
  block->size_and_flag = ( 2 * _Heap_Block_size( block ) ) | HEAP_PREV_BLOCK_USED;
  test_call_heap_walk( false );

  puts( "\twalk a heap with blocks with different states of the previous-used flag" );
  test_heap_init_custom();
  test_create_heap_with_gaps();
  test_allocate_block(); /* fill one gap */
  test_call_heap_walk( true );

  puts( "\tcreate a block with a not page aligned size" );
  test_heap_init_custom();
  p1 = test_allocate_block();
  p2 = test_allocate_block();
  _Heap_Free( &TestHeap, p1 );
  block = _Heap_Block_of_alloc_area( (uintptr_t) p2, TestHeap.page_size );
  block->size_and_flag = (3 * TestHeap.page_size / 2) & ~HEAP_PREV_BLOCK_USED;
  test_call_heap_walk( false );

  puts( "\tcreate a block with a size smaller than the min_block_size" );
  test_heap_init_default();
  p1 = test_allocate_block();
  p2 = test_allocate_block();
  _Heap_Free( &TestHeap, p1 );
  block = _Heap_Block_of_alloc_area( (uintptr_t) p2, TestHeap.page_size );
  block->size_and_flag = 0;
  test_call_heap_walk( false );

  puts( "\tmake a block with a size, so that the block reaches into the next block" );
  test_heap_init_default();
  p1 = test_allocate_block();
  p2 = test_allocate_block();
  block = _Heap_Block_of_alloc_area( (uintptr_t) p1, TestHeap.page_size  );
  block->size_and_flag = ( 3 * _Heap_Block_size( block ) / 2 ) | HEAP_PREV_BLOCK_USED;
  test_call_heap_walk( false );

  puts( "\tcreate a block with invalid successor" );
  test_heap_init_default();
  test_allocate_block();
  p1 = test_allocate_block();
  block = _Heap_Block_of_alloc_area( (uintptr_t) p1, TestHeap.page_size  );
  block->size_and_flag = (0 - TestHeap.page_size) | HEAP_PREV_BLOCK_USED;
  test_call_heap_walk( false );

  puts( "\tmake a block with a size, so that it includes the next block" );
  test_heap_init_default();
  p1 = test_allocate_block();
  p2 = test_allocate_block();
  block = _Heap_Block_of_alloc_area( (uintptr_t) p1, TestHeap.page_size  );
  next_block = _Heap_Block_at( block, _Heap_Block_size( block ) );
  block->size_and_flag = ( _Heap_Block_size( block ) + _Heap_Block_size( next_block ) ) | HEAP_PREV_BLOCK_USED;
  test_call_heap_walk( true );
}

static void test_check_free_block(void)
{
  Heap_Block *block = NULL;
  Heap_Block *next_block = NULL;
  Heap_Block *first_free_block = NULL;
  Heap_Block *secound_free_block = NULL;
  void *p1 = NULL;

  puts( "test the _Heap_Walk_check_free_block() function" );

  puts( "\tset a previous size for the next block which is not equal to the size of the actual block" );
  test_heap_init_default();
  block = _Heap_Free_list_first( &TestHeap );
  next_block = _Heap_Block_at( block, _Heap_Block_size( block ) );
  next_block->prev_size = _Heap_Block_size( block ) - 1;
  test_call_heap_walk( false );

  puts( "\tclear the previous_used flag of the first free block after an used block" );
  test_heap_init_default();
  p1 = test_allocate_block();
  block = _Heap_Block_of_alloc_area( (uintptr_t) p1, TestHeap.page_size );
  first_free_block = _Heap_Free_list_first( &TestHeap );
  first_free_block->size_and_flag &= ~HEAP_PREV_BLOCK_USED;
  first_free_block->prev_size = _Heap_Block_size( block );
  _Heap_Free_list_insert_after( first_free_block, block );
  test_call_heap_walk( false );

  puts( "\ttake a free block out of the free list" );
  test_heap_init_custom();
  test_create_heap_with_gaps();
  first_free_block = _Heap_Free_list_first( &TestHeap );
  secound_free_block = first_free_block->next;
  _Heap_Free_list_remove( secound_free_block );
  test_call_heap_walk( false );
}

static void test_output(void)
{
  puts( "test the output-function for the _Heap_Walk()" );
  puts( "therefore use the (already tested) case with a page size of 0" );
  /* use simple case where one PASS and one FAIL will be put out */
  test_heap_init_default();
  TestHeap.page_size = 0;
  test_call_heap_walk( false );
   _Heap_Walk( &TestHeap, 0, true );
}

rtems_task Init(
  rtems_task_argument argument
)
{
  TEST_BEGIN();

  test_system_not_up();
  test_check_control();
  test_check_free_list();
  test_freshly_initialized();
  test_main_loop();
  test_check_free_block();
  test_output();

  TEST_END();
  rtems_test_exit(0);
}