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 | |
| 30 | // public static native void assertCallerIsInterpreted(); |
| 31 | |
| 32 | extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsInterpreted(JNIEnv* env, jclass) { |
| 33 | LOG(INFO) << "assertCallerIsInterpreted"; |
| 34 | |
| 35 | ScopedObjectAccess soa(env); |
| 36 | NthCallerVisitor caller(soa.Self(), 1, false); |
| 37 | caller.WalkStack(); |
| 38 | CHECK(caller.caller != nullptr); |
| 39 | LOG(INFO) << PrettyMethod(caller.caller); |
| 40 | CHECK(caller.GetCurrentShadowFrame() != nullptr); |
| 41 | } |
| 42 | |
| 43 | // public static native void assertCallerIsManaged(); |
| 44 | |
| 45 | extern "C" JNIEXPORT void JNICALL Java_Main_assertCallerIsManaged(JNIEnv* env, jclass cls) { |
| 46 | // Note: needs some smarts to not fail if there is no managed code, at all. |
| 47 | LOG(INFO) << "assertCallerIsManaged"; |
| 48 | |
| 49 | ScopedObjectAccess soa(env); |
| 50 | |
| 51 | mirror::Class* klass = soa.Decode<mirror::Class*>(cls); |
| 52 | const DexFile& dex_file = klass->GetDexFile(); |
| 53 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 54 | if (oat_dex_file == nullptr) { |
| 55 | // No oat file, this must be a test configuration that doesn't compile at all. Ignore that the |
| 56 | // result will be that we're running the interpreter. |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | NthCallerVisitor caller(soa.Self(), 1, false); |
| 61 | caller.WalkStack(); |
| 62 | CHECK(caller.caller != nullptr); |
| 63 | LOG(INFO) << PrettyMethod(caller.caller); |
| 64 | |
| 65 | if (caller.GetCurrentShadowFrame() == nullptr) { |
| 66 | // Not a shadow frame, this looks good. |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // This could be an interpret-only or a verify-at-runtime compilation, or a read-barrier variant, |
| 71 | // or... It's not really safe to just reject now. Let's look at the access flags. If the method |
| 72 | // was successfully verified, its access flags should be set to mark it preverified, except when |
| 73 | // we're running soft-fail tests. |
| 74 | if (Runtime::Current()->IsVerificationSoftFail()) { |
| 75 | // Soft-fail config. Everything should be running with interpreter access checks, potentially. |
| 76 | return; |
| 77 | } |
| 78 | CHECK(caller.caller->IsPreverified()); |
| 79 | } |
| 80 | |
| 81 | } // namespace art |