From 56d01184e3b69688dec04bd97a3017df9462f4fc Mon Sep 17 00:00:00 2001 From: Alexander Egorenkov Date: Sat, 4 Jul 2020 13:54:43 +0200 Subject: executor: fix bitfields for big-endian arch Add bitfield tests for big-endian arch Issue: #1885 Signed-off-by: Alexander Egorenkov --- executor/executor.cc | 12 +++++++++++- executor/test.h | 13 +++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'executor') diff --git a/executor/executor.cc b/executor/executor.cc index d0b352fc8..1e7625d7e 100644 --- a/executor/executor.cc +++ b/executor/executor.cc @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -1157,12 +1158,21 @@ void copyin_int(char* addr, uint64 val, uint64 bf, uint64 bf_off, uint64 bf_len) return; } T x = swap(*(T*)addr, sizeof(T), bf); - x = (x & ~BITMASK(bf_off, bf_len)) | ((val << bf_off) & BITMASK(bf_off, bf_len)); + debug_verbose("copyin_int<%zu>: old x=0x%llx\n", sizeof(T), (uint64)x); +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + const uint64 shift = sizeof(T) * CHAR_BIT - bf_off - bf_len; +#else + const uint64 shift = bf_off; +#endif + x = (x & ~BITMASK(shift, bf_len)) | ((val << shift) & BITMASK(shift, bf_len)); + debug_verbose("copyin_int<%zu>: new x=0x%llx\n", sizeof(T), (uint64)x); *(T*)addr = swap(x, sizeof(T), bf); } void copyin(char* addr, uint64 val, uint64 size, uint64 bf, uint64 bf_off, uint64 bf_len) { + debug_verbose("copyin: addr=%p val=0x%llx size=%llu bf=%llu bf_off=%llu bf_len=%llu\n", + addr, val, size, bf, bf_off, bf_len); if (bf != binary_format_native && bf != binary_format_bigendian && (bf_off != 0 || bf_len != 0)) fail("bitmask for string format %llu/%llu", bf_off, bf_len); switch (bf) { diff --git a/executor/test.h b/executor/test.h index 0d3587b84..cec0228e2 100644 --- a/executor/test.h +++ b/executor/test.h @@ -14,7 +14,7 @@ static int test_copyin() if (x[0] != 0 || x[1] != 0 || x[2] != 0x34 || x[3] != 0x12 || x[4] != 0 || x[5] != 0) { - printf("bad result of STORE_BY_BITMASK(0, 0): %x %x %x %x %x %x\n", + printf("bad result of STORE_BY_BITMASK(le16, 0x1234, 0, 16): %x %x %x %x %x %x\n", x[0], x[1], x[2], x[3], x[4], x[5]); return 1; } @@ -23,7 +23,16 @@ static int test_copyin() if (x[0] != 0 || x[1] != 0 || x[2] != 0x54 || x[3] != 0x13 || x[4] != 0 || x[5] != 0) { - printf("bad result of STORE_BY_BITMASK(7, 3): %x %x %x %x %x %x\n", + printf("bad result of STORE_BY_BITMASK(le16, 0x555a, 5, 4): %x %x %x %x %x %x\n", + x[0], x[1], x[2], x[3], x[4], x[5]); + return 1; + } + STORE_BY_BITMASK(uint16, htobe16, &buf[1], 0x4567, 13, 3); + memcpy(x, buf, sizeof(x)); + if (x[0] != 0 || x[1] != 0 || + x[2] != 0xf4 || x[3] != 0x13 || + x[4] != 0 || x[5] != 0) { + printf("bad result of STORE_BY_BITMASK(be16, 0x4567, 13, 3): %x %x %x %x %x %x\n", x[0], x[1], x[2], x[3], x[4], x[5]); return 1; } -- cgit mrf-deployment