llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[flang-rt] Incorrect REAL(10)/COMPLEX(10) component sizes in runtime type info can corrupt derived-type assignment

Open Saieiei opened this issue 7 months ago • 1 comments

On x86-64, Component::GetElementByteSize() in flang-rt/lib/runtime/type-info.cpp uses the Fortran kind value for REAL(10) / COMPLEX(10) instead of the actual storage size. This causes the runtime's componentwise derived-type assignment path to copy too few bytes and corrupt data.

This was originally noticed in a non-public external test, and the same underlying bug can be reproduced with the standalone example below.

Standalone reproducer

program real10_bug
  implicit none
  type :: seq_t
    sequence
    real(kind=10) :: arr(3)
    integer       :: tag
  end type
  type(seq_t) :: a(4), b(4)
  integer :: i

  do i = 1, 4
    a(i)%arr(1) = real(i * 100, kind=10)
    a(i)%arr(2) = real(i * 200, kind=10)
    a(i)%arr(3) = real(i * 300, kind=10)
    a(i)%tag = i
    b(i)%arr(:) = 0.0_10
    b(i)%tag = 0
  end do

  ! Strided section assignment forces runtime componentwise copy
  b(1:3:2) = a(2:4:2)

  if (b(1)%arr(3) /= 600.0_10) then
    print *, "FAIL: b(1)%arr(3) =", b(1)%arr(3), " expected 600"
    stop 1
  end if
  if (b(3)%arr(3) /= 1200.0_10) then
    print *, "FAIL: b(3)%arr(3) =", b(3)%arr(3), " expected 1200"
    stop 1
  end if
  if (b(1)%tag /= 2 .or. b(3)%tag /= 4) then
    print *, "FAIL: tag mismatch"
    stop 1
  end if

  print *, "PASS"
end program

Observed behavior

Unfixed build:

FAIL: b(1)%arr(3) = 0.  expected 600
STOP 1

Fixed build:

PASS

Stack trace

With a breakpoint on Component::SizeInBytes() while running the standalone reproducer:

#0  Fortran::runtime::typeInfo::Component::SizeInBytes at type-info.cpp:84
#1  Fortran::runtime::DerivedAssignTicket<true>::Continue at assign.cpp:620
#2  Fortran::runtime::WorkQueue::ContinueItem at work-queue.cpp:90
#3  Fortran::runtime::WorkQueue::Run at work-queue.cpp:50
#4  Fortran::runtime::AssignTicket::Continue at assign.cpp:474
#5  Fortran::runtime::WorkQueue::ContinueItem at work-queue.cpp:90
#6  Fortran::runtime::WorkQueue::Run at work-queue.cpp:50
#7  Fortran::runtime::Assign at assign.cpp:272
#8  _FortranAAssign at assign.cpp:793
#9  _QQmain at reproducer.f90:29

SizeInBytes() returns 30 for real(kind=10) :: arr(3), but the correct size on x86-64 is 48.

Root cause

GetElementByteSize() currently does this:

case TypeCategory::Real:
  return kind_;
case TypeCategory::Complex:
  return 2 * kind_;

For kind 10 on x86-64, that is smaller than the actual storage size.

Proposed fix

PR #192049 updates GetElementByteSize() to use Descriptor::BytesFor(category(), kind_) for Real and Complex.

Saieiei avatar Apr 14 '26 16:04 Saieiei

@llvm/issue-subscribers-flang-runtime

Author: Sairudra More (Saieiei)

On x86-64, `Component::GetElementByteSize()` in `flang-rt/lib/runtime/type-info.cpp` uses the Fortran kind value for `REAL(10)` / `COMPLEX(10)` instead of the actual storage size. This causes the runtime's componentwise derived-type assignment path to copy too few bytes and corrupt data.

This was originally noticed in a non-public external test, and the same underlying bug can be reproduced with the standalone example below.

Standalone reproducer

program real10_bug
  implicit none
  type :: seq_t
    sequence
    real(kind=10) :: arr(3)
    integer       :: tag
  end type
  type(seq_t) :: a(4), b(4)
  integer :: i

  do i = 1, 4
    a(i)%arr(1) = real(i * 100, kind=10)
    a(i)%arr(2) = real(i * 200, kind=10)
    a(i)%arr(3) = real(i * 300, kind=10)
    a(i)%tag = i
    b(i)%arr(:) = 0.0_10
    b(i)%tag = 0
  end do

  ! Strided section assignment forces runtime componentwise copy
  b(1:3:2) = a(2:4:2)

  if (b(1)%arr(3) /= 600.0_10) then
    print *, "FAIL: b(1)%arr(3) =", b(1)%arr(3), " expected 600"
    stop 1
  end if
  if (b(3)%arr(3) /= 1200.0_10) then
    print *, "FAIL: b(3)%arr(3) =", b(3)%arr(3), " expected 1200"
    stop 1
  end if
  if (b(1)%tag /= 2 .or. b(3)%tag /= 4) then
    print *, "FAIL: tag mismatch"
    stop 1
  end if

  print *, "PASS"
end program

Observed behavior

Unfixed build:

FAIL: b(1)%arr(3) = 0.  expected 600
STOP 1

Fixed build:

PASS

Stack trace

With a breakpoint on Component::SizeInBytes() while running the standalone reproducer:

#<!-- -->0  Fortran::runtime::typeInfo::Component::SizeInBytes at type-info.cpp:84
#<!-- -->1  Fortran::runtime::DerivedAssignTicket&lt;true&gt;::Continue at assign.cpp:620
#<!-- -->2  Fortran::runtime::WorkQueue::ContinueItem at work-queue.cpp:90
#<!-- -->3  Fortran::runtime::WorkQueue::Run at work-queue.cpp:50
#<!-- -->4  Fortran::runtime::AssignTicket::Continue at assign.cpp:474
#<!-- -->5  Fortran::runtime::WorkQueue::ContinueItem at work-queue.cpp:90
#<!-- -->6  Fortran::runtime::WorkQueue::Run at work-queue.cpp:50
#<!-- -->7  Fortran::runtime::Assign at assign.cpp:272
#<!-- -->8  _FortranAAssign at assign.cpp:793
#<!-- -->9  _QQmain at reproducer.f90:29

SizeInBytes() returns 30 for real(kind=10) :: arr(3), but the correct size on x86-64 is 48.

Root cause

GetElementByteSize() currently does this:

case TypeCategory::Real:
  return kind_;
case TypeCategory::Complex:
  return 2 * kind_;

For kind 10 on x86-64, that is smaller than the actual storage size.

Proposed fix

PR #192049 updates GetElementByteSize() to use Descriptor::BytesFor(category(), kind_) for Real and Complex.

llvmbot avatar Apr 14 '26 17:04 llvmbot