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: Unable to Change CharacterComponent's collider shape properties without causing crash #2428

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions sources/engine/Stride.Physics/Elements/CharacterComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,54 @@ protected override void OnAttach()
Simulation.AddCharacter(this, (CollisionFilterGroupFlags)CollisionGroup, CanCollideWith);
}

/// <summary>
/// Reconstruct of character controller to avoid possible UAF issues
/// Context: On ColliderShape changes, when ComposeShape is ran to rebuild ColliderShape properties, disposing the old ColliderShape can cause UAF
/// issues inside KinematicCharacter and inside Simulation discreteDynamicWorld due to old disposed references to the native object.
/// </summary>
public override void ComposeShape()
{
//Disposing of ColliderShape should happen before we remove NativeCollisionObject and KinematicCharacter from Simulation
base.ComposeShape();

//make PairCachingGhostObject references in KinematicCharacter valid, therefore make new instance of kinematic character controller with
//updated NativeCollisionObject keep references valid
if (KinematicCharacter != null)
{
//very mediocre workaround to avoid the nullref when we remove character
Simulation simRef = Simulation;

//remove references in discreteDynamicsWorld
Simulation.RemoveCharacter(this);
Simulation = simRef;

//destroy and deref KinematicCharacter then reconstruct it
BulletSharp.KinematicCharacterController kinematicCharacterProperties = KinematicCharacter;
KinematicCharacter.Dispose();
KinematicCharacter = null;
BulletSharp.Math.Vector3 unitY = new BulletSharp.Math.Vector3(0f, 1f, 0f);
//Make new KinematicCharacter
KinematicCharacter = new BulletSharp.KinematicCharacterController((BulletSharp.PairCachingGhostObject)NativeCollisionObject, (BulletSharp.ConvexShape)ColliderShape.InternalShape, StepHeight, ref unitY);
OverrideKinematicCharacterValues(kinematicCharacterProperties);

//now we can add these references BACK in Simulation's discreteDynamicsWorld
Simulation.AddCharacter(this, (CollisionFilterGroupFlags)CollisionGroup, CanCollideWith);
}
Eideren marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// When we reconstruct our KinematicCharacter, we would want to preserve some physics properties. Try to have the
/// physics of the object try to match as close as possible.
/// </summary>
/// <param name="oldController"></param>
private void OverrideKinematicCharacterValues(BulletSharp.KinematicCharacterController oldController)
{
KinematicCharacter.FallSpeed = oldController.FallSpeed;
KinematicCharacter.Gravity = oldController.Gravity;
KinematicCharacter.JumpSpeed = oldController.JumpSpeed;
KinematicCharacter.LinearVelocity = oldController.LinearVelocity;
}

protected override void OnDetach()
{
if (KinematicCharacter == null) return;
Expand Down
5 changes: 4 additions & 1 deletion sources/engine/Stride.Physics/Engine/PhysicsComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,10 @@ public void UpdatePhysicsTransformation(bool forceUpdateTransform = true)
}
}

public void ComposeShape()
/// <summary>
/// Made virtual for added behavior in CharacterComponent with UAF exception
/// </summary>
public virtual void ComposeShape()
{
ColliderShapeChanged = false;

Expand Down