From f18d4757c6345344df9c9112f2a37f13d8ad7365 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Sat, 12 Jun 2021 12:08:53 +0200 Subject: [PATCH] Introduce STACK_GUARD_MARGIN in all variants Create a gap between the BSS and the stack guard. Set this gap (STACK_GUARD_MARGIN) to 32 bytes in all variants. The gap serves two purposes: - Detect a stack overflow earlier (falsely triggering in overtight situations is OK!), so that we can hopefully avoid smashing the heap and have a clean view during the dump. - Reserve spack space itself for the stack dumping machinery, which is going to grow the stack even further. Remove get_stack_guard_test_value() which was unused. --- Firmware/SdFatUtil.cpp | 12 ++---------- Firmware/SdFatUtil.h | 3 +-- Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h | 1 + Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h | 1 + Firmware/variants/1_75mm_MK25-RAMBo10a-E3Dv6full.h | 1 + Firmware/variants/1_75mm_MK25-RAMBo13a-E3Dv6full.h | 1 + Firmware/variants/1_75mm_MK25S-RAMBo10a-E3Dv6full.h | 1 + Firmware/variants/1_75mm_MK25S-RAMBo13a-E3Dv6full.h | 1 + Firmware/variants/1_75mm_MK3-EINSy10a-E3Dv6full.h | 1 + Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h | 1 + 10 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Firmware/SdFatUtil.cpp b/Firmware/SdFatUtil.cpp index 51da4ee2..50206ab9 100644 --- a/Firmware/SdFatUtil.cpp +++ b/Firmware/SdFatUtil.cpp @@ -48,24 +48,16 @@ void SdFatUtil::set_stack_guard() { uint32_t *stack_guard; - stack_guard = (uint32_t*)&__bss_end; + stack_guard = (uint32_t*)(&__bss_end + STACK_GUARD_MARGIN); *stack_guard = STACK_GUARD_TEST_VALUE; } bool SdFatUtil::test_stack_integrity() { - uint32_t* stack_guard = (uint32_t*)&__bss_end; + uint32_t* stack_guard = (uint32_t*)(&__bss_end + STACK_GUARD_MARGIN); return (*stack_guard == STACK_GUARD_TEST_VALUE); } -uint32_t SdFatUtil::get_stack_guard_test_value() -{ - uint32_t* stack_guard; - uint32_t output; - stack_guard = (uint32_t*)&__bss_end; - output = *stack_guard; - return(output); -} //------------------------------------------------------------------------------ /** %Print a string in flash memory. * diff --git a/Firmware/SdFatUtil.h b/Firmware/SdFatUtil.h index c42b74b1..2a70d98a 100644 --- a/Firmware/SdFatUtil.h +++ b/Firmware/SdFatUtil.h @@ -41,11 +41,10 @@ namespace SdFatUtil { void SerialPrintln_P(PGM_P str); void set_stack_guard(); bool test_stack_integrity(); - uint32_t get_stack_guard_test_value(); } using namespace SdFatUtil; // NOLINT #endif // #define SdFatUtil_h -#endif \ No newline at end of file +#endif diff --git a/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h index 4c20082e..7ccd9151 100644 --- a/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h @@ -418,6 +418,7 @@ THERMISTORS SETTINGS #endif #define STACK_GUARD_TEST_VALUE 0xA2A2 +#define STACK_GUARD_MARGIN 32 #define MAX_BED_TEMP_CALIBRATION 50 #define MAX_HOTEND_TEMP_CALIBRATION 50 diff --git a/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h b/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h index 35313a41..4686f097 100644 --- a/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h @@ -407,6 +407,7 @@ THERMISTORS SETTINGS #endif #define STACK_GUARD_TEST_VALUE 0xA2A2 +#define STACK_GUARD_MARGIN 32 #define MAX_BED_TEMP_CALIBRATION 50 #define MAX_HOTEND_TEMP_CALIBRATION 50 diff --git a/Firmware/variants/1_75mm_MK25-RAMBo10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK25-RAMBo10a-E3Dv6full.h index b05475ab..fa7f2690 100644 --- a/Firmware/variants/1_75mm_MK25-RAMBo10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK25-RAMBo10a-E3Dv6full.h @@ -469,6 +469,7 @@ #define TEMP_SENSOR_PINDA 1 #define STACK_GUARD_TEST_VALUE 0xA2A2 +#define STACK_GUARD_MARGIN 32 #define MAX_BED_TEMP_CALIBRATION 50 #define MAX_HOTEND_TEMP_CALIBRATION 50 diff --git a/Firmware/variants/1_75mm_MK25-RAMBo13a-E3Dv6full.h b/Firmware/variants/1_75mm_MK25-RAMBo13a-E3Dv6full.h index 0750bc5c..10aa14f9 100644 --- a/Firmware/variants/1_75mm_MK25-RAMBo13a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK25-RAMBo13a-E3Dv6full.h @@ -470,6 +470,7 @@ #define TEMP_SENSOR_PINDA 1 #define STACK_GUARD_TEST_VALUE 0xA2A2 +#define STACK_GUARD_MARGIN 32 #define MAX_BED_TEMP_CALIBRATION 50 #define MAX_HOTEND_TEMP_CALIBRATION 50 diff --git a/Firmware/variants/1_75mm_MK25S-RAMBo10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK25S-RAMBo10a-E3Dv6full.h index d8a4158b..4540fd72 100644 --- a/Firmware/variants/1_75mm_MK25S-RAMBo10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK25S-RAMBo10a-E3Dv6full.h @@ -469,6 +469,7 @@ #define TEMP_SENSOR_PINDA 1 #define STACK_GUARD_TEST_VALUE 0xA2A2 +#define STACK_GUARD_MARGIN 32 #define MAX_BED_TEMP_CALIBRATION 50 #define MAX_HOTEND_TEMP_CALIBRATION 50 diff --git a/Firmware/variants/1_75mm_MK25S-RAMBo13a-E3Dv6full.h b/Firmware/variants/1_75mm_MK25S-RAMBo13a-E3Dv6full.h index 9a25dce2..53773e97 100644 --- a/Firmware/variants/1_75mm_MK25S-RAMBo13a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK25S-RAMBo13a-E3Dv6full.h @@ -470,6 +470,7 @@ #define TEMP_SENSOR_PINDA 1 #define STACK_GUARD_TEST_VALUE 0xA2A2 +#define STACK_GUARD_MARGIN 32 #define MAX_BED_TEMP_CALIBRATION 50 #define MAX_HOTEND_TEMP_CALIBRATION 50 diff --git a/Firmware/variants/1_75mm_MK3-EINSy10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK3-EINSy10a-E3Dv6full.h index b03fe726..f6b17889 100644 --- a/Firmware/variants/1_75mm_MK3-EINSy10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK3-EINSy10a-E3Dv6full.h @@ -598,6 +598,7 @@ #define TEMP_SENSOR_AMBIENT 2000 #define STACK_GUARD_TEST_VALUE 0xA2A2 +#define STACK_GUARD_MARGIN 32 #define MAX_BED_TEMP_CALIBRATION 50 #define MAX_HOTEND_TEMP_CALIBRATION 50 diff --git a/Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h index 934c0913..92698b47 100644 --- a/Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h @@ -602,6 +602,7 @@ #define TEMP_SENSOR_AMBIENT 2000 #define STACK_GUARD_TEST_VALUE 0xA2A2 +#define STACK_GUARD_MARGIN 32 #define MAX_BED_TEMP_CALIBRATION 50 #define MAX_HOTEND_TEMP_CALIBRATION 50