Contiki-NG
stack-check.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, University of Bristol - http://www.bris.ac.uk/
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 /**
32  * \addtogroup stack
33  * @{
34  */
35 
36 /**
37  * \file
38  * Implementation of the stack checker library.
39  * \author
40  * Atis Elsts <atis.elsts@bristol.ac.uk>
41  */
42 
43 #include "contiki.h"
44 #include "sys/stack-check.h"
45 #include "dev/watchdog.h"
46 #include <string.h>
47 #include <inttypes.h>
48 
49 #include "sys/log.h"
50 #define LOG_MODULE "Stack"
51 #define LOG_LEVEL LOG_LEVEL_MAIN
52 
53 /*---------------------------------------------------------------------------*/
54 /* linker will provide a symbol for the end of the .bss segment */
55 extern uint8_t _stack;
56 
57 #if STACK_CHECK_PERIODIC_CHECKS
58 PROCESS(stack_check_process, "Stack check");
59 #endif
60 /*---------------------------------------------------------------------------*/
61 /* The symbol with which the stack memory is initially filled */
62 #define STACK_FILL 0xcd
63 /*---------------------------------------------------------------------------*/
64 #ifdef STACK_ORIGIN
65 /* use the #defined value */
66 #define GET_STACK_ORIGIN() STACK_ORIGIN
67 #else
68 /* use the value provided by the linker script */
69 extern int _stack_origin;
70 #define GET_STACK_ORIGIN() (&_stack_origin)
71 #endif
72 /*---------------------------------------------------------------------------*/
73 void
75 {
76  uint8_t *p;
77 
78  /* Make this static to avoid destroying it in the while loop */
79  static void *stack_top;
80  /* Use address of this local variable as a boundary */
81  stack_top = &p;
82 
83  /* Note: this is expected to be called before the WDT is started! */
84  p = &_stack;
85  while(p < (uint8_t *)stack_top) {
86  *p++ = STACK_FILL;
87  }
88 
89 #if STACK_CHECK_PERIODIC_CHECKS
90  /* Start the periodic checker process */
91  process_start(&stack_check_process, NULL);
92 #endif
93 }
94 /*---------------------------------------------------------------------------*/
95 int32_t
97 {
98  uint8_t *p = &_stack;
99 
100  /* Make sure WDT is not triggered */
102 
103  /* Skip the bytes used after heap; it's 1 byte by default for _stack,
104  * more than that means dynamic memory allocation is used somewhere.
105  */
106  while(*p != STACK_FILL && p < (uint8_t *)GET_STACK_ORIGIN()) {
107  p++;
108  }
109 
110  /* Skip the region of the memory reserved for the stack not used yet by the program */
111  while(*p == STACK_FILL && p < (uint8_t *)GET_STACK_ORIGIN()) {
112  p++;
113  }
114 
115  /* Make sure WDT is not triggered */
117 
118  if(p >= (uint8_t*)GET_STACK_ORIGIN()) {
119  /* This means the stack is screwed. */
120  return -1;
121  }
122 
123  return (uint8_t *)GET_STACK_ORIGIN() - p;
124 }
125 /*---------------------------------------------------------------------------*/
126 int32_t
128 {
129  return (uint8_t *)GET_STACK_ORIGIN() - &_stack;
130 }
131 /*---------------------------------------------------------------------------*/
132 #if STACK_CHECK_PERIODIC_CHECKS
133 /*---------------------------------------------------------------------------*/
134 PROCESS_THREAD(stack_check_process, ev, data)
135 {
136  static struct etimer et;
137 
138  PROCESS_BEGIN();
139 
140  etimer_set(&et, STACK_CHECK_PERIOD);
141 
142  while(1) {
143  int32_t actual, allowed;
144 
146 
147  actual = stack_check_get_usage();
148  allowed = stack_check_get_reserved_size();
149  if(actual < 0 || allowed < 0) {
150  LOG_ERR("Check in inconsistent state: %" PRId32 " vs. %" PRId32 "\n", actual, allowed);
151  } else if(actual > allowed) {
152  LOG_ERR("Check failed: %" PRId32 " vs. %" PRId32 "\n", actual, allowed);
153  } else {
154  LOG_DBG("Check ok: %" PRId32 " vs. %" PRId32 "\n", actual, allowed);
155  }
156 
157  etimer_reset(&et);
158  }
159 
160  PROCESS_END();
161 }
162 /*---------------------------------------------------------------------------*/
163 #endif /* STACK_CHECK_PERIODIC_CHECKS */
164 /*---------------------------------------------------------------------------*/
165 /** @} */
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
int32_t stack_check_get_usage(void)
Calculate the maximal stack usage so far.
Definition: stack-check.c:96
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
void stack_check_init(void)
Initialize the stack area with a known pattern.
Definition: stack-check.c:74
A timer.
Definition: etimer.h:76
Stack checker library header file.
int32_t stack_check_get_reserved_size(void)
Calculate the maximal permitted stack usage.
Definition: stack-check.c:127
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:85
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1107
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
Header file for the logging system
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99