Changeset f6a41d2 in rtems


Ignore:
Timestamp:
Sep 6, 2009, 3:24:56 PM (10 years ago)
Author:
Joel Sherrill <joel.sherrill@…>
Branches:
4.10, 4.11, master
Children:
0c7fbde
Parents:
dea3eccb
Message:

2009-09-03 Christian Mauderer <christian.mauderer@…>

  • malloctest/init.c: New test cases. Update for heap API changes.
  • stackchk/blow.c: Update for heap API changes.
Location:
testsuites/libtests
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • testsuites/libtests/ChangeLog

    rdea3eccb rf6a41d2  
     12009-09-03      Christian Mauderer <christian.mauderer@embedded-brains.de>
     2
     3        * malloctest/init.c: New test cases. Update for heap API changes.
     4        * stackchk/blow.c: Update for heap API changes.
     5
    162009-08-19      Sebastian Huber <Sebastian.Huber@embedded-brains.de>
    27
  • testsuites/libtests/malloctest/init.c

    rdea3eccb rf6a41d2  
    2727
    2828#include <stdlib.h>
     29#include <string.h>
    2930#include <inttypes.h>
    3031#include <errno.h>
     
    3435 *  A simple test of realloc
    3536 */
    36 void test_realloc(void)
     37static void test_realloc(void)
    3738{
    3839  void *p1, *p2, *p3, *p4;
    39   int i;
     40  size_t i;
    4041  int sc;
    4142
     
    4647    if (p2 != p1)
    4748      printf( "realloc - failed grow in place: "
    48               "%p != realloc(%p,%d)\n", p1, p2, i );
     49              "%p != realloc(%p,%d)\n", p1, p2, i);
    4950    p1 = p2;
    5051  }
     
    5758    if (p2 != p1)
    5859      printf( "realloc - failed shrink in place: "
    59               "%p != realloc(%p,%d)\n", p1, p2, i );
     60              "%p != realloc(%p,%d)\n", p1, p2, i);
    6061    p1 = p2;
    6162  }
     
    7071  if (p3 == p1 || p3 == NULL)
    7172    printf(
    72       "realloc - failed non-in place: realloc(%p,%d) = %p\n", p1, 64, p3 );
     73      "realloc - failed non-in place: realloc(%p,%d) = %p\n", p1, 64, p3);
    7374  free(p3);
    7475  free(p2);
     
    109110
    110111#define TEST_HEAP_SIZE 1024
     112
    111113uint8_t TestHeapMemory[TEST_HEAP_SIZE];
     114
    112115Heap_Control TestHeap;
    113116
    114 void test_heap_init()
    115 {
     117static void test_heap_default_init()
     118{
     119  memset( &TestHeapMemory, 0x7f, TEST_HEAP_SIZE );
    116120  _Heap_Initialize( &TestHeap, TestHeapMemory, TEST_HEAP_SIZE, 0 );
    117121}
    118122
    119 void test_heap_cases_1()
     123static void test_free( void *addr )
     124{
     125  rtems_test_assert( _Heap_Free( &TestHeap, addr ) );
     126}
     127
     128static void test_heap_cases_1()
    120129{
    121130  void     *p1, *p2, *p3, *p4;
     
    129138   * code like this:
    130139   */
    131   test_heap_init();
     140  test_heap_default_init();
    132141  p1 = _Heap_Allocate( &TestHeap, 12 );
    133142  p2 = _Heap_Allocate( &TestHeap, 32 );
    134143  p3 = _Heap_Allocate( &TestHeap, 32 );
    135   _Heap_Free( &TestHeap, p2 );
     144  test_free( p2 );
    136145  p2 = _Heap_Allocate_aligned( &TestHeap, 8, 28 );
    137   _Heap_Free( &TestHeap, p1 );
    138   _Heap_Free( &TestHeap, p2 );
    139   _Heap_Free( &TestHeap, p3 );
     146  test_free( p1 );
     147  test_free( p2 );
     148  test_free( p3 );
    140149
    141150  /*
    142151   *  Odd case in resizing a block.  Again test case outline per Sergei
    143152   */
    144   test_heap_init();
     153  test_heap_default_init();
    145154  p1 = _Heap_Allocate( &TestHeap, 32 );
    146155  p2 = _Heap_Allocate( &TestHeap, 8 );
    147156  p3 = _Heap_Allocate( &TestHeap, 32 );
    148   _Heap_Free( &TestHeap, p2 );
     157  test_free( p2 );
    149158  rsc = _Heap_Resize_block( &TestHeap, p1, 41, &u1, &u2 );
    150159  /* XXX what should we expect */
    151   _Heap_Free( &TestHeap, p3 );
    152   _Heap_Free( &TestHeap, p1 );
     160  test_free( p3 );
     161  test_free( p1 );
    153162 
    154163  /*
     
    159168   *  are contiguous.
    160169   */
    161   test_heap_init();
     170  test_heap_default_init();
    162171  puts( "Heap Initialized" );
    163172  p1 = _Heap_Allocate( &TestHeap, 500 );
     
    167176  rsc = _Heap_Resize_block( &TestHeap, p1, 256, &u1, &u2 );
    168177  rtems_test_assert( rsc == HEAP_RESIZE_SUCCESSFUL );
    169   _Heap_Free( &TestHeap, p1 );
    170   _Heap_Free( &TestHeap, p2 ); 
    171 }
    172 
    173 void test_heap_extend()
     178  test_free( p1 );
     179  test_free( p2 ); 
     180}
     181
     182#define TEST_DEFAULT_PAGE_SIZE 128
     183
     184static void test_heap_init(uintptr_t page_size )
     185{
     186  uintptr_t rv = 0;
     187
     188  memset( &TestHeapMemory, 0x7f, TEST_HEAP_SIZE );
     189
     190  rv = _Heap_Initialize( &TestHeap, TestHeapMemory, TEST_HEAP_SIZE, page_size );
     191  rtems_test_assert( rv > 0 );
     192}
     193
     194static void test_check_alloc(
     195  void *alloc_begin_ptr,
     196  void *expected_alloc_begin_ptr,
     197  uintptr_t alloc_size,
     198  uintptr_t alignment,
     199  uintptr_t boundary
     200)
     201{
     202  uintptr_t const min_block_size = TestHeap.min_block_size;
     203  uintptr_t const page_size = TestHeap.page_size;
     204
     205  rtems_test_assert( alloc_begin_ptr == expected_alloc_begin_ptr );
     206
     207  if( expected_alloc_begin_ptr != NULL ) {
     208    uintptr_t const alloc_begin = (uintptr_t ) alloc_begin_ptr;
     209    uintptr_t const alloc_end = alloc_begin + alloc_size;
     210
     211    uintptr_t const alloc_area_begin = _Heap_Align_down( alloc_begin, page_size );
     212    uintptr_t const alloc_area_offset = alloc_begin - alloc_area_begin;
     213    uintptr_t const alloc_area_size = alloc_area_offset + alloc_size;
     214
     215    Heap_Block *block = _Heap_Block_of_alloc_area( alloc_area_begin, page_size );
     216    uintptr_t const block_begin = (uintptr_t ) block;
     217    uintptr_t const block_size = _Heap_Block_size( block );
     218    uintptr_t const block_end = block_begin + block_size;
     219
     220    rtems_test_assert( block_size >= min_block_size );
     221    rtems_test_assert( block_begin < block_end );
     222    rtems_test_assert(
     223      _Heap_Is_aligned( block_begin + HEAP_BLOCK_HEADER_SIZE, page_size )
     224    );
     225    rtems_test_assert(
     226      _Heap_Is_aligned( block_size, page_size )
     227    );
     228
     229    rtems_test_assert( alloc_end <= block_end + HEAP_BLOCK_SIZE_OFFSET );
     230    rtems_test_assert( alloc_area_begin > block_begin );
     231    rtems_test_assert( alloc_area_offset < page_size );
     232
     233    rtems_test_assert( _Heap_Is_aligned( alloc_area_begin, page_size ) );
     234    if ( alignment == 0 ) {
     235      rtems_test_assert( alloc_begin == alloc_area_begin );
     236    } else {
     237      rtems_test_assert( _Heap_Is_aligned( alloc_begin, alignment ) );
     238    }
     239
     240    if ( boundary != 0 ) {
     241      uintptr_t boundary_line = _Heap_Align_down( alloc_end, boundary );
     242
     243      rtems_test_assert( alloc_size <= boundary );
     244      rtems_test_assert(
     245        boundary_line <= alloc_begin
     246          || alloc_end <= boundary_line
     247      );
     248    }
     249  }
     250
     251  rtems_test_assert(
     252    page_size < CPU_ALIGNMENT
     253      || _Heap_Walk( &TestHeap, 0, false )
     254  );
     255}
     256
     257static void test_check_alloc_simple(
     258  void *alloc_begin_ptr,
     259  uintptr_t alloc_size,
     260  uintptr_t alignment,
     261  uintptr_t boundary
     262)
     263{
     264  test_check_alloc(
     265    alloc_begin_ptr,
     266    alloc_begin_ptr,
     267    alloc_size,
     268    alignment,
     269    boundary
     270  );
     271}
     272
     273static void *test_alloc(
     274  uintptr_t alloc_size,
     275  uintptr_t alignment,
     276  uintptr_t boundary,
     277  void *expected_alloc_begin_ptr
     278)
     279{
     280  void *alloc_begin_ptr = _Heap_Allocate_aligned_with_boundary(
     281    &TestHeap,
     282    alloc_size,
     283    alignment,
     284    boundary
     285  );
     286
     287  test_check_alloc(
     288    alloc_begin_ptr,
     289    expected_alloc_begin_ptr,
     290    alloc_size,
     291    alignment,
     292    boundary
     293  );
     294
     295  return alloc_begin_ptr;
     296}
     297
     298static void *test_alloc_simple(
     299  uintptr_t alloc_size,
     300  uintptr_t alignment,
     301  uintptr_t boundary
     302)
     303{
     304  void *alloc_begin_ptr = _Heap_Allocate_aligned_with_boundary(
     305    &TestHeap,
     306    alloc_size,
     307    alignment,
     308    boundary
     309  );
     310
     311  test_check_alloc_simple(
     312    alloc_begin_ptr,
     313    alloc_size,
     314    alignment,
     315    boundary
     316  );
     317
     318  rtems_test_assert( alloc_begin_ptr != NULL );
     319
     320  return alloc_begin_ptr;
     321}
     322
     323static void *test_init_and_alloc(
     324  uintptr_t alloc_size,
     325  uintptr_t alignment,
     326  uintptr_t boundary,
     327  void *expected_alloc_begin_ptr
     328)
     329{
     330  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
     331
     332  return test_alloc(
     333    alloc_size,
     334    alignment,
     335    boundary,
     336    expected_alloc_begin_ptr
     337  );
     338}
     339
     340static void *test_init_and_alloc_simple(
     341  uintptr_t alloc_size,
     342  uintptr_t alignment,
     343  uintptr_t boundary
     344)
     345{
     346  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
     347
     348  return test_alloc_simple(
     349    alloc_size,
     350    alignment,
     351    boundary
     352  );
     353}
     354
     355static uintptr_t test_page_size(void)
     356{
     357  return TestHeap.page_size;
     358}
     359
     360static void test_heap_cases_2()
     361{
     362  void *p1 = NULL;
     363  void *p2 = NULL;
     364  void *p3 = NULL;
     365  uintptr_t alloc_size = 0;
     366  uintptr_t alignment = 0;
     367  uintptr_t boundary = 0;
     368  uintptr_t page_size = 0;
     369  uintptr_t first_page_begin = 0;
     370  uintptr_t previous_last_block_begin = 0;
     371  uintptr_t previous_last_page_begin = 0;
     372
     373  uintptr_t first_block_begin = 0;
     374
     375  uintptr_t last_block_begin = 0;
     376  uintptr_t last_alloc_begin = 0;
     377
     378  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
     379
     380  first_block_begin = (uintptr_t) TestHeap.first_block;
     381
     382  last_block_begin = (uintptr_t) TestHeap.last_block;
     383  last_alloc_begin = _Heap_Alloc_area_of_block( TestHeap.last_block );
     384
     385  puts( "run tests for _Heap_Allocate_aligned_with_boundary()");
     386
     387  puts( "\tcheck if NULL will be returned if size causes integer overflow" );
     388
     389  alloc_size = (uintptr_t ) -1;
     390  alignment = 0;
     391  boundary = 0;
     392  test_init_and_alloc( alloc_size, alignment, boundary, NULL );
     393
     394  puts( "\ttry to allocate more space than the one which fits in the boundary" );
     395
     396  alloc_size = 2;
     397  alignment = 0;
     398  boundary = alloc_size - 1;
     399  test_init_and_alloc( alloc_size, alignment, boundary, NULL );
     400
     401  puts( "\tcheck if alignment will be set to page size if only a boundary is given" );
     402
     403  alloc_size = 1;
     404  boundary = 1;
     405
     406  alignment = 0;
     407  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
     408
     409  alignment = test_page_size();
     410  test_init_and_alloc( alloc_size, alignment, boundary, p1 );
     411
     412  puts( "\tcreate a block which is bigger then the first free space" );
     413
     414  alignment = 0;
     415  boundary = 0;
     416
     417  alloc_size = test_page_size();
     418  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
     419  p2 = test_alloc_simple( alloc_size, alignment, boundary );
     420  test_free( p1 );
     421
     422  alloc_size = 2 * alloc_size;
     423  p3 = test_alloc_simple( alloc_size, alignment, boundary );
     424  rtems_test_assert( p1 != p3 );
     425
     426  puts( "\tset boundary before allocation begin" );
     427
     428  alloc_size = 1;
     429  alignment = 0;
     430  boundary = last_alloc_begin - test_page_size();
     431  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
     432  rtems_test_assert( (uintptr_t ) p1 >= boundary );
     433
     434  puts( "\tset boundary between allocation begin and end" );
     435  alloc_size = test_page_size();
     436  alignment = 0;
     437  boundary = last_alloc_begin - alloc_size / 2;
     438  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
     439  rtems_test_assert( (uintptr_t ) p1 + alloc_size <= boundary );
     440
     441  puts( "\tset boundary after allocation end" );
     442  alloc_size = 1;
     443  alignment = 0;
     444  boundary = last_alloc_begin;
     445  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
     446  rtems_test_assert( (uintptr_t ) p1 + alloc_size < boundary );
     447
     448  puts( "\tset boundary on allocation end" );
     449  alloc_size = TEST_DEFAULT_PAGE_SIZE - HEAP_BLOCK_HEADER_SIZE;
     450  alignment = 0;
     451  boundary = last_block_begin;
     452  p1 = (void *) (last_alloc_begin - TEST_DEFAULT_PAGE_SIZE);
     453  test_init_and_alloc( alloc_size, alignment, boundary, p1);
     454
     455  puts( "\talign the allocation to different positions in the block header" );
     456
     457  page_size = sizeof(uintptr_t);
     458  alloc_size = 1;
     459  boundary = 0;
     460
     461  test_heap_init( page_size );
     462
     463  /* Force the page size to a small enough value */
     464  TestHeap.page_size = page_size;
     465 
     466  alignment = first_page_begin - sizeof(uintptr_t);
     467  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
     468 
     469  first_page_begin = ((uintptr_t) TestHeap.first_block ) + HEAP_BLOCK_HEADER_SIZE;
     470  alignment = first_page_begin + sizeof(uintptr_t);
     471  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
     472
     473  first_page_begin = ((uintptr_t) TestHeap.first_block )
     474          + HEAP_BLOCK_HEADER_SIZE;
     475  alignment = first_page_begin;
     476  p1 = test_alloc_simple( alloc_size, alignment, boundary );
     477 
     478  puts( "\tallocate last block with different boundarys" );
     479  page_size = TEST_DEFAULT_PAGE_SIZE;
     480  test_heap_init( page_size );
     481  previous_last_block_begin = ((uintptr_t) TestHeap.last_block )
     482          - TestHeap.min_block_size;
     483  previous_last_page_begin = previous_last_block_begin
     484          + HEAP_BLOCK_HEADER_SIZE;
     485  alloc_size = TestHeap.page_size - HEAP_BLOCK_HEADER_SIZE;
     486  alignment = sizeof(uintptr_t);
     487  boundary = 0;
     488  p1 = test_alloc( alloc_size, alignment, boundary, (void *) (previous_last_page_begin + sizeof(uintptr_t)));
     489 
     490  test_heap_init( page_size );
     491  boundary = ((uintptr_t) TestHeap.last_block );
     492  p1 = test_alloc( alloc_size, alignment, boundary, (void *) previous_last_page_begin );
     493
     494  puts( "\tbreak the boundaries and aligns more than one time" );
     495
     496  page_size = CPU_ALIGNMENT * 20;
     497  alloc_size = page_size / 4;
     498  alignment = page_size / 5;
     499  boundary = page_size / 4;
     500  test_heap_init( page_size );
     501  p1 = (void *) (_Heap_Alloc_area_of_block( TestHeap.last_block ) - page_size );
     502  test_alloc( alloc_size, alignment, boundary, p1);
     503
     504  puts( "\tdifferent combinations, so that there is no valid block at the end" );
     505
     506  page_size = sizeof(uintptr_t);
     507
     508  test_heap_init( 0 );
     509
     510  /* Force the page size to a small enough value */
     511  TestHeap.page_size = page_size;
     512
     513  alloc_size = 1;
     514  alignment = (uintptr_t) TestHeap.last_block;
     515  boundary = 0;
     516  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
     517
     518  boundary = (uintptr_t) TestHeap.last_block;
     519  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
     520 
     521  alloc_size = 0;
     522  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
     523 
     524  alloc_size = 1;
     525  alignment = sizeof(uintptr_t);
     526  boundary = 0;
     527  p1 = test_alloc_simple( alloc_size, alignment, boundary );
     528   
     529  puts( "\ttry to create a block, which is not possible because of the alignment and boundary" );
     530
     531  alloc_size = 2;
     532  boundary = _Heap_Alloc_area_of_block( TestHeap.first_block )
     533          + _Heap_Block_size( TestHeap.first_block ) / 2;
     534  alignment = boundary - 1;
     535  p1 = test_init_and_alloc( alloc_size, alignment, boundary, NULL );
     536
     537  alloc_size = 2;
     538  alignment = _Heap_Alloc_area_of_block( TestHeap.first_block );
     539  boundary = alignment + 1;
     540  p1 = test_init_and_alloc( alloc_size, alignment, boundary, NULL );
     541}
     542
     543static void test_block_alloc( uintptr_t alloc_begin, uintptr_t alloc_size )
     544{
     545  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
     546
     547  _Heap_Block_allocate(
     548    &TestHeap,
     549    TestHeap.first_block,
     550    alloc_begin,
     551    alloc_size
     552  );
     553
     554  test_check_alloc_simple( (void *) alloc_begin, alloc_size, 0, 0 );
     555}
     556
     557static void test_heap_cases_block_allocate()
     558{
     559  uintptr_t alloc_begin = 0;
     560  uintptr_t alloc_size = 0;
     561  uintptr_t alloc_box_begin = 0;
     562  uintptr_t alloc_box_end = 0;
     563  uintptr_t alloc_box_size = 0;
     564
     565  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
     566
     567  alloc_box_begin = _Heap_Alloc_area_of_block( TestHeap.first_block );
     568  alloc_box_size = _Heap_Block_size( TestHeap.first_block );
     569  alloc_box_end = alloc_box_begin + alloc_box_size;
     570
     571  puts( "run tests for _Heap_Block_allocate()" );
     572
     573  puts( "\tallocate block at the beginning");
     574  alloc_begin = alloc_box_begin;
     575  alloc_size = 0;
     576  test_block_alloc( alloc_begin, alloc_size );
     577 
     578  puts( "\tallocate block full space");
     579  alloc_begin = alloc_box_begin;
     580  alloc_size = alloc_box_size + HEAP_BLOCK_SIZE_OFFSET
     581    - HEAP_BLOCK_HEADER_SIZE;
     582  test_block_alloc( alloc_begin, alloc_size );
     583
     584  puts( "\tallocate block in the middle");
     585  alloc_begin = alloc_box_begin + TEST_DEFAULT_PAGE_SIZE;
     586  alloc_size = 0;
     587  test_block_alloc( alloc_begin, alloc_size );
     588 
     589  puts( "\tallocate block at the end");
     590  alloc_begin = alloc_box_end - TEST_DEFAULT_PAGE_SIZE;
     591  alloc_size = TEST_DEFAULT_PAGE_SIZE + HEAP_BLOCK_SIZE_OFFSET
     592    - HEAP_BLOCK_HEADER_SIZE;
     593  test_block_alloc( alloc_begin, alloc_size );
     594}
     595
     596static void test_heap_extend()
    174597{
    175598  void     *p1, *p2, *p3, *p4;
     
    192615}
    193616
    194 void test_heap_info(void)
     617static void test_heap_info(void)
    195618{
    196619  size_t                  s1, s2;
     
    238661}
    239662
    240 void test_protected_heap_info(void)
     663static void test_protected_heap_info(void)
    241664{
    242665  Heap_Control           heap;
     
    253676}
    254677
    255 void test_heap_resize(void)
     678static void test_heap_resize(void)
    256679{
    257680  Heap_Resize_status  rc;
     
    261684
    262685  puts( "Initialize Test Heap" );
    263   test_heap_init();
     686  test_heap_default_init();
    264687 
    265688  puts( "Allocate most of heap" );
     
    275698 *  A simple test of posix_memalign
    276699 */
    277 void test_posix_memalign(void)
     700static void test_posix_memalign(void)
    278701{
    279702  void *p1, *p2;
     
    334757  directive_failed( status, "rtems_clock_set" );
    335758
     759  test_heap_cases_2();
     760  test_heap_cases_block_allocate();
    336761  test_realloc();
    337762  test_heap_cases_1();
  • testsuites/libtests/stackchk/blow.c

    rdea3eccb rf6a41d2  
    3131 area = (unsigned char *)_Thread_Executing->Start.Initial_stack.area;
    3232
    33  low  = (volatile uint32_t   *) (area + HEAP_LAST_BLOCK_OVERHEAD);
     33 low  = (volatile uint32_t   *) (area + HEAP_BLOCK_HEADER_SIZE);
    3434 high = (volatile uint32_t   *)
    3535            (area + _Thread_Executing->Start.Initial_stack.size - 16);
Note: See TracChangeset for help on using the changeset viewer.