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" |
| 24 | #include "scoped_thread_state_change.h" |
| 25 | #include "stack.h" |
| 26 | #include "thread-inl.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame^] | 30 | // public static native boolean isCallerInterpreted(); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 31 | |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame^] | 32 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerInterpreted(JNIEnv* env, jclass) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 33 | ScopedObjectAccess soa(env); |
| 34 | NthCallerVisitor caller(soa.Self(), 1, false); |
| 35 | caller.WalkStack(); |
| 36 | CHECK(caller.caller != nullptr); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame^] | 37 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_TRUE : JNI_FALSE; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame^] | 40 | // public static native void assertCallerIsInterpreted(); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 41 | |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame^] | 42 | extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass klass) { |
| 43 | CHECK(Java_Main_isCallerInterpreted(env, klass)); |
| 44 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 45 | |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame^] | 46 | |
| 47 | // public static native boolean isCallerManaged(); |
| 48 | |
| 49 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isCallerManaged(JNIEnv* env, jclass cls) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 50 | ScopedObjectAccess soa(env); |
| 51 | |
| 52 | mirror::Class* klass = soa.Decode<mirror::Class*>(cls); |
| 53 | const DexFile& dex_file = klass->GetDexFile(); |
| 54 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 55 | if (oat_dex_file == nullptr) { |
| 56 | // No oat file, this must be a test configuration that doesn't compile at all. Ignore that the |
| 57 | // result will be that we're running the interpreter. |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame^] | 58 | return JNI_FALSE; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | NthCallerVisitor caller(soa.Self(), 1, false); |
| 62 | caller.WalkStack(); |
| 63 | CHECK(caller.caller != nullptr); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 64 | |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame^] | 65 | return caller.GetCurrentShadowFrame() != nullptr ? JNI_FALSE : JNI_TRUE; |
| 66 | } |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 67 | |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame^] | 68 | // public static native void assertCallerIsManaged(); |
| 69 | |
| 70 | extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) { |
| 71 | CHECK(Java_Main_isCallerManaged(env, cls)); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | } // namespace art |