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 "arch/context.h"
22 #include "base/mutex.h"
23 #include "dex/dex_file-inl.h"
24 #include "jni/jni_internal.h"
25 #include "mirror/class-inl.h"
26 #include "nth_caller_visitor.h"
27 #include "oat_file.h"
28 #include "oat_quick_method_header.h"
29 #include "runtime.h"
30 #include "scoped_thread_state_change-inl.h"
31 #include "stack.h"
32 #include "thread-current-inl.h"
33 
34 namespace art {
35 
36 static bool asserts_enabled = true;
37 
38 // public static native void disableStackFrameAsserts();
39 // Note: to globally disable asserts in unsupported configurations.
40 
Java_Main_disableStackFrameAsserts(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)41 extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED,
42                                                                      jclass cls ATTRIBUTE_UNUSED) {
43   asserts_enabled = false;
44 }
45 
IsInterpreted(JNIEnv * env,jclass,size_t level)46 static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) {
47   ScopedObjectAccess soa(env);
48   NthCallerVisitor caller(soa.Self(), level, false);
49   caller.WalkStack();
50   CHECK(caller.caller != nullptr);
51   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;
55 }
56 
57 // public static native boolean isInterpreted();
58 
Java_Main_isInterpreted(JNIEnv * env,jclass klass)59 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) {
60   return IsInterpreted(env, klass, 1);
61 }
62 
63 // public static native boolean isInterpreted(int depth);
64 
Java_Main_isInterpretedAt(JNIEnv * env,jclass klass,jint depth)65 extern "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 
IsMethodInterpreted(Thread * self,const ArtMethod * goal,const bool require_deoptable,bool * method_is_interpreted)74 static 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 }
98 
99 // 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)100 extern "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   }
115   {
116     ScopedObjectAccess soa(env);
117     ArtMethod* goal = jni::DecodeArtMethod(id);
118     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     }
123     bool enters_interpreter = Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(
124         goal->GetEntryPointFromQuickCompiledCode());
125     return (is_interpreted || enters_interpreter);
126   }
127 }
128 
129 // public static native void assertIsInterpreted();
130 
Java_Main_assertIsInterpreted(JNIEnv * env,jclass klass)131 extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
132   if (asserts_enabled) {
133     CHECK(Java_Main_isInterpreted(env, klass));
134   }
135 }
136 
IsManaged(JNIEnv * env,jclass,size_t level)137 static jboolean IsManaged(JNIEnv* env, jclass, size_t level) {
138   ScopedObjectAccess soa(env);
139   NthCallerVisitor caller(soa.Self(), level, false);
140   caller.WalkStack();
141   CHECK(caller.caller != nullptr);
142   return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
143 }
144 
145 // public static native boolean isManaged();
146 
Java_Main_isManaged(JNIEnv * env,jclass cls)147 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
148   return IsManaged(env, cls, 1);
149 }
150 
151 // public static native void assertIsManaged();
152 
Java_Main_assertIsManaged(JNIEnv * env,jclass cls)153 extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
154   if (asserts_enabled) {
155     CHECK(Java_Main_isManaged(env, cls));
156   }
157 }
158 
159 // public static native boolean isCallerInterpreted();
160 
Java_Main_isCallerInterpreted(JNIEnv * env,jclass klass)161 extern "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 
Java_Main_assertCallerIsInterpreted(JNIEnv * env,jclass klass)167 extern "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 
Java_Main_isCallerManaged(JNIEnv * env,jclass cls)175 extern "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 
Java_Main_assertCallerIsManaged(JNIEnv * env,jclass cls)181 extern "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 
Java_Main_getThisOfCaller(JNIEnv * env,jclass cls ATTRIBUTE_UNUSED)187 extern "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());
191   jobject result = nullptr;
192   StackVisitor::WalkStack(
193       [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
194         // 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()))) {
199           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;
208 }
209 
210 }  // namespace art
211