back

Libffi Types and Argument Order

2022-02-07

Ran into a pretty bizarre issue and it was 100% because I have no idea what I'm doing.

While working with the libffi library, I created my own type with the size left at 0 so that libffi I assume will set it for me. I used this type when setting up my arguments that get passed into the function. Everything seemed to work but I had an issue where the function seemed to be reading in the same variable twice each time. So if I passed in 0, 1, 2, 3, 4, the function would see 0, 1, 1, 2, 2. Very weird. I then wrote a stub function that I could control the mimicked the one I was trying to use and started printing out addresses. The function was definitely getting weird addresses. The pointers going into the function looked healthy and were 8 bytes long.

It was only after hours that I thought about changing the ffi_type i have set. Instead of using my custom type, I made it just a ffi_type_pointer to force it be 8 bytes. Voila! Everything started working! I'm guessing that my custom type was set up incorrectly and so when libffi passed parameters into the function, it was incorrect defining addresses.

Not really sure what's going on but for now I got my little node library working.

Also, searching for libffi help points me to my dev.to post. That was the most unhelpful search I've done where I'm looking for an answer and google directed me to myself. Very funny.