File: lower_byte_operators.cpp

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (521 lines) | stat: -rw-r--r-- 17,229 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*******************************************************************\

 Module: Unit tests for byte_extract/byte_update lowering

 Author: Michael Tautschnig

\*******************************************************************/

#include <util/arith_tools.h>
#include <util/byte_operators.h>
#include <util/c_types.h>
#include <util/cmdline.h>
#include <util/config.h>
#include <util/expr_util.h>
#include <util/namespace.h>
#include <util/pointer_offset_size.h>
#include <util/simplify_expr.h>
#include <util/simplify_utils.h>
#include <util/std_types.h>
#include <util/string_constant.h>
#include <util/symbol_table.h>

#include <testing-utils/use_catch.h>

TEST_CASE("byte extract and bits", "[core][util][lowering][byte_extract]")
{
  // this test does require a proper architecture to be set so that byte extract
  // uses adequate endianness
  cmdlinet cmdline;
  config.set(cmdline);

  const symbol_tablet symbol_table;
  const namespacet ns(symbol_table);

  const unsignedbv_typet u16{16};
  const exprt sixteen_bits = from_integer(0x1234, u16);
  const array_typet bit_array_type{bv_typet{1}, from_integer(16, size_type())};

  bool little_endian;
  GIVEN("Little endian")
  {
    little_endian = true;

    const auto bit_string = expr2bits(sixteen_bits, little_endian, ns);
    REQUIRE(bit_string.has_value());
    REQUIRE(bit_string->size() == 16);

    const auto array_of_bits =
      bits2expr(*bit_string, bit_array_type, little_endian, ns);
    REQUIRE(array_of_bits.has_value());

    const auto bit_string2 = expr2bits(*array_of_bits, little_endian, ns);
    REQUIRE(bit_string2.has_value());
    REQUIRE(*bit_string == *bit_string2);

    const byte_extract_exprt be1{
      little_endian ? ID_byte_extract_little_endian
                    : ID_byte_extract_big_endian,
      sixteen_bits,
      from_integer(0, c_index_type()),
      config.ansi_c.char_width,
      bit_array_type};
    const exprt lower_be1 = lower_byte_extract(be1, ns);
    REQUIRE(lower_be1 == *array_of_bits);
  }

  GIVEN("Big endian")
  {
    little_endian = false;

    const auto bit_string = expr2bits(sixteen_bits, little_endian, ns);
    REQUIRE(bit_string.has_value());
    REQUIRE(bit_string->size() == 16);

    const auto array_of_bits =
      bits2expr(*bit_string, bit_array_type, little_endian, ns);
    REQUIRE(array_of_bits.has_value());

    const auto bit_string2 = expr2bits(*array_of_bits, little_endian, ns);
    REQUIRE(bit_string2.has_value());
    REQUIRE(*bit_string == *bit_string2);

    const byte_extract_exprt be1{
      little_endian ? ID_byte_extract_little_endian
                    : ID_byte_extract_big_endian,
      sixteen_bits,
      from_integer(0, c_index_type()),
      config.ansi_c.char_width,
      bit_array_type};
    const exprt lower_be1 = lower_byte_extract(be1, ns);
    REQUIRE(lower_be1 == *array_of_bits);
  }
}

SCENARIO("byte_extract_lowering", "[core][util][lowering][byte_extract]")
{
  // this test does require a proper architecture to be set so that byte extract
  // uses adequate endianness
  cmdlinet cmdline;
  config.set(cmdline);

  const symbol_tablet symbol_table;
  const namespacet ns(symbol_table);

  GIVEN("A byte_extract over a POD")
  {
    const exprt deadbeef = from_integer(0xdeadbeef, unsignedbv_typet(32));
    const byte_extract_exprt be1(
      ID_byte_extract_little_endian,
      deadbeef,
      from_integer(1, c_index_type()),
      config.ansi_c.char_width,
      signedbv_typet(config.ansi_c.char_width));

    THEN("byte_extract lowering yields the expected value")
    {
      const exprt lower_be1 = lower_byte_extract(be1, ns);
      const exprt lower_be1_s = simplify_expr(lower_be1, ns);

      REQUIRE(!has_subexpr(lower_be1, ID_byte_extract_little_endian));
      REQUIRE(lower_be1.type() == be1.type());
      REQUIRE(lower_be1_s == from_integer(0xbe, signedbv_typet(8)));

      byte_extract_exprt be2 = be1;
      be2.id(ID_byte_extract_big_endian);
      const exprt lower_be2 = lower_byte_extract(be2, ns);
      const exprt lower_be2_s = simplify_expr(lower_be2, ns);

      REQUIRE(!has_subexpr(lower_be2, ID_byte_extract_big_endian));
      REQUIRE(lower_be2.type() == be2.type());
      REQUIRE(lower_be2_s == from_integer(0xad, signedbv_typet(8)));
    }
  }

  GIVEN("A an unbounded byte_extract over a bounded operand")
  {
    const exprt deadbeef = from_integer(0xdeadbeef, unsignedbv_typet(32));
    const byte_extract_exprt be1(
      ID_byte_extract_little_endian,
      deadbeef,
      from_integer(1, c_index_type()),
      config.ansi_c.char_width,
      struct_typet(
        {{"unbounded_array",
          array_typet(
            unsignedbv_typet(16), exprt(ID_infinity, size_type()))}}));

    THEN("byte_extract lowering does not raise an exception")
    {
      const exprt lower_be1 = lower_byte_extract(be1, ns);

      REQUIRE(!has_subexpr(lower_be1, ID_byte_extract_little_endian));
      REQUIRE(lower_be1.type() == be1.type());

      byte_extract_exprt be2 = be1;
      be2.id(ID_byte_extract_big_endian);
      const exprt lower_be2 = lower_byte_extract(be2, ns);

      REQUIRE(!has_subexpr(lower_be2, ID_byte_extract_big_endian));
      REQUIRE(lower_be2.type() == be2.type());
    }
  }

  GIVEN("A an unbounded union byte_extract over a bounded operand")
  {
    const exprt deadbeef = from_integer(0xdeadbeef, unsignedbv_typet(32));
    const byte_extract_exprt be1(
      ID_byte_extract_little_endian,
      deadbeef,
      from_integer(1, c_index_type()),
      config.ansi_c.char_width,
      union_typet(
        {{"unbounded_array",
          array_typet(
            unsignedbv_typet(16), exprt(ID_infinity, size_type()))}}));

    THEN("byte_extract lowering does not raise an exception")
    {
      const exprt lower_be1 = lower_byte_extract(be1, ns);

      REQUIRE(!has_subexpr(lower_be1, ID_byte_extract_little_endian));
      REQUIRE(lower_be1.type() == be1.type());

      byte_extract_exprt be2 = be1;
      be2.id(ID_byte_extract_big_endian);
      const exprt lower_be2 = lower_byte_extract(be2, ns);

      REQUIRE(!has_subexpr(lower_be2, ID_byte_extract_big_endian));
      REQUIRE(lower_be2.type() == be2.type());
    }
  }

  GIVEN("A an empty union byte_extract over a bounded operand")
  {
    const exprt deadbeef = from_integer(0xdeadbeef, unsignedbv_typet(32));
    const byte_extract_exprt be1(
      ID_byte_extract_little_endian,
      deadbeef,
      from_integer(1, c_index_type()),
      config.ansi_c.char_width,
      union_typet{});

    THEN("byte_extract lowering does not raise an exception")
    {
      const exprt lower_be1 = lower_byte_extract(be1, ns);

      REQUIRE(!has_subexpr(lower_be1, ID_byte_extract_little_endian));
      REQUIRE(lower_be1.type() == be1.type());

      byte_extract_exprt be2 = be1;
      be2.id(ID_byte_extract_big_endian);
      const exprt lower_be2 = lower_byte_extract(be2, ns);

      REQUIRE(!has_subexpr(lower_be2, ID_byte_extract_big_endian));
      REQUIRE(lower_be2.type() == be2.type());
    }
  }

  GIVEN("A a byte_extract from a string constant")
  {
    string_constantt s("ABCD");
    const byte_extract_exprt be1(
      ID_byte_extract_little_endian,
      s,
      from_integer(1, c_index_type()),
      config.ansi_c.char_width,
      unsignedbv_typet(16));

    THEN("byte_extract lowering yields the expected value")
    {
      const exprt lower_be1 = lower_byte_extract(be1, ns);

      REQUIRE(!has_subexpr(lower_be1, ID_byte_extract_little_endian));
      REQUIRE(lower_be1.type() == be1.type());
      REQUIRE(
        lower_be1 == from_integer((int{'C'} << 8) + 'B', unsignedbv_typet(16)));

      byte_extract_exprt be2 = be1;
      be2.id(ID_byte_extract_big_endian);
      const exprt lower_be2 = lower_byte_extract(be2, ns);

      REQUIRE(!has_subexpr(lower_be2, ID_byte_extract_big_endian));
      REQUIRE(lower_be2.type() == be2.type());
      REQUIRE(
        lower_be2 == from_integer((int{'B'} << 8) + 'C', unsignedbv_typet(16)));
    }
  }

  GIVEN("A collection of types")
  {
    unsignedbv_typet u8(8);
    signedbv_typet s8(8);
    unsignedbv_typet u16(16);
    signedbv_typet s16(16);
    unsignedbv_typet u32(32);
    signedbv_typet s32(32);
    unsignedbv_typet u64(64);
    signedbv_typet s64(64);

    constant_exprt size = from_integer(8, size_type());

    std::vector<typet> types = {
      struct_typet({{"comp1", u16}, {"comp2", u16}}),
      struct_typet({{"comp1", u32}, {"comp2", u64}}),
      struct_typet(
        {{"comp1", u32},
         {"compX", c_bit_field_typet(u8, 4)},
         {"pad", c_bit_field_typet(u8, 4)},
         {"comp2", u8}}),
      union_typet({{"compA", u32}, {"compB", u64}}),
      c_enum_typet(u16),
      c_enum_typet(unsignedbv_typet(128)),
      array_typet(u8, size),
      array_typet(s32, size),
      array_typet(u64, size),
      unsignedbv_typet(24),
      unsignedbv_typet(128),
      signedbv_typet(24),
      signedbv_typet(128),
      ieee_float_spect::single_precision().to_type(),
      // generates the correct value, but remains wrapped in a typecast
      // pointer_typet(u64, sizeof(void *) * CHAR_BIT),
      vector_typet(size_type(), u8, size),
      vector_typet(size_type(), u64, size),
      complex_typet(s16),
      complex_typet(u64)};

    THEN("byte_extract lowering yields the expected value")
    {
      for(const auto &endianness :
          {ID_byte_extract_little_endian, ID_byte_extract_big_endian})
      {
        for(const auto &t1 : types)
        {
          std::ostringstream oss;
          for(int i = 0; i < 64; ++i)
          {
            std::string bits = integer2binary(i, 8);
            std::reverse(bits.begin(), bits.end());
            oss << bits;
          }

          const auto type_bits = pointer_offset_bits(t1, ns);
          REQUIRE(type_bits);
          const auto type_bits_int = numeric_cast_v<std::size_t>(*type_bits);
          REQUIRE(type_bits_int <= oss.str().size());
          const auto s = bits2expr(
            oss.str().substr(0, type_bits_int),
            t1,
            endianness == ID_byte_extract_little_endian,
            ns);
          REQUIRE(s.has_value());

          for(const auto &t2 : types)
          {
            oss.str("");
            for(int i = 2; i < 64; ++i)
            {
              std::string bits = integer2binary(i, 8);
              std::reverse(bits.begin(), bits.end());
              oss << bits;
            }

            const auto type_bits_2 = pointer_offset_bits(t2, ns);
            REQUIRE(type_bits_2);

            // for now only extract within bounds
            if(*type_bits_2 + 16 > *type_bits)
              continue;

            const auto type_bits_2_int =
              numeric_cast_v<std::size_t>(*type_bits_2);
            REQUIRE(type_bits_2_int <= oss.str().size());
            const auto r = bits2expr(
              oss.str().substr(0, type_bits_2_int),
              t2,
              endianness == ID_byte_extract_little_endian,
              ns);
            REQUIRE(r.has_value());

            const byte_extract_exprt be(
              endianness,
              *s,
              from_integer(2, c_index_type()),
              config.ansi_c.char_width,
              t2);

            const exprt lower_be = lower_byte_extract(be, ns);
            const exprt lower_be_s = simplify_expr(lower_be, ns);

            REQUIRE(!has_subexpr(lower_be, ID_byte_extract_little_endian));
            REQUIRE(!has_subexpr(lower_be, ID_byte_extract_big_endian));
            REQUIRE(lower_be.type() == be.type());
            REQUIRE(lower_be == *r);
            REQUIRE(lower_be_s == *r);
          }
        }
      }
    }
  }
}

SCENARIO("byte_update_lowering", "[core][util][lowering][byte_update]")
{
  // this test does require a proper architecture to be set so that byte extract
  // uses adequate endianness
  cmdlinet cmdline;
  config.set(cmdline);

  const symbol_tablet symbol_table;
  const namespacet ns(symbol_table);

  GIVEN("A byte_update of a POD")
  {
    const exprt deadbeef = from_integer(0xdeadbeef, unsignedbv_typet(32));
    const byte_update_exprt bu1(
      ID_byte_update_little_endian,
      deadbeef,
      from_integer(1, c_index_type()),
      from_integer(0x42, unsignedbv_typet(8)),
      config.ansi_c.char_width);

    THEN("byte_update lowering yields the expected value")
    {
      const exprt lower_bu1 = lower_byte_operators(bu1, ns);
      const exprt lower_bu1_s = simplify_expr(lower_bu1, ns);

      REQUIRE(!has_subexpr(lower_bu1, ID_byte_extract_little_endian));
      REQUIRE(!has_subexpr(lower_bu1, ID_byte_update_little_endian));
      REQUIRE(lower_bu1.type() == bu1.type());
      REQUIRE(lower_bu1_s == from_integer(0xdead42ef, unsignedbv_typet(32)));

      byte_update_exprt bu2 = bu1;
      bu2.id(ID_byte_update_big_endian);
      const exprt lower_bu2 = lower_byte_operators(bu2, ns);
      const exprt lower_bu2_s = simplify_expr(lower_bu2, ns);

      REQUIRE(!has_subexpr(lower_bu2, ID_byte_extract_big_endian));
      REQUIRE(!has_subexpr(lower_bu2, ID_byte_update_big_endian));
      REQUIRE(lower_bu2.type() == bu2.type());
      REQUIRE(lower_bu2_s == from_integer(0xde42beef, unsignedbv_typet(32)));
    }
  }

  GIVEN("A collection of types")
  {
    unsignedbv_typet u8(8);
    signedbv_typet s8(8);
    unsignedbv_typet u16(16);
    signedbv_typet s16(16);
    unsignedbv_typet u32(32);
    signedbv_typet s32(32);
    unsignedbv_typet u64(64);
    signedbv_typet s64(64);

    constant_exprt size = from_integer(8, size_type());

    std::vector<typet> types = {
      struct_typet({{"comp1", u16}, {"comp2", u16}}),
      struct_typet({{"comp1", u32}, {"comp2", u64}}),
      struct_typet(
        {{"comp1", u32},
         {"compX", c_bit_field_typet(u8, 4)},
         {"pad", c_bit_field_typet(u8, 4)},
         {"comp2", u8}}),
      union_typet({{"compA", u32}, {"compB", u64}}),
      c_enum_typet(u16),
      c_enum_typet(unsignedbv_typet(128)),
      array_typet(u8, size),
      array_typet(s32, size),
      array_typet(u64, size),
      unsignedbv_typet(24),
      unsignedbv_typet(128),
      signedbv_typet(24),
      signedbv_typet(128),
      ieee_float_spect::single_precision().to_type(),
      // generates the correct value, but remains wrapped in a typecast
      // pointer_typet(u64, sizeof(void *) * CHAR_BIT),
      vector_typet(size_type(), u8, size),
      vector_typet(size_type(), u64, size),
      // complex_typet(s16),
      // complex_typet(u64)
    };

    THEN("byte_update lowering yields the expected value")
    {
      for(const auto &endianness :
          {ID_byte_update_little_endian, ID_byte_update_big_endian})
      {
        for(const auto &t1 : types)
        {
          std::ostringstream oss;
          for(int i = 0; i < 64; ++i)
          {
            std::string bits = integer2binary(i, 8);
            std::reverse(bits.begin(), bits.end());
            oss << bits;
          }

          const auto type_bits = pointer_offset_bits(t1, ns);
          REQUIRE(type_bits);
          const auto type_bits_int = numeric_cast_v<std::size_t>(*type_bits);
          REQUIRE(type_bits_int <= oss.str().size());
          const std::string s_string = oss.str().substr(0, type_bits_int);
          const auto s = bits2expr(
            s_string, t1, endianness == ID_byte_update_little_endian, ns);
          REQUIRE(s.has_value());

          for(const auto &t2 : types)
          {
            oss.str("");
            for(int i = 64; i < 128; ++i)
            {
              std::string bits = integer2binary(i, 8);
              std::reverse(bits.begin(), bits.end());
              oss << bits;
            }

            const auto type_bits_2 = pointer_offset_bits(t2, ns);
            REQUIRE(type_bits_2);

            // for now only update within bounds
            if(*type_bits_2 + 16 > *type_bits)
              continue;

            const auto type_bits_2_int =
              numeric_cast_v<std::size_t>(*type_bits_2);
            REQUIRE(type_bits_2_int <= oss.str().size());
            const std::string u_string = oss.str().substr(0, type_bits_2_int);
            const auto u = bits2expr(
              u_string, t2, endianness == ID_byte_update_little_endian, ns);
            REQUIRE(u.has_value());

            std::string r_string = s_string;
            r_string.replace(16, u_string.size(), u_string);
            const auto r = bits2expr(
              r_string, t1, endianness == ID_byte_update_little_endian, ns);
            REQUIRE(r.has_value());

            const byte_update_exprt bu(
              endianness,
              *s,
              from_integer(2, c_index_type()),
              *u,
              config.ansi_c.char_width);

            const exprt lower_bu = lower_byte_operators(bu, ns);
            const exprt lower_bu_s = simplify_expr(lower_bu, ns);

            REQUIRE(!has_subexpr(lower_bu, endianness));
            REQUIRE(!has_subexpr(lower_bu, ID_byte_extract_big_endian));
            REQUIRE(!has_subexpr(lower_bu, ID_byte_extract_little_endian));
            REQUIRE(lower_bu.type() == bu.type());
            REQUIRE(lower_bu == *r);
            REQUIRE(lower_bu_s == *r);
          }
        }
      }
    }
  }
}