Hi guys,
I am writing a wrapper using FFI, and I have a segmentation fault when the code on the client part or in the library change a bit (which sometimes depending on the building mode [release|debug]). I have this problem for one week for now and made multiple searches on Internet, without success yet. I hope I post my question in the right place. If not, please tell me where I have to post it. Thanks
Context
I'm writing a raytracing program in Rust. I have written several of this program in the past, but I think the memory and thread safety in Rust is a neat feature for this type of application. In raytracing, we compute a lot of intersection point between a ray and a 3D model (triangle soup). Having a good performance for intersection testing is crucial raytracing. It is for this reason that I have started to make a wrapper for the Intel's Embree library (C-style library).
Note, I'm not the first one that has attempted to write an Embree wrapper for Rust. In my knowledge, two other wrappers are existing on GitHub:
- https://github.com/anderslanglands/embree-rs: This wrapper is outdated and seems to be not maintained.
- https://github.com/Twinklebear/embree-rs: Good wrapper which has used rust-bindgen for generating the interface between Rust and Embree. However, its major drawback is that this wrapper doesn't support multi-threading which is essential for achieving good performance.
Moreover, when I have tested this two library, I got a segmentation fault. For example, Twinklebear wrapper, provide an example "triangle_geometry" that works in release mode but not in debug mode (get a similar error than my wrapper).
It is for this reason that I have started my own Embree wrapper:
https://github.com/beltegeuse/embree-rs
Issue
Depending on some code changes (inside the library or the client code), the library segfault when calling the triangle intersection procedure (rtcIntersect in Embree) which corresponding to the function "Scene.intersect" in scene.rs:
pub fn intersect(&self, mut ray: Ray) -> Option<Intersection> {
unsafe { rtcIntersect(self.ptr, &mut ray) };
Intersection::from_ray(&self, ray)
}
Sometimes, I get the segmentation fault only in the release or debug mode, sometimes in both.
My attempts
I have tried several different ways to found the problem, without being successful yet. These what I have tried:
- Disable multithreading ( which not an issue as Embree support multithreading call for testing the intersection)
- Checked that no error occurred during the building of the acceleration data structure (using Embree function for that just after rtcCommit)
- Checked that the Embree device or scene have not been drop before I do the intersection test
What I am suspecting
I am suspecting that is somehow, a compliation related issue as sometimes, using a different compilation error leads to different behavior. Like:
- Call rearrangement
- Memory layout rearrangement
What I am seeking for
Anybody had encountered this problem behavior before?
Do there is a way to link to the debug library version with Rust? (So it makes easier to found the problem by inspecting the memory)
Configuration tested
Archlinux with Rustup. Tested in nightly and stable. Embree version 2.17.3-2 (in Pacman).