From 3a5626bc279c6168da3a1f879675b71690d7614f Mon Sep 17 00:00:00 2001 From: Kyle Bayes Date: Tue, 2 Jun 2026 09:14:33 -0700 Subject: [PATCH] Align GJK internal memory to avoid misalignment segfaults when compiled as single precision. This is effectively a no-op under double precision. PiperOrigin-RevId: 925389982 Change-Id: I7117779669965ebf72cb77ccbece419acf9b1b96 --- src/engine/engine_collision_gjk.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/engine/engine_collision_gjk.c b/src/engine/engine_collision_gjk.c index 8ad8d782..c662529b 100644 --- a/src/engine/engine_collision_gjk.c +++ b/src/engine/engine_collision_gjk.c @@ -28,6 +28,11 @@ #define mjMINVAL2 (mjMINVAL * mjMINVAL) #define mjMAXVAL2 (mjMAXVAL * mjMAXVAL) +// align memory size on 8-byte boundary; needed for single precision +static inline size_t align8(size_t size) { + return ((size + 7) / 8) * 8; +} + // subdistance algorithm for GJK that computes the barycentric coordinates of the point in a // simplex closest to the origin // implementation adapted from Montanari et al, ToG 2017 @@ -2217,10 +2222,11 @@ static inline void inflate(mjCCDStatus* status, mjtNum margin1, mjtNum margin2) // return size in bytes of the buffer needed for mjc_ccd for a given number of iterations size_t mjc_ccdSize(int iterations) { - return (sizeof(Face) * 6 * iterations) // faces in polytope - + (sizeof(Face*) * 6 * iterations) // map in polytope - + (sizeof(Vertex) * (5 + iterations)) // vertices in polytope - + 2 * (24 * sizeof(int)); // horizon data + return align8(sizeof(Vertex) * (5 + iterations)) // vertices in polytope + + align8(sizeof(Face) * 6 * iterations) // faces in polytope + + align8(sizeof(Face*) * 6 * iterations) // map in polytope + + align8(sizeof(int) * 24) // horizon indices + + align8(sizeof(int) * 24); // horizon edges } @@ -2313,13 +2319,13 @@ mjtNum mjc_ccd(const mjCCDConfig* config, mjCCDStatus* status, mjCCDObj* obj1, m pt.maxfaces = 6 * N; uint8_t* buffer = config->buffer; pt.verts = (Vertex*)buffer; - buffer += sizeof(Vertex) * (5 + N); + buffer += align8(sizeof(Vertex) * (5 + N)); pt.faces = (Face*)buffer; - buffer += sizeof(Face) * (6 * N); + buffer += align8(sizeof(Face) * (6 * N)); pt.map = (Face**)buffer; - buffer += sizeof(Face*) * (6 * N); + buffer += align8(sizeof(Face*) * (6 * N)); pt.horizon.indices = (int*)buffer; - buffer += sizeof(int) * 24; + buffer += align8(sizeof(int) * 24); pt.horizon.edges = (int*)buffer; int ret;