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