1 /*
2  * Copyright (C) 2014 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 "class_linker.h"
18 #include "dex_file-inl.h"
19 #include "gc/heap.h"
20 #include "gc/space/image_space.h"
21 #include "mirror/class-inl.h"
22 #include "runtime.h"
23 #include "scoped_thread_state_change.h"
24 #include "thread.h"
25 
26 namespace art {
27 
28 class NoPatchoatTest {
29  public:
getOatDexFile(jclass cls)30   static const OatFile::OatDexFile* getOatDexFile(jclass cls) {
31     ScopedObjectAccess soa(Thread::Current());
32     mirror::Class* klass = soa.Decode<mirror::Class*>(cls);
33     const DexFile& dex_file = klass->GetDexFile();
34     return dex_file.GetOatDexFile();
35   }
36 
isRelocationDeltaZero()37   static bool isRelocationDeltaZero() {
38     std::vector<gc::space::ImageSpace*> spaces =
39         Runtime::Current()->GetHeap()->GetBootImageSpaces();
40     return !spaces.empty() && spaces[0]->GetImageHeader().GetPatchDelta() == 0;
41   }
42 
hasExecutableOat(jclass cls)43   static bool hasExecutableOat(jclass cls) {
44     const OatFile::OatDexFile* oat_dex_file = getOatDexFile(cls);
45 
46     return oat_dex_file != nullptr && oat_dex_file->GetOatFile()->IsExecutable();
47   }
48 
needsRelocation(jclass cls)49   static bool needsRelocation(jclass cls) {
50     const OatFile::OatDexFile* oat_dex_file = getOatDexFile(cls);
51 
52     if (oat_dex_file == nullptr) {
53       return false;
54     }
55 
56     const OatFile* oat_file = oat_dex_file->GetOatFile();
57     return !oat_file->IsPic()
58         && CompilerFilter::IsBytecodeCompilationEnabled(oat_file->GetCompilerFilter());
59   }
60 };
61 
Java_Main_isRelocationDeltaZero(JNIEnv *,jclass)62 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isRelocationDeltaZero(JNIEnv*, jclass) {
63   return NoPatchoatTest::isRelocationDeltaZero();
64 }
65 
Java_Main_hasExecutableOat(JNIEnv *,jclass cls)66 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasExecutableOat(JNIEnv*, jclass cls) {
67   return NoPatchoatTest::hasExecutableOat(cls);
68 }
69 
Java_Main_needsRelocation(JNIEnv *,jclass cls)70 extern "C" JNIEXPORT jboolean JNICALL Java_Main_needsRelocation(JNIEnv*, jclass cls) {
71   return NoPatchoatTest::needsRelocation(cls);
72 }
73 
74 }  // namespace art
75