various recording fixes (#182)

This commit is contained in:
Erin Catto 2026-10-06 22:45:33 -07:00 • committed by GitHub
commit 3d9db79ee1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 213 additions and 73 deletions

View file

@ -19,19 +19,19 @@ set(BOX3D_SOURCE_FILES
compound.h
constraint_graph.c
constraint_graph.h
contact.c
contact.h
contact_solver_wide.inl
contact_solver.c
contact_solver.h
contact.c
contact.h
container.h
convex_manifold_wide.inl
convex_manifold.c
core.c
core.h
ctz.h
distance.c
distance_joint.c
distance.c
dynamic_tree.c
dynamic_tree.h
height_field.c
@ -47,8 +47,8 @@ set(BOX3D_SOURCE_FILES
manifold.h
math_functions.c
math_internal.h
mesh.c
mesh_contact.c
mesh.c
motor_joint.c
mover.c
name_cache.c
@ -62,13 +62,11 @@ set(BOX3D_SOURCE_FILES
prismatic_joint.c
qsort.h
rapidhash.h
recording_ops.inl
recording.c
recording.h
recording_ops.inl
recording_replay.c
recording_replay.h
world_snapshot.c
world_snapshot.h
replay.c
replay.h
revolute_joint.c
scheduler.c
scheduler.h
@ -76,17 +74,17 @@ set(BOX3D_SOURCE_FILES
sensor.h
shape.c
shape.h
simd.c
simd.h
simd_avx2.h
simd_neon.h
simd_scalar.h
simd_sse2.h
simd_wide.h
solver.c
solver.h
simd.c
simd.h
solver_set.c
solver_set.h
solver.c
solver.h
sphere.c
spherical_joint.c
table.c
@ -99,6 +97,8 @@ set(BOX3D_SOURCE_FILES
wheel_joint.c
wide4.c
wide8.c
world_snapshot.c
world_snapshot.h
box3d.natvis
)

View file

@ -563,24 +563,14 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
compound->byteCount = (int)byteCount;
compound->nodeOffset = nodeOffset;
compound->proxyOffset = proxyOffset;
memcpy( &compound->tree, &tree, sizeof( b3DynamicTree ) );
// todo clean up this mess
compound->tree.version = tree.version;
compound->tree.nodeEnd = tree.nodeEnd;
compound->tree.nodeCapacity = tree.nodeEnd;
compound->tree.pairFreeList = B3_NULL_INDEX;
compound->tree.proxyCount = tree.proxyCount;
compound->tree.proxyCapacity = tree.proxyCount;
compound->tree.proxyFreeList = B3_NULL_INDEX;
compound->tree.swapNodes = NULL;
compound->tree.leafIndices = NULL;
compound->tree.leafNodes = NULL;
compound->tree.leafBoxes = NULL;
compound->tree.leafCenters = NULL;
compound->tree.binIndices = NULL;
compound->tree.rebuildCapacity = 0;
compound->tree.nodes = NULL;
compound->tree.parents = NULL;
compound->tree.proxies = NULL;
compound->tree.dfsOrdered = tree.dfsOrdered;
compound->materialOffset = materialOffset;
compound->materialCount = materialCount;
compound->capsuleOffset = capsuleOffset;

View file

@ -189,7 +189,7 @@ void b3Free( void* mem, size_t size )
if ( b3_freeFcn != NULL )
{
size_t alignedSize = ( ( size - 1 ) | ( B3_ALIGNMENT - 1 ) ) + 1;
size_t alignedSize = size > 0 ? ( ( size - 1 ) | ( B3_ALIGNMENT - 1 ) ) + 1 : 0;
b3_freeFcn( mem, alignedSize );
}
else

View file

@ -1178,7 +1178,7 @@ uint32_t b3RecInternCompound( b3Recording* rec, const b3CompoundData* compound )
int byteCount = compound->byteCount;
uint8_t* bytes = b3Alloc( (size_t)byteCount );
memcpy( bytes, compound, (size_t)byteCount );
// Null the tree node pointer in the copy so the canonical bytes are pointer-free.
// Null the tree pointers in the copy so the canonical bytes are pointer-free.
// b3ConvertBytesToCompound fixes it back on load via nodeOffset.
b3ConvertCompoundToBytes( (b3CompoundData*)bytes );
uint64_t h = b3Hash64NonZero( bytes, byteCount );

View file

@ -5,7 +5,7 @@
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "recording_replay.h"
#include "replay.h"
#include "body.h"
#include "physics_world.h"
@ -713,6 +713,15 @@ static void b3RecCheckJointId( b3RecReader* rdr, b3JointId got, b3JointId rec )
// Registry slot reconstruction. Returns the live pointer for the given slot, building it
// on first use. The hull case is handled inline at the call site since it doesn't cache.
b3RegistrySlot* b3RecGetSlot( b3RecReader* rdr, uint32_t id, b3GeometryKind kind )
{
if ( rdr == NULL || id >= (uint32_t)rdr->slotCount || rdr->slots[id].kind != kind )
{
return NULL;
}
return rdr->slots + id;
}
static void* b3RecGetLiveMesh( b3RegistrySlot* slot )
{
// Mesh is a self-contained blob used by reference, with no pointer fixup. Hand back the pristine
@ -728,19 +737,25 @@ static void* b3RecGetLiveHeightField( b3RegistrySlot* slot )
return slot->bytes;
}
static void* b3RecGetLiveCompound( b3RegistrySlot* slot )
const b3CompoundData* b3RecGetLiveCompound( b3RegistrySlot* slot )
{
B3_ASSERT( slot->kind == b3_geometryCompound );
if ( slot->live != NULL )
{
return slot->live;
return (const b3CompoundData*)slot->live;
}
// The copy is unavoidable here: b3ConvertBytesToCompound rewrites its input in place, while the
// pristine bytes must survive for keyframe registry seeding (b3RecSeedKeyframeRegistry). So we
// keep both the serialized bytes and a separate converted live object.
slot->live = b3Alloc( (size_t)slot->byteCount );
memcpy( slot->live, slot->bytes, (size_t)slot->byteCount );
b3ConvertBytesToCompound( (uint8_t*)slot->live, slot->byteCount );
return slot->live;
uint8_t* live = (uint8_t*)b3Alloc( (size_t)slot->byteCount );
memcpy( live, slot->bytes, (size_t)slot->byteCount );
if ( b3ConvertBytesToCompound( live, slot->byteCount ) == NULL )
{
b3Free( live, (size_t)slot->byteCount );
return NULL;
}
slot->live = live;
return (const b3CompoundData*)live;
}
// Dispatch functions, one per op
@ -1015,13 +1030,13 @@ static void b3RecDispatch_CreateHullShape( const b3RecArgs_CreateHullShape* a, b
return;
}
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryHull );
if ( slot == NULL )
{
printf( "b3ReplayFile: hull geometryId %u out of range\n", id );
printf( "b3ReplayFile: hull geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
b3BodyId bodyId = b3RecMakeBodyId( rdr, a->body );
// Hull is cloned by b3CreateHullShape into the world DB; no caching needed.
b3ShapeId gotId = b3CreateHullShape( bodyId, &a->def, (const b3HullData*)slot->bytes );
@ -1036,13 +1051,13 @@ static void b3RecDispatch_CreateMeshShape( const b3RecArgs_CreateMeshShape* a, b
return;
}
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryMesh );
if ( slot == NULL )
{
printf( "b3ReplayFile: mesh geometryId %u out of range\n", id );
printf( "b3ReplayFile: mesh geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
const b3MeshData* mesh = b3RecGetLiveMesh( slot );
b3BodyId bodyId = b3RecMakeBodyId( rdr, a->body );
b3ShapeId gotId = b3CreateMeshShape( bodyId, &a->def, mesh, a->scale );
@ -1057,13 +1072,13 @@ static void b3RecDispatch_CreateHeightFieldShape( const b3RecArgs_CreateHeightFi
return;
}
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryHeightField );
if ( slot == NULL )
{
printf( "b3ReplayFile: heightfield geometryId %u out of range\n", id );
printf( "b3ReplayFile: heightfield geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
const b3HeightFieldData* hf = (const b3HeightFieldData*)b3RecGetLiveHeightField( slot );
if ( hf == NULL )
{
@ -1084,14 +1099,20 @@ static void b3RecDispatch_CreateCompoundShape( const b3RecArgs_CreateCompoundSha
return;
}
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryCompound );
if ( slot == NULL )
{
printf( "b3ReplayFile: compound geometryId %u out of range\n", id );
printf( "b3ReplayFile: compound geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
const b3CompoundData* compound = b3RecGetLiveCompound( slot );
if ( compound == NULL )
{
printf( "b3ReplayFile: compound geometry %u is corrupt\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
const b3CompoundData* compound = (const b3CompoundData*)b3RecGetLiveCompound( slot );
b3BodyId bodyId = b3RecMakeBodyId( rdr, a->body );
// b3CreateCompoundShape takes a non-const def pointer; cast away const for the scratch def
b3ShapeDef shapeDef = a->def;
@ -1172,13 +1193,13 @@ static void b3RecDispatch_ShapeSetCapsule( const b3RecArgs_ShapeSetCapsule* a, b
static void b3RecDispatch_ShapeSetHull( const b3RecArgs_ShapeSetHull* a, b3RecReader* rdr )
{
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryHull );
if ( slot == NULL )
{
printf( "b3ReplayFile: hull geometryId %u out of range\n", id );
printf( "b3ReplayFile: hull geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
b3ShapeId shapeId = b3RecMakeShapeId( rdr, a->shape );
b3Shape_SetHull( shapeId, (const b3HullData*)slot->bytes );
}
@ -1186,13 +1207,13 @@ static void b3RecDispatch_ShapeSetHull( const b3RecArgs_ShapeSetHull* a, b3RecRe
static void b3RecDispatch_ShapeSetMesh( const b3RecArgs_ShapeSetMesh* a, b3RecReader* rdr )
{
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryMesh );
if ( slot == NULL )
{
printf( "b3ReplayFile: mesh geometryId %u out of range\n", id );
printf( "b3ReplayFile: mesh geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
b3ShapeId shapeId = b3RecMakeShapeId( rdr, a->shape );
const b3MeshData* mesh = b3RecGetLiveMesh( slot );
b3Shape_SetMesh( shapeId, mesh, a->scale );
@ -2485,9 +2506,18 @@ static bool b3RecLoadSlots( b3RecReader* rdr, const void* data, int size, uint64
uint8_t kind = rp[0];
uint32_t byteCount = (uint32_t)rp[1] | ( (uint32_t)rp[2] << 8 ) | ( (uint32_t)rp[3] << 16 ) | ( (uint32_t)rp[4] << 24 );
rp += 5;
const char* error = NULL;
if ( rp + byteCount > dataEnd )
{
printf( "b3ReplayFile: registry entry %u bytes out of bounds\n", i );
error = "bytes out of bounds";
}
else if ( kind == b3_geometryCompound && byteCount < (uint32_t)sizeof( b3CompoundData ) )
{
error = "too small for a compound";
}
if ( error != NULL )
{
printf( "b3ReplayFile: registry entry %u %s\n", i, error );
for ( uint32_t j = 0; j < i; ++j )
{
if ( slots[j].bytes != NULL )

View file

@ -256,3 +256,9 @@ b3PlaneResult b3RecR_PLANERESULT( b3RecReader* rdr );
// Grow the reader's hit scratch to at least n entries, preserving contents. n is bounded by the
// file size since every recorded hit consumes at least one byte.
void b3RecEnsureHits( b3RecReader* rdr, int n );
// Get a geometry slot with kind validation.
b3RegistrySlot* b3RecGetSlot( b3RecReader* rdr, uint32_t id, b3GeometryKind kind );
// Get the recorded compound with the pointers fixed up.
const b3CompoundData* b3RecGetLiveCompound( b3RegistrySlot* slot );

View file

@ -725,13 +725,13 @@ static void b3DesShapes( b3SnapReader* r, b3World* world, b3RecReader* rdr )
{
break;
}
if ( rdr == NULL || gid >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, gid, b3_geometryHull );
if ( slot == NULL )
{
r->ok = false;
break;
}
// Hull is cloned into the world DB; pass raw bytes directly
b3RegistrySlot* slot = rdr->slots + gid;
dst->hull = b3AddHullToDatabase( world, (const b3HullData*)slot->bytes );
break;
}
@ -744,12 +744,12 @@ static void b3DesShapes( b3SnapReader* r, b3World* world, b3RecReader* rdr )
{
break;
}
if ( rdr == NULL || gid >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, gid, b3_geometryMesh );
if ( slot == NULL )
{
r->ok = false;
break;
}
b3RegistrySlot* slot = rdr->slots + gid;
// Mesh is a self-contained blob used by reference; point straight at the pristine bytes.
dst->mesh.data = (const b3MeshData*)slot->bytes;
dst->mesh.scale = scale;
@ -762,12 +762,12 @@ static void b3DesShapes( b3SnapReader* r, b3World* world, b3RecReader* rdr )
{
break;
}
if ( rdr == NULL || gid >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, gid, b3_geometryHeightField );
if ( slot == NULL )
{
r->ok = false;
break;
}
b3RegistrySlot* slot = rdr->slots + gid;
// Self-contained blob used by reference; point straight at the pristine bytes.
dst->heightField = (const b3HeightFieldData*)slot->bytes;
break;
@ -779,19 +779,12 @@ static void b3DesShapes( b3SnapReader* r, b3World* world, b3RecReader* rdr )
{
break;
}
if ( rdr == NULL || gid >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, gid, b3_geometryCompound );
dst->compound = slot != NULL ? b3RecGetLiveCompound( slot ) : NULL;
if ( dst->compound == NULL )
{
r->ok = false;
break;
}
b3RegistrySlot* slot = rdr->slots + gid;
if ( slot->live == NULL )
{
slot->live = b3Alloc( (size_t)slot->byteCount );
memcpy( slot->live, slot->bytes, (size_t)slot->byteCount );
b3ConvertBytesToCompound( (uint8_t*)slot->live, slot->byteCount );
}
dst->compound = (const b3CompoundData*)slot->live;
break;
}
default:

View file

@ -4,7 +4,7 @@
#pragma once
#include "recording.h"
#include "recording_replay.h"
#include "replay.h"
#include <stdbool.h>
#include <stdint.h>

View file

@ -855,6 +855,22 @@ static float QueryReplayCastFcn( b3ShapeId shapeId, b3Pos point, b3Vec3 normal,
return fraction;
}
static float CompoundHitCastFcn( b3ShapeId shapeId, b3Pos point, b3Vec3 normal, float fraction, uint64_t userMaterialId,
int triangleIndex, int childIndex, void* context )
{
(void)point;
(void)normal;
(void)fraction;
(void)userMaterialId;
(void)triangleIndex;
(void)childIndex;
if ( b3Shape_GetType( shapeId ) == b3_compoundShape )
{
*(bool*)context = true;
}
return 1.0f;
}
static bool QueryReplayPlaneFcn( b3ShapeId shapeId, const b3PlaneResult* planes, int planeCount, void* context )
{
(void)shapeId;
@ -1672,6 +1688,27 @@ static int AllOps( void )
ENSURE( b3RecPlayer_GetFrame( player ) == 12 );
ENSURE( b3RecPlayer_IsAtEnd( player ) );
ENSURE( b3RecPlayer_HasDiverged( player ) == false );
ENSURE( b3RecPlayer_GetKeyframeBytes( player ) > 0 );
// Seek back onto the frame 4 keyframe, then skip forward onto the frame 8 keyframe. Each
// restore rebuilds the compound from the registry, so a ray must still reach its tree and
// the state hashes must hold through the re-stepped frames.
int seekTargets[] = { 6, 9 };
for ( int k = 0; k < 2; ++k )
{
b3RecPlayer_SeekFrame( player, seekTargets[k] );
ENSURE( b3RecPlayer_GetFrame( player ) == seekTargets[k] );
bool hitCompound = false;
b3World_CastRay( b3RecPlayer_GetWorldId( player ), (b3Pos){ 30.0f, 10.0f, 0.0f }, (b3Vec3){ 0.0f, -20.0f, 0.0f },
b3DefaultQueryFilter(), CompoundHitCastFcn, &hitCompound );
ENSURE( hitCompound );
}
while ( b3RecPlayer_StepFrame( player ) )
{
}
ENSURE( b3RecPlayer_GetFrame( player ) == 12 );
ENSURE( b3RecPlayer_HasDiverged( player ) == false );
// The trailing DestroyWorld is an end marker; the world stays valid after end
ENSURE( b3World_IsValid( b3RecPlayer_GetWorldId( player ) ) );
@ -1837,6 +1874,89 @@ static int ReservedHeaderBytes( void )
return 0;
}
// A corrupt compound in the geometry registry must fail the replay cleanly. A stale version or a
// wrong kind is rejected when the shape is created and a truncated entry is rejected at load.
static int CorruptCompound( void )
{
b3CompoundSphereDef sphereDef;
sphereDef.sphere = (b3Sphere){ { 0.0f, 0.0f, 0.0f }, 1.0f };
sphereDef.material = b3DefaultSurfaceMaterial();
b3CompoundDef compoundDef;
memset( &compoundDef, 0, sizeof( compoundDef ) );
compoundDef.spheres = &sphereDef;
compoundDef.sphereCount = 1;
b3CompoundData* compound = b3CreateCompound( &compoundDef );
ENSURE( compound != NULL );
int compoundByteCount = compound->byteCount;
b3Recording* rec = b3CreateRecording( 0 );
ENSURE( rec != NULL );
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3World_StartRecording( worldId, rec );
b3BodyDef bd = b3DefaultBodyDef();
b3BodyId groundId = b3CreateBody( worldId, &bd );
b3ShapeDef sd = b3DefaultShapeDef();
b3CreateBakedCompoundShape( groundId, &sd, compound );
bd.type = b3_dynamicBody;
bd.position = (b3Pos){ 0.0f, 3.0f, 0.0f };
b3BodyId bodyId = b3CreateBody( worldId, &bd );
b3Sphere s = { { 0.0f, 0.0f, 0.0f }, 0.5f };
b3CreateSphereShape( bodyId, &sd, &s );
for ( int i = 0; i < 10; ++i )
{
b3World_Step( worldId, 1.0f / 60.0f, 4 );
}
b3World_StopRecording( worldId );
b3DestroyWorld( worldId );
b3DestroyCompound( compound );
const uint8_t* recData = b3Recording_GetData( rec );
int recSize = b3Recording_GetSize( rec );
ENSURE( b3ValidateReplay( recData, recSize, 1 ) );
// The registry entry is a kind byte and a 4 byte length, then the blob, which leads with its version
uint64_t version = B3_COMPOUND_VERSION;
int blobOffset = -1;
for ( int i = 5; i + (int)sizeof( version ) <= recSize; ++i )
{
if ( memcmp( recData + i, &version, sizeof( version ) ) == 0 )
{
blobOffset = i;
break;
}
}
ENSURE( blobOffset > 0 );
ENSURE( recData[blobOffset - 5] == (uint8_t)b3_geometryCompound );
int32_t storedByteCount;
memcpy( &storedByteCount, recData + blobOffset - 4, sizeof( storedByteCount ) );
ENSURE( storedByteCount == compoundByteCount );
uint8_t* patched = (uint8_t*)b3Alloc( (size_t)recSize );
memcpy( patched, recData, (size_t)recSize );
patched[blobOffset] ^= 0xFF;
ENSURE( b3ValidateReplay( patched, recSize, 1 ) == false );
memcpy( patched, recData, (size_t)recSize );
int32_t shortByteCount = 8;
memcpy( patched + blobOffset - 4, &shortByteCount, sizeof( shortByteCount ) );
ENSURE( b3CreatePlayer( patched, recSize, 1 ) == NULL );
memcpy( patched, recData, (size_t)recSize );
patched[blobOffset - 5] = (uint8_t)b3_geometryMesh;
ENSURE( b3ValidateReplay( patched, recSize, 1 ) == false );
b3Free( patched, (size_t)recSize );
b3DestroyRecording( rec );
return 0;
}
// Geometry that shares a content hash but differs in bytes must dedup exactly. Mirrors the keyframe
// flow that crashed: the seed appends every slot 1:1 (even byte-identical duplicates a hash collision
// left in an already-recorded file), then capture must resolve a live blob back to an existing slot
@ -2265,5 +2385,6 @@ int RecordingTest( void )
RUN_SUBTEST( GeometryMutatorReplay );
RUN_SUBTEST( AllOps );
RUN_SUBTEST( ReservedHeaderBytes );
RUN_SUBTEST( CorruptCompound );
return 0;
}