Skip to content

Segfault when Calling: Callback -> C-Function -> Callback #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nwrkbiz opened this issue Dec 20, 2022 · 4 comments
Closed

Segfault when Calling: Callback -> C-Function -> Callback #47

nwrkbiz opened this issue Dec 20, 2022 · 4 comments

Comments

@nwrkbiz
Copy link

nwrkbiz commented Dec 20, 2022

Reproduced on Windows (64 Bit) and Linux (64 Bit Debian).

.DLL/.SO:

// fail.cpp
extern "C" {

typedef void (*cb)(char);

void callMe(cb clbk){
  clbk('a');
  clbk('b'); // this will fail
}

char getC(){
  return 'c';
}

}

// built using:
// g++ -g -c -o fail.o ./fail.cpp
// g++ -shared -o fail.so fail.o

.js

// fail.js
const koffi = require('./build/koffi.node');
const lib = koffi.load('./fail.so');

const cb = koffi.callback('void cb(char)');
const getC = lib.func('char getC()');
const callMe = lib.func('void callMe(cb* clbk)');

const rawCB = koffi.register(function(result){
        console.log(getC()); // this call somehow messes up the second trampoline call, a wrong CallData object (the object of getC) is then used for clbk('b')
        console.log(result);
    }, koffi.pointer(cb));


callMe(rawCB);
// node ./fail.js
// prints:
// 99 (c)
// 97 (a)
// Segementation fault

It seems like this pointer here points to the already deleted CallData object created by the getC call, but should point to the CallData Object of the initial callMe call:
image

@Koromix
Copy link
Owner

Koromix commented Dec 21, 2022

Thanks for the report, I've pushed a fix and I will release 2.2.1 today :)

@Koromix
Copy link
Owner

Koromix commented Dec 21, 2022

Released :)

@nwrkbiz
Copy link
Author

nwrkbiz commented Dec 21, 2022

Thank you for the fast fix, I will test with my lib.

@nwrkbiz
Copy link
Author

nwrkbiz commented Dec 21, 2022

Works like a charm!

@Koromix Koromix transferred this issue from Koromix/rygel Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants