-
Notifications
You must be signed in to change notification settings - Fork 108
issue/1042 - fix address misaligned error of rearrange #1044
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -144,11 +144,14 @@ void rearrange( | |
| utils::Result<RearrangeMeta> RearrangeMeta::distributeUnit(const std::vector<size_t> &candidates) const { | ||
| // 获取当前的unit大小 | ||
| size_t current_unit = _meta[0]; | ||
| const size_t ndim = this->ndim(); | ||
| const ptrdiff_t dst_strides_0 = _meta[2 + ndim]; | ||
| const ptrdiff_t src_strides_0 = _meta[2 + ndim + ndim]; | ||
|
|
||
| // 寻找满足条件的unit值:当前unit能被其整除 | ||
| size_t new_unit = 0; | ||
| for (size_t candidate : candidates) { | ||
| if (current_unit % candidate == 0) { | ||
| for (const size_t &candidate : candidates) { | ||
| if ((current_unit % candidate == 0) && 0 == (dst_strides_0 & (candidate - 1)) && 0 == (src_strides_0 & (candidate - 1))) { | ||
|
||
| new_unit = candidate; | ||
| break; | ||
| } | ||
|
|
||
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.
When
ndim == 0(all dimensions were merged intounitduringRearrangeMeta::create()),_metahas only 2 elements (indices 0 and 1). In that case,_meta[2 + ndim]=_meta[2]and_meta[2 + ndim + ndim]=_meta[2]are out-of-bounds accesses, causing undefined behavior.A guard should be added so that when
ndim == 0, the alignment check on strides is skipped entirely (there are no strides to check). For example, the alignment checks should only be performed whenndim > 0, and whenndim == 0, only thecurrent_unit % candidate == 0condition needs to be satisfied.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.
在这个函数中,ndim会永远大于0,不会存在ndim == 0的情况