Skip to content

Conversation

@mchlswyr
Copy link
Contributor

@mchlswyr mchlswyr commented Sep 2, 2024

Summary

This PR builds on #7358. In return_ffi_value, integers of type char, short, and long are cast to type int. In an x86-64 Linux build, long is 64-bit and int is 32-bit, so the value is truncated.

Testing

I added some long tests. The tests could not be added to the existing ffi_types test because two .exp files were required for the 32-bit and 64-bit results. Code common to both the ffi_types and long tests was factored into ffi_int_base. ffi_types was renamed to ffi_int_types to group the related tests under the "ffi_int" prefix.

I ran the tests in the following four cases:

  1. Old modffi.c and MICROPY_FORCE_32BIT=0: ffi_int_long64 will fail;
  2. Old modffi.c and MICROPY_FORCE_32BIT=1: all tests will pass or skip;
  3. New modffi.c and MICROPY_FORCE_32BIT=0: all tests will pass or skip;
  4. New modffi.c and MICROPY_FORCE_32BIT=1: all tests will pass or skip.

@codecov
Copy link

codecov bot commented Sep 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.43%. Comparing base (1897fe6) to head (efb47ee).
Report is 54 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #15767   +/-   ##
=======================================
  Coverage   98.43%   98.43%           
=======================================
  Files         163      163           
  Lines       21294    21294           
=======================================
  Hits        20960    20960           
  Misses        334      334           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions
Copy link

github-actions bot commented Sep 2, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:   -80 -0.010% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@mchlswyr mchlswyr changed the title ports/unix/modffi: Remove downcast in return_ffi_value. ports/unix/modffi: Fix signed integer cast in return_ffi_value. Sep 3, 2024
Casting an ffi_arg to a signed int may truncate the value. E.g., when the
ffi_arg is 64-bit and the signed int is 32-bit. Also, casting an ffi_arg
to a larger signed type will not sign extend the value. E.g., when the
ffi_arg is 32-bit and the larger signed type is int64_t. If the value is
signed, it should be cast to ffi_sarg, which is the same size as ffi_arg.

Signed-off-by: Michael Sawyer <mjfsawyer@gmail.com>
Added the "long" modffi tests. The tests could not be added to the existing
ffi_types test because two .exp files were required for the 32-bit and
64-bit results. Code common to both the ffi_types and type "long" tests was
factored into ffi_int_base. ffi_types was renamed to ffi_int_types to group
the related tests under the "ffi_int" prefix.

Signed-off-by: Michael Sawyer <mjfsawyer@gmail.com>
Copy link
Contributor

@projectgus projectgus left a comment

Choose a reason for hiding this comment

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

This looks very good to me, especially the additional test coverage. Thanks, @mchlswyr!

@projectgus projectgus requested a review from dpgeorge September 10, 2024 01:47
Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

@dpgeorge
Copy link
Member

Rebased and merged in b05983f and 230e521

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants