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 | |
| 19 | #include "base/logging.h" |
| 20 | #include "dex_file-inl.h" |
| 21 | #include "mirror/class-inl.h" |
| 22 | #include "nth_caller_visitor.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 23 | #include "oat_file.h" |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 24 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 26 | #include "stack.h" |
| 27 | #include "thread-inl.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 31 | static bool asserts_enabled = true; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 32 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 33 | // public static native void disableStackFrameAsserts(); |
| 34 | // Note: to globally disable asserts in unsupported configurations. |
| 35 | |
| 36 | extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED, |
| 37 | jclass cls ATTRIBUTE_UNUSED) { |
| 38 | asserts_enabled = false; |
| 39 | } |
| 40 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 41 | static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 42 | ScopedObjectAccess soa(env); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 43 | NthCallerVisitor caller(soa.Self(), level, false); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 44 | caller.WalkStack(); |
| 45 | CHECK(caller.caller != nullptr); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 46 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 49 | // public static native boolean isInterpreted(); |
| 50 | |
| 51 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) { |
| 52 | return IsInterpreted(env, klass, 1); |
| 53 | } |
| 54 | |
Alex Light | d8936da | 2016-11-28 16:24:32 -0800 | [diff] [blame^] | 55 | // public static native boolean isInterpreted(int depth); |
| 56 | |
| 57 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedAt(JNIEnv* env, |
| 58 | jclass klass, |
| 59 | jint depth) { |
| 60 | return IsInterpreted(env, klass, depth); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | // public static native boolean isInterpretedFunction(String smali); |
| 65 | |
| 66 | struct MethodIsInterpretedVisitor : public StackVisitor { |
| 67 | public: |
| 68 | MethodIsInterpretedVisitor(Thread* thread, std::string shorty) |
| 69 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 70 | shorty_(shorty), |
| 71 | method_is_interpreted_(false) {} |
| 72 | |
| 73 | virtual bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 74 | if (!GetMethod()->IsRuntimeMethod() && shorty_ == GetMethod()->GetShorty()) { |
| 75 | method_is_interpreted_ = IsShadowFrame(); |
| 76 | return false; |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool IsInterpreted() { |
| 82 | return method_is_interpreted_; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | const std::string shorty_; |
| 87 | bool method_is_interpreted_; |
| 88 | }; |
| 89 | |
| 90 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpretedFunction(JNIEnv* env, |
| 91 | jclass klass ATTRIBUTE_UNUSED, |
| 92 | jstring name) { |
| 93 | if (Runtime::Current() == nullptr) { |
| 94 | return JNI_TRUE; |
| 95 | } |
| 96 | ScopedObjectAccess soa(env); |
| 97 | MethodIsInterpretedVisitor v(soa.Self(), soa.Decode<mirror::String>(name)->ToModifiedUtf8()); |
| 98 | v.WalkStack(); |
| 99 | return v.IsInterpreted(); |
| 100 | } |
| 101 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 102 | // public static native void assertIsInterpreted(); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 103 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 104 | extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) { |
| 105 | if (asserts_enabled) { |
| 106 | CHECK(Java_Main_isInterpreted(env, klass)); |
| 107 | } |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 108 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 109 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 110 | static jboolean IsManaged(JNIEnv* env, jclass cls, size_t level) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 111 | ScopedObjectAccess soa(env); |
| 112 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 113 | ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 114 | const DexFile& dex_file = klass->GetDexFile(); |
| 115 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 116 | if (oat_dex_file == nullptr) { |
| 117 | // No oat file, this must be a test configuration that doesn't compile at all. Ignore that the |
| 118 | // result will be that we're running the interpreter. |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 119 | return JNI_FALSE; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 122 | NthCallerVisitor caller(soa.Self(), level, false); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 123 | caller.WalkStack(); |
| 124 | CHECK(caller.caller != nullptr); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 125 | |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 126 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE; |
| 127 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 128 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 129 | // public static native boolean isManaged(); |
| 130 | |
| 131 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) { |
| 132 | return IsManaged(env, cls, 1); |
| 133 | } |
| 134 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 135 | // public static native void assertIsManaged(); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 136 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 137 | extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) { |
| 138 | if (asserts_enabled) { |
| 139 | CHECK(Java_Main_isManaged(env, cls)); |
| 140 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 143 | // public static native boolean isCallerInterpreted(); |
| 144 | |
| 145 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerInterpreted(JNIEnv* env, jclass klass) { |
| 146 | return IsInterpreted(env, klass, 2); |
| 147 | } |
| 148 | |
| 149 | // public static native void assertCallerIsInterpreted(); |
| 150 | |
| 151 | extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass klass) { |
| 152 | if (asserts_enabled) { |
| 153 | CHECK(Java_Main_isCallerInterpreted(env, klass)); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // public static native boolean isCallerManaged(); |
| 158 | |
| 159 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerManaged(JNIEnv* env, jclass cls) { |
| 160 | return IsManaged(env, cls, 2); |
| 161 | } |
| 162 | |
| 163 | // public static native void assertCallerIsManaged(); |
| 164 | |
| 165 | extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) { |
| 166 | if (asserts_enabled) { |
| 167 | CHECK(Java_Main_isCallerManaged(env, cls)); |
| 168 | } |
| 169 | } |
| 170 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 171 | } // namespace art |