duke@435: /* trims@2708: * Copyright (c) 2003, 2011, 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: #ifndef SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP stefank@2314: #define SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP stefank@2314: stefank@2314: #include "classfile/stackMapFrame.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "oops/constantPoolOop.hpp" stefank@2314: #include "oops/methodOop.hpp" stefank@2314: #include "utilities/globalDefinitions.hpp" stefank@2314: #ifdef TARGET_ARCH_x86 stefank@2314: # include "bytes_x86.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_sparc stefank@2314: # include "bytes_sparc.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_zero stefank@2314: # include "bytes_zero.hpp" stefank@2314: #endif bobv@2508: #ifdef TARGET_ARCH_arm bobv@2508: # include "bytes_arm.hpp" bobv@2508: #endif bobv@2508: #ifdef TARGET_ARCH_ppc bobv@2508: # include "bytes_ppc.hpp" bobv@2508: #endif stefank@2314: duke@435: class StackMapReader; duke@435: duke@435: // StackMapTable class is the StackMap table used by type checker duke@435: class StackMapTable : public StackObj { duke@435: private: duke@435: // Logically, the _frame_count (as well as many fields in the StackFrame) duke@435: // should be a u2, but if we defined the variable as that type it will duke@435: // be difficult to detect/recover from overflow or underflow conditions. duke@435: // Widening the type and making it signed will help detect these. duke@435: int32_t _code_length; duke@435: int32_t _frame_count; // Stackmap frame count duke@435: StackMapFrame** _frame_array; duke@435: duke@435: public: duke@435: StackMapTable(StackMapReader* reader, StackMapFrame* init_frame, duke@435: u2 max_locals, u2 max_stack, duke@435: char* code_data, int code_len, TRAPS); duke@435: duke@435: inline int32_t get_frame_count() const { return _frame_count; } duke@435: inline int get_offset(int index) const { duke@435: return _frame_array[index]->offset(); duke@435: } duke@435: duke@435: // Match and/or update current_frame to the frame in stackmap table with duke@435: // specified offset. Return true if the two frames match. duke@435: bool match_stackmap( duke@435: StackMapFrame* current_frame, int32_t offset, duke@435: bool match, bool update, TRAPS) const; duke@435: // Match and/or update current_frame to the frame in stackmap table with duke@435: // specified offset and frame index. Return true if the two frames match. duke@435: bool match_stackmap( duke@435: StackMapFrame* current_frame, int32_t offset, int32_t frame_index, duke@435: bool match, bool update, TRAPS) const; duke@435: duke@435: // Check jump instructions. Make sure there are no uninitialized duke@435: // instances on backward branch. duke@435: void check_jump_target(StackMapFrame* frame, int32_t target, TRAPS) const; duke@435: duke@435: // The following methods are only used inside this class. duke@435: duke@435: // Returns the frame array index where the frame with offset is stored. duke@435: int get_index_from_offset(int32_t offset) const; duke@435: duke@435: // Make sure that there's no uninitialized object exist on backward branch. duke@435: void check_new_object( duke@435: const StackMapFrame* frame, int32_t target, TRAPS) const; duke@435: duke@435: // Debugging duke@435: void print() const PRODUCT_RETURN; duke@435: }; duke@435: duke@435: class StackMapStream : StackObj { duke@435: private: duke@435: typeArrayHandle _data; duke@435: int _index; duke@435: public: duke@435: StackMapStream(typeArrayHandle ah) duke@435: : _data(ah), _index(0) { duke@435: } duke@435: u1 get_u1(TRAPS) { duke@435: if (_data == NULL || _index >= _data->length()) { duke@435: stackmap_format_error("access beyond the end of attribute", CHECK_0); duke@435: } duke@435: return _data->byte_at(_index++); duke@435: } duke@435: u2 get_u2(TRAPS) { duke@435: if (_data == NULL || _index >= _data->length() - 1) { duke@435: stackmap_format_error("access beyond the end of attribute", CHECK_0); duke@435: } duke@435: u2 res = Bytes::get_Java_u2((u1*)_data->byte_at_addr(_index)); duke@435: _index += 2; duke@435: return res; duke@435: } duke@435: bool at_end() { duke@435: return (_data == NULL) || (_index == _data->length()); duke@435: } duke@435: static void stackmap_format_error(const char* msg, TRAPS); duke@435: }; duke@435: duke@435: class StackMapReader : StackObj { duke@435: private: duke@435: // information about the class and method duke@435: constantPoolHandle _cp; duke@435: ClassVerifier* _verifier; duke@435: StackMapStream* _stream; duke@435: char* _code_data; duke@435: int32_t _code_length; duke@435: duke@435: // information get from the attribute duke@435: int32_t _frame_count; // frame count duke@435: duke@435: int32_t chop(VerificationType* locals, int32_t length, int32_t chops); duke@435: VerificationType parse_verification_type(u1* flags, TRAPS); duke@435: void check_verification_type_array_size( duke@435: int32_t size, int32_t max_size, TRAPS) { duke@435: if (size < 0 || size > max_size) { duke@435: // Since this error could be caused someone rewriting the method duke@435: // but not knowing to update the stackmap data, we call the the duke@435: // verifier's error method, which may not throw an exception and duke@435: // failover to the old verifier instead. duke@435: _verifier->class_format_error( duke@435: "StackMapTable format error: bad type array size"); duke@435: } duke@435: } duke@435: duke@435: enum { duke@435: SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247, duke@435: SAME_EXTENDED = 251, duke@435: FULL = 255 duke@435: }; duke@435: duke@435: public: duke@435: // Constructor duke@435: StackMapReader(ClassVerifier* v, StackMapStream* stream, char* code_data, duke@435: int32_t code_len, TRAPS) : duke@435: _verifier(v), _stream(stream), duke@435: _code_data(code_data), _code_length(code_len) { duke@435: methodHandle m = v->method(); duke@435: if (m->has_stackmap_table()) { duke@435: _cp = constantPoolHandle(THREAD, m->constants()); duke@435: _frame_count = _stream->get_u2(CHECK); duke@435: } else { duke@435: // There's no stackmap table present. Frame count and size are 0. duke@435: _frame_count = 0; duke@435: } duke@435: } duke@435: duke@435: inline int32_t get_frame_count() const { return _frame_count; } duke@435: StackMapFrame* next(StackMapFrame* pre_frame, bool first, duke@435: u2 max_locals, u2 max_stack, TRAPS); duke@435: duke@435: void check_end(TRAPS) { duke@435: if (!_stream->at_end()) { duke@435: StackMapStream::stackmap_format_error("wrong attribute size", CHECK); duke@435: } duke@435: } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP