1 /*
2  * Copyright (C) 2016 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 #ifndef ART_RUNTIME_IMAGE_INL_H_
18 #define ART_RUNTIME_IMAGE_INL_H_
19 
20 #include "image.h"
21 
22 #include "art_method.h"
23 #include "imt_conflict_table.h"
24 #include "imtable.h"
25 #include "mirror/object_array-inl.h"
26 #include "obj_ptr-inl.h"
27 #include "read_barrier-inl.h"
28 
29 namespace art {
30 
31 template <ReadBarrierOption kReadBarrierOption>
GetImageRoot(ImageRoot image_root)32 inline ObjPtr<mirror::Object> ImageHeader::GetImageRoot(ImageRoot image_root) const {
33   ObjPtr<mirror::ObjectArray<mirror::Object>> image_roots = GetImageRoots<kReadBarrierOption>();
34   return image_roots->Get<kVerifyNone, kReadBarrierOption>(static_cast<int32_t>(image_root));
35 }
36 
37 template <ReadBarrierOption kReadBarrierOption>
GetImageRoots()38 inline ObjPtr<mirror::ObjectArray<mirror::Object>> ImageHeader::GetImageRoots() const {
39   // Need a read barrier as it's not visited during root scan.
40   // Pass in the address of the local variable to the read barrier
41   // rather than image_roots_ because it won't move (asserted below)
42   // and it's a const member.
43   mirror::ObjectArray<mirror::Object>* image_roots =
44       reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(image_roots_);
45   mirror::ObjectArray<mirror::Object>* result =
46       ReadBarrier::BarrierForRoot<mirror::ObjectArray<mirror::Object>, kReadBarrierOption>(
47           &image_roots);
48   DCHECK_EQ(image_roots, result);
49   return image_roots;
50 }
51 
52 template <typename Visitor>
VisitPackedArtFields(const Visitor & visitor,uint8_t * base)53 inline void ImageHeader::VisitPackedArtFields(const Visitor& visitor, uint8_t* base) const {
54   const ImageSection& fields = GetFieldsSection();
55   for (size_t pos = 0u; pos < fields.Size(); ) {
56     auto* array = reinterpret_cast<LengthPrefixedArray<ArtField>*>(base + fields.Offset() + pos);
57     for (size_t i = 0u; i < array->size(); ++i) {
58       visitor(array->At(i, sizeof(ArtField)));
59     }
60     pos += array->ComputeSize(array->size());
61   }
62 }
63 
64 template <typename Visitor>
VisitPackedArtMethods(const Visitor & visitor,uint8_t * base,PointerSize pointer_size)65 inline void ImageHeader::VisitPackedArtMethods(const Visitor& visitor,
66                                                uint8_t* base,
67                                                PointerSize pointer_size) const {
68   const size_t method_alignment = ArtMethod::Alignment(pointer_size);
69   const size_t method_size = ArtMethod::Size(pointer_size);
70   const ImageSection& methods = GetMethodsSection();
71   for (size_t pos = 0u; pos < methods.Size(); ) {
72     auto* array = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(base + methods.Offset() + pos);
73     for (size_t i = 0u; i < array->size(); ++i) {
74       visitor(array->At(i, method_size, method_alignment));
75     }
76     pos += array->ComputeSize(array->size(), method_size, method_alignment);
77   }
78   const ImageSection& runtime_methods = GetRuntimeMethodsSection();
79   for (size_t pos = 0u; pos < runtime_methods.Size(); ) {
80     auto* method = reinterpret_cast<ArtMethod*>(base + runtime_methods.Offset() + pos);
81     visitor(*method);
82     pos += method_size;
83   }
84 }
85 
86 template <typename Visitor>
VisitPackedImTables(const Visitor & visitor,uint8_t * base,PointerSize pointer_size)87 inline void ImageHeader::VisitPackedImTables(const Visitor& visitor,
88                                              uint8_t* base,
89                                              PointerSize pointer_size) const {
90   const ImageSection& section = GetImTablesSection();
91   for (size_t pos = 0; pos < section.Size();) {
92     ImTable* imt = reinterpret_cast<ImTable*>(base + section.Offset() + pos);
93     for (size_t i = 0; i < ImTable::kSize; ++i) {
94       ArtMethod* orig = imt->Get(i, pointer_size);
95       ArtMethod* updated = visitor(orig);
96       if (updated != orig) {
97         imt->Set(i, updated, pointer_size);
98       }
99     }
100     pos += ImTable::SizeInBytes(pointer_size);
101   }
102 }
103 
104 template <typename Visitor>
VisitPackedImtConflictTables(const Visitor & visitor,uint8_t * base,PointerSize pointer_size)105 inline void ImageHeader::VisitPackedImtConflictTables(const Visitor& visitor,
106                                                       uint8_t* base,
107                                                       PointerSize pointer_size) const {
108   const ImageSection& section = GetIMTConflictTablesSection();
109   for (size_t pos = 0; pos < section.Size(); ) {
110     auto* table = reinterpret_cast<ImtConflictTable*>(base + section.Offset() + pos);
111     table->Visit([&visitor](const std::pair<ArtMethod*, ArtMethod*>& methods) {
112       return std::make_pair(visitor(methods.first), visitor(methods.second));
113     }, pointer_size);
114     pos += table->ComputeSize(pointer_size);
115   }
116 }
117 
118 }  // namespace art
119 
120 #endif  // ART_RUNTIME_IMAGE_INL_H_
121