duke@435: /* drchase@6680: * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "code/exceptionHandlerTable.hpp" stefank@2314: #include "code/nmethod.hpp" stefank@2314: #include "memory/allocation.inline.hpp" duke@435: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC drchase@6680: duke@435: void ExceptionHandlerTable::add_entry(HandlerTableEntry entry) { duke@435: _nesting.check(); duke@435: if (_length >= _size) { duke@435: // not enough space => grow the table (amortized growth, double its size) duke@435: guarantee(_size > 0, "no space allocated => cannot grow the table since it is part of nmethod"); duke@435: int new_size = _size * 2; duke@435: _table = REALLOC_RESOURCE_ARRAY(HandlerTableEntry, _table, _size, new_size); duke@435: _size = new_size; duke@435: } duke@435: assert(_length < _size, "sanity check"); duke@435: _table[_length++] = entry; duke@435: } duke@435: duke@435: duke@435: HandlerTableEntry* ExceptionHandlerTable::subtable_for(int catch_pco) const { duke@435: int i = 0; duke@435: while (i < _length) { duke@435: HandlerTableEntry* t = _table + i; duke@435: if (t->pco() == catch_pco) { duke@435: // found subtable matching the catch_pco duke@435: return t; duke@435: } else { duke@435: // advance to next subtable duke@435: i += t->len() + 1; // +1 for header duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: ExceptionHandlerTable::ExceptionHandlerTable(int initial_size) { duke@435: guarantee(initial_size > 0, "initial size must be > 0"); duke@435: _table = NEW_RESOURCE_ARRAY(HandlerTableEntry, initial_size); duke@435: _length = 0; duke@435: _size = initial_size; duke@435: } duke@435: duke@435: duke@435: ExceptionHandlerTable::ExceptionHandlerTable(const nmethod* nm) { duke@435: _table = (HandlerTableEntry*)nm->handler_table_begin(); duke@435: _length = nm->handler_table_size() / sizeof(HandlerTableEntry); duke@435: _size = 0; // no space allocated by ExeptionHandlerTable! duke@435: } duke@435: duke@435: duke@435: void ExceptionHandlerTable::add_subtable( duke@435: int catch_pco, duke@435: GrowableArray* handler_bcis, duke@435: GrowableArray* scope_depths_from_top_scope, duke@435: GrowableArray* handler_pcos duke@435: ) { duke@435: assert(subtable_for(catch_pco) == NULL, "catch handlers for this catch_pco added twice"); duke@435: assert(handler_bcis->length() == handler_pcos->length(), "bci & pc table have different length"); duke@435: assert(scope_depths_from_top_scope == NULL || handler_bcis->length() == scope_depths_from_top_scope->length(), "bci & scope_depths table have different length"); duke@435: if (handler_bcis->length() > 0) { duke@435: // add subtable header duke@435: add_entry(HandlerTableEntry(handler_bcis->length(), catch_pco, 0)); duke@435: // add individual entries duke@435: for (int i = 0; i < handler_bcis->length(); i++) { duke@435: intptr_t scope_depth = 0; duke@435: if (scope_depths_from_top_scope != NULL) { duke@435: scope_depth = scope_depths_from_top_scope->at(i); duke@435: } duke@435: add_entry(HandlerTableEntry(handler_bcis->at(i), handler_pcos->at(i), scope_depth)); duke@435: assert(entry_for(catch_pco, handler_bcis->at(i), scope_depth)->pco() == handler_pcos->at(i), "entry not added correctly (1)"); duke@435: assert(entry_for(catch_pco, handler_bcis->at(i), scope_depth)->scope_depth() == scope_depth, "entry not added correctly (2)"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void ExceptionHandlerTable::copy_to(nmethod* nm) { duke@435: assert(size_in_bytes() == nm->handler_table_size(), "size of space allocated in nmethod incorrect"); duke@435: memmove(nm->handler_table_begin(), _table, size_in_bytes()); duke@435: } duke@435: duke@435: duke@435: HandlerTableEntry* ExceptionHandlerTable::entry_for(int catch_pco, int handler_bci, int scope_depth) const { duke@435: HandlerTableEntry* t = subtable_for(catch_pco); duke@435: if (t != NULL) { duke@435: int l = t->len(); duke@435: while (l-- > 0) { duke@435: t++; duke@435: if (t->bci() == handler_bci && t->scope_depth() == scope_depth) return t; duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: void ExceptionHandlerTable::print_subtable(HandlerTableEntry* t) const { duke@435: int l = t->len(); duke@435: tty->print_cr("catch_pco = %d (%d entries)", t->pco(), l); duke@435: while (l-- > 0) { duke@435: t++; duke@435: tty->print_cr(" bci %d at scope depth %d -> pco %d", t->bci(), t->scope_depth(), t->pco()); duke@435: } duke@435: } duke@435: duke@435: duke@435: void ExceptionHandlerTable::print() const { duke@435: tty->print_cr("ExceptionHandlerTable (size = %d bytes)", size_in_bytes()); duke@435: int i = 0; duke@435: while (i < _length) { duke@435: HandlerTableEntry* t = _table + i; duke@435: print_subtable(t); duke@435: // advance to next subtable duke@435: i += t->len() + 1; // +1 for header duke@435: } duke@435: } duke@435: duke@435: void ExceptionHandlerTable::print_subtable_for(int catch_pco) const { duke@435: HandlerTableEntry* subtable = subtable_for(catch_pco); duke@435: duke@435: if( subtable != NULL ) { print_subtable( subtable ); } duke@435: } duke@435: duke@435: // ---------------------------------------------------------------------------- duke@435: // Implicit null exception tables. Maps an exception PC offset to a duke@435: // continuation PC offset. During construction it's a variable sized duke@435: // array with a max size and current length. When stored inside an duke@435: // nmethod a zero length table takes no space. This is detected by duke@435: // nul_chk_table_size() == 0. Otherwise the table has a length word duke@435: // followed by pairs of . duke@435: void ImplicitExceptionTable::set_size( uint size ) { duke@435: _size = size; duke@435: _data = NEW_RESOURCE_ARRAY(implicit_null_entry, (size*2)); duke@435: _len = 0; duke@435: } duke@435: duke@435: void ImplicitExceptionTable::append( uint exec_off, uint cont_off ) { duke@435: assert( (sizeof(implicit_null_entry) >= 4) || (exec_off < 65535), "" ); duke@435: assert( (sizeof(implicit_null_entry) >= 4) || (cont_off < 65535), "" ); duke@435: uint l = len(); duke@435: if (l == _size) { duke@435: uint old_size_in_elements = _size*2; duke@435: if (_size == 0) _size = 4; duke@435: _size *= 2; duke@435: uint new_size_in_elements = _size*2; duke@435: _data = REALLOC_RESOURCE_ARRAY(uint, _data, old_size_in_elements, new_size_in_elements); duke@435: } duke@435: *(adr(l) ) = exec_off; duke@435: *(adr(l)+1) = cont_off; duke@435: _len = l+1; duke@435: }; duke@435: duke@435: uint ImplicitExceptionTable::at( uint exec_off ) const { duke@435: uint l = len(); duke@435: for( uint i=0; iprint("{"); duke@435: for( uint i=0; iprint("< " INTPTR_FORMAT ", " INTPTR_FORMAT " > ",base + *adr(i), base + *(adr(i)+1)); duke@435: tty->print_cr("}"); duke@435: } duke@435: duke@435: ImplicitExceptionTable::ImplicitExceptionTable(const nmethod* nm) { duke@435: if (nm->nul_chk_table_size() == 0) { duke@435: _len = 0; duke@435: _data = NULL; duke@435: } else { duke@435: // the first word is the length if non-zero, so read it out and duke@435: // skip to the next word to get the table. duke@435: _data = (implicit_null_entry*)nm->nul_chk_table_begin(); duke@435: _len = _data[0]; duke@435: _data++; duke@435: } duke@435: _size = len(); duke@435: assert(size_in_bytes() <= nm->nul_chk_table_size(), "size of space allocated in nmethod incorrect"); duke@435: } duke@435: duke@435: void ImplicitExceptionTable::copy_to( nmethod* nm ) { duke@435: assert(size_in_bytes() <= nm->nul_chk_table_size(), "size of space allocated in nmethod incorrect"); duke@435: if (len() != 0) { duke@435: implicit_null_entry* nmdata = (implicit_null_entry*)nm->nul_chk_table_begin(); duke@435: // store the length in the first uint duke@435: nmdata[0] = _len; duke@435: nmdata++; duke@435: // copy the table after the length duke@435: memmove( nmdata, _data, 2 * len() * sizeof(implicit_null_entry)); duke@435: } else { duke@435: // zero length table takes zero bytes duke@435: assert(size_in_bytes() == 0, "bad size"); duke@435: assert(nm->nul_chk_table_size() == 0, "bad size"); duke@435: } duke@435: } duke@435: duke@435: void ImplicitExceptionTable::verify(nmethod *nm) const { duke@435: for (uint i = 0; i < len(); i++) { twisti@2103: if ((*adr(i) > (unsigned int)nm->insts_size()) || twisti@2103: (*(adr(i)+1) > (unsigned int)nm->insts_size())) jcoomes@1845: fatal(err_msg("Invalid offset in ImplicitExceptionTable at " PTR_FORMAT, _data)); duke@435: } duke@435: }