Countless times of suffering from Stack Overflow, as the system becomes more complex, fault points become increasingly difficult to find!
The main overflow situations are as follows:
1. Generally, the last two parts of RAM are heap and stack. the stack is used up from bottom to top, and the stack is used up from top to bottom. If any one is used up, it will enter the space of the other party.
2. If the stack is used up and enters the heap space, the system will not have any exceptions at this time. That is to say, there is no meaning at the bottom of the stack. Unless the stack and the stack pointer overlap, everyone will be safe, even though the stack uses the stack
3. If the stack is used up and enters the heap space, the system still has no exception at this time, but the stack will modify data for each other. The worst thing is that the address LR stored in the stack will be changed. Once the heap pointer is modified, the returned address will jump to another address space. This is the case in most cases, and most of them jump to the invalid space. You should thank it for skipping to the invalid space so that you can immediately discover the error. Otherwise, the stack will penetrate into each other and no error will be reported, and data will be disordered during system work. It will be time to see if you want to hit your head or jump to the building!
4. When using the Keil microlibrary, malloc will use the heap space. If the heap space is used up, a null pointer will be obtained when malloc is used up, but no error will be reported. However, if C ++'s new is used, an error will be reported at this time!
Because the main thread and interrupt processing exist, the memory may be allocated and released at any time, which may cause problems at any time! Difficult to check the problem!
Therefore, smartos v2.5 adds a memory Stack Overflow detection module.
Statement:
#ifdef DEBUGvoid* operator new(uint size);void* operator new[](uint size);void operator delete(void * p);void operator delete [] (void * p);#endif
Implementation:
Extern uint _ heap_base; extern uint _ heap_limit; void * operator new (uint size) {debug_printf ("New Size: % d", size ); void * P = malloc (size); If (! P) debug_printf ("malloc failed! Size = % d ", size); else {debug_printf (" 0x % 08x ", P); // If the heap contains only 64 bytes, the report fails, the user is required to expand the heap space to avoid unexpected uint end = (uint) & __ heap_limit; If (uint) P + size + 0x40> = end) debug_printf ("+ % d near heapend = 0x % 08x", size, end);} assert_param (p); Return P;} void * operator new [] (uint size) {debug_printf ("New Size []: % d", size); void * P = malloc (size); If (! P) debug_printf ("malloc failed! Size = % d ", size); else {debug_printf (" 0x % 08x ", P); // If the heap contains only 64 bytes, the report fails, the user is required to expand the heap space to avoid unexpected uint end = (uint) & __ heap_limit; If (uint) P + size + 0x40> = end) debug_printf ("+ % d near heapend = 0x % 08x", size, end);} assert_param (p); Return P;} void operator Delete (void * P) {debug_printf ("delete 0x % 08x", P); If (p) Free (p);} void operator Delete [] (void * P) {debug_printf ("Delete [] 0x % 08x", P); If (p) Free (p );}
Implemented by reloading new/delete with the 64-byte advance prediction function! Warning before the heap is used up!
End.
Reprinted stone brother
Research on memory Stack Overflow detection on stm32/gd32