Skip to content

Fix buffer safety, MPI type size correctness, and empty-data edge cases#6

Open
konnyakucstdio wants to merge 4 commits into
ZCCLorg:mainfrom
konnyakucstdio:fix/buffer-safety-and-type-correctness
Open

Fix buffer safety, MPI type size correctness, and empty-data edge cases#6
konnyakucstdio wants to merge 4 commits into
ZCCLorg:mainfrom
konnyakucstdio:fix/buffer-safety-and-type-correctness

Conversation

@konnyakucstdio

Copy link
Copy Markdown

Summary

This PR fixes several critical bugs in ZCCL related to memory safety, MPI datatype correctness, and edge-case handling. All fixes are backward-compatible.

Changes

1. Empty-data guards (crash fix)

Added nbEle == 0 guards to 13 compress/decompress/homomorphic-add functions in ZCCL_float.c and ZCCLd_float.c. When the input is empty, the original code performs invalid memory accesses (null pointer dereference, zero-size allocations), causing crashes. The guards return early with safe defaults (*outSize = 0; return NULL or equivalent).

2. Compression buffer expansion factor (memory safety fix)

Added get_buf_exp(size_t nbytes) in ZCCL.h — lossy compression can produce output larger than the input, especially for small data where metadata overhead dominates. The expansion factor is:

  • ≤4 KB → 24×
  • ≤64 KB → 12×
  • ≤1 MB → 6×
  • 1 MB → 4×

Without this, compressed data can overflow the output buffer (buffer overrun).

3. MPI Datatype size correctness (correctness fix)

Added mpi_sizeof(MPI_Datatype) in ZCCL_utils.h using MPI_Type_size() instead of C sizeof(). When users pass custom MPI types (e.g., MPI_Type_create_struct), sizeof(type) returns the C type's size rather than the actual wire data size, leading to undersized buffers and incorrect MPI communication parameters.

All sizeof(sendtype) / sizeof(recvtype) / sizeof(datatype) in ZCCL_ring.c, ZCCL_ring_ho.c, ZCCL_scatter.c, and ZCCL_broadcast.c are replaced with mpi_sizeof(...).

4. Ring Allreduce buffer offset refactoring (correctness fix)

The buffer expansion factor (point 2) changes the memory layout of compressed data buffers. All related code in ZCCL_ring.c and ZCCL_ring_ho.c is updated:

  • Temporary buffer allocations: count * extentbuf_exp * count * extent
  • MPI_Irecv receive sizes multiplied by buf_exp
  • Pointer offset calculations: displs[i] * extentbuf_exp * displs[i] * extent
  • Applied to both single-thread (_st_) and multi-thread (_mt_) code paths

Files Changed

File Changes
ZCCL/include/ZCCL.h +get_buf_exp()
ZCCL/include/ZCCL_utils.h +mpi_sizeof()
ZCCL/src/ZCCL_float.c +empty guards, +buffer safety
ZCCL/src/ZCCLd_float.c +empty guards
ZCCL/src/ZCCL_ring.c buf_exp + mpi_sizeof fixes
ZCCL/src/ZCCL_ring_ho.c buf_exp + mpi_sizeof fixes
ZCCL/src/ZCCL_scatter.c buf_exp + mpi_sizeof fixes
ZCCL/src/ZCCL_broadcast.c buf_exp + mpi_sizeof fixes
configure.ac -O3 → -ggdb -O0 (debug build)

Testing

These fixes have been verified in practice — the empty-data guards prevent crashes on edge cases, the buffer expansion prevents silent data corruption from compressed output overflow, and the MPI type size fix correctly handles custom datatypes in collective operations.

konnyakucstdio and others added 4 commits June 16, 2026 15:30
- get_buf_exp(nbytes): compute safe compression buffer expansion factor
  based on data size (up to 24x for very small data, 4x for large data).
  Prevents buffer overflow when lossy compression expands data.

- mpi_sizeof(datatype): use MPI_Type_size() instead of sizeof() to
  correctly handle user-defined MPI datatypes (e.g., MPI_Type_create_struct).
  sizeof() on an MPI type returns the C type size, not the actual wire size.

Co-Authored-By: Claude <noreply@anthropic.com>
- Replace sizeof(sendtype/recvtype/datatype) with mpi_sizeof() for
  correct MPI type size in all buffer calculations.

- Use buf_exp multiplier from get_buf_exp() for:
  * temp_recvbuf / tmpbuf / outputBytes allocations
  * MPI_Irecv receive buffer sizes (expanded to accommodate inflated
    compressed data)
  * Pointer offset calculations (displs[i] * extent → buf_exp * displs[i] * extent)
  * Single-thread and OpenMP-threadblock code paths

This is a coordinated fix: the buffer expansion factor changes the
memory layout, so all pointer arithmetic and communication sizes must
be updated together to prevent out-of-bounds reads/writes.

Co-Authored-By: Claude <noreply@anthropic.com>
When nbEle == 0 (empty input), the original code would attempt invalid
memory access (null pointer dereference, zero-size mallocs, etc.),
causing crashes.

Add nbEle == 0 guards to 13 functions across ZCCL_float.c and
ZCCLd_float.c, returning early with safe defaults:
  * Compress functions: *outSize = 0; return NULL
  * Decompress functions: *newData = NULL; return
  * Homomorphic add functions: *final_cmpSize = 0; return

Also include:
- Per-thread buffer size safety multiplier (2x) to prevent overflow
  in multi-threaded compression paths
- VERBOSE debug output (disabled by default, #ifdef-guarded)

Co-Authored-By: Claude <noreply@anthropic.com>
Change default CFLAGS from -O3 to -ggdb -O0 for easier debugging
during development. Production builds can override via CFLAGS env.

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant