1 /*
  2  * Copyright (c) 2018, 2023, 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_RUNTIME_JNIHANDLES_INLINE_HPP
 26 #define SHARE_RUNTIME_JNIHANDLES_INLINE_HPP
 27 
 28 #include "runtime/jniHandles.hpp"
 29 
 30 #include "oops/access.inline.hpp"
 31 #include "oops/oop.hpp"
 32 #include "utilities/debug.hpp"
 33 #include "utilities/globalDefinitions.hpp"
 34 
 35 inline bool JNIHandles::is_tagged_with(jobject handle, TypeTag tag) {
 36   return (reinterpret_cast<uintptr_t>(handle) & tag_mask) == tag;
 37 }
 38 
 39 inline bool JNIHandles::is_local_tagged(jobject handle) {
 40   return is_tagged_with(handle, TypeTag::local);
 41 }
 42 
 43 inline bool JNIHandles::is_weak_global_tagged(jobject handle) {
 44   return is_tagged_with(handle, TypeTag::weak_global);
 45 }
 46 
 47 inline bool JNIHandles::is_global_tagged(jobject handle) {
 48   return is_tagged_with(handle, TypeTag::global);
 49 }
 50 
 51 inline oop* JNIHandles::local_ptr(jobject handle) {
 52   assert(is_local_tagged(handle), "precondition");
 53   STATIC_ASSERT(TypeTag::local == 0);
 54   return reinterpret_cast<oop*>(handle);
 55 }
 56 
 57 inline oop* JNIHandles::global_ptr(jobject handle) {
 58   assert(is_global_tagged(handle), "precondition");
 59   char* ptr = reinterpret_cast<char*>(handle) - TypeTag::global;
 60   return reinterpret_cast<oop*>(ptr);
 61 }
 62 
 63 inline oop* JNIHandles::weak_global_ptr(jweak handle) {
 64   assert(is_weak_global_tagged(handle), "precondition");
 65   char* ptr = reinterpret_cast<char*>(handle) - TypeTag::weak_global;
 66   return reinterpret_cast<oop*>(ptr);
 67 }
 68 
 69 // external_guard is true if called from resolve_external_guard.
 70 template <DecoratorSet decorators, bool external_guard>
 71 inline oop JNIHandles::resolve_impl(jobject handle) {
 72   assert(handle != nullptr, "precondition");
 73   assert(!current_thread_in_native(), "must not be in native");
 74   oop result;
 75   if (is_weak_global_tagged(handle)) {       // Unlikely
 76     result = NativeAccess<ON_PHANTOM_OOP_REF|decorators>::oop_load(weak_global_ptr(handle));
 77   } else if (is_global_tagged(handle)) {
 78     result = NativeAccess<decorators>::oop_load(global_ptr(handle));
 79     // Construction of jobjects canonicalize a null value into a null
 80     // jobject, so for non-jweak the pointee should never be null.
 81     assert(external_guard || result != nullptr, "Invalid JNI handle");
 82   } else {
 83     result = *local_ptr(handle);
 84     // Construction of jobjects canonicalize a null value into a null
 85     // jobject, so for non-jweak the pointee should never be null.
 86     assert(external_guard || result != nullptr, "Invalid JNI handle");
 87   }
 88   return result;
 89 }
 90 
 91 inline oop JNIHandles::resolve(jobject handle) {
 92   oop result = nullptr;
 93   if (handle != nullptr) {
 94     result = resolve_impl<DECORATORS_NONE, false /* external_guard */>(handle);
 95   }
 96   return result;
 97 }
 98 
 99 inline oop JNIHandles::resolve_no_keepalive(jobject handle) {
100   oop result = nullptr;
101   if (handle != nullptr) {
102     result = resolve_impl<AS_NO_KEEPALIVE, false /* external_guard */>(handle);
103   }
104   return result;
105 }
106 
107 inline oop JNIHandles::resolve_non_null(jobject handle) {
108   assert(handle != nullptr, "JNI handle should not be null");
109   oop result = resolve_impl<DECORATORS_NONE, false /* external_guard */>(handle);
110   assert(result != nullptr, "null read from jni handle");
111   return result;
112 }
113 
114 inline void JNIHandles::destroy_local(jobject handle) {
115   if (handle != nullptr) {
116     *local_ptr(handle) = nullptr;
117   }
118 }
119 
120 #endif // SHARE_RUNTIME_JNIHANDLES_INLINE_HPP