-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathconvert_method.cpp
843 lines (798 loc) · 28.4 KB
/
convert_method.cpp
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*******************************************************************\
Module: Unit tests for converting methods.
Author: Diffblue Limited.
\*******************************************************************/
#include <testing-utils/expr_query.h>
#include <testing-utils/use_catch.h>
#include <util/irep.h>
#include <util/symbol_table.h>
#include <java-testing-utils/load_java_class.h>
#include <java-testing-utils/require_type.h>
#include <java_bytecode/java_bytecode_convert_method_class.h>
#include <java_bytecode/java_utils.h>
#include <testing-utils/message.h>
SCENARIO(
"java_bytecode_convert_bridge_method",
"[core][java_bytecode][java_bytecode_convert_method]")
{
GIVEN("A class with a bridge method")
{
const symbol_tablet symbol_table = load_java_class(
"ClassWithBridgeMethod", "./java_bytecode/java_bytecode_convert_method");
const std::string method_name = "java::ClassWithBridgeMethod.compareTo";
WHEN("When parsing the bridge method")
{
const symbolt function_symbol =
symbol_table.lookup_ref(method_name + ":(Ljava/lang/Object;)I");
const java_method_typet &function_type =
require_type::require_java_method(function_symbol.type);
THEN("The method symbol should be of java_method_typet")
{
REQUIRE(function_type.get_bool(ID_C_java_method_type));
}
THEN("And the method should be marked as a bridge method")
{
REQUIRE(function_type.get_bool(ID_is_bridge_method));
}
THEN("The method should be marked as declared by its class")
{
REQUIRE(
id2string(*declaring_class(function_symbol)) ==
"java::ClassWithBridgeMethod");
}
}
WHEN("When parsing a non-bridge method")
{
const symbolt function_symbol =
symbol_table.lookup_ref(method_name + ":(LClassWithBridgeMethod;)I");
const java_method_typet &function_type =
require_type::require_java_method(function_symbol.type);
THEN("The method should not be marked as a bridge method.")
{
REQUIRE_FALSE(function_type.get_bool(ID_is_bridge_method));
}
THEN("The method should be marked as declared by its class")
{
REQUIRE(
id2string(*declaring_class(function_symbol)) ==
"java::ClassWithBridgeMethod");
}
}
}
GIVEN("A class with a native method")
{
const symbol_tablet symbol_table = load_java_class(
"ClassWithNativeMethod", "./java_bytecode/java_bytecode_convert_method");
const std::string method_name = "java::ClassWithNativeMethod.f";
WHEN("When parsing the native method")
{
const symbolt function_symbol =
symbol_table.lookup_ref(method_name + ":()Z");
const java_method_typet &function_type =
require_type::require_java_method(function_symbol.type);
THEN("The method symbol should be of java_method_typet")
{
REQUIRE(function_type.get_bool(ID_C_java_method_type));
}
THEN("The method should be marked as declared by its class")
{
REQUIRE(
id2string(*declaring_class(function_symbol)) ==
"java::ClassWithNativeMethod");
}
}
WHEN("When parsing a non-native method")
{
const symbolt function_symbol =
symbol_table.lookup_ref(method_name + ":(I)Z");
THEN("The method should be marked as declared by its class")
{
REQUIRE(
id2string(*declaring_class(function_symbol)) ==
"java::ClassWithNativeMethod");
}
}
}
}
SCENARIO(
"java_bytecode_convert_final_method",
"[core][java_bytecode][java_bytecode_convert_method]")
{
GIVEN("A class with final and non-final methods")
{
const symbol_tablet symbol_table = load_java_class(
"ClassWithFinalMethod", "./java_bytecode/java_bytecode_convert_method");
WHEN("When parsing a final method")
{
const symbolt function_symbol =
symbol_table.lookup_ref("java::ClassWithFinalMethod.finalFunc:()I");
THEN("The method should be marked as declared by its class")
{
REQUIRE(
id2string(*declaring_class(function_symbol)) ==
"java::ClassWithFinalMethod");
}
}
WHEN("When parsing a non-final method")
{
const symbolt function_symbol =
symbol_table.lookup_ref("java::ClassWithFinalMethod.nonFinalFunc:()I");
THEN("The method should be marked as declared by its class")
{
REQUIRE(
id2string(*declaring_class(function_symbol)) ==
"java::ClassWithFinalMethod");
}
}
WHEN("When parsing an opaque method")
{
const symbolt function_symbol =
symbol_table.lookup_ref("java::OpaqueClass.staticFunc:()I");
THEN("The method should be marked as declared by its class")
{
REQUIRE(
id2string(*declaring_class(function_symbol)) == "java::OpaqueClass");
}
}
}
}
SCENARIO(
"java_bytecode_convert_varargs_method",
"[core][java_bytecode][java_bytecode_convert_method]")
{
GIVEN("A class with varargs and non-varargs methods")
{
const symbol_tablet symbol_table = load_java_class(
"ClassWithVarArgsMethod", "./java_bytecode/java_bytecode_convert_method");
WHEN("When parsing a method with a variable number of arguments")
{
const symbolt method_symbol = symbol_table.lookup_ref(
"java::ClassWithVarArgsMethod.varArgsFunc:([I)I");
const java_method_typet &method_type =
require_type::require_java_method(method_symbol.type);
THEN("The method should be marked as varargs")
{
REQUIRE(method_type.get_is_varargs());
}
THEN("The method should be marked as declared by its class")
{
REQUIRE(
id2string(*declaring_class(method_symbol)) ==
"java::ClassWithVarArgsMethod");
}
}
WHEN("When parsing a method with constant number of arguments")
{
const symbolt method_symbol = symbol_table.lookup_ref(
"java::ClassWithVarArgsMethod.nonVarArgsFunc:([I)I");
const java_method_typet &method_type =
require_type::require_java_method(method_symbol.type);
THEN("The method should not be marked as varargs")
{
REQUIRE_FALSE(method_type.get_is_varargs());
}
THEN("The method should be marked as declared by its class")
{
REQUIRE(
id2string(*declaring_class(method_symbol)) ==
"java::ClassWithVarArgsMethod");
}
}
}
}
SCENARIO(
"java_bytecode_convert_static_method",
"[core][java_bytecode][java_bytecode_convert_method]")
{
GIVEN("A class with a static method.")
{
const symbol_tablet symbol_table = load_java_class(
"ClassWithStaticMethod", "./java_bytecode/java_bytecode_convert_method");
WHEN("Parsing a static method.")
{
const symbolt method_symbol =
symbol_table.lookup_ref("java::ClassWithStaticMethod.staticFunc:()I");
THEN("The method should be marked as declared by its class")
{
REQUIRE(
id2string(*declaring_class(method_symbol)) ==
"java::ClassWithStaticMethod");
}
}
}
}
SCENARIO(
"java_bytecode_convert_synthetic_method",
"[core][java_bytecode][java_bytecode_convert_method]")
{
GIVEN("A class with a synthetic method.")
{
const symbol_tablet symbol_table = load_java_class(
"ClassWithSyntheticMethod",
"./java_bytecode/java_bytecode_convert_method");
WHEN("Parsing a synthetic method.")
{
const java_method_typet *const synthetic_method =
type_try_dynamic_cast<java_method_typet>(symbol_table.lookup_ref(
"java::ClassWithSyntheticMethod.access$000:()I").type);
REQUIRE(synthetic_method);
THEN("The method should be marked as synthetic.")
{
REQUIRE(synthetic_method->is_synthetic());
}
}
WHEN("Parsing a non synthetic method.")
{
const java_method_typet *const non_synthetic_method =
type_try_dynamic_cast<java_method_typet>(symbol_table.lookup_ref(
"java::ClassWithSyntheticMethod.inaccessible:()I").type);
REQUIRE(non_synthetic_method);
THEN("The method should not be marked as synthetic.")
{
REQUIRE(!non_synthetic_method->is_synthetic());
}
}
}
}
/// Allow access to private methods so that they can be unit tested
class java_bytecode_convert_method_unit_testt
{
public:
static exprt
convert_aload(const irep_idt &statement, const exprt::operandst &op)
{
return java_bytecode_convert_methodt::convert_aload(statement, op);
}
static code_blockt convert_astore(
java_bytecode_convert_methodt &converter,
const irep_idt &statement,
const exprt::operandst &op,
const source_locationt &location)
{
return converter.convert_astore(statement, op, location);
}
static exprt variable(
java_bytecode_convert_methodt &converter,
const exprt &arg,
char type_char,
size_t address)
{
return converter.variable(arg, type_char, address);
}
static void add_variable(
java_bytecode_convert_methodt &converter,
std::size_t index,
symbol_exprt symbol_expr,
std::size_t start_pc,
std::size_t length,
bool is_parameter,
std::vector<java_bytecode_convert_methodt::holet> holes)
{
converter.variables[index].emplace_back(
std::move(symbol_expr), start_pc, length, is_parameter, std::move(holes));
}
static exprt convert_load(
java_bytecode_convert_methodt &converter,
const exprt &index,
char type_char,
size_t address)
{
return converter.convert_load(index, type_char, address);
}
static code_blockt convert_store(
java_bytecode_convert_methodt &converter,
const irep_idt &statement,
const exprt &arg0,
const exprt::operandst &op,
const java_bytecode_convert_methodt::method_offsett address,
const source_locationt &location)
{
return converter.convert_store(statement, arg0, op, address, location);
}
};
SCENARIO(
"baload byte array",
"[core][java_bytecode][java_bytecode_convert_method][convert_aload]")
{
GIVEN("A byte array")
{
const typet byte_array_type = java_array_type('b');
const symbol_exprt byte_array{"byte_array", byte_array_type};
const exprt offset = from_integer(1, java_int_type());
WHEN("baload is called on the byte array")
{
const exprt result =
java_bytecode_convert_method_unit_testt::convert_aload(
"baload", {byte_array, offset});
THEN("The Result is of the form `(int)(*(byte_array->data + offset))`")
// See \ref java_bytecode_promotion on why we need a typecast to int.
{
const auto query = make_query(result)
.as<typecast_exprt>()[0]
.as<dereference_exprt>()[0]
.as<plus_exprt>();
REQUIRE(query[1].get() == offset);
auto member = query[0].as<member_exprt>();
REQUIRE(member.get().get_component_name() == "data");
REQUIRE(member[0].as<dereference_exprt>()[0].get() == byte_array);
REQUIRE(result.type() == java_int_type());
// byte_array->data has type *byte
REQUIRE(member.get().type() == pointer_type(java_byte_type()));
}
}
}
}
SCENARIO(
"baload boolean array",
"[core][java_bytecode][java_bytecode_convert_method][convert_aload]")
{
GIVEN("A boolean array")
{
const typet boolean_array_type = java_array_type('z');
const symbol_exprt boolean_array{"boolean_array", boolean_array_type};
const exprt offset = from_integer(2, java_int_type());
WHEN("baload is called on the byte array")
{
const exprt result =
java_bytecode_convert_method_unit_testt::convert_aload(
"baload", {boolean_array, offset});
THEN("The result is of the form `(int)(*(boolean_array->data + offset))`")
// See \ref java_bytecode_promotion on why we need a typecast to int.
{
const auto query = make_query(result)
.as<typecast_exprt>()[0]
.as<dereference_exprt>()[0]
.as<plus_exprt>();
REQUIRE(query[1].get() == offset);
REQUIRE(
query[0].as<member_exprt>()[0].as<dereference_exprt>()[0].get() ==
boolean_array);
// boolean_array->data has type *boolean
REQUIRE(
query[0].as<member_exprt>().get().type() ==
pointer_type(java_boolean_type()));
}
}
}
}
SCENARIO(
"iaload int array",
"[core][java_bytecode][java_bytecode_convert_method][convert_aload]")
{
GIVEN("An int array")
{
const typet int_array_type = java_array_type('i');
const symbol_exprt int_array{"int_array", int_array_type};
const exprt offset = from_integer(2, java_int_type());
WHEN("iaload is called on the int array")
{
const exprt result =
java_bytecode_convert_method_unit_testt::convert_aload(
"iaload", {int_array, offset});
THEN("The result is of the form `*(int_array->data + offset)`")
{
const auto query =
make_query(result).as<dereference_exprt>()[0].as<plus_exprt>();
REQUIRE(query[1].get() == offset);
REQUIRE(
query[0].as<member_exprt>()[0].as<dereference_exprt>()[0].get() ==
int_array);
// int_array->data has type *int
REQUIRE(
query[0].as<member_exprt>().get().type() ==
pointer_type(java_int_type()));
}
}
}
}
SCENARIO(
"astore",
"[core][java_bytecode][java_bytecode_convert_method][convert_astore]")
{
symbol_tablet symbol_table;
java_string_library_preprocesst string_preprocess;
const class_hierarchyt class_hierarchy{};
java_bytecode_convert_methodt converter{symbol_table,
null_message_handler,
10,
true,
{},
string_preprocess,
class_hierarchy,
false};
GIVEN("An int array")
{
const source_locationt location;
const typet int_array_type = java_array_type('i');
const symbol_exprt int_array{"int_array", int_array_type};
const exprt offset = from_integer(3, java_int_type());
const exprt value = from_integer(4, java_int_type());
WHEN("iastore is called on the int array")
{
const code_blockt result =
java_bytecode_convert_method_unit_testt::convert_astore(
converter, "iastore", {int_array, offset, value}, location);
THEN(
"The result contains 1 statement of the form `*(int_array->data + 3) = "
"4`")
{
REQUIRE(result.statements().size() == 1);
auto query = make_query(result.statements()[0]).as<code_assignt>();
REQUIRE(query[1].get() == value);
auto plus = query[0].as<dereference_exprt>()[0].as<plus_exprt>();
REQUIRE(plus[1].get() == offset);
REQUIRE(
plus[0].as<member_exprt>().get().get_component_name() == "data");
REQUIRE(
plus[0].as<member_exprt>()[0].as<dereference_exprt>()[0].get() ==
int_array);
THEN("int_array->data has type *int")
{
REQUIRE(
plus[0].as<member_exprt>().get().type() ==
pointer_type(java_int_type()));
}
}
}
}
GIVEN("A boolean array")
{
const source_locationt location;
const typet boolean_array_type = java_array_type('z');
const symbol_exprt boolean_array{"boolean_array", boolean_array_type};
const exprt offset = from_integer(3, java_int_type());
const exprt value = from_integer(true, java_boolean_type());
WHEN("bastore is called on the boolean array")
{
const code_blockt result =
java_bytecode_convert_method_unit_testt::convert_astore(
converter, "bastore", {boolean_array, offset, value}, location);
THEN(
"The result contains 1 statement of the form "
"`*(boolean_array->data + 3) = true`")
{
REQUIRE(result.statements().size() == 1);
auto query = make_query(result.statements()[0]).as<code_assignt>();
REQUIRE(query[1].get() == value);
auto plus = query[0].as<dereference_exprt>()[0].as<plus_exprt>();
REQUIRE(plus[1].get() == offset);
REQUIRE(
plus[0].as<member_exprt>().get().get_component_name() == "data");
REQUIRE(
plus[0].as<member_exprt>()[0].as<dereference_exprt>()[0].get() ==
boolean_array);
THEN("boolean_array->data has type *boolean")
{
REQUIRE(
plus[0].as<member_exprt>().get().type() ==
pointer_type(java_boolean_type()));
}
}
}
WHEN("iastore is called on the boolean array")
{
const code_blockt result =
java_bytecode_convert_method_unit_testt::convert_astore(
converter, "iastore", {boolean_array, offset, value}, location);
THEN(
"The result contains 1 statement of the form "
"`*(((int[])boolean_array)->data + offset)`")
{
REQUIRE(result.statements().size() == 1);
REQUIRE(
make_query(result.statements()[0])
.as<code_assignt>()[0]
.as<dereference_exprt>()[0]
.as<plus_exprt>()[0]
.as<member_exprt>()[0]
.as<dereference_exprt>()[0]
.as<typecast_exprt>()
.get()
.type() == java_array_type('i'));
}
}
}
}
SCENARIO(
"java convert method variable",
"[core][java_bytecode][java_bytecode_convert_method][variable]")
{
symbol_tablet symbol_table;
java_string_library_preprocesst string_preprocess;
const class_hierarchyt class_hierarchy{};
const std::size_t max_array_length = 10;
const bool throw_assertion_error = true;
const bool threading_support = false;
java_bytecode_convert_methodt converter{symbol_table,
null_message_handler,
max_array_length,
throw_assertion_error,
{},
string_preprocess,
class_hierarchy,
threading_support};
GIVEN("An int_array variable")
{
const source_locationt location;
const typet int_array_type = java_array_type('i');
const symbol_exprt int_array{"int_array", int_array_type};
const std::size_t variable_index = 0;
const std::size_t start_pc = 0;
const std::size_t length = 1;
const bool is_parameter = false;
java_bytecode_convert_method_unit_testt::add_variable(
converter, variable_index, int_array, start_pc, length, is_parameter, {});
const std::size_t address = 0;
WHEN("The variable is retrieved via its index with type_char a")
{
const constant_exprt index_expr =
from_integer(variable_index, java_int_type());
const exprt result = java_bytecode_convert_method_unit_testt::variable(
converter, index_expr, 'a', address);
THEN("the result is int_array")
{
REQUIRE(result == int_array);
}
}
WHEN("There is no variable at the given index")
{
const constant_exprt index_expr =
from_integer(variable_index + 1, java_int_type());
const exprt result = java_bytecode_convert_method_unit_testt::variable(
converter, index_expr, 'a', address);
THEN("A new reference variable is created")
{
REQUIRE(result != int_array);
REQUIRE(can_cast_expr<symbol_exprt>(result));
REQUIRE(result.type() == java_type_from_char('a'));
}
}
}
GIVEN("An Object variable")
{
const source_locationt location;
const typet object_type = java_lang_object_type();
const symbol_exprt obj{"obj", object_type};
const std::size_t variable_index = 0;
const std::size_t start_pc = 0;
const std::size_t length = 1;
const bool is_parameter = false;
java_bytecode_convert_method_unit_testt::add_variable(
converter, variable_index, obj, start_pc, length, is_parameter, {});
const std::size_t address = 0;
WHEN("The variable is retrieved via its index with type_char a")
{
const constant_exprt index_expr =
from_integer(variable_index, java_int_type());
const exprt result = java_bytecode_convert_method_unit_testt::variable(
converter, index_expr, 'a', address);
THEN("the result is obj")
{
REQUIRE(result == obj);
}
}
}
GIVEN("An long variable")
{
const source_locationt location;
const typet long_type = java_long_type();
const symbol_exprt long_var{"long_var", long_type};
const std::size_t variable_index = 0;
const std::size_t start_pc = 0;
const std::size_t length = 1;
const bool is_parameter = false;
java_bytecode_convert_method_unit_testt::add_variable(
converter, variable_index, long_var, start_pc, length, is_parameter, {});
const std::size_t address = 0;
WHEN("The variable is retrieved via its index with type_char l")
{
const constant_exprt index_expr =
from_integer(variable_index, java_int_type());
const exprt result = java_bytecode_convert_method_unit_testt::variable(
converter, index_expr, 'l', address);
THEN("the result is long_var")
{
REQUIRE(result == long_var);
}
}
}
}
SCENARIO(
"java convert load",
"[core][java_bytecode][java_bytecode_convert_method][convert_load]")
{
symbol_tablet symbol_table;
java_string_library_preprocesst string_preprocess;
const class_hierarchyt class_hierarchy{};
const std::size_t max_array_length = 10;
const bool throw_assertion_error = true;
const bool threading_support = false;
java_bytecode_convert_methodt converter{symbol_table,
null_message_handler,
max_array_length,
throw_assertion_error,
{},
string_preprocess,
class_hierarchy,
threading_support};
GIVEN("An int_array variable")
{
const source_locationt location;
const typet int_array_type = java_array_type('i');
const symbol_exprt int_array{"int_array", int_array_type};
const std::size_t variable_index = 0;
const std::size_t start_pc = 0;
const std::size_t length = 1;
const bool is_parameter = false;
java_bytecode_convert_method_unit_testt::add_variable(
converter, variable_index, int_array, start_pc, length, is_parameter, {});
const std::size_t address = 0;
WHEN("The variable is loaded with aload")
{
const constant_exprt index_expr =
from_integer(variable_index, java_int_type());
const exprt result =
java_bytecode_convert_method_unit_testt::convert_load(
converter, index_expr, 'a', address);
THEN("the result is int_array")
{
REQUIRE(result == int_array);
}
}
WHEN("There is no variable at the given index")
{
const constant_exprt index_expr =
from_integer(variable_index + 1, java_int_type());
const exprt result = java_bytecode_convert_method_unit_testt::variable(
converter, index_expr, 'a', address);
THEN("A new reference variable is created")
{
REQUIRE(result != int_array);
REQUIRE(can_cast_expr<symbol_exprt>(result));
REQUIRE(result.type() == java_type_from_char('a'));
}
}
}
GIVEN("An Object variable")
{
const source_locationt location;
const typet object_type = java_lang_object_type();
const symbol_exprt obj{"obj", object_type};
const std::size_t variable_index = 0;
const std::size_t start_pc = 0;
const std::size_t length = 1;
const bool is_parameter = false;
java_bytecode_convert_method_unit_testt::add_variable(
converter, variable_index, obj, start_pc, length, is_parameter, {});
const std::size_t address = 0;
WHEN("The variable is loaded with aload")
{
const constant_exprt index_expr =
from_integer(variable_index, java_int_type());
const exprt result =
java_bytecode_convert_method_unit_testt::convert_load(
converter, index_expr, 'a', address);
THEN("the result is obj")
{
REQUIRE(result == obj);
}
}
}
GIVEN("A long variable")
{
const source_locationt location;
const typet long_type = java_long_type();
const symbol_exprt long_var{"long_var", long_type};
const std::size_t variable_index = 0;
const std::size_t start_pc = 0;
const std::size_t length = 1;
const bool is_parameter = false;
java_bytecode_convert_method_unit_testt::add_variable(
converter, variable_index, long_var, start_pc, length, is_parameter, {});
const std::size_t address = 0;
WHEN("The variable is loaded with lload")
{
const constant_exprt index_expr =
from_integer(variable_index, java_int_type());
const exprt result =
java_bytecode_convert_method_unit_testt::convert_load(
converter, index_expr, 'l', address);
THEN("the result is long_var")
{
REQUIRE(result == long_var);
}
}
}
GIVEN("A boolean variable")
{
const source_locationt location;
const symbol_exprt boolean_var{"boolean_var", java_boolean_type()};
const std::size_t variable_index = 0;
const std::size_t start_pc = 0;
const std::size_t length = 1;
const bool is_parameter = false;
java_bytecode_convert_method_unit_testt::add_variable(
converter,
variable_index,
boolean_var,
start_pc,
length,
is_parameter,
{});
const std::size_t address = 0;
WHEN("The variable is loaded with iload")
{
const constant_exprt index_expr =
from_integer(variable_index, java_int_type());
const exprt result =
java_bytecode_convert_method_unit_testt::convert_load(
converter, index_expr, 'i', address);
THEN("the result is (int)boolean_var")
{
REQUIRE(result.type() == java_int_type());
REQUIRE(
make_query(result).as<typecast_exprt>()[0].get() == boolean_var);
}
}
}
}
SCENARIO(
"convert store",
"[core][java_bytecode][java_bytecode_convert_method][convert_store]")
{
symbol_tablet symbol_table;
java_string_library_preprocesst string_preprocess;
const class_hierarchyt class_hierarchy{};
const std::size_t max_array_length = 10;
const bool throw_assertion_error = true;
const bool threading_support = false;
java_bytecode_convert_methodt converter{symbol_table,
null_message_handler,
max_array_length,
throw_assertion_error,
{},
string_preprocess,
class_hierarchy,
threading_support};
GIVEN("An int_array variable")
{
const source_locationt location;
const typet int_array_type = java_array_type('i');
const symbol_exprt int_array{"int_array", int_array_type};
const symbol_exprt int_array_copy{"int_array_copy", int_array_type};
const std::size_t variable_index = 0;
const std::size_t start_pc = 0;
const std::size_t length = 1;
const bool is_parameter = false;
java_bytecode_convert_method_unit_testt::add_variable(
converter,
variable_index,
int_array_copy,
start_pc,
length,
is_parameter,
{});
WHEN("astore is called on the int array")
{
const constant_exprt index_expr =
from_integer(variable_index, java_int_type());
const code_blockt result_code =
java_bytecode_convert_method_unit_testt::convert_store(
converter, "astore", index_expr, {int_array}, 0, location);
THEN(
"The result is one assignment of the form "
"`int_array_copy = int_array`")
{
REQUIRE(result_code.statements().size() == 1);
auto assign_query =
make_query(result_code.statements()[0]).as<code_assignt>();
REQUIRE(can_cast_expr<symbol_exprt>(assign_query[0].get()));
REQUIRE(assign_query[1].get() == int_array);
THEN("Left-hand-side has type int array")
{
REQUIRE(assign_query[0].get().type() == int_array_type);
}
}
}
}
}