stackMapTable.hpp revision 1879:f95d63e2154a
1/*
2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#ifndef SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP
26#define SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP
27
28#include "classfile/stackMapFrame.hpp"
29#include "memory/allocation.hpp"
30#include "oops/constantPoolOop.hpp"
31#include "oops/methodOop.hpp"
32#include "utilities/globalDefinitions.hpp"
33#ifdef TARGET_ARCH_x86
34# include "bytes_x86.hpp"
35#endif
36#ifdef TARGET_ARCH_sparc
37# include "bytes_sparc.hpp"
38#endif
39#ifdef TARGET_ARCH_zero
40# include "bytes_zero.hpp"
41#endif
42
43class StackMapReader;
44
45// StackMapTable class is the StackMap table used by type checker
46class StackMapTable : public StackObj {
47 private:
48  // Logically, the _frame_count (as well as many fields in the StackFrame)
49  // should be a u2, but if we defined the variable as that type it will
50  // be difficult to detect/recover from overflow or underflow conditions.
51  // Widening the type and making it signed will help detect these.
52  int32_t              _code_length;
53  int32_t              _frame_count;     // Stackmap frame count
54  StackMapFrame**       _frame_array;
55
56 public:
57  StackMapTable(StackMapReader* reader, StackMapFrame* init_frame,
58                u2 max_locals, u2 max_stack,
59                char* code_data, int code_len, TRAPS);
60
61  inline int32_t get_frame_count() const { return _frame_count; }
62  inline int get_offset(int index) const {
63    return _frame_array[index]->offset();
64  }
65
66  // Match and/or update current_frame to the frame in stackmap table with
67  // specified offset. Return true if the two frames match.
68  bool match_stackmap(
69    StackMapFrame* current_frame, int32_t offset,
70    bool match, bool update, TRAPS) const;
71  // Match and/or update current_frame to the frame in stackmap table with
72  // specified offset and frame index. Return true if the two frames match.
73  bool match_stackmap(
74    StackMapFrame* current_frame, int32_t offset, int32_t frame_index,
75    bool match, bool update, TRAPS) const;
76
77  // Check jump instructions. Make sure there are no uninitialized
78  // instances on backward branch.
79  void check_jump_target(StackMapFrame* frame, int32_t target, TRAPS) const;
80
81  // The following methods are only used inside this class.
82
83  // Returns the frame array index where the frame with offset is stored.
84  int get_index_from_offset(int32_t offset) const;
85
86  // Make sure that there's no uninitialized object exist on backward branch.
87  void check_new_object(
88    const StackMapFrame* frame, int32_t target, TRAPS) const;
89
90  // Debugging
91  void print() const PRODUCT_RETURN;
92};
93
94class StackMapStream : StackObj {
95 private:
96  typeArrayHandle _data;
97  int _index;
98 public:
99  StackMapStream(typeArrayHandle ah)
100    : _data(ah), _index(0) {
101  }
102  u1 get_u1(TRAPS) {
103    if (_data == NULL || _index >= _data->length()) {
104      stackmap_format_error("access beyond the end of attribute", CHECK_0);
105    }
106    return _data->byte_at(_index++);
107  }
108  u2 get_u2(TRAPS) {
109    if (_data == NULL || _index >= _data->length() - 1) {
110      stackmap_format_error("access beyond the end of attribute", CHECK_0);
111    }
112    u2 res = Bytes::get_Java_u2((u1*)_data->byte_at_addr(_index));
113    _index += 2;
114    return res;
115  }
116  bool at_end() {
117    return (_data == NULL) || (_index == _data->length());
118  }
119  static void stackmap_format_error(const char* msg, TRAPS);
120};
121
122class StackMapReader : StackObj {
123 private:
124  // information about the class and method
125  constantPoolHandle  _cp;
126  ClassVerifier* _verifier;
127  StackMapStream* _stream;
128  char* _code_data;
129  int32_t _code_length;
130
131  // information get from the attribute
132  int32_t  _frame_count;       // frame count
133
134  int32_t chop(VerificationType* locals, int32_t length, int32_t chops);
135  VerificationType parse_verification_type(u1* flags, TRAPS);
136  void check_verification_type_array_size(
137      int32_t size, int32_t max_size, TRAPS) {
138    if (size < 0 || size > max_size) {
139      // Since this error could be caused someone rewriting the method
140      // but not knowing to update the stackmap data, we call the the
141      // verifier's error method, which may not throw an exception and
142      // failover to the old verifier instead.
143      _verifier->class_format_error(
144        "StackMapTable format error: bad type array size");
145    }
146  }
147
148  enum {
149    SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247,
150    SAME_EXTENDED = 251,
151    FULL = 255
152  };
153
154 public:
155  // Constructor
156  StackMapReader(ClassVerifier* v, StackMapStream* stream, char* code_data,
157                 int32_t code_len, TRAPS) :
158                 _verifier(v), _stream(stream),
159                 _code_data(code_data), _code_length(code_len) {
160    methodHandle m = v->method();
161    if (m->has_stackmap_table()) {
162      _cp = constantPoolHandle(THREAD, m->constants());
163      _frame_count = _stream->get_u2(CHECK);
164    } else {
165      // There's no stackmap table present. Frame count and size are 0.
166      _frame_count = 0;
167    }
168  }
169
170  inline int32_t get_frame_count() const                { return _frame_count; }
171  StackMapFrame* next(StackMapFrame* pre_frame, bool first,
172                      u2 max_locals, u2 max_stack, TRAPS);
173
174  void check_end(TRAPS) {
175    if (!_stream->at_end()) {
176      StackMapStream::stackmap_format_error("wrong attribute size", CHECK);
177    }
178  }
179};
180
181#endif // SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP
182