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