Skip to content

Fix out-of-bounds write when counting molecules per type - #14

Merged
sobolevnrm merged 1 commit into
masterfrom
fix-basesystem-type-count
Oct 25, 2026
Merged

sobolevnrm merged 1 commit into
masterfrom
fix-basesystem-type-count

Conversation

@sobolevnrm

Copy link
Copy Markdown
Member

Summary

Fixes a heap-buffer-overflow in the BaseSystem and SystemAM constructors that corrupts memory during PBSAM runs.

Problem

Both constructors count molecules per type with if (i >= maxj.size()) maxj.push_back(0); maxj[i] = ....
That grows the vector by only one element, so a type index more than one past the end writes out of bounds.
PBSAM::make_subsystem builds a one-molecule system for each molecule type during self-polarization, so the type-1 subsystem writes maxj[1] into a one-element vector.
AddressSanitizer reports this as a heap-buffer-overflow from PBSAM::initialize_pbsam.

In APBS, the PBSAM regression test (examples/pbsam-gly/gly_energyforce.in) gives the expected energy on macOS but 1.25E+306 on Ubuntu, where the self-polarization loop runs for hundreds of iterations without converging.
See Electrostatics/apbs#321 and Electrostatics/apbs#323.

Fix

Resize the count vector to cover the type index, initializing new entries to −1 so that types absent from a subsystem end with a count of 0.
typect_ is documented as the count of molecules of each type.

Testing

  • pbamtest (160 tests) and pbsamtest (102 tests) pass on macOS.
  • APBS built with this checkout and -fsanitize=address,undefined runs the PBAM and PBSAM regression examples with no sanitizer reports and reproduces the expected energies.

BaseSystem and SystemAM grew the per-type count vector by one element when
they saw a new type index, then wrote to maxj[i]. A subsystem containing
only a type-1 molecule (as PBSAM::make_subsystem builds for self-
polarization) therefore wrote past the end of the vector, corrupting the
heap and producing platform-dependent PBSAM results. Resize the vector to
cover the type index, and count absent types as zero.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3eaf20ea-3ac0-4701-958f-28527bf641fc
@sobolevnrm
sobolevnrm merged commit cef00d7 into master Oct 25, 2026
7 checks passed
@sobolevnrm
sobolevnrm deleted the fix-basesystem-type-count branch October 25, 2026 23:26
sobolevnrm pushed a commit to Electrostatics/apbs that referenced this pull request Oct 25, 2026
pb_solvers cef00d7 fixes a heap-buffer-overflow in BaseSystem that made
PBSAM results platform dependent (Electrostatics/pb_solvers#14).

Refs #321

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3eaf20ea-3ac0-4701-958f-28527bf641fc
sobolevnrm added a commit to Electrostatics/apbs that referenced this pull request Oct 26, 2026
* Fix PBAM/PBSAM regression tests and test them in CI

Restore the salt concentrations that were commented out in 2021, replace
the obsolete pbsam-auto keyword with pbsam, and update the PBSAM reference
energies for the solvent-dielectric scaling that pb_solvers 5671943 (2017)
applied to energies to match forces. Add a PR-triggered Linux job that
builds with PBAM/PBSAM enabled and runs their regression tests.

Refs #321

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3eaf20ea-3ac0-4701-958f-28527bf641fc

* Fix out-of-bounds read in PBAM/PBSAM write parsing

NOsh_parsePBAM and NOsh_parsePBSAM read writefmt[numwrite - 1] and
writestem[numwrite - 1] even when the input had no write statement,
indexing element -1. Only check the write format when a write exists.

Refs #321

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3eaf20ea-3ac0-4701-958f-28527bf641fc

* Pin pb_solvers with the BaseSystem type-count fix

pb_solvers cef00d7 fixes a heap-buffer-overflow in BaseSystem that made
PBSAM results platform dependent (Electrostatics/pb_solvers#14).

Refs #321

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3eaf20ea-3ac0-4701-958f-28527bf641fc

* Pin pb_solvers with the OpenBLAS applyMMat fix

pb_solvers 8d254e5 makes PBSAM's matrix multiply work with OpenBLAS
(Electrostatics/pb_solvers#15), which fixes the Linux PBSAM results.

Refs #321

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3eaf20ea-3ac0-4701-958f-28527bf641fc

---------

Co-authored-by: Nathan Baker <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3eaf20ea-3ac0-4701-958f-28527bf641fc
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