blob: 85ea1c8dd1742965bdec959f0ae8ed8e052d47c5 [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"
23#include "runtime.h"
24#include "scoped_thread_state_change.h"
25#include "stack.h"
26#include "thread-inl.h"
27
28namespace art {
29
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070030static bool asserts_enabled = true;
Andreas Gampea727e372015-08-25 09:22:37 -070031
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070032// public static native void disableStackFrameAsserts();
33// Note: to globally disable asserts in unsupported configurations.
34
35extern "C" JNIEXPORT void JNICALL Java_Main_disableStackFrameAsserts(JNIEnv* env ATTRIBUTE_UNUSED,
36 jclass cls ATTRIBUTE_UNUSED) {
37 asserts_enabled = false;
38}
39
Mingyao Yangf711f2c2016-05-23 12:29:39 -070040static jboolean IsInterpreted(JNIEnv* env, jclass, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -070041 ScopedObjectAccess soa(env);
Mingyao Yangf711f2c2016-05-23 12:29:39 -070042 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -070043 caller.WalkStack();
44 CHECK(caller.caller != nullptr);
Andreas Gampe89df7bf2015-09-30 13:13:21 -070045 return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE;
Andreas Gampea727e372015-08-25 09:22:37 -070046}
47
Mingyao Yangf711f2c2016-05-23 12:29:39 -070048// public static native boolean isInterpreted();
49
50extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass klass) {
51 return IsInterpreted(env, klass, 1);
52}
53
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070054// public static native void assertIsInterpreted();
Andreas Gampea727e372015-08-25 09:22:37 -070055
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070056extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
57 if (asserts_enabled) {
58 CHECK(Java_Main_isInterpreted(env, klass));
59 }
Andreas Gampe89df7bf2015-09-30 13:13:21 -070060}
Andreas Gampea727e372015-08-25 09:22:37 -070061
Mingyao Yangf711f2c2016-05-23 12:29:39 -070062static jboolean IsManaged(JNIEnv* env, jclass cls, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -070063 ScopedObjectAccess soa(env);
64
65 mirror::Class* klass = soa.Decode<mirror::Class*>(cls);
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 Gampe89df7bf2015-09-30 13:13:21 -070071 return JNI_FALSE;
Andreas Gampea727e372015-08-25 09:22:37 -070072 }
73
Mingyao Yangf711f2c2016-05-23 12:29:39 -070074 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -070075 caller.WalkStack();
76 CHECK(caller.caller != nullptr);
Andreas Gampea727e372015-08-25 09:22:37 -070077
Andreas Gampe89df7bf2015-09-30 13:13:21 -070078 return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
79}
Andreas Gampea727e372015-08-25 09:22:37 -070080
Mingyao Yangf711f2c2016-05-23 12:29:39 -070081// public static native boolean isManaged();
82
83extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
84 return IsManaged(env, cls, 1);
85}
86
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070087// public static native void assertIsManaged();
Andreas Gampe89df7bf2015-09-30 13:13:21 -070088
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070089extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
90 if (asserts_enabled) {
91 CHECK(Java_Main_isManaged(env, cls));
92 }
Andreas Gampea727e372015-08-25 09:22:37 -070093}
94
Mingyao Yangf711f2c2016-05-23 12:29:39 -070095// public static native boolean isCallerInterpreted();
96
97extern "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
103extern "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
111extern "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
117extern "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 Gampea727e372015-08-25 09:22:37 -0700123} // namespace art