Skip to content

fix: use MPI_STATUS_SIZE=6 for Open MPI (avoids 4-byte stack overflow) - #137

Open
HarshitaKalani wants to merge 1 commit into
lfortran:mainfrom
HarshitaKalani:fix/mpi-status-size-openmpi
Open

fix: use MPI_STATUS_SIZE=6 for Open MPI (avoids 4-byte stack overflow)#137
HarshitaKalani wants to merge 1 commit into
lfortran:mainfrom
HarshitaKalani:fix/mpi-status-size-openmpi

Conversation

@HarshitaKalani

Copy link
Copy Markdown

Summary

  • Open MPI 5.x uses a 6-int (24-byte) MPI_Status, but the Fortran binding hardcoded MPI_STATUS_SIZE = 5. This causes the native MPI_Recv (and any other status-writing call) to write 4 bytes past the end of the Fortran tmp_status buffer allocated inside our wrappers, corrupting the caller's stack.
  • Depending on the caller's exact stack layout, this manifests as a hard segfault deep inside a subsequent runtime call (e.g. _lcompilers_string_format_fortran) at a suspicious address such as 0x7fff00000000 — the upper half of a stack pointer left over after the 4-byte overflow.
  • Gate MPI_STATUS_SIZE on the existing -DOPEN_MPI preprocessor symbol (already used elsewhere in mpi_c_bindings.f90) so it matches the underlying C header:
    • Open MPI → 6
    • MPICH → 5 (unchanged)

Rationale

Verified against Open MPI 5.0.10 that sizeof(MPI_Status) == 24 and the Fortran binding shipped by mpi.mod exposes MPI_STATUS_SIZE = 6. Our own binding must agree, otherwise we hand C a smaller buffer than it expects.

Verification

  • Repro (before): tests/recv_1.f90 crashes with Segmentation fault: address not mapped to object at address 0x7fff00000000 inside print_into_string of the LFortran runtime.
  • After this fix: the full tests/run_tests.sh suite passes (57/57 test-rank combinations, including recv_1) at the currently pinned upstream commit 31033d3 using LFortran built with LLVM 15 on Linux/x86_64 with Open MPI 5.0.10.

Test plan

  • FC="lfortran --cpp" ./tests/run_tests.sh passes end-to-end with Open MPI 5.
  • Re-check under MPICH (unchanged path) — no source changes on that leg beyond additional comments.

Made with Cursor

Open MPI 5.x defines `sizeof(MPI_Status) = 24` bytes (6 ints on typical
LP64 platforms), whereas MPICH and older Open MPI releases used a
5-int (20-byte) layout. The Fortran binding hardcoded
`MPI_STATUS_SIZE = 5`, so when calling into the native MPI_Recv (and
similar) with the fixed-size `integer, dimension(MPI_STATUS_SIZE) ::
tmp_status` buffer, the C runtime would write past the end of the
buffer and corrupt the surrounding stack.

Depending on the caller's stack layout this could manifest as a hard
segmentation fault deep inside the caller (e.g. inside a subsequent
`_lcompilers_string_format_fortran`) at an address of the form
`0x7fff00000000` — the upper half of a stack pointer overwritten by
the 4-byte overflow.

Gate `MPI_STATUS_SIZE` on the existing `-DOPEN_MPI` preprocessor
symbol (already used elsewhere in mpi_c_bindings.f90) so it matches
the underlying C header:

  * Open MPI  -> 6
  * MPICH     -> 5 (unchanged)

This is caught by `tests/recv_1.f90`, which crashed on Linux/x86_64
with Open MPI 5.

Co-authored-by: Cursor <cursoragent@cursor.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