0N/A/*
3955N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP
1879N/A#define SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP
1879N/A
1879N/A#include "classfile/stackMapFrame.hpp"
3955N/A#include "classfile/verifier.hpp"
1879N/A#include "memory/allocation.hpp"
1879N/A#include "oops/constantPoolOop.hpp"
1879N/A#include "oops/methodOop.hpp"
1879N/A#include "utilities/globalDefinitions.hpp"
1879N/A#ifdef TARGET_ARCH_x86
1879N/A# include "bytes_x86.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_ARCH_sparc
1879N/A# include "bytes_sparc.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_ARCH_zero
1879N/A# include "bytes_zero.hpp"
1879N/A#endif
2073N/A#ifdef TARGET_ARCH_arm
2073N/A# include "bytes_arm.hpp"
2073N/A#endif
2073N/A#ifdef TARGET_ARCH_ppc
2073N/A# include "bytes_ppc.hpp"
2073N/A#endif
1879N/A
0N/Aclass StackMapReader;
0N/A
0N/A// StackMapTable class is the StackMap table used by type checker
0N/Aclass StackMapTable : public StackObj {
0N/A private:
0N/A // Logically, the _frame_count (as well as many fields in the StackFrame)
0N/A // should be a u2, but if we defined the variable as that type it will
0N/A // be difficult to detect/recover from overflow or underflow conditions.
0N/A // Widening the type and making it signed will help detect these.
0N/A int32_t _code_length;
0N/A int32_t _frame_count; // Stackmap frame count
0N/A StackMapFrame** _frame_array;
0N/A
0N/A public:
0N/A StackMapTable(StackMapReader* reader, StackMapFrame* init_frame,
0N/A u2 max_locals, u2 max_stack,
0N/A char* code_data, int code_len, TRAPS);
0N/A
0N/A inline int32_t get_frame_count() const { return _frame_count; }
0N/A inline int get_offset(int index) const {
0N/A return _frame_array[index]->offset();
0N/A }
0N/A
0N/A // Match and/or update current_frame to the frame in stackmap table with
0N/A // specified offset. Return true if the two frames match.
0N/A bool match_stackmap(
0N/A StackMapFrame* current_frame, int32_t offset,
3955N/A bool match, bool update, ErrorContext* ctx, TRAPS) const;
0N/A // Match and/or update current_frame to the frame in stackmap table with
0N/A // specified offset and frame index. Return true if the two frames match.
0N/A bool match_stackmap(
0N/A StackMapFrame* current_frame, int32_t offset, int32_t frame_index,
3955N/A bool match, bool update, ErrorContext* ctx, TRAPS) const;
0N/A
0N/A // Check jump instructions. Make sure there are no uninitialized
0N/A // instances on backward branch.
0N/A void check_jump_target(StackMapFrame* frame, int32_t target, TRAPS) const;
0N/A
0N/A // The following methods are only used inside this class.
0N/A
0N/A // Returns the frame array index where the frame with offset is stored.
0N/A int get_index_from_offset(int32_t offset) const;
0N/A
0N/A // Make sure that there's no uninitialized object exist on backward branch.
0N/A void check_new_object(
0N/A const StackMapFrame* frame, int32_t target, TRAPS) const;
0N/A
3955N/A void print_on(outputStream* str) const;
0N/A};
0N/A
0N/Aclass StackMapStream : StackObj {
0N/A private:
0N/A typeArrayHandle _data;
0N/A int _index;
0N/A public:
0N/A StackMapStream(typeArrayHandle ah)
0N/A : _data(ah), _index(0) {
0N/A }
0N/A u1 get_u1(TRAPS) {
0N/A if (_data == NULL || _index >= _data->length()) {
0N/A stackmap_format_error("access beyond the end of attribute", CHECK_0);
0N/A }
0N/A return _data->byte_at(_index++);
0N/A }
0N/A u2 get_u2(TRAPS) {
0N/A if (_data == NULL || _index >= _data->length() - 1) {
0N/A stackmap_format_error("access beyond the end of attribute", CHECK_0);
0N/A }
0N/A u2 res = Bytes::get_Java_u2((u1*)_data->byte_at_addr(_index));
0N/A _index += 2;
0N/A return res;
0N/A }
0N/A bool at_end() {
0N/A return (_data == NULL) || (_index == _data->length());
0N/A }
0N/A static void stackmap_format_error(const char* msg, TRAPS);
0N/A};
0N/A
0N/Aclass StackMapReader : StackObj {
0N/A private:
0N/A // information about the class and method
0N/A constantPoolHandle _cp;
0N/A ClassVerifier* _verifier;
0N/A StackMapStream* _stream;
0N/A char* _code_data;
0N/A int32_t _code_length;
0N/A
0N/A // information get from the attribute
0N/A int32_t _frame_count; // frame count
0N/A
0N/A int32_t chop(VerificationType* locals, int32_t length, int32_t chops);
0N/A VerificationType parse_verification_type(u1* flags, TRAPS);
0N/A void check_verification_type_array_size(
0N/A int32_t size, int32_t max_size, TRAPS) {
0N/A if (size < 0 || size > max_size) {
0N/A // Since this error could be caused someone rewriting the method
0N/A // but not knowing to update the stackmap data, we call the the
0N/A // verifier's error method, which may not throw an exception and
0N/A // failover to the old verifier instead.
0N/A _verifier->class_format_error(
0N/A "StackMapTable format error: bad type array size");
0N/A }
0N/A }
0N/A
0N/A enum {
0N/A SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247,
0N/A SAME_EXTENDED = 251,
0N/A FULL = 255
0N/A };
0N/A
0N/A public:
0N/A // Constructor
0N/A StackMapReader(ClassVerifier* v, StackMapStream* stream, char* code_data,
0N/A int32_t code_len, TRAPS) :
0N/A _verifier(v), _stream(stream),
0N/A _code_data(code_data), _code_length(code_len) {
0N/A methodHandle m = v->method();
0N/A if (m->has_stackmap_table()) {
0N/A _cp = constantPoolHandle(THREAD, m->constants());
0N/A _frame_count = _stream->get_u2(CHECK);
0N/A } else {
0N/A // There's no stackmap table present. Frame count and size are 0.
0N/A _frame_count = 0;
0N/A }
0N/A }
0N/A
0N/A inline int32_t get_frame_count() const { return _frame_count; }
0N/A StackMapFrame* next(StackMapFrame* pre_frame, bool first,
0N/A u2 max_locals, u2 max_stack, TRAPS);
0N/A
0N/A void check_end(TRAPS) {
0N/A if (!_stream->at_end()) {
0N/A StackMapStream::stackmap_format_error("wrong attribute size", CHECK);
0N/A }
0N/A }
0N/A};
1879N/A
1879N/A#endif // SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP