Skip to content

Fix UB in allgather with zero-size output#495

Open
pavanbalaji wants to merge 1 commit intopytorch:mainfrom
pavanbalaji:export-D95712700
Open

Fix UB in allgather with zero-size output#495
pavanbalaji wants to merge 1 commit intopytorch:mainfrom
pavanbalaji:export-D95712700

Conversation

@pavanbalaji
Copy link

Summary:
When the output buffer is empty (outBytes == 0), the input
buffer pointer may be null. The memcpy at line 50 was called
before the early return check, passing a null pointer to
memcpy which is undefined behavior even with size 0. UBSAN
catches this in the Count_0 test cases.

Move the outBytes == 0 early return before the memcpy to
avoid the null pointer. Split the context->size == 1 check
to remain after the memcpy, since single-process still needs
the input-to-output copy.

Differential Revision: D95712700

Summary:
When the output buffer is empty (outBytes == 0), the input
buffer pointer may be null. The memcpy at line 50 was called
before the early return check, passing a null pointer to
memcpy which is undefined behavior even with size 0. UBSAN
catches this in the Count_0 test cases.

Move the outBytes == 0 early return before the memcpy to
avoid the null pointer. Split the context->size == 1 check
to remain after the memcpy, since single-process still needs
the input-to-output copy.

Differential Revision: D95712700
@meta-cla meta-cla bot added the CLA Signed label Mar 8, 2026
@meta-codesync
Copy link

meta-codesync bot commented Mar 8, 2026

@pavanbalaji has exported this pull request. If you are a Meta employee, you can view the originating Diff in D95712700.

const size_t inBytes = out->size / context->size;
const size_t outBytes = out->size;

// Short circuit if the output is empty.

Choose a reason for hiding this comment

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

will this be better before calculating sendRank & recvRank i.e under sanity checks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants