| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 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 | |
| Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
| Andreas Gampe | e5d2398 | 2019-01-08 10:34:26 -0800 | [diff] [blame] | 21 | #include "arch/context.h" |
| Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 22 | #include "base/mutex.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 23 | #include "dex/dex_file-inl.h" |
| Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 24 | #include "jni/jni_internal.h" |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
| 26 | #include "nth_caller_visitor.h" |
| Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 27 | #include "oat_file.h" |
| Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 28 | #include "oat_quick_method_header.h" |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 29 | #include "runtime.h" |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 30 | #include "scoped_thread_state_change-inl.h" |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 31 | #include "stack.h" |
| Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 32 | #include "thread-current-inl.h" |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
| Nicolas Geoffray | 1949baf | 2017-10-17 12:14:53 +0000 | [diff] [blame] | 36 | static bool asserts_enabled = true; |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 37 | |
| Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 38 | // public static native void disableStackFrameAsserts(); |
| 39 | // Note: to globally disable asserts in unsupported configurations. |
| 40 | |
| 41 | extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED, |
| 42 | jclass cls ATTRIBUTE_UNUSED) { |
| 43 | asserts_enabled = false; |
| 44 | } |
| 45 | |
| Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 46 | static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) { |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 47 | ScopedObjectAccess soa(env); |
| Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 48 | NthCallerVisitor caller(soa.Self(), level, false); |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 49 | caller.WalkStack(); |
| 50 | CHECK(caller.caller != nullptr); |
| Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 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; |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 57 | // public static native boolean isInterpreted(); |
| 58 | |
| 59 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) { |
| 60 | return IsInterpreted(env, klass, 1); |
| 61 | } |
| 62 | |
| Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 63 | // public static native boolean isInterpreted(int depth); |
| 64 | |
| 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 | |
| Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 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 | } |
| Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 98 | |
| 99 | // TODO Remove 'require_deoptimizable' option once we have deoptimization through runtime frames. |
| 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 | } |
| Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 115 | { |
| 116 | ScopedObjectAccess soa(env); |
| 117 | ArtMethod* goal = jni::DecodeArtMethod(id); |
| Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 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 | } |
| Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 123 | bool enters_interpreter = Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge( |
| 124 | goal->GetEntryPointFromQuickCompiledCode()); |
| Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 125 | return (is_interpreted || enters_interpreter); |
| Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 126 | } |
| Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 129 | // public static native void assertIsInterpreted(); |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 130 | |
| Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 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 | } |
| Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 135 | } |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 136 | |
| Mingyao Yang | a1e0367 | 2017-04-12 15:39:54 -0700 | [diff] [blame] | 137 | static jboolean IsManaged(JNIEnv* env, jclass, size_t level) { |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 138 | ScopedObjectAccess soa(env); |
| Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 139 | NthCallerVisitor caller(soa.Self(), level, false); |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 140 | caller.WalkStack(); |
| 141 | CHECK(caller.caller != nullptr); |
| Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 142 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE; |
| 143 | } |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 144 | |
| Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 145 | // public static native boolean isManaged(); |
| 146 | |
| 147 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) { |
| 148 | return IsManaged(env, cls, 1); |
| 149 | } |
| 150 | |
| Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 151 | // public static native void assertIsManaged(); |
| Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 152 | |
| Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 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 | } |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 159 | // public static native boolean isCallerInterpreted(); |
| 160 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 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()); |
| Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 191 | jobject result = nullptr; |
| 192 | StackVisitor::WalkStack( |
| 193 | [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| Artem Serov | 2808be8 | 2018-12-20 19:15:11 +0000 | [diff] [blame] | 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()))) { |
| Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 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; |
| Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 210 | } // namespace art |