Contiki-NG
Loading...
Searching...
No Matches
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#include <stddef.h>
58
59/* Determine whether stack checking is supported depending on the plaform. */
60#ifdef PLATFORM_CONF_SUPPORTS_STACK_CHECK
61#if !PLATFORM_CONF_SUPPORTS_STACK_CHECK
62/* Stack checker cannot be enabled, since the platform does not support it */
63#undef STACK_CHECK_CONF_ENABLED
64#define STACK_CHECK_CONF_ENABLED 0
65#endif /* !PLATFORM_CONF_SUPPORTS_STACK_CHECK */
66#endif /* ifdef PLATFORM_CONF_SUPPORTS_STACK_CHECK */
67
68/* If this is disabled, the functions are no-ops */
69#ifdef STACK_CHECK_CONF_ENABLED
70#define STACK_CHECK_ENABLED STACK_CHECK_CONF_ENABLED
71#else
72#define STACK_CHECK_ENABLED 1 /* Enable by default */
73#endif
74
75/* Perform periodic stack integrity checks? */
76#ifdef STACK_CHECK_CONF_PERIODIC_CHECKS
77#define STACK_CHECK_PERIODIC_CHECKS STACK_CHECK_CONF_PERIODIC_CHECKS
78#else
79#define STACK_CHECK_PERIODIC_CHECKS 1 /* Enable by default */
80#endif
81
82/* How often to do the periodic integrity checks, if enabled? */
83#ifdef STACK_CHECK_CONF_PERIOD
84#define STACK_CHECK_PERIOD STACK_CHECK_CONF_PERIOD
85#else
86#define STACK_CHECK_PERIOD (10 * CLOCK_SECOND)
87#endif
88
89/**
90 * \brief Initialize the stack area with a known pattern
91 *
92 * This function initializes the memory between the stack and heap
93 * areas. The function should be called by the system
94 * during boot-up.
95 */
96void stack_check_init(void);
97
98/**
99 * \brief Calculate the maximal stack usage so far.
100 *
101 * This function relies on the assumption that the stack memory
102 * that has been reserved by functions and local variables
103 * is actually overwritten with new contents. If the stack is
104 * just reserved, but not used, the function will fail to detect
105 * that usage.
106 * In addition, this function can warn if the stack memory range
107 * has been completely used, but it cannot detect
108 * and warn if stack overflow has already taken place.
109 * Returns SIZE_MAX when an error occurs.
110 */
111size_t stack_check_get_usage(void);
112
113/**
114 * \brief Calculate the maximal permitted stack usage.
115 *
116 * This function returns the number of bytes between the origin
117 * of the stack and the end of heap.
118 */
120
121/**
122 * \brief The origin point from which the stack grows (an optional #define)
123 *
124 */
125#ifdef STACK_CONF_ORIGIN
126#define STACK_ORIGIN STACK_CONF_ORIGIN
127#endif
128
129#endif /* STACK_CHECK_H_ */
130
131/** @} */
132/** @} */
size_t stack_check_get_usage(void)
Calculate the maximal stack usage so far.
size_t stack_check_get_reserved_size(void)
Calculate the maximal permitted stack usage.
void stack_check_init(void)
Initialize the stack area with a known pattern.
Definition stack-check.c:74