From bdfb584b294bf1379921b08ec020386b8ff6257b Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 5 Nov 2020 22:39:54 +1100 Subject: [PATCH] extmod/moductypes: Fix storing to (U)INT64 arrays on 32-bit archs. Fixes issue #6583. Signed-off-by: Damien George --- extmod/moductypes.c | 2 +- tests/extmod/uctypes_array_load_store.py | 19 ++++++++++++++++ tests/extmod/uctypes_array_load_store.py.exp | 24 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/extmod/uctypes_array_load_store.py create mode 100644 tests/extmod/uctypes_array_load_store.py.exp diff --git a/extmod/moductypes.c b/extmod/moductypes.c index c5fbf12e42..79a49d5c32 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -399,7 +399,7 @@ STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) { ((uint64_t *)p)[index] = (uint64_t)v; } else { // TODO: Doesn't offer atomic store semantics, but should at least try - set_unaligned(val_type, p, MP_ENDIANNESS_BIG, val); + set_unaligned(val_type, (void *)&((uint64_t *)p)[index], MP_ENDIANNESS_BIG, val); } return; default: diff --git a/tests/extmod/uctypes_array_load_store.py b/tests/extmod/uctypes_array_load_store.py new file mode 100644 index 0000000000..709b9f5c29 --- /dev/null +++ b/tests/extmod/uctypes_array_load_store.py @@ -0,0 +1,19 @@ +# Test uctypes array, load and store, with array size > 1 + +try: + import uctypes +except ImportError: + print("SKIP") + raise SystemExit + +N = 3 + +for endian in ("NATIVE", "LITTLE_ENDIAN", "BIG_ENDIAN"): + for type_ in ("INT8", "UINT8", "INT16", "UINT16", "INT32", "UINT32", "INT64", "UINT64"): + desc = {"arr": (uctypes.ARRAY | 0, getattr(uctypes, type_) | N)} + sz = uctypes.sizeof(desc) + data = bytearray(sz) + s = uctypes.struct(uctypes.addressof(data), desc, getattr(uctypes, endian)) + for i in range(N): + s.arr[i] = i + print(endian, type_, sz, *(s.arr[i] for i in range(N))) diff --git a/tests/extmod/uctypes_array_load_store.py.exp b/tests/extmod/uctypes_array_load_store.py.exp new file mode 100644 index 0000000000..f6f7bc96ef --- /dev/null +++ b/tests/extmod/uctypes_array_load_store.py.exp @@ -0,0 +1,24 @@ +NATIVE INT8 3 0 1 2 +NATIVE UINT8 3 0 1 2 +NATIVE INT16 6 0 1 2 +NATIVE UINT16 6 0 1 2 +NATIVE INT32 12 0 1 2 +NATIVE UINT32 12 0 1 2 +NATIVE INT64 24 0 1 2 +NATIVE UINT64 24 0 1 2 +LITTLE_ENDIAN INT8 3 0 1 2 +LITTLE_ENDIAN UINT8 3 0 1 2 +LITTLE_ENDIAN INT16 6 0 1 2 +LITTLE_ENDIAN UINT16 6 0 1 2 +LITTLE_ENDIAN INT32 12 0 1 2 +LITTLE_ENDIAN UINT32 12 0 1 2 +LITTLE_ENDIAN INT64 24 0 1 2 +LITTLE_ENDIAN UINT64 24 0 1 2 +BIG_ENDIAN INT8 3 0 1 2 +BIG_ENDIAN UINT8 3 0 1 2 +BIG_ENDIAN INT16 6 0 1 2 +BIG_ENDIAN UINT16 6 0 1 2 +BIG_ENDIAN INT32 12 0 1 2 +BIG_ENDIAN UINT32 12 0 1 2 +BIG_ENDIAN INT64 24 0 1 2 +BIG_ENDIAN UINT64 24 0 1 2