Segmentation fault in embree when using any allocator anywhere in code

might be a 0.11.0 compiler bug

1 Like

unfortunately, result is the same

Could be a thing where it assumes everything is 16-byte-aligned. Will get back to this tomorrow.

2 Likes

is there anything I could try or check on my own?

Keep track of the various addresses, when debugging the code that does not crash vs the code that crash. (e.g. The address of scene minus the address of device remains the same?)
Write down all the stack variables addresses for the crash and not crash case (to print the address of a variable use: p &variable)
Check if the variables are aligned, the library expects the alignment to be 16 bytes, that means that the last hex digit in address must be 0.
Maybe when the stack size is increasing, by adding more variables and/or calls that return values, something is changing in the placement of the other variables.
Note the differences, see also what happens when you remove the @alignCast.
EDIT: see what happens if you place align(16) after * or ] in variable declarations.
e.g.

var vertex_buff: [*]align(16) f32 = ...
1 Like

oh my!

i’ve checked that vertex_buff and index_buff are 16-aligned, and difference between them stays the same,
BUT
address of rayhit variable on the stack was 16-aligned when program run, and not aligned when it crashed.
so changing it’s declaration to

var rayhit: embree.RTCRayHit align(16) = std.mem.zeroes(embree.RTCRayHit);

prevented the crash!
Have not tested anything else, but at least all cases above are not segfaulting any more

Is this a valid thing i did there?

1 Like

Yes, it is valid.

What is happening:
There is a define macro RTC_ALIGN(x) called with x=16.
This is translated by the c preprocessor to __attribute__((aligned(16))) that means: the alignment must be at least 16 bytes.
Unfortunately zig translate-c does not work correctly and ignores the alignment directive.

Workaround:
If you have RTC_ALIGN(16) in the C declaration you put align(16) in zig variable type.

1 Like

Understood! nice to have a workaround, thank you!

Is it possible to define a type that is same as one coming from embree header, but with alignment?

something like

const RTCRayHit = embree.RTCRayHit align(16);

(the way above does not work obviously)

Currently it does not work for types; it works for variable declaration.
Discussion: Difference between struct level align(x) and field level align(x)

2 Likes