Skip to content

Main thread stack checking alt impl #2402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions hal/common/retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "mbed_interface.h"
#include "SingletonPtr.h"
#include "PlatformMutex.h"
#include "mbed_error.h"
#include <stdlib.h>
#if DEVICE_STDIO_MESSAGES
#include <stdio.h>
#endif
Expand Down Expand Up @@ -68,6 +70,10 @@ extern const char __stdout_name[] = "/stdout";
extern const char __stderr_name[] = "/stderr";
#endif

// Heap limits - only used if set
unsigned char *mbed_heap_start = 0;
uint32_t mbed_heap_size = 0;

/* newlib has the filehandle field in the FILE struct as a short, so
* we can't just return a Filehandle* from _open and instead have to
* put it in a filehandles array and return the index into that array
Expand Down Expand Up @@ -596,6 +602,12 @@ extern "C" caddr_t _sbrk(int incr) {
return (caddr_t)-1;
}

// Additional heap checking if set
if (mbed_heap_size && (new_heap >= mbed_heap_start + mbed_heap_size)) {
errno = ENOMEM;
return (caddr_t)-1;
}

heap = new_heap;
return (caddr_t) prev_heap;
}
Expand Down Expand Up @@ -714,3 +726,34 @@ extern "C" void __env_unlock( struct _reent *_r )
#endif

} // namespace mbed

void *operator new(std::size_t count)
{
void *buffer = malloc(count);
if (NULL == buffer) {
error("Operator new out of memory\r\n");
}
return buffer;
}

void *operator new[](std::size_t count)
{
void *buffer = malloc(count);
if (NULL == buffer) {
error("Operator new[] out of memory\r\n");
}
return buffer;
}

void operator delete(void *ptr)
{
if (ptr != NULL) {
free(ptr);
}
}
void operator delete[](void *ptr)
{
if (ptr != NULL) {
free(ptr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
; POSSIBILITY OF SUCH DAMAGE.
; ---------------------------------------------------------------------------*/

__initial_sp EQU 0x20008000
__initial_sp EQU 0x20010000

PRESERVE8
THUMB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ define symbol __ICFEDIT_region_IRAM_end__ = 0x20010000;
define symbol __ICFEDIT_region_XRAM_start__ = 0x60000000;
define symbol __ICFEDIT_region_XRAM_end__ = 0x60100000;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x3000;
define symbol __ICFEDIT_size_cstack__ = 0x2000;
define symbol __ICFEDIT_size_heap__ = 0xC0000;
/**** End of ICF editor section. ###ICF###*/

Expand Down
27 changes: 0 additions & 27 deletions rtos/rtx/TARGET_CORTEX_M/HAL_CM.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,6 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {
/* Task entry point. */
p_TCB->ptask = task_body;


#ifdef __MBED_CMSIS_RTOS_CM
/* Set a magic word for checking of stack overflow.
For the main thread (ID: MAIN_THREAD_ID) the stack is in a memory area shared with the
heap, therefore the last word of the stack is a moving target.
We want to do stack/heap collision detection instead.
Similar applies to stack filling for the magic pattern.
*/
if (p_TCB->task_id != MAIN_THREAD_ID) {
p_TCB->stack[0] = MAGIC_WORD;

/* Initialize stack with magic pattern. */
if (os_stackinfo & 0x10000000U) {
if (size > (16U+1U)) {
for (i = ((size - 16U)/2U) - 1U; i; i--) {
stk -= 2U;
stk[1] = MAGIC_PATTERN;
stk[0] = MAGIC_PATTERN;
}
if (--stk > p_TCB->stack) {
*stk = MAGIC_PATTERN;
}
}
}
}
#else
/* Initialize stack with magic pattern. */
if (os_stackinfo & 0x10000000U) {
if (size > (16U+1U)) {
Expand All @@ -132,7 +106,6 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {

/* Set a magic word for checking of stack overflow. */
p_TCB->stack[0] = MAGIC_WORD;
#endif
}


Expand Down
Loading