Skip to content

fix: memory safety bugs in AxBRowIP.c and add CMakeLists.txt#3

Open
piyush0028 wants to merge 1 commit into
merledu:mainfrom
piyush0028:fix/memory-safety-and-build
Open

fix: memory safety bugs in AxBRowIP.c and add CMakeLists.txt#3
piyush0028 wants to merge 1 commit into
merledu:mainfrom
piyush0028:fix/memory-safety-and-build

Conversation

@piyush0028

Copy link
Copy Markdown

Summary

This PR fixes four memory safety bugs in src/matmul/AxBRowIP.c and adds a CMakeLists.txt so the project can be compiled with the standard cmake .. && make workflow.

Bugs Fixed

Bug 1 - undefined behaviour (malloc → calloc)
The per-row dense accumulator cD_ was allocated with malloc, leaving garbage values in memory. Every cD_[idx] += valA valB was accumulating onto uninitialised data, producing incorrect SpGEMM results. Fixed by replacing with calloc, which zero-initialises.

Bug 2 - memory leak
cD_ was allocated inside the row loop but never freed. For a matrix with M rows this leaks N×4 bytes × M iterations gigabytes for large inputs. Fixed by adding free(cD_) at the end of each row iteration.

Bug 3 - buffer overflow in output allocation
cNNZ_estimation used a density formula that can return a value smaller than the actual output NNZ, causing cD[row_count] and cC[row_count] to write past the end of their arrays (silent out-of-bounds write). Replaced with safe_cNNZ(M, N) = M * N, which is always a valid upper bound. A symbolic pre-count pass can be added later to get exact NNZ cheaply.

Bug 4 - accumulator sized K instead of N
The accumulator cD_ was sized K (columns of A), but it is indexed by output column (0..N-1). For non-square A×B this is incorrect. Fixed to use N.

Added

  • CMakeLists.txt targeting C11/C++17 with -Wall -Wextra -O3

Tested

Compiled cleanly with GCC 11.4.0 and CMake 3.22.1 on Ubuntu 22.04 with zero errors and zero warnings.

- Replace malloc with calloc for cD_ accumulator (Bug 1: UB from
  uninitialised memory causing incorrect SpGEMM results)
- Add free(cD_) inside row loop (Bug 2: leaked N*4 bytes per row)
- Replace density-based cNNZ estimate with safe M*N upper bound
  (Bug 3: underestimate caused out-of-bounds writes to cD/cC)
- Fix accumulator size from K to N (Bug 4: wrong for non-square A*B)
- Add CMakeLists.txt so project compiles with standard cmake/make
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