Skip to content

fix: prevent integer overflow in Psi offsets - #8107

Merged
mohanchen merged 1 commit into
deepmodeling:developfrom
arcusbuilds:fix/8088-psi-offset-overflow
Oct 10, 2026
Merged

mohanchen merged 1 commit into
deepmodeling:developfrom
arcusbuilds:fix/8088-psi-offset-overflow

Conversation

@arcusbuilds

Copy link
Copy Markdown

Fixes #8088.

Compute Psi offsets using std::size_t to prevent integer overflow and silent incorrect results in large wave-function arrays. Adds two regression tests covering both storage layouts.

All 10 CPU tests pass with release optimization and UBSan.

Verification details

Both new tests fail against the original implementation and pass with the fix. Tested in Ubuntu WSL with GNU 15.2.0:

g++ -std=c++17 -O3 -DNDEBUG -fsanitize=undefined -fno-sanitize-recover=all -ffunction-sections -fdata-sections -Isource source/source_psi/test/psi_test.cpp source/source_psi/psi.cpp source/source_base/module_device/memory_op.cpp source/source_base/memory_recorder.cpp source/source_base/global_variable.cpp source/source_io/module_parameter/parameter.cpp -Wl,--gc-sections -lgtest_main -lgtest -pthread -o build-8088/psi_tests_release_ubsan
OMP_NUM_THREADS=1 build-8088/psi_tests_release_ubsan
g++ -std=c++11 -Isource -c source/source_psi/psi.cpp -o build-8088/psi-cxx11.o
python tools/03_code_analysis/agent_governance_check.py --base HEAD~1 --head HEAD --event-path build-8088/pr-event.json --format text
git diff --check

Results: 10/10 tests passed, no UBSan diagnostics; C++11 compilation passed; governance and whitespace checks passed. Installed GoogleTest requires C++17.

CMake configuration could not find BLAS, so verification used the real production sources linked directly. CUDA/ROCm builds and the reported Fe calculation were not run. The tests reserve 12 GiB of virtual address space and touch only a few elements.

No documentation update required: no INPUT parameters or public interfaces changed. Core impact is limited to Psi pointer arithmetic. No governance exceptions requested.

Copilot AI balanced review requested due to automatic review settings October 10, 2026 00:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The offset conversions are complete, C++11-compatible, and covered by focused regression tests.

0 open findings

What changed in this PR

Fixes integer overflow in large Psi array offsets.

Changes:

  • Promotes pointer-offset arithmetic to std::size_t.
  • Adds regression tests for both storage layouts.
  • Verified changed files use LF line endings; tests were not rerun during review.
File Description
source/​source_psi/​psi.cpp Uses size-safe offset calculations.
source/​source_psi/​test/​psi_test.cpp Tests offsets exceeding INT_MAX.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@mohanchen mohanchen added the Refactor Refactor ABACUS codes label Oct 10, 2026
@mohanchen

Copy link
Copy Markdown
Collaborator

LGTM. Thanks for your contributions.

@mohanchen mohanchen added the Memory Memory issues label Oct 10, 2026
@mohanchen
mohanchen merged commit fc16d95 into deepmodeling:develop Oct 10, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Memory Memory issues Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Silent wrong results in LCAO multi-k runs: int overflow in Psi::fix_k psi offset when (nks−1)·nbands·nbasis > INT_MAX

3 participants