Skip to content

Commit 4382db1

Browse files
authored
Merge pull request #2402 from c1728p9/main_thread_stack_checking_alt_impl
Main thread stack checking alt impl
2 parents 52658e5 + d9ac33d commit 4382db1

File tree

8 files changed

+372
-113
lines changed

8 files changed

+372
-113
lines changed

hal/common/retarget.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "mbed_interface.h"
2424
#include "SingletonPtr.h"
2525
#include "PlatformMutex.h"
26+
#include "mbed_error.h"
27+
#include <stdlib.h>
2628
#if DEVICE_STDIO_MESSAGES
2729
#include <stdio.h>
2830
#endif
@@ -68,6 +70,10 @@ extern const char __stdout_name[] = "/stdout";
6870
extern const char __stderr_name[] = "/stderr";
6971
#endif
7072

73+
// Heap limits - only used if set
74+
unsigned char *mbed_heap_start = 0;
75+
uint32_t mbed_heap_size = 0;
76+
7177
/* newlib has the filehandle field in the FILE struct as a short, so
7278
* we can't just return a Filehandle* from _open and instead have to
7379
* put it in a filehandles array and return the index into that array
@@ -596,6 +602,12 @@ extern "C" caddr_t _sbrk(int incr) {
596602
return (caddr_t)-1;
597603
}
598604

605+
// Additional heap checking if set
606+
if (mbed_heap_size && (new_heap >= mbed_heap_start + mbed_heap_size)) {
607+
errno = ENOMEM;
608+
return (caddr_t)-1;
609+
}
610+
599611
heap = new_heap;
600612
return (caddr_t) prev_heap;
601613
}
@@ -714,3 +726,34 @@ extern "C" void __env_unlock( struct _reent *_r )
714726
#endif
715727

716728
} // namespace mbed
729+
730+
void *operator new(std::size_t count)
731+
{
732+
void *buffer = malloc(count);
733+
if (NULL == buffer) {
734+
error("Operator new out of memory\r\n");
735+
}
736+
return buffer;
737+
}
738+
739+
void *operator new[](std::size_t count)
740+
{
741+
void *buffer = malloc(count);
742+
if (NULL == buffer) {
743+
error("Operator new[] out of memory\r\n");
744+
}
745+
return buffer;
746+
}
747+
748+
void operator delete(void *ptr)
749+
{
750+
if (ptr != NULL) {
751+
free(ptr);
752+
}
753+
}
754+
void operator delete[](void *ptr)
755+
{
756+
if (ptr != NULL) {
757+
free(ptr);
758+
}
759+
}

hal/targets/cmsis/TARGET_NORDIC/TARGET_NRF5/TARGET_MCU_NRF52832/TOOLCHAIN_ARM_STD/startup_nrf52832.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
; POSSIBILITY OF SUCH DAMAGE.
2626
; ---------------------------------------------------------------------------*/
2727

28-
__initial_sp EQU 0x20008000
28+
__initial_sp EQU 0x20010000
2929

3030
PRESERVE8
3131
THUMB

hal/targets/cmsis/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/TOOLCHAIN_IAR/NUC472_442.icf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ define symbol __ICFEDIT_region_IRAM_end__ = 0x20010000;
1111
define symbol __ICFEDIT_region_XRAM_start__ = 0x60000000;
1212
define symbol __ICFEDIT_region_XRAM_end__ = 0x60100000;
1313
/*-Sizes-*/
14-
define symbol __ICFEDIT_size_cstack__ = 0x3000;
14+
define symbol __ICFEDIT_size_cstack__ = 0x2000;
1515
define symbol __ICFEDIT_size_heap__ = 0xC0000;
1616
/**** End of ICF editor section. ###ICF###*/
1717

rtos/rtx/TARGET_CORTEX_M/HAL_CM.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,6 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {
9090
/* Task entry point. */
9191
p_TCB->ptask = task_body;
9292

93-
94-
#ifdef __MBED_CMSIS_RTOS_CM
95-
/* Set a magic word for checking of stack overflow.
96-
For the main thread (ID: MAIN_THREAD_ID) the stack is in a memory area shared with the
97-
heap, therefore the last word of the stack is a moving target.
98-
We want to do stack/heap collision detection instead.
99-
Similar applies to stack filling for the magic pattern.
100-
*/
101-
if (p_TCB->task_id != MAIN_THREAD_ID) {
102-
p_TCB->stack[0] = MAGIC_WORD;
103-
104-
/* Initialize stack with magic pattern. */
105-
if (os_stackinfo & 0x10000000U) {
106-
if (size > (16U+1U)) {
107-
for (i = ((size - 16U)/2U) - 1U; i; i--) {
108-
stk -= 2U;
109-
stk[1] = MAGIC_PATTERN;
110-
stk[0] = MAGIC_PATTERN;
111-
}
112-
if (--stk > p_TCB->stack) {
113-
*stk = MAGIC_PATTERN;
114-
}
115-
}
116-
}
117-
}
118-
#else
11993
/* Initialize stack with magic pattern. */
12094
if (os_stackinfo & 0x10000000U) {
12195
if (size > (16U+1U)) {
@@ -132,7 +106,6 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {
132106

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

138111

0 commit comments

Comments
 (0)