Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:47

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2010 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
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     /* Test double free */
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     /* Test begin overwrite */
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     /* Test end overwrite */
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     /* Test use after free */
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>