0

Introduction

Lua implements co-routine based on setjmp/longjmp functions. I found some problems during porting lua to these RTOS environment:

1. Zephyr RTOS: Failed

Heading

MISRA C 2012 Rule 21.4 pointed:

The standard header file <setjmp.h> shall not be used

and when I call setjmp/longjmp functions it suggests that a MPU Fault is occurred although CONFIG_MPU=n is set in prj.conf.

2. Mbed OS: Success

I call lua API in main() and it contains no other thread or task in program. It was OK. But I do not test multi-thread cases.

3. FreeRTOS: Not Tested yet, but likely ok

Opensource project like NodeMCU and Lua RTOS use Lua and FreeRTOS as their basic framework. But I am not sure that they have tested in FreeRTOS multi-task situation.

I found a thread on FreeRTOS Forum about this problem. One of FreeRTOS developer said he did not know. Another people indicated may cause unexpected behaviors in ISR or task context switching due to conflict register change.

My Question

  • If I disabled all interrupts during setjmp/longjmp usage, could I avoid unexpected behaviors in case of conflicting of setjmp/longjmp and ISR/task switching?

  • Why do setjmp/longjmp have failed in Zephyr RTOS?

  • This is quite unclear. Why are you using those dangerous headers on a MISRA C system? What's your actual requirements, is this project actually safety-related or are you just fooling around with some non-critical project? – Lundin Jan 17 '22 at 10:06
  • As for why those calls will fail: they save the "environment status" which typically means stack pointer, program counter, maybe some registers. In a RTOS with multiple process stacks, it likely means that everything will go bananas, unless those functions are provided by the RTOS. – Lundin Jan 17 '22 at 10:08
  • I described not enough clearly. My project depends on lua library, and lua library use setjmp/longjmp as impletements. I could not stop using setjmp/longjmp unless rewrite all relative code. @Lundin – user8416300 Jan 17 '22 at 12:02
  • So it would seem that your Lua library and safety-related systems are mutually exclusive. Ask whoever made the lib why it sucks. It should come as no surprise to any C programmer that setjmp/longjmp are dirty dirty dangerous hacks. – Lundin Jan 17 '22 at 13:22
  • If you can work with Lua 5.1/2 you might want to try LuaJIT (if there is a port for your architecture). AFAIK LuaJIT does not rely on setjmp/longjmp – sneusse Jan 18 '22 at 08:57

0 Answers0