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"
24#include "scoped_thread_state_change.h"
25#include "stack.h"
26#include "thread-inl.h"
27
28namespace art {
29
30static bool asserts_enabled = true;
31
32// 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
40
41// public static native boolean isInterpreted();
42
43extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterpreted(JNIEnv* env, jclass) {
44  ScopedObjectAccess soa(env);
45  NthCallerVisitor caller(soa.Self(), 1, false);
46  caller.WalkStack();
47  CHECK(caller.caller != nullptr);
48  return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE;
49}
50
51// public static native void assertIsInterpreted();
52
53extern "C" JNIEXPORT void JNICALL Java_Main_assertIsInterpreted(JNIEnv* env, jclass klass) {
54  if (asserts_enabled) {
55    CHECK(Java_Main_isInterpreted(env, klass));
56  }
57}
58
59
60// public static native boolean isManaged();
61
62extern "C" JNIEXPORT jboolean JNICALL Java_Main_isManaged(JNIEnv* env, jclass cls) {
63  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.
71    return JNI_FALSE;
72  }
73
74  NthCallerVisitor caller(soa.Self(), 1, false);
75  caller.WalkStack();
76  CHECK(caller.caller != nullptr);
77
78  return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE;
79}
80
81// public static native void assertIsManaged();
82
83extern "C" JNIEXPORT void JNICALL Java_Main_assertIsManaged(JNIEnv* env, jclass cls) {
84  if (asserts_enabled) {
85    CHECK(Java_Main_isManaged(env, cls));
86  }
87}
88
89}  // namespace art
90