Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
runtime: fix pointer calculations to avoid overflows. Fixes #5713. #5716
runtime: fix pointer calculations to avoid overflows. Fixes #5713. #5716
Changes from 1 commit
fe8e4aa
729daaf
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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 think there should be parentheses/casts here to avoid the possibility of
dim(d).stride * type().bytes()
overflowing.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 don't think this is necessary since
shift
(now aptrdiff_t
) should force promotion of the other field in the expression. Is that not correct?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 was thinking it would depend on the order in which the compiler associates this arithmetic. Even if it's well defined behavior for the compiler to implement it as (shift * dim(d).stride()) * type().bytes(), we should at least add parentheses to make that explicit.
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 the associativity is guaranteed to be left-to-right: https://en.cppreference.com/w/c/language/operator_precedence
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.
It may be, but I still think we should not rely on this, by adding explicit parentheses. It's easy for things like this to unintentionally break later because it is so subtle.
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.
OK. And BTW, thanks for reviewing this.
I can get back to modifying this PR tomorrow afternoon and add explicit parenthesis, though frankly I think it actually makes things more confusing. I would look at such code and do a double take, wondering why those parenthesis are there at all, thinking I was actually missing something subtle. The same applies to unnecessary casts. IMO, if your goal is to inform developers who run across this code later, it's better to just add appropriate comments. If for some reason you don't trust the compiler to follow the C++ spec, then the correct approach is to add unit tests to actually verify behavior, which can then also detect regressions.
These are also the sorts of problems that can often be detected by static analysis.
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'm not worried about a non-conforming compiler, I'm more thinking that someone might come along and refactor the code somehow (e.g. pulling something into a temporary, reordering the expression) without realizing the order is important.
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.
Added parenthesis
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 think there should be parentheses/casts here to avoid the possibility of dim(d).stride * type().bytes() overflowing.
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.
Same reply as above.
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.
Just noticed this on the last skim: should this be int64_t ?
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.
The fields in
struct device_copy
areuint64_t
type, includingsrc_begin
. Like I said - there's a lack of consistency in the codebase. This assumes thatstride >= 0
anddst->dim[i].min >= src->dim[i].min
. If these assumptions don't hold, then there is something more seriously wrong here I think.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 was thinking that the RHS here could be signed (as opposed to the two examples below, where it seems harmless to cast to uint64). But I agree that there are other issues here.
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.
This should indeed be int64_t. The operations required to compute an offset given some strides, etc are multiplications, subtractions, additions. None of those differ between signed and unsigned (yay modulo arithmetic), so none of that matters. But there are also upcasts, and those are the only thing that differs between sign and unsigned. The source field is an int32_t, so it must be sign-extended. This PR casts an int32_t to a uint64_t. I'm honestly not sure whether that sign or zero extends. Using an int64_t would make it clear that it's sign-extending it. The rest of the arithmetic is invariant to signed vs unsigned.
There may be bugs elsewhere when the strides are negative, but let's not add a new one.
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.
Is
stride
ever allowed to be a negative value? If not, then the PR should be fine as-is I think, otherwise we can do something like:The
uint64_t
cast is not strictly needed.edit: actually, you could want
src_begin
to actually decrease in value?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.
Yes, we might want src_begin to decrease in value, and we may want to support negative strides in future. For now we should just write new code assuming they could be negative.
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.
OK, but to be clear -
stride
is currently never negative? I want to make sure this change didn't break anything.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.
Some parts of Halide support negative strides and some don't. device buffer copies currently do not. So in this code, stride is currently not going to be negative (or other things would break too).
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.
In theory, negative strides should be fully supported. As you've noticed, though, this is undertested (probably for both normal and
large_buffers
mode). We should add some tests that specifically exercise negative strides to verify that this works (and stays working). (Or declare that we don't support negative strides...)