Re: SIGSEGV in LuaJIT 2.1 VM

  • From: Tomas Kvasnicka <nzt4567@xxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Fri, 18 Nov 2016 15:52:50 +0100

Again, thanks a lot for the responses.

Let's put aside the thread affinity approach, as it's more complex. You can 
diagnose the issue quite rapidly with the unprotected mcode approach: in 
'src/lj_mcode.h', find "LUAJIT_UNPROTECT_MCODE" symbol. It's a define, that's 
not set by default. Set it unconditionally above the first usage, re-compile 
and re-deploy. If it stops crashing, that's our bug.
Please read and understand carefully what this define means (right after the 
first usage) and use it at your own risk. As a side note, it does not affect 
stability, but brings a very-very potential vulnerability in your service. 
Like, very-very potentinal. I think it's the best way to use this approach 
for diagnostic purpose only and, if it helps, return here for more solid 
solution (for example, the one proposed by Peter).
Noted, thanks for the warning.

An alternative hypothesis is that the memory is seen as executable,
but the contents of the memory is still seen as 00 00 00 00 by the
executing CPU, which decodes as `add [eax], ax`, and would segfault
given that rax contained 0xC. That said, rapidly flipping page
protection causing confusion seems a more likely hypothesis.
Interesting idea indeed - this got me checking some other core dumps available 
to see if RAX is always filled with such low values. The answer is that mostly 
no - even values like 0x41978ae8 occur in RAX and these would probably be 
dereferenced without any problem (sections marked as "ALLOC LOAD HAS_CONTENTS”).

More information regarding the infrastructure can be provided
- the code is statically linked into nginx binary (compiled using luajit -bg 
<source.lua>)
- there is no virtualization at all, bare metal computers having mostly Intel 
E5-2620
- lvmst (part of openresty-gdb-utils) shows different trace number then the one 
which contains the address in RIP

EG:
(gdb) lvmst
Compiled Lua code (trace #428)
(gdb) ltracebymcode $rip
(GCtrace*)0x40e05808 (trace #434)
machine code start addr: 0x834c87b
machine code end addr: 0x834cb1f
@modules/my_modules.lua:27
(GCtrace*)0x41fe9540 (trace #435)
machine code start addr: 0x834c556
machine code end addr: 0x834c87b
@modules/my_modules.lua:27

Thanks a lot for your time!

Tomas

Other related posts: