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" |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 28 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 29 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 30 | #include "stack.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 31 | #include "thread-current-inl.h" |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Nicolas Geoffray | 1949baf | 2017-10-17 12:14:53 +0000 | [diff] [blame] | 35 | static bool asserts_enabled = true; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 36 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 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 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 45 | static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 46 | ScopedObjectAccess soa(env); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 47 | NthCallerVisitor caller(soa.Self(), level, false); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 48 | caller.WalkStack(); |
| 49 | CHECK(caller.caller != nullptr); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 50 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 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 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 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 | |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 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 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 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 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 111 | { |
| 112 | ScopedObjectAccess soa(env); |
| 113 | ArtMethod* goal = jni::DecodeArtMethod(id); |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 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 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 119 | bool enters_interpreter = Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge( |
| 120 | goal->GetEntryPointFromQuickCompiledCode()); |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 121 | return (is_interpreted || enters_interpreter); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 122 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 125 | // public static native void assertIsInterpreted(); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 126 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 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 | } |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 131 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 132 | |
Mingyao Yang | a1e0367 | 2017-04-12 15:39:54 -0700 | [diff] [blame] | 133 | static jboolean IsManaged(JNIEnv* env, jclass, size_t level) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 134 | ScopedObjectAccess soa(env); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 135 | NthCallerVisitor caller(soa.Self(), level, false); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 136 | caller.WalkStack(); |
| 137 | CHECK(caller.caller != nullptr); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 138 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE; |
| 139 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 140 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 147 | // public static native void assertIsManaged(); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 148 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 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 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 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 | |
Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 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()); |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 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; |
Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 203 | } // namespace art |