annotate src/hotspot/share/classfile/verificationType.hpp @ 54526:ee29b516a36a

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