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