Scheduler randomly crashing on riscv

Hi, i’m working on a freertos port for an in house developed riscv architecture. The code executes correctly with everything working (timer, exceptions, interrupt handling). After some time the exception_handler puts a bad address in mepc and the cpu crashes at the subsequent mret. The pxCurretTCB it is reading from is valid, and it has read and written from it succesfully before in the code, and the task that TCBs describes is schedulable.
The code executes correctly in QEMU riscv32 emulator.

Do you have any hint?

Which exception executes? I presume it’s caused by executing code from the invalid address.

Yes the exceptions is an “illegal instruction” caused by executing a bad address. The thing i’m struggling to understand is how it is possible for a bad address to end un in the TCB, while the same code is behaving correctly on qemu

Seems like some memory corruption and seems like PC value got corrupted somehow. One possible reason is stack overflow. Do you have stack overflow check enabled? As a quick check, can you try increasing the stack size for tasks? If that does not help, the next step is to remove all the functionalities from your application and add them back one by one to pin point the problematic part.