blob: 393e773275beced7e155c3b8672e04b0bdf938fd [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
21#include "base/mutex.h"
David Sehr9e734c72018-01-04 17:56:19 -080022#include "dex/dex_file-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010023#include "jni/jni_internal.h"
Andreas Gampea727e372015-08-25 09:22:37 -070024#include "mirror/class-inl.h"
25#include "nth_caller_visitor.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010026#include "oat_file.h"
Andreas Gampea727e372015-08-25 09:22:37 -070027#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070028#include "scoped_thread_state_change-inl.h"
Andreas Gampea727e372015-08-25 09:22:37 -070029#include "stack.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070030#include "thread-current-inl.h"
Andreas Gampea727e372015-08-25 09:22:37 -070031
32namespace art {
33
Nicolas Geoffray1949baf2017-10-17 12:14:53 +000034static bool asserts_enabled = true;
Andreas Gampea727e372015-08-25 09:22:37 -070035
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070036// public static native void disableStackFrameAsserts();
37// Note: to globally disable asserts in unsupported configurations.
38
39extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED,
40 jclass cls ATTRIBUTE_UNUSED) {
41 asserts_enabled = false;
42}
43
Mingyao Yangf711f2c2016-05-23 12:29:39 -070044static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -070045 ScopedObjectAccess soa(env);
Mingyao Yangf711f2c2016-05-23 12:29:39 -070046 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -070047 caller.WalkStack();
48 CHECK(caller.caller != nullptr);
Andreas Gampe89df7bf2015-09-30 13:13:21 -070049 return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE;
Andreas Gampea727e372015-08-25 09:22:37 -070050}
51
Mingyao Yangf711f2c2016-05-23 12:29:39 -070052// public static native boolean isInterpreted();
53
54extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) {
55 return IsInterpreted(env, klass, 1);
56}
57
Alex Lightdba61482016-12-21 08:20:29 -080058// public static native boolean isInterpreted(int depth);
59
60extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedAt(JNIEnv* env,
61 jclass klass,
62 jint depth) {
63 return IsInterpreted(env, klass, depth);
64}
65
66
67// public static native boolean isInterpretedFunction(String smali);
68
Andreas Gampec7d878d2018-11-19 18:42:06 +000069static bool IsMethodInterpreted(Thread* self,
70 const ArtMethod* goal,
71 const bool require_deoptable,
72 /* out */ bool* method_is_interpreted)
73 REQUIRES_SHARED(Locks::mutator_lock_) {
74 *method_is_interpreted = true;
75 bool method_found = false;
76 bool prev_was_runtime = true;
77 StackVisitor::WalkStack(
78 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
79 if (goal == stack_visitor->GetMethod()) {
80 *method_is_interpreted =
81 (require_deoptable && prev_was_runtime) || stack_visitor->IsShadowFrame();
82 method_found = true;
83 return false;
84 }
85 prev_was_runtime = stack_visitor->GetMethod()->IsRuntimeMethod();
86 return true;
87 },
88 self,
89 /* context= */ nullptr,
90 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
91 return method_found;
92}
Alex Lightdba61482016-12-21 08:20:29 -080093
94// TODO Remove 'require_deoptimizable' option once we have deoptimization through runtime frames.
95extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedFunction(
96 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method, jboolean require_deoptimizable) {
97 // Return false if this seems to not be an ART runtime.
98 if (Runtime::Current() == nullptr) {
99 return JNI_FALSE;
100 }
101 if (method == nullptr) {
102 env->ThrowNew(env->FindClass("java/lang/NullPointerException"), "method is null!");
103 return JNI_FALSE;
104 }
105 jmethodID id = env->FromReflectedMethod(method);
106 if (id == nullptr) {
107 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to interpret method argument!");
108 return JNI_FALSE;
109 }
Alex Lightdba61482016-12-21 08:20:29 -0800110 {
111 ScopedObjectAccess soa(env);
112 ArtMethod* goal = jni::DecodeArtMethod(id);
Andreas Gampec7d878d2018-11-19 18:42:06 +0000113 bool is_interpreted;
114 if (!IsMethodInterpreted(soa.Self(), goal, require_deoptimizable, &is_interpreted)) {
115 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to find given method in stack!");
116 return JNI_FALSE;
117 }
Alex Lightdba61482016-12-21 08:20:29 -0800118 bool enters_interpreter = Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(
119 goal->GetEntryPointFromQuickCompiledCode());
Andreas Gampec7d878d2018-11-19 18:42:06 +0000120 return (is_interpreted || enters_interpreter);
Alex Lightdba61482016-12-21 08:20:29 -0800121 }
Alex Lightdba61482016-12-21 08:20:29 -0800122}
123
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700124// public static native void assertIsInterpreted();
Andreas Gampea727e372015-08-25 09:22:37 -0700125
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700126extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
127 if (asserts_enabled) {
128 CHECK(Java_Main_isInterpreted(env, klass));
129 }
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700130}
Andreas Gampea727e372015-08-25 09:22:37 -0700131
Mingyao Yanga1e03672017-04-12 15:39:54 -0700132static jboolean IsManaged(JNIEnv* env, jclass, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -0700133 ScopedObjectAccess soa(env);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700134 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -0700135 caller.WalkStack();
136 CHECK(caller.caller != nullptr);
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700137 return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
138}
Andreas Gampea727e372015-08-25 09:22:37 -0700139
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700140// public static native boolean isManaged();
141
142extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
143 return IsManaged(env, cls, 1);
144}
145
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700146// public static native void assertIsManaged();
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700147
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700148extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
149 if (asserts_enabled) {
150 CHECK(Java_Main_isManaged(env, cls));
151 }
Andreas Gampea727e372015-08-25 09:22:37 -0700152}
153
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700154// public static native boolean isCallerInterpreted();
155
156extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerInterpreted(JNIEnv* env, jclass klass) {
157 return IsInterpreted(env, klass, 2);
158}
159
160// public static native void assertCallerIsInterpreted();
161
162extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass klass) {
163 if (asserts_enabled) {
164 CHECK(Java_Main_isCallerInterpreted(env, klass));
165 }
166}
167
168// public static native boolean isCallerManaged();
169
170extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerManaged(JNIEnv* env, jclass cls) {
171 return IsManaged(env, cls, 2);
172}
173
174// public static native void assertCallerIsManaged();
175
176extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) {
177 if (asserts_enabled) {
178 CHECK(Java_Main_isCallerManaged(env, cls));
179 }
180}
181
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100182extern "C" JNIEXPORT jobject JNICALL Java_Main_getThisOfCaller(
183 JNIEnv* env, jclass cls ATTRIBUTE_UNUSED) {
184 ScopedObjectAccess soa(env);
185 std::unique_ptr<art::Context> context(art::Context::Create());
Andreas Gampec7d878d2018-11-19 18:42:06 +0000186 jobject result = nullptr;
187 StackVisitor::WalkStack(
188 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
189 // Discard stubs and Main.getThisOfCaller.
190 if (stack_visitor->GetMethod() == nullptr || stack_visitor->GetMethod()->IsNative()) {
191 return true;
192 }
193 result = soa.AddLocalReference<jobject>(stack_visitor->GetThisObject());
194 return false;
195 },
196 soa.Self(),
197 context.get(),
198 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
199 return result;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100200}
201
Andreas Gampea727e372015-08-25 09:22:37 -0700202} // namespace art