Skip to content

Commit 8f465df

Browse files
agattidpgeorge
authored andcommitted
unix/modffi: Restrict uint32_t values to 32 bits.
This commit clears the upper 32 bits of returned `uint32_t` values, which are handled as `unsigned int`s by the MicroPython runtime and thus could be extended to 64 bits on some platforms. RV64 holds 32-bit values as signed integers when held in registers, but the code handling the FFI unsigned int case did not take this into account. That introduced test failures when a 32-bit value had its most significant bit set, as when performing the value extension from 32 to 64 bits, the upper half of the value would be filled with ones. On 32 bit platforms this change should be converted to a no-op, and on other 64 bit platforms that aren't RISC-V it shouldn't hurt as the value being manipulated is expected to only hold valid bits in its lower half. Signed-off-by: Alessandro Gatti <[email protected]>
1 parent bb3c711 commit 8f465df

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

ports/unix/modffi.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,12 @@ static mp_obj_t return_ffi_value(ffi_union_t *val, char type) {
191191
case 'i':
192192
case 'l':
193193
return mp_obj_new_int((ffi_sarg)val->ffi);
194+
case 'I':
195+
// On RV64, 32-bit values are stored as signed integers inside the
196+
// holding register.
197+
return mp_obj_new_int_from_uint(val->ffi & 0xFFFFFFFF);
194198
case 'B':
195199
case 'H':
196-
case 'I':
197200
case 'L':
198201
return mp_obj_new_int_from_uint(val->ffi);
199202
case 'q':

0 commit comments

Comments
 (0)