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

[SofaConstraint] fix segfault in GenericConstraintSolver #2265

Merged

Conversation

EulalieCoevoet
Copy link
Contributor

This PR fixes a bug introduced in #2225. It's basically just a revert.
Half of my scenes were crashing :(


By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).


Reviewers will merge this pull-request only if

  • it builds with SUCCESS for all platforms on the CI.
  • it does not generate new warnings.
  • it does not generate new unit test failures.
  • it does not generate new scene test failures.
  • it does not break API compatibility.
  • it is more than 1 week old (or has fast-merge label).

@EulalieCoevoet EulalieCoevoet added the issue: bug (major) Critical bug affecting everyone: not working, performances or accuracy degraded label Jul 23, 2021
Comment on lines 623 to 631
for(unsigned int i=0; i<constraintsResolutions.size(); i++)
{
delete constraintsResolution;
if (constraintsResolutions[i] != nullptr)
{
delete constraintsResolutions[i];
constraintsResolutions[i] = nullptr;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe if we want to keep the modern for-loop, the fix would be like this: (cannot make a suggestion)

    for(auto* constraintsResolution : constraintsResolutions)
    {
        if (constraintsResolution != nullptr)
        {
            delete constraintsResolution;
            constraintsResolution = nullptr;
        }
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure it would do it. If I am not wrong, constraintsResolution is a copy of a pointer, and not the actual element of the vector. See my suggestion in my review.

Copy link
Contributor

@alxbilger alxbilger Jul 26, 2021

Choose a reason for hiding this comment

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

Also, nullptr checking is not necessary. Deallocation of a null pointer does nothing (see https://en.cppreference.com/w/cpp/memory/new/operator_delete)

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure it would do it. If I am not wrong, constraintsResolution is a copy of a pointer, and not the actual element of the vector. See my suggestion in my review.

nice catch 👍

@fredroy
Copy link
Contributor

fredroy commented Jul 26, 2021

@EulalieCoevoet Thank you for reporting the error (too bad it skipped the reviews 😕)
By any chance, would have a simple scene to submit as a test? thanks ! 😙

@fredroy fredroy added pr: fast merge Minor change that can be merged without waiting for the 7 review days pr: fix Fix a bug pr: status to review To notify reviewers to review this pull-request labels Jul 26, 2021
Copy link
Contributor

@alxbilger alxbilger left a comment

Choose a reason for hiding this comment

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

I was surprised by this fix, but it is explained by the fact that this function is not only used in the destructor. When it is not in the destructor, the std::vector is not cleared, but resized, and the pointers remain. Since they were not set to nullptr, it leads to pointer dereference on an invalid address.

Note that I prefer the range-based for loop. It would look like:

for(auto*& constraintsResolution : constraintsResolutions)
{
    delete constraintsResolution;
    constraintsResolution = nullptr;
}

to be tested.

Alternatively, we could keep the current implementation and clear constraintsResolutions after the loop.

I am surprised that the CI did not catch this problem. I suggest to add a unit test for it. I can do it, but I need a minimal example leading to a crash.

It seems to be a major source of crash. @fredroy @guparan would a patch for v21.06 be conceivable?

@pedroperrusi
Copy link
Contributor

I confirm this version works in my scenes:

Note that I prefer the range-based for loop. It would look like:

for(auto*& constraintsResolution : constraintsResolutions)
{
    delete constraintsResolution;
    constraintsResolution = nullptr;
}

The alternative of clearing the vector after the loop also seems to work well:

for (auto*& constraintsResolution : constraintsResolutions)
    {
        delete constraintsResolution;
    }
constraintsResolutions.clear();

It is a pretty important bug to fix, half of my scenes are also crashing.
Thanks for finding the bug and the solution ideas

@EulalieCoevoet
Copy link
Contributor Author

I'm sorry I can't make a minimal example. I tried in vain, all the crashing scenes I found are using private plugins.
@pedroperrusi is it the same for you?

@pedroperrusi
Copy link
Contributor

@EulalieCoevoet, same for me, unfortunately.... all my scenes crashing are using private components

@guparan guparan removed the issue: bug (major) Critical bug affecting everyone: not working, performances or accuracy degraded label Jul 28, 2021
@guparan guparan merged commit 46e7e2c into sofa-framework:master Jul 28, 2021
@fredroy fredroy added the pr: backport todo This PR will be backported into the release preceeding its milestone. label Jul 28, 2021
@guparan guparan added this to the v21.12 milestone Jul 28, 2021
@hugtalbot
Copy link
Contributor

I am catching up with all PRs of the last four weeks.
@pedroperrusi I would be awesome if you could contribute a test ensuring that we detect the possible problem induced. Would you agree?

guparan pushed a commit that referenced this pull request Sep 23, 2021
* [SofaConstraint] fix segfault in GenericConstraintSolver (bug introduced in PR#2225)

* [SofaConstraint] range based for loop

Co-authored-by: EulalieCoevoet <>
Co-authored-by: alxbilger <alxbilger@users.noreply.github.com>
@guparan guparan added pr: backport done This PR has been backported into the release before its milestone. and removed pr: backport todo This PR will be backported into the release preceeding its milestone. labels Oct 4, 2021
@EulalieCoevoet EulalieCoevoet deleted the fixGenericConstraintSolver branch March 22, 2022 20:28
@hugtalbot hugtalbot added pr: status ready Approved a pull-request, ready to be squashed and removed pr: status to review To notify reviewers to review this pull-request labels May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: backport done This PR has been backported into the release before its milestone. pr: fast merge Minor change that can be merged without waiting for the 7 review days pr: fix Fix a bug pr: status ready Approved a pull-request, ready to be squashed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants