1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/code/exceptionHandlerTable.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,230 @@ 1.4 +/* 1.5 + * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "code/exceptionHandlerTable.hpp" 1.30 +#include "code/nmethod.hpp" 1.31 +#include "memory/allocation.inline.hpp" 1.32 + 1.33 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 1.34 + 1.35 +void ExceptionHandlerTable::add_entry(HandlerTableEntry entry) { 1.36 + _nesting.check(); 1.37 + if (_length >= _size) { 1.38 + // not enough space => grow the table (amortized growth, double its size) 1.39 + guarantee(_size > 0, "no space allocated => cannot grow the table since it is part of nmethod"); 1.40 + int new_size = _size * 2; 1.41 + _table = REALLOC_RESOURCE_ARRAY(HandlerTableEntry, _table, _size, new_size); 1.42 + _size = new_size; 1.43 + } 1.44 + assert(_length < _size, "sanity check"); 1.45 + _table[_length++] = entry; 1.46 +} 1.47 + 1.48 + 1.49 +HandlerTableEntry* ExceptionHandlerTable::subtable_for(int catch_pco) const { 1.50 + int i = 0; 1.51 + while (i < _length) { 1.52 + HandlerTableEntry* t = _table + i; 1.53 + if (t->pco() == catch_pco) { 1.54 + // found subtable matching the catch_pco 1.55 + return t; 1.56 + } else { 1.57 + // advance to next subtable 1.58 + i += t->len() + 1; // +1 for header 1.59 + } 1.60 + } 1.61 + return NULL; 1.62 +} 1.63 + 1.64 + 1.65 +ExceptionHandlerTable::ExceptionHandlerTable(int initial_size) { 1.66 + guarantee(initial_size > 0, "initial size must be > 0"); 1.67 + _table = NEW_RESOURCE_ARRAY(HandlerTableEntry, initial_size); 1.68 + _length = 0; 1.69 + _size = initial_size; 1.70 +} 1.71 + 1.72 + 1.73 +ExceptionHandlerTable::ExceptionHandlerTable(const nmethod* nm) { 1.74 + _table = (HandlerTableEntry*)nm->handler_table_begin(); 1.75 + _length = nm->handler_table_size() / sizeof(HandlerTableEntry); 1.76 + _size = 0; // no space allocated by ExeptionHandlerTable! 1.77 +} 1.78 + 1.79 + 1.80 +void ExceptionHandlerTable::add_subtable( 1.81 + int catch_pco, 1.82 + GrowableArray<intptr_t>* handler_bcis, 1.83 + GrowableArray<intptr_t>* scope_depths_from_top_scope, 1.84 + GrowableArray<intptr_t>* handler_pcos 1.85 +) { 1.86 + assert(subtable_for(catch_pco) == NULL, "catch handlers for this catch_pco added twice"); 1.87 + assert(handler_bcis->length() == handler_pcos->length(), "bci & pc table have different length"); 1.88 + assert(scope_depths_from_top_scope == NULL || handler_bcis->length() == scope_depths_from_top_scope->length(), "bci & scope_depths table have different length"); 1.89 + if (handler_bcis->length() > 0) { 1.90 + // add subtable header 1.91 + add_entry(HandlerTableEntry(handler_bcis->length(), catch_pco, 0)); 1.92 + // add individual entries 1.93 + for (int i = 0; i < handler_bcis->length(); i++) { 1.94 + intptr_t scope_depth = 0; 1.95 + if (scope_depths_from_top_scope != NULL) { 1.96 + scope_depth = scope_depths_from_top_scope->at(i); 1.97 + } 1.98 + add_entry(HandlerTableEntry(handler_bcis->at(i), handler_pcos->at(i), scope_depth)); 1.99 + assert(entry_for(catch_pco, handler_bcis->at(i), scope_depth)->pco() == handler_pcos->at(i), "entry not added correctly (1)"); 1.100 + assert(entry_for(catch_pco, handler_bcis->at(i), scope_depth)->scope_depth() == scope_depth, "entry not added correctly (2)"); 1.101 + } 1.102 + } 1.103 +} 1.104 + 1.105 + 1.106 +void ExceptionHandlerTable::copy_to(nmethod* nm) { 1.107 + assert(size_in_bytes() == nm->handler_table_size(), "size of space allocated in nmethod incorrect"); 1.108 + memmove(nm->handler_table_begin(), _table, size_in_bytes()); 1.109 +} 1.110 + 1.111 + 1.112 +HandlerTableEntry* ExceptionHandlerTable::entry_for(int catch_pco, int handler_bci, int scope_depth) const { 1.113 + HandlerTableEntry* t = subtable_for(catch_pco); 1.114 + if (t != NULL) { 1.115 + int l = t->len(); 1.116 + while (l-- > 0) { 1.117 + t++; 1.118 + if (t->bci() == handler_bci && t->scope_depth() == scope_depth) return t; 1.119 + } 1.120 + } 1.121 + return NULL; 1.122 +} 1.123 + 1.124 + 1.125 +void ExceptionHandlerTable::print_subtable(HandlerTableEntry* t) const { 1.126 + int l = t->len(); 1.127 + tty->print_cr("catch_pco = %d (%d entries)", t->pco(), l); 1.128 + while (l-- > 0) { 1.129 + t++; 1.130 + tty->print_cr(" bci %d at scope depth %d -> pco %d", t->bci(), t->scope_depth(), t->pco()); 1.131 + } 1.132 +} 1.133 + 1.134 + 1.135 +void ExceptionHandlerTable::print() const { 1.136 + tty->print_cr("ExceptionHandlerTable (size = %d bytes)", size_in_bytes()); 1.137 + int i = 0; 1.138 + while (i < _length) { 1.139 + HandlerTableEntry* t = _table + i; 1.140 + print_subtable(t); 1.141 + // advance to next subtable 1.142 + i += t->len() + 1; // +1 for header 1.143 + } 1.144 +} 1.145 + 1.146 +void ExceptionHandlerTable::print_subtable_for(int catch_pco) const { 1.147 + HandlerTableEntry* subtable = subtable_for(catch_pco); 1.148 + 1.149 + if( subtable != NULL ) { print_subtable( subtable ); } 1.150 +} 1.151 + 1.152 +// ---------------------------------------------------------------------------- 1.153 +// Implicit null exception tables. Maps an exception PC offset to a 1.154 +// continuation PC offset. During construction it's a variable sized 1.155 +// array with a max size and current length. When stored inside an 1.156 +// nmethod a zero length table takes no space. This is detected by 1.157 +// nul_chk_table_size() == 0. Otherwise the table has a length word 1.158 +// followed by pairs of <excp-offset, const-offset>. 1.159 +void ImplicitExceptionTable::set_size( uint size ) { 1.160 + _size = size; 1.161 + _data = NEW_RESOURCE_ARRAY(implicit_null_entry, (size*2)); 1.162 + _len = 0; 1.163 +} 1.164 + 1.165 +void ImplicitExceptionTable::append( uint exec_off, uint cont_off ) { 1.166 + assert( (sizeof(implicit_null_entry) >= 4) || (exec_off < 65535), "" ); 1.167 + assert( (sizeof(implicit_null_entry) >= 4) || (cont_off < 65535), "" ); 1.168 + uint l = len(); 1.169 + if (l == _size) { 1.170 + uint old_size_in_elements = _size*2; 1.171 + if (_size == 0) _size = 4; 1.172 + _size *= 2; 1.173 + uint new_size_in_elements = _size*2; 1.174 + _data = REALLOC_RESOURCE_ARRAY(uint, _data, old_size_in_elements, new_size_in_elements); 1.175 + } 1.176 + *(adr(l) ) = exec_off; 1.177 + *(adr(l)+1) = cont_off; 1.178 + _len = l+1; 1.179 +}; 1.180 + 1.181 +uint ImplicitExceptionTable::at( uint exec_off ) const { 1.182 + uint l = len(); 1.183 + for( uint i=0; i<l; i++ ) 1.184 + if( *adr(i) == exec_off ) 1.185 + return *(adr(i)+1); 1.186 + return 0; // Failed to find any execption offset 1.187 +} 1.188 + 1.189 +void ImplicitExceptionTable::print(address base) const { 1.190 + tty->print("{"); 1.191 + for( uint i=0; i<len(); i++ ) 1.192 + tty->print("< "INTPTR_FORMAT", "INTPTR_FORMAT" > ",base + *adr(i), base + *(adr(i)+1)); 1.193 + tty->print_cr("}"); 1.194 +} 1.195 + 1.196 +ImplicitExceptionTable::ImplicitExceptionTable(const nmethod* nm) { 1.197 + if (nm->nul_chk_table_size() == 0) { 1.198 + _len = 0; 1.199 + _data = NULL; 1.200 + } else { 1.201 + // the first word is the length if non-zero, so read it out and 1.202 + // skip to the next word to get the table. 1.203 + _data = (implicit_null_entry*)nm->nul_chk_table_begin(); 1.204 + _len = _data[0]; 1.205 + _data++; 1.206 + } 1.207 + _size = len(); 1.208 + assert(size_in_bytes() <= nm->nul_chk_table_size(), "size of space allocated in nmethod incorrect"); 1.209 +} 1.210 + 1.211 +void ImplicitExceptionTable::copy_to( nmethod* nm ) { 1.212 + assert(size_in_bytes() <= nm->nul_chk_table_size(), "size of space allocated in nmethod incorrect"); 1.213 + if (len() != 0) { 1.214 + implicit_null_entry* nmdata = (implicit_null_entry*)nm->nul_chk_table_begin(); 1.215 + // store the length in the first uint 1.216 + nmdata[0] = _len; 1.217 + nmdata++; 1.218 + // copy the table after the length 1.219 + memmove( nmdata, _data, 2 * len() * sizeof(implicit_null_entry)); 1.220 + } else { 1.221 + // zero length table takes zero bytes 1.222 + assert(size_in_bytes() == 0, "bad size"); 1.223 + assert(nm->nul_chk_table_size() == 0, "bad size"); 1.224 + } 1.225 +} 1.226 + 1.227 +void ImplicitExceptionTable::verify(nmethod *nm) const { 1.228 + for (uint i = 0; i < len(); i++) { 1.229 + if ((*adr(i) > (unsigned int)nm->insts_size()) || 1.230 + (*(adr(i)+1) > (unsigned int)nm->insts_size())) 1.231 + fatal(err_msg("Invalid offset in ImplicitExceptionTable at " PTR_FORMAT, _data)); 1.232 + } 1.233 +}