86 error() <<
"invalid constant pool index (" << index <<
")" <<
eom;
111 bytecodes[p->opcode].mnemonic=p->mnemonic;
141 for(std::size_t i=0; i<bytes; i++)
145 error() <<
"unexpected end of bytecode file" <<
eom;
155 for(
size_t i=0; i<bytes; i++)
159 error() <<
"unexpected end of bytecode file" <<
eom;
190 size_t bootstrap_method_index,
194 #define CONSTANT_Class 7 195 #define CONSTANT_Fieldref 9 196 #define CONSTANT_Methodref 10 197 #define CONSTANT_InterfaceMethodref 11 198 #define CONSTANT_String 8 199 #define CONSTANT_Integer 3 200 #define CONSTANT_Float 4 201 #define CONSTANT_Long 5 202 #define CONSTANT_Double 6 203 #define CONSTANT_NameAndType 12 204 #define CONSTANT_Utf8 1 205 #define CONSTANT_MethodHandle 15 206 #define CONSTANT_MethodType 16 207 #define CONSTANT_InvokeDynamic 18 209 #define VTYPE_INFO_TOP 0 210 #define VTYPE_INFO_INTEGER 1 211 #define VTYPE_INFO_FLOAT 2 212 #define VTYPE_INFO_LONG 3 213 #define VTYPE_INFO_DOUBLE 4 214 #define VTYPE_INFO_ITEM_NULL 5 215 #define VTYPE_INFO_UNINIT_THIS 6 216 #define VTYPE_INFO_OBJECT 7 217 #define VTYPE_INFO_UNINIT 8 232 typedef std::function<java_bytecode_parsert::pool_entryt &(u2)>
304 static std::set<u1> info_tags = {
326 "name_and_typeindex did not correspond to a name_and_type in the " 424 catch(
const char *message)
430 catch(
const std::string &message)
445 #define ACC_PUBLIC 0x0001 446 #define ACC_PRIVATE 0x0002 447 #define ACC_PROTECTED 0x0004 448 #define ACC_STATIC 0x0008 449 #define ACC_FINAL 0x0010 450 #define ACC_SYNCHRONIZED 0x0020 451 #define ACC_BRIDGE 0x0040 452 #define ACC_VARARGS 0x0080 453 #define ACC_NATIVE 0x0100 454 #define ACC_INTERFACE 0x0200 455 #define ACC_ABSTRACT 0x0400 456 #define ACC_STRICT 0x0800 457 #define ACC_SYNTHETIC 0x1000 458 #define ACC_ANNOTATION 0x2000 459 #define ACC_ENUM 0x4000 464 #define UNUSED __attribute__((unused)) 475 if(magic!=0xCAFEBABE)
483 error() <<
"unexpected major version" <<
eom;
523 for(std::size_t j=0; j<attributes_count; j++)
558 if(field.signature.has_value())
579 if(method.signature.has_value())
592 for(
const auto &var : method.local_variable_table)
595 if(var.signature.has_value())
613 if(src.
id()==ID_code)
621 else if(src.
id()==ID_symbol)
626 const typet &element_type=
627 static_cast<const typet &
>(src.
find(ID_C_element_type));
633 else if(src.
id()==ID_struct)
639 else if(src.
id()==ID_pointer)
646 if(constant_pool_count==0)
648 error() <<
"invalid constant_pool_count" <<
eom;
654 for(constant_poolt::iterator
697 error() <<
"invalid double entry" <<
eom;
709 for(std::string::iterator s_it=s.begin(); s_it!=s.end(); s_it++)
721 error() <<
"unknown constant pool entry (" << it->tag <<
")" 728 for(constant_poolt::iterator
786 exprt virtual_function(ID_virtual_function, type);
787 virtual_function.
set(ID_component_name, component_name);
788 virtual_function.
set(ID_C_class, class_name);
789 virtual_function.
set(ID_C_base_name, name_entry.
s);
790 virtual_function.
set(ID_identifier, identifier);
792 it->expr=virtual_function;
799 exprt string_literal(ID_java_string_literal);
801 it->expr=string_literal;
831 it->expr.id(
"nameandtype");
837 it->expr.id(
"methodhandle");
843 it->expr.id(
"methodtype");
849 it->expr.id(
"invokedynamic");
852 type.
set(ID_java_lambda_method_handle_index, it->ref1);
853 it->expr.type()=type;
866 for(std::size_t i=0; i<interfaces_count; i++)
875 for(std::size_t i=0; i<fields_count; i++)
896 DATA_INVARIANT(flags<=1,
"at most one of public, protected, private");
898 for(std::size_t j=0; j<attributes_count; j++)
918 size_t bytecode_index=0;
920 for(address=0; address<code_length; address++)
922 bool wide_instruction=
false;
923 u4 start_of_instruction=address;
929 wide_instruction=
true;
937 "Unexpected wide instruction: " +
944 instruction.
address=start_of_instruction;
984 instruction.
args.push_back(
995 instruction.
args.push_back(
1003 if(wide_instruction)
1021 if(wide_instruction)
1053 u4 base_offset=address;
1056 while(((address+1)&3)!=0) {
read_u1(); address++; }
1062 instruction.
args.push_back(
1070 for(std::size_t i=0; i<npairs; i++)
1074 instruction.
args.push_back(
1078 instruction.
args.push_back(
1087 size_t base_offset=address;
1090 while(((address+1)&3)!=0) {
read_u1(); address++; }
1094 instruction.
args.push_back(
1107 for(
s4 i=low_value; i<=high_value; i++)
1113 instruction.
args.push_back(
1125 instruction.
args.push_back(
1142 case T_INT: t.
id(ID_int);
break;
1160 throw "unknown JVM bytecode instruction";
1165 if(address!=code_length)
1167 error() <<
"bytecode length mismatch" <<
eom;
1179 if(attribute_name==
"Code")
1189 for(std::size_t e=0; e<exception_table_length; e++)
1197 "The start_pc must be less than the end_pc as this is the range the " 1198 "exception is active");
1212 for(std::size_t j=0; j<attributes_count; j++)
1218 for(methodt::instructionst::iterator
1223 if(!it->source_location.get_line().empty())
1224 line_number=it->source_location.get_line();
1225 else if(!line_number.
empty())
1226 it->source_location.set_line(line_number);
1236 method.
instructions.begin()->source_location.get_line());
1238 else if(attribute_name==
"Signature")
1243 else if(attribute_name==
"RuntimeInvisibleAnnotations" ||
1244 attribute_name==
"RuntimeVisibleAnnotations")
1259 if(attribute_name==
"Signature")
1264 else if(attribute_name==
"RuntimeInvisibleAnnotations" ||
1265 attribute_name==
"RuntimeVisibleAnnotations")
1280 if(attribute_name==
"LineNumberTable")
1283 typedef std::map<unsigned,
1284 methodt::instructionst::iterator> instruction_mapt;
1285 instruction_mapt instruction_map;
1287 for(methodt::instructionst::iterator
1292 instruction_map[it->address]=it;
1297 for(std::size_t i=0; i<line_number_table_length; i++)
1303 instruction_mapt::const_iterator it=
1304 instruction_map.find(start_pc);
1306 if(it!=instruction_map.end())
1307 it->second->source_location.set_line(line_number);
1310 else if(attribute_name==
"LocalVariableTable")
1312 u2 local_variable_table_length=
read_u2();
1316 for(std::size_t i=0; i<local_variable_table_length; i++)
1332 else if(attribute_name==
"LocalVariableTypeTable")
1336 else if(attribute_name==
"StackMapTable")
1342 for(
size_t i=0; i<stack_map_entries; i++)
1351 else if(64<=frame_type && frame_type<=127)
1361 else if(frame_type==247)
1373 else if(248<=frame_type && frame_type<=250)
1381 else if(frame_type==251)
1390 else if(252<=frame_type && frame_type<=254)
1392 size_t new_locals=(size_t) (frame_type-251);
1398 for(
size_t k=0; k<new_locals; k++)
1407 else if(frame_type==255)
1414 for(
size_t k=0; k<(size_t) number_locals; k++)
1424 for(
size_t k=0; k<(size_t) number_stack_items; k++)
1434 throw "error: unknown stack frame type encountered";
1477 throw "error: unknown verification type info encountered";
1486 for(
u2 number=0; number<num_annotations; number++)
1490 annotations.push_back(annotation);
1506 element_value_pairs.resize(num_element_value_pairs);
1508 for(
auto &element_value_pair : element_value_pairs)
1511 element_value_pair.element_name=
pool_entry(element_name_index).
s;
1553 for(std::size_t i=0; i<num_values; i++)
1586 if(attribute_name==
"SourceFile")
1592 size_t last_index=fqn.find_last_of(
".");
1593 if(last_index==std::string::npos)
1597 std::string package_name=fqn.substr(0, last_index+1);
1598 std::replace(package_name.begin(), package_name.end(),
'.',
'/');
1599 const std::string &full_file_name=
1601 sourcefile_name=full_file_name;
1604 for(methodst::iterator m_it=parsed_class.
methods.begin();
1605 m_it!=parsed_class.
methods.end();
1608 m_it->source_location.set_file(sourcefile_name);
1609 for(instructionst::iterator i_it=m_it->instructions.begin();
1610 i_it!=m_it->instructions.end();
1613 if(!i_it->source_location.get_line().empty())
1614 i_it->source_location.set_file(sourcefile_name);
1618 else if(attribute_name==
"Signature")
1626 else if(attribute_name==
"RuntimeInvisibleAnnotations" ||
1627 attribute_name==
"RuntimeVisibleAnnotations")
1631 else if(attribute_name ==
"BootstrapMethods")
1637 "only one BootstrapMethods argument is allowed in a class file");
1651 for(std::size_t j=0; j<methods_count; j++)
1655 #define ACC_PUBLIC 0x0001 1656 #define ACC_PRIVATE 0x0002 1657 #define ACC_PROTECTED 0x0004 1658 #define ACC_STATIC 0x0008 1659 #define ACC_FINAL 0x0010 1660 #define ACC_SUPER 0x0020 1661 #define ACC_VOLATILE 0x0040 1662 #define ACC_TRANSIENT 0x0080 1663 #define ACC_INTERFACE 0x0200 1664 #define ACC_ABSTRACT 0x0400 1665 #define ACC_SYNTHETIC 0x1000 1666 #define ACC_ANNOTATION 0x2000 1667 #define ACC_ENUM 0x4000 1692 DATA_INVARIANT(flags<=1,
"at most one of public, protected, private");
1695 for(std::size_t j=0; j<attributes_count; j++)
1703 java_bytecode_parser.
in=&istream;
1706 bool parser_result=java_bytecode_parser.
parse();
1713 return std::move(java_bytecode_parser.
parse_tree);
1719 std::ifstream in(
file, std::ios::binary);
1724 message.
error() <<
"failed to open input file `" 1737 u2 local_variable_type_table_length=
read_u2();
1741 "Local variable type table cannot have more elements " 1742 "than the local variable table.");
1743 for(std::size_t i=0; i<local_variable_type_table_length; i++)
1755 if(lvar.index==index &&
1757 lvar.start_pc==start_pc &&
1758 lvar.length==length)
1767 "Entry in LocalVariableTypeTable must be present in LVT");
1779 const std::function<pool_entryt &(u2)> pool_entry_lambda =
1788 std::string class_name = class_entry.
get_name(pool_entry_lambda);
1790 std::replace(class_name.begin(), class_name.end(),
'.',
'$');
1792 std::replace(class_name.begin(), class_name.end(),
'/',
'.');
1793 const std::string method_ref =
1794 class_name +
"." + name_and_type.
get_name(pool_entry_lambda) +
':' +
1806 name_and_type.
get_name(pool_entry_lambda);
1811 return lambda_method_handle;
1823 for(
size_t bootstrap_method_index = 0;
1824 bootstrap_method_index < num_bootstrap_methods;
1825 ++bootstrap_method_index)
1827 u2 bootstrap_methodhandle_ref =
read_u2();
1833 debug() <<
"INFO: parse BootstrapMethod handle " << num_bootstrap_arguments
1837 u2_valuest u2_values(num_bootstrap_arguments);
1838 for(
size_t i = 0; i < num_bootstrap_arguments; i++)
1871 if(num_bootstrap_arguments < 3)
1874 parsed_class, bootstrap_method_index, std::move(u2_values));
1876 <<
"format of BootstrapMethods entry not recognized: too few arguments" 1881 u2 interface_type_index = u2_values[0];
1882 u2 method_handle_index = u2_values[1];
1883 u2 method_type_index = u2_values[2];
1889 bool recognized =
true;
1890 for(
size_t i = 3; i < num_bootstrap_arguments; i++)
1892 u2 skipped_argument = u2_values[i];
1898 debug() <<
"format of BootstrapMethods entry not recognized: extra " 1899 "arguments of wrong type" 1902 parsed_class, bootstrap_method_index, std::move(u2_values));
1916 debug() <<
"format of BootstrapMethods entry not recognized: arguments " 1920 parsed_class, bootstrap_method_index, std::move(u2_values));
1924 debug() <<
"INFO: parse lambda handle" <<
eom;
1928 if(!lambda_method_handle.has_value())
1930 debug() <<
"format of BootstrapMethods entry not recognized: method " 1931 "handle not recognised" 1934 parsed_class, bootstrap_method_index, std::move(u2_values));
1942 lambda_method_handle->interface_type =
1944 lambda_method_handle->method_type =
pool_entry(method_type_argument.
ref1).
s;
1945 lambda_method_handle->u2_values = std::move(u2_values);
1946 debug() <<
"lambda function reference " 1947 <<
id2string(lambda_method_handle->lambda_method_name)
1948 <<
" in class \"" << parsed_class.
name <<
"\"" 1949 <<
"\n interface type is " 1951 <<
"\n method type is " 1954 bootstrap_method_index, *lambda_method_handle);
1965 size_t bootstrap_method_index,
#define VTYPE_INFO_OBJECT
The type of an expression.
static lambda_method_handlet create_unknown_handle(const u2_valuest params)
java_bytecode_parse_treet::classt::lambda_method_handlet lambda_method_handlet
Fixed-width bit-vector with unsigned binary interpretation.
void rinterfaces(classt &parsed_class)
void store_unknown_method_handle(classt &parsed_class, size_t bootstrap_method_index, u2_valuest u2_values) const
Creates an unknown method handle and puts it into the parsed_class.
const std::string & id2string(const irep_idt &d)
void set_java_bytecode_index(const irep_idt &index)
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.
java_bytecode_parse_treet::classt::u2_valuest u2_valuest
void rRuntimeAnnotation_attribute(annotationst &)
#define CONSTANT_Methodref
constant_exprt to_expr() const
#define CONSTANT_MethodType
pool_entryt & pool_entry(u2 index)
void rmethod(classt &parsed_class)
method_handle_kindt
Correspond to the different valid values for field reference_kind From Java 8 spec 4...
source_locationt source_location
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
exception_tablet exception_table
java_bytecode_parse_treet::annotationst annotationst
void rmethods(classt &parsed_class)
#define CONSTANT_InterfaceMethodref
method_handle_typet handle_type
struct bytecode_infot const bytecode_info[]
const symbol_typet & to_symbol_type(const typet &type)
Cast a generic typet to a symbol_typet.
An expression denoting a type.
java_bytecode_parse_treet parse_tree
#define VTYPE_INFO_DOUBLE
const componentst & components() const
#define CONSTANT_Fieldref
Represents the argument of an instruction that uses a CONSTANT_Fieldref This is used for example as a...
Corresponds to the CONSTANT_Class_info Structure Described in Java 8 specification 4...
class_infot(const pool_entryt &entry)
exprt & constant(u2 index)
#define VTYPE_INFO_ITEM_NULL
static mstreamt & eom(mstreamt &m)
#define POSTCONDITION(CONDITION)
std::string get_name(pool_entry_lookupt pool_entry) const
#define INVARIANT(CONDITION, REASON)
static ieee_float_spect double_precision()
class_infot get_class(pool_entry_lookupt pool_entry) const
void relement_value_pairs(annotationt::element_value_pairst &)
#define CONSTANT_MethodHandle
java_bytecode_parsert::pool_entryt pool_entryt
instructionst instructions
std::vector< annotationt > annotationst
const irep_idt & id() const
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...
java_bytecode_parse_treet::instructiont instructiont
void unpack(const mp_integer &i)
A reference into the symbol table.
std::vector< pool_entryt > constant_poolt
name_and_type_infot get_name_and_type(pool_entry_lookupt pool_entry) const
#define VTYPE_INFO_UNINIT
#define CONSTANT_NameAndType
Fixed-width bit-vector with two's complement interpretation.
u2 get_class_index() const
void parse_local_variable_type_table(methodt &method)
Parses the local variable type table of a method.
verification_type_info_type type
nonstd::optional< T > optionalt
java_bytecode_parse_treet::classt classt
bool attribute_bootstrapmethods_read
API to expression classes.
optionalt< std::string > signature
const irep_idt & get(const irep_namet &name) const
void set_line(const irep_idt &line)
static ieee_float_spect single_precision()
std::vector< instructiont > instructionst
name_and_type_infot(java_bytecode_parsert::pool_entryt entry)
base_ref_infot get_reference(pool_entry_lookupt pool_entry) const
#define PRECONDITION(CONDITION)
#define VTYPE_INFO_UNINIT_THIS
bool has_prefix(const std::string &s, const std::string &prefix)
base_ref_infot(pool_entryt entry)
virtual void set_message_handler(message_handlert &_message_handler)
void rbytecode(methodt::instructionst &)
void skip_bytes(std::size_t bytes)
u8 read_bytes(size_t bytes)
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
optionalt< class java_bytecode_parse_treet > java_bytecode_parse(std::istream &istream, message_handlert &message_handler)
void rclass_attribute(classt &parsed_class)
void rmethod_attribute(methodt &method)
void get_class_refs_rec(const typet &)
typet java_type_from_string_with_exception(const std::string &descriptor, const optionalt< std::string > &signature, const std::string &class_name)
void relement_value_pair(annotationt::element_value_pairt &)
Corresponds to the element_value structure Described in Java 8 specification 4.7.16.1 https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.16.1.
std::string get_name(pool_entry_lookupt pool_entry) const
std::list< methodt > methodst
java_bytecode_parse_treet::classt::fieldst fieldst
method_handle_kindt reference_kind
std::vector< element_value_pairt > element_value_pairst
void rfields(classt &parsed_class)
std::vector< bytecodet > bytecodes
stack_map_tablet stack_map_table
element_value_pairst element_value_pairs
java_bytecode_parse_treet::annotationt annotationt
mstreamt & result() const
constant_poolt constant_pool
std::function< java_bytecode_parsert::pool_entryt &(u2)> pool_entry_lookupt
structured_pool_entryt(java_bytecode_parsert::pool_entryt entry)
irep_idt lambda_method_ref
Base class for all expressions.
#define VTYPE_INFO_INTEGER
java_bytecode_parse_treet::methodt::instructionst instructionst
source_locationt source_location
const parameterst & parameters() const
void read_bootstrapmethods_entry(classt &)
Read all entries of the BootstrapMethods attribute of a class file.
java_bytecode_parse_treet::fieldt fieldt
void read_verification_type_info(methodt::verification_type_infot &)
irep_idt lambda_method_name
u2 get_name_and_type_index() const
std::string to_string(const string_constraintt &expr)
Used for debug printing.
method_handle_infot(java_bytecode_parsert::pool_entryt entry)
java_bytecode_parse_treet::methodt methodt
std::vector< u2 > u2_valuest
const typet type_entry(u2 index)
void rcode_attribute(methodt &method)
Corresponds to the CONSTANT_NameAndType_info Structure Described in Java 8 specification 4...
Expression to hold a symbol (variable)
void add_method_handle(size_t bootstrap_index, lambda_method_handlet handle)
local_variable_tablet local_variable_table
goto_programt coverage_criteriont message_handlert & message_handler
void rRuntimeAnnotation(annotationt &)
const typet & subtype() const
#define DATA_INVARIANT(CONDITION, REASON)
java_bytecode_parse_treet::classt::methodst methodst
symbol_typet java_classname(const std::string &id)
std::string get_descriptor(pool_entry_lookupt pool_entry) const
java_bytecode_parse_treet::classt::method_handle_typet method_handle_typet
const irept & find(const irep_namet &name) const
optionalt< std::string > signature
#define CONSTANT_InvokeDynamic
const typet & return_type() const
void rfield_attribute(fieldt &)
static std::string read_utf8_constant(const pool_entryt &entry)
const irep_idt & get_identifier() const
void set(const irep_namet &name, const irep_idt &value)
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...
std::list< fieldt > fieldst