blob: eefaabca9c79f89b755e69f289906675f4f38c79 [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
Alex Lightd8936da2016-11-28 16:24:32 -080055// public static native boolean isInterpreted(int depth);
56
57extern "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
66struct 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
90extern "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 Gampe0dfc9bc2015-09-30 17:13:59 -0700102// public static native void assertIsInterpreted();
Andreas Gampea727e372015-08-25 09:22:37 -0700103
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700104extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
105 if (asserts_enabled) {
106 CHECK(Java_Main_isInterpreted(env, klass));
107 }
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700108}
Andreas Gampea727e372015-08-25 09:22:37 -0700109
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700110static jboolean IsManaged(JNIEnv* env, jclass cls, size_t level) {
Andreas Gampea727e372015-08-25 09:22:37 -0700111 ScopedObjectAccess soa(env);
112
Mathieu Chartier0795f232016-09-27 18:43:30 -0700113 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
Andreas Gampea727e372015-08-25 09:22:37 -0700114 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 Gampe89df7bf2015-09-30 13:13:21 -0700119 return JNI_FALSE;
Andreas Gampea727e372015-08-25 09:22:37 -0700120 }
121
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700122 NthCallerVisitor caller(soa.Self(), level, false);
Andreas Gampea727e372015-08-25 09:22:37 -0700123 caller.WalkStack();
124 CHECK(caller.caller != nullptr);
Andreas Gampea727e372015-08-25 09:22:37 -0700125
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700126 return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
127}
Andreas Gampea727e372015-08-25 09:22:37 -0700128
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700129// public static native boolean isManaged();
130
131extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
132 return IsManaged(env, cls, 1);
133}
134
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700135// public static native void assertIsManaged();
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700136
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700137extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
138 if (asserts_enabled) {
139 CHECK(Java_Main_isManaged(env, cls));
140 }
Andreas Gampea727e372015-08-25 09:22:37 -0700141}
142
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700143// public static native boolean isCallerInterpreted();
144
145extern "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
151extern "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
159extern "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
165extern "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 Gampea727e372015-08-25 09:22:37 -0700171} // namespace art