DefaultPreInit gets random address in debug mode [Stack pointer initialization issue] #102
Description
Hi all,
I have stumbled on a strange issue that took some work to find as it does not happen often.
My issue was that when compiling in debug mode the address provided by __pre_init
becomes different addresses, depending on when the system was reset. And this comes from the stack pointer not being initialized yet, and a corrupt stack pointer passing through.
This can occur if the system's stack pointer has been corrupted and a soft-reset has been requested, then the erroneous value of the stack pointer sticks until after reset.
I wrongly assumed that the system always read the stack pointer from the vector table and wrote it, hence I recommend that we add an assembler instruction to set MSP and not allow this kind of undefined behavior.
Comments @rust-embedded/cortex-m ?
Example of corrupt stack pointer after reset:
r0 0x8000251 134218321
r1 0xf00000 15728640
r2 0x20000000 536870912
r3 0x0 0
r4 0x0 0
r5 0x0 0
r6 0x0 0
r7 0x0 0
r8 0x0 0
r9 0x0 0
r10 0x0 0
r11 0x0 0
r12 0x0 0
sp 0xfffffe90 0xfffffe90
lr 0x8000315 0x8000315 <core::mem::uninitialized+12>
pc 0x80001c4 0x80001c4 <Reset+12>
xpsr 0x61000003 1627389955
fpscr 0x0 0
msp 0xfffffe90 0xfffffe90
psp 0x0 0x0
primask 0x0 0
basepri 0x0 0
faultmask 0x0 0
control 0x0 0