blob: 4df2d470ecf2214d36688add029c1432abce4990 [file] [log] [blame]
Andreas Gampea727e372015-08-25 09:22:37 -07001/*
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 Marko97d7e1c2016-10-04 14:44:28 +010023#include "oat_file.h"
Andreas Gampea727e372015-08-25 09:22:37 -070024#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Andreas Gampea727e372015-08-25 09:22:37 -070026#include "stack.h"
27#include "thread-inl.h"
28
29namespace art {
30
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070031static bool asserts_enabled = true;
Andreas Gampea727e372015-08-25 09:22:37 -070032
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070033// public static native void disableStackFrameAsserts();
34// Note: to globally disable asserts in unsupported configurations.
35
36extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED,
37 jclass cls ATTRIBUTE_UNUSED) {
38 asserts_enabled = false;
39}
40
Mingyao Yangf711f2c2016-05-23 12:29:39 -070041static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -070042 ScopedObjectAccess soa(env);
Mingyao Yangf711f2c2016-05-23 12:29:39 -070043 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -070044 caller.WalkStack();
45 CHECK(caller.caller != nullptr);
Andreas Gampe89df7bf2015-09-30 13:13:21 -070046 return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE;
Andreas Gampea727e372015-08-25 09:22:37 -070047}
48
Mingyao Yangf711f2c2016-05-23 12:29:39 -070049// public static native boolean isInterpreted();
50
51extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) {
52 return IsInterpreted(env, klass, 1);
53}
54
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070055// public static native void assertIsInterpreted();
Andreas Gampea727e372015-08-25 09:22:37 -070056
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070057extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
58 if (asserts_enabled) {
59 CHECK(Java_Main_isInterpreted(env, klass));
60 }
Andreas Gampe89df7bf2015-09-30 13:13:21 -070061}
Andreas Gampea727e372015-08-25 09:22:37 -070062
Mingyao Yangf711f2c2016-05-23 12:29:39 -070063static jboolean IsManaged(JNIEnv* env, jclass cls, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -070064 ScopedObjectAccess soa(env);
65
Mathieu Chartier0795f232016-09-27 18:43:30 -070066 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
Andreas Gampea727e372015-08-25 09:22:37 -070067 const DexFile& dex_file = klass->GetDexFile();
68 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
69 if (oat_dex_file == nullptr) {
70 // No oat file, this must be a test configuration that doesn't compile at all. Ignore that the
71 // result will be that we're running the interpreter.
Andreas Gampe89df7bf2015-09-30 13:13:21 -070072 return JNI_FALSE;
Andreas Gampea727e372015-08-25 09:22:37 -070073 }
74
Mingyao Yangf711f2c2016-05-23 12:29:39 -070075 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -070076 caller.WalkStack();
77 CHECK(caller.caller != nullptr);
Andreas Gampea727e372015-08-25 09:22:37 -070078
Andreas Gampe89df7bf2015-09-30 13:13:21 -070079 return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
80}
Andreas Gampea727e372015-08-25 09:22:37 -070081
Mingyao Yangf711f2c2016-05-23 12:29:39 -070082// public static native boolean isManaged();
83
84extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
85 return IsManaged(env, cls, 1);
86}
87
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070088// public static native void assertIsManaged();
Andreas Gampe89df7bf2015-09-30 13:13:21 -070089
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070090extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
91 if (asserts_enabled) {
92 CHECK(Java_Main_isManaged(env, cls));
93 }
Andreas Gampea727e372015-08-25 09:22:37 -070094}
95
Mingyao Yangf711f2c2016-05-23 12:29:39 -070096// public static native boolean isCallerInterpreted();
97
98extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerInterpreted(JNIEnv* env, jclass klass) {
99 return IsInterpreted(env, klass, 2);
100}
101
102// public static native void assertCallerIsInterpreted();
103
104extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass klass) {
105 if (asserts_enabled) {
106 CHECK(Java_Main_isCallerInterpreted(env, klass));
107 }
108}
109
110// public static native boolean isCallerManaged();
111
112extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerManaged(JNIEnv* env, jclass cls) {
113 return IsManaged(env, cls, 2);
114}
115
116// public static native void assertCallerIsManaged();
117
118extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) {
119 if (asserts_enabled) {
120 CHECK(Java_Main_isCallerManaged(env, cls));
121 }
122}
123
Andreas Gampea727e372015-08-25 09:22:37 -0700124} // namespace art