Skip to content
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

Fix several undefined behavior issues (strict aliasing and ODR violations) #4594

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion src/Bullet3OpenCL/BroadphaseCollision/b3GpuSapBroadphase.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

bool searchIncremental3dSapOnGpu = true;
#include <limits.h>
#include <string.h>
#include "b3GpuSapBroadphase.h"
#include "Bullet3Common/b3Vector3.h"
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
Expand Down Expand Up @@ -183,7 +184,8 @@ static bool TestAabbAgainstAabb2(const b3Vector3& aabbMin1, const b3Vector3& aab
//http://stereopsis.com/radix.html
static unsigned int FloatFlip(float fl)
{
unsigned int f = *(unsigned int*)&fl;
unsigned int f;
memcpy(&f, &fl, sizeof(f));
unsigned int mask = -(int)(f >> 31) | 0x80000000;
return f ^ mask;
};
Expand Down
2 changes: 1 addition & 1 deletion src/Bullet3OpenCL/ParallelPrimitives/b3RadixSort32CL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void b3RadixSort32CL::execute(b3OpenCLArray<b3SortData>& keyValuesInOut, int sor

#define USE_BTFILL
#ifdef USE_BTFILL
m_fill->execute((b3OpenCLArray<b3Int2>&)*m_workBuffer4, (b3Int2&)fillValue, workingSize - originalSize, originalSize);
m_fill->execute((b3OpenCLArray<b3Int2>&)*m_workBuffer4, b3MakeInt2(fillValue.m_key, fillValue.m_value), workingSize - originalSize, originalSize);
#else
//fill the remaining bits (very slow way, todo: fill on GPU/OpenCL side)

Expand Down
13 changes: 3 additions & 10 deletions src/Bullet3OpenCL/RigidBody/b3GpuPgsContactSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,9 @@ b3GpuPgsContactSolver::~b3GpuPgsContactSolver()
delete m_data;
}

struct b3ConstraintCfg
struct b3ConstraintCfg : public b3SolverBase::ConstraintCfg
{
b3ConstraintCfg(float dt = 0.f) : m_positionDrift(0.005f), m_positionConstraintCoeff(0.2f), m_dt(dt), m_staticIdx(0) {}

float m_positionDrift;
float m_positionConstraintCoeff;
float m_dt;
bool m_enableParallelSolve;
float m_batchCellSize;
int m_staticIdx;
b3ConstraintCfg(float dt = 0.f) : b3SolverBase::ConstraintCfg(dt) {m_staticIdx = 0;}
};

void b3GpuPgsContactSolver::solveContactConstraintBatchSizes(const b3OpenCLArray<b3RigidBodyData>* bodyBuf, const b3OpenCLArray<b3InertiaData>* shapeBuf,
Expand Down Expand Up @@ -1040,7 +1033,7 @@ void b3GpuPgsContactSolver::solveContacts(int numBodies, cl_mem bodyBuf, cl_mem
shapeBuf, m_data->m_solverGPU->m_contactBuffer2,
contactConstraintOut,
additionalData, nContacts,
(b3SolverBase::ConstraintCfg&)csCfg);
csCfg);
clFinish(m_data->m_queue);
}

Expand Down
10 changes: 7 additions & 3 deletions src/BulletCollision/Gimpact/btContactProcessingStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution.
*/

#include "LinearMath/btTransform.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "btTriangleShapeEx.h"
#include "LinearMath/btVector3.h"

/**
Configuration var for applying interpolation of contact normals
Expand All @@ -37,6 +35,12 @@ Configuration var for applying interpolation of contact normals

///The GIM_CONTACT is an internal GIMPACT structure, similar to btManifoldPoint.
///@todo: remove and replace GIM_CONTACT by btManifoldPoint.

/// Structure for collision results
///Functions for managing and sorting contacts resulting from a collision query.
///Contact lists must be create by calling \ref GIM_CREATE_CONTACT_LIST
///After querys, contact lists must be destroy by calling \ref GIM_DYNARRAY_DESTROY
///Contacts can be merge for avoid duplicate results by calling \ref gim_merge_contacts
class GIM_CONTACT
{
public:
Expand Down
90 changes: 1 addition & 89 deletions src/BulletCollision/Gimpact/gim_contact.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,95 +36,7 @@ email: projectileman@yahoo.com
#include "gim_radixsort.h"
#include "gim_array.h"

/**
Configuration var for applying interpolation of contact normals
*/
#ifndef NORMAL_CONTACT_AVERAGE
#define NORMAL_CONTACT_AVERAGE 1
#endif

#ifndef CONTACT_DIFF_EPSILON
#define CONTACT_DIFF_EPSILON 0.00001f
#endif

#ifndef BT_CONTACT_H_STRUCTS_INCLUDED

/// Structure for collision results
///Functions for managing and sorting contacts resulting from a collision query.
///Contact lists must be create by calling \ref GIM_CREATE_CONTACT_LIST
///After querys, contact lists must be destroy by calling \ref GIM_DYNARRAY_DESTROY
///Contacts can be merge for avoid duplicate results by calling \ref gim_merge_contacts
class GIM_CONTACT
{
public:
btVector3 m_point;
btVector3 m_normal;
GREAL m_depth; //Positive value indicates interpenetration
GREAL m_distance; //Padding not for use
GUINT m_feature1; //Face number
GUINT m_feature2; //Face number
public:
GIM_CONTACT()
{
}

GIM_CONTACT(const GIM_CONTACT &contact) : m_point(contact.m_point),
m_normal(contact.m_normal),
m_depth(contact.m_depth),
m_feature1(contact.m_feature1),
m_feature2(contact.m_feature2)
{
m_point = contact.m_point;
m_normal = contact.m_normal;
m_depth = contact.m_depth;
m_feature1 = contact.m_feature1;
m_feature2 = contact.m_feature2;
}

GIM_CONTACT(const btVector3 &point, const btVector3 &normal,
GREAL depth, GUINT feature1, GUINT feature2) : m_point(point),
m_normal(normal),
m_depth(depth),
m_feature1(feature1),
m_feature2(feature2)
{
}

//! Calcs key for coord classification
SIMD_FORCE_INLINE GUINT calc_key_contact() const
{
GINT _coords[] = {
(GINT)(m_point[0] * 1000.0f + 1.0f),
(GINT)(m_point[1] * 1333.0f),
(GINT)(m_point[2] * 2133.0f + 3.0f)};
GUINT _hash = 0;
GUINT *_uitmp = (GUINT *)(&_coords[0]);
_hash = *_uitmp;
_uitmp++;
_hash += (*_uitmp) << 4;
_uitmp++;
_hash += (*_uitmp) << 8;
return _hash;
}

SIMD_FORCE_INLINE void interpolate_normals(btVector3 *normals, GUINT normal_count)
{
btVector3 vec_sum(m_normal);
for (GUINT i = 0; i < normal_count; i++)
{
vec_sum += normals[i];
}

GREAL vec_sum_len = vec_sum.length2();
if (vec_sum_len < CONTACT_DIFF_EPSILON) return;

GIM_INV_SQRT(vec_sum_len, vec_sum_len); // 1/sqrt(vec_sum_len)

m_normal = vec_sum * vec_sum_len;
}
};

#endif
#include "btContactProcessingStructs.h"

class gim_contact_array : public gim_array<GIM_CONTACT>
{
Expand Down
23 changes: 19 additions & 4 deletions src/BulletCollision/Gimpact/gim_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ email: projectileman@yahoo.com
-----------------------------------------------------------------------------
*/

#include <string.h>
#include "LinearMath/btScalar.h"

#define GREAL btScalar
Expand Down Expand Up @@ -70,17 +71,31 @@ enum GIM_SCALAR_TYPES
#define G_DEGTORAD(X) ((X)*3.1415926f / 180.0f)
#define G_RADTODEG(X) ((X)*180.0f / 3.1415926f)

static GUINT gim_ir__(float r)
{
GUINT i;
memcpy(&i, &r, sizeof(i));
return i;
}

static GREAL gim_fr__(GUINT i)
{
float r;
memcpy(&r, &i, sizeof(r));
return r;
}

//! Integer representation of a floating-point value.
#define GIM_IR(x) ((GUINT&)(x))
#define GIM_IR(x) (gim_ir__(x))

//! Signed integer representation of a floating-point value.
#define GIM_SIR(x) ((GINT&)(x))
#define GIM_SIR(x) ((GINT)gim_ir__(x))

//! Absolute integer representation of a floating-point value
#define GIM_AIR(x) (GIM_IR(x) & 0x7fffffff)
#define GIM_AIR(x) (gim_ir__(x) & 0x7fffffff)

//! Floating-point representation of an integer value.
#define GIM_FR(x) ((GREAL&)(x))
#define GIM_FR(x) (gim_fr__(x))

#define GIM_MAX(a, b) (a < b ? b : a)
#define GIM_MIN(a, b) (a > b ? b : a)
Expand Down
4 changes: 2 additions & 2 deletions src/BulletSoftBody/btSoftBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2423,7 +2423,7 @@ void btSoftBody::pointersToIndices()
{
if (m_nodes[i].m_leaf)
{
m_nodes[i].m_leaf->data = *(void**)&i;
m_nodes[i].m_leaf->dataAsInt = i;
}
}
for (i = 0, ni = m_links.size(); i < ni; ++i)
Expand All @@ -2438,7 +2438,7 @@ void btSoftBody::pointersToIndices()
m_faces[i].m_n[2] = PTR2IDX(m_faces[i].m_n[2], base);
if (m_faces[i].m_leaf)
{
m_faces[i].m_leaf->data = *(void**)&i;
m_faces[i].m_leaf->dataAsInt = i;
}
}
for (i = 0, ni = m_anchors.size(); i < ni; ++i)
Expand Down
2 changes: 2 additions & 0 deletions src/BulletSoftBody/btSoftMultiBodyDynamicsWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ void btSoftMultiBodyDynamicsWorld::debugDrawWorld()
}
}

namespace {
struct btSoftSingleRayCallback : public btBroadphaseRayCallback
{
btVector3 m_rayFromWorld;
Expand Down Expand Up @@ -250,6 +251,7 @@ struct btSoftSingleRayCallback : public btBroadphaseRayCallback
return true;
}
};
}

void btSoftMultiBodyDynamicsWorld::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback) const
{
Expand Down
2 changes: 2 additions & 0 deletions src/BulletSoftBody/btSoftRigidDynamicsWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ void btSoftRigidDynamicsWorld::debugDrawWorld()
}
}

namespace {
struct btSoftSingleRayCallback : public btBroadphaseRayCallback
{
btVector3 m_rayFromWorld;
Expand Down Expand Up @@ -245,6 +246,7 @@ struct btSoftSingleRayCallback : public btBroadphaseRayCallback
return true;
}
};
}

void btSoftRigidDynamicsWorld::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback) const
{
Expand Down
Loading