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 | |
| 21 | #include "base/mutex.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 22 | #include "dex/dex_file-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 23 | #include "jni/jni_internal.h" |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 24 | #include "mirror/class-inl.h" |
| 25 | #include "nth_caller_visitor.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 26 | #include "oat_file.h" |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 27 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 28 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 29 | #include "stack.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 30 | #include "thread-current-inl.h" |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Nicolas Geoffray | 1949baf | 2017-10-17 12:14:53 +0000 | [diff] [blame] | 34 | static bool asserts_enabled = true; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 35 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 36 | // public static native void disableStackFrameAsserts(); |
| 37 | // Note: to globally disable asserts in unsupported configurations. |
| 38 | |
| 39 | extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED, |
| 40 | jclass cls ATTRIBUTE_UNUSED) { |
| 41 | asserts_enabled = false; |
| 42 | } |
| 43 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 44 | static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 45 | ScopedObjectAccess soa(env); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 46 | NthCallerVisitor caller(soa.Self(), level, false); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 47 | caller.WalkStack(); |
| 48 | CHECK(caller.caller != nullptr); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 49 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 52 | // public static native boolean isInterpreted(); |
| 53 | |
| 54 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) { |
| 55 | return IsInterpreted(env, klass, 1); |
| 56 | } |
| 57 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 58 | // public static native boolean isInterpreted(int depth); |
| 59 | |
| 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: |
| 72 | 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 | |
| 80 | 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 | |
| 90 | bool IsInterpreted() { |
| 91 | return method_is_interpreted_; |
| 92 | } |
| 93 | |
| 94 | 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. |
| 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 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 141 | // public static native void assertIsInterpreted(); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 142 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 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 | } |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 147 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 148 | |
Mingyao Yang | a1e0367 | 2017-04-12 15:39:54 -0700 | [diff] [blame] | 149 | static jboolean IsManaged(JNIEnv* env, jclass, size_t level) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 150 | ScopedObjectAccess soa(env); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 151 | NthCallerVisitor caller(soa.Self(), level, false); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 152 | caller.WalkStack(); |
| 153 | CHECK(caller.caller != nullptr); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 154 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE; |
| 155 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 156 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 157 | // public static native boolean isManaged(); |
| 158 | |
| 159 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) { |
| 160 | return IsManaged(env, cls, 1); |
| 161 | } |
| 162 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 163 | // public static native void assertIsManaged(); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 164 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 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 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 171 | // public static native boolean isCallerInterpreted(); |
| 172 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 199 | } // namespace art |