Need help with FFI on 32 vs 64-bit versions of Love2d 11.2

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
ivan
Party member
Posts: 1530
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Need help with FFI on 32 vs 64-bit versions of Love2d 11.2

Post by ivan » Sun Jun 23, 2019 6:18 am

Hello. I am working on a wrapper library for Steamworks v1.44 using just FFI. The library uses just pure FFI without custom-built binaries. This allows for easy maintenance and upgrades in the future. The source code is available here:
https://bitbucket.org/itraykov/sworks-ffi/src/master/
The library works good, but I am having trouble with the callbacks mechanism behind Sworks. I managed to get it working on Love2D 64-bit, but I can't get it to work on 32-bit versions of Love2D. Note that the callbacks mechanism requires C++ which makes things messy but not impossible. Also note that I have tried to take struct packing into account between platforms. I use "jit.off(func, true)" for the function that triggers the Sworks callbacks.
On 32-bit versions of Love2D, I get a crash after a callback is triggered (see vt:Run1 and vt:Run2 which are supposed to be virtual functions):
https://bitbucket.org/itraykov/sworks-f ... er/api.lua
Would appreciate any feedback as to why this works fine on 64-bit versions of Love2D but fails on 32-bit. I don't know much about FFI so any help is greatly appreciated. Thanks!

Further reading:
https://garry.tv/2016/11/01/steamworks-sdk/

grump
Party member
Posts: 587
Joined: Sat Jul 22, 2017 7:43 pm

Re: Need help with FFI on 32 vs 64-bit versions of Love2d 11.2

Post by grump » Tue Jun 25, 2019 5:11 pm

Have you tried to run it on a "real" 32 bit OS? Maybe it's a pointer size issue. Just guessing though.

It's probably not relevant to your problem, but I'm pretty sure the cast from <event> to <event>* is not required.

Code: Select all

local ffi = require('ffi')

ffi.cdef("typedef struct Blah { int i; };")

local b = ffi.new("struct Blah")
local p = ffi.cast("struct Blah*", b)

assert(b == p) -- true
ffi.new already returns a pointer to the allocated object.

Code: Select all

ffi.cdef([[
typedef unsigned char uint8;
typedef unsigned char uint8;
typedef signed char int8;
typedef short int16;
...
uint8 is defined twice. These are already defined, btw. They are named uint8_t, int16_t, etc.

User avatar
ivan
Party member
Posts: 1530
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Need help with FFI on 32 vs 64-bit versions of Love2d 11.2

Post by ivan » Wed Jun 26, 2019 1:31 pm

Hey Grump,
Thank you so much for taking a look at this. I'm not very good with C or FFI so any help is greatly appreciated.
Have you tried to run it on a "real" 32 bit OS? Maybe it's a pointer size issue. Just guessing though.
Have not tried that yet, everything works great, except for Steamworks' callbacks mechanism.
Also, note that the 64-bit binaries work fine even with the callbacks enabled.
ffi.new already returns a pointer to the allocated object.
I'm trying to cast a void pointer there, so without the asterisk * I get:

Code: Select all

Error: utils/avatar/sworks/api.lua:697: bad argument #1 to 'cast' (invalid C type)
Note that events are C++ objects with inheritance and virtual functions.
Not really sure how to handle this otherwise.
uint8 is defined twice.
Good catch there, this should be fixed as of today!

grump
Party member
Posts: 587
Joined: Sat Jul 22, 2017 7:43 pm

Re: Need help with FFI on 32 vs 64-bit versions of Love2d 11.2

Post by grump » Wed Jun 26, 2019 8:02 pm

ivan wrote:
Wed Jun 26, 2019 1:31 pm
I'm trying to cast a void pointer there
I see. Please disregard my comment, I wasn't reading the code properly.

User avatar
raidho36
Party member
Posts: 1943
Joined: Mon Jun 17, 2013 12:00 pm

Re: Need help with FFI on 32 vs 64-bit versions of Love2d 11.2

Post by raidho36 » Wed Jun 26, 2019 9:10 pm

I'm asking because you haven't specified: are you using 32 bit DLLs with 32 bit executables?

User avatar
ivan
Party member
Posts: 1530
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Need help with FFI on 32 vs 64-bit versions of Love2d 11.2

Post by ivan » Thu Jun 27, 2019 3:34 am

True, the code is hacky because we are faking C++ which is clearly unsupported by FFI.
raidho36 wrote:
Wed Jun 26, 2019 9:10 pm
I'm asking because you haven't specified: are you using 32 bit DLLs with 32 bit executables?
Yes, 32 bit binaries (vanilla love2d and sworks) on a 64 bit OS.

User avatar
pgimeno
Party member
Posts: 1903
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

Re: Need help with FFI on 32 vs 64-bit versions of Love2d 11.2

Post by pgimeno » Fri Jun 28, 2019 6:27 am

This looks dodgy to me:

Code: Select all

typedef unsigned char uint8;
typedef signed char int8;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;
Maybe try

Code: Select all

typedef uint8_t uint8;
typedef int8_t int8;
typedef int16_t int16;
typedef uint16_t uint16;
typedef int32_t int32;
typedef uint32_t uint32;
typedef int64_t int64;
typedef uint64_t uint64;

User avatar
ivan
Party member
Posts: 1530
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Need help with FFI on 32 vs 64-bit versions of Love2d 11.2

Post by ivan » Fri Jun 28, 2019 5:14 pm

I tried your version and it still crashes at the same point: virtual function call at vt:Run1
Btw, I didn't come up with that definition, it comes from "steam_api_flat.h"

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests