Repository navigation
Conversation
…BehaviourData The arrays of a BehaviourData were given without check to the behaviour passed to integrate and integrate_debug. A behaviour needing other arrays, or the same behaviour for another hypothesis, read and wrote out of bounds: segmentation fault with Gurson on the data of Elasticity, heap buffer overflow with Norton in 3D on data in plane strain. An error is now raised if the sizes of the arrays differ from what the behaviour and its hypothesis need. The behaviours are not compared, since loading the same behaviour twice is legitimate.
A buffer object such as an array.array is accepted as external storage, but the ndarray holding its buffer export was destroyed when the conversion returned: only the object itself was kept. Extending it then reallocated the values while the manager still used them, which gave a use after free in the integration. The converted array is now what the clean-up hook and the bind and useExternalArrayOf methods keep. While it is alive, the object refuses to be resized with a BufferError.
With EXTERNAL_STORAGE and NOUPDATE, a read-only array was assumed to be the given object or a view of it. An object whose __array__ method returns a new read-only array breaks this assumption: this array owned the values and was destroyed at return, so the manager used freed memory. This case now goes through the check of mgis_convert_to_mutable_span, shared in a new function. The error message no longer assumes that values are written.
The buffer of the messages of the behaviours holds 512 bytes: a longer message may be cut in the middle of a multibyte character, and the message of a behaviour may not be valid UTF-8. Reading BehaviourData.error_message or BehaviourIntegrationResult.error_message then raised UnicodeDecodeError, and the message was lost.
…scalars Only float and int were treated as scalars. A numpy scalar other than np.float64, a Decimal, a Fraction or a 0D array went to the path of arrays and raised "expected one dimensional array", whereas all the other setters accept them.
…taManager A member function pointer bound without named argument lets None through as self, as a null pointer: allocateArrayOfTangentOperatorBlocks, releaseArrayOfTangentOperatorBlocks, allocateArrayOfSpeedOfSounds, releaseArrayOfSpeedOfSounds and setThreadSafe ended in a segmentation fault. These methods have no argument to name, except setThreadSafe: they are now bound through a lambda taking a reference.
…n success When nothing failed, n holds the largest value of the size type (18446744073709551615 on a 64-bit system), which the docstring did not say.
For an initializer, an empty array means that no array is given. An empty array given to a bind method was thus silently replaced by an internal storage: the array of the caller was not used, without any error.
"use and externally defined array" and "spped of sounds".
The closing parenthesis was missing in the docstrings of parameters and integer_parameters, and the latter referred to a property which does not exist: integer_params instead of iparams.
"setExternalSateVariable" and "1expected", in a message reaching python.
The message of the KeyError was not formatted: it held '{}' instead of the
name of the variable.
A bare except around the import of mfront.database also caught KeyboardInterrupt and any other error, and the message was printed on the standard output of every program importing mgis.
type(value) in [int, float] sent np.float64 and the other numpy scalars to compute_on_quadrature, which failed with an AttributeError on ufl_shape. This applies to material properties and external state variables.
The three overloads of wrapInNumPyArray taking a std::variant have no caller, and <iostream> is not used. This header is only used by the sources of the bindings: it is not installed.
Entry of the issue 444 in the issues fixed by version 3.2.
|
[AI-assisted] /!\ not ready to merge yet. @thelfer the commits of this PR are mechanical. The points below remain open, each with a proposal. "Tried" means that the proposal was built and run on a local copy of this branch, without being committed. The FEniCS proposals could only be read, since FEniCS can't run here. Two consequences of this PR to check
Crashes and memory errors still reachableView of a data with another behaviour. The check of this PR applies to a Proposal: Array bound through Proposal: add a Number of integration points. Proposal: check the multiplication in the two constructors of Allocation failure. With the default build, a failed allocation while copying an array given with Proposal: put these constructions of
Proposal: nothing more if the view proposal above is accepted, since Deadlock with pybind11 older than 2.12. The first use of an array wrapped by pybind11 from several python threads can hang: 7 runs out of 8 with pybind11 2.11.1, the version of Ubuntu 24.04. No hang in 10 runs with 3.1.0. CMake accepts any version. (S-32) Proposal: build one empty Unaligned arrays. Proposal: refuse them in Arrays kept for too long
Proposal: they take an optional Parts of the python API which can't be used
Proposal: remove the helper. Add
Proposal: add a Array without storage mode. Proposal: add an overload taking a Missing bindings. Part of the public C++ API has no binding. There is no way to know from python what a state holds, and python callers compute the sizes of the post-processings by hand. (S-27) Proposal: bind what has a use in python, all thin wrappers:
Leave out the rest, with no need in python so far: FEniCS bindingsAutomatic compilation. The bare Proposal: Integer parameters. Proposal: choose the setter by name, with Error message lost. The integration uses the overload returning an Proposal: use the overload taking Gradients of fixed dimension. Only Proposal: leave it as it is. It fails with an error and gives no wrong result, and it can't be tested here. TestsPython API never run by a test. Proposal: a separate PR with one small test file: a few assertions on the read-only path, Proposal: leave these as they are
@thelfer do these choices suit, and can they be implemented? |
[AI-assisted]
Fixes #444.
Mechanical fixes from the new review of the python bindings. Points which need a decision are listed in a separate comment.
Fixes
Memory errors:
integrateandintegrate_debugon aBehaviourDatacheck the sizes of its arrays against the given behaviour and hypothesis. A clear error replaces a segmentation fault or a heap overflow. Loading the same behaviour twice still works.array.arraynow stays alive as long as the manager or the initializer uses it. It is kept by the clean-up hook of [choice] Python bindings: arrays bound as external storage checked and kept alive #377 and by thebind*anduseExternalArrayOf*methods. Extending anarray.arrayin use raisesBufferErrorinstead of leading to a use after free.__array__method returns a new read-only array is refused withNOUPDATE. The manager used to read freed memory.Noneasselfis refused byallocateArrayOf*,releaseArrayOf*andsetThreadSafeofMaterialDataManager, instead of a segmentation fault. The change onsetThreadSafeis the one made by Add a pip package for the python bindings (issue #254) #449.Errors and accepted values:
error_messagegetters decode witherrors="replace", so a truncated or invalid message no longer raisesUnicodeDecodeError.setExternalStateVariableon aStateaccepts all the scalars accepted by the other setters: numpy scalars,Decimal,Fraction, 0D arrays.bind*method is refused. It used to be silently ignored. This also applies to a behaviour without internal state variables, where binding an empty array was harmless: such a call must now be skipped.mfront.databaseis reported bywarnings.warn, and onlyImportErroris caught.setExternalStateVariablehas its typos fixed.FEniCS bindings:
KeyErrorof a missing tangent block names the variable.Documentation and clean-up:
BehaviourIntegrationResult.nstates its value when nothing failed. The value is unchanged.MaterialDataManagerandBehaviourDescription.std::variantoverloads ofwrapInNumPyArray, which have no caller, are removed.NumPySupport.hxxis not installed.A new test,
ArgumentsCheckTest, holds a few assertions on the layout check, the kept arrays, the scalars,Noneasselfand the empty arrays.Not covered
executeInitializeFunctionandexecutePostProcessingonly accept aBehaviourDataViewin python, and a view does not know its behaviour. These functions and the view overloads ofintegrateandintegrate_debugare left unchanged.Conflicts with #449
MaterialDataManager.cxxandMaterialStateManager.cxxconflict on the.defof thebind*anduseExternalArrayOf*methods: #449 adds named arguments there, this PR removeskeep_alive. Keeping the names and droppingkeep_alivesolves them.Tested locally
ctest -R py: 17/17 with the default build (pybind11 2.11.1), with-Denable-exceptions=ON, and with pybind11 3.1.0.BehaviourDatawith another behaviour or hypothesis give aRuntimeError, the 4 cases of a reallocatedarray.arraygive aBufferError, the new read-only array gives aRuntimeError, the truncated and invalid messages are returned with replacement characters, and the same behaviour loaded twice still integrates.setExternalStateVariableon aState, by name and by offset.Noneasselfbefore.Not tested