-
Notifications
You must be signed in to change notification settings - Fork 24
Fix Use-After-Free in NumPy/df #478
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
Conversation
For AoS and SoA `.to_numpy()` and dependent logic like `.to_df()`, the lifetime of the temporary copied variable is only handled by NumPy if we first create a named variable. The `to_host()` returned object is a temporary, and `np.array` using the `__array_interface__` protocol does not keep it alive automatically unless it is stored in an actual variable (eg., `tmp`).
a7dfdc0 to
da9d3a4
Compare
| import numpy as np | ||
|
|
||
| if self.size() > 0: | ||
| if copy: |
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.
We can generally make this a bit more explicit by checking if self is on GPU and otherwise just copying from it directly, with another if/else branch. But the logic here should have the same effect and amount of copies involved...
The only difference is that here we will always end up with pinned memory, while we might be sometimes interested using regular host memory. But seems niche.
roelof-groenewald
left a comment
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.
Looks good to me.
Just one question: Do we need the asserts to "trick" Python into keeping the object alive?
|
The assert is not needed, but if it is ever violated I want a python error instead of a segfault in code that uses the returned variable. I was too lasy to |
|
Ah, |
| # np.array using the __array_interface__ protocol does | ||
| # not keep it alive automatically unless it is stored | ||
| # in an actual variable (tmp). | ||
| tmp = self.to_host() |
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.
Hm, looks like there is more / this is not a sufficient fix (at least for some SP tests on conda-forge):
conda-forge/impactx-feedstock#56 (comment)
It already has an issue here, so the problem could be in the implementation of to_host()...
https://github.com/AMReX-Codes/pyamrex/blob/25.09/src/Base/PODVector.cpp#L79-L87
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.
Very hard to reproduce locally... also cannot spot anything in valgrind yet.
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.
seems to be fixed by BLAST-ImpactX/impactx#1156 o.0
For AoS and SoA
.to_numpy()and dependent logiclike
.to_df(), the lifetime of the temporary copied variable is only handled by NumPy if we first create a named variable.The
to_host()returned object is a temporary, andnp.arrayusing the__array_interface__protocol does not keep it alive automatically unless it is stored in an actual variable (eg.,tmp).X-ref: