From 7b2dcad6bc45f1321c6e5935c02a17e5696680a3 Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Mon, 29 Nov 2021 11:36:44 +0000 Subject: [PATCH] Extend unit test of bits2expr/expr2bits with struct type Structs with sub-byte bit fields demonstrate that big-endian ordering also re-orders the bits within a byte. An executable variant of this unit test was validated on mips (using qemu-mips), demonstrating the same bit ordering. No bug fixes/code changes required, this test just confirms that our endianness interpretation matches actual hardware. --- unit/util/simplify_expr.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/unit/util/simplify_expr.cpp b/unit/util/simplify_expr.cpp index a7f1e19a62b..0c830c84f69 100644 --- a/unit/util/simplify_expr.cpp +++ b/unit/util/simplify_expr.cpp @@ -108,6 +108,30 @@ TEST_CASE("expr2bits and bits2expr respect bit order", "[core][util]") const auto should_be_deadbeef2 = bits2expr(*be, unsignedbv_typet(32), false, ns); REQUIRE(deadbeef == *should_be_deadbeef2); + + c_bit_field_typet four_bits{unsignedbv_typet{8}, 4}; + struct_typet st{{{"s", unsignedbv_typet{16}}, + {"bf1", four_bits}, + {"bf2", four_bits}, + {"b", unsignedbv_typet{8}}}}; + + const auto fill_struct_le = bits2expr(*le, st, true, ns); + REQUIRE(fill_struct_le.has_value()); + REQUIRE( + to_struct_expr(*fill_struct_le).operands()[1] == + from_integer(0xd, four_bits)); + REQUIRE( + to_struct_expr(*fill_struct_le).operands()[2] == + from_integer(0xa, four_bits)); + + const auto fill_struct_be = bits2expr(*be, st, false, ns); + REQUIRE(fill_struct_be.has_value()); + REQUIRE( + to_struct_expr(*fill_struct_be).operands()[1] == + from_integer(0xb, four_bits)); + REQUIRE( + to_struct_expr(*fill_struct_be).operands()[2] == + from_integer(0xe, four_bits)); } TEST_CASE("Simplify extractbit", "[core][util]")