Skip to content

Fix heap-buffer-overflow in ResidualBasedEliminationBuilderAndSolverWithConstraints#14562

Open
FrogZR wants to merge 1 commit into
KratosMultiphysics:masterfrom
FrogZR:fix/elimination-builder-reactions-equation-id-mismatch
Open

Fix heap-buffer-overflow in ResidualBasedEliminationBuilderAndSolverWithConstraints#14562
FrogZR wants to merge 1 commit into
KratosMultiphysics:masterfrom
FrogZR:fix/elimination-builder-reactions-equation-id-mismatch

Conversation

@FrogZR

@FrogZR FrogZR commented Jul 7, 2026

Copy link
Copy Markdown

Summary

Fixes #14561.

AssembleRHSWithoutConstraints looked up a DOF via
BaseType::mDofSet.begin() + i_global, treating the equation ID as if it were a
container position in mDofSet. mDofSet is sorted by DOF identity (see
SetUpDofSetWithConstraints), while equation IDs are assigned via two independent
counters (ascending for free DOFs, descending for master-fixed ones) in that same
identity-sorted iteration order — so a DOF's equation ID has no fixed relationship to
its position in mDofSet.

This fetched an arbitrary, usually wrong Dof, corrupting the is_master_fixed/
is_slave classification and the mReactionEquationIdMap lookup, causing a
heap-buffer-overflow (out-of-bounds read/write into the reactions vector) whenever
MasterSlaveConstraints, genuinely fixed DOFs, and reactions were all in use at once.

Fix

Classify purely via equation_id, using a new mReactionCategoryByEquationId map
built alongside the existing mReactionEquationIdMap in SetUpSystemWithConstraints
(which already iterates real Dof objects correctly, unlike the buggy lookup).

Test plan

🤖 Generated with Claude Code

…ithConstraints

AssembleRHSWithoutConstraints looked up a DOF via
"BaseType::mDofSet.begin() + i_global", treating the equation ID as if
it were a container position in mDofSet. mDofSet is sorted by DOF
identity (see SetUpDofSetWithConstraints), while equation IDs are
assigned via two independent counters (ascending for free DOFs,
descending for master-fixed ones) in that same identity-sorted
iteration order, so a DOF's equation ID has no fixed relationship to
its position in mDofSet.

This fetched an arbitrary, usually wrong Dof, corrupting the
is_master_fixed/is_slave classification and the mReactionEquationIdMap
lookup, causing a heap-buffer-overflow (out-of-bounds read/write into
the reactions vector) whenever MasterSlaveConstraints, genuinely fixed
DOFs, and reactions were all in use at once.

Fixed by classifying purely via equation_id, using a new
mReactionCategoryByEquationId map built alongside mReactionEquationIdMap
in SetUpSystemWithConstraints (which already iterates real Dof objects
correctly).

Confirmed with AddressSanitizer: the heap-buffer-overflow no longer
occurs, and a model that previously crashed 100% of the time (5+
consecutive repro attempts under both a Debug+ASan build and a plain
optimized Release build) now runs multiple time steps to completion.

Fixes KratosMultiphysics#14561
@FrogZR
FrogZR requested a review from a team as a code owner July 7, 2026 18:47
@loumalouomega

Copy link
Copy Markdown
Member

🤖 Generated with Claude Code

Thanks for the honesty

@loumalouomega loumalouomega left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I think this fixes the issue found by @AlejandroCornejo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Heap-buffer-overflow in ResidualBasedEliminationBuilderAndSolverWithConstraints when MasterSlaveConstraints + fixed DOFs + reactions are combined

2 participants