Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions news/133.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* Removed scatterers, scattering powers and molecule sub-objects are now deleted instead of leaked, unless still referenced from Python.

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Fix crashes (use-after-free, double free) and stale wrappers when ObjCryst++ deletes objects still referenced from Python (#121, #132).

**Security:**

* <news item>
3 changes: 0 additions & 3 deletions src/extensions/helpers_nb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,19 +315,16 @@ inline void refobj_fwd_add_par_obj(ObjCryst::RefinableObj& obj, nb::object o, co

inline ObjCryst::RefinablePar& refobj_fwd_get_par_long(ObjCryst::RefinableObj& obj, const long i)
{
obj.SetDeleteRefParInDestructor(0);
return obj.GetPar(i);
}

inline ObjCryst::RefinablePar& refobj_fwd_get_par_string(ObjCryst::RefinableObj& obj, const std::string& s)
{
obj.SetDeleteRefParInDestructor(0);
return obj.GetPar(s);
}

inline ObjCryst::RefinablePar& refobj_fwd_get_par_not_fixed(ObjCryst::RefinableObj& obj, const long i)
{
obj.SetDeleteRefParInDestructor(0);
return obj.GetParNotFixed(i);
}

Expand Down
13 changes: 11 additions & 2 deletions src/extensions/nb_crystal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void _RemoveScatterer(Crystal& crystal, nb::handle obj)
}
}
if (!scatt) throw nb::value_error("Cannot remove nonexistent Scatterer");
crystal.RemoveScatterer(scatt, false);
crystal.RemoveScatterer(scatt, true);
}

Scatterer& _GetScattByIndex(Crystal& crystal, int idx)
Expand Down Expand Up @@ -155,7 +155,16 @@ void _RemoveScatteringPower(Crystal& crystal, nb::handle obj)
throw nb::type_error("RemoveScatteringPower: argument must be a ScatteringPower instance");
found:
if (!sp) throw nb::value_error("Cannot remove nonexistent ScatteringPower");
crystal.RemoveScatteringPower(sp, false);
// Only release the scattering power if no scatterer still refers to it,
// otherwise it is kept (and leaked) as before.
bool in_use = false;
const ScatteringComponentList& scl = crystal.GetScatteringComponentList();
for (long i = 0; i < scl.GetNbComponent(); ++i)
if (scl(i).mpScattPow == sp) in_use = true;
const ObjRegistry<RefinableObj>& clients = sp->GetClientRegistry();
for (long i = 0; i < clients.GetNb(); ++i)
if (&clients.GetObj(i) != static_cast<RefinableObj*>(&crystal)) in_use = true;
crystal.RemoveScatteringPower(sp, !in_use);
}

nb::list _GetScatteringComponentList(Crystal& c)
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/nb_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <ObjCryst/ObjCryst/General.h>
#include <ObjCryst/RefinableObj/RefinableObj.h>

#include "nb_release_hooks.hpp"

namespace nb = nanobind;

// Forward declarations for all wrap_* functions
Expand Down Expand Up @@ -133,4 +135,7 @@ NB_MODULE(_pyobjcryst, m)
wrap_radiation(m);
wrap_zatom(m);
wrap_zpolyhedron(m);

// Let Python wrappers own sub-objects released by ObjCryst
install_release_hooks();
}
43 changes: 34 additions & 9 deletions src/extensions/nb_molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <nanobind/stl/string.h>
#include <nanobind/make_iterator.h>

#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
Expand All @@ -16,6 +17,7 @@
#include <ObjCryst/ObjCryst/ScatteringPower.h>

#include "helpers_nb.hpp"
#include "nb_release_hooks.hpp"

namespace nb = nanobind;
using namespace ObjCryst;
Expand Down Expand Up @@ -100,6 +102,29 @@ MolAtom& _GetAtomIdx(Molecule& m, int idx)
return m.GetAtom(i);
}

// Molecule::RemoveAtom only unlists the atom's x/y/z parameters, whose
// values live in the atom. Release them as well, so that any Python wrapper
// of these parameters is invalidated instead of dangling.
void _RemoveAtom(Molecule& m, MolAtom& atom)
{
RefinablePar* pars[3] = {nullptr, nullptr, nullptr};
if (std::find(m.GetAtomList().begin(), m.GetAtomList().end(), &atom)
!= m.GetAtomList().end())
{
pars[0] = &m.GetPar(&atom.X());
pars[1] = &m.GetPar(&atom.Y());
pars[2] = &m.GetPar(&atom.Z());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

are there any other atom attributes that have be treated the same way, such as ADPs and occupancies?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

In a Molecule we need this for the xyz atom coordinates because they are used as refinable parameters, so there are references pointing to them in the Molecule (not in a MolAtom because that class is not a RefinableObj). That's not the case for other parameters.
For ADPs these belong inside a ScatteringPower, and handled properly there.

Of course this is still a hook, the correct longer-term fix would be to implement reference counting in objcryst.

}
// RemoveAtom does not update rigid groups: keep (leak) the atom if one
// still refers to it.
bool del = true;
for (const RigidGroup* rg : m.GetRigidGroupList())
if (rg->count(&atom) != 0) del = false;
KeepRestraintAtomsAlive guard;
m.RemoveAtom(atom, del);
for (RefinablePar* p : pars) ReleaseObj(p);
}

MolAtom* _FindAtom(Molecule& m, const std::string& name)
{
auto ii = m.FindAtom(name);
Expand Down Expand Up @@ -359,8 +384,8 @@ void wrap_molecule(nb::module_& m)
nb::arg("x"), nb::arg("y"), nb::arg("z"), nb::arg("pPow").none(),
nb::arg("name"), nb::arg("updateDisplay") = true,
nb::keep_alive<1,5>(), nb::rv_policy::reference_internal)
.def("RemoveAtom", [](Molecule& mol, MolAtom& a){ mol.RemoveAtom(a, false); })
.def("RemoveAtom", [](Molecule& mol, int idx){ mol.RemoveAtom(_GetAtomIdx(mol, idx), false); })
.def("RemoveAtom", &_RemoveAtom)
.def("RemoveAtom", [](Molecule& mol, int idx){ _RemoveAtom(mol, _GetAtomIdx(mol, idx)); })
.def("AddBond", &_AddBond,
nb::arg("atom1"), nb::arg("atom2"), nb::arg("length"),
nb::arg("sigma"), nb::arg("delta"), nb::arg("bondOrder") = 1.,
Expand All @@ -379,8 +404,8 @@ void wrap_molecule(nb::module_& m)
// the Molecule that returned it, so nothing here can dangle
// once the redundant direction is removed.
nb::rv_policy::reference_internal)
.def("RemoveBond", [](Molecule& mol, const MolBond& b){ mol.RemoveBond(b, false); })
.def("RemoveBond", [](Molecule& mol, int idx){ mol.RemoveBond(_GetBondIdx(mol, idx), false); })
.def("RemoveBond", [](Molecule& mol, const MolBond& b){ KeepRestraintAtomsAlive g; mol.RemoveBond(b, true); })
.def("RemoveBond", [](Molecule& mol, int idx){ KeepRestraintAtomsAlive g; mol.RemoveBond(_GetBondIdx(mol, idx), true); })
.def("GetBond", &_GetBondIdx, nb::rv_policy::reference_internal)
.def("FindBond", &_FindBond)
.def("AddBondAngle", &_AddBondAngle,
Expand All @@ -390,8 +415,8 @@ void wrap_molecule(nb::module_& m)
// See the comment on AddBond above -- same reasoning applies to
// atom1/atom2/atom3 here (§10).
nb::rv_policy::reference_internal)
.def("RemoveBondAngle", [](Molecule& mol, MolBondAngle& ba){ mol.RemoveBondAngle(ba, false); })
.def("RemoveBondAngle", [](Molecule& mol, int idx){ mol.RemoveBondAngle(_GetBondAngleIdx(mol, idx), false); })
.def("RemoveBondAngle", [](Molecule& mol, MolBondAngle& ba){ KeepRestraintAtomsAlive g; mol.RemoveBondAngle(ba, true); })
.def("RemoveBondAngle", [](Molecule& mol, int idx){ KeepRestraintAtomsAlive g; mol.RemoveBondAngle(_GetBondAngleIdx(mol, idx), true); })
.def("GetBondAngle", &_GetBondAngleIdx, nb::rv_policy::reference_internal)
.def("FindBondAngle", &_FindBondAngle)
.def("AddDihedralAngle", &_AddDihedralAngle,
Expand All @@ -401,8 +426,8 @@ void wrap_molecule(nb::module_& m)
// See the comment on AddBond above -- same reasoning applies to
// atom1..atom4 here (§10).
nb::rv_policy::reference_internal)
.def("RemoveDihedralAngle", [](Molecule& mol, MolDihedralAngle& da){ mol.RemoveDihedralAngle(da, false); })
.def("RemoveDihedralAngle", [](Molecule& mol, int idx){ mol.RemoveDihedralAngle(_GetDihedralAngleIdx(mol, idx), false); })
.def("RemoveDihedralAngle", [](Molecule& mol, MolDihedralAngle& da){ KeepRestraintAtomsAlive g; mol.RemoveDihedralAngle(da, true); })
.def("RemoveDihedralAngle", [](Molecule& mol, int idx){ KeepRestraintAtomsAlive g; mol.RemoveDihedralAngle(_GetDihedralAngleIdx(mol, idx), true); })
.def("GetDihedralAngle", &_GetDihedralAngleIdx, nb::rv_policy::reference_internal)
.def("FindDihedralAngle", &_FindDihedralAngle)
.def("AddRigidGroup",
Expand All @@ -413,7 +438,7 @@ void wrap_molecule(nb::module_& m)
[](Molecule& mol, nb::object& group, const bool updateDisplay) -> RigidGroup& { return _AddRigidGroupIterable(mol, group, updateDisplay); },
nb::arg("group"), nb::arg("updateDisplay") = true,
nb::rv_policy::reference_internal)
.def("RemoveRigidGroup", [](Molecule& mol, RigidGroup& rg, const bool ud){ mol.RemoveRigidGroup(rg, ud, false); },
.def("RemoveRigidGroup", [](Molecule& mol, RigidGroup& rg, const bool ud){ KeepRestraintAtomsAlive g; mol.RemoveRigidGroup(rg, ud, true); },
nb::arg("group"), nb::arg("updateDisplay") = true)
.def("GetAtom", [](Molecule& mol, int idx) -> MolAtom& { return _GetAtomIdx(mol, idx); }, nb::rv_policy::reference_internal)
.def("GetAtom", [](Molecule& mol, const std::string& name) -> MolAtom& { return *_GetAtomByName(mol, name); }, nb::rv_policy::reference_internal)
Expand Down
8 changes: 4 additions & 4 deletions src/extensions/nb_refinableobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
*
* Changes from ObjCryst::RefinableObj:
* - XMLOutput and XMLInput accept python file-like objects or strings
* - SetDeleteRefParInDestructor(false) called in constructors
* - SetDeleteRefParInDestructor(false) called by AddPar, since an added
* parameter may be shared with another object
* - Wrappers of parameters deleted by ObjCryst are invalidated by the
* release hooks (nb_release_hooks.cpp)
* - GetParamSet returns a copy
* - RemovePar returns None
*/
Expand Down Expand Up @@ -81,19 +84,16 @@ void _AddParObj(RefinableObj& obj, nb::object o, const bool copyParam = false)

RefinablePar& _GetParLong(RefinableObj& obj, const long i)
{
obj.SetDeleteRefParInDestructor(0);
return obj.GetPar(i);
}

RefinablePar& _GetParString(RefinableObj& obj, const std::string& s)
{
obj.SetDeleteRefParInDestructor(0);
return obj.GetPar(s);
}

RefinablePar& _GetParNotFixed(RefinableObj& obj, const long i)
{
obj.SetDeleteRefParInDestructor(0);
return obj.GetParNotFixed(i);
}

Expand Down
Loading
Loading