blob: 192274e5aedc51ee4d3178fa7bad8fea35316d7c [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
69// TODO Remove 'allow_runtime_frames' option once we have deoptimization through runtime frames.
70struct MethodIsInterpretedVisitor : public StackVisitor {
71 public:
72 MethodIsInterpretedVisitor(Thread* thread, ArtMethod* goal, bool require_deoptable)
73 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
74 goal_(goal),
75 method_is_interpreted_(true),
76 method_found_(false),
77 prev_was_runtime_(true),
78 require_deoptable_(require_deoptable) {}
79
80 virtual bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
81 if (goal_ == GetMethod()) {
82 method_is_interpreted_ = (require_deoptable_ && prev_was_runtime_) || IsShadowFrame();
83 method_found_ = true;
84 return false;
85 }
86 prev_was_runtime_ = GetMethod()->IsRuntimeMethod();
87 return true;
88 }
89
90 bool IsInterpreted() {
91 return method_is_interpreted_;
92 }
93
94 bool IsFound() {
95 return method_found_;
96 }
97
98 private:
99 const ArtMethod* goal_;
100 bool method_is_interpreted_;
101 bool method_found_;
102 bool prev_was_runtime_;
103 bool require_deoptable_;
104};
105
106// TODO Remove 'require_deoptimizable' option once we have deoptimization through runtime frames.
107extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedFunction(
108 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method, jboolean require_deoptimizable) {
109 // Return false if this seems to not be an ART runtime.
110 if (Runtime::Current() == nullptr) {
111 return JNI_FALSE;
112 }
113 if (method == nullptr) {
114 env->ThrowNew(env->FindClass("java/lang/NullPointerException"), "method is null!");
115 return JNI_FALSE;
116 }
117 jmethodID id = env->FromReflectedMethod(method);
118 if (id == nullptr) {
119 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to interpret method argument!");
120 return JNI_FALSE;
121 }
122 bool result;
123 bool found;
124 {
125 ScopedObjectAccess soa(env);
126 ArtMethod* goal = jni::DecodeArtMethod(id);
127 MethodIsInterpretedVisitor v(soa.Self(), goal, require_deoptimizable);
128 v.WalkStack();
129 bool enters_interpreter = Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(
130 goal->GetEntryPointFromQuickCompiledCode());
131 result = (v.IsInterpreted() || enters_interpreter);
132 found = v.IsFound();
133 }
134 if (!found) {
135 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to find given method in stack!");
136 return JNI_FALSE;
137 }
138 return result;
139}
140
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700141// public static native void assertIsInterpreted();
Andreas Gampea727e372015-08-25 09:22:37 -0700142
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700143extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
144 if (asserts_enabled) {
145 CHECK(Java_Main_isInterpreted(env, klass));
146 }
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700147}
Andreas Gampea727e372015-08-25 09:22:37 -0700148
Mingyao Yanga1e03672017-04-12 15:39:54 -0700149static jboolean IsManaged(JNIEnv* env, jclass, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -0700150 ScopedObjectAccess soa(env);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700151 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -0700152 caller.WalkStack();
153 CHECK(caller.caller != nullptr);
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700154 return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
155}
Andreas Gampea727e372015-08-25 09:22:37 -0700156
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700157// public static native boolean isManaged();
158
159extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
160 return IsManaged(env, cls, 1);
161}
162
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700163// public static native void assertIsManaged();
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700164
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700165extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
166 if (asserts_enabled) {
167 CHECK(Java_Main_isManaged(env, cls));
168 }
Andreas Gampea727e372015-08-25 09:22:37 -0700169}
170
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700171// public static native boolean isCallerInterpreted();
172
173extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerInterpreted(JNIEnv* env, jclass klass) {
174 return IsInterpreted(env, klass, 2);
175}
176
177// public static native void assertCallerIsInterpreted();
178
179extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass klass) {
180 if (asserts_enabled) {
181 CHECK(Java_Main_isCallerInterpreted(env, klass));
182 }
183}
184
185// public static native boolean isCallerManaged();
186
187extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerManaged(JNIEnv* env, jclass cls) {
188 return IsManaged(env, cls, 2);
189}
190
191// public static native void assertCallerIsManaged();
192
193extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) {
194 if (asserts_enabled) {
195 CHECK(Java_Main_isCallerManaged(env, cls));
196 }
197}
198
Andreas Gampea727e372015-08-25 09:22:37 -0700199} // namespace art