src/share/vm/classfile/classFileParser.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8049632 Sdiff src/share/vm/classfile

src/share/vm/classfile/classFileParser.cpp

Print this page




  45 #include "oops/instanceKlass.hpp"
  46 #include "oops/instanceMirrorKlass.hpp"
  47 #include "oops/klass.inline.hpp"
  48 #include "oops/klassVtable.hpp"
  49 #include "oops/method.hpp"
  50 #include "oops/symbol.hpp"
  51 #include "prims/jvm.h"
  52 #include "prims/jvmtiExport.hpp"
  53 #include "prims/jvmtiThreadState.hpp"
  54 #include "runtime/javaCalls.hpp"
  55 #include "runtime/perfData.hpp"
  56 #include "runtime/reflection.hpp"
  57 #include "runtime/signature.hpp"
  58 #include "runtime/timer.hpp"
  59 #include "services/classLoadingService.hpp"
  60 #include "services/threadService.hpp"
  61 #include "utilities/array.hpp"
  62 #include "utilities/globalDefinitions.hpp"
  63 #include "utilities/macros.hpp"
  64 #include "utilities/ostream.hpp"

  65 #if INCLUDE_CDS
  66 #include "classfile/systemDictionaryShared.hpp"
  67 #endif
  68 
  69 // We generally try to create the oops directly when parsing, rather than
  70 // allocating temporary data structures and copying the bytes twice. A
  71 // temporary area is only needed when parsing utf8 entries in the constant
  72 // pool and when parsing line number tables.
  73 
  74 // We add assert in debug mode when class format is not checked.
  75 
  76 #define JAVA_CLASSFILE_MAGIC              0xCAFEBABE
  77 #define JAVA_MIN_SUPPORTED_VERSION        45
  78 #define JAVA_MAX_SUPPORTED_VERSION        52
  79 #define JAVA_MAX_SUPPORTED_MINOR_VERSION  0
  80 
  81 // Used for two backward compatibility reasons:
  82 // - to check for new additions to the class file format in JDK1.5
  83 // - to check for bug fixes in the format checker in JDK1.5
  84 #define JAVA_1_5_VERSION                  49


1353 
1354   if ((*write_stream) == NULL) {
1355     if (length_in_bytes > fixed_buffer_size) {
1356       (*write_stream) = new CompressedLineNumberWriteStream(length_in_bytes);
1357     } else {
1358       (*write_stream) = new CompressedLineNumberWriteStream(
1359         linenumbertable_buffer, fixed_buffer_size);
1360     }
1361   }
1362 
1363   while (num_entries-- > 0) {
1364     u2 bci  = cfs->get_u2_fast(); // start_pc
1365     u2 line = cfs->get_u2_fast(); // line_number
1366     guarantee_property(bci < code_length,
1367         "Invalid pc in LineNumberTable in class file %s", CHECK);
1368     (*write_stream)->write_pair(bci, line);
1369   }
1370 }
1371 
1372 
1373 // Class file LocalVariableTable elements.
1374 class Classfile_LVT_Element VALUE_OBJ_CLASS_SPEC {
1375  public:
1376   u2 start_bci;
1377   u2 length;
1378   u2 name_cp_index;
1379   u2 descriptor_cp_index;
1380   u2 slot;
1381 };
1382 
1383 
1384 class LVT_Hash: public CHeapObj<mtClass> {
1385  public:
1386   LocalVariableTableElement  *_elem;  // element
1387   LVT_Hash*                   _next;  // Next entry in hash table
1388 };
1389 
1390 unsigned int hash(LocalVariableTableElement *elem) {
1391   unsigned int raw_hash = elem->start_bci;
1392 
1393   raw_hash = elem->length        + raw_hash * 37;
1394   raw_hash = elem->name_cp_index + raw_hash * 37;
1395   raw_hash = elem->slot          + raw_hash * 37;
1396 
1397   return raw_hash % HASH_ROW_SIZE;
1398 }
1399 
1400 void initialize_hashtable(LVT_Hash** table) {
1401   for (int i = 0; i < HASH_ROW_SIZE; i++) {
1402     table[i] = NULL;
1403   }
1404 }
1405 
1406 void clear_hashtable(LVT_Hash** table) {
1407   for (int i = 0; i < HASH_ROW_SIZE; i++) {
1408     LVT_Hash* current = table[i];
1409     LVT_Hash* next;
1410     while (current != NULL) {
1411       next = current->_next;
1412       current->_next = NULL;
1413       delete(current);
1414       current = next;
1415     }
1416     table[i] = NULL;
1417   }
1418 }
1419 
1420 LVT_Hash* LVT_lookup(LocalVariableTableElement *elem, int index, LVT_Hash** table) {
1421   LVT_Hash* entry = table[index];
1422 

1423   /*
1424    * 3-tuple start_bci/length/slot has to be unique key,
1425    * so the following comparison seems to be redundant:
1426    *       && elem->name_cp_index == entry->_elem->name_cp_index
1427    */
1428   while (entry != NULL) {
1429     if (elem->start_bci           == entry->_elem->start_bci
1430      && elem->length              == entry->_elem->length
1431      && elem->name_cp_index       == entry->_elem->name_cp_index
1432      && elem->slot                == entry->_elem->slot
1433     ) {
1434       return entry;
1435     }
1436     entry = entry->_next;
1437   }
1438   return NULL;
1439 }
1440 
1441 // Return false if the local variable is found in table.
1442 // Return true if no duplicate is found.
1443 // And local variable is added as a new entry in table.
1444 bool LVT_put_after_lookup(LocalVariableTableElement *elem, LVT_Hash** table) {
1445   // First lookup for duplicates
1446   int index = hash(elem);
1447   LVT_Hash* entry = LVT_lookup(elem, index, table);
1448 
1449   if (entry != NULL) {
1450       return false;
1451   }
1452   // No duplicate is found, allocate a new entry and fill it.
1453   if ((entry = new LVT_Hash()) == NULL) {
1454     return false;
1455   }
1456   entry->_elem = elem;
1457 
1458   // Insert into hash table
1459   entry->_next = table[index];
1460   table[index] = entry;
1461 
1462   return true;
1463 }







1464 
1465 void copy_lvt_element(Classfile_LVT_Element *src, LocalVariableTableElement *lvt) {
1466   lvt->start_bci           = Bytes::get_Java_u2((u1*) &src->start_bci);
1467   lvt->length              = Bytes::get_Java_u2((u1*) &src->length);
1468   lvt->name_cp_index       = Bytes::get_Java_u2((u1*) &src->name_cp_index);
1469   lvt->descriptor_cp_index = Bytes::get_Java_u2((u1*) &src->descriptor_cp_index);
1470   lvt->signature_cp_index  = 0;
1471   lvt->slot                = Bytes::get_Java_u2((u1*) &src->slot);
1472 }
1473 
1474 // Function is used to parse both attributes:
1475 //       LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT)
1476 u2* ClassFileParser::parse_localvariable_table(u4 code_length,
1477                                                u2 max_locals,
1478                                                u4 code_attribute_length,
1479                                                u2* localvariable_table_length,
1480                                                bool isLVTT,
1481                                                TRAPS) {
1482   ClassFileStream* cfs = stream();
1483   const char * tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable";


1844  * Rules for LVT's and LVTT's are:
1845  *   - There can be any number of LVT's and LVTT's.
1846  *   - If there are n LVT's, it is the same as if there was just
1847  *     one LVT containing all the entries from the n LVT's.
1848  *   - There may be no more than one LVT entry per local variable.
1849  *     Two LVT entries are 'equal' if these fields are the same:
1850  *        start_pc, length, name, slot
1851  *   - There may be no more than one LVTT entry per each LVT entry.
1852  *     Each LVTT entry has to match some LVT entry.
1853  *   - HotSpot internal LVT keeps natural ordering of class file LVT entries.
1854  */
1855 void ClassFileParser::copy_localvariable_table(ConstMethod* cm,
1856                                                int lvt_cnt,
1857                                                u2* localvariable_table_length,
1858                                                u2** localvariable_table_start,
1859                                                int lvtt_cnt,
1860                                                u2* localvariable_type_table_length,
1861                                                u2** localvariable_type_table_start,
1862                                                TRAPS) {
1863 
1864   LVT_Hash** lvt_Hash = NEW_RESOURCE_ARRAY(LVT_Hash*, HASH_ROW_SIZE);
1865   initialize_hashtable(lvt_Hash);




1866 
1867   // To fill LocalVariableTable in
1868   Classfile_LVT_Element*  cf_lvt;
1869   LocalVariableTableElement* lvt = cm->localvariable_table_start();
1870 
1871   for (int tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) {
1872     cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no];
1873     for (int idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) {
1874       copy_lvt_element(&cf_lvt[idx], lvt);
1875       // If no duplicates, add LVT elem in hashtable lvt_Hash.
1876       if (LVT_put_after_lookup(lvt, lvt_Hash) == false
1877           && _need_verify
1878           && _major_version >= JAVA_1_5_VERSION) {
1879         clear_hashtable(lvt_Hash);
1880         classfile_parse_error("Duplicated LocalVariableTable attribute "
1881                               "entry for '%s' in class file %s",
1882                                _cp->symbol_at(lvt->name_cp_index)->as_utf8(),
1883                                CHECK);
1884       }
1885     }
1886   }
1887 
1888   // To merge LocalVariableTable and LocalVariableTypeTable
1889   Classfile_LVT_Element* cf_lvtt;
1890   LocalVariableTableElement lvtt_elem;
1891 
1892   for (int tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) {
1893     cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no];
1894     for (int idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) {
1895       copy_lvt_element(&cf_lvtt[idx], &lvtt_elem);
1896       int index = hash(&lvtt_elem);
1897       LVT_Hash* entry = LVT_lookup(&lvtt_elem, index, lvt_Hash);
1898       if (entry == NULL) {
1899         if (_need_verify) {
1900           clear_hashtable(lvt_Hash);
1901           classfile_parse_error("LVTT entry for '%s' in class file %s "
1902                                 "does not match any LVT entry",
1903                                  _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),
1904                                  CHECK);
1905         }
1906       } else if (entry->_elem->signature_cp_index != 0 && _need_verify) {
1907         clear_hashtable(lvt_Hash);
1908         classfile_parse_error("Duplicated LocalVariableTypeTable attribute "
1909                               "entry for '%s' in class file %s",
1910                                _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),
1911                                CHECK);
1912       } else {
1913         // to add generic signatures into LocalVariableTable
1914         entry->_elem->signature_cp_index = lvtt_elem.descriptor_cp_index;
1915       }
1916     }
1917   }
1918   clear_hashtable(lvt_Hash);
1919 }
1920 
1921 
1922 void ClassFileParser::copy_method_annotations(ConstMethod* cm,
1923                                        u1* runtime_visible_annotations,
1924                                        int runtime_visible_annotations_length,
1925                                        u1* runtime_invisible_annotations,
1926                                        int runtime_invisible_annotations_length,
1927                                        u1* runtime_visible_parameter_annotations,
1928                                        int runtime_visible_parameter_annotations_length,
1929                                        u1* runtime_invisible_parameter_annotations,
1930                                        int runtime_invisible_parameter_annotations_length,
1931                                        u1* runtime_visible_type_annotations,
1932                                        int runtime_visible_type_annotations_length,
1933                                        u1* runtime_invisible_type_annotations,
1934                                        int runtime_invisible_type_annotations_length,
1935                                        u1* annotation_default,
1936                                        int annotation_default_length,
1937                                        TRAPS) {
1938 




  45 #include "oops/instanceKlass.hpp"
  46 #include "oops/instanceMirrorKlass.hpp"
  47 #include "oops/klass.inline.hpp"
  48 #include "oops/klassVtable.hpp"
  49 #include "oops/method.hpp"
  50 #include "oops/symbol.hpp"
  51 #include "prims/jvm.h"
  52 #include "prims/jvmtiExport.hpp"
  53 #include "prims/jvmtiThreadState.hpp"
  54 #include "runtime/javaCalls.hpp"
  55 #include "runtime/perfData.hpp"
  56 #include "runtime/reflection.hpp"
  57 #include "runtime/signature.hpp"
  58 #include "runtime/timer.hpp"
  59 #include "services/classLoadingService.hpp"
  60 #include "services/threadService.hpp"
  61 #include "utilities/array.hpp"
  62 #include "utilities/globalDefinitions.hpp"
  63 #include "utilities/macros.hpp"
  64 #include "utilities/ostream.hpp"
  65 #include "utilities/resourceHash.hpp"
  66 #if INCLUDE_CDS
  67 #include "classfile/systemDictionaryShared.hpp"
  68 #endif
  69 
  70 // We generally try to create the oops directly when parsing, rather than
  71 // allocating temporary data structures and copying the bytes twice. A
  72 // temporary area is only needed when parsing utf8 entries in the constant
  73 // pool and when parsing line number tables.
  74 
  75 // We add assert in debug mode when class format is not checked.
  76 
  77 #define JAVA_CLASSFILE_MAGIC              0xCAFEBABE
  78 #define JAVA_MIN_SUPPORTED_VERSION        45
  79 #define JAVA_MAX_SUPPORTED_VERSION        52
  80 #define JAVA_MAX_SUPPORTED_MINOR_VERSION  0
  81 
  82 // Used for two backward compatibility reasons:
  83 // - to check for new additions to the class file format in JDK1.5
  84 // - to check for bug fixes in the format checker in JDK1.5
  85 #define JAVA_1_5_VERSION                  49


1354 
1355   if ((*write_stream) == NULL) {
1356     if (length_in_bytes > fixed_buffer_size) {
1357       (*write_stream) = new CompressedLineNumberWriteStream(length_in_bytes);
1358     } else {
1359       (*write_stream) = new CompressedLineNumberWriteStream(
1360         linenumbertable_buffer, fixed_buffer_size);
1361     }
1362   }
1363 
1364   while (num_entries-- > 0) {
1365     u2 bci  = cfs->get_u2_fast(); // start_pc
1366     u2 line = cfs->get_u2_fast(); // line_number
1367     guarantee_property(bci < code_length,
1368         "Invalid pc in LineNumberTable in class file %s", CHECK);
1369     (*write_stream)->write_pair(bci, line);
1370   }
1371 }
1372 
1373 
1374 class LVT_Hash : public AllStatic {











1375  public:




































1376 
1377   static bool equals(LocalVariableTableElement const& e0, LocalVariableTableElement const& e1) {
1378   /*
1379    * 3-tuple start_bci/length/slot has to be unique key,
1380    * so the following comparison seems to be redundant:
1381    *       && elem->name_cp_index == entry->_elem->name_cp_index
1382    */
1383     return (e0.start_bci     == e1.start_bci && 
1384             e0.length        == e1.length && 
1385             e0.name_cp_index == e1.name_cp_index && 
1386             e0.slot          == e1.slot);





1387   }


1388 
1389   static unsigned int hash(LocalVariableTableElement const& e0) {
1390     unsigned int raw_hash = e0.start_bci;





1391   
1392     raw_hash = e0.length        + raw_hash * 37;
1393     raw_hash = e0.name_cp_index + raw_hash * 37;
1394     raw_hash = e0.slot          + raw_hash * 37;
1395 
1396     return raw_hash;

1397   }
1398 };
1399 



1400 
1401 // Class file LocalVariableTable elements.
1402 class Classfile_LVT_Element VALUE_OBJ_CLASS_SPEC {
1403  public:
1404   u2 start_bci;
1405   u2 length;
1406   u2 name_cp_index;
1407   u2 descriptor_cp_index;
1408   u2 slot;
1409 };
1410 
1411 void copy_lvt_element(Classfile_LVT_Element *src, LocalVariableTableElement *lvt) {
1412   lvt->start_bci           = Bytes::get_Java_u2((u1*) &src->start_bci);
1413   lvt->length              = Bytes::get_Java_u2((u1*) &src->length);
1414   lvt->name_cp_index       = Bytes::get_Java_u2((u1*) &src->name_cp_index);
1415   lvt->descriptor_cp_index = Bytes::get_Java_u2((u1*) &src->descriptor_cp_index);
1416   lvt->signature_cp_index  = 0;
1417   lvt->slot                = Bytes::get_Java_u2((u1*) &src->slot);
1418 }
1419 
1420 // Function is used to parse both attributes:
1421 //       LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT)
1422 u2* ClassFileParser::parse_localvariable_table(u4 code_length,
1423                                                u2 max_locals,
1424                                                u4 code_attribute_length,
1425                                                u2* localvariable_table_length,
1426                                                bool isLVTT,
1427                                                TRAPS) {
1428   ClassFileStream* cfs = stream();
1429   const char * tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable";


1790  * Rules for LVT's and LVTT's are:
1791  *   - There can be any number of LVT's and LVTT's.
1792  *   - If there are n LVT's, it is the same as if there was just
1793  *     one LVT containing all the entries from the n LVT's.
1794  *   - There may be no more than one LVT entry per local variable.
1795  *     Two LVT entries are 'equal' if these fields are the same:
1796  *        start_pc, length, name, slot
1797  *   - There may be no more than one LVTT entry per each LVT entry.
1798  *     Each LVTT entry has to match some LVT entry.
1799  *   - HotSpot internal LVT keeps natural ordering of class file LVT entries.
1800  */
1801 void ClassFileParser::copy_localvariable_table(ConstMethod* cm,
1802                                                int lvt_cnt,
1803                                                u2* localvariable_table_length,
1804                                                u2** localvariable_table_start,
1805                                                int lvtt_cnt,
1806                                                u2* localvariable_type_table_length,
1807                                                u2** localvariable_type_table_start,
1808                                                TRAPS) {
1809 
1810   ResourceMark rm(THREAD);
1811 
1812   typedef ResourceHashtable<LocalVariableTableElement, LocalVariableTableElement*,
1813                             &LVT_Hash::hash, &LVT_Hash::equals> LVT_HashTable;
1814 
1815   LVT_HashTable* table = new LVT_HashTable();
1816 
1817   // To fill LocalVariableTable in
1818   Classfile_LVT_Element*  cf_lvt;
1819   LocalVariableTableElement* lvt = cm->localvariable_table_start();
1820 
1821   for (int tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) {
1822     cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no];
1823     for (int idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) {
1824       copy_lvt_element(&cf_lvt[idx], lvt);
1825       // If no duplicates, add LVT elem in hashtable.
1826       if (table->put(*lvt, lvt) == false
1827           && _need_verify
1828           && _major_version >= JAVA_1_5_VERSION) {

1829         classfile_parse_error("Duplicated LocalVariableTable attribute "
1830                               "entry for '%s' in class file %s",
1831                                _cp->symbol_at(lvt->name_cp_index)->as_utf8(),
1832                                CHECK);
1833       }
1834     }
1835   }
1836 
1837   // To merge LocalVariableTable and LocalVariableTypeTable
1838   Classfile_LVT_Element* cf_lvtt;
1839   LocalVariableTableElement lvtt_elem;
1840 
1841   for (int tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) {
1842     cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no];
1843     for (int idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) {
1844       copy_lvt_element(&cf_lvtt[idx], &lvtt_elem);
1845       LocalVariableTableElement** entry = table->get(lvtt_elem);

1846       if (entry == NULL) {
1847         if (_need_verify) {

1848           classfile_parse_error("LVTT entry for '%s' in class file %s "
1849                                 "does not match any LVT entry",
1850                                  _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),
1851                                  CHECK);
1852         }
1853       } else if ((*entry)->signature_cp_index != 0 && _need_verify) {

1854         classfile_parse_error("Duplicated LocalVariableTypeTable attribute "
1855                               "entry for '%s' in class file %s",
1856                                _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),
1857                                CHECK);
1858       } else {
1859         // to add generic signatures into LocalVariableTable
1860         (*entry)->signature_cp_index = lvtt_elem.descriptor_cp_index;
1861       }
1862     }
1863   }

1864 }
1865 
1866 
1867 void ClassFileParser::copy_method_annotations(ConstMethod* cm,
1868                                        u1* runtime_visible_annotations,
1869                                        int runtime_visible_annotations_length,
1870                                        u1* runtime_invisible_annotations,
1871                                        int runtime_invisible_annotations_length,
1872                                        u1* runtime_visible_parameter_annotations,
1873                                        int runtime_visible_parameter_annotations_length,
1874                                        u1* runtime_invisible_parameter_annotations,
1875                                        int runtime_invisible_parameter_annotations_length,
1876                                        u1* runtime_visible_type_annotations,
1877                                        int runtime_visible_type_annotations_length,
1878                                        u1* runtime_invisible_type_annotations,
1879                                        int runtime_invisible_type_annotations_length,
1880                                        u1* annotation_default,
1881                                        int annotation_default_length,
1882                                        TRAPS) {
1883 


src/share/vm/classfile/classFileParser.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File