src/share/vm/classfile/verificationType.hpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 6876
710a3c8b516e
child 9572
624a0741915c
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_CLASSFILE_VERIFICATIONTYPE_HPP
aoqi@0 26 #define SHARE_VM_CLASSFILE_VERIFICATIONTYPE_HPP
aoqi@0 27
aoqi@0 28 #include "classfile/systemDictionary.hpp"
aoqi@0 29 #include "memory/allocation.hpp"
aoqi@0 30 #include "oops/instanceKlass.hpp"
aoqi@0 31 #include "oops/oop.inline.hpp"
aoqi@0 32 #include "oops/symbol.hpp"
aoqi@0 33 #include "runtime/handles.hpp"
aoqi@0 34 #include "runtime/signature.hpp"
aoqi@0 35
aoqi@0 36 enum {
aoqi@0 37 // As specifed in the JVM spec
aoqi@0 38 ITEM_Top = 0,
aoqi@0 39 ITEM_Integer = 1,
aoqi@0 40 ITEM_Float = 2,
aoqi@0 41 ITEM_Double = 3,
aoqi@0 42 ITEM_Long = 4,
aoqi@0 43 ITEM_Null = 5,
aoqi@0 44 ITEM_UninitializedThis = 6,
aoqi@0 45 ITEM_Object = 7,
aoqi@0 46 ITEM_Uninitialized = 8,
aoqi@0 47 ITEM_Bogus = (uint)-1
aoqi@0 48 };
aoqi@0 49
aoqi@0 50 class ClassVerifier;
aoqi@0 51
aoqi@0 52 class VerificationType VALUE_OBJ_CLASS_SPEC {
aoqi@0 53 private:
aoqi@0 54 // Least significant bits of _handle are always 0, so we use these as
aoqi@0 55 // the indicator that the _handle is valid. Otherwise, the _data field
aoqi@0 56 // contains encoded data (as specified below). Should the VM change
aoqi@0 57 // and the lower bits on oops aren't 0, the assert in the constructor
aoqi@0 58 // will catch this and we'll have to add a descriminator tag to this
aoqi@0 59 // structure.
aoqi@0 60 union {
aoqi@0 61 Symbol* _sym;
aoqi@0 62 uintptr_t _data;
aoqi@0 63 } _u;
aoqi@0 64
aoqi@0 65 enum {
aoqi@0 66 // These rest are not found in classfiles, but used by the verifier
aoqi@0 67 ITEM_Boolean = 9, ITEM_Byte, ITEM_Short, ITEM_Char,
aoqi@0 68 ITEM_Long_2nd, ITEM_Double_2nd
aoqi@0 69 };
aoqi@0 70
aoqi@0 71 // Enum for the _data field
aoqi@0 72 enum {
aoqi@0 73 // Bottom two bits determine if the type is a reference, primitive,
aoqi@0 74 // uninitialized or a query-type.
aoqi@0 75 TypeMask = 0x00000003,
aoqi@0 76
aoqi@0 77 // Topmost types encoding
aoqi@0 78 Reference = 0x0, // _sym contains the name
aoqi@0 79 Primitive = 0x1, // see below for primitive list
aoqi@0 80 Uninitialized = 0x2, // 0x00ffff00 contains bci
aoqi@0 81 TypeQuery = 0x3, // Meta-types used for category testing
aoqi@0 82
aoqi@0 83 // Utility flags
aoqi@0 84 ReferenceFlag = 0x00, // For reference query types
aoqi@0 85 Category1Flag = 0x01, // One-word values
aoqi@0 86 Category2Flag = 0x02, // First word of a two-word value
aoqi@0 87 Category2_2ndFlag = 0x04, // Second word of a two-word value
aoqi@0 88
aoqi@0 89 // special reference values
aoqi@0 90 Null = 0x00000000, // A reference with a 0 sym is null
aoqi@0 91
aoqi@0 92 // Primitives categories (the second byte determines the category)
aoqi@0 93 Category1 = (Category1Flag << 1 * BitsPerByte) | Primitive,
aoqi@0 94 Category2 = (Category2Flag << 1 * BitsPerByte) | Primitive,
aoqi@0 95 Category2_2nd = (Category2_2ndFlag << 1 * BitsPerByte) | Primitive,
aoqi@0 96
aoqi@0 97 // Primitive values (type descriminator stored in most-signifcant bytes)
aoqi@0 98 Bogus = (ITEM_Bogus << 2 * BitsPerByte) | Category1,
aoqi@0 99 Boolean = (ITEM_Boolean << 2 * BitsPerByte) | Category1,
aoqi@0 100 Byte = (ITEM_Byte << 2 * BitsPerByte) | Category1,
aoqi@0 101 Short = (ITEM_Short << 2 * BitsPerByte) | Category1,
aoqi@0 102 Char = (ITEM_Char << 2 * BitsPerByte) | Category1,
aoqi@0 103 Integer = (ITEM_Integer << 2 * BitsPerByte) | Category1,
aoqi@0 104 Float = (ITEM_Float << 2 * BitsPerByte) | Category1,
aoqi@0 105 Long = (ITEM_Long << 2 * BitsPerByte) | Category2,
aoqi@0 106 Double = (ITEM_Double << 2 * BitsPerByte) | Category2,
aoqi@0 107 Long_2nd = (ITEM_Long_2nd << 2 * BitsPerByte) | Category2_2nd,
aoqi@0 108 Double_2nd = (ITEM_Double_2nd << 2 * BitsPerByte) | Category2_2nd,
aoqi@0 109
aoqi@0 110 // Used by Uninitialized (second and third bytes hold the bci)
aoqi@0 111 BciMask = 0xffff << 1 * BitsPerByte,
aoqi@0 112 BciForThis = ((u2)-1), // A bci of -1 is an Unintialized-This
aoqi@0 113
aoqi@0 114 // Query values
aoqi@0 115 ReferenceQuery = (ReferenceFlag << 1 * BitsPerByte) | TypeQuery,
aoqi@0 116 Category1Query = (Category1Flag << 1 * BitsPerByte) | TypeQuery,
aoqi@0 117 Category2Query = (Category2Flag << 1 * BitsPerByte) | TypeQuery,
aoqi@0 118 Category2_2ndQuery = (Category2_2ndFlag << 1 * BitsPerByte) | TypeQuery
aoqi@0 119 };
aoqi@0 120
aoqi@0 121 VerificationType(uintptr_t raw_data) {
aoqi@0 122 _u._data = raw_data;
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 public:
aoqi@0 126
aoqi@0 127 VerificationType() { *this = bogus_type(); }
aoqi@0 128
aoqi@0 129 // Create verification types
aoqi@0 130 static VerificationType bogus_type() { return VerificationType(Bogus); }
aoqi@0 131 static VerificationType top_type() { return bogus_type(); } // alias
aoqi@0 132 static VerificationType null_type() { return VerificationType(Null); }
aoqi@0 133 static VerificationType integer_type() { return VerificationType(Integer); }
aoqi@0 134 static VerificationType float_type() { return VerificationType(Float); }
aoqi@0 135 static VerificationType long_type() { return VerificationType(Long); }
aoqi@0 136 static VerificationType long2_type() { return VerificationType(Long_2nd); }
aoqi@0 137 static VerificationType double_type() { return VerificationType(Double); }
aoqi@0 138 static VerificationType boolean_type() { return VerificationType(Boolean); }
aoqi@0 139 static VerificationType byte_type() { return VerificationType(Byte); }
aoqi@0 140 static VerificationType char_type() { return VerificationType(Char); }
aoqi@0 141 static VerificationType short_type() { return VerificationType(Short); }
aoqi@0 142 static VerificationType double2_type()
aoqi@0 143 { return VerificationType(Double_2nd); }
aoqi@0 144
aoqi@0 145 // "check" types are used for queries. A "check" type is not assignable
aoqi@0 146 // to anything, but the specified types are assignable to a "check". For
aoqi@0 147 // example, any category1 primitive is assignable to category1_check and
aoqi@0 148 // any reference is assignable to reference_check.
aoqi@0 149 static VerificationType reference_check()
aoqi@0 150 { return VerificationType(ReferenceQuery); }
aoqi@0 151 static VerificationType category1_check()
aoqi@0 152 { return VerificationType(Category1Query); }
aoqi@0 153 static VerificationType category2_check()
aoqi@0 154 { return VerificationType(Category2Query); }
aoqi@0 155 static VerificationType category2_2nd_check()
aoqi@0 156 { return VerificationType(Category2_2ndQuery); }
aoqi@0 157
aoqi@0 158 // For reference types, store the actual Symbol
aoqi@0 159 static VerificationType reference_type(Symbol* sh) {
aoqi@0 160 assert(((uintptr_t)sh & 0x3) == 0, "Symbols must be aligned");
aoqi@0 161 // If the above assert fails in the future because oop* isn't aligned,
aoqi@0 162 // then this type encoding system will have to change to have a tag value
aoqi@0 163 // to descriminate between oops and primitives.
aoqi@0 164 return VerificationType((uintptr_t)sh);
aoqi@0 165 }
aoqi@0 166 static VerificationType uninitialized_type(u2 bci)
aoqi@0 167 { return VerificationType(bci << 1 * BitsPerByte | Uninitialized); }
aoqi@0 168 static VerificationType uninitialized_this_type()
aoqi@0 169 { return uninitialized_type(BciForThis); }
aoqi@0 170
aoqi@0 171 // Create based on u1 read from classfile
aoqi@0 172 static VerificationType from_tag(u1 tag);
aoqi@0 173
aoqi@0 174 bool is_bogus() const { return (_u._data == Bogus); }
aoqi@0 175 bool is_null() const { return (_u._data == Null); }
aoqi@0 176 bool is_boolean() const { return (_u._data == Boolean); }
aoqi@0 177 bool is_byte() const { return (_u._data == Byte); }
aoqi@0 178 bool is_char() const { return (_u._data == Char); }
aoqi@0 179 bool is_short() const { return (_u._data == Short); }
aoqi@0 180 bool is_integer() const { return (_u._data == Integer); }
aoqi@0 181 bool is_long() const { return (_u._data == Long); }
aoqi@0 182 bool is_float() const { return (_u._data == Float); }
aoqi@0 183 bool is_double() const { return (_u._data == Double); }
aoqi@0 184 bool is_long2() const { return (_u._data == Long_2nd); }
aoqi@0 185 bool is_double2() const { return (_u._data == Double_2nd); }
aoqi@0 186 bool is_reference() const { return ((_u._data & TypeMask) == Reference); }
aoqi@0 187 bool is_category1() const {
aoqi@0 188 // This should return true for all one-word types, which are category1
aoqi@0 189 // primitives, and references (including uninitialized refs). Though
aoqi@0 190 // the 'query' types should technically return 'false' here, if we
aoqi@0 191 // allow this to return true, we can perform the test using only
aoqi@0 192 // 2 operations rather than 8 (3 masks, 3 compares and 2 logical 'ands').
aoqi@0 193 // Since noone should call this on a query type anyway, this is ok.
aoqi@0 194 assert(!is_check(), "Must not be a check type (wrong value returned)");
aoqi@0 195 return ((_u._data & Category1) != Primitive);
aoqi@0 196 // should only return false if it's a primitive, and the category1 flag
aoqi@0 197 // is not set.
aoqi@0 198 }
aoqi@0 199 bool is_category2() const { return ((_u._data & Category2) == Category2); }
aoqi@0 200 bool is_category2_2nd() const {
aoqi@0 201 return ((_u._data & Category2_2nd) == Category2_2nd);
aoqi@0 202 }
aoqi@0 203 bool is_reference_check() const { return _u._data == ReferenceQuery; }
aoqi@0 204 bool is_category1_check() const { return _u._data == Category1Query; }
aoqi@0 205 bool is_category2_check() const { return _u._data == Category2Query; }
aoqi@0 206 bool is_category2_2nd_check() const { return _u._data == Category2_2ndQuery; }
aoqi@0 207 bool is_check() const { return (_u._data & TypeQuery) == TypeQuery; }
aoqi@0 208
aoqi@0 209 bool is_x_array(char sig) const {
aoqi@0 210 return is_null() || (is_array() && (name()->byte_at(1) == sig));
aoqi@0 211 }
aoqi@0 212 bool is_int_array() const { return is_x_array('I'); }
aoqi@0 213 bool is_byte_array() const { return is_x_array('B'); }
aoqi@0 214 bool is_bool_array() const { return is_x_array('Z'); }
aoqi@0 215 bool is_char_array() const { return is_x_array('C'); }
aoqi@0 216 bool is_short_array() const { return is_x_array('S'); }
aoqi@0 217 bool is_long_array() const { return is_x_array('J'); }
aoqi@0 218 bool is_float_array() const { return is_x_array('F'); }
aoqi@0 219 bool is_double_array() const { return is_x_array('D'); }
aoqi@0 220 bool is_object_array() const { return is_x_array('L'); }
aoqi@0 221 bool is_array_array() const { return is_x_array('['); }
aoqi@0 222 bool is_reference_array() const
aoqi@0 223 { return is_object_array() || is_array_array(); }
aoqi@0 224 bool is_object() const
aoqi@0 225 { return (is_reference() && !is_null() && name()->utf8_length() >= 1 &&
aoqi@0 226 name()->byte_at(0) != '['); }
aoqi@0 227 bool is_array() const
aoqi@0 228 { return (is_reference() && !is_null() && name()->utf8_length() >= 2 &&
aoqi@0 229 name()->byte_at(0) == '['); }
aoqi@0 230 bool is_uninitialized() const
aoqi@0 231 { return ((_u._data & Uninitialized) == Uninitialized); }
aoqi@0 232 bool is_uninitialized_this() const
aoqi@0 233 { return is_uninitialized() && bci() == BciForThis; }
aoqi@0 234
aoqi@0 235 VerificationType to_category2_2nd() const {
aoqi@0 236 assert(is_category2(), "Must be a double word");
aoqi@0 237 return VerificationType(is_long() ? Long_2nd : Double_2nd);
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 u2 bci() const {
aoqi@0 241 assert(is_uninitialized(), "Must be uninitialized type");
aoqi@0 242 return ((_u._data & BciMask) >> 1 * BitsPerByte);
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 Symbol* name() const {
aoqi@0 246 assert(is_reference() && !is_null(), "Must be a non-null reference");
aoqi@0 247 return _u._sym;
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 bool equals(const VerificationType& t) const {
aoqi@0 251 return (_u._data == t._u._data ||
aoqi@0 252 (is_reference() && t.is_reference() && !is_null() && !t.is_null() &&
aoqi@0 253 name() == t.name()));
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 bool operator ==(const VerificationType& t) const {
aoqi@0 257 return equals(t);
aoqi@0 258 }
aoqi@0 259
aoqi@0 260 bool operator !=(const VerificationType& t) const {
aoqi@0 261 return !equals(t);
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 // The whole point of this type system - check to see if one type
aoqi@0 265 // is assignable to another. Returns true if one can assign 'from' to
aoqi@0 266 // this.
aoqi@0 267 bool is_assignable_from(
aoqi@0 268 const VerificationType& from, ClassVerifier* context,
aoqi@0 269 bool from_field_is_protected, TRAPS) const {
aoqi@0 270 if (equals(from) || is_bogus()) {
aoqi@0 271 return true;
aoqi@0 272 } else {
aoqi@0 273 switch(_u._data) {
aoqi@0 274 case Category1Query:
aoqi@0 275 return from.is_category1();
aoqi@0 276 case Category2Query:
aoqi@0 277 return from.is_category2();
aoqi@0 278 case Category2_2ndQuery:
aoqi@0 279 return from.is_category2_2nd();
aoqi@0 280 case ReferenceQuery:
aoqi@0 281 return from.is_reference() || from.is_uninitialized();
aoqi@0 282 case Boolean:
aoqi@0 283 case Byte:
aoqi@0 284 case Char:
aoqi@0 285 case Short:
aoqi@0 286 // An int can be assigned to boolean, byte, char or short values.
aoqi@0 287 return from.is_integer();
aoqi@0 288 default:
aoqi@0 289 if (is_reference() && from.is_reference()) {
aoqi@0 290 return is_reference_assignable_from(from, context,
aoqi@0 291 from_field_is_protected,
aoqi@0 292 CHECK_false);
aoqi@0 293 } else {
aoqi@0 294 return false;
aoqi@0 295 }
aoqi@0 296 }
aoqi@0 297 }
aoqi@0 298 }
aoqi@0 299
aoqi@0 300 VerificationType get_component(ClassVerifier* context, TRAPS) const;
aoqi@0 301
aoqi@0 302 int dimensions() const {
aoqi@0 303 assert(is_array(), "Must be an array");
aoqi@0 304 int index = 0;
aoqi@0 305 while (name()->byte_at(index) == '[') index++;
aoqi@0 306 return index;
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 void print_on(outputStream* st) const;
aoqi@0 310
aoqi@0 311 private:
aoqi@0 312
aoqi@0 313 bool is_reference_assignable_from(
aoqi@0 314 const VerificationType&, ClassVerifier*, bool from_field_is_protected,
aoqi@0 315 TRAPS) const;
aoqi@0 316 };
aoqi@0 317
aoqi@0 318 #endif // SHARE_VM_CLASSFILE_VERIFICATIONTYPE_HPP

mercurial