Page 1 of 1

[solved]LuaJIT + FFI small sample runs ok but crash within Love2D

Posted: Mon Feb 15, 2021 4:27 pm
by lost-in-code
Hi there.
First, I want to apologize for the mistakes or misunderstandings that may occur, as i am not an english native speaker.
Secondly, i've been around for several months, reading and learning, and now that i've ended creating an account, i want to thank all of you guys for the great help this forum has been for me, and for the awesome Love2D framework.
Now, as you have guessed, i have a little problem that i didn't manage to find a fix by myself.

I have this file compiled as a dynamic lib:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

int square (int a) {
    return a*a;
}
I have this little test script:

Code: Select all

ffi = require "ffi"

ffi.cdef [[
  int square (int a);
]]

local test = ffi.load("module_ffi")

output = test.square(4)

io.write(_VERSION .. os.execute("luajit -v") .."\nOutput of square() = " .. output .. "\n")
Now here are the results with first a call of "luajit main.lua", followed by a call of "love ."

Code: Select all

lost@wonderland hybrid_C_Lua % luajit main.lua
LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
Lua 5.10
Output of square() = 16
lost@wonderland hybrid_C_Lua % love .
Error: main.lua:7: dlopen(libmodule_ffi.dylib, 5): no suitable image found.  Did find:
        file system relative paths not allowed in hardened programs
stack traceback:
        [string "boot.lua"]:777: in function <[string "boot.lua"]:773>
        [C]: in function 'load'
        main.lua:7: in main chunk
        [C]: in function 'require'
        [string "boot.lua"]:570: in function <[string "boot.lua"]:380>
        [C]: in function 'xpcall'
        [string "boot.lua"]:787: in function <[string "boot.lua"]:780>
        [C]: in function 'xpcall'
io.write(_VERSION .. os.execute("luajit -v") .."\nOutput of square() = " .. output .. "\n")
It seems that there's something related to Love2d expectations that i did wrong. I've been scratching my head on this for enough time now, so if you have any idea about where i should go now, you would really make my day.

Thanks for your time,
And I hope i could be of help some day.

Re: LuaJIT + FFI small sample runs ok but crash within Love2D

Posted: Mon Feb 15, 2021 7:43 pm
by grump
file system relative paths not allowed in hardened programs
If this means what I think it means then copying the file to the save directory of your game and loading it with ffi.load(love.filesystem.getSaveDirectory() .. '/' .. module_filename) might fix the problem.

You sure about those Lua header files? The point of ffi is that you can import any module that has a C ABI. There's normally no need for any Lua stuff in there.

Re: LuaJIT + FFI small sample runs ok but crash within Love2D

Posted: Mon Feb 15, 2021 8:05 pm
by lost-in-code
Thanks a lot for answering grump.

I 've tried with love.filesystem.getSaveDirectory(), i had thinked of it but not tried because the documentation for ffi.load() explain that it require only the name of the lib and autocomplete the string. Anyway, the error message kind of shortened :

Error: main.lua:8: dlopen(/Users/lost/Library/Application Support/LOVE/hybrid_C_Lua/libmodule_ffi.dylib, 5): image not found

For the headers, if not there, the dynamic library does not compile. I'm no expert and just followed what my understanding of the Lua / C API and LuaJIT are at the moment ( pretty much nothing ^^).

Seems like you were right for the path involvement in this error.

Beside that, what makes me really lost is that when called with the luajit command, the original script works...

Re: LuaJIT + FFI small sample runs ok but crash within Love2D

Posted: Mon Feb 15, 2021 8:34 pm
by lost-in-code
OK !

Starting with grump's suggestion about the path involvement, i've tried several things.
And here is what i found, hoping that will be useful for someone... one day...

My tests with the luajit + FFI direct call requested some macos dynamic libraries, so i thought that was the same for the love2D's LuaJIT / FFI.
In fact ... no :)

Once recompiled my dynamic lib in the .so format, and putting it in the save file, that worked !
Edit : .so can be placed in the main folder, just have to use ffi.load(love.filesystem.getSource().."/".."module_ffi.so") for loading. That seems so logical now that i wonder why i even got lost at first...

I hope i managed to explain this clearly enough.

Thanks again grump !

Re: LuaJIT + FFI small sample runs ok but crash within Love2D

Posted: Mon Feb 15, 2021 9:25 pm
by grump
lost-in-code wrote: Mon Feb 15, 2021 8:05 pm For the headers, if not there, the dynamic library does not compile. I'm no expert and just followed what my understanding of the Lua / C API and LuaJIT are at the moment ( pretty much nothing ^^).
Lua's C API is used to make Lua modules in C, while ffi is used to import C modules in Lua. There's no connection between the two concepts. A library loaded with ffi.load does not need to know anything about Lua.

It's probably possible and doesn't hurt to do both things at the same time, but it seems weird. Unless you're doing it for a specific reason, you may be doing it wrong.

Re: [solved]LuaJIT + FFI small sample runs ok but crash within Love2D

Posted: Mon Feb 15, 2021 10:11 pm
by lost-in-code
Of course i'm doing it wrong !
And thanks for pointing it out.
I will dive further and further until it all become clear to me.
Having my little sample working is not sufficient.
I'm just happy that i did a step today ;)