summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/heapwalk.c
blob: a6628df0b33ee3195f1ebd217266c32c9cc93c78 (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
/*
 *  Heap Handler
 *
 *  COPYRIGHT (c) 1989-2007.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

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

#include <rtems/system.h>
#include <rtems/score/address.h>
#include <rtems/score/sysstate.h>
#include <rtems/score/heap.h>
#include <rtems/score/interr.h>
#include <rtems/bspIo.h>

#if defined(__GNUC__)
  #define DO_NOT_INLINE __attribute__((__noinline__))
#else
  #define DO_NOT_INLINE
#endif
/*
 *  Helper to avoid introducing even more branches and paths in this
 *  code to do coverage analysis on.
 *
 *  We do not want this inlined.
 */
static void hw_nl( 
  int    error,
  bool   do_dump
) DO_NOT_INLINE;

/*PAGE
 *
 *  _Heap_Walk
 *
 *  This kernel routine walks the heap and verifies its correctness.
 *
 *  Input parameters:
 *    the_heap  - pointer to heap header
 *    source    - a numeric indicator of the invoker of this routine
 *    do_dump   - when true print the information
 *
 *  Output parameters: NONE
 */

bool _Heap_Walk(
  Heap_Control  *the_heap,
  int            source,
  bool           do_dump
)
{
  Heap_Block *the_block = the_heap->start;
  Heap_Block *const end = the_heap->final;
  Heap_Block *const tail = _Heap_Free_list_tail(the_heap);
  int error = 0;
  int passes = 0;

  /* FIXME: Why is this disabled? */
  do_dump = false;

  /* FIXME: Why is this disabled? */
  /*
   * We don't want to allow walking the heap until we have
   * transferred control to the user task so we watch the
   * system state.
   */

/*
  if ( !_System_state_Is_up( _System_state_Get() ) )
    return true;
*/

  /* FIXME: Reason for this? */
  if (source < 0)
    source = (int) the_heap->stats.instance;

  if (do_dump)
    printk("\nPASS: %d start %p final %p first %p last %p begin %p end %p\n",
      source, the_block, end,
      _Heap_First_free_block(the_heap), _Heap_Last_free_block(the_heap),
      the_heap->begin, the_heap->end);

  /*
   * Handle the 1st block
   */

  if (!_Heap_Is_prev_used(the_block)) {
    printk("PASS: %d !HEAP_PREV_BLOCK_USED flag of 1st block isn't set\n", source);
    error = 1;
  }

  if (the_block->prev_size != the_heap->page_size) {
    printk("PASS: %d !prev_size of 1st block isn't page_size\n", source);
    error = 1;
  }

  while ( the_block != end ) {
    uint32_t const the_size = _Heap_Block_size(the_block);
    Heap_Block *const next_block = _Heap_Block_at(the_block, the_size);
    bool    prev_used = _Heap_Is_prev_used(the_block);

    if (do_dump) {
      printk("PASS: %d block %p size %d(%c)",
        source, the_block, the_size, (prev_used ? 'U' : 'F'));
      if (prev_used)
        printk(" prev_size %d", the_block->prev_size);
      else
        printk(" (prev_size) %d", the_block->prev_size);
    }


    if (!_Addresses_Is_aligned(next_block) ) {
      printk("PASS: %d next_block %p is not aligned\n", source, next_block);
      error = 1;
      break;
    }
   
    if (!_Heap_Is_prev_used(next_block)) {
      if (do_dump)
        printk( " prev %p next %p", the_block->prev, the_block->next);
      if (_Heap_Block_size(the_block) != next_block->prev_size) {
        if (do_dump) printk("\n");
        printk("PASS: %d !front and back sizes don't match", source);
        error = 1;
      }
      if (!prev_used) {
        
        hw_nl(do_dump, error);
        printk("PASS: %d !two consecutive blocks are free", source);
        error = 1;
      }

      { /* Check if 'the_block' is in the free block list */
        Heap_Block* block = _Heap_First_free_block(the_heap);
        if (!_Addresses_Is_aligned(block) ) {
          printk(
            "PASS: %d first free block %p is not aligned\n", source, block);
          error = 1;
          break;
        }
        while(block != the_block && block != tail) {
          if (!_Addresses_Is_aligned(block) ) {
            printk(
              "PASS: %d a free block %p is not aligned\n", source, block);
            error = 1;
            break;
          }
          if (!_Heap_Is_block_in_heap(the_heap, block)) {
            printk("PASS: %d a free block %p is not in heap\n", source, block);
            error = 1;
            break;
          }
          block = block->next;
        }
        if (block != the_block) {
          hw_nl(do_dump, error);
          printk("PASS: %d !the_block not in the free list", source);
          error = 1;
        }
      }

    }
    hw_nl(do_dump, error);

    if (the_size < the_heap->min_block_size) {
      printk("PASS: %d !block size is too small\n", source);
      error = 1;
      break;
    }
    if (!_Heap_Is_aligned( the_size, the_heap->page_size)) {
      printk("PASS: %d !block size is misaligned\n", source);
      error = 1;
    }

    if (++passes > (do_dump ? 10 : 0) && error)
      break;

    the_block = next_block;
  }

  if (the_block != end) {
    printk("PASS: %d !last block address isn't equal to 'final' %p %p\n",
      source, the_block, end);
    error = 1;
  }

  if (_Heap_Block_size(the_block) != the_heap->page_size) {
    printk("PASS: %d !last block's size isn't page_size (%d != %d)\n", source,
           _Heap_Block_size(the_block), the_heap->page_size);
    error = 1;
  }

  if (do_dump && error)
    _Internal_error_Occurred( INTERNAL_ERROR_CORE, true, 0xffff0000 );

  return error;

}

/*
 *  This method exists to simplify branch paths in the generated code above.
 */
static void hw_nl( 
  int            error,
  bool           do_dump
)
{
  if (do_dump || error) printk("\n");
}