Add stack check library and example
This commit is contained in:
parent
9476849afd
commit
f9516eae73
@ -96,11 +96,18 @@ SECTIONS
|
||||
_ebss = .;
|
||||
} > FRSRAM
|
||||
|
||||
_end = .; /* End of the .bss segment. */
|
||||
|
||||
/* This symbol is used by the stack check library. */
|
||||
_stack = .;
|
||||
|
||||
.stack (NOLOAD) :
|
||||
{
|
||||
*(.stack)
|
||||
} > FRSRAM
|
||||
|
||||
/* This symbol is used by the stack check library. */
|
||||
_stack_origin = .;
|
||||
_heap = .;
|
||||
_eheap = ORIGIN(FRSRAM) + LENGTH(FRSRAM);
|
||||
|
||||
|
@ -90,6 +90,12 @@ SECTIONS
|
||||
_ebss = .;
|
||||
} > SRAM
|
||||
|
||||
_end = .; /* End of the .bss segment. */
|
||||
|
||||
/* These symbols are used by the stack check library. */
|
||||
_stack = .;
|
||||
_stack_origin = ORIGIN(SRAM) + LENGTH(SRAM);
|
||||
|
||||
.ccfg :
|
||||
{
|
||||
KEEP(*(.ccfg))
|
||||
|
@ -182,6 +182,9 @@ endif # SMALL
|
||||
|
||||
endif # IAR
|
||||
|
||||
# Define the `_stack` symbol used by the stack check library to be equal to `_end`
|
||||
LDFLAGS += -Wl,--defsym=_stack=_end
|
||||
|
||||
CFLAGS += $(CFLAGSNO)
|
||||
|
||||
PROJECT_OBJECTFILES += ${addprefix $(OBJECTDIR)/,$(CONTIKI_TARGET_MAIN:.c=.o)}
|
||||
|
@ -10,3 +10,7 @@ MEMORY
|
||||
}
|
||||
|
||||
INCLUDE "nrf5x_common.ld"
|
||||
|
||||
/* These symbols are used by the stack check library. */
|
||||
_stack = end;
|
||||
_stack_origin = ORIGIN(RAM) + LENGTH(RAM);
|
||||
|
@ -10,3 +10,7 @@ MEMORY
|
||||
}
|
||||
|
||||
INCLUDE "nrf5x_common.ld"
|
||||
|
||||
/* These symbols are used by the stack check library. */
|
||||
_stack = end;
|
||||
_stack_origin = ORIGIN(RAM) + LENGTH(RAM);
|
||||
|
@ -10,3 +10,7 @@ MEMORY
|
||||
}
|
||||
|
||||
INCLUDE "nrf5x_common.ld"
|
||||
|
||||
/* These symbols are used by the stack check library. */
|
||||
_stack = end;
|
||||
_stack_origin = ORIGIN(RAM) + LENGTH(RAM);
|
||||
|
@ -135,4 +135,6 @@ typedef uint64_t rtimer_clock_t;
|
||||
#define RF_CHANNEL 26
|
||||
#define NETSTACK_RADIO_MAX_PAYLOAD_LEN 125
|
||||
|
||||
#define PLATFORM_CONF_SUPPORTS_STACK_CHECK 0
|
||||
|
||||
#endif /* CONTIKI_CONF_H_ */
|
||||
|
@ -164,6 +164,10 @@ CFLAGS := $(patsubst -I/cygdrive/c/%,-Ic:/%,$(CFLAGS))
|
||||
LDFLAGS := $(patsubst -L/cygdrive/c/%,-Lc:/%,$(LDFLAGS))
|
||||
endif
|
||||
|
||||
# These symbols are used by the stack check library
|
||||
LDFLAGS += -Wl,--defsym=_stack=_stack_low_water_mark
|
||||
LDFLAGS += -Wl,--defsym=_stack_origin=_ram_top
|
||||
|
||||
########################################################################
|
||||
|
||||
MOTELIST = python $(CONTIKI)/tools/jn516x/mote-list.py
|
||||
|
@ -101,5 +101,6 @@ int strcasecmp(const char*, const char*);
|
||||
|
||||
#define PLATFORM_CONF_PROVIDES_MAIN_LOOP 1
|
||||
#define PLATFORM_CONF_MAIN_ACCEPTS_ARGS 1
|
||||
#define PLATFORM_CONF_SUPPORTS_STACK_CHECK 0
|
||||
|
||||
#endif /* CONTIKI_CONF_H_ */
|
||||
|
@ -230,4 +230,12 @@ for SFD timestamping */
|
||||
#define CC2420_SPI_DISABLE() (CC2420_CSN_PORT(OUT) |= BV(CC2420_CSN_PIN))
|
||||
#define CC2420_SPI_IS_ENABLED() ((CC2420_CSN_PORT(OUT) & BV(CC2420_CSN_PIN)) != BV(CC2420_CSN_PIN))
|
||||
|
||||
/* Platform-specific define for the end of the stack region */
|
||||
#define STACK_CONF_ORIGIN ((void *)0x3900)
|
||||
|
||||
/* Disable the stack check library by default: .rom overflow otherwise */
|
||||
#ifndef STACK_CHECK_CONF_ENABLED
|
||||
#define STACK_CHECK_CONF_ENABLED 0
|
||||
#endif
|
||||
|
||||
#endif /* PLATFORM_CONF_H_ */
|
||||
|
5
examples/libs/stack-check/Makefile
Normal file
5
examples/libs/stack-check/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
CONTIKI_PROJECT = example-stack-check
|
||||
all: $(CONTIKI_PROJECT)
|
||||
|
||||
CONTIKI = ../../..
|
||||
include $(CONTIKI)/Makefile.include
|
90
examples/libs/stack-check/example-sky.csc
Normal file
90
examples/libs/stack-check/example-sky.csc
Normal file
@ -0,0 +1,90 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<simconf>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/mrm</project>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/mspsim</project>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/avrora</project>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/serial_socket</project>
|
||||
<simulation>
|
||||
<title>Stack checker example</title>
|
||||
<delaytime>0</delaytime>
|
||||
<randomseed>123456</randomseed>
|
||||
<motedelay_us>1000000</motedelay_us>
|
||||
<radiomedium>
|
||||
org.contikios.cooja.radiomediums.UDGM
|
||||
<transmitting_range>50.0</transmitting_range>
|
||||
<interference_range>50.0</interference_range>
|
||||
<success_ratio_tx>1.0</success_ratio_tx>
|
||||
<success_ratio_rx>1.0</success_ratio_rx>
|
||||
</radiomedium>
|
||||
<events>
|
||||
<logoutput>40000</logoutput>
|
||||
</events>
|
||||
<motetype>
|
||||
org.contikios.cooja.mspmote.SkyMoteType
|
||||
<identifier>node</identifier>
|
||||
<description>RPL Root</description>
|
||||
<source EXPORT="discard">[CONFIG_DIR]/example-stack-check.c</source>
|
||||
<commands EXPORT="discard">make example-stack-check.sky TARGET=sky</commands>
|
||||
<firmware EXPORT="copy">[CONFIG_DIR]/example-stack-check.sky</firmware>
|
||||
<moteinterface>org.contikios.cooja.interfaces.Position</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.interfaces.RimeAddress</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.interfaces.IPAddress</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.interfaces.Mote2MoteRelations</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.interfaces.MoteAttributes</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspClock</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspMoteID</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyButton</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyFlash</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyCoffeeFilesystem</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.mspmote.interfaces.Msp802154Radio</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspSerial</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyLED</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
|
||||
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyTemperature</moteinterface>
|
||||
</motetype>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
org.contikios.cooja.interfaces.Position
|
||||
<x>33.260163187353555</x>
|
||||
<y>30.643217359962595</y>
|
||||
<z>0.0</z>
|
||||
</interface_config>
|
||||
<interface_config>
|
||||
org.contikios.cooja.mspmote.interfaces.MspMoteID
|
||||
<id>1</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>node</motetype_identifier>
|
||||
</mote>
|
||||
</simulation>
|
||||
<plugin>
|
||||
org.contikios.cooja.plugins.SimControl
|
||||
<width>259</width>
|
||||
<z>4</z>
|
||||
<height>179</height>
|
||||
<location_x>0</location_x>
|
||||
<location_y>0</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
org.contikios.cooja.plugins.Visualizer
|
||||
<plugin_config>
|
||||
<skin>org.contikios.cooja.plugins.skins.IDVisualizerSkin</skin>
|
||||
<skin>org.contikios.cooja.plugins.skins.UDGMVisualizerSkin</skin>
|
||||
<skin>org.contikios.cooja.plugins.skins.MoteTypeVisualizerSkin</skin>
|
||||
<viewport>2.898638306051894 0.0 0.0 2.898638306051894 -68.40918308040007 -27.82360366026197</viewport>
|
||||
</plugin_config>
|
||||
<width>258</width>
|
||||
<z>2</z>
|
||||
<height>209</height>
|
||||
<location_x>0</location_x>
|
||||
<location_y>178</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
org.contikios.cooja.plugins.LogListener
|
||||
<width>1024</width>
|
||||
<z>3</z>
|
||||
<height>311</height>
|
||||
<location_x>0</location_x>
|
||||
<location_y>385</location_y>
|
||||
</plugin>
|
||||
</simconf>
|
88
examples/libs/stack-check/example-stack-check.c
Normal file
88
examples/libs/stack-check/example-stack-check.c
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 2017, University of Bristol - http://www.bris.ac.uk/
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* Test of Contiki system's stack checker functionality
|
||||
* \author
|
||||
* Atis Elsts <atis.elsts@bristol.ac.uk>
|
||||
*/
|
||||
|
||||
#include "contiki.h"
|
||||
#include "sys/stack-check.h"
|
||||
#include "random.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <alloca.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS(example_process, "Stack check example");
|
||||
AUTOSTART_PROCESSES(&example_process);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
nested_function(void)
|
||||
{
|
||||
printf("stack usage: %u permitted: %u\n",
|
||||
stack_check_get_usage(), stack_check_get_reserved_size());
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
test_function(void)
|
||||
{
|
||||
void *p;
|
||||
uint16_t s;
|
||||
|
||||
/* allocate and fill some random bytes */
|
||||
s = random_rand() % 1000;
|
||||
printf("allocating %u bytes on the stack\n", s);
|
||||
p = alloca(s);
|
||||
memset(p, 0, s);
|
||||
|
||||
/* call the nested function to print stack usage */
|
||||
nested_function();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(example_process, ev, data)
|
||||
{
|
||||
static struct etimer et;
|
||||
|
||||
PROCESS_BEGIN();
|
||||
|
||||
while(1) {
|
||||
etimer_set(&et, CLOCK_SECOND * 2);
|
||||
|
||||
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
|
||||
|
||||
test_function();
|
||||
}
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
44
examples/libs/stack-check/project-conf.h
Normal file
44
examples/libs/stack-check/project-conf.h
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2017, University of Bristol - http://www.bris.ac.uk/
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* Project config file
|
||||
* \author
|
||||
* Atis Elsts <atis.elsts@bristol.ac.uk>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __PROJECT_CONF_H__
|
||||
#define __PROJECT_CONF_H__
|
||||
|
||||
#define STACK_CHECK_CONF_ENABLED 1
|
||||
|
||||
#endif /* __PROJECT_CONF_H__ */
|
@ -44,6 +44,7 @@
|
||||
#include "contiki-net.h"
|
||||
#include "sys/platform.h"
|
||||
#include "sys/energest.h"
|
||||
#include "sys/stack-check.h"
|
||||
#include "dev/watchdog.h"
|
||||
|
||||
#include "services/orchestra/orchestra.h"
|
||||
@ -77,6 +78,10 @@ main(void)
|
||||
|
||||
energest_init();
|
||||
|
||||
#if STACK_CHECK_ENABLED
|
||||
stack_check_init();
|
||||
#endif
|
||||
|
||||
platform_init_stage_two();
|
||||
|
||||
LOG_INFO("Starting " CONTIKI_VERSION_STRING "\n");
|
||||
|
162
os/sys/stack-check.c
Normal file
162
os/sys/stack-check.c
Normal file
@ -0,0 +1,162 @@
|
||||
/*
|
||||
* Copyright (c) 2017, University of Bristol - http://www.bris.ac.uk/
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* \addtogroup stack
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* Implementation of the stack checker library.
|
||||
* \author
|
||||
* Atis Elsts <atis.elsts@bristol.ac.uk>
|
||||
*/
|
||||
|
||||
#include "contiki.h"
|
||||
#include "sys/stack-check.h"
|
||||
#include "dev/watchdog.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "sys/log.h"
|
||||
#define LOG_MODULE "Stack"
|
||||
#define LOG_LEVEL LOG_LEVEL_MAIN
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/* linker will provide a symbol for the end of the .bss segment */
|
||||
extern uint8_t _stack;
|
||||
|
||||
#if STACK_CHECK_PERIODIC_CHECKS
|
||||
PROCESS(stack_check_process, "Stack check");
|
||||
#endif
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/* The symbol with which the stack memory is initially filled */
|
||||
#define STACK_FILL 0xcd
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#ifdef STACK_ORIGIN
|
||||
/* use the #defined value */
|
||||
#define GET_STACK_ORIGIN() STACK_ORIGIN
|
||||
#else
|
||||
/* use the value provided by the linker script */
|
||||
extern int _stack_origin;
|
||||
#define GET_STACK_ORIGIN() (&_stack_origin)
|
||||
#endif
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
stack_check_init(void)
|
||||
{
|
||||
uint8_t *p;
|
||||
|
||||
/* Make this static to avoid destroying it in the while loop */
|
||||
static void *stack_top;
|
||||
/* Use address of this local variable as a boundary */
|
||||
stack_top = &p;
|
||||
|
||||
/* Note: this is expected to be called before the WDT is started! */
|
||||
p = &_stack;
|
||||
while(p < (uint8_t *)stack_top) {
|
||||
*p++ = STACK_FILL;
|
||||
}
|
||||
|
||||
#if STACK_CHECK_PERIODIC_CHECKS
|
||||
/* Start the periodic checker process */
|
||||
process_start(&stack_check_process, NULL);
|
||||
#endif
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint16_t
|
||||
stack_check_get_usage(void)
|
||||
{
|
||||
uint8_t *p = &_stack;
|
||||
|
||||
/* Make sure WDT is not triggered */
|
||||
watchdog_periodic();
|
||||
|
||||
/* Skip the bytes used after heap; it's 1 byte by default for _stack,
|
||||
* more than that means dynamic memory allocation is used somewhere.
|
||||
*/
|
||||
while(*p != STACK_FILL && p < (uint8_t *)GET_STACK_ORIGIN()) {
|
||||
p++;
|
||||
}
|
||||
|
||||
/* Skip the region of the memory reserved for the stack not used yet by the program */
|
||||
while(*p == STACK_FILL && p < (uint8_t *)GET_STACK_ORIGIN()) {
|
||||
p++;
|
||||
}
|
||||
|
||||
/* Make sure WDT is not triggered */
|
||||
watchdog_periodic();
|
||||
|
||||
if(p >= (uint8_t*)GET_STACK_ORIGIN()) {
|
||||
/* This means the stack is screwed. */
|
||||
return 0xffff;
|
||||
}
|
||||
|
||||
return (uint8_t *)GET_STACK_ORIGIN() - p;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint16_t
|
||||
stack_check_get_reserved_size(void)
|
||||
{
|
||||
return (uint8_t *)GET_STACK_ORIGIN() - &_stack;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#if STACK_CHECK_PERIODIC_CHECKS
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(stack_check_process, ev, data)
|
||||
{
|
||||
static struct etimer et;
|
||||
|
||||
PROCESS_BEGIN();
|
||||
|
||||
etimer_set(&et, STACK_CHECK_PERIOD);
|
||||
|
||||
while(1) {
|
||||
uint16_t actual, allowed;
|
||||
|
||||
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
|
||||
|
||||
actual = stack_check_get_usage();
|
||||
allowed = stack_check_get_reserved_size();
|
||||
if(actual > allowed) {
|
||||
LOG_ERR("Check failed: %u vs. %u\n", actual, allowed);
|
||||
} else {
|
||||
LOG_DBG("Check ok: %u vs. %u\n", actual, allowed);
|
||||
}
|
||||
|
||||
etimer_reset(&et);
|
||||
}
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#endif /* STACK_CHECK_PERIODIC_CHECKS */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @} */
|
130
os/sys/stack-check.h
Normal file
130
os/sys/stack-check.h
Normal file
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (c) 2017, University of Bristol - http://www.bris.ac.uk/
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* Stack checker library header file.
|
||||
* \author
|
||||
* Atis Elsts <atis.elsts@bristol.ac.uk>
|
||||
*/
|
||||
|
||||
/** \addtogroup sys
|
||||
* @{ */
|
||||
|
||||
/**
|
||||
* \defgroup stack Stack checker library
|
||||
*
|
||||
* Basic support for stack guards and stack overflow detection.
|
||||
* On startup, fills the area between the stack and the heap with a known pattern.
|
||||
* During execution, the fill can be checked in order to find out
|
||||
* the extent to which the stack has been used.
|
||||
*
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
#ifndef STACK_CHECK_H_
|
||||
#define STACK_CHECK_H_
|
||||
|
||||
#include "contiki-conf.h"
|
||||
|
||||
/* Determine whether stack checking is supported depending on the plaform. */
|
||||
#ifdef PLATFORM_CONF_SUPPORTS_STACK_CHECK
|
||||
#if !PLATFORM_CONF_SUPPORTS_STACK_CHECK
|
||||
/* Stack checker cannot be enabled, since the platform does not support it */
|
||||
#undef STACK_CHECK_CONF_ENABLED
|
||||
#define STACK_CHECK_CONF_ENABLED 0
|
||||
#endif /* !PLATFORM_CONF_SUPPORTS_STACK_CHECK */
|
||||
#endif /* ifdef PLATFORM_CONF_SUPPORTS_STACK_CHECK */
|
||||
|
||||
/* If this is disabled, the functions are no-ops */
|
||||
#ifdef STACK_CHECK_CONF_ENABLED
|
||||
#define STACK_CHECK_ENABLED STACK_CHECK_CONF_ENABLED
|
||||
#else
|
||||
#define STACK_CHECK_ENABLED 1 /* Enable by default */
|
||||
#endif
|
||||
|
||||
/* Perform periodic stack integrity checks? */
|
||||
#ifdef STACK_CHECK_CONF_PERIODIC_CHECKS
|
||||
#define STACK_CHECK_PERIODIC_CHECKS STACK_CHECK_CONF_PERIODIC_CHECKS
|
||||
#else
|
||||
#define STACK_CHECK_PERIODIC_CHECKS 1 /* Enable by default */
|
||||
#endif
|
||||
|
||||
/* How often to do the periodic integrity checks, if enabled? */
|
||||
#ifdef STACK_CHECK_CONF_PERIOD
|
||||
#define STACK_CHECK_PERIOD STACK_CHECK_CONF_PERIOD
|
||||
#else
|
||||
#define STACK_CHECK_PERIOD (10 * CLOCK_SECOND)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Initialize the stack area with a known pattern
|
||||
*
|
||||
* This function initializes the memory between the stack and heap
|
||||
* areas. The function should be called by the system
|
||||
* during boot-up.
|
||||
*/
|
||||
void stack_check_init(void);
|
||||
|
||||
/**
|
||||
* \brief Calculate the maximal stack usage so far.
|
||||
*
|
||||
* This function relies on the assumption that the stack memory
|
||||
* that has been reserved by functions and local variables
|
||||
* is actually overwritten with new contents. If the stack is
|
||||
* just reserved, but not used, the function will fail to detect
|
||||
* that usage.
|
||||
* In addition, this function can warn if the stack memory range
|
||||
* has been completely used, but it cannot detect
|
||||
* and warn if stack overflow has already taken place.
|
||||
*/
|
||||
uint16_t stack_check_get_usage(void);
|
||||
|
||||
/**
|
||||
* \brief Calculate the maximal permitted stack usage.
|
||||
*
|
||||
* This function returns the number of bytes between the origin
|
||||
* of the stack and the end of heap.
|
||||
*/
|
||||
uint16_t stack_check_get_reserved_size(void);
|
||||
|
||||
/**
|
||||
* \brief The origin point from which the stack grows (an optional #define)
|
||||
*
|
||||
*/
|
||||
#ifdef STACK_CONF_ORIGIN
|
||||
#define STACK_ORIGIN STACK_CONF_ORIGIN
|
||||
#endif
|
||||
|
||||
#endif /* STACK_CHECK_H_ */
|
||||
|
||||
/** @} */
|
||||
/** @} */
|
Loading…
Reference in New Issue
Block a user