blob: 79c7a36a8eaf3f74e903979c0e76a4dd140151c8 [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"
Nicolas Geoffray00391822019-12-10 10:17:23 +000028#include "oat_quick_method_header.h"
Andreas Gampea727e372015-08-25 09:22:37 -070029#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_thread_state_change-inl.h"
Andreas Gampea727e372015-08-25 09:22:37 -070031#include "stack.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070032#include "thread-current-inl.h"
Andreas Gampea727e372015-08-25 09:22:37 -070033
34namespace art {
35
Nicolas Geoffray1949baf2017-10-17 12:14:53 +000036static bool asserts_enabled = true;
Andreas Gampea727e372015-08-25 09:22:37 -070037
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070038// public static native void disableStackFrameAsserts();
39// Note: to globally disable asserts in unsupported configurations.
40
41extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED,
42 jclass cls ATTRIBUTE_UNUSED) {
43 asserts_enabled = false;
44}
45
Mingyao Yangf711f2c2016-05-23 12:29:39 -070046static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -070047 ScopedObjectAccess soa(env);
Mingyao Yangf711f2c2016-05-23 12:29:39 -070048 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -070049 caller.WalkStack();
50 CHECK(caller.caller != nullptr);
Nicolas Geoffray00391822019-12-10 10:17:23 +000051 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;
Andreas Gampea727e372015-08-25 09:22:37 -070055}
56
Mingyao Yangf711f2c2016-05-23 12:29:39 -070057// public static native boolean isInterpreted();
58
59extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) {
60 return IsInterpreted(env, klass, 1);
61}
62
Alex Lightdba61482016-12-21 08:20:29 -080063// public static native boolean isInterpreted(int depth);
64
65extern "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
Andreas Gampec7d878d2018-11-19 18:42:06 +000074static 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 *method_is_interpreted =
86 (require_deoptable && prev_was_runtime) || stack_visitor->IsShadowFrame();
87 method_found = true;
88 return false;
89 }
90 prev_was_runtime = stack_visitor->GetMethod()->IsRuntimeMethod();
91 return true;
92 },
93 self,
94 /* context= */ nullptr,
95 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
96 return method_found;
97}
Alex Lightdba61482016-12-21 08:20:29 -080098
99// TODO Remove 'require_deoptimizable' option once we have deoptimization through runtime frames.
100extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedFunction(
101 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method, jboolean require_deoptimizable) {
102 // Return false if this seems to not be an ART runtime.
103 if (Runtime::Current() == nullptr) {
104 return JNI_FALSE;
105 }
106 if (method == nullptr) {
107 env->ThrowNew(env->FindClass("java/lang/NullPointerException"), "method is null!");
108 return JNI_FALSE;
109 }
110 jmethodID id = env->FromReflectedMethod(method);
111 if (id == nullptr) {
112 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to interpret method argument!");
113 return JNI_FALSE;
114 }
Alex Lightdba61482016-12-21 08:20:29 -0800115 {
116 ScopedObjectAccess soa(env);
117 ArtMethod* goal = jni::DecodeArtMethod(id);
Andreas Gampec7d878d2018-11-19 18:42:06 +0000118 bool is_interpreted;
119 if (!IsMethodInterpreted(soa.Self(), goal, require_deoptimizable, &is_interpreted)) {
120 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to find given method in stack!");
121 return JNI_FALSE;
122 }
Alex Lightdba61482016-12-21 08:20:29 -0800123 bool enters_interpreter = Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(
124 goal->GetEntryPointFromQuickCompiledCode());
Andreas Gampec7d878d2018-11-19 18:42:06 +0000125 return (is_interpreted || enters_interpreter);
Alex Lightdba61482016-12-21 08:20:29 -0800126 }
Alex Lightdba61482016-12-21 08:20:29 -0800127}
128
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700129// public static native void assertIsInterpreted();
Andreas Gampea727e372015-08-25 09:22:37 -0700130
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700131extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
132 if (asserts_enabled) {
133 CHECK(Java_Main_isInterpreted(env, klass));
134 }
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700135}
Andreas Gampea727e372015-08-25 09:22:37 -0700136
Mingyao Yanga1e03672017-04-12 15:39:54 -0700137static jboolean IsManaged(JNIEnv* env, jclass, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -0700138 ScopedObjectAccess soa(env);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700139 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -0700140 caller.WalkStack();
141 CHECK(caller.caller != nullptr);
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700142 return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
143}
Andreas Gampea727e372015-08-25 09:22:37 -0700144
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700145// public static native boolean isManaged();
146
147extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
148 return IsManaged(env, cls, 1);
149}
150
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700151// public static native void assertIsManaged();
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700152
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700153extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
154 if (asserts_enabled) {
155 CHECK(Java_Main_isManaged(env, cls));
156 }
Andreas Gampea727e372015-08-25 09:22:37 -0700157}
158
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700159// public static native boolean isCallerInterpreted();
160
161extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerInterpreted(JNIEnv* env, jclass klass) {
162 return IsInterpreted(env, klass, 2);
163}
164
165// public static native void assertCallerIsInterpreted();
166
167extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass klass) {
168 if (asserts_enabled) {
169 CHECK(Java_Main_isCallerInterpreted(env, klass));
170 }
171}
172
173// public static native boolean isCallerManaged();
174
175extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerManaged(JNIEnv* env, jclass cls) {
176 return IsManaged(env, cls, 2);
177}
178
179// public static native void assertCallerIsManaged();
180
181extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) {
182 if (asserts_enabled) {
183 CHECK(Java_Main_isCallerManaged(env, cls));
184 }
185}
186
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100187extern "C" JNIEXPORT jobject JNICALL Java_Main_getThisOfCaller(
188 JNIEnv* env, jclass cls ATTRIBUTE_UNUSED) {
189 ScopedObjectAccess soa(env);
190 std::unique_ptr<art::Context> context(art::Context::Create());
Andreas Gampec7d878d2018-11-19 18:42:06 +0000191 jobject result = nullptr;
192 StackVisitor::WalkStack(
193 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Serov2808be82018-12-20 19:15:11 +0000194 // Discard stubs and Main.getThisOfCaller and methods without vreg info.
195 if (stack_visitor->GetMethod() == nullptr ||
196 stack_visitor->GetMethod()->IsNative() ||
197 (stack_visitor->GetCurrentShadowFrame() == nullptr &&
198 !Runtime::Current()->IsAsyncDeoptimizeable(stack_visitor->GetCurrentQuickFramePc()))) {
Andreas Gampec7d878d2018-11-19 18:42:06 +0000199 return true;
200 }
201 result = soa.AddLocalReference<jobject>(stack_visitor->GetThisObject());
202 return false;
203 },
204 soa.Self(),
205 context.get(),
206 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
207 return result;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100208}
209
Andreas Gampea727e372015-08-25 09:22:37 -0700210} // namespace art