blob: cb011a860707b9c7bfbcdbf5772eceb92eedeea5 [file] [log] [blame]
Andreas Gampea727e372015-08-25 09:22:37 -07001/*
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
Andreas Gampe57943812017-12-06 21:39:13 -080019#include <android-base/logging.h>
20
Andreas Gampee5d23982019-01-08 10:34:26 -080021#include "arch/context.h"
Andreas Gampe57943812017-12-06 21:39:13 -080022#include "base/mutex.h"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010024#include "jni/jni_internal.h"
Andreas Gampea727e372015-08-25 09:22:37 -070025#include "mirror/class-inl.h"
26#include "nth_caller_visitor.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010027#include "oat_file.h"
Andreas Gampea727e372015-08-25 09:22:37 -070028#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070029#include "scoped_thread_state_change-inl.h"
Andreas Gampea727e372015-08-25 09:22:37 -070030#include "stack.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070031#include "thread-current-inl.h"
Andreas Gampea727e372015-08-25 09:22:37 -070032
33namespace art {
34
Nicolas Geoffray1949baf2017-10-17 12:14:53 +000035static bool asserts_enabled = true;
Andreas Gampea727e372015-08-25 09:22:37 -070036
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070037// public static native void disableStackFrameAsserts();
38// Note: to globally disable asserts in unsupported configurations.
39
40extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED,
41 jclass cls ATTRIBUTE_UNUSED) {
42 asserts_enabled = false;
43}
44
Mingyao Yangf711f2c2016-05-23 12:29:39 -070045static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -070046 ScopedObjectAccess soa(env);
Mingyao Yangf711f2c2016-05-23 12:29:39 -070047 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -070048 caller.WalkStack();
49 CHECK(caller.caller != nullptr);
Andreas Gampe89df7bf2015-09-30 13:13:21 -070050 return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE;
Andreas Gampea727e372015-08-25 09:22:37 -070051}
52
Mingyao Yangf711f2c2016-05-23 12:29:39 -070053// public static native boolean isInterpreted();
54
55extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) {
56 return IsInterpreted(env, klass, 1);
57}
58
Alex Lightdba61482016-12-21 08:20:29 -080059// public static native boolean isInterpreted(int depth);
60
61extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedAt(JNIEnv* env,
62 jclass klass,
63 jint depth) {
64 return IsInterpreted(env, klass, depth);
65}
66
67
68// public static native boolean isInterpretedFunction(String smali);
69
Andreas Gampec7d878d2018-11-19 18:42:06 +000070static bool IsMethodInterpreted(Thread* self,
71 const ArtMethod* goal,
72 const bool require_deoptable,
73 /* out */ bool* method_is_interpreted)
74 REQUIRES_SHARED(Locks::mutator_lock_) {
75 *method_is_interpreted = true;
76 bool method_found = false;
77 bool prev_was_runtime = true;
78 StackVisitor::WalkStack(
79 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
80 if (goal == stack_visitor->GetMethod()) {
81 *method_is_interpreted =
82 (require_deoptable && prev_was_runtime) || stack_visitor->IsShadowFrame();
83 method_found = true;
84 return false;
85 }
86 prev_was_runtime = stack_visitor->GetMethod()->IsRuntimeMethod();
87 return true;
88 },
89 self,
90 /* context= */ nullptr,
91 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
92 return method_found;
93}
Alex Lightdba61482016-12-21 08:20:29 -080094
95// TODO Remove 'require_deoptimizable' option once we have deoptimization through runtime frames.
96extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedFunction(
97 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method, jboolean require_deoptimizable) {
98 // Return false if this seems to not be an ART runtime.
99 if (Runtime::Current() == nullptr) {
100 return JNI_FALSE;
101 }
102 if (method == nullptr) {
103 env->ThrowNew(env->FindClass("java/lang/NullPointerException"), "method is null!");
104 return JNI_FALSE;
105 }
106 jmethodID id = env->FromReflectedMethod(method);
107 if (id == nullptr) {
108 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to interpret method argument!");
109 return JNI_FALSE;
110 }
Alex Lightdba61482016-12-21 08:20:29 -0800111 {
112 ScopedObjectAccess soa(env);
113 ArtMethod* goal = jni::DecodeArtMethod(id);
Andreas Gampec7d878d2018-11-19 18:42:06 +0000114 bool is_interpreted;
115 if (!IsMethodInterpreted(soa.Self(), goal, require_deoptimizable, &is_interpreted)) {
116 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to find given method in stack!");
117 return JNI_FALSE;
118 }
Alex Lightdba61482016-12-21 08:20:29 -0800119 bool enters_interpreter = Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(
120 goal->GetEntryPointFromQuickCompiledCode());
Andreas Gampec7d878d2018-11-19 18:42:06 +0000121 return (is_interpreted || enters_interpreter);
Alex Lightdba61482016-12-21 08:20:29 -0800122 }
Alex Lightdba61482016-12-21 08:20:29 -0800123}
124
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700125// public static native void assertIsInterpreted();
Andreas Gampea727e372015-08-25 09:22:37 -0700126
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700127extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
128 if (asserts_enabled) {
129 CHECK(Java_Main_isInterpreted(env, klass));
130 }
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700131}
Andreas Gampea727e372015-08-25 09:22:37 -0700132
Mingyao Yanga1e03672017-04-12 15:39:54 -0700133static jboolean IsManaged(JNIEnv* env, jclass, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -0700134 ScopedObjectAccess soa(env);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700135 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -0700136 caller.WalkStack();
137 CHECK(caller.caller != nullptr);
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700138 return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
139}
Andreas Gampea727e372015-08-25 09:22:37 -0700140
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700141// public static native boolean isManaged();
142
143extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
144 return IsManaged(env, cls, 1);
145}
146
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700147// public static native void assertIsManaged();
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700148
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700149extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
150 if (asserts_enabled) {
151 CHECK(Java_Main_isManaged(env, cls));
152 }
Andreas Gampea727e372015-08-25 09:22:37 -0700153}
154
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700155// public static native boolean isCallerInterpreted();
156
157extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerInterpreted(JNIEnv* env, jclass klass) {
158 return IsInterpreted(env, klass, 2);
159}
160
161// public static native void assertCallerIsInterpreted();
162
163extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass klass) {
164 if (asserts_enabled) {
165 CHECK(Java_Main_isCallerInterpreted(env, klass));
166 }
167}
168
169// public static native boolean isCallerManaged();
170
171extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerManaged(JNIEnv* env, jclass cls) {
172 return IsManaged(env, cls, 2);
173}
174
175// public static native void assertCallerIsManaged();
176
177extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) {
178 if (asserts_enabled) {
179 CHECK(Java_Main_isCallerManaged(env, cls));
180 }
181}
182
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100183extern "C" JNIEXPORT jobject JNICALL Java_Main_getThisOfCaller(
184 JNIEnv* env, jclass cls ATTRIBUTE_UNUSED) {
185 ScopedObjectAccess soa(env);
186 std::unique_ptr<art::Context> context(art::Context::Create());
Andreas Gampec7d878d2018-11-19 18:42:06 +0000187 jobject result = nullptr;
188 StackVisitor::WalkStack(
189 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
190 // Discard stubs and Main.getThisOfCaller.
191 if (stack_visitor->GetMethod() == nullptr || stack_visitor->GetMethod()->IsNative()) {
192 return true;
193 }
194 result = soa.AddLocalReference<jobject>(stack_visitor->GetThisObject());
195 return false;
196 },
197 soa.Self(),
198 context.get(),
199 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
200 return result;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100201}
202
Andreas Gampea727e372015-08-25 09:22:37 -0700203} // namespace art