stackMapTable.hpp revision 0:a61af66fc99e
1/*
2 * Copyright 2003-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25class StackMapReader;
26
27// StackMapTable class is the StackMap table used by type checker
28class StackMapTable : public StackObj {
29 private:
30  // Logically, the _frame_count (as well as many fields in the StackFrame)
31  // should be a u2, but if we defined the variable as that type it will
32  // be difficult to detect/recover from overflow or underflow conditions.
33  // Widening the type and making it signed will help detect these.
34  int32_t              _code_length;
35  int32_t              _frame_count;     // Stackmap frame count
36  StackMapFrame**       _frame_array;
37
38 public:
39  StackMapTable(StackMapReader* reader, StackMapFrame* init_frame,
40                u2 max_locals, u2 max_stack,
41                char* code_data, int code_len, TRAPS);
42
43  inline int32_t get_frame_count() const { return _frame_count; }
44  inline int get_offset(int index) const {
45    return _frame_array[index]->offset();
46  }
47
48  // Match and/or update current_frame to the frame in stackmap table with
49  // specified offset. Return true if the two frames match.
50  bool match_stackmap(
51    StackMapFrame* current_frame, int32_t offset,
52    bool match, bool update, TRAPS) const;
53  // Match and/or update current_frame to the frame in stackmap table with
54  // specified offset and frame index. Return true if the two frames match.
55  bool match_stackmap(
56    StackMapFrame* current_frame, int32_t offset, int32_t frame_index,
57    bool match, bool update, TRAPS) const;
58
59  // Check jump instructions. Make sure there are no uninitialized
60  // instances on backward branch.
61  void check_jump_target(StackMapFrame* frame, int32_t target, TRAPS) const;
62
63  // The following methods are only used inside this class.
64
65  // Returns the frame array index where the frame with offset is stored.
66  int get_index_from_offset(int32_t offset) const;
67
68  // Make sure that there's no uninitialized object exist on backward branch.
69  void check_new_object(
70    const StackMapFrame* frame, int32_t target, TRAPS) const;
71
72  // Debugging
73  void print() const PRODUCT_RETURN;
74};
75
76class StackMapStream : StackObj {
77 private:
78  typeArrayHandle _data;
79  int _index;
80 public:
81  StackMapStream(typeArrayHandle ah)
82    : _data(ah), _index(0) {
83  }
84  u1 get_u1(TRAPS) {
85    if (_data == NULL || _index >= _data->length()) {
86      stackmap_format_error("access beyond the end of attribute", CHECK_0);
87    }
88    return _data->byte_at(_index++);
89  }
90  u2 get_u2(TRAPS) {
91    if (_data == NULL || _index >= _data->length() - 1) {
92      stackmap_format_error("access beyond the end of attribute", CHECK_0);
93    }
94    u2 res = Bytes::get_Java_u2((u1*)_data->byte_at_addr(_index));
95    _index += 2;
96    return res;
97  }
98  bool at_end() {
99    return (_data == NULL) || (_index == _data->length());
100  }
101  static void stackmap_format_error(const char* msg, TRAPS);
102};
103
104class StackMapReader : StackObj {
105 private:
106  // information about the class and method
107  constantPoolHandle  _cp;
108  ClassVerifier* _verifier;
109  StackMapStream* _stream;
110  char* _code_data;
111  int32_t _code_length;
112
113  // information get from the attribute
114  int32_t  _frame_count;       // frame count
115
116  int32_t chop(VerificationType* locals, int32_t length, int32_t chops);
117  VerificationType parse_verification_type(u1* flags, TRAPS);
118  void check_verification_type_array_size(
119      int32_t size, int32_t max_size, TRAPS) {
120    if (size < 0 || size > max_size) {
121      // Since this error could be caused someone rewriting the method
122      // but not knowing to update the stackmap data, we call the the
123      // verifier's error method, which may not throw an exception and
124      // failover to the old verifier instead.
125      _verifier->class_format_error(
126        "StackMapTable format error: bad type array size");
127    }
128  }
129
130  enum {
131    SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247,
132    SAME_EXTENDED = 251,
133    FULL = 255
134  };
135
136 public:
137  // Constructor
138  StackMapReader(ClassVerifier* v, StackMapStream* stream, char* code_data,
139                 int32_t code_len, TRAPS) :
140                 _verifier(v), _stream(stream),
141                 _code_data(code_data), _code_length(code_len) {
142    methodHandle m = v->method();
143    if (m->has_stackmap_table()) {
144      _cp = constantPoolHandle(THREAD, m->constants());
145      _frame_count = _stream->get_u2(CHECK);
146    } else {
147      // There's no stackmap table present. Frame count and size are 0.
148      _frame_count = 0;
149    }
150  }
151
152  inline int32_t get_frame_count() const                { return _frame_count; }
153  StackMapFrame* next(StackMapFrame* pre_frame, bool first,
154                      u2 max_locals, u2 max_stack, TRAPS);
155
156  void check_end(TRAPS) {
157    if (!_stream->at_end()) {
158      StackMapStream::stackmap_format_error("wrong attribute size", CHECK);
159    }
160  }
161};
162