[LLVMdev] Invalid or unaligned stack

Dibyendu Majumdar mobile at majumdar.org.uk
Tue Mar 31 15:31:34 PDT 2015


I constructed a minimal Lua program that reproduces the problem.
Essentially the problem occurs if a JITed function is recursively
called - and there is a longjmp from the inner call. Example:

function rais(n)
  if n == 0 then error()
  else rais(n-1)

function caller(n)


Here the call to error() triggers a longjmp. The pcall() calls setjmp.
The error only occurs on Windows as reported earlier.

I ran valgrind on Ubuntu to see if I could detect any memory issues.
Valgrind reports 6 errors of following type - not sure if this is an
issue or not.

==66154== HEAP SUMMARY:
==66154==     in use at exit: 115,521 bytes in 720 blocks
==66154==   total heap usage: 9,357 allocs, 8,637 frees, 3,887,031
bytes allocated
==66154== 152 bytes in 1 blocks are possibly lost in loss record 378 of 423
==66154==    at 0x4C2AB80: malloc (in
==66154==    by 0x516AD4D: llvm::MDNode::getMDNode(llvm::LLVMContext&,
llvm::ArrayRef<llvm::Value*>, llvm::MDNode::FunctionLocalness, bool)
(in /home/dylan/github/ravi/build/libravi.so)
==66154==    by 0x516A2B8:
llvm::MDBuilder::createTBAAStructTagNode(llvm::MDNode*, llvm::MDNode*,
unsigned long) (in /home/dylan/github/ravi/build/libravi.so)
==66154==    by 0x5068961:
ravi::LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext&) (in
==66154==    by 0x5063EF6: ravi::RaviJITStateImpl::RaviJITStateImpl()
(in /home/dylan/github/ravi/build/libravi.so)
==66154==    by 0x50644FD: raviV_initjit (in
==66154==    by 0x5057DF5: lua_newstate (in
==66154==    by 0x503641E: luaL_newstate (in
==66154==    by 0x401674: main (in /home/dylan/github/ravi/build/lua)

On 30 March 2015 at 23:59, Dibyendu Majumdar <mobile at majumdar.org.uk> wrote:
> Hi,
> I am encountering a problem that I do not know how to debug. I would
> greatly appreciate any guidance on this issue.
> On Windows when I run Lua test cases from JITed code I am getting
> following error:
> Unhandled exception at 0x00007FFCEEEAC500 (ntdll.dll) in lua.exe:
> 0xC0000028: An invalid or unaligned stack was encountered during an
> unwind operation.
> This is happening when the Lua code is attempting to call longjmp().
> The Lua test case that triggers this is a recursive call as shown below.
> function err_on_n (n)
>   if n==0 then error(); exit(1);
>   else err_on_n (n-1); exit(1);
>   end
> end
> do
>   function dummy (n)
>     if n > 0 then
>       assert(not pcall(err_on_n, n))
>       dummy(n-1)
>     end
>   end
> end
> dummy(10)
> I have a struct that is created on the stack in the JIT compiler, and
> this error is triggered when I add a field to the struct - if I remove
> the field the error stops. The struct is not very large in size - it
> is only 392 bytes with the new field.
> If I allocate this struct on the heap the error goes away.
> Note that just adding the field triggers the error even if I have no
> other code changes.
> The error occurs in Release build but not in Debug build.
> I am using Visual C++ 2013 - 64-bit, and LLVM 3.6.0 on Windows.
> I see no other unexpected behaviour - all the other tests pass.
> All tests pass on Ubuntu, using LLVM 3.5.1 and gcc 4.8.2.
> Of course I am currently assuming that somewhere my program is
> corrupting memory - but as I have no other signs of a memory
> corruption - the issue may be something else.
> Any tips on what the problem might be would be gratefully received.
> Thanks and Regards
> Dibyendu

More information about the llvm-dev mailing list