duke@435: /* coleenp@4037: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_RUNTIME_JFIELDIDWORKAROUND_HPP stefank@2314: #define SHARE_VM_RUNTIME_JFIELDIDWORKAROUND_HPP stefank@2314: duke@435: class jfieldIDWorkaround: AllStatic { duke@435: // This workaround is because JVMTI doesn't have distinct entry points duke@435: // for methods that use static jfieldIDs and instance jfieldIDs. duke@435: // The workaround is to steal a low-order bit: duke@435: // a 1 means the jfieldID is an instance jfieldID, duke@435: // and the rest of the word is the offset of the field. duke@435: // a 0 means the jfieldID is a static jfieldID, duke@435: // and the rest of the word is the JNIid*. duke@435: // duke@435: // Another low-order bit is used to mark if an instance field duke@435: // is accompanied by an indication of which class it applies to. duke@435: // duke@435: // Bit-format of a jfieldID (most significant first): duke@435: // address:30 instance=0:1 checked=0:1 duke@435: // offset:30 instance=1:1 checked=0:1 duke@435: // klass:23 offset:7 instance=1:1 checked=1:1 duke@435: // duke@435: // If the offset does not fit in 7 bits, or if the fieldID is duke@435: // not checked, then the checked bit is zero and the rest of duke@435: // the word (30 bits) contains only the offset. duke@435: // duke@435: private: duke@435: enum { duke@435: checked_bits = 1, duke@435: instance_bits = 1, duke@435: address_bits = BitsPerWord - checked_bits - instance_bits, duke@435: duke@435: large_offset_bits = address_bits, // unioned with address duke@435: small_offset_bits = 7, duke@435: klass_bits = address_bits - small_offset_bits, duke@435: duke@435: checked_shift = 0, duke@435: instance_shift = checked_shift + checked_bits, duke@435: address_shift = instance_shift + instance_bits, duke@435: duke@435: offset_shift = address_shift, // unioned with address duke@435: klass_shift = offset_shift + small_offset_bits, duke@435: duke@435: checked_mask_in_place = right_n_bits(checked_bits) << checked_shift, duke@435: instance_mask_in_place = right_n_bits(instance_bits) << instance_shift, duke@435: #ifndef _WIN64 duke@435: large_offset_mask = right_n_bits(large_offset_bits), duke@435: small_offset_mask = right_n_bits(small_offset_bits), duke@435: klass_mask = right_n_bits(klass_bits) duke@435: #endif duke@435: }; duke@435: duke@435: #ifdef _WIN64 duke@435: // These values are too big for Win64 duke@435: const static uintptr_t large_offset_mask = right_n_bits(large_offset_bits); duke@435: const static uintptr_t small_offset_mask = right_n_bits(small_offset_bits); duke@435: const static uintptr_t klass_mask = right_n_bits(klass_bits); duke@435: #endif duke@435: duke@435: // helper routines: duke@435: static bool is_checked_jfieldID(jfieldID id) { duke@435: uintptr_t as_uint = (uintptr_t) id; duke@435: return ((as_uint & checked_mask_in_place) != 0); duke@435: } duke@435: static intptr_t raw_instance_offset(jfieldID id) { duke@435: uintptr_t result = (uintptr_t) id >> address_shift; duke@435: if (VerifyJNIFields && is_checked_jfieldID(id)) { duke@435: result &= small_offset_mask; // cut off the hash bits duke@435: } duke@435: return (intptr_t)result; duke@435: } coleenp@4037: static intptr_t encode_klass_hash(Klass* k, intptr_t offset); coleenp@4037: static bool klass_hash_ok(Klass* k, jfieldID id); coleenp@4037: static void verify_instance_jfieldID(Klass* k, jfieldID id); duke@435: duke@435: public: coleenp@4037: static bool is_valid_jfieldID(Klass* k, jfieldID id); duke@435: coleenp@4037: static bool is_instance_jfieldID(Klass* k, jfieldID id) { duke@435: uintptr_t as_uint = (uintptr_t) id; duke@435: return ((as_uint & instance_mask_in_place) != 0); duke@435: } duke@435: static bool is_static_jfieldID(jfieldID id) { duke@435: uintptr_t as_uint = (uintptr_t) id; duke@435: return ((as_uint & instance_mask_in_place) == 0); duke@435: } duke@435: coleenp@4037: static jfieldID to_instance_jfieldID(Klass* k, int offset) { duke@435: intptr_t as_uint = ((offset & large_offset_mask) << offset_shift) | instance_mask_in_place; duke@435: if (VerifyJNIFields) { duke@435: as_uint |= encode_klass_hash(k, offset); duke@435: } duke@435: jfieldID result = (jfieldID) as_uint; duke@435: #ifndef ASSERT duke@435: // always verify in debug mode; switchable in anything else duke@435: if (VerifyJNIFields) duke@435: #endif // ASSERT duke@435: { duke@435: verify_instance_jfieldID(k, result); duke@435: } duke@435: assert(raw_instance_offset(result) == (offset & large_offset_mask), "extract right offset"); duke@435: return result; duke@435: } duke@435: coleenp@4037: static intptr_t from_instance_jfieldID(Klass* k, jfieldID id) { duke@435: #ifndef ASSERT duke@435: // always verify in debug mode; switchable in anything else duke@435: if (VerifyJNIFields) duke@435: #endif // ASSERT duke@435: { duke@435: verify_instance_jfieldID(k, id); duke@435: } duke@435: return raw_instance_offset(id); duke@435: } duke@435: duke@435: static jfieldID to_static_jfieldID(JNIid* id) { duke@435: assert(id->is_static_field_id(), "from_JNIid, but not static field id"); duke@435: jfieldID result = (jfieldID) id; duke@435: assert(from_static_jfieldID(result) == id, "must produce the same static id"); duke@435: return result; duke@435: } duke@435: duke@435: static JNIid* from_static_jfieldID(jfieldID id) { duke@435: assert(jfieldIDWorkaround::is_static_jfieldID(id), duke@435: "to_JNIid, but not static jfieldID"); duke@435: JNIid* result = (JNIid*) id; duke@435: assert(result->is_static_field_id(), "to_JNIid, but not static field id"); duke@435: return result; duke@435: } duke@435: duke@435: static jfieldID to_jfieldID(instanceKlassHandle k, int offset, bool is_static) { duke@435: if (is_static) { duke@435: JNIid *id = k->jni_id_for(offset); duke@435: debug_only(id->set_is_static_field_id()); duke@435: return jfieldIDWorkaround::to_static_jfieldID(id); duke@435: } else { duke@435: return jfieldIDWorkaround::to_instance_jfieldID(k(), offset); duke@435: } duke@435: } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_RUNTIME_JFIELDIDWORKAROUND_HPP