36 bool parse()
override;
70 error() <<
"invalid constant pool index (" << index <<
")" <<
eom;
105 void rbytecode(std::vector<instructiont> &);
117 for(std::size_t
i=0;
i<bytes;
i++)
121 error() <<
"unexpected end of bytecode file" <<
eom;
128 template <
typename T>
132 std::is_unsigned<T>::value,
"T should be an unsigned integer");
133 const constexpr size_t bytes =
sizeof(T);
135 for(
size_t i = 0;
i < bytes;
i++)
139 error() <<
"unexpected end of bytecode file" <<
eom;
151#define CONSTANT_Class 7
152#define CONSTANT_Fieldref 9
153#define CONSTANT_Methodref 10
154#define CONSTANT_InterfaceMethodref 11
155#define CONSTANT_String 8
156#define CONSTANT_Integer 3
157#define CONSTANT_Float 4
158#define CONSTANT_Long 5
159#define CONSTANT_Double 6
160#define CONSTANT_NameAndType 12
161#define CONSTANT_Utf8 1
162#define CONSTANT_MethodHandle 15
163#define CONSTANT_MethodType 16
164#define CONSTANT_InvokeDynamic 18
166#define VTYPE_INFO_TOP 0
167#define VTYPE_INFO_INTEGER 1
168#define VTYPE_INFO_FLOAT 2
169#define VTYPE_INFO_LONG 3
170#define VTYPE_INFO_DOUBLE 4
171#define VTYPE_INFO_ITEM_NULL 5
172#define VTYPE_INFO_UNINIT_THIS 6
173#define VTYPE_INFO_OBJECT 7
174#define VTYPE_INFO_UNINIT 8
283 "name_and_typeindex did not correspond to a name_and_type in the "
386 catch(
const char *message)
392 catch(
const std::string &message)
407#define ACC_PUBLIC 0x0001u
408#define ACC_PRIVATE 0x0002u
409#define ACC_PROTECTED 0x0004u
410#define ACC_STATIC 0x0008u
411#define ACC_FINAL 0x0010u
412#define ACC_SYNCHRONIZED 0x0020u
413#define ACC_BRIDGE 0x0040u
414#define ACC_NATIVE 0x0100u
415#define ACC_INTERFACE 0x0200u
416#define ACC_ABSTRACT 0x0400u
417#define ACC_STRICT 0x0800u
418#define ACC_SYNTHETIC 0x1000u
419#define ACC_ANNOTATION 0x2000u
420#define ACC_ENUM 0x4000u
422#define UNUSED_u2(x) \
424 const u2 x = read<u2>(); \
437 if(magic!=0xCAFEBABE)
445 error() <<
"unexpected major version" <<
eom;
518 if(
field.signature.has_value())
540 for(
const auto ¶meter_annotations : method.parameter_annotations)
543 if(method.signature.has_value())
558 for(
const auto &var : method.local_variable_table)
560 if(var.signature.has_value())
585 for(
const auto &p :
ct.parameters())
609 const std::vector<annotationt> &annotations)
645 error() <<
"invalid constant_pool_count" <<
eom;
687 error() <<
"invalid double entry" <<
eom;
711 error() <<
"unknown constant pool entry (" << it->tag <<
")"
721 [&](constant_poolt::value_type &entry) {
726 const std::string &s = id2string(pool_entry(entry.ref1).s);
727 entry.expr = type_exprt(java_classname(s));
731 case CONSTANT_Fieldref:
733 const pool_entryt &nameandtype_entry = pool_entry(entry.ref2);
734 const pool_entryt &name_entry=pool_entry(nameandtype_entry.ref1);
735 const pool_entryt &class_entry = pool_entry(entry.ref1);
736 const pool_entryt &class_name_entry=pool_entry(class_entry.ref1);
737 typet type=type_entry(nameandtype_entry.ref2);
739 auto class_tag = java_classname(id2string(class_name_entry.s));
741 fieldref_exprt fieldref(type, name_entry.s, class_tag.get_identifier());
743 entry.expr = fieldref;
747 case CONSTANT_Methodref:
748 case CONSTANT_InterfaceMethodref:
750 const pool_entryt &nameandtype_entry = pool_entry(entry.ref2);
751 const pool_entryt &name_entry=pool_entry(nameandtype_entry.ref1);
752 const pool_entryt &class_entry = pool_entry(entry.ref1);
753 const pool_entryt &class_name_entry=pool_entry(class_entry.ref1);
754 typet type=type_entry(nameandtype_entry.ref2);
756 auto class_tag = java_classname(id2string(class_name_entry.s));
758 irep_idt mangled_method_name =
759 id2string(name_entry.s) +
":" +
760 id2string(pool_entry(nameandtype_entry.ref2).s);
762 irep_idt class_id = class_tag.get_identifier();
764 entry.expr = class_method_descriptor_exprt{
765 type, mangled_method_name, class_id, name_entry.s};
769 case CONSTANT_String:
772 entry.expr = java_string_literal_exprt{pool_entry(entry.ref1).s};
776 case CONSTANT_Integer:
777 entry.expr = from_integer(entry.number, java_int_type());
782 ieee_floatt value(ieee_float_spect::single_precision());
783 value.unpack(entry.number);
784 entry.expr = value.to_expr();
789 entry.expr = from_integer(entry.number, java_long_type());
792 case CONSTANT_Double:
794 ieee_floatt value(ieee_float_spect::double_precision());
795 value.unpack(entry.number);
796 entry.expr = value.to_expr();
800 case CONSTANT_NameAndType:
802 entry.expr.id(
"nameandtype");
806 case CONSTANT_MethodHandle:
808 entry.expr.id(
"methodhandle");
812 case CONSTANT_MethodType:
814 entry.expr.id(
"methodtype");
818 case CONSTANT_InvokeDynamic:
820 entry.expr.id(
"invokedynamic");
821 const pool_entryt &nameandtype_entry = pool_entry(entry.ref2);
822 typet type=type_entry(nameandtype_entry.ref2);
823 type.set(ID_java_lambda_method_handle_index, entry.ref1);
824 entry.expr.type() = type;
862 const auto flags = (
field.is_public ? 1 : 0) +
863 (
field.is_protected?1:0)+
864 (
field.is_private?1:0);
865 DATA_INVARIANT(flags<=1,
"at most one of public, protected, private");
906 std::string(
"Unexpected wide instruction: ") +
910 instructions.emplace_back();
953 instruction.
args.push_back(
964 instruction.
args.push_back(
1025 while(((address + 1u) & 3u) != 0)
1035 instruction.
args.push_back(
1047 instruction.
args.push_back(
1051 instruction.
args.push_back(
1063 while(((address + 1u) & 3u) != 0)
1071 instruction.
args.push_back(
1090 instruction.
args.push_back(
1102 instruction.
args.push_back(
1137 throw "unknown JVM bytecode instruction";
1144 error() <<
"bytecode length mismatch" <<
eom;
1182 "The start_pc must be less than the end_pc as this is the range the "
1183 "exception is active");
1211 if(!instruction.source_location.get_line().empty())
1212 line_number = instruction.source_location.get_line();
1213 else if(!line_number.
empty())
1214 instruction.source_location.set_line(line_number);
1215 instruction.source_location.set_function(
1220 const auto it = std::find_if(
1224 return !instruction.source_location.get_line().empty();
1292 std::map<unsigned, std::reference_wrapper<instructiont>>
instruction_map;
1307 it->second.get().source_location.set_line(line_number);
1354 methodt::stack_map_table_entryt::SAME_LOCALS_ONE_STACK;
1364 methodt::stack_map_table_entryt::SAME_LOCALS_ONE_STACK_EXTENDED;
1384 =methodt::stack_map_table_entryt::SAME_EXTENDED;
1434 throw "error: unknown stack frame type encountered";
1448 v.
type=methodt::verification_type_infot::TOP;
1451 v.
type=methodt::verification_type_infot::INTEGER;
1454 v.
type=methodt::verification_type_infot::FLOAT;
1457 v.
type=methodt::verification_type_infot::LONG;
1460 v.
type=methodt::verification_type_infot::DOUBLE;
1463 v.
type=methodt::verification_type_infot::ITEM_NULL;
1466 v.
type=methodt::verification_type_infot::UNINITIALIZED_THIS;
1469 v.
type=methodt::verification_type_infot::OBJECT;
1473 v.
type=methodt::verification_type_infot::UNINITIALIZED;
1477 throw "error: unknown verification type info encountered";
1482 std::vector<annotationt> &annotations)
1592 std::string name = parsed_class.
name.
c_str();
1597 "The number of bytes to be read for the InnerClasses attribute does not "
1598 "match the attribute length.");
1604 str.erase(std::remove(str.begin(), str.end(),
ch), str.end());
1706 for(
auto &method : parsed_class.
methods)
1709 for(
auto &instruction : method.instructions)
1711 if(!instruction.source_location.get_line().empty())
1735 "only one BootstrapMethods argument is allowed in a class file");
1757#define ACC_PUBLIC 0x0001u
1758#define ACC_PRIVATE 0x0002u
1759#define ACC_PROTECTED 0x0004u
1760#define ACC_STATIC 0x0008u
1761#define ACC_FINAL 0x0010u
1762#define ACC_VARARGS 0x0080u
1763#define ACC_SUPER 0x0020u
1764#define ACC_VOLATILE 0x0040u
1765#define ACC_TRANSIENT 0x0080u
1766#define ACC_INTERFACE 0x0200u
1767#define ACC_ABSTRACT 0x0400u
1768#define ACC_SYNTHETIC 0x1000u
1769#define ACC_ANNOTATION 0x2000u
1770#define ACC_ENUM 0x4000u
1795 const auto flags = (method.
is_public ? 1 : 0) +
1798 DATA_INVARIANT(flags<=1,
"at most one of public, protected, private");
1806 std::istream &istream,
1809 bool skip_instructions)
1828 const std::string &
file,
1831 bool skip_instructions)
1833 std::ifstream in(
file, std::ios::binary);
1841 in, class_name, message_handler, skip_instructions);
1853 "Local variable type table cannot have more elements "
1854 "than the local variable table.");
1867 if(
lvar.index==index &&
1869 lvar.start_pc==start_pc &&
1870 lvar.length==length)
1879 "Entry in LocalVariableTypeTable must be present in LVT");
2025 <<
"format of BootstrapMethods entry not recognized: too few arguments"
2047 debug() <<
"format of BootstrapMethods entry not recognized: extra "
2048 "arguments of wrong type"
2064 debug() <<
"format of BootstrapMethods entry not recognized: arguments "
2071 debug() <<
"INFO: parse lambda handle" <<
eom;
2077 debug() <<
"format of BootstrapMethods entry not recognized: method "
2078 "handle not recognised"
2088 debug() <<
"lambda function reference "
2090 .base_method_name())
2092 <<
"\n interface type is "
2094 <<
"\n method type is "
struct bytecode_infot const bytecode_info[]
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Array constructor from list of elements.
class_infot get_class(const pool_entry_lookupt &pool_entry) const
name_and_type_infot get_name_and_type(const pool_entry_lookupt &pool_entry) const
u2 get_class_index() const
u2 get_name_and_type_index() const
base_ref_infot(const pool_entryt &entry)
Corresponds to the CONSTANT_Class_info Structure Described in Java 8 specification 4....
class_infot(const pool_entryt &entry)
std::string get_name(const pool_entry_lookupt &pool_entry) const
An expression describing a method on a class.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
const char * c_str() const
Base class for all expressions.
std::vector< exprt > operandst
typet & type()
Return the type of the expression.
const irep_idt & get(const irep_idt &name) const
const irep_idt & id() const
pool_entryt & pool_entry(u2 index)
void get_annotation_class_refs(const std::vector< annotationt > &annotations)
For each of the given annotations, get a reference to its class and recursively get class references ...
java_bytecode_parsert(bool skip_instructions)
void store_unknown_method_handle(size_t bootstrap_method_index)
Creates an unknown method handle and puts it into the parsed_class.
exprt get_relement_value()
Corresponds to the element_value structure Described in Java 8 specification 4.7.16....
java_bytecode_parse_treet::annotationt annotationt
java_bytecode_parse_treet::methodt methodt
void rRuntimeAnnotation_attribute(std::vector< annotationt > &)
const typet type_entry(u2 index)
optionalt< lambda_method_handlet > parse_method_handle(const class method_handle_infot &entry)
Read method handle pointed to from constant pool entry at index, return type of method handle and nam...
void rfield_attribute(fieldt &)
void rbytecode(std::vector< instructiont > &)
void rcode_attribute(methodt &method)
void read_bootstrapmethods_entry()
Read all entries of the BootstrapMethods attribute of a class file.
void get_class_refs()
Get the class references for the benefit of a dependency analysis.
void get_annotation_value_class_refs(const exprt &value)
See java_bytecode_parsert::get_annotation_class_refs.
java_bytecode_parse_treet parse_tree
std::vector< pool_entryt > constant_poolt
void skip_bytes(std::size_t bytes)
void read_verification_type_info(methodt::verification_type_infot &)
java_bytecode_parse_treet::fieldt fieldt
constant_poolt constant_pool
void get_class_refs_rec(const typet &)
void parse_local_variable_type_table(methodt &method)
Parses the local variable type table of a method.
const bool skip_instructions
void rRuntimeAnnotation(annotationt &)
void relement_value_pairs(annotationt::element_value_pairst &)
exprt & constant(u2 index)
void rinner_classes_attribute(const u4 &attribute_length)
Corresponds to the element_value structure Described in Java 8 specification 4.7.6 https://docs....
std::vector< irep_idt > rexceptions_attribute()
Corresponds to the element_value structure Described in Java 8 specification 4.7.5 https://docs....
void rmethod_attribute(methodt &method)
method_handle_kindt
Indicates what sort of code should be synthesised for a lambda call:
@ LAMBDA_STATIC_METHOD_HANDLE
Direct call to the given method.
@ LAMBDA_VIRTUAL_METHOD_HANDLE
Virtual call to the given interface or method.
@ UNKNOWN_HANDLE
Can't be called.
@ LAMBDA_CONSTRUCTOR_HANDLE
Instantiate the needed type then call a constructor.
mstreamt & result() const
method_handle_infot(const pool_entryt &entry)
base_ref_infot get_reference(const pool_entry_lookupt &pool_entry) const
method_handle_kindt handle_kind
method_handle_kindt get_handle_kind() const
method_handle_kindt
Correspond to the different valid values for field handle_kind From Java 8 spec 4....
Corresponds to the CONSTANT_NameAndType_info Structure Described in Java 8 specification 4....
name_and_type_infot(const pool_entryt &entry)
std::string get_descriptor(const pool_entry_lookupt &pool_entry) const
std::string get_name(const pool_entry_lookupt &pool_entry) const
Fixed-width bit-vector with two's complement interpretation.
const irep_idt & get_function() const
void set_java_bytecode_index(const irep_idt &index)
void set_line(const irep_idt &line)
void set_function(const irep_idt &function)
A struct tag type, i.e., struct_typet with an identifier.
Structure type, corresponds to C style structs.
std::function< pool_entryt &(u2)> pool_entry_lookupt
static std::string read_utf8_constant(const pool_entryt &entry)
structured_pool_entryt(const pool_entryt &entry)
static symbol_exprt typeless(const irep_idt &id)
Generate a symbol_exprt without a proper type.
const irep_idt & get_identifier() const
An expression denoting a type.
The type of an expression, extends irept.
const typet & subtype() const
Fixed-width bit-vector with unsigned binary interpretation.
const std::string & id2string(const irep_idt &d)
#define CONSTANT_Fieldref
#define VTYPE_INFO_DOUBLE
#define CONSTANT_InvokeDynamic
#define CONSTANT_Methodref
#define CONSTANT_NameAndType
#define VTYPE_INFO_OBJECT
optionalt< java_bytecode_parse_treet > java_bytecode_parse(std::istream &istream, const irep_idt &class_name, message_handlert &message_handler, bool skip_instructions)
Attempt to parse a Java class from the given stream.
#define CONSTANT_MethodType
#define VTYPE_INFO_INTEGER
static java_class_typet::method_handle_kindt get_method_handle_type(method_handle_infot::method_handle_kindt java_handle_kind)
Translate the lambda method reference kind in a class file into the kind of handling the method requi...
#define VTYPE_INFO_UNINIT
#define CONSTANT_InterfaceMethodref
#define VTYPE_INFO_UNINIT_THIS
#define CONSTANT_MethodHandle
#define VTYPE_INFO_ITEM_NULL
Representation of a constant Java string.
void get_dependencies_from_generic_parameters(const std::string &signature, std::set< irep_idt > &refs)
Collect information about generic type parameters from a given signature.
optionalt< typet > java_type_from_string(const std::string &src, const std::string &class_name_prefix)
Transforms a string representation of a Java type into an internal type representation thereof.
struct_tag_typet java_classname(const std::string &id)
bool is_java_array_tag(const irep_idt &tag)
See above.
const typet & java_array_element_type(const struct_tag_typet &array_symbol)
Return a const reference to the element type of a given java array type.
const java_method_typet & to_java_method_type(const typet &type)
optionalt< typet > java_type_from_string_with_exception(const std::string &descriptor, const optionalt< std::string > &signature, const std::string &class_name)
static optionalt< java_class_typet::java_lambda_method_handlet > lambda_method_handle(const symbol_tablet &symbol_table, const irep_idt &method_identifier, const java_method_typet &dynamic_method_type)
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
#define PRECONDITION(CONDITION)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
#define POSTCONDITION(CONDITION)
API to expression classes.
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
std::vector< element_value_pairt > element_value_pairst
static lambda_method_handlet get_unknown_handle()
optionalt< std::string > signature
bool attribute_bootstrapmethods_read
void add_method_handle(size_t bootstrap_index, const lambda_method_handlet &handle)
source_locationt source_location
optionalt< std::string > signature
verification_type_info_type type
exception_tablet exception_table
std::vector< annotationst > parameter_annotations
Java annotations that were applied to parameters of this method.
source_locationt source_location
stack_map_tablet stack_map_table
instructionst instructions
local_variable_tablet local_variable_table
std::vector< irep_idt > throws_exception_table