src/share/vm/classfile/verificationType.hpp

Thu, 27 May 2010 19:08:38 -0700

author
trims
date
Thu, 27 May 2010 19:08:38 -0700
changeset 1907
c18cbe5936b8
parent 435
a61af66fc99e
child 2314
f95d63e2154a
permissions
-rw-r--r--

6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair

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

mercurial