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 "runtime.h" 29 #include "scoped_thread_state_change-inl.h" 30 #include "stack.h" 31 #include "thread-current-inl.h" 32 33 namespace art { 34 35 static bool asserts_enabled = true; 36 37 // public static native void disableStackFrameAsserts(); 38 // Note: to globally disable asserts in unsupported configurations. 39 40 extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED, 41 jclass cls ATTRIBUTE_UNUSED) { 42 asserts_enabled = false; 43 } 44 45 static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) { 46 ScopedObjectAccess soa(env); 47 NthCallerVisitor caller(soa.Self(), level, false); 48 caller.WalkStack(); 49 CHECK(caller.caller != nullptr); 50 return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE; 51 } 52 53 // public static native boolean isInterpreted(); 54 55 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) { 56 return IsInterpreted(env, klass, 1); 57 } 58 59 // public static native boolean isInterpreted(int depth); 60 61 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedAt(JNIEnv* env, 62 jclass klass, 63 jint depth) { 64 return IsInterpreted(env, klass, depth); 65 } 66 67 68 // public static native boolean isInterpretedFunction(String smali); 69 70 static bool IsMethodInterpreted(Thread* self, 71 const ArtMethod* goal, 72 const bool require_deoptable, 73 /* out */ bool* method_is_interpreted) 74 REQUIRES_SHARED(Locks::mutator_lock_) { 75 *method_is_interpreted = true; 76 bool method_found = false; 77 bool prev_was_runtime = true; 78 StackVisitor::WalkStack( 79 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) { 80 if (goal == stack_visitor->GetMethod()) { 81 *method_is_interpreted = 82 (require_deoptable && prev_was_runtime) || stack_visitor->IsShadowFrame(); 83 method_found = true; 84 return false; 85 } 86 prev_was_runtime = stack_visitor->GetMethod()->IsRuntimeMethod(); 87 return true; 88 }, 89 self, 90 /* context= */ nullptr, 91 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames); 92 return method_found; 93 } 94 95 // TODO Remove 'require_deoptimizable' option once we have deoptimization through runtime frames. 96 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedFunction( 97 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method, jboolean require_deoptimizable) { 98 // Return false if this seems to not be an ART runtime. 99 if (Runtime::Current() == nullptr) { 100 return JNI_FALSE; 101 } 102 if (method == nullptr) { 103 env->ThrowNew(env->FindClass("java/lang/NullPointerException"), "method is null!"); 104 return JNI_FALSE; 105 } 106 jmethodID id = env->FromReflectedMethod(method); 107 if (id == nullptr) { 108 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to interpret method argument!"); 109 return JNI_FALSE; 110 } 111 { 112 ScopedObjectAccess soa(env); 113 ArtMethod* goal = jni::DecodeArtMethod(id); 114 bool is_interpreted; 115 if (!IsMethodInterpreted(soa.Self(), goal, require_deoptimizable, &is_interpreted)) { 116 env->ThrowNew(env->FindClass("java/lang/Error"), "Unable to find given method in stack!"); 117 return JNI_FALSE; 118 } 119 bool enters_interpreter = Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge( 120 goal->GetEntryPointFromQuickCompiledCode()); 121 return (is_interpreted || enters_interpreter); 122 } 123 } 124 125 // public static native void assertIsInterpreted(); 126 127 extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) { 128 if (asserts_enabled) { 129 CHECK(Java_Main_isInterpreted(env, klass)); 130 } 131 } 132 133 static jboolean IsManaged(JNIEnv* env, jclass, size_t level) { 134 ScopedObjectAccess soa(env); 135 NthCallerVisitor caller(soa.Self(), level, false); 136 caller.WalkStack(); 137 CHECK(caller.caller != nullptr); 138 return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE; 139 } 140 141 // public static native boolean isManaged(); 142 143 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) { 144 return IsManaged(env, cls, 1); 145 } 146 147 // public static native void assertIsManaged(); 148 149 extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) { 150 if (asserts_enabled) { 151 CHECK(Java_Main_isManaged(env, cls)); 152 } 153 } 154 155 // public static native boolean isCallerInterpreted(); 156 157 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerInterpreted(JNIEnv* env, jclass klass) { 158 return IsInterpreted(env, klass, 2); 159 } 160 161 // public static native void assertCallerIsInterpreted(); 162 163 extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass klass) { 164 if (asserts_enabled) { 165 CHECK(Java_Main_isCallerInterpreted(env, klass)); 166 } 167 } 168 169 // public static native boolean isCallerManaged(); 170 171 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerManaged(JNIEnv* env, jclass cls) { 172 return IsManaged(env, cls, 2); 173 } 174 175 // public static native void assertCallerIsManaged(); 176 177 extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) { 178 if (asserts_enabled) { 179 CHECK(Java_Main_isCallerManaged(env, cls)); 180 } 181 } 182 183 extern "C" JNIEXPORT jobject JNICALL Java_Main_getThisOfCaller( 184 JNIEnv* env, jclass cls ATTRIBUTE_UNUSED) { 185 ScopedObjectAccess soa(env); 186 std::unique_ptr<art::Context> context(art::Context::Create()); 187 jobject result = nullptr; 188 StackVisitor::WalkStack( 189 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) { 190 // Discard stubs and Main.getThisOfCaller. 191 if (stack_visitor->GetMethod() == nullptr || stack_visitor->GetMethod()->IsNative()) { 192 return true; 193 } 194 result = soa.AddLocalReference<jobject>(stack_visitor->GetThisObject()); 195 return false; 196 }, 197 soa.Self(), 198 context.get(), 199 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames); 200 return result; 201 } 202 203 } // namespace art 204