Changeset 8bb9d41 in rtems


Ignore:
Timestamp:
Sep 7, 2016, 12:08:06 PM (3 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
master
Children:
84e960e
Parents:
bba988d
git-author:
Sebastian Huber <sebastian.huber@…> (09/07/16 12:08:06)
git-committer:
Sebastian Huber <sebastian.huber@…> (09/08/16 05:43:02)
Message:

stackchk: Use a const pattern to check

Location:
cpukit
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libmisc/stackchk/check.c

    rbba988d r8bb9d41  
    5151 */
    5252
    53 #ifdef CPU_STACK_CHECK_SIZE
    54 #define PATTERN_SIZE_WORDS      (((CPU_STACK_CHECK_SIZE / 4) + 3) & ~0x3)
    55 #else
    56 #define PATTERN_SIZE_WORDS      (4)
     53#if !defined(CPU_STACK_CHECK_PATTERN_INITIALIZER)
     54#define CPU_STACK_CHECK_PATTERN_INITIALIZER \
     55  { \
     56    0xFEEDF00D, 0x0BAD0D06, /* FEED FOOD to  BAD DOG */ \
     57    0xDEADF00D, 0x600D0D06  /* DEAD FOOD but GOOD DOG */ \
     58  }
    5759#endif
    58 
    59 #define PATTERN_SIZE_BYTES      (PATTERN_SIZE_WORDS * sizeof(uint32_t))
    6060
    6161/*
     
    6666#define U32_PATTERN 0xA5A5A5A5
    6767
    68 typedef struct {
    69    uint32_t    pattern[ PATTERN_SIZE_WORDS ];
    70 } Stack_check_Control;
    71 
    7268/*
    7369 *  Variable to indicate when the stack checker has been initialized.
     
    7874 *  The "magic pattern" used to mark the end of the stack.
    7975 */
    80 Stack_check_Control Stack_check_Pattern;
     76static const uint32_t Stack_check_Pattern[] =
     77  CPU_STACK_CHECK_PATTERN_INITIALIZER;
     78
     79#define PATTERN_SIZE_BYTES sizeof(Stack_check_Pattern)
     80
     81#define PATTERN_SIZE_WORDS RTEMS_ARRAY_SIZE(Stack_check_Pattern)
    8182
    8283/*
     
    8687 */
    8788static inline bool Stack_check_Frame_pointer_in_range(
    88   Stack_Control *the_stack
     89  const Thread_Control *the_thread
    8990)
    9091{
    9192  #if defined(__GNUC__)
    9293    void *sp = __builtin_frame_address(0);
     94    const Stack_Control *the_stack = &the_thread->Start.Initial_stack;
    9395
    9496    if ( sp < the_stack->area ) {
     
    111113  #define Stack_check_Get_pattern( _the_stack ) \
    112114    ((char *)(_the_stack)->area + \
    113          (_the_stack)->size - sizeof( Stack_check_Control ) )
     115         (_the_stack)->size - PATTERN_SIZE_BYTES )
    114116
    115117  #define Stack_check_Calculate_used( _low, _size, _high_water ) \
     
    133135
    134136  #define Stack_check_usable_stack_start(_the_stack) \
    135       ((char *)(_the_stack)->area + sizeof(Stack_check_Control))
     137      ((char *)(_the_stack)->area + PATTERN_SIZE_BYTES)
    136138
    137139#endif
    138 
    139 /*
    140  *  Obtain a properly typed pointer to the area to check.
    141  */
    142 #define Stack_check_Get_pattern_area( _the_stack ) \
    143   (Stack_check_Control *) Stack_check_Get_pattern( _the_stack )
    144140
    145141/*
     
    148144 */
    149145#define Stack_check_usable_stack_size(_the_stack) \
    150     ((_the_stack)->size - sizeof(Stack_check_Control))
     146    ((_the_stack)->size - PATTERN_SIZE_BYTES)
    151147
    152148#if (CPU_ALLOCATE_INTERRUPT_STACK == TRUE)
     
    170166static void Stack_check_Initialize( void )
    171167{
    172   int       i;
    173   uint32_t *p;
    174   static    uint32_t pattern[ 4 ] = {
    175     0xFEEDF00D, 0x0BAD0D06,  /* FEED FOOD to  BAD DOG */
    176     0xDEADF00D, 0x600D0D06   /* DEAD FOOD but GOOD DOG */
    177   };
    178 
    179168  if ( Stack_check_Initialized )
    180169    return;
    181 
    182   /*
    183    * Dope the pattern and fill areas
    184    */
    185   p = Stack_check_Pattern.pattern;
    186   for ( i = 0; i < PATTERN_SIZE_WORDS; i++ ) {
    187       p[i] = pattern[ i%4 ];
    188   }
    189170
    190171  /*
     
    204185}
    205186
     187static bool Stack_check_Is_pattern_valid(const Thread_Control *the_thread)
     188{
     189  return memcmp(
     190    Stack_check_Get_pattern(&the_thread->Start.Initial_stack),
     191    Stack_check_Pattern,
     192    PATTERN_SIZE_BYTES
     193  ) == 0;
     194}
     195
    206196/*
    207197 *  rtems_stack_checker_create_extension
     
    227217)
    228218{
    229   Stack_check_Control  *the_pattern;
    230 
    231219  if ( the_thread->Object.id == 0 )        /* skip system tasks */
    232220    return;
    233221
    234   the_pattern = Stack_check_Get_pattern_area(&the_thread->Start.Initial_stack);
    235 
    236   *the_pattern = Stack_check_Pattern;
     222  memcpy(
     223    Stack_check_Get_pattern(&the_thread->Start.Initial_stack),
     224    Stack_check_Pattern,
     225    PATTERN_SIZE_BYTES
     226  );
    237227}
    238228
     
    246236 *        the following message out.
    247237 */
    248 void Stack_check_report_blown_task(
    249   Thread_Control *running,
     238static void Stack_check_report_blown_task(
     239  const Thread_Control *running,
    250240  bool pattern_ok
    251 ) RTEMS_NO_RETURN;
    252 
    253 void Stack_check_report_blown_task(Thread_Control *running, bool pattern_ok)
    254 {
    255   Stack_Control *stack = &running->Start.Initial_stack;
    256   void          *pattern_area = Stack_check_Get_pattern(stack);
    257   char           name[32];
     241)
     242{
     243  const Stack_Control *stack = &running->Start.Initial_stack;
     244  void                *pattern_area = Stack_check_Get_pattern(stack);
     245  char                 name[32];
    258246
    259247  printk("BLOWN STACK!!!\n");
     
    306294)
    307295{
    308   Stack_Control *the_stack = &running->Start.Initial_stack;
    309   void          *pattern;
    310   bool           sp_ok;
    311   bool           pattern_ok = true;
    312 
    313   pattern = Stack_check_Get_pattern_area(the_stack);
     296  bool sp_ok;
     297  bool pattern_ok;
    314298
    315299  /*
    316300   *  Check for an out of bounds stack pointer or an overwrite
    317301   */
    318   sp_ok = Stack_check_Frame_pointer_in_range( the_stack );
    319 
    320   pattern_ok = (!memcmp( pattern,
    321             (void *) Stack_check_Pattern.pattern, PATTERN_SIZE_BYTES));
     302  sp_ok = Stack_check_Frame_pointer_in_range( running );
     303
     304  pattern_ok = Stack_check_Is_pattern_valid( running );
    322305
    323306  if ( !sp_ok || !pattern_ok ) {
     
    331314bool rtems_stack_checker_is_blown( void )
    332315{
    333   Thread_Control *executing = _Thread_Get_executing();
    334   Stack_Control  *the_stack = &executing->Start.Initial_stack;
    335   bool            sp_ok;
    336   bool            pattern_ok = true;
    337 
    338   /*
    339    *  Check for an out of bounds stack pointer
    340    */
    341 
    342   sp_ok = Stack_check_Frame_pointer_in_range( the_stack );
    343 
    344   /*
    345    * The stack checker must be initialized before the pattern is there
    346    * to check.
    347    */
    348   if ( Stack_check_Initialized ) {
    349     pattern_ok = (!memcmp(
    350       Stack_check_Get_pattern(the_stack),
    351       (void *) Stack_check_Pattern.pattern,
    352       PATTERN_SIZE_BYTES
    353     ));
    354   }
    355 
    356 
    357   /*
    358    * Let's report as much as we can.
    359    */
    360   if ( !sp_ok || !pattern_ok ) {
    361     Stack_check_report_blown_task( executing, pattern_ok );
    362     /* DOES NOT RETURN */
    363   }
     316  rtems_stack_checker_switch_extension( _Thread_Get_executing(), NULL );
    364317
    365318  /*
  • cpukit/score/cpu/powerpc/rtems/score/cpu.h

    rbba988d r8bb9d41  
    650650 */
    651651
    652 #define CPU_STACK_CHECK_SIZE    (128)
     652#define CPU_STACK_CHECK_PATTERN_INITIALIZER \
     653  { 0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
     654    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
     655    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
     656    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
     657    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
     658    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
     659    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
     660    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06 }
    653661
    654662/*
Note: See TracChangeset for help on using the changeset viewer.