-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix a subtle uninitialized-memory-read in Buffer::for_each_value() #7330
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2172,9 +2172,13 @@ class Buffer { | |
} | ||
} | ||
|
||
// Return pair is <new_dimensions, innermost_strides_are_one> | ||
template<int N> | ||
HALIDE_NEVER_INLINE static bool for_each_value_prep(for_each_value_task_dim<N> *t, | ||
const halide_buffer_t **buffers) { | ||
HALIDE_NEVER_INLINE static std::pair<int, bool> for_each_value_prep(for_each_value_task_dim<N> *t, | ||
const halide_buffer_t **buffers) { | ||
const int dimensions = buffers[0]->dimensions; | ||
assert(dimensions > 0); | ||
|
||
// Check the buffers all have clean host allocations | ||
for (int i = 0; i < N; i++) { | ||
if (buffers[i]->device) { | ||
|
@@ -2188,8 +2192,6 @@ class Buffer { | |
} | ||
} | ||
|
||
const int dimensions = buffers[0]->dimensions; | ||
|
||
// Extract the strides in all the dimensions | ||
for (int i = 0; i < dimensions; i++) { | ||
for (int j = 0; j < N; j++) { | ||
|
@@ -2219,42 +2221,47 @@ class Buffer { | |
} | ||
if (flat) { | ||
t[i - 1].extent *= t[i].extent; | ||
for (int j = i; j < d; j++) { | ||
for (int j = i; j < d - 1; j++) { | ||
t[j] = t[j + 1]; | ||
} | ||
i--; | ||
d--; | ||
t[d].extent = 1; | ||
} | ||
} | ||
|
||
// Note that we assert() that dimensions > 0 above | ||
// (our one-and-only caller will only call us that way) | ||
// so the unchecked access to t[0] should be safe. | ||
bool innermost_strides_are_one = true; | ||
if (dimensions > 0) { | ||
for (int i = 0; i < N; i++) { | ||
innermost_strides_are_one &= (t[0].stride[i] == 1); | ||
} | ||
for (int i = 0; i < N; i++) { | ||
innermost_strides_are_one &= (t[0].stride[i] == 1); | ||
} | ||
|
||
return innermost_strides_are_one; | ||
return {d, innermost_strides_are_one}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this d can be after d-- on 2226, is this expected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is deliberate; if we 'flatten' multiple dimensions into one, we want to know the new, smaller number of dimensions. |
||
} | ||
|
||
template<typename Fn, typename... Args, int N = sizeof...(Args) + 1> | ||
void for_each_value_impl(Fn &&f, Args &&...other_buffers) const { | ||
if (dimensions() > 0) { | ||
const size_t alloc_size = dimensions() * sizeof(for_each_value_task_dim<N>); | ||
Buffer<>::for_each_value_task_dim<N> *t = | ||
(Buffer<>::for_each_value_task_dim<N> *)HALIDE_ALLOCA((dimensions() + 1) * sizeof(for_each_value_task_dim<N>)); | ||
(Buffer<>::for_each_value_task_dim<N> *)HALIDE_ALLOCA(alloc_size); | ||
// Move the preparatory code into a non-templated helper to | ||
// save code size. | ||
const halide_buffer_t *buffers[] = {&buf, (&other_buffers.buf)...}; | ||
bool innermost_strides_are_one = Buffer<>::for_each_value_prep(t, buffers); | ||
|
||
Buffer<>::for_each_value_helper(f, dimensions() - 1, | ||
innermost_strides_are_one, | ||
t, | ||
data(), (other_buffers.data())...); | ||
} else { | ||
f(*data(), (*other_buffers.data())...); | ||
auto [new_dims, innermost_strides_are_one] = Buffer<>::for_each_value_prep(t, buffers); | ||
if (new_dims > 0) { | ||
Buffer<>::for_each_value_helper(f, new_dims - 1, | ||
innermost_strides_are_one, | ||
t, | ||
data(), (other_buffers.data())...); | ||
return; | ||
} | ||
abadams marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// else fall thru | ||
} | ||
|
||
// zero-dimensional case | ||
f(*data(), (*other_buffers.data())...); | ||
abadams marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
// @} | ||
|
||
|
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.
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.
t[d-1].extent will be set on line 2231, what is about .stride?
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.
No, we want to leave that alone -- @abadams to confirm.
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.
Do we still need to set the extent, given that we're not going to iterate over that dimension?
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.
Oooh, good point. We can probably elide that now. Let me do some hackery to verify.
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, that was definitely unnecessary.