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_STACKMAPFRAME_HPP
1879N/A#define SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP
1879N/A
1879N/A#include "classfile/verificationType.hpp"
1879N/A#include "classfile/verifier.hpp"
1879N/A#include "oops/methodOop.hpp"
1879N/A#include "runtime/handles.hpp"
1879N/A#include "runtime/signature.hpp"
1879N/A#include "utilities/exceptions.hpp"
1879N/A
0N/A// A StackMapFrame represents one frame in the stack map attribute.
0N/A
3955N/Aclass TypeContext;
3955N/A
0N/Aenum {
0N/A FLAG_THIS_UNINIT = 0x01
0N/A};
0N/A
0N/Aclass StackMapFrame : public ResourceObj {
0N/A private:
0N/A int32_t _offset;
0N/A
0N/A // See comment in StackMapTable about _frame_count about why these
0N/A // fields are int32_t instead of u2.
0N/A int32_t _locals_size; // number of valid type elements in _locals
0N/A int32_t _stack_size; // number of valid type elements in _stack
0N/A
3955N/A int32_t _stack_mark; // Records the size of the stack prior to an
3955N/A // instruction modification, to allow rewinding
3955N/A // when/if an error occurs.
3955N/A
0N/A int32_t _max_locals;
0N/A int32_t _max_stack;
0N/A
0N/A u1 _flags;
0N/A VerificationType* _locals; // local variable type array
0N/A VerificationType* _stack; // operand stack type array
0N/A
0N/A ClassVerifier* _verifier; // the verifier verifying this method
0N/A
3955N/A StackMapFrame(const StackMapFrame& cp) :
3955N/A _offset(cp._offset), _locals_size(cp._locals_size),
3955N/A _stack_size(cp._stack_size), _stack_mark(cp._stack_mark),
3955N/A _max_locals(cp._max_locals), _max_stack(cp._max_stack),
3955N/A _flags(cp._flags) {
3955N/A _locals = NEW_RESOURCE_ARRAY(VerificationType, _max_locals);
3955N/A for (int i = 0; i < _max_locals; ++i) {
3955N/A if (i < _locals_size) {
3955N/A _locals[i] = cp._locals[i];
3955N/A } else {
3955N/A _locals[i] = VerificationType::bogus_type();
3955N/A }
3955N/A }
3955N/A int ss = MAX2(_stack_size, _stack_mark);
3955N/A _stack = NEW_RESOURCE_ARRAY(VerificationType, _max_stack);
3955N/A for (int i = 0; i < _max_stack; ++i) {
3955N/A if (i < ss) {
3955N/A _stack[i] = cp._stack[i];
3955N/A } else {
3955N/A _stack[i] = VerificationType::bogus_type();
3955N/A }
3955N/A }
3955N/A _verifier = NULL;
3955N/A }
3955N/A
0N/A public:
0N/A // constructors
0N/A
0N/A // This constructor is used by the type checker to allocate frames
0N/A // in type state, which have _max_locals and _max_stack array elements
0N/A // in _locals and _stack.
0N/A StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* verifier);
0N/A
0N/A // This constructor is used to initialize stackmap frames in stackmap table,
0N/A // which have _locals_size and _stack_size array elements in _locals and _stack.
0N/A StackMapFrame(int32_t offset,
0N/A u1 flags,
0N/A u2 locals_size,
0N/A u2 stack_size,
0N/A u2 max_locals,
0N/A u2 max_stack,
0N/A VerificationType* locals,
0N/A VerificationType* stack,
0N/A ClassVerifier* v) : _offset(offset), _flags(flags),
0N/A _locals_size(locals_size),
0N/A _stack_size(stack_size),
3955N/A _stack_mark(-1),
0N/A _max_locals(max_locals),
0N/A _max_stack(max_stack),
0N/A _locals(locals), _stack(stack),
0N/A _verifier(v) { }
0N/A
3955N/A static StackMapFrame* copy(StackMapFrame* smf) {
3955N/A return new StackMapFrame(*smf);
3955N/A }
3955N/A
0N/A inline void set_offset(int32_t offset) { _offset = offset; }
0N/A inline void set_verifier(ClassVerifier* v) { _verifier = v; }
0N/A inline void set_flags(u1 flags) { _flags = flags; }
0N/A inline void set_locals_size(u2 locals_size) { _locals_size = locals_size; }
3955N/A inline void set_stack_size(u2 stack_size) { _stack_size = _stack_mark = stack_size; }
0N/A inline void clear_stack() { _stack_size = 0; }
0N/A inline int32_t offset() const { return _offset; }
0N/A inline ClassVerifier* verifier() const { return _verifier; }
0N/A inline u1 flags() const { return _flags; }
0N/A inline int32_t locals_size() const { return _locals_size; }
0N/A inline VerificationType* locals() const { return _locals; }
0N/A inline int32_t stack_size() const { return _stack_size; }
0N/A inline VerificationType* stack() const { return _stack; }
0N/A inline int32_t max_locals() const { return _max_locals; }
0N/A inline int32_t max_stack() const { return _max_stack; }
0N/A inline bool flag_this_uninit() const { return _flags & FLAG_THIS_UNINIT; }
0N/A
0N/A // Set locals and stack types to bogus
0N/A inline void reset() {
0N/A int32_t i;
0N/A for (i = 0; i < _max_locals; i++) {
0N/A _locals[i] = VerificationType::bogus_type();
0N/A }
0N/A for (i = 0; i < _max_stack; i++) {
0N/A _stack[i] = VerificationType::bogus_type();
0N/A }
0N/A }
0N/A
0N/A // Return a StackMapFrame with the same local variable array and empty stack.
0N/A // Stack array is allocate with unused one element.
0N/A StackMapFrame* frame_in_exception_handler(u1 flags);
0N/A
0N/A // Set local variable type array based on m's signature.
0N/A VerificationType set_locals_from_arg(
0N/A const methodHandle m, VerificationType thisKlass, TRAPS);
0N/A
0N/A // Search local variable type array and stack type array.
0N/A // Return true if an uninitialized object is found.
0N/A bool has_new_object() const;
0N/A
0N/A // Search local variable type array and stack type array.
0N/A // Set every element with type of old_object to new_object.
0N/A void initialize_object(
0N/A VerificationType old_object, VerificationType new_object);
0N/A
0N/A // Copy local variable type array in src into this local variable type array.
0N/A void copy_locals(const StackMapFrame* src);
0N/A
0N/A // Copy stack type array in src into this stack type array.
0N/A void copy_stack(const StackMapFrame* src);
0N/A
0N/A // Return true if this stack map frame is assignable to target.
3955N/A bool is_assignable_to(
3955N/A const StackMapFrame* target, bool is_exception_handler,
3955N/A ErrorContext* ctx, TRAPS) const;
3955N/A
3955N/A inline void set_mark() {
3955N/A#ifdef DEBUG
3955N/A // Put bogus type to indicate it's no longer valid.
3955N/A if (_stack_mark != -1) {
3955N/A for (int i = _stack_mark; i >= _stack_size; --i) {
3955N/A _stack[i] = VerificationType::bogus_type();
3955N/A }
3955N/A }
3955N/A#endif // def DEBUG
3955N/A _stack_mark = _stack_size;
3955N/A }
3955N/A
3955N/A // Used when an error occurs and we want to reset the stack to the state
3955N/A // it was before operands were popped off.
3955N/A void restore() {
3955N/A if (_stack_mark != -1) {
3955N/A _stack_size = _stack_mark;
3955N/A }
3955N/A }
0N/A
0N/A // Push type into stack type array.
0N/A inline void push_stack(VerificationType type, TRAPS) {
0N/A assert(!type.is_check(), "Must be a real type");
0N/A if (_stack_size >= _max_stack) {
3955N/A verifier()->verify_error(
3955N/A ErrorContext::stack_overflow(_offset, this),
3955N/A "Operand stack overflow");
0N/A return;
0N/A }
0N/A _stack[_stack_size++] = type;
0N/A }
0N/A
0N/A inline void push_stack_2(
0N/A VerificationType type1, VerificationType type2, TRAPS) {
0N/A assert(type1.is_long() || type1.is_double(), "must be long/double");
0N/A assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
0N/A if (_stack_size >= _max_stack - 1) {
3955N/A verifier()->verify_error(
3955N/A ErrorContext::stack_overflow(_offset, this),
3955N/A "Operand stack overflow");
0N/A return;
0N/A }
0N/A _stack[_stack_size++] = type1;
0N/A _stack[_stack_size++] = type2;
0N/A }
0N/A
0N/A // Pop and return the top type on stack without verifying.
0N/A inline VerificationType pop_stack(TRAPS) {
0N/A if (_stack_size <= 0) {
3955N/A verifier()->verify_error(
3955N/A ErrorContext::stack_underflow(_offset, this),
3955N/A "Operand stack underflow");
0N/A return VerificationType::bogus_type();
0N/A }
0N/A VerificationType top = _stack[--_stack_size];
0N/A return top;
0N/A }
0N/A
0N/A // Pop and return the top type on stack type array after verifying it
0N/A // is assignable to type.
0N/A inline VerificationType pop_stack(VerificationType type, TRAPS) {
0N/A if (_stack_size != 0) {
0N/A VerificationType top = _stack[_stack_size - 1];
0N/A bool subtype = type.is_assignable_from(
2062N/A top, verifier(), CHECK_(VerificationType::bogus_type()));
0N/A if (subtype) {
3955N/A --_stack_size;
0N/A return top;
0N/A }
0N/A }
0N/A return pop_stack_ex(type, THREAD);
0N/A }
0N/A
0N/A inline void pop_stack_2(
0N/A VerificationType type1, VerificationType type2, TRAPS) {
0N/A assert(type1.is_long2() || type1.is_double2(), "must be long/double");
0N/A assert(type2.is_long() || type2.is_double(), "must be long/double_2");
0N/A if (_stack_size >= 2) {
0N/A VerificationType top1 = _stack[_stack_size - 1];
2062N/A bool subtype1 = type1.is_assignable_from(top1, verifier(), CHECK);
0N/A VerificationType top2 = _stack[_stack_size - 2];
2062N/A bool subtype2 = type2.is_assignable_from(top2, verifier(), CHECK);
0N/A if (subtype1 && subtype2) {
0N/A _stack_size -= 2;
0N/A return;
0N/A }
0N/A }
0N/A pop_stack_ex(type1, THREAD);
0N/A pop_stack_ex(type2, THREAD);
0N/A }
0N/A
3955N/A VerificationType local_at(int index) {
3955N/A return _locals[index];
3955N/A }
3955N/A
3955N/A VerificationType stack_at(int index) {
3955N/A return _stack[index];
3955N/A }
3955N/A
0N/A // Uncommon case that throws exceptions.
0N/A VerificationType pop_stack_ex(VerificationType type, TRAPS);
0N/A
0N/A // Return the type at index in local variable array after verifying
0N/A // it is assignable to type.
0N/A VerificationType get_local(int32_t index, VerificationType type, TRAPS);
0N/A // For long/double.
0N/A void get_local_2(
0N/A int32_t index, VerificationType type1, VerificationType type2, TRAPS);
0N/A
0N/A // Set element at index in local variable array to type.
0N/A void set_local(int32_t index, VerificationType type, TRAPS);
0N/A // For long/double.
0N/A void set_local_2(
0N/A int32_t index, VerificationType type1, VerificationType type2, TRAPS);
0N/A
0N/A // Private auxiliary method used only in is_assignable_to(StackMapFrame).
0N/A // Returns true if src is assignable to target.
3955N/A int is_assignable_to(
0N/A VerificationType* src, VerificationType* target, int32_t len, TRAPS) const;
0N/A
2150N/A bool has_flag_match_exception(const StackMapFrame* target) const;
2150N/A
3955N/A TypeOrigin stack_top_ctx();
3955N/A
3955N/A void print_on(outputStream* str) const;
0N/A};
1879N/A
1879N/A#endif // SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP