Closed mdrokz closed 10 months ago
It's strange that there's only a SIGSEV
when running under Node, but the issue seems unrelated. I see a couple of bugs in the unsafe code:
sgfpm
should be a LPSGFPM
(pointer to SGFPM
) and not a SGFPM
.as_mut_ptr()
gives you a pointer to the MaybeUninit
space, not the underlying pointerThe code is attempts to write a pointer into space allocated for the struct SGFPM
instead of a pointer. It then tries to use the pointer to the MaybeUninit
as if it were the pointer to SGFPM
. This is going to be garbage data.
A couple of recommendations:
MaybeUninit
.cast()
instead of as
The combination of these two will make type errors with pointers much less likely in unsafe code since they become compiler errors.
I don't have a good way to test, but I think it should look something like this:
let mut sgfpm = MaybeUninit::uninit();
unsafe {
CreateSGFPMObject(sgfpm.as_mut_ptr());
}
let sgfpm = sgfpm.assume_init();
if sgfpm.is_null() {
return cx.throw_error("Failed to create SGFPMObject");
}
unsafe {
// Not sure if this should be `sgfpm`, `&sgfpm` or `&mut sgfpm`
let err = SGFPM_Init(sgfpm, SGFDxDeviceName_SG_DEV_AUTO.into());
}
It's strange that there's only a
SIGSEV
when running under Node, but the issue seems unrelated. I see a couple of bugs in the unsafe code:
sgfpm
should be aLPSGFPM
(pointer toSGFPM
) and not aSGFPM
.- Calling
as_mut_ptr()
gives you a pointer to theMaybeUninit
space, not the underlying pointerThe code is attempts to write a pointer into space allocated for the struct
SGFPM
instead of a pointer. It then tries to use the pointer to theMaybeUninit
as if it were the pointer toSGFPM
. This is going to be garbage data.A couple of recommendations:
- Let Rust infer the type of
MaybeUninit
- Use
.cast()
instead ofas
The combination of these two will make type errors with pointers much less likely in unsafe code since they become compiler errors.
I don't have a good way to test, but I think it should look something like this:
let mut sgfpm = MaybeUninit::uninit(); unsafe { CreateSGFPMObject(sgfpm.as_mut_ptr()); } let sgfpm = sgfpm.assume_init(); if sgfpm.is_null() { return cx.throw_error("Failed to create SGFPMObject"); } unsafe { // Not sure if this should be `sgfpm`, `&sgfpm` or `&mut sgfpm` let err = SGFPM_Init(sgfpm, SGFDxDeviceName_SG_DEV_AUTO.into()); }
Hey thanks for the suggestions i didnt know you could do that i was trying to figure out how to use MaybeUninit
properly. I tried this & it still didnt work so i debugged with gdb and it seems to crash at this function in libusb
LD_LIBRARY_PATH=/usr/local/lib/ gdb node
Thread 1 "node" received signal SIGSEGV, Segmentation fault.
0x00007ffff403cf3f in usb_control_msg (dev=0x0, bmRequestType=64, bRequest=<optimized out>, wValue=<optimized out>, wIndex=0, bytes=0x0, size=0, timeout=2000)
at /usr/src/debug/libusb-compat-0.1-0.1.7-9.fc37.x86_64/libusb/core.c:878
878 r = libusb_control_transfer(dev->handle, bmRequestType & 0xff,
Its weird that it only happens with node & my rust binary works fine do you have any idea what interaction there is with node & libusb ?
I'm not aware of anything that would cause this in Node. Is the source available that I could run and test?
I'm not aware of anything that would cause this in Node. Is the source available that I could run and test?
no let me quickly set up a repo for you
I'm not aware of anything that would cause this in Node. Is the source available that I could run and test?
I set up a repo here https://github.com/mdrokz/secugen_rs
just set "cargo:rustc-link-search=native=/usr/local/lib"
to your cloned repo path i have the libs in there.
@mdrokz Looks like libsgimage.so
(and possibly others) is missing.
I found some libs on line and got a missing CreateISensor
symbol. If I create a stub for that, I get further and there's no SIGSEGV.
Init device
Init device
err: 6
Device initialized
I'm not sure how else to help. I don't think this is a Node/Neon issue.
I found some libs on line and got a missing
CreateISensor
symbol. If I create a stub for that, I get further and there's no SIGSEGV.Init device Init device err: 6 Device initialized
I'm not sure how else to help. I don't think this is a Node/Neon issue.
Oh it worked for you i guess its probably a system related issue with the libs. Sorry for taking your time i will close this now i will test it on other systems.
Hi thanks for the wonderful crate Im currently trying to use neon with a C library for scanning fingerprints so i can integrate it into my frontend with neon bindings. but i get a segfault everytime i run the init method. I have it working with a normal rust binary but with neon it doesnt work here's my rust code
build.rs
lib.rs
When i invoke this from the node side like this
I get this error