From 2b5e1bc8b22c905822ec4ac345d01ede0de3e52a Mon Sep 17 00:00:00 2001 From: wangmingrong1 Date: Wed, 22 Jan 2025 19:20:55 +0800 Subject: [PATCH] Docu: Stack overflow check Signed-off-by: wangmingrong1 --- Documentation/guides/index.rst | 1 + Documentation/guides/stackcheck.rst | 65 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 Documentation/guides/stackcheck.rst diff --git a/Documentation/guides/index.rst b/Documentation/guides/index.rst index 7a4f691b72716..e7b3b494b9df9 100644 --- a/Documentation/guides/index.rst +++ b/Documentation/guides/index.rst @@ -31,6 +31,7 @@ Guides automounter.rst stm32nullpointer.rst stm32ccm.rst + stackcheck.rst stackrecord.rst etcromfs.rst thread_local_storage.rst diff --git a/Documentation/guides/stackcheck.rst b/Documentation/guides/stackcheck.rst new file mode 100644 index 0000000000000..c42ef7472a9f4 --- /dev/null +++ b/Documentation/guides/stackcheck.rst @@ -0,0 +1,65 @@ +==================================== +Stack Overflow Check +==================================== + +Overview +-------- + +Currently NuttX supports three types of stack overflow detection: + 1. Stack Overflow Software Check + 2. Stack Overflow Hardware Check + 3. Stack Canary Check + +The software stack detection includes two implementation ideas: + 1. Implemented by coloring the stack memory + 2. Implemented by comparing the sp and sl registers + +Support +------- + +Software and hardware stack overflow detection implementation, +currently only implemented on ARM Cortex-M (32-bit) series chips +Stack Canary Check is available on all platforms + +Stack Overflow Software Check +----------------------------- + +1. Memory Coloring Implementation Principle + 1. Before using the stack, Thread will refresh the stack area to 0xdeadbeef + 2. When Thread is running, it will overwrite 0xdeadbeef + 3. up_check_tcbstack() detects 0xdeadbeef to get the stack peak value + + Usage: + Enable CONFIG_STACK_COLORATION + +2. Compare sp and sl + When compiling the program, keep r10 and use r10 as stackbase:: + ''' + ARCHOPTIMIZATION += -finstrument-functions -ffixed-r10 + + Each function will automatically add the following when entering and exiting: + __cyg_profile_func_enter + __cyg_profile_func_exit + + Usage: + Enable CONFIG_ARMV8M_STACKCHECK or CONFIG_ARMV7M_STACKCHECK + +Stack Overflow Hardware Check +----------------------------- + +1. Set MSPLIM PSPLIM when context switching +2. Each time sp is operated, the hardware automatically compares sp and PSPLIM. If sp is lower than PSPLIM, crash + +Usage: + Enable CONFIG_ARMV8M_STACKCHECK_HARDWARE + +Stack Canary Check +----------------------------- + +1. Add a canary value to the stack +2. When the thread is running, the canary value is overwritten +3. When the thread is running, the canary value is compared with the original value +4. If the value is different, it means that the stack is overflowed + +Usage: + Enable CONFIG_STACK_CANARIES