Search code examples
ruststm32freertoscortex-m

Rust interop with FreeRTOS causes INVSTATE HardFault


I am working on an STM32F4 project with code generated by STM32CubeMX. In order to add some Rust to my project, I have ported the Makefile to CMake and am then using Corrosion to build and link a Rust project as a static library. This has worked surprisingly well up until the point I tried adding FreeRTOS to the project. For some reason, trying to enter a delay after doing memory allocation on the Rust side causes the chip to HardFault. Looking at the status registers, it appears this is being caused by a INVSTATE UsageFault. Unfortunately, the LR pushed to the stack doesn't seem to point to an actual location at this point, so I have no idea what instruction is causing the problem.

The description for this type of error at https://interrupt.memfault.com/blog/cortex-m-hardfault-debug claims "If this rule is violated, a INVSTATE exception will be generated. When writing C code, the compiler will take care of this automatically, but this is a common bug which can arise when hand-writing assembly.". Could it possibly be that something is getting messed up between the C and Rust compilers?

P.S. Rust is using a custom global allocator powered by pvPortMalloc and vPortFree and this seems to work just fine.


Solution

  • It turns out the problem had nothing to do with the interop between the languages. It was simply that the stack size for the FreeRTOS thread was not large enough.