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 all commits
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.
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...)