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" |
| 23 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 24 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 25 | #include "stack.h" |
| 26 | #include "thread-inl.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 30 | static bool asserts_enabled = true; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 31 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 32 | // public static native void disableStackFrameAsserts(); |
| 33 | // Note: to globally disable asserts in unsupported configurations. |
| 34 | |
| 35 | extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED, |
| 36 | jclass cls ATTRIBUTE_UNUSED) { |
| 37 | asserts_enabled = false; |
| 38 | } |
| 39 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 40 | static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 41 | ScopedObjectAccess soa(env); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 42 | NthCallerVisitor caller(soa.Self(), level, false); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 43 | caller.WalkStack(); |
| 44 | CHECK(caller.caller != nullptr); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 45 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 48 | // public static native boolean isInterpreted(); |
| 49 | |
| 50 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) { |
| 51 | return IsInterpreted(env, klass, 1); |
| 52 | } |
| 53 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 54 | // public static native void assertIsInterpreted(); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 55 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 56 | extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) { |
| 57 | if (asserts_enabled) { |
| 58 | CHECK(Java_Main_isInterpreted(env, klass)); |
| 59 | } |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 60 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 61 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 62 | static jboolean IsManaged(JNIEnv* env, jclass cls, size_t level) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 63 | ScopedObjectAccess soa(env); |
| 64 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 65 | ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 66 | const DexFile& dex_file = klass->GetDexFile(); |
| 67 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 68 | if (oat_dex_file == nullptr) { |
| 69 | // No oat file, this must be a test configuration that doesn't compile at all. Ignore that the |
| 70 | // result will be that we're running the interpreter. |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 71 | return JNI_FALSE; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 74 | NthCallerVisitor caller(soa.Self(), level, false); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 75 | caller.WalkStack(); |
| 76 | CHECK(caller.caller != nullptr); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 77 | |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 78 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE; |
| 79 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 80 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 81 | // public static native boolean isManaged(); |
| 82 | |
| 83 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) { |
| 84 | return IsManaged(env, cls, 1); |
| 85 | } |
| 86 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 87 | // public static native void assertIsManaged(); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 88 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 89 | extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) { |
| 90 | if (asserts_enabled) { |
| 91 | CHECK(Java_Main_isManaged(env, cls)); |
| 92 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 95 | // public static native boolean isCallerInterpreted(); |
| 96 | |
| 97 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerInterpreted(JNIEnv* env, jclass klass) { |
| 98 | return IsInterpreted(env, klass, 2); |
| 99 | } |
| 100 | |
| 101 | // public static native void assertCallerIsInterpreted(); |
| 102 | |
| 103 | extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass klass) { |
| 104 | if (asserts_enabled) { |
| 105 | CHECK(Java_Main_isCallerInterpreted(env, klass)); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // public static native boolean isCallerManaged(); |
| 110 | |
| 111 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerManaged(JNIEnv* env, jclass cls) { |
| 112 | return IsManaged(env, cls, 2); |
| 113 | } |
| 114 | |
| 115 | // public static native void assertCallerIsManaged(); |
| 116 | |
| 117 | extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) { |
| 118 | if (asserts_enabled) { |
| 119 | CHECK(Java_Main_isCallerManaged(env, cls)); |
| 120 | } |
| 121 | } |
| 122 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 123 | } // namespace art |