• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/mutex.h"
22 #include "dex/dex_file-inl.h"
23 #include "jni_internal.h"
24 #include "mirror/class-inl.h"
25 #include "nth_caller_visitor.h"
26 #include "oat_file.h"
27 #include "runtime.h"
28 #include "scoped_thread_state_change-inl.h"
29 #include "stack.h"
30 #include "thread-current-inl.h"
31 
32 namespace art {
33 
34 static bool asserts_enabled = true;
35 
36 // public static native void disableStackFrameAsserts();
37 // Note: to globally disable asserts in unsupported configurations.
38 
Java_Main_disableStackFrameAsserts(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)39 extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED,
40                                                                      jclass cls ATTRIBUTE_UNUSED) {
41   asserts_enabled = false;
42 }
43 
IsInterpreted(JNIEnv * env,jclass,size_t level)44 static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) {
45   ScopedObjectAccess soa(env);
46   NthCallerVisitor caller(soa.Self(), level, false);
47   caller.WalkStack();
48   CHECK(caller.caller != nullptr);
49   return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE;
50 }
51 
52 // public static native boolean isInterpreted();
53 
Java_Main_isInterpreted(JNIEnv * env,jclass klass)54 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) {
55   return IsInterpreted(env, klass, 1);
56 }
57 
58 // public static native boolean isInterpreted(int depth);
59 
Java_Main_isInterpretedAt(JNIEnv * env,jclass klass,jint depth)60 extern "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.
70 struct MethodIsInterpretedVisitor : public StackVisitor {
71  public:
MethodIsInterpretedVisitorart::MethodIsInterpretedVisitor72   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 
VisitFrameart::MethodIsInterpretedVisitor80   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 
IsInterpretedart::MethodIsInterpretedVisitor90   bool IsInterpreted() {
91     return method_is_interpreted_;
92   }
93 
IsFoundart::MethodIsInterpretedVisitor94   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.
Java_Main_isInterpretedFunction(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method,jboolean require_deoptimizable)107 extern "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 
141 // public static native void assertIsInterpreted();
142 
Java_Main_assertIsInterpreted(JNIEnv * env,jclass klass)143 extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
144   if (asserts_enabled) {
145     CHECK(Java_Main_isInterpreted(env, klass));
146   }
147 }
148 
IsManaged(JNIEnv * env,jclass,size_t level)149 static jboolean IsManaged(JNIEnv* env, jclass, size_t level) {
150   ScopedObjectAccess soa(env);
151   NthCallerVisitor caller(soa.Self(), level, false);
152   caller.WalkStack();
153   CHECK(caller.caller != nullptr);
154   return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
155 }
156 
157 // public static native boolean isManaged();
158 
Java_Main_isManaged(JNIEnv * env,jclass cls)159 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
160   return IsManaged(env, cls, 1);
161 }
162 
163 // public static native void assertIsManaged();
164 
Java_Main_assertIsManaged(JNIEnv * env,jclass cls)165 extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
166   if (asserts_enabled) {
167     CHECK(Java_Main_isManaged(env, cls));
168   }
169 }
170 
171 // public static native boolean isCallerInterpreted();
172 
Java_Main_isCallerInterpreted(JNIEnv * env,jclass klass)173 extern "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 
Java_Main_assertCallerIsInterpreted(JNIEnv * env,jclass klass)179 extern "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 
Java_Main_isCallerManaged(JNIEnv * env,jclass cls)187 extern "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 
Java_Main_assertCallerIsManaged(JNIEnv * env,jclass cls)193 extern "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 
199 }  // namespace art
200