Discussion:
-jdump changes behavior
lex pops
2014-08-21 00:02:44 UTC
Permalink
Tried this on latest git repo. Running without -jdump always returns
101 for this code:

-----
local ffi = require("ffi")
ffi.cdef('struct box {double value;}')
local doublebox = ffi.typeof('struct box')

local arr = ffi.new('struct box *[1]')
arr[0] = doublebox(1)

for i=1,100 do
arr[0].value = arr[0].value + 1
end

print(arr[0].value)
-----

However running with -jdump produces varying bogus output ('nil',
'dispatchmap', 'stop', segfault, 43, etc.)

Using an array of struct (instead of struct *) fixes the issue. I'm
not 100% sure, but I assume my code is well defined because without
jdump it is consistent and correct.

~Lex
Alex
2014-08-21 00:25:46 UTC
Permalink
Post by lex pops
but I assume my code is well defined because without
jdump it is consistent and correct.
It isn't. In the following segment:

local arr = ffi.new('struct box *[1]')
arr[0] = doublebox(1)

The new doublebox is allocated by LuaJIT, so you need to keep a reference
to it. LuaJIT's GC does not traverse cdata pointers (since there's not an
easy way to tell if it is pointing to a cdata object or some other,
externally allocated object), so you discard your reference to the
allocated object and it becomes "unreachable".

Without -jdump the code doesn't run for too long and exits without needing
a GC step, but when ran with the flag, the dumping code allocates some
strings and things and uses enough memory to do a GC step and collect the
allocated object, making any pointers to it invalid.
lex pops
2014-08-21 03:30:04 UTC
Permalink
Post by lex pops
Post by lex pops
but I assume my code is well defined because without
jdump it is consistent and correct.
local arr = ffi.new('struct box *[1]')
arr[0] = doublebox(1)
The new doublebox is allocated by LuaJIT, so you need to keep a reference
to it.
Ah of course! Thanks for pointing it out.

lex

Loading...