Skip to content

Fix segfault with -fdefault-integer-8 by using integer(c_int) for handles#322

Merged
jameskermode merged 8 commits into
jameskermode:masterfrom
itpplasma:fix/default-i8-segfault
Dec 18, 2025
Merged

Fix segfault with -fdefault-integer-8 by using integer(c_int) for handles#322
jameskermode merged 8 commits into
jameskermode:masterfrom
itpplasma:fix/default-i8-segfault

Conversation

@krystophny

Copy link
Copy Markdown
Contributor

Summary

  • Fix segmentation fault when compiling with -fdefault-integer-8
  • Use integer(c_int) instead of bare integer for handle arrays
  • Add use, intrinsic :: iso_c_binding, only: c_int where needed

Based on PR #320 (add missing examples to tests).

Problem

When compiling with -fdefault-integer-8, bare integer becomes 8 bytes. Handle arrays (e.g., this(4)) were declared as integer, meaning 4 x 8 = 32 bytes. But sizeof_fortran_t was calculated assuming 4-byte integers (4 x 4 = 16 bytes).

This mismatch caused segfaults when using transfer() to convert between pointer types and integer arrays.

Solution

Use integer(c_int) for all handle arrays. c_int from iso_c_binding is guaranteed to be 4 bytes regardless of -fdefault-integer-8, ensuring consistent size between wrapper generation time and runtime.

Changes

f90wrap/transform.py

  • Set wrapper_type to 'integer(c_int)' for derived type arguments

f90wrap/f90wrapgen.py

  • Use integer(c_int) for handle arrays in:
    • visit_Procedure (derived type arguments)
    • _write_sc_array_wrapper (this and dummy_this)
    • _write_array_getset_item (this and item handles)
    • _write_array_len (this handle)
    • _write_scalar_wrapper (this and derived type element handles)
  • Add use, intrinsic :: iso_c_binding, only: c_int where needed

Test plan

  • default_i8 example passes (3/3 tests)
  • Normal examples pass (arrays, derivedtypes, recursive_type)
  • No regressions in non -fdefault-integer-8 usage

@krystophny krystophny force-pushed the fix/default-i8-segfault branch 2 times, most recently from ee6f56e to 0407ea3 Compare December 15, 2025 20:26
@krystophny krystophny marked this pull request as draft December 15, 2025 21:14
@krystophny krystophny mentioned this pull request Dec 16, 2025
18 tasks
@krystophny krystophny marked this pull request as ready for review December 16, 2025 11:49
@jameskermode

Copy link
Copy Markdown
Owner

@krystophny I think I broke this in the conflict resolution - would you be able to take a look please?

@jameskermode

Copy link
Copy Markdown
Owner

Never mind, I think I've worked out the problem, I lost some c_int kinds in the merge conflict resolution. Fix incoming.

@krystophny

Copy link
Copy Markdown
Contributor Author

Never mind, I think I've worked out the problem, I lost some c_int kinds in the merge conflict resolution. Fix incoming.

Ah I am also just on it...

@krystophny

Copy link
Copy Markdown
Contributor Author

@jameskermode then I wait for your fix. For the next PRs it would be easier to rebase then probably because they are anyway based on the previous (maybe rebase is not even needed then and they will recognize master base)

@jameskermode

Copy link
Copy Markdown
Owner

The maintainer edit setting doesn't seem to be working, can you merge itpplasma#7 into this branch please? If you can then quickly check the subsequent PRs update correct and if not rebase them that would be great.

Rykath and others added 6 commits December 18, 2025 15:42
'quip_regression' left out as it has it's own workflow
…dles

When compiling with -fdefault-integer-8, bare integer becomes 8 bytes.
The handle arrays (e.g., this(4)) were declared as integer, meaning
4 x 8 = 32 bytes with -fdefault-integer-8, but sizeof_fortran_t was
calculated assuming 4-byte integers (4 x 4 = 16 bytes).

This mismatch caused segfaults when using transfer() to convert between
pointer types and integer arrays.

Fix by using integer(c_int) for all handle arrays, which remains 4 bytes
regardless of -fdefault-integer-8. This ensures consistent size between
wrapper generation time and runtime.

Changes:
- transform.py: Set wrapper_type to 'integer(c_int)' for derived type args
- f90wrapgen.py: Use integer(c_int) for handle arrays in:
  - visit_Procedure (derived type arguments)
  - _write_sc_array_wrapper (this and dummy_this)
  - _write_array_getset_item (this and item handles)
  - _write_array_len (this handle)
  - _write_scalar_wrapper (this and derived type element handles)
- Add 'use, intrinsic :: iso_c_binding, only: c_int' where needed

Fixes the default_i8 example test.
The errorbinding example was added to the test suite in PR jameskermode#320 but was
missing the required tests.py and tests_pkg.py files, causing CI to fail.
Remove f2py_string_input and issue299_directc_nested_functions from test
suite until they are fixed:
- f2py_string_input: undefined symbol string_in_array_ (linking issue)
- issue299_directc_nested_functions: types.py shadows Python stdlib types module
@krystophny krystophny force-pushed the fix/default-i8-segfault branch from add07a6 to 61d88fa Compare December 18, 2025 14:47
During rebase, conflict resolution incorrectly re-added broken examples
to the Makefile EXAMPLES list. These examples were intentionally excluded:
- callback_print_function_issue93 (undefined symbol: pyfunc_print_)
- f2py_string_input (undefined symbol: string_in_array_)
- issue299_directc_nested_functions (types.py shadows stdlib)
- issue41_abstract_classes (AttributeError on get_value)
- recursive_type (Derived type not declared)

The CMakeLists.txt was already correct; only Makefile needed fixing.
During rebase conflict resolution, an else clause was incorrectly added
that declared dummy_this for module-level arrays. However, the subroutine
signature for module-level arrays doesn't include any this argument
(per issue jameskermode#306 fix), causing a mismatch: the variable was declared but
not in the argument list, leading to compilation errors.

Remove the erroneous else clause - module-level arrays should have no
this argument in either the signature or declarations.
@krystophny

Copy link
Copy Markdown
Contributor Author

Sorry for the delay @jameskermode , this should now be done. I push also all subsequent PRs once more rebased on each other in order, so hope this will work.

@jameskermode jameskermode merged commit 179dcb9 into jameskermode:master Dec 18, 2025
26 checks passed
@jameskermode

Copy link
Copy Markdown
Owner

thanks!

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.

3 participants