blob: 0404e99bef719eed3c2ace20e34e569ea55038ce [file] [log] [blame]
Mathieu Chartier4a26f172016-01-26 14:26:18 -08001/*
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
Mathieu Chartiere42888f2016-04-14 10:49:19 -070022#include "art_method.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070023#include "imt_conflict_table.h"
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000024#include "imtable.h"
Vladimir Marko6834d342018-05-25 13:12:09 +010025#include "mirror/object_array-inl.h"
Vladimir Markoc13fbd82018-06-04 16:16:28 +010026#include "obj_ptr-inl.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010027#include "read_barrier-inl.h"
Mathieu Chartiere42888f2016-04-14 10:49:19 -070028
Mathieu Chartier4a26f172016-01-26 14:26:18 -080029namespace art {
30
31template <ReadBarrierOption kReadBarrierOption>
Vladimir Markoc13fbd82018-06-04 16:16:28 +010032inline ObjPtr<mirror::Object> ImageHeader::GetImageRoot(ImageRoot image_root) const {
33 ObjPtr<mirror::ObjectArray<mirror::Object>> image_roots = GetImageRoots<kReadBarrierOption>();
Mathieu Chartier4a26f172016-01-26 14:26:18 -080034 return image_roots->Get<kVerifyNone, kReadBarrierOption>(static_cast<int32_t>(image_root));
35}
36
37template <ReadBarrierOption kReadBarrierOption>
Vladimir Markoc13fbd82018-06-04 16:16:28 +010038inline ObjPtr<mirror::ObjectArray<mirror::Object>> ImageHeader::GetImageRoots() const {
Mathieu Chartier4a26f172016-01-26 14:26:18 -080039 // 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
Mathieu Chartier9d5956a2019-03-22 11:29:08 -070052template <typename Visitor>
53inline void ImageHeader::VisitPackedArtFields(const Visitor& visitor, uint8_t* base) const {
Vladimir Marko4df2d802018-09-27 16:42:44 +000054 const ImageSection& fields = GetFieldsSection();
Vladimir Markoafe14eb2018-10-01 12:11:14 +010055 for (size_t pos = 0u; pos < fields.Size(); ) {
Vladimir Marko4df2d802018-09-27 16:42:44 +000056 auto* array = reinterpret_cast<LengthPrefixedArray<ArtField>*>(base + fields.Offset() + pos);
Vladimir Markoafe14eb2018-10-01 12:11:14 +010057 for (size_t i = 0u; i < array->size(); ++i) {
Mathieu Chartier9d5956a2019-03-22 11:29:08 -070058 visitor(array->At(i, sizeof(ArtField)));
Vladimir Marko4df2d802018-09-27 16:42:44 +000059 }
60 pos += array->ComputeSize(array->size());
61 }
62}
63
Mathieu Chartier9d5956a2019-03-22 11:29:08 -070064template <typename Visitor>
65inline void ImageHeader::VisitPackedArtMethods(const Visitor& visitor,
66 uint8_t* base,
67 PointerSize pointer_size) const {
Vladimir Marko4df2d802018-09-27 16:42:44 +000068 const size_t method_alignment = ArtMethod::Alignment(pointer_size);
69 const size_t method_size = ArtMethod::Size(pointer_size);
70 const ImageSection& methods = GetMethodsSection();
Vladimir Markoafe14eb2018-10-01 12:11:14 +010071 for (size_t pos = 0u; pos < methods.Size(); ) {
Vladimir Marko4df2d802018-09-27 16:42:44 +000072 auto* array = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(base + methods.Offset() + pos);
Vladimir Markoafe14eb2018-10-01 12:11:14 +010073 for (size_t i = 0u; i < array->size(); ++i) {
Mathieu Chartier9d5956a2019-03-22 11:29:08 -070074 visitor(array->At(i, method_size, method_alignment));
Vladimir Marko4df2d802018-09-27 16:42:44 +000075 }
76 pos += array->ComputeSize(array->size(), method_size, method_alignment);
77 }
78 const ImageSection& runtime_methods = GetRuntimeMethodsSection();
Vladimir Markoafe14eb2018-10-01 12:11:14 +010079 for (size_t pos = 0u; pos < runtime_methods.Size(); ) {
Vladimir Marko4df2d802018-09-27 16:42:44 +000080 auto* method = reinterpret_cast<ArtMethod*>(base + runtime_methods.Offset() + pos);
Mathieu Chartier9d5956a2019-03-22 11:29:08 -070081 visitor(*method);
Vladimir Marko4df2d802018-09-27 16:42:44 +000082 pos += method_size;
83 }
84}
85
Mathieu Chartiere42888f2016-04-14 10:49:19 -070086template <typename Visitor>
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000087inline void ImageHeader::VisitPackedImTables(const Visitor& visitor,
88 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -070089 PointerSize pointer_size) const {
Vladimir Markocd87c3e2017-09-05 13:11:57 +010090 const ImageSection& section = GetImTablesSection();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000091 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
104template <typename Visitor>
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700105inline void ImageHeader::VisitPackedImtConflictTables(const Visitor& visitor,
106 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700107 PointerSize pointer_size) const {
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100108 const ImageSection& section = GetIMTConflictTablesSection();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700109 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
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800118} // namespace art
119
120#endif // ART_RUNTIME_IMAGE_INL_H_