Path: blob/master/src/hotspot/share/runtime/jfieldIDWorkaround.hpp
40951 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_RUNTIME_JFIELDIDWORKAROUND_HPP25#define SHARE_RUNTIME_JFIELDIDWORKAROUND_HPP2627class jfieldIDWorkaround: AllStatic {28// This workaround is because JVMTI doesn't have distinct entry points29// for methods that use static jfieldIDs and instance jfieldIDs.30// The workaround is to steal a low-order bit:31// a 1 means the jfieldID is an instance jfieldID,32// and the rest of the word is the offset of the field.33// a 0 means the jfieldID is a static jfieldID,34// and the rest of the word is the JNIid*.35//36// Another low-order bit is used to mark if an instance field37// is accompanied by an indication of which class it applies to.38//39// Bit-format of a jfieldID (most significant first):40// address:30 instance=0:1 checked=0:141// offset:30 instance=1:1 checked=0:142// klass:23 offset:7 instance=1:1 checked=1:143//44// If the offset does not fit in 7 bits, or if the fieldID is45// not checked, then the checked bit is zero and the rest of46// the word (30 bits) contains only the offset.47//48private:49enum {50checked_bits = 1,51instance_bits = 1,52address_bits = BitsPerWord - checked_bits - instance_bits,5354large_offset_bits = address_bits, // unioned with address55small_offset_bits = 7,56klass_bits = address_bits - small_offset_bits,5758checked_shift = 0,59instance_shift = checked_shift + checked_bits,60address_shift = instance_shift + instance_bits,6162offset_shift = address_shift, // unioned with address63klass_shift = offset_shift + small_offset_bits,6465checked_mask_in_place = right_n_bits(checked_bits) << checked_shift,66instance_mask_in_place = right_n_bits(instance_bits) << instance_shift,67#ifndef _WIN6468large_offset_mask = right_n_bits(large_offset_bits),69small_offset_mask = right_n_bits(small_offset_bits),70klass_mask = right_n_bits(klass_bits)71#endif72};7374#ifdef _WIN6475// These values are too big for Win6476const static uintptr_t large_offset_mask = right_n_bits(large_offset_bits);77const static uintptr_t small_offset_mask = right_n_bits(small_offset_bits);78const static uintptr_t klass_mask = right_n_bits(klass_bits);79#endif8081// helper routines:82static bool is_checked_jfieldID(jfieldID id) {83uintptr_t as_uint = (uintptr_t) id;84return ((as_uint & checked_mask_in_place) != 0);85}86static intptr_t raw_instance_offset(jfieldID id) {87uintptr_t result = (uintptr_t) id >> address_shift;88if (VerifyJNIFields && is_checked_jfieldID(id)) {89result &= small_offset_mask; // cut off the hash bits90}91return (intptr_t)result;92}93static intptr_t encode_klass_hash(Klass* k, intptr_t offset);94static bool klass_hash_ok(Klass* k, jfieldID id);95static void verify_instance_jfieldID(Klass* k, jfieldID id);9697public:98static bool is_valid_jfieldID(Klass* k, jfieldID id);99100static bool is_instance_jfieldID(Klass* k, jfieldID id) {101uintptr_t as_uint = (uintptr_t) id;102return ((as_uint & instance_mask_in_place) != 0);103}104static bool is_static_jfieldID(jfieldID id) {105uintptr_t as_uint = (uintptr_t) id;106return ((as_uint & instance_mask_in_place) == 0);107}108109static jfieldID to_instance_jfieldID(Klass* k, int offset) {110intptr_t as_uint = ((offset & large_offset_mask) << offset_shift) | instance_mask_in_place;111if (VerifyJNIFields) {112as_uint |= encode_klass_hash(k, offset);113}114jfieldID result = (jfieldID) as_uint;115#ifndef ASSERT116// always verify in debug mode; switchable in anything else117if (VerifyJNIFields)118#endif // ASSERT119{120verify_instance_jfieldID(k, result);121}122assert(raw_instance_offset(result) == (offset & large_offset_mask), "extract right offset");123return result;124}125126static intptr_t from_instance_jfieldID(Klass* k, jfieldID id) {127#ifndef ASSERT128// always verify in debug mode; switchable in anything else129if (VerifyJNIFields)130#endif // ASSERT131{132verify_instance_jfieldID(k, id);133}134return raw_instance_offset(id);135}136137static jfieldID to_static_jfieldID(JNIid* id) {138assert(id->is_static_field_id(), "from_JNIid, but not static field id");139jfieldID result = (jfieldID) id;140assert(from_static_jfieldID(result) == id, "must produce the same static id");141return result;142}143144static JNIid* from_static_jfieldID(jfieldID id) {145assert(jfieldIDWorkaround::is_static_jfieldID(id),146"to_JNIid, but not static jfieldID");147JNIid* result = (JNIid*) id;148assert(result->is_static_field_id(), "to_JNIid, but not static field id");149return result;150}151152static jfieldID to_jfieldID(InstanceKlass* k, int offset, bool is_static) {153if (is_static) {154JNIid *id = k->jni_id_for(offset);155debug_only(id->set_is_static_field_id());156return jfieldIDWorkaround::to_static_jfieldID(id);157} else {158return jfieldIDWorkaround::to_instance_jfieldID(k, offset);159}160}161};162163#endif // SHARE_RUNTIME_JFIELDIDWORKAROUND_HPP164165166