forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjava_bytecode_convert_class.cpp
1277 lines (1155 loc) · 45.9 KB
/
java_bytecode_convert_class.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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************\
Module: JAVA Bytecode Language Conversion
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// JAVA Bytecode Language Conversion
#include "java_bytecode_convert_class.h"
#ifdef DEBUG
#include <iostream>
#endif
#include "java_root_class.h"
#include <util/arith_tools.h>
#include <util/expr_initializer.h>
#include <util/namespace.h>
#include <util/prefix.h>
#include <util/std_expr.h>
#include <util/symbol_table_base.h>
#include "ci_lazy_methods.h"
#include "java_bytecode_convert_method.h"
#include "java_string_library_preprocess.h"
#include "java_types.h"
#include "java_utils.h"
class java_bytecode_convert_classt
{
public:
java_bytecode_convert_classt(
symbol_table_baset &_symbol_table,
message_handlert &_message_handler,
size_t _max_array_length,
method_bytecodet &method_bytecode,
java_string_library_preprocesst &_string_preprocess,
const std::unordered_set<std::string> &no_load_classes)
: log(_message_handler),
symbol_table(_symbol_table),
max_array_length(_max_array_length),
method_bytecode(method_bytecode),
string_preprocess(_string_preprocess),
no_load_classes(no_load_classes)
{
}
/// Converts all the class parse trees into a class symbol and adds it to the
/// symbol table.
/// \param parse_trees: The parse trees found for the class to be converted.
/// \remarks
/// Allows multiple definitions of the same class to appear on the
/// classpath, so long as all but the first definition are marked with the
/// attribute `\@java::org.cprover.OverlayClassImplementation`.
/// Overlay class definitions can contain methods with the same signature
/// as methods in the original class, so long as these are marked with the
/// attribute `\@java::org.cprover.OverlayMethodImplementation`; such
/// overlay methods are replaced in the original file with the version
/// found in the last overlay on the classpath. Later definitions can
/// also contain new supporting methods and fields that are merged in.
/// This will allow insertion of Java methods into library classes to
/// handle, for example, modelling dependency injection.
void operator()(
const java_class_loadert::parse_tree_with_overlayst &parse_trees)
{
PRECONDITION(!parse_trees.empty());
const java_bytecode_parse_treet &parse_tree = parse_trees.front();
// Add array types to the symbol table
add_java_array_types(symbol_table);
const bool loading_success =
parse_tree.loading_successful &&
!no_load_classes.count(id2string(parse_tree.parsed_class.name));
if(loading_success)
{
overlay_classest overlay_classes;
for(auto overlay_class_it = std::next(parse_trees.begin());
overlay_class_it != parse_trees.end();
++overlay_class_it)
{
overlay_classes.push_front(std::cref(overlay_class_it->parsed_class));
}
convert(parse_tree.parsed_class, overlay_classes);
}
// Add as string type if relevant
const irep_idt &class_name = parse_tree.parsed_class.name;
if(string_preprocess.is_known_string_type(class_name))
string_preprocess.add_string_type(class_name, symbol_table);
else if(!loading_success)
// Generate stub if couldn't load from bytecode and wasn't string type
generate_class_stub(
class_name,
symbol_table,
log.get_message_handler(),
struct_union_typet::componentst{});
}
typedef java_bytecode_parse_treet::classt classt;
typedef java_bytecode_parse_treet::fieldt fieldt;
typedef java_bytecode_parse_treet::methodt methodt;
typedef java_bytecode_parse_treet::annotationt annotationt;
private:
messaget log;
symbol_table_baset &symbol_table;
const size_t max_array_length;
method_bytecodet &method_bytecode;
java_string_library_preprocesst &string_preprocess;
// conversion
typedef std::list<std::reference_wrapper<const classt>> overlay_classest;
void convert(const classt &c, const overlay_classest &overlay_classes);
void convert(symbolt &class_symbol, const fieldt &f);
/// Check if a method is an overlay method by searching for
/// `ID_overlay_method` in its list of annotations.
///
/// An overlay method is a method with the annotation
/// \@OverlayMethodImplementation. They should only appear in
/// [overlay classes](\ref is_overlay_class). They
/// will be loaded by JBMC instead of the method with the same signature
/// in the underlying class. It is an error if there is no method with the
/// same signature in the underlying class. It is an error if a method in
/// an overlay class has the same signature as a method in the underlying
/// class and it isn't marked as an overlay method or an
/// [ignored method](\ref java_bytecode_convert_classt::is_ignored_method).
///
/// \param method: a `methodt` object from a java bytecode parse tree
/// \return true if the method is an overlay method, else false
static bool is_overlay_method(const methodt &method)
{
return method.has_annotation(ID_overlay_method);
}
/// Check if a method is an ignored method, by one of two mechanisms:
///
/// 1. If the class under consideration is org.cprover.CProver, and the method
/// is listed as ignored.
///
/// 2. If it has the annotation\@IgnoredMethodImplementation.
/// This kind of ignord method is intended for use in
/// [overlay classes](\ref is_overlay_class), in
/// particular for methods which must exist in the overlay class so that
/// it will compile, e.g. default constructors, but which we do not want
/// to overlay the corresponding method in the
/// underlying class. It is an error if a method in
/// an overlay class has the same signature as a method in the underlying
/// class and it isn't marked as an
/// [overlay method](\ref java_bytecode_convert_classt::is_overlay_method)
/// or an ignored method.
///
/// \param class_name: class the method is declared on
/// \param method: a `methodt` object from a java bytecode parse tree
/// \return true if the method is an ignored method, else false
static bool is_ignored_method(
const irep_idt &class_name, const methodt &method)
{
static irep_idt org_cprover_CProver_name = "org.cprover.CProver";
return
(class_name == org_cprover_CProver_name &&
cprover_methods_to_ignore.count(id2string(method.name)) != 0) ||
method.has_annotation(ID_ignored_method);
}
bool check_field_exists(
const fieldt &field,
const irep_idt &qualified_fieldname,
const struct_union_typet::componentst &fields) const;
std::unordered_set<std::string> no_load_classes;
};
/// Auxiliary function to extract the generic superclass reference from the
/// class signature. If the signature is empty or the superclass is not generic
/// it returns empty.
/// Example:
/// - class: A<T> extends B<T, Integer> implements C, D<T>
/// - signature: <T:Ljava/lang/Object;>B<TT;Ljava/lang/Integer;>;LC;LD<TT;>;
/// - returned superclass reference: B<TT;Ljava/lang/Integer;>;
/// \param signature: Signature of the class
/// \return Reference of the generic superclass, or empty if the superclass
/// is not generic
static std::optional<std::string> extract_generic_superclass_reference(
const std::optional<std::string> &signature)
{
if(signature.has_value())
{
// skip the (potential) list of generic parameters at the beginning of the
// signature
const size_t start =
signature.value().front() == '<'
? find_closing_delimiter(signature.value(), 0, '<', '>') + 1
: 0;
// extract the superclass reference
const size_t end =
find_closing_semi_colon_for_reference_type(signature.value(), start);
const std::string superclass_ref =
signature.value().substr(start, (end - start) + 1);
// if the superclass is generic then the reference is of form
// `Lsuperclass-name<generic-types;>;` if it is implicitly generic, then the
// reference is of the form
// `Lsuperclass-name<Tgeneric-types;>.Innerclass-Name`
if(superclass_ref.find('<') != std::string::npos)
return superclass_ref;
}
return {};
}
/// Auxiliary function to extract the generic interface reference of an
/// interface with the specified name from the class signature. If the
/// signature is empty or the interface is not generic it returns empty.
/// Example:
/// - class: A<T> extends B<T, Integer> implements C, D<T>
/// - signature: <T:Ljava/lang/Object;>B<TT;Ljava/lang/Integer;>;LC;LD<TT;>;
/// - returned interface reference for C: LC;
/// - returned interface reference for D: LD<TT;>;
/// \param signature: Signature of the class
/// \param interface_name: The interface name
/// \return Reference of the generic interface, or empty if the interface
/// is not generic
static std::optional<std::string> extract_generic_interface_reference(
const std::optional<std::string> &signature,
const std::string &interface_name)
{
if(signature.has_value())
{
// skip the (potential) list of generic parameters at the beginning of the
// signature
size_t start =
signature.value().front() == '<'
? find_closing_delimiter(signature.value(), 0, '<', '>') + 1
: 0;
// skip the superclass reference (if there is at least one interface
// reference in the signature, then there is a superclass reference)
start =
find_closing_semi_colon_for_reference_type(signature.value(), start) + 1;
// if the interface name includes package name, convert dots to slashes
std::string interface_name_slash_to_dot = interface_name;
std::replace(
interface_name_slash_to_dot.begin(),
interface_name_slash_to_dot.end(),
'.',
'/');
start =
signature.value().find("L" + interface_name_slash_to_dot + "<", start);
if(start != std::string::npos)
{
const size_t &end =
find_closing_semi_colon_for_reference_type(signature.value(), start);
return signature.value().substr(start, (end - start) + 1);
}
}
return {};
}
/// Convert a class, adding symbols to the symbol table for its members
/// \param c: Bytecode of the class to convert
/// \param overlay_classes: Bytecode of any overlays for the class to convert
void java_bytecode_convert_classt::convert(
const classt &c,
const overlay_classest &overlay_classes)
{
std::string qualified_classname="java::"+id2string(c.name);
if(symbol_table.has_symbol(qualified_classname))
{
log.debug() << "Skip class " << c.name << " (already loaded)"
<< messaget::eom;
return;
}
java_class_typet class_type;
if(c.signature.has_value() && c.signature.value()[0]=='<')
{
java_generic_class_typet generic_class_type;
#ifdef DEBUG
std::cout << "INFO: found generic class signature "
<< c.signature.value()
<< " in parsed class "
<< c.name << "\n";
#endif
try
{
const std::vector<typet> &generic_types=java_generic_type_from_string(
id2string(c.name),
c.signature.value());
for(const typet &t : generic_types)
{
generic_class_type.generic_types()
.push_back(to_java_generic_parameter(t));
}
class_type=generic_class_type;
}
catch(const unsupported_java_class_signature_exceptiont &e)
{
log.debug() << "Class: " << c.name
<< "\n could not parse signature: " << c.signature.value()
<< "\n " << e.what()
<< "\n ignoring that the class is generic" << messaget::eom;
}
}
class_type.set_tag(c.name);
class_type.set_inner_name(c.inner_name);
class_type.set_abstract(c.is_abstract);
class_type.set_is_annotation(c.is_annotation);
class_type.set_interface(c.is_interface);
class_type.set_synthetic(c.is_synthetic);
class_type.set_final(c.is_final);
class_type.set_is_inner_class(c.is_inner_class);
class_type.set_is_static_class(c.is_static_class);
class_type.set_is_anonymous_class(c.is_anonymous_class);
class_type.set_outer_class(c.outer_class);
class_type.set_super_class(c.super_class);
if(c.is_enum)
{
if(max_array_length != 0 && c.enum_elements > max_array_length)
{
log.warning() << "Java Enum " << c.name
<< " won't work properly because max "
<< "array length (" << max_array_length
<< ") is less than the "
<< "enum size (" << c.enum_elements << ")" << messaget::eom;
}
class_type.set(
ID_java_enum_static_unwind,
std::to_string(c.enum_elements+1));
class_type.set_is_enumeration(true);
}
if(c.is_public)
class_type.set_access(ID_public);
else if(c.is_protected)
class_type.set_access(ID_protected);
else if(c.is_private)
class_type.set_access(ID_private);
else
class_type.set_access(ID_default);
if(!c.super_class.empty())
{
const struct_tag_typet base("java::" + id2string(c.super_class));
// if the superclass is generic then the class has the superclass reference
// including the generic info in its signature
// e.g., signature for class 'A<T>' that extends
// 'Generic<Integer>' is '<T:Ljava/lang/Object;>LGeneric<LInteger;>;'
const std::optional<std::string> &superclass_ref =
extract_generic_superclass_reference(c.signature);
if(superclass_ref.has_value())
{
try
{
const java_generic_struct_tag_typet generic_base(
base, superclass_ref.value(), qualified_classname);
class_type.add_base(generic_base);
}
catch(const unsupported_java_class_signature_exceptiont &e)
{
log.debug() << "Superclass: " << c.super_class
<< " of class: " << c.name
<< "\n could not parse signature: "
<< superclass_ref.value() << "\n " << e.what()
<< "\n ignoring that the superclass is generic"
<< messaget::eom;
class_type.add_base(base);
}
}
else
{
class_type.add_base(base);
}
java_class_typet::componentt base_class_field;
base_class_field.type() = class_type.bases().at(0).type();
base_class_field.set_name("@" + id2string(c.super_class));
base_class_field.set_base_name("@" + id2string(c.super_class));
base_class_field.set_pretty_name("@" + id2string(c.super_class));
class_type.components().push_back(base_class_field);
}
// interfaces are recorded as bases
for(const auto &interface : c.implements)
{
const struct_tag_typet base("java::" + id2string(interface));
// if the interface is generic then the class has the interface reference
// including the generic info in its signature
// e.g., signature for class 'A implements GenericInterface<Integer>' is
// 'Ljava/lang/Object;LGenericInterface<LInteger;>;'
const std::optional<std::string> interface_ref =
extract_generic_interface_reference(c.signature, id2string(interface));
if(interface_ref.has_value())
{
try
{
const java_generic_struct_tag_typet generic_base(
base, interface_ref.value(), qualified_classname);
class_type.add_base(generic_base);
}
catch(const unsupported_java_class_signature_exceptiont &e)
{
log.debug() << "Interface: " << interface << " of class: " << c.name
<< "\n could not parse signature: " << interface_ref.value()
<< "\n " << e.what()
<< "\n ignoring that the interface is generic"
<< messaget::eom;
class_type.add_base(base);
}
}
else
{
class_type.add_base(base);
}
}
// now do lambda method handles (bootstrap methods)
for(const auto &lambda_entry : c.lambda_method_handle_map)
{
// if the handle is of unknown type, we still need to store it to preserve
// the correct indexing (invokedynamic instructions will retrieve
// method handles by index)
lambda_entry.second.is_unknown_handle()
? class_type.add_unknown_lambda_method_handle()
: class_type.add_lambda_method_handle(
lambda_entry.second.get_method_descriptor(),
lambda_entry.second.handle_type);
}
// Load annotations
if(!c.annotations.empty())
convert_annotations(c.annotations, class_type.get_annotations());
// the base name is the name of the class without the package
const irep_idt base_name = [](const std::string &full_name) {
const size_t last_dot = full_name.find_last_of('.');
return last_dot == std::string::npos
? full_name
: std::string(full_name, last_dot + 1, std::string::npos);
}(id2string(c.name));
// produce class symbol
class_type.set_name(qualified_classname);
type_symbolt new_symbol{qualified_classname, class_type, ID_java};
new_symbol.base_name = base_name;
new_symbol.pretty_name=c.name;
symbolt *class_symbol;
// add before we do members
log.debug() << "Adding symbol: class '" << c.name << "'" << messaget::eom;
if(symbol_table.move(new_symbol, class_symbol))
{
log.error() << "failed to add class symbol " << new_symbol.name
<< messaget::eom;
throw 0;
}
// Now do fields
const class_typet::componentst &fields =
to_class_type(class_symbol->type).components();
// Include fields from overlay classes as they will be required by the
// methods defined there
for(auto overlay_class : overlay_classes)
{
for(const auto &field : overlay_class.get().fields)
{
std::string field_id = qualified_classname + "." + id2string(field.name);
if(check_field_exists(field, field_id, fields))
{
std::string err =
"Duplicate field definition for " + field_id + " in overlay class";
// TODO: This could just be a warning if the types match
log.error() << err << messaget::eom;
throw err.c_str();
}
log.debug() << "Adding symbol from overlay class: field '" << field.name
<< "'" << messaget::eom;
convert(*class_symbol, field);
POSTCONDITION(check_field_exists(field, field_id, fields));
}
}
for(const auto &field : c.fields)
{
std::string field_id = qualified_classname + "." + id2string(field.name);
if(check_field_exists(field, field_id, fields))
{
// TODO: This could be a warning if the types match
log.error() << "Field definition for " << field_id
<< " already loaded from overlay class" << messaget::eom;
continue;
}
log.debug() << "Adding symbol: field '" << field.name << "'"
<< messaget::eom;
convert(*class_symbol, field);
POSTCONDITION(check_field_exists(field, field_id, fields));
}
// Now do methods
std::set<irep_idt> overlay_methods;
for(auto overlay_class : overlay_classes)
{
for(const methodt &method : overlay_class.get().methods)
{
const irep_idt method_identifier =
qualified_classname + "." + id2string(method.name)
+ ":" + method.descriptor;
if(is_ignored_method(c.name, method))
{
log.debug() << "Ignoring method: '" << method_identifier << "'"
<< messaget::eom;
continue;
}
if(method_bytecode.contains_method(method_identifier))
{
// This method has already been discovered and added to method_bytecode
// while parsing an overlay class that appears later in the classpath
// (we're working backwards)
// Warn the user if the definition already found was not an overlay,
// otherwise simply don't load this earlier definition
if(overlay_methods.count(method_identifier) == 0)
{
// This method was defined in a previous class definition without
// being marked as an overlay method
log.warning()
<< "Method " << method_identifier
<< " exists in an overlay class without being marked as an "
"overlay and also exists in another overlay class that appears "
"earlier in the classpath"
<< messaget::eom;
}
continue;
}
// Always run the lazy pre-stage, as it symbol-table
// registers the function.
log.debug() << "Adding symbol from overlay class: method '"
<< method_identifier << "'" << messaget::eom;
java_bytecode_convert_method_lazy(
*class_symbol,
method_identifier,
method,
symbol_table,
log.get_message_handler());
method_bytecode.add(qualified_classname, method_identifier, method);
if(is_overlay_method(method))
overlay_methods.insert(method_identifier);
}
}
for(const methodt &method : c.methods)
{
const irep_idt method_identifier=
qualified_classname + "." + id2string(method.name)
+ ":" + method.descriptor;
if(is_ignored_method(c.name, method))
{
log.debug() << "Ignoring method: '" << method_identifier << "'"
<< messaget::eom;
continue;
}
if(method_bytecode.contains_method(method_identifier))
{
// This method has already been discovered while parsing an overlay
// class
// If that definition is an overlay then we simply don't load this
// original definition and we remove it from the list of overlays
if(overlay_methods.erase(method_identifier) == 0)
{
// This method was defined in a previous class definition without
// being marked as an overlay method
log.warning()
<< "Method " << method_identifier
<< " exists in an overlay class without being marked as an overlay "
"and also exists in the underlying class"
<< messaget::eom;
}
continue;
}
// Always run the lazy pre-stage, as it symbol-table
// registers the function.
log.debug() << "Adding symbol: method '" << method_identifier << "'"
<< messaget::eom;
java_bytecode_convert_method_lazy(
*class_symbol,
method_identifier,
method,
symbol_table,
log.get_message_handler());
method_bytecode.add(qualified_classname, method_identifier, method);
if(is_overlay_method(method))
{
log.warning()
<< "Method " << method_identifier
<< " marked as an overlay where defined in the underlying class"
<< messaget::eom;
}
}
if(!overlay_methods.empty())
{
log.error()
<< "Overlay methods defined in overlay classes did not exist in the "
"underlying class:\n";
for(const irep_idt &method_id : overlay_methods)
log.error() << " " << method_id << "\n";
log.error() << messaget::eom;
}
// is this a root class?
if(c.super_class.empty())
java_root_class(*class_symbol);
}
bool java_bytecode_convert_classt::check_field_exists(
const java_bytecode_parse_treet::fieldt &field,
const irep_idt &qualified_fieldname,
const struct_union_typet::componentst &fields) const
{
if(field.is_static)
return symbol_table.has_symbol(qualified_fieldname);
auto existing_field = std::find_if(
fields.begin(),
fields.end(),
[&field](const struct_union_typet::componentt &f)
{
return f.get_name() == field.name;
});
return existing_field != fields.end();
}
/// Convert a field, adding a symbol to teh symbol table for it
/// \param class_symbol: The already added symbol for the containing class
/// \param f: The bytecode for the field to convert
void java_bytecode_convert_classt::convert(
symbolt &class_symbol,
const fieldt &f)
{
typet field_type;
if(f.signature.has_value())
{
field_type = *java_type_from_string_with_exception(
f.descriptor, f.signature, id2string(class_symbol.name));
/// this is for a free type variable, e.g., a field of the form `T f;`
if(is_java_generic_parameter(field_type))
{
#ifdef DEBUG
std::cout << "fieldtype: generic "
<< to_java_generic_parameter(field_type).type_variable()
.get_identifier()
<< " name " << f.name << "\n";
#endif
}
/// this is for a field that holds a generic type, either with instantiated
/// or with free type variables, e.g., `List<T> l;` or `List<Integer> l;`
else if(is_java_generic_type(field_type))
{
java_generic_typet &with_gen_type=
to_java_generic_type(field_type);
#ifdef DEBUG
std::cout << "fieldtype: generic container type "
<< std::to_string(with_gen_type.generic_type_arguments().size())
<< " type " << with_gen_type.id()
<< " name " << f.name
<< " subtype id " << with_gen_type.subtype().id() << "\n";
#endif
field_type=with_gen_type;
}
}
else
field_type = *java_type_from_string(f.descriptor);
// determine access
irep_idt access;
if(f.is_private)
access = ID_private;
else if(f.is_protected)
access = ID_protected;
else if(f.is_public)
access = ID_public;
else
access = ID_default;
auto &class_type = to_java_class_type(class_symbol.type);
// is this a static field?
if(f.is_static)
{
const irep_idt field_identifier =
id2string(class_symbol.name) + "." + id2string(f.name);
class_type.static_members().emplace_back();
auto &component = class_type.static_members().back();
component.set_name(field_identifier);
component.set_base_name(f.name);
component.set_pretty_name(f.name);
component.set_access(access);
component.set_is_final(f.is_final);
component.type() = field_type;
// Create the symbol
symbolt new_symbol{
id2string(class_symbol.name) + "." + id2string(f.name),
field_type,
ID_java};
new_symbol.is_static_lifetime=true;
new_symbol.is_lvalue=true;
new_symbol.is_state_var=true;
new_symbol.base_name=f.name;
// Provide a static field -> class link, like
// java_bytecode_convert_method::convert does for method -> class.
set_declaring_class(new_symbol, class_symbol.name);
new_symbol.type.set(ID_C_field, f.name);
new_symbol.type.set(ID_C_constant, f.is_final);
new_symbol.pretty_name=id2string(class_symbol.pretty_name)+
"."+id2string(f.name);
// These annotations use `ID_C_access` instead of `ID_access` like methods
// to avoid type clashes in expressions like `some_static_field = 0`, where
// with ID_access the constant '0' would need to have an access modifier
// too, or else appear to have incompatible type.
if(f.is_public)
new_symbol.type.set(ID_C_access, ID_public);
else if(f.is_protected)
new_symbol.type.set(ID_C_access, ID_protected);
else if(f.is_private)
new_symbol.type.set(ID_C_access, ID_private);
else
new_symbol.type.set(ID_C_access, ID_default);
const namespacet ns(symbol_table);
const auto value = zero_initializer(field_type, class_symbol.location, ns);
if(!value.has_value())
{
log.error().source_location = class_symbol.location;
log.error() << "failed to zero-initialize " << f.name << messaget::eom;
throw 0;
}
new_symbol.value = *value;
// Load annotations
if(!f.annotations.empty())
{
convert_annotations(
f.annotations,
type_checked_cast<annotated_typet>(new_symbol.type).get_annotations());
}
// Do we have the static field symbol already?
const auto s_it=symbol_table.symbols.find(new_symbol.name);
if(s_it!=symbol_table.symbols.end())
symbol_table.erase(s_it); // erase, we stubbed it
const bool failed = symbol_table.add(new_symbol);
CHECK_RETURN_WITH_DIAGNOSTICS(!failed, "failed to add static field symbol");
}
else
{
class_type.components().emplace_back();
auto &component = class_type.components().back();
component.set_name(f.name);
component.set_base_name(f.name);
component.set_pretty_name(f.name);
component.set_access(access);
component.set_is_final(f.is_final);
component.type() = field_type;
// Load annotations
if(!f.annotations.empty())
{
convert_annotations(
f.annotations,
static_cast<annotated_typet &>(component.type()).get_annotations());
}
}
}
void add_java_array_types(symbol_table_baset &symbol_table)
{
const std::string letters="ijsbcfdza";
for(const char l : letters)
{
struct_tag_typet struct_tag_type =
to_struct_tag_type(java_array_type(l).subtype());
const irep_idt &struct_tag_type_identifier =
struct_tag_type.get_identifier();
if(symbol_table.has_symbol(struct_tag_type_identifier))
return;
java_class_typet class_type;
// we have the base class, java.lang.Object, length and data
// of appropriate type
class_type.set_tag(struct_tag_type_identifier);
// Note that non-array types don't have "java::" at the beginning of their
// tag, and their name is "java::" + their tag. Since arrays do have
// "java::" at the beginning of their tag we set the name to be the same as
// the tag.
class_type.set_name(struct_tag_type_identifier);
class_type.components().reserve(3);
java_class_typet::componentt base_class_component(
"@java.lang.Object", struct_tag_typet("java::java.lang.Object"));
base_class_component.set_pretty_name("@java.lang.Object");
base_class_component.set_base_name("@java.lang.Object");
class_type.components().push_back(base_class_component);
java_class_typet::componentt length_component("length", java_int_type());
length_component.set_pretty_name("length");
length_component.set_base_name("length");
class_type.components().push_back(length_component);
java_class_typet::componentt data_component(
"data", java_reference_type(java_type_from_char(l)));
data_component.set_pretty_name("data");
data_component.set_base_name("data");
class_type.components().push_back(data_component);
if(l == 'a')
{
// This is a reference array (java::array[reference]). Add extra fields to
// carry the innermost element type and array dimension.
java_class_typet::componentt array_element_classid_component(
JAVA_ARRAY_ELEMENT_CLASSID_FIELD_NAME, string_typet());
array_element_classid_component.set_pretty_name(
JAVA_ARRAY_ELEMENT_CLASSID_FIELD_NAME);
array_element_classid_component.set_base_name(
JAVA_ARRAY_ELEMENT_CLASSID_FIELD_NAME);
class_type.components().push_back(array_element_classid_component);
java_class_typet::componentt array_dimension_component(
JAVA_ARRAY_DIMENSION_FIELD_NAME, java_int_type());
array_dimension_component.set_pretty_name(
JAVA_ARRAY_DIMENSION_FIELD_NAME);
array_dimension_component.set_base_name(JAVA_ARRAY_DIMENSION_FIELD_NAME);
class_type.components().push_back(array_dimension_component);
}
class_type.add_base(struct_tag_typet("java::java.lang.Object"));
INVARIANT(
is_valid_java_array(class_type),
"Constructed a new type representing a Java Array "
"object that doesn't match expectations");
type_symbolt symbol{struct_tag_type_identifier, class_type, ID_java};
symbol.base_name = struct_tag_type.get(ID_C_base_name);
symbol_table.add(symbol);
// Also provide a clone method:
// ----------------------------
const irep_idt clone_name =
id2string(struct_tag_type_identifier) + ".clone:()Ljava/lang/Object;";
java_method_typet::parametert this_param(
java_reference_type(struct_tag_type));
this_param.set_identifier(id2string(clone_name)+"::this");
this_param.set_base_name(ID_this);
this_param.set_this();
const java_method_typet clone_type({this_param}, java_lang_object_type());
parameter_symbolt this_symbol;
this_symbol.name=this_param.get_identifier();
this_symbol.base_name=this_param.get_base_name();
this_symbol.pretty_name=this_symbol.base_name;
this_symbol.type=this_param.type();
this_symbol.mode=ID_java;
symbol_table.add(this_symbol);
const irep_idt local_name=
id2string(clone_name)+"::cloned_array";
auxiliary_symbolt local_symbol;
local_symbol.name=local_name;
local_symbol.base_name="cloned_array";
local_symbol.pretty_name=local_symbol.base_name;
local_symbol.type = java_reference_type(struct_tag_type);
local_symbol.mode=ID_java;
symbol_table.add(local_symbol);
const auto local_symexpr = local_symbol.symbol_expr();
code_declt declare_cloned(local_symexpr);
source_locationt location;
location.set_function(local_name);
side_effect_exprt java_new_array(
ID_java_new_array, java_reference_type(struct_tag_type), location);
dereference_exprt old_array{this_symbol.symbol_expr()};
dereference_exprt new_array{local_symexpr};
member_exprt old_length(
old_array, length_component.get_name(), length_component.type());
java_new_array.copy_to_operands(old_length);
code_frontend_assignt create_blank(local_symexpr, java_new_array);
codet copy_type_information = code_skipt();
if(l == 'a')
{
// Reference arrays carry additional type information in their classids
// which should be copied:
const auto &array_dimension_component =
class_type.get_component(JAVA_ARRAY_DIMENSION_FIELD_NAME);
const auto &array_element_classid_component =
class_type.get_component(JAVA_ARRAY_ELEMENT_CLASSID_FIELD_NAME);
member_exprt old_array_dimension(old_array, array_dimension_component);
member_exprt old_array_element_classid(
old_array, array_element_classid_component);
member_exprt new_array_dimension(new_array, array_dimension_component);
member_exprt new_array_element_classid(
new_array, array_element_classid_component);
copy_type_information = code_blockt{
{code_frontend_assignt(new_array_dimension, old_array_dimension),
code_frontend_assignt(
new_array_element_classid, old_array_element_classid)}};
}
member_exprt old_data(
old_array, data_component.get_name(), data_component.type());
member_exprt new_data(
new_array, data_component.get_name(), data_component.type());
/*
// TODO use this instead of a loop.
codet array_copy;
array_copy.set_statement(ID_array_copy);
array_copy.add_to_operands(std::move(new_data), std::move(old_data));
clone_body.add_to_operands(std::move(array_copy));
*/
// Begin for-loop to replace:
const irep_idt index_name=
id2string(clone_name)+"::index";
auxiliary_symbolt index_symbol;
index_symbol.name=index_name;
index_symbol.base_name="index";
index_symbol.pretty_name=index_symbol.base_name;
index_symbol.type = length_component.type();
index_symbol.mode=ID_java;
symbol_table.add(index_symbol);
const auto &index_symexpr=index_symbol.symbol_expr();
code_declt declare_index(index_symexpr);
dereference_exprt old_cell(
plus_exprt(old_data, index_symexpr),
to_type_with_subtype(old_data.type()).subtype());
dereference_exprt new_cell(
plus_exprt(new_data, index_symexpr),
to_type_with_subtype(new_data.type()).subtype());
const code_fort copy_loop = code_fort::from_index_bounds(
from_integer(0, index_symexpr.type()),
old_length,
index_symexpr,
code_frontend_assignt(std::move(new_cell), std::move(old_cell)),
location);
member_exprt new_base_class(
new_array, base_class_component.get_name(), base_class_component.type());
address_of_exprt retval(new_base_class);
code_returnt return_inst(retval);
const code_blockt clone_body({declare_cloned,
create_blank,
copy_type_information,
declare_index,
copy_loop,
return_inst});
symbolt clone_symbol{clone_name, clone_type, ID_java};
clone_symbol.pretty_name =
id2string(struct_tag_type_identifier) + ".clone:()";
clone_symbol.base_name="clone";
clone_symbol.value=clone_body;
symbol_table.add(clone_symbol);
}
}
bool java_bytecode_convert_class(
const java_class_loadert::parse_tree_with_overlayst &parse_trees,
symbol_table_baset &symbol_table,
message_handlert &message_handler,
size_t max_array_length,
method_bytecodet &method_bytecode,