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 #include "precompiled.hpp"
26 #include "gc/shared/collectedHeap.hpp"
27 #include "gc/shared/oopStorage.inline.hpp"
28 #include "gc/shared/oopStorageSet.hpp"
29 #include "logging/log.hpp"
30 #include "memory/iterator.hpp"
31 #include "memory/universe.hpp"
32 #include "oops/access.inline.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "runtime/handles.inline.hpp"
35 #include "runtime/javaThread.inline.hpp"
36 #include "runtime/jniHandles.inline.hpp"
37 #include "runtime/mutexLocker.hpp"
38 #include "utilities/align.hpp"
39 #include "utilities/debug.hpp"
40
41 OopStorage* JNIHandles::global_handles() {
42 return _global_handles;
43 }
44
45 OopStorage* JNIHandles::weak_global_handles() {
46 return _weak_global_handles;
47 }
48
49 // Serviceability agent support.
50 OopStorage* JNIHandles::_global_handles = nullptr;
51 OopStorage* JNIHandles::_weak_global_handles = nullptr;
52
53 void jni_handles_init() {
54 JNIHandles::_global_handles = OopStorageSet::create_strong("JNI Global", mtInternal);
270 guarantee(oopDesc::is_oop_or_null(RawAccess<>::oop_load(root)), "Invalid oop");
271 }
272 virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
273 };
274
275 void JNIHandles::verify() {
276 VerifyJNIHandles verify_handle;
277
278 oops_do(&verify_handle);
279 weak_oops_do(&verify_handle);
280 }
281
282 // This method is implemented here to avoid circular includes between
283 // jniHandles.hpp and thread.hpp.
284 bool JNIHandles::current_thread_in_native() {
285 Thread* thread = Thread::current();
286 return (thread->is_Java_thread() &&
287 JavaThread::cast(thread)->thread_state() == _thread_in_native);
288 }
289
290 int JNIHandleBlock::_blocks_allocated = 0;
291
292 static inline bool is_tagged_free_list(uintptr_t value) {
293 return (value & 1u) != 0;
294 }
295
296 static inline uintptr_t tag_free_list(uintptr_t value) {
297 return value | 1u;
298 }
299
300 static inline uintptr_t untag_free_list(uintptr_t value) {
301 return value & ~(uintptr_t)1u;
302 }
303
304 // There is a freelist of handles running through the JNIHandleBlock
305 // with a tagged next pointer, distinguishing these next pointers from
306 // oops. The freelist handling currently relies on the size of oops
307 // being the same as a native pointer. If this ever changes, then
308 // this freelist handling must change too.
309 STATIC_ASSERT(sizeof(oop) == sizeof(uintptr_t));
|
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 #include "precompiled.hpp"
26 #include "classfile/vmSymbols.hpp"
27 #include "gc/shared/collectedHeap.hpp"
28 #include "gc/shared/oopStorage.inline.hpp"
29 #include "gc/shared/oopStorageSet.hpp"
30 #include "logging/log.hpp"
31 #include "memory/iterator.hpp"
32 #include "memory/universe.hpp"
33 #include "oops/access.inline.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "runtime/handles.inline.hpp"
36 #include "runtime/javaCalls.hpp"
37 #include "runtime/javaThread.inline.hpp"
38 #include "runtime/jniHandles.inline.hpp"
39 #include "runtime/mutexLocker.hpp"
40 #include "utilities/align.hpp"
41 #include "utilities/debug.hpp"
42
43 OopStorage* JNIHandles::global_handles() {
44 return _global_handles;
45 }
46
47 OopStorage* JNIHandles::weak_global_handles() {
48 return _weak_global_handles;
49 }
50
51 // Serviceability agent support.
52 OopStorage* JNIHandles::_global_handles = nullptr;
53 OopStorage* JNIHandles::_weak_global_handles = nullptr;
54
55 void jni_handles_init() {
56 JNIHandles::_global_handles = OopStorageSet::create_strong("JNI Global", mtInternal);
272 guarantee(oopDesc::is_oop_or_null(RawAccess<>::oop_load(root)), "Invalid oop");
273 }
274 virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
275 };
276
277 void JNIHandles::verify() {
278 VerifyJNIHandles verify_handle;
279
280 oops_do(&verify_handle);
281 weak_oops_do(&verify_handle);
282 }
283
284 // This method is implemented here to avoid circular includes between
285 // jniHandles.hpp and thread.hpp.
286 bool JNIHandles::current_thread_in_native() {
287 Thread* thread = Thread::current();
288 return (thread->is_Java_thread() &&
289 JavaThread::cast(thread)->thread_state() == _thread_in_native);
290 }
291
292 bool JNIHandles::is_same_object(jobject handle1, jobject handle2) {
293 oop obj1 = resolve_no_keepalive(handle1);
294 oop obj2 = resolve_no_keepalive(handle2);
295
296 bool ret = obj1 == obj2;
297
298 if (EnableValhalla) {
299 if (!ret && obj1 != nullptr && obj2 != nullptr && obj1->klass() == obj2->klass() && obj1->klass()->is_inline_klass()) {
300 // The two references are different, they are not null and they are both inline types,
301 // a full substitutability test is required, calling ValueObjectMethods.isSubstitutable()
302 // (similarly to InterpreterRuntime::is_substitutable)
303 JavaThread* THREAD = JavaThread::current();
304 Handle ha(THREAD, obj1);
305 Handle hb(THREAD, obj2);
306 JavaValue result(T_BOOLEAN);
307 JavaCallArguments args;
308 args.push_oop(ha);
309 args.push_oop(hb);
310 methodHandle method(THREAD, Universe::is_substitutable_method());
311 JavaCalls::call(&result, method, &args, THREAD);
312 if (HAS_PENDING_EXCEPTION) {
313 // Something really bad happened because isSubstitutable() should not throw exceptions
314 // If it is an error, just let it propagate
315 // If it is an exception, wrap it into an InternalError
316 if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
317 Handle e(THREAD, PENDING_EXCEPTION);
318 CLEAR_PENDING_EXCEPTION;
319 THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in substitutability test", e, false);
320 }
321 }
322 ret = result.get_jboolean();
323 }
324 }
325
326 return ret;
327 }
328
329
330 int JNIHandleBlock::_blocks_allocated = 0;
331
332 static inline bool is_tagged_free_list(uintptr_t value) {
333 return (value & 1u) != 0;
334 }
335
336 static inline uintptr_t tag_free_list(uintptr_t value) {
337 return value | 1u;
338 }
339
340 static inline uintptr_t untag_free_list(uintptr_t value) {
341 return value & ~(uintptr_t)1u;
342 }
343
344 // There is a freelist of handles running through the JNIHandleBlock
345 // with a tagged next pointer, distinguishing these next pointers from
346 // oops. The freelist handling currently relies on the size of oops
347 // being the same as a native pointer. If this ever changes, then
348 // this freelist handling must change too.
349 STATIC_ASSERT(sizeof(oop) == sizeof(uintptr_t));
|