1 | /* |
---|
2 | * Test of Heap Walker |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2009. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #define __RTEMS_VIOLATE_KERNEL_VISIBILITY__ |
---|
15 | #define CONFIGURE_INIT |
---|
16 | #include "system.h" |
---|
17 | |
---|
18 | #include <stdlib.h> |
---|
19 | #include <inttypes.h> |
---|
20 | #include <errno.h> |
---|
21 | #include <string.h> |
---|
22 | #include <rtems/score/heap.h> |
---|
23 | #include <rtems/dumpbuf.h> |
---|
24 | |
---|
25 | #define TEST_HEAP_SIZE 1024 |
---|
26 | unsigned TestHeapMemory[TEST_HEAP_SIZE]; |
---|
27 | Heap_Control TestHeap; |
---|
28 | |
---|
29 | static void test_heap_init(void) |
---|
30 | { |
---|
31 | memset( TestHeapMemory, '\0', sizeof(TestHeapMemory) ); |
---|
32 | _Heap_Initialize( &TestHeap, TestHeapMemory, sizeof(TestHeapMemory), 0 ); |
---|
33 | } |
---|
34 | |
---|
35 | void test_heap_walk_body( int source, bool do_dump ); |
---|
36 | |
---|
37 | static void test_heap_walk( int source ) |
---|
38 | { |
---|
39 | test_heap_walk_body( source, true ); |
---|
40 | test_heap_walk_body( source, false ); |
---|
41 | } |
---|
42 | |
---|
43 | void test_heap_walk_body( int source, bool do_dump ) |
---|
44 | { |
---|
45 | unsigned i, j, original; |
---|
46 | |
---|
47 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
48 | |
---|
49 | /* |
---|
50 | * Now corrupt all non-zero values |
---|
51 | */ |
---|
52 | for (i=0 ; i<TEST_HEAP_SIZE ; i++) { |
---|
53 | original = TestHeapMemory[i]; |
---|
54 | if ( original == 0 ) |
---|
55 | continue; |
---|
56 | |
---|
57 | /* mark it free -- may or may not have already been */ |
---|
58 | TestHeapMemory[i] &= ~0x01U; |
---|
59 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
60 | |
---|
61 | /* mark it used -- may or may not have already been */ |
---|
62 | TestHeapMemory[i] |= 0x01; |
---|
63 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
64 | |
---|
65 | /* try values of 2-7 in the last three bits -- push alignment issues */ |
---|
66 | for (j=2 ; j<=7 ; j++) { |
---|
67 | TestHeapMemory[i] = (TestHeapMemory[i] & ~0x7U) | j; |
---|
68 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /* try 0 and see what that does */ |
---|
73 | TestHeapMemory[i] = 0x00; |
---|
74 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
75 | |
---|
76 | /* try 0xffffffff and see what that does */ |
---|
77 | TestHeapMemory[i] = 0xffffffff; |
---|
78 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
79 | |
---|
80 | TestHeapMemory[i] = original; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | static void test_walk_freshly_initialized(void) |
---|
85 | { |
---|
86 | puts( "Walk freshly initialized heap" ); |
---|
87 | test_heap_init(); |
---|
88 | test_heap_walk(1); |
---|
89 | } |
---|
90 | |
---|
91 | static void test_negative_source_value(void) |
---|
92 | { |
---|
93 | /* |
---|
94 | * Passing a negative value for source so that |
---|
95 | * the control enters the if block on line 67 |
---|
96 | */ |
---|
97 | puts( "Passing negative value for source" ); |
---|
98 | test_heap_init(); |
---|
99 | test_heap_walk( -1 ); |
---|
100 | } |
---|
101 | |
---|
102 | static void test_prev_block_flag_check(void) |
---|
103 | { |
---|
104 | /* Calling heapwalk without initialising the heap. |
---|
105 | * Covers line 80 and 85 on heapwalk. |
---|
106 | * Actually covers more than that. |
---|
107 | */ |
---|
108 | puts( "Calling Heap Walk without initialising" ); |
---|
109 | test_heap_walk( 1 ); |
---|
110 | } |
---|
111 | |
---|
112 | static void test_not_aligned(void) |
---|
113 | { |
---|
114 | /* |
---|
115 | * Hack to get to the error case where the blocks are |
---|
116 | * not on the page size. We initialize a heap with |
---|
117 | * default settings and change the page size to something |
---|
118 | * large. |
---|
119 | */ |
---|
120 | puts( "Testing case of blocks not on page size" ); |
---|
121 | test_heap_init(); |
---|
122 | TestHeap.page_size = 128; |
---|
123 | test_heap_walk( -1 ); |
---|
124 | } |
---|
125 | |
---|
126 | static void test_first_block_not_aligned(void) |
---|
127 | { |
---|
128 | /* |
---|
129 | * Hack to get to the error case where the blocks are |
---|
130 | * not on the page size. We initialize a heap with |
---|
131 | * default settings and change the page size to something |
---|
132 | * large. |
---|
133 | */ |
---|
134 | puts( "Testing case of blocks not on page size" ); |
---|
135 | test_heap_init(); |
---|
136 | _Heap_Free_list_head(&TestHeap)->next = (void *)1; |
---|
137 | test_heap_walk( -1 ); |
---|
138 | } |
---|
139 | |
---|
140 | static void test_not_in_free_list(void) |
---|
141 | { |
---|
142 | void *p1, *p2, *p3; |
---|
143 | |
---|
144 | /* |
---|
145 | * Hack to get to the error case where the blocks are |
---|
146 | * not on the page size. We initialize a heap with |
---|
147 | * default settings and change the page size to something |
---|
148 | * large. |
---|
149 | */ |
---|
150 | puts( "Testing case of blocks not on page size" ); |
---|
151 | test_heap_init(); |
---|
152 | p1 =_Heap_Allocate( &TestHeap, 32 ); |
---|
153 | p2 =_Heap_Allocate( &TestHeap, 32 ); |
---|
154 | p3 =_Heap_Allocate( &TestHeap, 32 ); |
---|
155 | _Heap_Free( &TestHeap, p2 ); |
---|
156 | test_heap_walk( -1 ); |
---|
157 | } |
---|
158 | |
---|
159 | rtems_task Init( |
---|
160 | rtems_task_argument argument |
---|
161 | ) |
---|
162 | { |
---|
163 | puts( "\n\n*** HEAP WALK TEST ***" ); |
---|
164 | |
---|
165 | test_prev_block_flag_check(); |
---|
166 | test_walk_freshly_initialized(); |
---|
167 | test_negative_source_value(); |
---|
168 | test_not_aligned(); |
---|
169 | test_not_in_free_list(); |
---|
170 | test_first_block_not_aligned(); |
---|
171 | |
---|
172 | puts( "*** END OF HEAP WALK TEST ***" ); |
---|
173 | rtems_test_exit(0); |
---|
174 | } |
---|