Encountering hardfaults in FreeRTOS v10.3.0 on a Microchip PIC32 while using vsnprintf or sprin

Hi guys, I'm encountering hardfaults in FreeRTOS v10.3.0 on a Microchip PIC32 while using vsnprintf or sprintf functions. I suspect stack corruption, as I'm using static memory allocation no malloc with a pre-allocated buffer char _log_buffer[128]. I've tried different printf implementations newlib, newlib nano, tinyprintf without success. I tried troubleshooting with these: ~Verified thread stack alignment. ~Ruled out dynamic allocation issues by mocking malloc/calloc/realloc/free. Are there any recommended techniques to confirm stack corruption specifically related to printf calls in FreeRTOS? With static memory allocation, what are some effective ways to debug the stack pointer behavior during these hardfaults? @Middleware & OS
4 Replies
Marvee Amasi
Marvee Amasi3w ago
Hi @Sterling you should start with basic format strings like "%d" instead of complex ones first of all to isolate formatting issues.
Marvee Amasi
Marvee Amasi3w ago
Add a canary value in the end of your stack and you check it before using printf to check for any potential stuffs overwriting
Marvee Amasi
Marvee Amasi3w ago
See here :
#define STACK_CANARY_VALUE 0xDEADBEEF

char _log_buffer[128];
uint32_t _log_buffer_canary = STACK_CANARY_VALUE;

void my_function(void) {
// your code bla bla bla

// This is where you should check the canary before using printf
if (_log_buffer_canary != STACK_CANARY_VALUE) {
// Stack corruption detected!
// Handle the error prolly stop execution or trigger a watchdog
}

// Now it is safe hopefully to use printf
vsnprintf(_log_buffer, sizeof(_log_buffer), "%d", some_variable);
}
#define STACK_CANARY_VALUE 0xDEADBEEF

char _log_buffer[128];
uint32_t _log_buffer_canary = STACK_CANARY_VALUE;

void my_function(void) {
// your code bla bla bla

// This is where you should check the canary before using printf
if (_log_buffer_canary != STACK_CANARY_VALUE) {
// Stack corruption detected!
// Handle the error prolly stop execution or trigger a watchdog
}

// Now it is safe hopefully to use printf
vsnprintf(_log_buffer, sizeof(_log_buffer), "%d", some_variable);
}
UC GEE
UC GEE3w ago
@Sterling Try out a debugger like MPLAB X to inspect the stack pointer and stack contents during the execution of your code.Then, you can now set breakpoints and inspect variables to identify any issues with stack pointer behavior.You can try these techniques out.
Want results from more Discord servers?
Add your server
More Posts
Connect and stream sensor data from a Raspberry Pi running on Raspbian OS to an AWS IoT Core@Middleware & OS @IoT Cloud Hello guys, how can I connect and stream sensor (QMC5883) data from a RHow can I ensure my CAN ISR completes processing quickly enough to avoid the interrupt WDT panic?@Middleware & OS I'm using an MCP25625 CAN controller with an ESP32-S3 and esp-idf with RTOS. I'm encollect temperature data from a sensor connected to my mcu and store it every minute on SD card@Middleware & OS Hello guys can i implement a simple sensor data collection and storage system usinHow can I modify the state machine logic?I'm working on this RTOS-based application for a vending machine controller with `C` and a *state maCan someone decode what data in hex is being sent in the following image?hello everyone can someone decode what data in hex is being sent in the following image?What is the role of a bootloader in embedded systems programming?A bootloader is a crucial piece of software in embedded systems, responsible for preparing the devictesting honeypot for Matter over Thread and WifiHello, For my masters thesis, We've made a honeypot with a Matter over Thread and WiFi network usingApple Intelligence: Apple’s New Generative AI UnveiledAt WWDC 2024, Apple introduced Apple Intelligence, a generative AI integrated across its ecosystem fHow can I set up a CoAP server using the libcoap library on a Raspberry Pi using c language?@Middleware & OS Hello guys how can I set up a CoAP server using the libcoap library on a RaspberryDebugging Variable Display Issues on ARM M7 with FreeRTOS using Lauterbach Trace32Hey guys @MCU, MPU & Firmware i am currently developing a test device using an ARM M7 (NXP S32KXXX)