-
Notifications
You must be signed in to change notification settings - Fork 119
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
Initialize prop_id_
in NPyMechObj
.
#2604
Conversation
This locally fixes the segfault in one test that reliably used to cause a segfault. The next steps are to make sure that this doesn't leak, because we might need to call the dtor manually. Then if there's more segfault, I'll deal with those. |
Codecov Report
@@ Coverage Diff @@
## master #2604 +/- ##
=======================================
Coverage 66.19% 66.19%
=======================================
Files 559 559
Lines 108933 108939 +6
=======================================
+ Hits 72105 72117 +12
+ Misses 36828 36822 -6
... and 6 files with indirect coverage changes 📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today! |
This comment has been minimized.
This comment has been minimized.
When creating a `NPyMechObj` the Python C API will perform the equivalent of a `malloc(sizeof(NPyMechObj))`. This will return an invalid object, that must be fixed using placement new. Details: * `PyObject_New(NPyMechObj, pmech_generic_type)` does not call `tp_new`. * `tp_free` is the mental equivalent of `free`. * Creating a valid NPyMechObj object should be done via `new_mechobj()`.
This comment has been minimized.
This comment has been minimized.
Since `NPyMechObj` contains a `ob_base` I'm not convinced it's correct to default initialize the entire struct.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you fixed it !
There is one question I didn't answer: Yes, I think we should avoid the warning. The way I get around this is by converting:
to
and removing the What made me unsure about the first change is the mention of "external linkage" and "for linkage purposes" on cppreference. To my knowledge structs aren't linked. However, in C++ their name is used to do name-mangling, which affects linkage and I think that's what they're referring to. Therefore, I think the change is fine. The reason why the Python C API uses the pattern is because it allows us to say Justification for removing the If something doesn't sound right, please let me know so we can dig deeper. |
I'll run the NMODL CI with Python 3.12 on this PR. |
✔️ 67b39de -> Azure artifacts URL |
This comment has been minimized.
This comment has been minimized.
Your explanation seems reasonable to me. I believe it also may be useful to have code comments about why the pattern (that we use everywhere else) changed from I experimented a bit more by removing the initializer in the struct from I see that this file is still In summary, I tentatively propose: (if substantively acceptable, feel free to improve the comments. If you think it is more or less pointless, I accept your current version)
|
I like the comments.
|
The previous failing CI has is running against this version; and some tests that used to segfault now pass. (Got cancelled by pushing commits to this repository.) |
✔️ b5cce09 -> Azure artifacts URL |
The NMODL CI is green: |
This comment has been minimized.
This comment has been minimized.
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
✔️ d409241 -> Azure artifacts URL |
This PR fixes initialization of
NPyMechObj
. The Python API seems to perform the equivalent of amalloc
followed by calling a user suppliedinit(NPyMechObj *)
. In theinit
function:nrn/src/nrnpython/nrnpy_nrn.cpp
Lines 264 to 275 in fa132e4
assigns a to
prop_id_
, which at that point I believe is filled with random values. This can cause either a segfault, or freeing some random address.The solution pursued here is to use placement new:
https://en.cppreference.com/w/cpp/language/new
(There's a section "Placement New" halfway down the document.)
and if needed it'll call the destructor manually.