1 /* 2 * Copyright (c) 2003, 2012, 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 #ifndef SHARE_VM_RUNTIME_JFIELDIDWORKAROUND_HPP 26 #define SHARE_VM_RUNTIME_JFIELDIDWORKAROUND_HPP 27 28 class jfieldIDWorkaround: AllStatic { 29 // This workaround is because JVMTI doesn't have distinct entry points 30 // for methods that use static jfieldIDs and instance jfieldIDs. 31 // The workaround is to steal a low-order bit: 32 // a 1 means the jfieldID is an instance jfieldID, 33 // and the rest of the word is the offset of the field. 34 // a 0 means the jfieldID is a static jfieldID, 35 // and the rest of the word is the JNIid*. 36 // 37 // Another low-order bit is used to mark if an instance field 38 // is accompanied by an indication of which class it applies to. 39 // 40 // Bit-format of a jfieldID (most significant first): 41 // address:29 final=0:1 instance=0:1 checked=0:1 42 // offset:29 final=0:1 instance=1:1 checked=0:1 43 // klass:22 offset:7 final=1:1 instance=1:1 checked=1:1 44 // 45 // If the offset does not fit in 7 bits, or if the fieldID is 46 // not checked, then the checked bit is zero and the rest of 47 // the word (30 bits) contains only the offset. 48 // 49 public: 50 enum { 51 checked_bits = 1, 52 instance_bits = 1, 53 final_bits = 1, 54 address_bits = BitsPerWord - checked_bits - instance_bits - final_bits, 55 56 large_offset_bits = address_bits, // unioned with address 57 small_offset_bits = 7, 58 klass_bits = address_bits - small_offset_bits, 59 60 checked_shift = 0, 61 instance_shift = checked_shift + checked_bits, 62 final_shift = instance_shift + instance_bits, 63 address_shift = final_shift + final_bits, 64 65 offset_shift = address_shift, // unioned with address 66 klass_shift = offset_shift + small_offset_bits, 67 68 checked_mask_in_place = right_n_bits(checked_bits) << checked_shift, 69 instance_mask_in_place = right_n_bits(instance_bits) << instance_shift, 70 final_mask_in_place = right_n_bits(final_bits) << final_shift, 71 #ifndef _WIN64 72 large_offset_mask = right_n_bits(large_offset_bits), 73 small_offset_mask = right_n_bits(small_offset_bits), 74 klass_mask = right_n_bits(klass_bits) 75 #endif 76 }; 77 78 #ifdef _WIN64 79 // These values are too big for Win64 80 const static uintptr_t large_offset_mask = right_n_bits(large_offset_bits); 81 const static uintptr_t small_offset_mask = right_n_bits(small_offset_bits); 82 const static uintptr_t klass_mask = right_n_bits(klass_bits); 83 #endif 84 private: 85 // helper routines: 86 static bool is_checked_jfieldID(jfieldID id) { 87 uintptr_t as_uint = (uintptr_t) id; 88 return ((as_uint & checked_mask_in_place) != 0); 89 } 90 static intptr_t raw_instance_offset(jfieldID id) { 91 uintptr_t result = (uintptr_t) id >> address_shift; 92 if (VerifyJNIFields && is_checked_jfieldID(id)) { 93 result &= small_offset_mask; // cut off the hash bits 94 } 95 return (intptr_t)result; 96 } 97 static intptr_t encode_klass_hash(Klass* k, intptr_t offset); 98 static bool klass_hash_ok(Klass* k, jfieldID id); 99 static void verify_instance_jfieldID(Klass* k, jfieldID id); 100 101 public: 102 static bool is_valid_jfieldID(Klass* k, jfieldID id); 103 104 static bool is_instance_jfieldID(jfieldID id) { 105 uintptr_t as_uint = (uintptr_t) id; 106 return ((as_uint & instance_mask_in_place) != 0); 107 } 108 static bool is_static_jfieldID(jfieldID id) { 109 uintptr_t as_uint = (uintptr_t) id; 110 return ((as_uint & instance_mask_in_place) == 0); 111 } 112 static bool is_final_jfieldID(jfieldID id) { 113 uintptr_t as_uint = (uintptr_t) id; 114 return ((as_uint & final_mask_in_place) != 0); 115 } 116 117 static jfieldID to_instance_jfieldID(Klass* k, int offset, bool is_final) { 118 intptr_t as_uint = ((offset & large_offset_mask) << offset_shift) | instance_mask_in_place; 119 if (is_final) as_uint |= final_mask_in_place; 120 if (VerifyJNIFields) { 121 as_uint |= encode_klass_hash(k, offset); 122 } 123 jfieldID result = (jfieldID) as_uint; 124 #ifndef ASSERT 125 // always verify in debug mode; switchable in anything else 126 if (VerifyJNIFields) 127 #endif // ASSERT 128 { 129 verify_instance_jfieldID(k, result); 130 } 131 assert(raw_instance_offset(result) == (offset & large_offset_mask), "extract right offset"); 132 return result; 133 } 134 135 static intptr_t from_instance_jfieldID(Klass* k, jfieldID id) { 136 #ifndef ASSERT 137 // always verify in debug mode; switchable in anything else 138 if (VerifyJNIFields) 139 #endif // ASSERT 140 { 141 verify_instance_jfieldID(k, id); 142 } 143 return raw_instance_offset(id); 144 } 145 146 static jfieldID to_static_jfieldID(JNIid* id) { 147 assert(id->is_static_field_id(), "from_JNIid, but not static field id"); 148 jfieldID result = (jfieldID) id; 149 assert(from_static_jfieldID(result) == id, "must produce the same static id"); 150 return result; 151 } 152 153 static JNIid* from_static_jfieldID(jfieldID id) { 154 assert(jfieldIDWorkaround::is_static_jfieldID(id), 155 "to_JNIid, but not static jfieldID"); 156 JNIid* result = (JNIid*) id; 157 assert(result->is_static_field_id(), "to_JNIid, but not static field id"); 158 return result; 159 } 160 161 static jfieldID to_jfieldID(instanceKlassHandle k, int offset, bool is_static, bool is_final) { 162 if (is_static) { 163 JNIid *id = k->jni_id_for(offset); 164 debug_only(id->set_is_static_field_id()); 165 return jfieldIDWorkaround::to_static_jfieldID(id); 166 } else { 167 return jfieldIDWorkaround::to_instance_jfieldID(k(), offset, is_final); 168 } 169 } 170 }; 171 172 #endif // SHARE_VM_RUNTIME_JFIELDIDWORKAROUND_HPP