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