1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "code/exceptionHandlerTable.hpp"
  27 #include "code/nmethod.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 
  30 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  31 
  32 void ExceptionHandlerTable::add_entry(HandlerTableEntry entry) {
  33   _nesting.check();
  34   if (_length >= _size) {
  35     // not enough space => grow the table (amortized growth, double its size)
  36     guarantee(_size > 0, "no space allocated => cannot grow the table since it is part of nmethod");
  37     int new_size = _size * 2;
  38     _table = REALLOC_RESOURCE_ARRAY(HandlerTableEntry, _table, _size, new_size);
  39     _size = new_size;
  40   }
  41   assert(_length < _size, "sanity check");
  42   _table[_length++] = entry;
  43 }
  44 
  45 
  46 HandlerTableEntry* ExceptionHandlerTable::subtable_for(int catch_pco) const {
  47   int i = 0;
  48   while (i < _length) {
  49     HandlerTableEntry* t = _table + i;
  50     if (t->pco() == catch_pco) {
  51       // found subtable matching the catch_pco
  52       return t;
  53     } else {
  54       // advance to next subtable
  55       i += t->len() + 1; // +1 for header
  56     }
  57   }
  58   return NULL;
  59 }
  60 
  61 
  62 ExceptionHandlerTable::ExceptionHandlerTable(int initial_size) {
  63   guarantee(initial_size > 0, "initial size must be > 0");
  64   _table  = NEW_RESOURCE_ARRAY(HandlerTableEntry, initial_size);
  65   _length = 0;
  66   _size   = initial_size;
  67 }
  68 
  69 
  70 ExceptionHandlerTable::ExceptionHandlerTable(const nmethod* nm) {
  71   _table  = (HandlerTableEntry*)nm->handler_table_begin();
  72   _length = nm->handler_table_size() / sizeof(HandlerTableEntry);
  73   _size   = 0; // no space allocated by ExeptionHandlerTable!
  74 }
  75 
  76 
  77 void ExceptionHandlerTable::add_subtable(
  78   int                 catch_pco,
  79   GrowableArray<intptr_t>* handler_bcis,
  80   GrowableArray<intptr_t>* scope_depths_from_top_scope,
  81   GrowableArray<intptr_t>* handler_pcos
  82 ) {
  83   assert(subtable_for(catch_pco) == NULL, "catch handlers for this catch_pco added twice");
  84   assert(handler_bcis->length() == handler_pcos->length(), "bci & pc table have different length");
  85   assert(scope_depths_from_top_scope == NULL || handler_bcis->length() == scope_depths_from_top_scope->length(), "bci & scope_depths table have different length");
  86   if (handler_bcis->length() > 0) {
  87     // add subtable header
  88     add_entry(HandlerTableEntry(handler_bcis->length(), catch_pco, 0));
  89     // add individual entries
  90     for (int i = 0; i < handler_bcis->length(); i++) {
  91       intptr_t scope_depth = 0;
  92       if (scope_depths_from_top_scope != NULL) {
  93         scope_depth = scope_depths_from_top_scope->at(i);
  94       }
  95       add_entry(HandlerTableEntry(handler_bcis->at(i), handler_pcos->at(i), scope_depth));
  96       assert(entry_for(catch_pco, handler_bcis->at(i), scope_depth)->pco() == handler_pcos->at(i), "entry not added correctly (1)");
  97       assert(entry_for(catch_pco, handler_bcis->at(i), scope_depth)->scope_depth() == scope_depth, "entry not added correctly (2)");
  98     }
  99   }
 100 }
 101 
 102 
 103 void ExceptionHandlerTable::copy_to(nmethod* nm) {
 104   assert(size_in_bytes() == nm->handler_table_size(), "size of space allocated in nmethod incorrect");
 105   copy_bytes_to(nm->handler_table_begin());
 106 }
 107 
 108 void ExceptionHandlerTable::copy_bytes_to(address addr) {
 109   memmove(addr, _table, size_in_bytes());
 110 }
 111 
 112 HandlerTableEntry* ExceptionHandlerTable::entry_for(int catch_pco, int handler_bci, int scope_depth) const {
 113   HandlerTableEntry* t = subtable_for(catch_pco);
 114   if (t != NULL) {
 115     int l = t->len();
 116     while (l-- > 0) {
 117       t++;
 118       if (t->bci() == handler_bci && t->scope_depth() == scope_depth) return t;
 119     }
 120   }
 121   return NULL;
 122 }
 123 
 124 
 125 void ExceptionHandlerTable::print_subtable(HandlerTableEntry* t) const {
 126   int l = t->len();
 127   tty->print_cr("catch_pco = %d (%d entries)", t->pco(), l);
 128   while (l-- > 0) {
 129     t++;
 130     tty->print_cr("  bci %d at scope depth %d -> pco %d", t->bci(), t->scope_depth(), t->pco());
 131   }
 132 }
 133 
 134 
 135 void ExceptionHandlerTable::print() const {
 136   tty->print_cr("ExceptionHandlerTable (size = %d bytes)", size_in_bytes());
 137   int i = 0;
 138   while (i < _length) {
 139     HandlerTableEntry* t = _table + i;
 140     print_subtable(t);
 141     // advance to next subtable
 142     i += t->len() + 1; // +1 for header
 143   }
 144 }
 145 
 146 void ExceptionHandlerTable::print_subtable_for(int catch_pco) const {
 147   HandlerTableEntry* subtable = subtable_for(catch_pco);
 148 
 149   if( subtable != NULL ) { print_subtable( subtable ); }
 150 }
 151 
 152 // ----------------------------------------------------------------------------
 153 // Implicit null exception tables.  Maps an exception PC offset to a
 154 // continuation PC offset.  During construction it's a variable sized
 155 // array with a max size and current length.  When stored inside an
 156 // nmethod a zero length table takes no space.  This is detected by
 157 // nul_chk_table_size() == 0.  Otherwise the table has a length word
 158 // followed by pairs of <excp-offset, const-offset>.
 159 void ImplicitExceptionTable::set_size( uint size ) {
 160   _size = size;
 161   _data = NEW_RESOURCE_ARRAY(implicit_null_entry, (size*2));
 162   _len = 0;
 163 }
 164 
 165 void ImplicitExceptionTable::append( uint exec_off, uint cont_off ) {
 166   assert( (sizeof(implicit_null_entry) >= 4) || (exec_off < 65535), "" );
 167   assert( (sizeof(implicit_null_entry) >= 4) || (cont_off < 65535), "" );
 168   uint l = len();
 169   if (l == _size) {
 170     uint old_size_in_elements = _size*2;
 171     if (_size == 0) _size = 4;
 172     _size *= 2;
 173     uint new_size_in_elements = _size*2;
 174     _data = REALLOC_RESOURCE_ARRAY(uint, _data, old_size_in_elements, new_size_in_elements);
 175   }
 176   *(adr(l)  ) = exec_off;
 177   *(adr(l)+1) = cont_off;
 178   _len = l+1;
 179 };
 180 
 181 uint ImplicitExceptionTable::at( uint exec_off ) const {
 182   uint l = len();
 183   for( uint i=0; i<l; i++ )
 184     if( *adr(i) == exec_off )
 185       return *(adr(i)+1);
 186   return 0;                     // Failed to find any execption offset
 187 }
 188 
 189 void ImplicitExceptionTable::print(address base) const {
 190   tty->print("{");
 191   for( uint i=0; i<len(); i++ )
 192     tty->print("< " INTPTR_FORMAT ", " INTPTR_FORMAT " > ",base + *adr(i), base + *(adr(i)+1));
 193   tty->print_cr("}");
 194 }
 195 
 196 ImplicitExceptionTable::ImplicitExceptionTable(const nmethod* nm) {
 197   if (nm->nul_chk_table_size() == 0) {
 198     _len = 0;
 199     _data = NULL;
 200   } else {
 201     // the first word is the length if non-zero, so read it out and
 202     // skip to the next word to get the table.
 203     _data  = (implicit_null_entry*)nm->nul_chk_table_begin();
 204     _len = _data[0];
 205     _data++;
 206   }
 207   _size = len();
 208   assert(size_in_bytes() <= nm->nul_chk_table_size(), "size of space allocated in nmethod incorrect");
 209 }
 210 
 211 void ImplicitExceptionTable::copy_to( nmethod* nm ) {
 212   assert(size_in_bytes() <= nm->nul_chk_table_size(), "size of space allocated in nmethod incorrect");
 213   if (len() != 0) {
 214     implicit_null_entry* nmdata = (implicit_null_entry*)nm->nul_chk_table_begin();
 215     // store the length in the first uint
 216     nmdata[0] = _len;
 217     nmdata++;
 218     // copy the table after the length
 219     memmove( nmdata, _data, 2 * len() * sizeof(implicit_null_entry));
 220   } else {
 221     // zero length table takes zero bytes
 222     assert(size_in_bytes() == 0, "bad size");
 223     assert(nm->nul_chk_table_size() == 0, "bad size");
 224   }
 225 }
 226 
 227 void ImplicitExceptionTable::verify(nmethod *nm) const {
 228   for (uint i = 0; i < len(); i++) {
 229      if ((*adr(i) > (unsigned int)nm->insts_size()) ||
 230          (*(adr(i)+1) > (unsigned int)nm->insts_size()))
 231        fatal(err_msg("Invalid offset in ImplicitExceptionTable at " PTR_FORMAT, _data));
 232   }
 233 }