fix: memory safety bugs in AxBRowIP.c and add CMakeLists.txt#3
Open
piyush0028 wants to merge 1 commit into
Open
fix: memory safety bugs in AxBRowIP.c and add CMakeLists.txt#3piyush0028 wants to merge 1 commit into
piyush0028 wants to merge 1 commit into
Conversation
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes four memory safety bugs in
src/matmul/AxBRowIP.cand adds aCMakeLists.txtso the project can be compiled with the standardcmake .. && makeworkflow.Bugs Fixed
Bug 1 - undefined behaviour (malloc → calloc)
The per-row dense accumulator
cD_was allocated withmalloc, leaving garbage values in memory. EverycD_[idx] += valA valBwas accumulating onto uninitialised data, producing incorrect SpGEMM results. Fixed by replacing withcalloc, 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 addingfree(cD_)at the end of each row iteration.Bug 3 - buffer overflow in output allocation
cNNZ_estimationused a density formula that can return a value smaller than the actual output NNZ, causingcD[row_count]andcC[row_count]to write past the end of their arrays (silent out-of-bounds write). Replaced withsafe_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 sizedK(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.txttargeting C11/C++17 with-Wall -Wextra -O3Tested
Compiled cleanly with GCC 11.4.0 and CMake 3.22.1 on Ubuntu 22.04 with zero errors and zero warnings.