File indexing completed on 2025-05-11 08:24:47
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031
0032 #include <stdio.h>
0033 #include <stdlib.h>
0034 #include <assert.h>
0035
0036 #include <bsp.h>
0037
0038 #include <rtems/test-info.h>
0039 #include <rtems/score/heapimpl.h>
0040
0041 #include <tmacros.h>
0042
0043 const char rtems_test_name[] = "SPHEAPPROT";
0044
0045 #ifdef HEAP_PROTECTION
0046 static void test_heap_block_error(
0047 Heap_Control *heap,
0048 Heap_Block *block,
0049 Heap_Error_reason reason
0050 )
0051 {
0052 bool *error = heap->Protection.handler_data;
0053
0054 *error = true;
0055 }
0056
0057 static void test_heap_initialize(
0058 Heap_Control *heap,
0059 void *begin,
0060 uintptr_t size,
0061 bool *error
0062 )
0063 {
0064 size = _Heap_Initialize(heap, begin, size, 0);
0065 assert(size > 0);
0066
0067 heap->Protection.handler_data = error;
0068 heap->Protection.block_error = test_heap_block_error;
0069
0070 *error = false;
0071 }
0072
0073 static void test_heap_protection(void)
0074 {
0075 Heap_Control heap;
0076 Heap_Block *block = NULL;
0077 char area [512];
0078 uintptr_t *p = NULL;
0079 uintptr_t max_size = 0;
0080 bool ok = false;
0081 bool error = false;
0082
0083
0084
0085 test_heap_initialize(&heap, area, sizeof(area), &error);
0086
0087 max_size = heap.stats.free_size
0088 - HEAP_BLOCK_HEADER_SIZE + HEAP_ALLOC_BONUS;
0089
0090 p = _Heap_Allocate(&heap, max_size);
0091 assert(p != NULL);
0092
0093 ok = _Heap_Free(&heap, p);
0094 assert(ok && !error);
0095
0096 ok = _Heap_Free(&heap, p);
0097 assert(ok && error);
0098
0099
0100
0101 test_heap_initialize(&heap, area, sizeof(area), &error);
0102
0103 p = _Heap_Allocate(&heap, max_size);
0104 assert(p != NULL);
0105
0106 *(p - 1) = 0;
0107
0108 ok = _Heap_Free(&heap, p);
0109 assert(ok && error);
0110
0111
0112
0113 test_heap_initialize(&heap, area, sizeof(area), &error);
0114
0115 p = _Heap_Allocate(&heap, max_size);
0116 assert(p != NULL);
0117
0118 *(uintptr_t *)((char *) p + max_size) = 0;
0119
0120 ok = _Heap_Free(&heap, p);
0121 assert(ok && error);
0122
0123
0124
0125 test_heap_initialize(&heap, area, sizeof(area), &error);
0126
0127 p = _Heap_Allocate(&heap, max_size);
0128 assert(p != NULL);
0129
0130 ok = _Heap_Free(&heap, p);
0131 assert(ok && !error);
0132
0133 *p = 0;
0134
0135 block = _Heap_Block_of_alloc_area((uintptr_t) p, heap.page_size);
0136 block->Protection_begin.next_delayed_free_block = HEAP_PROTECTION_OBOLUS;
0137 ok = _Heap_Free(&heap, p);
0138 assert(ok && error);
0139 }
0140 #else
0141 #define test_heap_protection() ((void) 0)
0142 #endif
0143
0144 static rtems_task Init(rtems_task_argument argument)
0145 {
0146 TEST_BEGIN();
0147
0148 test_heap_protection();
0149
0150 TEST_END();
0151
0152 exit(0);
0153 }
0154
0155 #define CONFIGURE_INIT
0156
0157 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0158 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0159
0160 #define CONFIGURE_MAXIMUM_TASKS 2
0161
0162 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0163
0164 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0165
0166 #include <rtems/confdefs.h>