1 /*
2 * Copyright (C) 2015 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 #include "jni.h"
18
19 #include <android-base/logging.h>
20
21 #include "arch/context.h"
22 #include "base/mutex.h"
23 #include "dex/dex_file-inl.h"
24 #include "jni/jni_internal.h"
25 #include "mirror/class-inl.h"
26 #include "nth_caller_visitor.h"
27 #include "oat/oat_file.h"
28 #include "oat/oat_quick_method_header.h"
29 #include "runtime.h"
30 #include "scoped_thread_state_change-inl.h"
31 #include "stack.h"
32 #include "thread-current-inl.h"
33
34 namespace art {
35
36 static bool asserts_enabled = true;
37
38 // public static native void disableStackFrameAsserts();
39 // Note: to globally disable asserts in unsupported configurations.
40
Java_Main_disableStackFrameAsserts(JNIEnv * env,jclass cls)41 extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts([[maybe_unused]] JNIEnv* env,
42 [[maybe_unused]] jclass cls) {
43 asserts_enabled = false;
44 }
45
IsInterpreted(JNIEnv * env,jclass,size_t level)46 static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) {
47 ScopedObjectAccess soa(env);
48 NthCallerVisitor caller(soa.Self(), level, false);
49 caller.WalkStack();
50 CHECK(caller.caller != nullptr);
51 bool is_shadow_frame = (caller.GetCurrentShadowFrame() != nullptr);
52 bool is_nterp_frame = (caller.GetCurrentQuickFrame() != nullptr) &&
53 (caller.GetCurrentOatQuickMethodHeader()->IsNterpMethodHeader());
54 return (is_shadow_frame || is_nterp_frame) ? JNI_TRUE : JNI_FALSE;
55 }
56
57 // public static native boolean isInterpreted();
58
Java_Main_isInterpreted(JNIEnv * env,jclass klass)59 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) {
60 return IsInterpreted(env, klass, 1);
61 }
62
63 // public static native boolean isInterpreted(int depth);
64
Java_Main_isInterpretedAt(JNIEnv * env,jclass klass,jint depth)65 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedAt(JNIEnv* env,
66 jclass klass,
67 jint depth) {
68 return IsInterpreted(env, klass, depth);
69 }
70
71
72 // public static native boolean isInterpretedFunction(String smali);
73
IsMethodInterpreted(Thread * self,const ArtMethod * goal,const bool require_deoptable,bool * method_is_interpreted)74 static bool IsMethodInterpreted(Thread* self,
75 const ArtMethod* goal,
76 const bool require_deoptable,
77 /* out */ bool* method_is_interpreted)
78 REQUIRES_SHARED(Locks::mutator_lock_) {
79 *method_is_interpreted = true;
80 bool method_found = false;
81 bool prev_was_runtime = true;
82 StackVisitor::WalkStack(
83 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
84 if (goal == stack_visitor->GetMethod()) {
85 // We don't deoptimize beyond a runtime frame. So if we need the method to be
86 // deoptimizeable we cannot allow the previous frame to be a runtime frame.
87 *method_is_interpreted =
88 (require_deoptable && prev_was_runtime) || stack_visitor->IsShadowFrame();
89 method_found = true;
90 return false;
91 }
92 prev_was_runtime = stack_visitor->GetMethod()->IsRuntimeMethod();
93 return true;
94 },
95 self,
96 /* context= */ nullptr,
97 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
98 return method_found;
99 }
100
101 // TODO Remove 'require_deoptimizable' option once we have deoptimization through runtime frames.
Java_Main_isInterpretedFunction(JNIEnv * env,jclass klass,jobject method,jboolean require_deoptimizable)102 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedFunction(
103 JNIEnv* env, [[maybe_unused]] jclass klass, jobject method, jboolean require_deoptimizable) {
104 // Return false if this seems to not be an ART runtime.
105 if (Runtime::Current() == nullptr) {
106 return JNI_FALSE;
107 }
108 if (method == nullptr) {
109 env->ThrowNew(env->FindClass("java/lang/NullPointerException"), "method is null!");
110 return JNI_FALSE;
111 }
112 jmethodID id = env->FromReflectedMethod(method);
113 if (id == nullptr) {
114 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to interpret method argument!");
115 return JNI_FALSE;
116 }
117 {
118 ScopedObjectAccess soa(env);
119 ArtMethod* goal = jni::DecodeArtMethod(id);
120 bool is_interpreted;
121 if (!IsMethodInterpreted(soa.Self(), goal, require_deoptimizable, &is_interpreted)) {
122 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to find given method in stack!");
123 return JNI_FALSE;
124 }
125 bool enters_interpreter = Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(
126 goal->GetEntryPointFromQuickCompiledCode());
127 return (is_interpreted || enters_interpreter);
128 }
129 }
130
131 // public static native void assertIsInterpreted();
132
Java_Main_assertIsInterpreted(JNIEnv * env,jclass klass)133 extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
134 if (asserts_enabled) {
135 CHECK(Java_Main_isInterpreted(env, klass));
136 }
137 }
138
IsManaged(JNIEnv * env,jclass,size_t level)139 static jboolean IsManaged(JNIEnv* env, jclass, size_t level) {
140 ScopedObjectAccess soa(env);
141 NthCallerVisitor caller(soa.Self(), level, false);
142 caller.WalkStack();
143 CHECK(caller.caller != nullptr);
144 return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
145 }
146
147 // public static native boolean isManaged();
148
Java_Main_isManaged(JNIEnv * env,jclass cls)149 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
150 return IsManaged(env, cls, 1);
151 }
152
153 // public static native void assertIsManaged();
154
Java_Main_assertIsManaged(JNIEnv * env,jclass cls)155 extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
156 if (asserts_enabled) {
157 CHECK(Java_Main_isManaged(env, cls));
158 }
159 }
160
161 // public static native boolean isCallerInterpreted();
162
Java_Main_isCallerInterpreted(JNIEnv * env,jclass klass)163 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerInterpreted(JNIEnv* env, jclass klass) {
164 return IsInterpreted(env, klass, 2);
165 }
166
167 // public static native void assertCallerIsInterpreted();
168
Java_Main_assertCallerIsInterpreted(JNIEnv * env,jclass klass)169 extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass klass) {
170 if (asserts_enabled) {
171 CHECK(Java_Main_isCallerInterpreted(env, klass));
172 }
173 }
174
175 // public static native boolean isCallerManaged();
176
Java_Main_isCallerManaged(JNIEnv * env,jclass cls)177 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerManaged(JNIEnv* env, jclass cls) {
178 return IsManaged(env, cls, 2);
179 }
180
181 // public static native void assertCallerIsManaged();
182
Java_Main_assertCallerIsManaged(JNIEnv * env,jclass cls)183 extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) {
184 if (asserts_enabled) {
185 CHECK(Java_Main_isCallerManaged(env, cls));
186 }
187 }
188
Java_Main_getThisOfCaller(JNIEnv * env,jclass cls)189 extern "C" JNIEXPORT jobject JNICALL Java_Main_getThisOfCaller(
190 JNIEnv* env, [[maybe_unused]] jclass cls) {
191 ScopedObjectAccess soa(env);
192 std::unique_ptr<art::Context> context(art::Context::Create());
193 jobject result = nullptr;
194 StackVisitor::WalkStack(
195 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
196 // Discard stubs and Main.getThisOfCaller and methods without vreg info.
197 if (stack_visitor->GetMethod() == nullptr ||
198 stack_visitor->GetMethod()->IsNative() ||
199 (stack_visitor->GetCurrentShadowFrame() == nullptr &&
200 !Runtime::Current()->IsAsyncDeoptimizeable(stack_visitor->GetOuterMethod(),
201 stack_visitor->GetCurrentQuickFramePc()))) {
202 return true;
203 }
204 result = soa.AddLocalReference<jobject>(stack_visitor->GetThisObject());
205 return false;
206 },
207 soa.Self(),
208 context.get(),
209 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
210 return result;
211 }
212
213 } // namespace art
214