1 | /* |
---|
2 | * Copyright (c) 2010 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Obere Lagerstr. 30 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <stdlib.h> |
---|
21 | #include <assert.h> |
---|
22 | |
---|
23 | #include <bsp.h> |
---|
24 | |
---|
25 | #include <rtems/test.h> |
---|
26 | #include <rtems/score/heapimpl.h> |
---|
27 | |
---|
28 | #include <tmacros.h> |
---|
29 | |
---|
30 | const char rtems_test_name[] = "SPHEAPPROT"; |
---|
31 | |
---|
32 | #ifdef HEAP_PROTECTION |
---|
33 | static void test_heap_block_error(Heap_Control *heap, Heap_Block *block) |
---|
34 | { |
---|
35 | bool *error = heap->Protection.handler_data; |
---|
36 | |
---|
37 | *error = true; |
---|
38 | } |
---|
39 | |
---|
40 | static void test_heap_initialize( |
---|
41 | Heap_Control *heap, |
---|
42 | void *begin, |
---|
43 | uintptr_t size, |
---|
44 | bool *error |
---|
45 | ) |
---|
46 | { |
---|
47 | size = _Heap_Initialize(heap, begin, size, 0); |
---|
48 | assert(size > 0); |
---|
49 | |
---|
50 | heap->Protection.handler_data = error; |
---|
51 | heap->Protection.block_error = test_heap_block_error; |
---|
52 | |
---|
53 | *error = false; |
---|
54 | } |
---|
55 | |
---|
56 | static void test_heap_protection(void) |
---|
57 | { |
---|
58 | Heap_Control heap; |
---|
59 | Heap_Block *block = NULL; |
---|
60 | char area [512]; |
---|
61 | uintptr_t *p = NULL; |
---|
62 | uintptr_t max_size = 0; |
---|
63 | bool ok = false; |
---|
64 | bool error = false; |
---|
65 | |
---|
66 | /* Test double free */ |
---|
67 | |
---|
68 | test_heap_initialize(&heap, area, sizeof(area), &error); |
---|
69 | |
---|
70 | max_size = heap.stats.free_size |
---|
71 | - HEAP_BLOCK_HEADER_SIZE + HEAP_ALLOC_BONUS; |
---|
72 | |
---|
73 | p = _Heap_Allocate(&heap, max_size); |
---|
74 | assert(p != NULL); |
---|
75 | |
---|
76 | ok = _Heap_Free(&heap, p); |
---|
77 | assert(ok && !error); |
---|
78 | |
---|
79 | ok = _Heap_Free(&heap, p); |
---|
80 | assert(ok && error); |
---|
81 | |
---|
82 | /* Test begin overwrite */ |
---|
83 | |
---|
84 | test_heap_initialize(&heap, area, sizeof(area), &error); |
---|
85 | |
---|
86 | p = _Heap_Allocate(&heap, max_size); |
---|
87 | assert(p != NULL); |
---|
88 | |
---|
89 | *(p - 1) = 0; |
---|
90 | |
---|
91 | ok = _Heap_Free(&heap, p); |
---|
92 | assert(ok && error); |
---|
93 | |
---|
94 | /* Test end overwrite */ |
---|
95 | |
---|
96 | test_heap_initialize(&heap, area, sizeof(area), &error); |
---|
97 | |
---|
98 | p = _Heap_Allocate(&heap, max_size); |
---|
99 | assert(p != NULL); |
---|
100 | |
---|
101 | *(uintptr_t *)((char *) p + max_size) = 0; |
---|
102 | |
---|
103 | ok = _Heap_Free(&heap, p); |
---|
104 | assert(ok && error); |
---|
105 | |
---|
106 | /* Test use after free */ |
---|
107 | |
---|
108 | test_heap_initialize(&heap, area, sizeof(area), &error); |
---|
109 | |
---|
110 | p = _Heap_Allocate(&heap, max_size); |
---|
111 | assert(p != NULL); |
---|
112 | |
---|
113 | ok = _Heap_Free(&heap, p); |
---|
114 | assert(ok && !error); |
---|
115 | |
---|
116 | *p = 0; |
---|
117 | |
---|
118 | block = _Heap_Block_of_alloc_area((uintptr_t) p, heap.page_size); |
---|
119 | block->Protection_begin.next_delayed_free_block = HEAP_PROTECTION_OBOLUS; |
---|
120 | ok = _Heap_Free(&heap, p); |
---|
121 | assert(ok && error); |
---|
122 | } |
---|
123 | #else |
---|
124 | #define test_heap_protection() ((void) 0) |
---|
125 | #endif |
---|
126 | |
---|
127 | static rtems_task Init(rtems_task_argument argument) |
---|
128 | { |
---|
129 | TEST_BEGIN(); |
---|
130 | |
---|
131 | test_heap_protection(); |
---|
132 | |
---|
133 | TEST_END(); |
---|
134 | |
---|
135 | exit(0); |
---|
136 | } |
---|
137 | |
---|
138 | #define CONFIGURE_INIT |
---|
139 | |
---|
140 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
141 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
142 | |
---|
143 | #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM |
---|
144 | |
---|
145 | #define CONFIGURE_MAXIMUM_TASKS 2 |
---|
146 | #define CONFIGURE_MAXIMUM_DRIVERS 1 |
---|
147 | |
---|
148 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
149 | |
---|
150 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
151 | |
---|
152 | #include <rtems/confdefs.h> |
---|