Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/utils/rearrange.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Comment on lines +147 to +149
Copy link

Copilot AI Mar 4, 2026

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 into unit during RearrangeMeta::create()), _meta has 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 when ndim > 0, and when ndim == 0, only the current_unit % candidate == 0 condition needs to be satisfied.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在这个函数中,ndim会永远大于0,不会存在ndim == 0的情况


// 寻找满足条件的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))) {
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alignment check only validates the first ([0]-th) element of dst_strides and src_strides. For correct alignment, all outer strides must be multiples of the chosen new_unit, not just the first one. When ndim > 1, intermediate strides at indices 1 through ndim-1 are not checked.

For example, if ndim == 2 and dst_strides == [40, 12] with candidate=8, then 40 & 7 == 0 passes the check but 12 & 7 == 4 != 0 means stride index 1 is not 8-byte aligned, leading to misaligned access.

The loop over candidates should check alignment for ALL strides (dst_strides()[0..ndim-1] and src_strides()[0..ndim-1]), not just index 0.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有必要检查每一个维度的stride么

new_unit = candidate;
break;
}
Expand Down