Skip to content

SUKU use lua_absindex to handle serialization of nested tables correctly#329

Open
SukuWc wants to merge 1 commit intomasterfrom
SUKU-evaluate-nested-fix
Open

SUKU use lua_absindex to handle serialization of nested tables correctly#329
SukuWc wants to merge 1 commit intomasterfrom
SUKU-evaluate-nested-fix

Conversation

@SukuWc
Copy link
Member

@SukuWc SukuWc commented Mar 3, 2026

Input string in Editor

intechstudio/grid-editor#1437

return {1, 2, {3, 4}}

Input packet

��0042f9f7000000000000��086e000104001a<?lua return{1,2,{3,4}} ?>��42

Crash dump

Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.                                  
                                                                                                                 
  Core  1 register dump:                                                                                         
  PC      : 0x4206d61b  PS      : 0x00060a30  A0      : 0x82016f7d  A1      : 0x3fcdf860                         
  --- 0x4206d61b: luaH_realasize at /project/grid_common/lua-5.4.3/src/ltable.c:242                              
  A2      : 0x00000001  A3      : 0x000003a5  A4      : 0x3c0870cc  A5      : 0x3fcdf880                         
  --- 0x00000001: $xrv32i2p1_m2p0_c2p0_zmmul1p0 at ulp_riscv_vectors.S.obj:?                                     
  A6      : 0x3fcdf860  A7      : 0x00000008  A8      : 0x8205933d  A9      : 0x3fcdf780                         
  --- 0x00000008: __start at ??:?                                                                                
  A10     : 0x00000000  A11     : 0x3fcdfa0f  A12     : 0x000003a5  A13     : 0x3c0870cc                         
  --- 0x00000000: $xrv32i2p1_m2p0_c2p0_zmmul1p0 at ulp_riscv_vectors.S.obj:?                                     
  A14     : 0x00000002  A15     : 0x3fcc36a4  SAR     : 0x00000014  EXCCAUSE: 0x0000001c                         
  --- 0x00000002: $xrv32i2p1_m2p0_c2p0_zmmul1p0 at ulp_riscv_vectors.S.obj:?                                     
  --- 0x00000014: __ctzsi2 at /builds/idf/crosstool-NG/.build/riscv32-esp-elf/src/gcc/libgcc/libgcc2.c:722       
  --- 0x0000001c: __ctzsi2 at /builds/idf/crosstool-NG/.build/riscv32-esp-elf/src/gcc/libgcc/libgcc2.c:722       
  EXCVADDR: 0x00000007  LBEG    : 0x400556d5  LEND    : 0x400556e5  LCOUNT  : 0xffffffff                         
  --- 0x00000007: __start at ??:?                                                                                
  --- 0x400556d5: strlen in ROM                                                                                  
  --- 0x400556e5: strlen in ROM                                                                                  
                                                                                                                 
                                                                                                                 
  Backtrace: 0x4206d618:0x3fcdf860 0x42016f7a:0x3fcdf880 0x42015682:0x3fcdf8a0 0x42011385:0x3fcdf8c0             
  0x4201135b:0x3fcdf930 0x42011409:0x3fcdf9a0 0x42029bd2:0x3fcdf9c0 0x4202b00a:0x3fcdfdf0 0x4037b80d:0x3fcdfe10  
  0x4203be59:0x3fce0230 0x40381b21:0x3fce02b0                                                                    
  --- 0x4206d618: luaH_realasize at /project/grid_common/lua-5.4.3/src/ltable.c:241                              
  --- 0x42016f7a: luaH_next at /project/grid_common/lua-5.4.3/src/ltable.c:339                                   
  --- 0x42015682: lua_next at /project/grid_common/lua-5.4.3/src/lapi.c:1251                                     
  --- 0x42011385: grid_lua_serialize_stack_element at /project/grid_common/grid_lua.c:753                        
  --- 0x4201135b: grid_lua_serialize_stack_element at /project/grid_common/grid_lua.c:759                        
  --- 0x42011409: grid_lua_serialize_evaluation_results at /project/grid_common/grid_lua.c:798                   
  --- 0x42029bd2: grid_decode_evaluate_to_ui at /project/grid_common/grid_decode.c:603                           
  --- 0x4202b00a: grid_port_decode_class at /project/grid_common/grid_decode.c:1281                              
  --- (inlined by) grid_port_decode_msg at /project/grid_common/grid_decode.c:1308                               
  --- 0x4037b80d: grid_port_send_ui at /project/grid_common/grid_port.c:274                                      
  --- 0x4203be59: grid_esp32_port_task at /project/grid_esp/components/grid_esp32_port/grid_esp32_port.c:535     
  --- 0x40381b21: vPortTaskWrapper at /esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:139

The root cause:

  • element is passed as -1 (relative stack index) when serializing the nested table value
  • lua_pushnil(L) is called first to initialize lua_next iteration — this shifts the stack by 1
  • -1 now points to the nil that was just pushed, not the table
  • lua_next receives a nil where it expects a table; Lua's nil tag value (0x1) gets dereferenced as a table pointer
    → LoadProhibited at address 0x7

The fix: lua_absindex(L, element) converts the relative index to an absolute (positive) one before anything is
pushed onto the stack. This is safe for all call sites — positive indices are returned unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant