Contiki-NG
stack-check.h
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 * \file
33 * Stack checker library header file.
34 * \author
35 * Atis Elsts <atis.elsts@bristol.ac.uk>
36 */
37
38/** \addtogroup sys
39 * @{ */
40
41/**
42 * \defgroup stack Stack checker library
43 *
44 * Basic support for stack guards and stack overflow detection.
45 * On startup, fills the area between the stack and the heap with a known pattern.
46 * During execution, the fill can be checked in order to find out
47 * the extent to which the stack has been used.
48 *
49 * @{
50 */
51
52#ifndef STACK_CHECK_H_
53#define STACK_CHECK_H_
54
55#include "contiki-conf.h"
56
57/* Determine whether stack checking is supported depending on the plaform. */
58#ifdef PLATFORM_CONF_SUPPORTS_STACK_CHECK
59#if !PLATFORM_CONF_SUPPORTS_STACK_CHECK
60/* Stack checker cannot be enabled, since the platform does not support it */
61#undef STACK_CHECK_CONF_ENABLED
62#define STACK_CHECK_CONF_ENABLED 0
63#endif /* !PLATFORM_CONF_SUPPORTS_STACK_CHECK */
64#endif /* ifdef PLATFORM_CONF_SUPPORTS_STACK_CHECK */
65
66/* If this is disabled, the functions are no-ops */
67#ifdef STACK_CHECK_CONF_ENABLED
68#define STACK_CHECK_ENABLED STACK_CHECK_CONF_ENABLED
69#else
70#define STACK_CHECK_ENABLED 1 /* Enable by default */
71#endif
72
73/* Perform periodic stack integrity checks? */
74#ifdef STACK_CHECK_CONF_PERIODIC_CHECKS
75#define STACK_CHECK_PERIODIC_CHECKS STACK_CHECK_CONF_PERIODIC_CHECKS
76#else
77#define STACK_CHECK_PERIODIC_CHECKS 1 /* Enable by default */
78#endif
79
80/* How often to do the periodic integrity checks, if enabled? */
81#ifdef STACK_CHECK_CONF_PERIOD
82#define STACK_CHECK_PERIOD STACK_CHECK_CONF_PERIOD
83#else
84#define STACK_CHECK_PERIOD (10 * CLOCK_SECOND)
85#endif
86
87/**
88 * \brief Initialize the stack area with a known pattern
89 *
90 * This function initializes the memory between the stack and heap
91 * areas. The function should be called by the system
92 * during boot-up.
93 */
94void stack_check_init(void);
95
96/**
97 * \brief Calculate the maximal stack usage so far.
98 *
99 * This function relies on the assumption that the stack memory
100 * that has been reserved by functions and local variables
101 * is actually overwritten with new contents. If the stack is
102 * just reserved, but not used, the function will fail to detect
103 * that usage.
104 * In addition, this function can warn if the stack memory range
105 * has been completely used, but it cannot detect
106 * and warn if stack overflow has already taken place.
107 */
108int32_t stack_check_get_usage(void);
109
110/**
111 * \brief Calculate the maximal permitted stack usage.
112 *
113 * This function returns the number of bytes between the origin
114 * of the stack and the end of heap.
115 */
117
118/**
119 * \brief The origin point from which the stack grows (an optional #define)
120 *
121 */
122#ifdef STACK_CONF_ORIGIN
123#define STACK_ORIGIN STACK_CONF_ORIGIN
124#endif
125
126#endif /* STACK_CHECK_H_ */
127
128/** @} */
129/** @} */
int32_t stack_check_get_reserved_size(void)
Calculate the maximal permitted stack usage.
Definition: stack-check.c:128
int32_t stack_check_get_usage(void)
Calculate the maximal stack usage so far.
Definition: stack-check.c:97
void stack_check_init(void)
Initialize the stack area with a known pattern.
Definition: stack-check.c:74