1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ART_RUNTIME_JNI_JNI_INTERNAL_H_
18 #define ART_RUNTIME_JNI_JNI_INTERNAL_H_
19
20 #include <jni.h>
21 #include <iosfwd>
22
23 #include "base/locks.h"
24 #include "base/macros.h"
25 #include "reflective_handle.h"
26 #include "reflective_handle_scope.h"
27 #include "runtime.h"
28 #include "thread.h"
29
30 namespace art {
31
32 class ArtField;
33 class ArtMethod;
34 class ScopedObjectAccess;
35
36 const JNINativeInterface* GetJniNativeInterface();
37 const JNINativeInterface* GetRuntimeShutdownNativeInterface();
38
39 int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause);
40
41 // Enables native stack checking for field and method resolutions via JNI. This should be called
42 // during runtime initialization after libjavacore and libopenjdk have been dlopen()'ed.
43 void JniInitializeNativeCallerCheck();
44
45 // Removes native stack checking state.
46 void JniShutdownNativeCallerCheck();
47
48 // Finds the method using JNI semantics and initializes any classes. Does not encode the method in a
49 // JNI id
50 ArtMethod* FindMethodJNI(const ScopedObjectAccess& soa,
51 jclass java_class,
52 const char* name,
53 const char* sig,
54 bool is_static) REQUIRES_SHARED(Locks::mutator_lock_);
55
56 // Finds the field using JNI semantics and initializes any classes. Does not encode the method in a
57 // JNI id.
58 ArtField* FindFieldJNI(const ScopedObjectAccess& soa,
59 jclass java_class,
60 const char* name,
61 const char* sig,
62 bool is_static) REQUIRES_SHARED(Locks::mutator_lock_);
63
64 namespace jni {
65
66 // We want to maintain a branchless fast-path for performance reasons. The JniIdManager is the
67 // ultimate source of truth for how the IDs are handed out but we inline the normal non-index cases
68 // here.
69
70 template <bool kEnableIndexIds>
71 ALWAYS_INLINE
IsIndexId(jmethodID mid)72 static bool IsIndexId(jmethodID mid) {
73 return kEnableIndexIds && ((reinterpret_cast<uintptr_t>(mid) % 2) != 0);
74 }
75
76 template <bool kEnableIndexIds>
77 ALWAYS_INLINE
IsIndexId(jfieldID fid)78 static bool IsIndexId(jfieldID fid) {
79 return kEnableIndexIds && ((reinterpret_cast<uintptr_t>(fid) % 2) != 0);
80 }
81
82 template <bool kEnableIndexIds = true>
83 ALWAYS_INLINE
DecodeArtField(jfieldID fid)84 static inline ArtField* DecodeArtField(jfieldID fid) {
85 if (IsIndexId<kEnableIndexIds>(fid)) {
86 return Runtime::Current()->GetJniIdManager()->DecodeFieldId(fid);
87 } else {
88 return reinterpret_cast<ArtField*>(fid);
89 }
90 }
91
92 template <bool kEnableIndexIds = true>
EncodeArtField(ReflectiveHandle<ArtField> field)93 ALWAYS_INLINE static inline jfieldID EncodeArtField(ReflectiveHandle<ArtField> field)
94 REQUIRES_SHARED(Locks::mutator_lock_) {
95 if (kEnableIndexIds && Runtime::Current()->GetJniIdType() != JniIdType::kPointer) {
96 return Runtime::Current()->GetJniIdManager()->EncodeFieldId(field);
97 } else {
98 return reinterpret_cast<jfieldID>(field.Get());
99 }
100 }
101
102 template <bool kEnableIndexIds = true>
103 ALWAYS_INLINE
EncodeArtField(ArtField * field)104 static inline jfieldID EncodeArtField(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_) {
105 if (kEnableIndexIds && Runtime::Current()->GetJniIdType() != JniIdType::kPointer) {
106 return Runtime::Current()->GetJniIdManager()->EncodeFieldId(field);
107 } else {
108 return reinterpret_cast<jfieldID>(field);
109 }
110 }
111
112 template <bool kEnableIndexIds = true>
113 ALWAYS_INLINE
EncodeArtMethod(ReflectiveHandle<ArtMethod> art_method)114 static inline jmethodID EncodeArtMethod(ReflectiveHandle<ArtMethod> art_method)
115 REQUIRES_SHARED(Locks::mutator_lock_) {
116 if (kEnableIndexIds && Runtime::Current()->GetJniIdType() != JniIdType::kPointer) {
117 return Runtime::Current()->GetJniIdManager()->EncodeMethodId(art_method);
118 } else if (art_method == nullptr ||
119 !art_method->IsCopied() ||
120 art_method->IsDefaultConflicting()) {
121 return reinterpret_cast<jmethodID>(art_method.Get());
122 } else {
123 return reinterpret_cast<jmethodID>(art_method->GetCanonicalMethod());
124 }
125 }
126
127 template <bool kEnableIndexIds = true>
128 ALWAYS_INLINE
EncodeArtMethod(ArtMethod * art_method)129 static inline jmethodID EncodeArtMethod(ArtMethod* art_method)
130 REQUIRES_SHARED(Locks::mutator_lock_) {
131 if (kEnableIndexIds && Runtime::Current()->GetJniIdType() != JniIdType::kPointer) {
132 return Runtime::Current()->GetJniIdManager()->EncodeMethodId(art_method);
133 } else if (art_method == nullptr ||
134 !art_method->IsCopied() ||
135 art_method->IsDefaultConflicting()) {
136 return reinterpret_cast<jmethodID>(art_method);
137 } else {
138 return reinterpret_cast<jmethodID>(art_method->GetCanonicalMethod());
139 }
140 }
141
142 template <bool kEnableIndexIds = true>
143 ALWAYS_INLINE
DecodeArtMethod(jmethodID method_id)144 static inline ArtMethod* DecodeArtMethod(jmethodID method_id) {
145 if (IsIndexId<kEnableIndexIds>(method_id)) {
146 return Runtime::Current()->GetJniIdManager()->DecodeMethodId(method_id);
147 } else {
148 return reinterpret_cast<ArtMethod*>(method_id);
149 }
150 }
151
152 } // namespace jni
153 } // namespace art
154
155 std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
156
157 #endif // ART_RUNTIME_JNI_JNI_INTERNAL_H_
158