1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "image_space.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <sys/statvfs.h>
20*795d594fSAndroid Build Coastguard Worker #include <sys/types.h>
21*795d594fSAndroid Build Coastguard Worker #include <unistd.h>
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Worker #include <array>
24*795d594fSAndroid Build Coastguard Worker #include <memory>
25*795d594fSAndroid Build Coastguard Worker #include <optional>
26*795d594fSAndroid Build Coastguard Worker #include <random>
27*795d594fSAndroid Build Coastguard Worker #include <string>
28*795d594fSAndroid Build Coastguard Worker #include <vector>
29*795d594fSAndroid Build Coastguard Worker
30*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
31*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
32*795d594fSAndroid Build Coastguard Worker #include "android-base/strings.h"
33*795d594fSAndroid Build Coastguard Worker #include "android-base/unique_fd.h"
34*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
35*795d594fSAndroid Build Coastguard Worker #include "art_field-inl.h"
36*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
37*795d594fSAndroid Build Coastguard Worker #include "base/array_ref.h"
38*795d594fSAndroid Build Coastguard Worker #include "base/bit_memory_region.h"
39*795d594fSAndroid Build Coastguard Worker #include "base/callee_save_type.h"
40*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
41*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
42*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
43*795d594fSAndroid Build Coastguard Worker #include "base/memfd.h"
44*795d594fSAndroid Build Coastguard Worker #include "base/os.h"
45*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
46*795d594fSAndroid Build Coastguard Worker #include "base/stl_util.h"
47*795d594fSAndroid Build Coastguard Worker #include "base/systrace.h"
48*795d594fSAndroid Build Coastguard Worker #include "base/time_utils.h"
49*795d594fSAndroid Build Coastguard Worker #include "base/utils.h"
50*795d594fSAndroid Build Coastguard Worker #include "class_root-inl.h"
51*795d594fSAndroid Build Coastguard Worker #include "dex/art_dex_file_loader.h"
52*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_loader.h"
53*795d594fSAndroid Build Coastguard Worker #include "exec_utils.h"
54*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/space_bitmap-inl.h"
55*795d594fSAndroid Build Coastguard Worker #include "gc/task_processor.h"
56*795d594fSAndroid Build Coastguard Worker #include "intern_table-inl.h"
57*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
58*795d594fSAndroid Build Coastguard Worker #include "mirror/executable-inl.h"
59*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
60*795d594fSAndroid Build Coastguard Worker #include "mirror/object-refvisitor-inl.h"
61*795d594fSAndroid Build Coastguard Worker #include "mirror/var_handle.h"
62*795d594fSAndroid Build Coastguard Worker #include "oat/image-inl.h"
63*795d594fSAndroid Build Coastguard Worker #include "oat/image.h"
64*795d594fSAndroid Build Coastguard Worker #include "oat/oat.h"
65*795d594fSAndroid Build Coastguard Worker #include "oat/oat_file.h"
66*795d594fSAndroid Build Coastguard Worker #include "profile/profile_compilation_info.h"
67*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
68*795d594fSAndroid Build Coastguard Worker #include "runtime_globals.h"
69*795d594fSAndroid Build Coastguard Worker #include "space-inl.h"
70*795d594fSAndroid Build Coastguard Worker
71*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
72*795d594fSAndroid Build Coastguard Worker namespace gc {
73*795d594fSAndroid Build Coastguard Worker namespace space {
74*795d594fSAndroid Build Coastguard Worker
75*795d594fSAndroid Build Coastguard Worker namespace {
76*795d594fSAndroid Build Coastguard Worker
77*795d594fSAndroid Build Coastguard Worker using ::android::base::Join;
78*795d594fSAndroid Build Coastguard Worker using ::android::base::StringAppendF;
79*795d594fSAndroid Build Coastguard Worker using ::android::base::StringPrintf;
80*795d594fSAndroid Build Coastguard Worker
81*795d594fSAndroid Build Coastguard Worker // We do not allow the boot image and extensions to take more than 1GiB. They are
82*795d594fSAndroid Build Coastguard Worker // supposed to be much smaller and allocating more that this would likely fail anyway.
83*795d594fSAndroid Build Coastguard Worker static constexpr size_t kMaxTotalImageReservationSize = 1 * GB;
84*795d594fSAndroid Build Coastguard Worker
85*795d594fSAndroid Build Coastguard Worker } // namespace
86*795d594fSAndroid Build Coastguard Worker
87*795d594fSAndroid Build Coastguard Worker Atomic<uint32_t> ImageSpace::bitmap_index_(0);
88*795d594fSAndroid Build Coastguard Worker
ImageSpace(const std::string & image_filename,const char * image_location,const std::vector<std::string> & profile_files,MemMap && mem_map,accounting::ContinuousSpaceBitmap && live_bitmap,uint8_t * end)89*795d594fSAndroid Build Coastguard Worker ImageSpace::ImageSpace(const std::string& image_filename,
90*795d594fSAndroid Build Coastguard Worker const char* image_location,
91*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>& profile_files,
92*795d594fSAndroid Build Coastguard Worker MemMap&& mem_map,
93*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap&& live_bitmap,
94*795d594fSAndroid Build Coastguard Worker uint8_t* end)
95*795d594fSAndroid Build Coastguard Worker : MemMapSpace(image_filename,
96*795d594fSAndroid Build Coastguard Worker std::move(mem_map),
97*795d594fSAndroid Build Coastguard Worker mem_map.Begin(),
98*795d594fSAndroid Build Coastguard Worker end,
99*795d594fSAndroid Build Coastguard Worker end,
100*795d594fSAndroid Build Coastguard Worker kGcRetentionPolicyNeverCollect),
101*795d594fSAndroid Build Coastguard Worker live_bitmap_(std::move(live_bitmap)),
102*795d594fSAndroid Build Coastguard Worker oat_file_non_owned_(nullptr),
103*795d594fSAndroid Build Coastguard Worker image_location_(image_location),
104*795d594fSAndroid Build Coastguard Worker profile_files_(profile_files) {
105*795d594fSAndroid Build Coastguard Worker DCHECK(live_bitmap_.IsValid());
106*795d594fSAndroid Build Coastguard Worker }
107*795d594fSAndroid Build Coastguard Worker
ChooseRelocationOffsetDelta(int32_t min_delta,int32_t max_delta)108*795d594fSAndroid Build Coastguard Worker static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
109*795d594fSAndroid Build Coastguard Worker CHECK_ALIGNED(min_delta, kElfSegmentAlignment);
110*795d594fSAndroid Build Coastguard Worker CHECK_ALIGNED(max_delta, kElfSegmentAlignment);
111*795d594fSAndroid Build Coastguard Worker CHECK_LT(min_delta, max_delta);
112*795d594fSAndroid Build Coastguard Worker
113*795d594fSAndroid Build Coastguard Worker int32_t r = GetRandomNumber<int32_t>(min_delta, max_delta);
114*795d594fSAndroid Build Coastguard Worker if (r % 2 == 0) {
115*795d594fSAndroid Build Coastguard Worker r = RoundUp(r, kElfSegmentAlignment);
116*795d594fSAndroid Build Coastguard Worker } else {
117*795d594fSAndroid Build Coastguard Worker r = RoundDown(r, kElfSegmentAlignment);
118*795d594fSAndroid Build Coastguard Worker }
119*795d594fSAndroid Build Coastguard Worker CHECK_LE(min_delta, r);
120*795d594fSAndroid Build Coastguard Worker CHECK_GE(max_delta, r);
121*795d594fSAndroid Build Coastguard Worker CHECK_ALIGNED(r, kElfSegmentAlignment);
122*795d594fSAndroid Build Coastguard Worker return r;
123*795d594fSAndroid Build Coastguard Worker }
124*795d594fSAndroid Build Coastguard Worker
ChooseRelocationOffsetDelta()125*795d594fSAndroid Build Coastguard Worker static int32_t ChooseRelocationOffsetDelta() {
126*795d594fSAndroid Build Coastguard Worker return ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, ART_BASE_ADDRESS_MAX_DELTA);
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker
FindImageFilenameImpl(const char * image_location,const InstructionSet image_isa,bool * has_system,std::string * system_filename)129*795d594fSAndroid Build Coastguard Worker static bool FindImageFilenameImpl(const char* image_location,
130*795d594fSAndroid Build Coastguard Worker const InstructionSet image_isa,
131*795d594fSAndroid Build Coastguard Worker bool* has_system,
132*795d594fSAndroid Build Coastguard Worker std::string* system_filename) {
133*795d594fSAndroid Build Coastguard Worker *has_system = false;
134*795d594fSAndroid Build Coastguard Worker
135*795d594fSAndroid Build Coastguard Worker // image_location = /system/framework/boot.art
136*795d594fSAndroid Build Coastguard Worker // system_image_location = /system/framework/<image_isa>/boot.art
137*795d594fSAndroid Build Coastguard Worker std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
138*795d594fSAndroid Build Coastguard Worker if (OS::FileExists(system_image_filename.c_str())) {
139*795d594fSAndroid Build Coastguard Worker *system_filename = system_image_filename;
140*795d594fSAndroid Build Coastguard Worker *has_system = true;
141*795d594fSAndroid Build Coastguard Worker }
142*795d594fSAndroid Build Coastguard Worker
143*795d594fSAndroid Build Coastguard Worker return *has_system;
144*795d594fSAndroid Build Coastguard Worker }
145*795d594fSAndroid Build Coastguard Worker
FindImageFilename(const char * image_location,const InstructionSet image_isa,std::string * system_filename,bool * has_system)146*795d594fSAndroid Build Coastguard Worker bool ImageSpace::FindImageFilename(const char* image_location,
147*795d594fSAndroid Build Coastguard Worker const InstructionSet image_isa,
148*795d594fSAndroid Build Coastguard Worker std::string* system_filename,
149*795d594fSAndroid Build Coastguard Worker bool* has_system) {
150*795d594fSAndroid Build Coastguard Worker return FindImageFilenameImpl(image_location,
151*795d594fSAndroid Build Coastguard Worker image_isa,
152*795d594fSAndroid Build Coastguard Worker has_system,
153*795d594fSAndroid Build Coastguard Worker system_filename);
154*795d594fSAndroid Build Coastguard Worker }
155*795d594fSAndroid Build Coastguard Worker
ReadSpecificImageHeader(File * image_file,const char * file_description,ImageHeader * image_header,std::string * error_msg)156*795d594fSAndroid Build Coastguard Worker static bool ReadSpecificImageHeader(File* image_file,
157*795d594fSAndroid Build Coastguard Worker const char* file_description,
158*795d594fSAndroid Build Coastguard Worker /*out*/ImageHeader* image_header,
159*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
160*795d594fSAndroid Build Coastguard Worker if (!image_file->PreadFully(image_header, sizeof(ImageHeader), /*offset=*/ 0)) {
161*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Unable to read image header from \"%s\"", file_description);
162*795d594fSAndroid Build Coastguard Worker return false;
163*795d594fSAndroid Build Coastguard Worker }
164*795d594fSAndroid Build Coastguard Worker if (!image_header->IsValid()) {
165*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Image header from \"%s\" is invalid", file_description);
166*795d594fSAndroid Build Coastguard Worker return false;
167*795d594fSAndroid Build Coastguard Worker }
168*795d594fSAndroid Build Coastguard Worker return true;
169*795d594fSAndroid Build Coastguard Worker }
170*795d594fSAndroid Build Coastguard Worker
ReadSpecificImageHeader(const char * filename,ImageHeader * image_header,std::string * error_msg)171*795d594fSAndroid Build Coastguard Worker static bool ReadSpecificImageHeader(const char* filename,
172*795d594fSAndroid Build Coastguard Worker /*out*/ImageHeader* image_header,
173*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
174*795d594fSAndroid Build Coastguard Worker std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
175*795d594fSAndroid Build Coastguard Worker if (image_file.get() == nullptr) {
176*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Unable to open file \"%s\" for reading image header", filename);
177*795d594fSAndroid Build Coastguard Worker return false;
178*795d594fSAndroid Build Coastguard Worker }
179*795d594fSAndroid Build Coastguard Worker return ReadSpecificImageHeader(image_file.get(), filename, image_header, error_msg);
180*795d594fSAndroid Build Coastguard Worker }
181*795d594fSAndroid Build Coastguard Worker
ReadSpecificImageHeader(const char * filename,std::string * error_msg)182*795d594fSAndroid Build Coastguard Worker static std::unique_ptr<ImageHeader> ReadSpecificImageHeader(const char* filename,
183*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
184*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ImageHeader> hdr(new ImageHeader);
185*795d594fSAndroid Build Coastguard Worker if (!ReadSpecificImageHeader(filename, hdr.get(), error_msg)) {
186*795d594fSAndroid Build Coastguard Worker return nullptr;
187*795d594fSAndroid Build Coastguard Worker }
188*795d594fSAndroid Build Coastguard Worker return hdr;
189*795d594fSAndroid Build Coastguard Worker }
190*795d594fSAndroid Build Coastguard Worker
VerifyImageAllocations()191*795d594fSAndroid Build Coastguard Worker void ImageSpace::VerifyImageAllocations() {
192*795d594fSAndroid Build Coastguard Worker uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
193*795d594fSAndroid Build Coastguard Worker while (current < End()) {
194*795d594fSAndroid Build Coastguard Worker CHECK_ALIGNED(current, kObjectAlignment);
195*795d594fSAndroid Build Coastguard Worker auto* obj = reinterpret_cast<mirror::Object*>(current);
196*795d594fSAndroid Build Coastguard Worker CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
197*795d594fSAndroid Build Coastguard Worker CHECK(live_bitmap_.Test(obj)) << obj->PrettyTypeOf();
198*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
199*795d594fSAndroid Build Coastguard Worker obj->AssertReadBarrierState();
200*795d594fSAndroid Build Coastguard Worker }
201*795d594fSAndroid Build Coastguard Worker current += RoundUp(obj->SizeOf(), kObjectAlignment);
202*795d594fSAndroid Build Coastguard Worker }
203*795d594fSAndroid Build Coastguard Worker }
204*795d594fSAndroid Build Coastguard Worker
205*795d594fSAndroid Build Coastguard Worker // Helper class for relocating from one range of memory to another.
206*795d594fSAndroid Build Coastguard Worker class RelocationRange {
207*795d594fSAndroid Build Coastguard Worker public:
208*795d594fSAndroid Build Coastguard Worker RelocationRange(const RelocationRange&) = default;
RelocationRange(uintptr_t source,uintptr_t dest,uintptr_t length)209*795d594fSAndroid Build Coastguard Worker RelocationRange(uintptr_t source, uintptr_t dest, uintptr_t length)
210*795d594fSAndroid Build Coastguard Worker : source_(source),
211*795d594fSAndroid Build Coastguard Worker dest_(dest),
212*795d594fSAndroid Build Coastguard Worker length_(length) {}
213*795d594fSAndroid Build Coastguard Worker
InSource(uintptr_t address) const214*795d594fSAndroid Build Coastguard Worker bool InSource(uintptr_t address) const {
215*795d594fSAndroid Build Coastguard Worker return address - source_ < length_;
216*795d594fSAndroid Build Coastguard Worker }
217*795d594fSAndroid Build Coastguard Worker
InDest(const void * dest) const218*795d594fSAndroid Build Coastguard Worker bool InDest(const void* dest) const {
219*795d594fSAndroid Build Coastguard Worker return InDest(reinterpret_cast<uintptr_t>(dest));
220*795d594fSAndroid Build Coastguard Worker }
221*795d594fSAndroid Build Coastguard Worker
InDest(uintptr_t address) const222*795d594fSAndroid Build Coastguard Worker bool InDest(uintptr_t address) const {
223*795d594fSAndroid Build Coastguard Worker return address - dest_ < length_;
224*795d594fSAndroid Build Coastguard Worker }
225*795d594fSAndroid Build Coastguard Worker
226*795d594fSAndroid Build Coastguard Worker // Translate a source address to the destination space.
ToDest(uintptr_t address) const227*795d594fSAndroid Build Coastguard Worker uintptr_t ToDest(uintptr_t address) const {
228*795d594fSAndroid Build Coastguard Worker DCHECK(InSource(address));
229*795d594fSAndroid Build Coastguard Worker return address + Delta();
230*795d594fSAndroid Build Coastguard Worker }
231*795d594fSAndroid Build Coastguard Worker
232*795d594fSAndroid Build Coastguard Worker template <typename T>
ToDest(T * src) const233*795d594fSAndroid Build Coastguard Worker T* ToDest(T* src) const {
234*795d594fSAndroid Build Coastguard Worker return reinterpret_cast<T*>(ToDest(reinterpret_cast<uintptr_t>(src)));
235*795d594fSAndroid Build Coastguard Worker }
236*795d594fSAndroid Build Coastguard Worker
237*795d594fSAndroid Build Coastguard Worker // Returns the delta between the dest from the source.
Delta() const238*795d594fSAndroid Build Coastguard Worker uintptr_t Delta() const {
239*795d594fSAndroid Build Coastguard Worker return dest_ - source_;
240*795d594fSAndroid Build Coastguard Worker }
241*795d594fSAndroid Build Coastguard Worker
Source() const242*795d594fSAndroid Build Coastguard Worker uintptr_t Source() const {
243*795d594fSAndroid Build Coastguard Worker return source_;
244*795d594fSAndroid Build Coastguard Worker }
245*795d594fSAndroid Build Coastguard Worker
Dest() const246*795d594fSAndroid Build Coastguard Worker uintptr_t Dest() const {
247*795d594fSAndroid Build Coastguard Worker return dest_;
248*795d594fSAndroid Build Coastguard Worker }
249*795d594fSAndroid Build Coastguard Worker
Length() const250*795d594fSAndroid Build Coastguard Worker uintptr_t Length() const {
251*795d594fSAndroid Build Coastguard Worker return length_;
252*795d594fSAndroid Build Coastguard Worker }
253*795d594fSAndroid Build Coastguard Worker
254*795d594fSAndroid Build Coastguard Worker private:
255*795d594fSAndroid Build Coastguard Worker const uintptr_t source_;
256*795d594fSAndroid Build Coastguard Worker const uintptr_t dest_;
257*795d594fSAndroid Build Coastguard Worker const uintptr_t length_;
258*795d594fSAndroid Build Coastguard Worker };
259*795d594fSAndroid Build Coastguard Worker
operator <<(std::ostream & os,const RelocationRange & reloc)260*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const RelocationRange& reloc) {
261*795d594fSAndroid Build Coastguard Worker return os << "(" << reinterpret_cast<const void*>(reloc.Source()) << "-"
262*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<const void*>(reloc.Source() + reloc.Length()) << ")->("
263*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<const void*>(reloc.Dest()) << "-"
264*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<const void*>(reloc.Dest() + reloc.Length()) << ")";
265*795d594fSAndroid Build Coastguard Worker }
266*795d594fSAndroid Build Coastguard Worker
267*795d594fSAndroid Build Coastguard Worker template <PointerSize kPointerSize, typename HeapVisitor, typename NativeVisitor>
268*795d594fSAndroid Build Coastguard Worker class ImageSpace::PatchObjectVisitor final {
269*795d594fSAndroid Build Coastguard Worker public:
PatchObjectVisitor(HeapVisitor heap_visitor,NativeVisitor native_visitor)270*795d594fSAndroid Build Coastguard Worker explicit PatchObjectVisitor(HeapVisitor heap_visitor, NativeVisitor native_visitor)
271*795d594fSAndroid Build Coastguard Worker : heap_visitor_(heap_visitor), native_visitor_(native_visitor) {}
272*795d594fSAndroid Build Coastguard Worker
VisitClass(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Class> class_class)273*795d594fSAndroid Build Coastguard Worker void VisitClass(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Class> class_class)
274*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
275*795d594fSAndroid Build Coastguard Worker // A mirror::Class object consists of
276*795d594fSAndroid Build Coastguard Worker // - instance fields inherited from j.l.Object,
277*795d594fSAndroid Build Coastguard Worker // - instance fields inherited from j.l.Class,
278*795d594fSAndroid Build Coastguard Worker // - embedded tables (vtable, interface method table),
279*795d594fSAndroid Build Coastguard Worker // - static fields of the class itself.
280*795d594fSAndroid Build Coastguard Worker // The reference fields are at the start of each field section (this is how the
281*795d594fSAndroid Build Coastguard Worker // ClassLinker orders fields; except when that would create a gap between superclass
282*795d594fSAndroid Build Coastguard Worker // fields and the first reference of the subclass due to alignment, it can be filled
283*795d594fSAndroid Build Coastguard Worker // with smaller fields - but that's not the case for j.l.Object and j.l.Class).
284*795d594fSAndroid Build Coastguard Worker
285*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(klass.Ptr(), kObjectAlignment);
286*795d594fSAndroid Build Coastguard Worker static_assert(IsAligned<kHeapReferenceSize>(kObjectAlignment), "Object alignment check.");
287*795d594fSAndroid Build Coastguard Worker // First, patch the `klass->klass_`, known to be a reference to the j.l.Class.class.
288*795d594fSAndroid Build Coastguard Worker // This should be the only reference field in j.l.Object and we assert that below.
289*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(class_class,
290*795d594fSAndroid Build Coastguard Worker heap_visitor_(klass->GetClass<kVerifyNone, kWithoutReadBarrier>()));
291*795d594fSAndroid Build Coastguard Worker klass->SetFieldObjectWithoutWriteBarrier<
292*795d594fSAndroid Build Coastguard Worker /*kTransactionActive=*/ false,
293*795d594fSAndroid Build Coastguard Worker /*kCheckTransaction=*/ true,
294*795d594fSAndroid Build Coastguard Worker kVerifyNone>(mirror::Object::ClassOffset(), class_class);
295*795d594fSAndroid Build Coastguard Worker // Then patch the reference instance fields described by j.l.Class.class.
296*795d594fSAndroid Build Coastguard Worker // Use the sizeof(Object) to determine where these reference fields start;
297*795d594fSAndroid Build Coastguard Worker // this is the same as `class_class->GetFirstReferenceInstanceFieldOffset()`
298*795d594fSAndroid Build Coastguard Worker // after patching but the j.l.Class may not have been patched yet.
299*795d594fSAndroid Build Coastguard Worker size_t num_reference_instance_fields = class_class->NumReferenceInstanceFields<kVerifyNone>();
300*795d594fSAndroid Build Coastguard Worker DCHECK_NE(num_reference_instance_fields, 0u);
301*795d594fSAndroid Build Coastguard Worker static_assert(IsAligned<kHeapReferenceSize>(sizeof(mirror::Object)), "Size alignment check.");
302*795d594fSAndroid Build Coastguard Worker MemberOffset instance_field_offset(sizeof(mirror::Object));
303*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i != num_reference_instance_fields; ++i) {
304*795d594fSAndroid Build Coastguard Worker PatchReferenceField(klass, instance_field_offset);
305*795d594fSAndroid Build Coastguard Worker static_assert(sizeof(mirror::HeapReference<mirror::Object>) == kHeapReferenceSize,
306*795d594fSAndroid Build Coastguard Worker "Heap reference sizes equality check.");
307*795d594fSAndroid Build Coastguard Worker instance_field_offset =
308*795d594fSAndroid Build Coastguard Worker MemberOffset(instance_field_offset.Uint32Value() + kHeapReferenceSize);
309*795d594fSAndroid Build Coastguard Worker }
310*795d594fSAndroid Build Coastguard Worker // Now that we have patched the `super_class_`, if this is the j.l.Class.class,
311*795d594fSAndroid Build Coastguard Worker // we can get a reference to j.l.Object.class and assert that it has only one
312*795d594fSAndroid Build Coastguard Worker // reference instance field (the `klass_` patched above).
313*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && klass == class_class) {
314*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> object_class =
315*795d594fSAndroid Build Coastguard Worker klass->GetSuperClass<kVerifyNone, kWithoutReadBarrier>();
316*795d594fSAndroid Build Coastguard Worker CHECK_EQ(object_class->NumReferenceInstanceFields<kVerifyNone>(), 1u);
317*795d594fSAndroid Build Coastguard Worker }
318*795d594fSAndroid Build Coastguard Worker // Then patch static fields.
319*795d594fSAndroid Build Coastguard Worker size_t num_reference_static_fields = klass->NumReferenceStaticFields<kVerifyNone>();
320*795d594fSAndroid Build Coastguard Worker if (num_reference_static_fields != 0u) {
321*795d594fSAndroid Build Coastguard Worker MemberOffset static_field_offset =
322*795d594fSAndroid Build Coastguard Worker klass->GetFirstReferenceStaticFieldOffset<kVerifyNone>(kPointerSize);
323*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i != num_reference_static_fields; ++i) {
324*795d594fSAndroid Build Coastguard Worker PatchReferenceField(klass, static_field_offset);
325*795d594fSAndroid Build Coastguard Worker static_assert(sizeof(mirror::HeapReference<mirror::Object>) == kHeapReferenceSize,
326*795d594fSAndroid Build Coastguard Worker "Heap reference sizes equality check.");
327*795d594fSAndroid Build Coastguard Worker static_field_offset =
328*795d594fSAndroid Build Coastguard Worker MemberOffset(static_field_offset.Uint32Value() + kHeapReferenceSize);
329*795d594fSAndroid Build Coastguard Worker }
330*795d594fSAndroid Build Coastguard Worker }
331*795d594fSAndroid Build Coastguard Worker // Then patch native pointers.
332*795d594fSAndroid Build Coastguard Worker klass->FixupNativePointers<kVerifyNone>(klass.Ptr(), kPointerSize, *this);
333*795d594fSAndroid Build Coastguard Worker }
334*795d594fSAndroid Build Coastguard Worker
335*795d594fSAndroid Build Coastguard Worker template <typename T>
operator ()(T * ptr,void ** dest_addr) const336*795d594fSAndroid Build Coastguard Worker T* operator()(T* ptr, [[maybe_unused]] void** dest_addr) const {
337*795d594fSAndroid Build Coastguard Worker return (ptr != nullptr) ? native_visitor_(ptr) : nullptr;
338*795d594fSAndroid Build Coastguard Worker }
339*795d594fSAndroid Build Coastguard Worker
VisitPointerArray(ObjPtr<mirror::PointerArray> pointer_array)340*795d594fSAndroid Build Coastguard Worker void VisitPointerArray(ObjPtr<mirror::PointerArray> pointer_array)
341*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
342*795d594fSAndroid Build Coastguard Worker // Fully patch the pointer array, including the `klass_` field.
343*795d594fSAndroid Build Coastguard Worker PatchReferenceField</*kMayBeNull=*/ false>(pointer_array, mirror::Object::ClassOffset());
344*795d594fSAndroid Build Coastguard Worker
345*795d594fSAndroid Build Coastguard Worker int32_t length = pointer_array->GetLength<kVerifyNone>();
346*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0; i != length; ++i) {
347*795d594fSAndroid Build Coastguard Worker ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(
348*795d594fSAndroid Build Coastguard Worker pointer_array->ElementAddress<kVerifyNone>(i, kPointerSize));
349*795d594fSAndroid Build Coastguard Worker PatchNativePointer</*kMayBeNull=*/ false>(method_entry);
350*795d594fSAndroid Build Coastguard Worker }
351*795d594fSAndroid Build Coastguard Worker }
352*795d594fSAndroid Build Coastguard Worker
VisitObject(mirror::Object * object)353*795d594fSAndroid Build Coastguard Worker void VisitObject(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) {
354*795d594fSAndroid Build Coastguard Worker // Visit all reference fields.
355*795d594fSAndroid Build Coastguard Worker object->VisitReferences</*kVisitNativeRoots=*/ false,
356*795d594fSAndroid Build Coastguard Worker kVerifyNone,
357*795d594fSAndroid Build Coastguard Worker kWithoutReadBarrier>(*this, *this);
358*795d594fSAndroid Build Coastguard Worker // This function should not be called for classes.
359*795d594fSAndroid Build Coastguard Worker DCHECK(!object->IsClass<kVerifyNone>());
360*795d594fSAndroid Build Coastguard Worker }
361*795d594fSAndroid Build Coastguard Worker
362*795d594fSAndroid Build Coastguard Worker // Visitor for VisitReferences().
operator ()(ObjPtr<mirror::Object> object,MemberOffset field_offset,bool is_static) const363*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(ObjPtr<mirror::Object> object,
364*795d594fSAndroid Build Coastguard Worker MemberOffset field_offset,
365*795d594fSAndroid Build Coastguard Worker bool is_static)
366*795d594fSAndroid Build Coastguard Worker const REQUIRES_SHARED(Locks::mutator_lock_) {
367*795d594fSAndroid Build Coastguard Worker DCHECK(!is_static);
368*795d594fSAndroid Build Coastguard Worker PatchReferenceField(object, field_offset);
369*795d594fSAndroid Build Coastguard Worker }
370*795d594fSAndroid Build Coastguard Worker // Visitor for VisitReferences(), java.lang.ref.Reference case.
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const371*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const
372*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
373*795d594fSAndroid Build Coastguard Worker DCHECK(klass->IsTypeOfReferenceClass());
374*795d594fSAndroid Build Coastguard Worker this->operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false);
375*795d594fSAndroid Build Coastguard Worker }
376*795d594fSAndroid Build Coastguard Worker // Ignore class native roots; not called from VisitReferences() for kVisitNativeRoots == false.
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const377*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(
378*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] mirror::CompressedReference<mirror::Object>* root) const {}
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const379*795d594fSAndroid Build Coastguard Worker void VisitRoot([[maybe_unused]] mirror::CompressedReference<mirror::Object>* root) const {}
380*795d594fSAndroid Build Coastguard Worker
VisitNativeDexCacheArray(mirror::NativeArray<T> * array)381*795d594fSAndroid Build Coastguard Worker template <typename T> void VisitNativeDexCacheArray(mirror::NativeArray<T>* array)
382*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
383*795d594fSAndroid Build Coastguard Worker if (array == nullptr) {
384*795d594fSAndroid Build Coastguard Worker return;
385*795d594fSAndroid Build Coastguard Worker }
386*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(array, static_cast<size_t>(kPointerSize));
387*795d594fSAndroid Build Coastguard Worker uint32_t size = (kPointerSize == PointerSize::k32)
388*795d594fSAndroid Build Coastguard Worker ? reinterpret_cast<uint32_t*>(array)[-1]
389*795d594fSAndroid Build Coastguard Worker : dchecked_integral_cast<uint32_t>(reinterpret_cast<uint64_t*>(array)[-1]);
390*795d594fSAndroid Build Coastguard Worker for (uint32_t i = 0; i < size; ++i) {
391*795d594fSAndroid Build Coastguard Worker PatchNativePointer(array->GetPtrEntryPtrSize(i, kPointerSize));
392*795d594fSAndroid Build Coastguard Worker }
393*795d594fSAndroid Build Coastguard Worker }
394*795d594fSAndroid Build Coastguard Worker
VisitGcRootDexCacheArray(mirror::GcRootArray<T> * array)395*795d594fSAndroid Build Coastguard Worker template <typename T> void VisitGcRootDexCacheArray(mirror::GcRootArray<T>* array)
396*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
397*795d594fSAndroid Build Coastguard Worker if (array == nullptr) {
398*795d594fSAndroid Build Coastguard Worker return;
399*795d594fSAndroid Build Coastguard Worker }
400*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(array, sizeof(GcRoot<T>));
401*795d594fSAndroid Build Coastguard Worker static_assert(sizeof(GcRoot<T>) == sizeof(uint32_t));
402*795d594fSAndroid Build Coastguard Worker uint32_t size = reinterpret_cast<uint32_t*>(array)[-1];
403*795d594fSAndroid Build Coastguard Worker for (uint32_t i = 0; i < size; ++i) {
404*795d594fSAndroid Build Coastguard Worker PatchGcRoot(array->GetGcRootAddress(i));
405*795d594fSAndroid Build Coastguard Worker }
406*795d594fSAndroid Build Coastguard Worker }
407*795d594fSAndroid Build Coastguard Worker
VisitDexCacheArrays(ObjPtr<mirror::DexCache> dex_cache)408*795d594fSAndroid Build Coastguard Worker void VisitDexCacheArrays(ObjPtr<mirror::DexCache> dex_cache)
409*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
410*795d594fSAndroid Build Coastguard Worker mirror::NativeArray<ArtMethod>* old_resolved_methods = dex_cache->GetResolvedMethodsArray();
411*795d594fSAndroid Build Coastguard Worker if (old_resolved_methods != nullptr) {
412*795d594fSAndroid Build Coastguard Worker mirror::NativeArray<ArtMethod>* resolved_methods = native_visitor_(old_resolved_methods);
413*795d594fSAndroid Build Coastguard Worker dex_cache->SetResolvedMethodsArray(resolved_methods);
414*795d594fSAndroid Build Coastguard Worker VisitNativeDexCacheArray(resolved_methods);
415*795d594fSAndroid Build Coastguard Worker }
416*795d594fSAndroid Build Coastguard Worker
417*795d594fSAndroid Build Coastguard Worker mirror::NativeArray<ArtField>* old_resolved_fields = dex_cache->GetResolvedFieldsArray();
418*795d594fSAndroid Build Coastguard Worker if (old_resolved_fields != nullptr) {
419*795d594fSAndroid Build Coastguard Worker mirror::NativeArray<ArtField>* resolved_fields = native_visitor_(old_resolved_fields);
420*795d594fSAndroid Build Coastguard Worker dex_cache->SetResolvedFieldsArray(resolved_fields);
421*795d594fSAndroid Build Coastguard Worker VisitNativeDexCacheArray(resolved_fields);
422*795d594fSAndroid Build Coastguard Worker }
423*795d594fSAndroid Build Coastguard Worker
424*795d594fSAndroid Build Coastguard Worker mirror::GcRootArray<mirror::String>* old_strings = dex_cache->GetStringsArray();
425*795d594fSAndroid Build Coastguard Worker if (old_strings != nullptr) {
426*795d594fSAndroid Build Coastguard Worker mirror::GcRootArray<mirror::String>* strings = native_visitor_(old_strings);
427*795d594fSAndroid Build Coastguard Worker dex_cache->SetStringsArray(strings);
428*795d594fSAndroid Build Coastguard Worker VisitGcRootDexCacheArray(strings);
429*795d594fSAndroid Build Coastguard Worker }
430*795d594fSAndroid Build Coastguard Worker
431*795d594fSAndroid Build Coastguard Worker mirror::GcRootArray<mirror::Class>* old_types = dex_cache->GetResolvedTypesArray();
432*795d594fSAndroid Build Coastguard Worker if (old_types != nullptr) {
433*795d594fSAndroid Build Coastguard Worker mirror::GcRootArray<mirror::Class>* types = native_visitor_(old_types);
434*795d594fSAndroid Build Coastguard Worker dex_cache->SetResolvedTypesArray(types);
435*795d594fSAndroid Build Coastguard Worker VisitGcRootDexCacheArray(types);
436*795d594fSAndroid Build Coastguard Worker }
437*795d594fSAndroid Build Coastguard Worker }
438*795d594fSAndroid Build Coastguard Worker
439*795d594fSAndroid Build Coastguard Worker template <bool kMayBeNull = true, typename T>
PatchGcRoot(GcRoot<T> * root) const440*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void PatchGcRoot(/*inout*/GcRoot<T>* root) const
441*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
442*795d594fSAndroid Build Coastguard Worker static_assert(sizeof(GcRoot<mirror::Class*>) == sizeof(uint32_t), "GcRoot size check");
443*795d594fSAndroid Build Coastguard Worker T* old_value = root->template Read<kWithoutReadBarrier>();
444*795d594fSAndroid Build Coastguard Worker DCHECK(kMayBeNull || old_value != nullptr);
445*795d594fSAndroid Build Coastguard Worker if (!kMayBeNull || old_value != nullptr) {
446*795d594fSAndroid Build Coastguard Worker *root = GcRoot<T>(heap_visitor_(old_value));
447*795d594fSAndroid Build Coastguard Worker }
448*795d594fSAndroid Build Coastguard Worker }
449*795d594fSAndroid Build Coastguard Worker
450*795d594fSAndroid Build Coastguard Worker template <bool kMayBeNull = true, typename T>
PatchNativePointer(T ** entry) const451*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void PatchNativePointer(/*inout*/T** entry) const {
452*795d594fSAndroid Build Coastguard Worker if (kPointerSize == PointerSize::k64) {
453*795d594fSAndroid Build Coastguard Worker uint64_t* raw_entry = reinterpret_cast<uint64_t*>(entry);
454*795d594fSAndroid Build Coastguard Worker T* old_value = reinterpret_cast64<T*>(*raw_entry);
455*795d594fSAndroid Build Coastguard Worker DCHECK(kMayBeNull || old_value != nullptr);
456*795d594fSAndroid Build Coastguard Worker if (!kMayBeNull || old_value != nullptr) {
457*795d594fSAndroid Build Coastguard Worker T* new_value = native_visitor_(old_value);
458*795d594fSAndroid Build Coastguard Worker *raw_entry = reinterpret_cast64<uint64_t>(new_value);
459*795d594fSAndroid Build Coastguard Worker }
460*795d594fSAndroid Build Coastguard Worker } else {
461*795d594fSAndroid Build Coastguard Worker uint32_t* raw_entry = reinterpret_cast<uint32_t*>(entry);
462*795d594fSAndroid Build Coastguard Worker T* old_value = reinterpret_cast32<T*>(*raw_entry);
463*795d594fSAndroid Build Coastguard Worker DCHECK(kMayBeNull || old_value != nullptr);
464*795d594fSAndroid Build Coastguard Worker if (!kMayBeNull || old_value != nullptr) {
465*795d594fSAndroid Build Coastguard Worker T* new_value = native_visitor_(old_value);
466*795d594fSAndroid Build Coastguard Worker *raw_entry = reinterpret_cast32<uint32_t>(new_value);
467*795d594fSAndroid Build Coastguard Worker }
468*795d594fSAndroid Build Coastguard Worker }
469*795d594fSAndroid Build Coastguard Worker }
470*795d594fSAndroid Build Coastguard Worker
471*795d594fSAndroid Build Coastguard Worker template <bool kMayBeNull = true>
PatchReferenceField(ObjPtr<mirror::Object> object,MemberOffset offset) const472*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void PatchReferenceField(ObjPtr<mirror::Object> object, MemberOffset offset) const
473*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
474*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> old_value =
475*795d594fSAndroid Build Coastguard Worker object->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset);
476*795d594fSAndroid Build Coastguard Worker DCHECK(kMayBeNull || old_value != nullptr);
477*795d594fSAndroid Build Coastguard Worker if (!kMayBeNull || old_value != nullptr) {
478*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> new_value = heap_visitor_(old_value.Ptr());
479*795d594fSAndroid Build Coastguard Worker object->SetFieldObjectWithoutWriteBarrier</*kTransactionActive=*/ false,
480*795d594fSAndroid Build Coastguard Worker /*kCheckTransaction=*/ true,
481*795d594fSAndroid Build Coastguard Worker kVerifyNone>(offset, new_value);
482*795d594fSAndroid Build Coastguard Worker }
483*795d594fSAndroid Build Coastguard Worker }
484*795d594fSAndroid Build Coastguard Worker
485*795d594fSAndroid Build Coastguard Worker private:
486*795d594fSAndroid Build Coastguard Worker // Heap objects visitor.
487*795d594fSAndroid Build Coastguard Worker HeapVisitor heap_visitor_;
488*795d594fSAndroid Build Coastguard Worker
489*795d594fSAndroid Build Coastguard Worker // Native objects visitor.
490*795d594fSAndroid Build Coastguard Worker NativeVisitor native_visitor_;
491*795d594fSAndroid Build Coastguard Worker };
492*795d594fSAndroid Build Coastguard Worker
493*795d594fSAndroid Build Coastguard Worker template <typename ReferenceVisitor>
494*795d594fSAndroid Build Coastguard Worker class ImageSpace::ClassTableVisitor final {
495*795d594fSAndroid Build Coastguard Worker public:
ClassTableVisitor(const ReferenceVisitor & reference_visitor)496*795d594fSAndroid Build Coastguard Worker explicit ClassTableVisitor(const ReferenceVisitor& reference_visitor)
497*795d594fSAndroid Build Coastguard Worker : reference_visitor_(reference_visitor) {}
498*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const499*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
500*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
501*795d594fSAndroid Build Coastguard Worker DCHECK(root->AsMirrorPtr() != nullptr);
502*795d594fSAndroid Build Coastguard Worker root->Assign(reference_visitor_(root->AsMirrorPtr()));
503*795d594fSAndroid Build Coastguard Worker }
504*795d594fSAndroid Build Coastguard Worker
505*795d594fSAndroid Build Coastguard Worker private:
506*795d594fSAndroid Build Coastguard Worker ReferenceVisitor reference_visitor_;
507*795d594fSAndroid Build Coastguard Worker };
508*795d594fSAndroid Build Coastguard Worker
509*795d594fSAndroid Build Coastguard Worker class ImageSpace::RemapInternedStringsVisitor {
510*795d594fSAndroid Build Coastguard Worker public:
RemapInternedStringsVisitor(const SafeMap<mirror::String *,mirror::String * > & intern_remap)511*795d594fSAndroid Build Coastguard Worker explicit RemapInternedStringsVisitor(
512*795d594fSAndroid Build Coastguard Worker const SafeMap<mirror::String*, mirror::String*>& intern_remap)
513*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_)
514*795d594fSAndroid Build Coastguard Worker : intern_remap_(intern_remap),
515*795d594fSAndroid Build Coastguard Worker string_class_(GetStringClass()) {}
516*795d594fSAndroid Build Coastguard Worker
517*795d594fSAndroid Build Coastguard Worker // Visitor for VisitReferences().
operator ()(ObjPtr<mirror::Object> object,MemberOffset field_offset,bool is_static) const518*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(ObjPtr<mirror::Object> object,
519*795d594fSAndroid Build Coastguard Worker MemberOffset field_offset,
520*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] bool is_static) const
521*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
522*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> old_value =
523*795d594fSAndroid Build Coastguard Worker object->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(field_offset);
524*795d594fSAndroid Build Coastguard Worker if (old_value != nullptr &&
525*795d594fSAndroid Build Coastguard Worker old_value->GetClass<kVerifyNone, kWithoutReadBarrier>() == string_class_) {
526*795d594fSAndroid Build Coastguard Worker auto it = intern_remap_.find(old_value->AsString().Ptr());
527*795d594fSAndroid Build Coastguard Worker if (it != intern_remap_.end()) {
528*795d594fSAndroid Build Coastguard Worker mirror::String* new_value = it->second;
529*795d594fSAndroid Build Coastguard Worker object->SetFieldObjectWithoutWriteBarrier</*kTransactionActive=*/ false,
530*795d594fSAndroid Build Coastguard Worker /*kCheckTransaction=*/ true,
531*795d594fSAndroid Build Coastguard Worker kVerifyNone>(field_offset, new_value);
532*795d594fSAndroid Build Coastguard Worker }
533*795d594fSAndroid Build Coastguard Worker }
534*795d594fSAndroid Build Coastguard Worker }
535*795d594fSAndroid Build Coastguard Worker // Visitor for VisitReferences(), java.lang.ref.Reference case.
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const536*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const
537*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
538*795d594fSAndroid Build Coastguard Worker DCHECK(klass->IsTypeOfReferenceClass());
539*795d594fSAndroid Build Coastguard Worker this->operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false);
540*795d594fSAndroid Build Coastguard Worker }
541*795d594fSAndroid Build Coastguard Worker // Ignore class native roots; not called from VisitReferences() for kVisitNativeRoots == false.
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const542*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(
543*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] mirror::CompressedReference<mirror::Object>* root) const {}
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const544*795d594fSAndroid Build Coastguard Worker void VisitRoot([[maybe_unused]] mirror::CompressedReference<mirror::Object>* root) const {}
545*795d594fSAndroid Build Coastguard Worker
546*795d594fSAndroid Build Coastguard Worker private:
GetStringClass()547*795d594fSAndroid Build Coastguard Worker mirror::Class* GetStringClass() REQUIRES_SHARED(Locks::mutator_lock_) {
548*795d594fSAndroid Build Coastguard Worker DCHECK(!intern_remap_.empty());
549*795d594fSAndroid Build Coastguard Worker return intern_remap_.begin()->first->GetClass<kVerifyNone, kWithoutReadBarrier>();
550*795d594fSAndroid Build Coastguard Worker }
551*795d594fSAndroid Build Coastguard Worker
552*795d594fSAndroid Build Coastguard Worker const SafeMap<mirror::String*, mirror::String*>& intern_remap_;
553*795d594fSAndroid Build Coastguard Worker mirror::Class* const string_class_;
554*795d594fSAndroid Build Coastguard Worker };
555*795d594fSAndroid Build Coastguard Worker
556*795d594fSAndroid Build Coastguard Worker // Helper class encapsulating loading, so we can access private ImageSpace members (this is a
557*795d594fSAndroid Build Coastguard Worker // nested class), but not declare functions in the header.
558*795d594fSAndroid Build Coastguard Worker class ImageSpace::Loader {
559*795d594fSAndroid Build Coastguard Worker public:
InitAppImage(const char * image_filename,const char * image_location,const OatFile * oat_file,ArrayRef<ImageSpace * const> boot_image_spaces,std::string * error_msg)560*795d594fSAndroid Build Coastguard Worker static std::unique_ptr<ImageSpace> InitAppImage(const char* image_filename,
561*795d594fSAndroid Build Coastguard Worker const char* image_location,
562*795d594fSAndroid Build Coastguard Worker const OatFile* oat_file,
563*795d594fSAndroid Build Coastguard Worker ArrayRef<ImageSpace* const> boot_image_spaces,
564*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg)
565*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::mutator_lock_) {
566*795d594fSAndroid Build Coastguard Worker TimingLogger logger(__PRETTY_FUNCTION__, /*precise=*/ true, VLOG_IS_ON(image));
567*795d594fSAndroid Build Coastguard Worker
568*795d594fSAndroid Build Coastguard Worker if (gPageSize != kMinPageSize) {
569*795d594fSAndroid Build Coastguard Worker *error_msg = "Loading app image is only supported on devices with 4K page size";
570*795d594fSAndroid Build Coastguard Worker return nullptr;
571*795d594fSAndroid Build Coastguard Worker }
572*795d594fSAndroid Build Coastguard Worker
573*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ImageSpace> space = Init(image_filename,
574*795d594fSAndroid Build Coastguard Worker image_location,
575*795d594fSAndroid Build Coastguard Worker &logger,
576*795d594fSAndroid Build Coastguard Worker /*image_reservation=*/ nullptr,
577*795d594fSAndroid Build Coastguard Worker error_msg);
578*795d594fSAndroid Build Coastguard Worker if (space != nullptr) {
579*795d594fSAndroid Build Coastguard Worker space->oat_file_non_owned_ = oat_file;
580*795d594fSAndroid Build Coastguard Worker const ImageHeader& image_header = space->GetImageHeader();
581*795d594fSAndroid Build Coastguard Worker
582*795d594fSAndroid Build Coastguard Worker // Check the oat file checksum.
583*795d594fSAndroid Build Coastguard Worker const uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
584*795d594fSAndroid Build Coastguard Worker const uint32_t image_oat_checksum = image_header.GetOatChecksum();
585*795d594fSAndroid Build Coastguard Worker // Note image_oat_checksum is 0 for images generated by the runtime.
586*795d594fSAndroid Build Coastguard Worker if (image_oat_checksum != 0u && oat_checksum != image_oat_checksum) {
587*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Oat checksum 0x%x does not match the image one 0x%x in image %s",
588*795d594fSAndroid Build Coastguard Worker oat_checksum,
589*795d594fSAndroid Build Coastguard Worker image_oat_checksum,
590*795d594fSAndroid Build Coastguard Worker image_filename);
591*795d594fSAndroid Build Coastguard Worker return nullptr;
592*795d594fSAndroid Build Coastguard Worker }
593*795d594fSAndroid Build Coastguard Worker size_t boot_image_space_dependencies;
594*795d594fSAndroid Build Coastguard Worker if (!ValidateBootImageChecksum(image_filename,
595*795d594fSAndroid Build Coastguard Worker image_header,
596*795d594fSAndroid Build Coastguard Worker oat_file,
597*795d594fSAndroid Build Coastguard Worker boot_image_spaces,
598*795d594fSAndroid Build Coastguard Worker &boot_image_space_dependencies,
599*795d594fSAndroid Build Coastguard Worker error_msg)) {
600*795d594fSAndroid Build Coastguard Worker DCHECK(!error_msg->empty());
601*795d594fSAndroid Build Coastguard Worker return nullptr;
602*795d594fSAndroid Build Coastguard Worker }
603*795d594fSAndroid Build Coastguard Worker
604*795d594fSAndroid Build Coastguard Worker uint32_t expected_reservation_size = RoundUp(image_header.GetImageSize(),
605*795d594fSAndroid Build Coastguard Worker kElfSegmentAlignment);
606*795d594fSAndroid Build Coastguard Worker if (!CheckImageReservationSize(*space, expected_reservation_size, error_msg) ||
607*795d594fSAndroid Build Coastguard Worker !CheckImageComponentCount(*space, /*expected_component_count=*/ 1u, error_msg)) {
608*795d594fSAndroid Build Coastguard Worker return nullptr;
609*795d594fSAndroid Build Coastguard Worker }
610*795d594fSAndroid Build Coastguard Worker
611*795d594fSAndroid Build Coastguard Worker {
612*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("RelocateImage", &logger);
613*795d594fSAndroid Build Coastguard Worker const PointerSize pointer_size = image_header.GetPointerSize();
614*795d594fSAndroid Build Coastguard Worker uint32_t boot_image_begin =
615*795d594fSAndroid Build Coastguard Worker reinterpret_cast32<uint32_t>(boot_image_spaces.front()->Begin());
616*795d594fSAndroid Build Coastguard Worker bool result;
617*795d594fSAndroid Build Coastguard Worker if (pointer_size == PointerSize::k64) {
618*795d594fSAndroid Build Coastguard Worker result = RelocateInPlace<PointerSize::k64>(boot_image_begin,
619*795d594fSAndroid Build Coastguard Worker space->GetMemMap()->Begin(),
620*795d594fSAndroid Build Coastguard Worker space->GetLiveBitmap(),
621*795d594fSAndroid Build Coastguard Worker oat_file,
622*795d594fSAndroid Build Coastguard Worker error_msg);
623*795d594fSAndroid Build Coastguard Worker } else {
624*795d594fSAndroid Build Coastguard Worker result = RelocateInPlace<PointerSize::k32>(boot_image_begin,
625*795d594fSAndroid Build Coastguard Worker space->GetMemMap()->Begin(),
626*795d594fSAndroid Build Coastguard Worker space->GetLiveBitmap(),
627*795d594fSAndroid Build Coastguard Worker oat_file,
628*795d594fSAndroid Build Coastguard Worker error_msg);
629*795d594fSAndroid Build Coastguard Worker }
630*795d594fSAndroid Build Coastguard Worker if (!result) {
631*795d594fSAndroid Build Coastguard Worker return nullptr;
632*795d594fSAndroid Build Coastguard Worker }
633*795d594fSAndroid Build Coastguard Worker }
634*795d594fSAndroid Build Coastguard Worker
635*795d594fSAndroid Build Coastguard Worker DCHECK_LE(boot_image_space_dependencies, boot_image_spaces.size());
636*795d594fSAndroid Build Coastguard Worker if (boot_image_space_dependencies != boot_image_spaces.size()) {
637*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("DeduplicateInternedStrings", &logger);
638*795d594fSAndroid Build Coastguard Worker // There shall be no duplicates with boot image spaces this app image depends on.
639*795d594fSAndroid Build Coastguard Worker ArrayRef<ImageSpace* const> old_spaces =
640*795d594fSAndroid Build Coastguard Worker boot_image_spaces.SubArray(/*pos=*/ boot_image_space_dependencies);
641*795d594fSAndroid Build Coastguard Worker SafeMap<mirror::String*, mirror::String*> intern_remap;
642*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
643*795d594fSAndroid Build Coastguard Worker RemoveInternTableDuplicates(old_spaces, space.get(), &intern_remap);
644*795d594fSAndroid Build Coastguard Worker if (!intern_remap.empty()) {
645*795d594fSAndroid Build Coastguard Worker RemapInternedStringDuplicates(intern_remap, space.get());
646*795d594fSAndroid Build Coastguard Worker }
647*795d594fSAndroid Build Coastguard Worker }
648*795d594fSAndroid Build Coastguard Worker
649*795d594fSAndroid Build Coastguard Worker const ImageHeader& primary_header = boot_image_spaces.front()->GetImageHeader();
650*795d594fSAndroid Build Coastguard Worker static_assert(static_cast<size_t>(ImageHeader::kResolutionMethod) == 0u);
651*795d594fSAndroid Build Coastguard Worker for (size_t i = 0u; i != static_cast<size_t>(ImageHeader::kImageMethodsCount); ++i) {
652*795d594fSAndroid Build Coastguard Worker ImageHeader::ImageMethod method = static_cast<ImageHeader::ImageMethod>(i);
653*795d594fSAndroid Build Coastguard Worker CHECK_EQ(primary_header.GetImageMethod(method), image_header.GetImageMethod(method))
654*795d594fSAndroid Build Coastguard Worker << method;
655*795d594fSAndroid Build Coastguard Worker }
656*795d594fSAndroid Build Coastguard Worker
657*795d594fSAndroid Build Coastguard Worker VLOG(image) << "ImageSpace::Loader::InitAppImage exiting " << *space.get();
658*795d594fSAndroid Build Coastguard Worker }
659*795d594fSAndroid Build Coastguard Worker if (VLOG_IS_ON(image)) {
660*795d594fSAndroid Build Coastguard Worker logger.Dump(LOG_STREAM(INFO));
661*795d594fSAndroid Build Coastguard Worker }
662*795d594fSAndroid Build Coastguard Worker return space;
663*795d594fSAndroid Build Coastguard Worker }
664*795d594fSAndroid Build Coastguard Worker
Init(const char * image_filename,const char * image_location,TimingLogger * logger,MemMap * image_reservation,std::string * error_msg)665*795d594fSAndroid Build Coastguard Worker static std::unique_ptr<ImageSpace> Init(const char* image_filename,
666*795d594fSAndroid Build Coastguard Worker const char* image_location,
667*795d594fSAndroid Build Coastguard Worker TimingLogger* logger,
668*795d594fSAndroid Build Coastguard Worker /*inout*/MemMap* image_reservation,
669*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
670*795d594fSAndroid Build Coastguard Worker CHECK(image_filename != nullptr);
671*795d594fSAndroid Build Coastguard Worker CHECK(image_location != nullptr);
672*795d594fSAndroid Build Coastguard Worker
673*795d594fSAndroid Build Coastguard Worker std::unique_ptr<File> file;
674*795d594fSAndroid Build Coastguard Worker {
675*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("OpenImageFile", logger);
676*795d594fSAndroid Build Coastguard Worker file.reset(OS::OpenFileForReading(image_filename));
677*795d594fSAndroid Build Coastguard Worker if (file == nullptr) {
678*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to open '%s'", image_filename);
679*795d594fSAndroid Build Coastguard Worker return nullptr;
680*795d594fSAndroid Build Coastguard Worker }
681*795d594fSAndroid Build Coastguard Worker }
682*795d594fSAndroid Build Coastguard Worker return Init(file.get(),
683*795d594fSAndroid Build Coastguard Worker image_filename,
684*795d594fSAndroid Build Coastguard Worker image_location,
685*795d594fSAndroid Build Coastguard Worker /*profile_files=*/ {},
686*795d594fSAndroid Build Coastguard Worker /*allow_direct_mapping=*/ true,
687*795d594fSAndroid Build Coastguard Worker logger,
688*795d594fSAndroid Build Coastguard Worker image_reservation,
689*795d594fSAndroid Build Coastguard Worker error_msg);
690*795d594fSAndroid Build Coastguard Worker }
691*795d594fSAndroid Build Coastguard Worker
Init(File * file,const char * image_filename,const char * image_location,const std::vector<std::string> & profile_files,bool allow_direct_mapping,TimingLogger * logger,MemMap * image_reservation,std::string * error_msg)692*795d594fSAndroid Build Coastguard Worker static std::unique_ptr<ImageSpace> Init(File* file,
693*795d594fSAndroid Build Coastguard Worker const char* image_filename,
694*795d594fSAndroid Build Coastguard Worker const char* image_location,
695*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>& profile_files,
696*795d594fSAndroid Build Coastguard Worker bool allow_direct_mapping,
697*795d594fSAndroid Build Coastguard Worker TimingLogger* logger,
698*795d594fSAndroid Build Coastguard Worker /*inout*/MemMap* image_reservation,
699*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
700*795d594fSAndroid Build Coastguard Worker CHECK(image_filename != nullptr);
701*795d594fSAndroid Build Coastguard Worker CHECK(image_location != nullptr);
702*795d594fSAndroid Build Coastguard Worker
703*795d594fSAndroid Build Coastguard Worker VLOG(image) << "ImageSpace::Init entering image_filename=" << image_filename;
704*795d594fSAndroid Build Coastguard Worker
705*795d594fSAndroid Build Coastguard Worker ImageHeader image_header;
706*795d594fSAndroid Build Coastguard Worker {
707*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("ReadImageHeader", logger);
708*795d594fSAndroid Build Coastguard Worker bool success = file->PreadFully(&image_header, sizeof(image_header), /*offset=*/ 0u);
709*795d594fSAndroid Build Coastguard Worker if (!success || !image_header.IsValid()) {
710*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
711*795d594fSAndroid Build Coastguard Worker return nullptr;
712*795d594fSAndroid Build Coastguard Worker }
713*795d594fSAndroid Build Coastguard Worker }
714*795d594fSAndroid Build Coastguard Worker // Check that the file is larger or equal to the header size + data size.
715*795d594fSAndroid Build Coastguard Worker const uint64_t image_file_size = static_cast<uint64_t>(file->GetLength());
716*795d594fSAndroid Build Coastguard Worker if (image_file_size < sizeof(ImageHeader) + image_header.GetDataSize()) {
717*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf(
718*795d594fSAndroid Build Coastguard Worker "Image file truncated: %" PRIu64 " vs. %" PRIu64 ".",
719*795d594fSAndroid Build Coastguard Worker image_file_size,
720*795d594fSAndroid Build Coastguard Worker static_cast<uint64_t>(sizeof(ImageHeader) + image_header.GetDataSize()));
721*795d594fSAndroid Build Coastguard Worker return nullptr;
722*795d594fSAndroid Build Coastguard Worker }
723*795d594fSAndroid Build Coastguard Worker
724*795d594fSAndroid Build Coastguard Worker if (VLOG_IS_ON(startup)) {
725*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Dumping image sections";
726*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
727*795d594fSAndroid Build Coastguard Worker const auto section_idx = static_cast<ImageHeader::ImageSections>(i);
728*795d594fSAndroid Build Coastguard Worker auto& section = image_header.GetImageSection(section_idx);
729*795d594fSAndroid Build Coastguard Worker LOG(INFO) << section_idx << " start="
730*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<void*>(image_header.GetImageBegin() + section.Offset()) << " "
731*795d594fSAndroid Build Coastguard Worker << section;
732*795d594fSAndroid Build Coastguard Worker }
733*795d594fSAndroid Build Coastguard Worker }
734*795d594fSAndroid Build Coastguard Worker
735*795d594fSAndroid Build Coastguard Worker const auto& bitmap_section = image_header.GetImageBitmapSection();
736*795d594fSAndroid Build Coastguard Worker // The location we want to map from is the first aligned page after the end of the stored
737*795d594fSAndroid Build Coastguard Worker // (possibly compressed) data.
738*795d594fSAndroid Build Coastguard Worker const size_t image_bitmap_offset =
739*795d594fSAndroid Build Coastguard Worker RoundUp(sizeof(ImageHeader) + image_header.GetDataSize(), kElfSegmentAlignment);
740*795d594fSAndroid Build Coastguard Worker const size_t end_of_bitmap = image_bitmap_offset + bitmap_section.Size();
741*795d594fSAndroid Build Coastguard Worker if (end_of_bitmap != image_file_size) {
742*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf(
743*795d594fSAndroid Build Coastguard Worker "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.",
744*795d594fSAndroid Build Coastguard Worker image_file_size,
745*795d594fSAndroid Build Coastguard Worker end_of_bitmap);
746*795d594fSAndroid Build Coastguard Worker return nullptr;
747*795d594fSAndroid Build Coastguard Worker }
748*795d594fSAndroid Build Coastguard Worker
749*795d594fSAndroid Build Coastguard Worker // GetImageBegin is the preferred address to map the image. If we manage to map the
750*795d594fSAndroid Build Coastguard Worker // image at the image begin, the amount of fixup work required is minimized.
751*795d594fSAndroid Build Coastguard Worker // If it is pic we will retry with error_msg for the2 failure case. Pass a null error_msg to
752*795d594fSAndroid Build Coastguard Worker // avoid reading proc maps for a mapping failure and slowing everything down.
753*795d594fSAndroid Build Coastguard Worker // For the boot image, we have already reserved the memory and we load the image
754*795d594fSAndroid Build Coastguard Worker // into the `image_reservation`.
755*795d594fSAndroid Build Coastguard Worker MemMap map = LoadImageFile(
756*795d594fSAndroid Build Coastguard Worker image_filename,
757*795d594fSAndroid Build Coastguard Worker image_location,
758*795d594fSAndroid Build Coastguard Worker image_header,
759*795d594fSAndroid Build Coastguard Worker file->Fd(),
760*795d594fSAndroid Build Coastguard Worker allow_direct_mapping,
761*795d594fSAndroid Build Coastguard Worker logger,
762*795d594fSAndroid Build Coastguard Worker image_reservation,
763*795d594fSAndroid Build Coastguard Worker error_msg);
764*795d594fSAndroid Build Coastguard Worker if (!map.IsValid()) {
765*795d594fSAndroid Build Coastguard Worker DCHECK(!error_msg->empty());
766*795d594fSAndroid Build Coastguard Worker return nullptr;
767*795d594fSAndroid Build Coastguard Worker }
768*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(0, memcmp(&image_header, map.Begin(), sizeof(ImageHeader)));
769*795d594fSAndroid Build Coastguard Worker
770*795d594fSAndroid Build Coastguard Worker MemMap image_bitmap_map = MemMap::MapFile(bitmap_section.Size(),
771*795d594fSAndroid Build Coastguard Worker PROT_READ,
772*795d594fSAndroid Build Coastguard Worker MAP_PRIVATE,
773*795d594fSAndroid Build Coastguard Worker file->Fd(),
774*795d594fSAndroid Build Coastguard Worker image_bitmap_offset,
775*795d594fSAndroid Build Coastguard Worker /*low_4gb=*/ false,
776*795d594fSAndroid Build Coastguard Worker image_filename,
777*795d594fSAndroid Build Coastguard Worker error_msg);
778*795d594fSAndroid Build Coastguard Worker if (!image_bitmap_map.IsValid()) {
779*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
780*795d594fSAndroid Build Coastguard Worker return nullptr;
781*795d594fSAndroid Build Coastguard Worker }
782*795d594fSAndroid Build Coastguard Worker const uint32_t bitmap_index = ImageSpace::bitmap_index_.fetch_add(1);
783*795d594fSAndroid Build Coastguard Worker std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u",
784*795d594fSAndroid Build Coastguard Worker image_filename,
785*795d594fSAndroid Build Coastguard Worker bitmap_index));
786*795d594fSAndroid Build Coastguard Worker // Bitmap only needs to cover until the end of the mirror objects section.
787*795d594fSAndroid Build Coastguard Worker const ImageSection& image_objects = image_header.GetObjectsSection();
788*795d594fSAndroid Build Coastguard Worker // We only want the mirror object, not the ArtFields and ArtMethods.
789*795d594fSAndroid Build Coastguard Worker uint8_t* const image_end = map.Begin() + image_objects.End();
790*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap bitmap;
791*795d594fSAndroid Build Coastguard Worker {
792*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("CreateImageBitmap", logger);
793*795d594fSAndroid Build Coastguard Worker bitmap = accounting::ContinuousSpaceBitmap::CreateFromMemMap(
794*795d594fSAndroid Build Coastguard Worker bitmap_name,
795*795d594fSAndroid Build Coastguard Worker std::move(image_bitmap_map),
796*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uint8_t*>(map.Begin()),
797*795d594fSAndroid Build Coastguard Worker // Make sure the bitmap is aligned to card size instead of just bitmap word size.
798*795d594fSAndroid Build Coastguard Worker RoundUp(image_objects.End(), gc::accounting::CardTable::kCardSize));
799*795d594fSAndroid Build Coastguard Worker if (!bitmap.IsValid()) {
800*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
801*795d594fSAndroid Build Coastguard Worker return nullptr;
802*795d594fSAndroid Build Coastguard Worker }
803*795d594fSAndroid Build Coastguard Worker }
804*795d594fSAndroid Build Coastguard Worker // We only want the mirror object, not the ArtFields and ArtMethods.
805*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename,
806*795d594fSAndroid Build Coastguard Worker image_location,
807*795d594fSAndroid Build Coastguard Worker profile_files,
808*795d594fSAndroid Build Coastguard Worker std::move(map),
809*795d594fSAndroid Build Coastguard Worker std::move(bitmap),
810*795d594fSAndroid Build Coastguard Worker image_end));
811*795d594fSAndroid Build Coastguard Worker return space;
812*795d594fSAndroid Build Coastguard Worker }
813*795d594fSAndroid Build Coastguard Worker
CheckImageComponentCount(const ImageSpace & space,uint32_t expected_component_count,std::string * error_msg)814*795d594fSAndroid Build Coastguard Worker static bool CheckImageComponentCount(const ImageSpace& space,
815*795d594fSAndroid Build Coastguard Worker uint32_t expected_component_count,
816*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
817*795d594fSAndroid Build Coastguard Worker const ImageHeader& header = space.GetImageHeader();
818*795d594fSAndroid Build Coastguard Worker if (header.GetComponentCount() != expected_component_count) {
819*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Unexpected component count in %s, received %u, expected %u",
820*795d594fSAndroid Build Coastguard Worker space.GetImageFilename().c_str(),
821*795d594fSAndroid Build Coastguard Worker header.GetComponentCount(),
822*795d594fSAndroid Build Coastguard Worker expected_component_count);
823*795d594fSAndroid Build Coastguard Worker return false;
824*795d594fSAndroid Build Coastguard Worker }
825*795d594fSAndroid Build Coastguard Worker return true;
826*795d594fSAndroid Build Coastguard Worker }
827*795d594fSAndroid Build Coastguard Worker
CheckImageReservationSize(const ImageSpace & space,uint32_t expected_reservation_size,std::string * error_msg)828*795d594fSAndroid Build Coastguard Worker static bool CheckImageReservationSize(const ImageSpace& space,
829*795d594fSAndroid Build Coastguard Worker uint32_t expected_reservation_size,
830*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
831*795d594fSAndroid Build Coastguard Worker const ImageHeader& header = space.GetImageHeader();
832*795d594fSAndroid Build Coastguard Worker if (header.GetImageReservationSize() != expected_reservation_size) {
833*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Unexpected reservation size in %s, received %u, expected %u",
834*795d594fSAndroid Build Coastguard Worker space.GetImageFilename().c_str(),
835*795d594fSAndroid Build Coastguard Worker header.GetImageReservationSize(),
836*795d594fSAndroid Build Coastguard Worker expected_reservation_size);
837*795d594fSAndroid Build Coastguard Worker return false;
838*795d594fSAndroid Build Coastguard Worker }
839*795d594fSAndroid Build Coastguard Worker return true;
840*795d594fSAndroid Build Coastguard Worker }
841*795d594fSAndroid Build Coastguard Worker
842*795d594fSAndroid Build Coastguard Worker template <typename Container>
RemoveInternTableDuplicates(const Container & old_spaces,ImageSpace * new_space,SafeMap<mirror::String *,mirror::String * > * intern_remap)843*795d594fSAndroid Build Coastguard Worker static void RemoveInternTableDuplicates(
844*795d594fSAndroid Build Coastguard Worker const Container& old_spaces,
845*795d594fSAndroid Build Coastguard Worker /*inout*/ImageSpace* new_space,
846*795d594fSAndroid Build Coastguard Worker /*inout*/SafeMap<mirror::String*, mirror::String*>* intern_remap)
847*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
848*795d594fSAndroid Build Coastguard Worker const ImageSection& new_interns = new_space->GetImageHeader().GetInternedStringsSection();
849*795d594fSAndroid Build Coastguard Worker if (new_interns.Size() != 0u) {
850*795d594fSAndroid Build Coastguard Worker const uint8_t* new_data = new_space->Begin() + new_interns.Offset();
851*795d594fSAndroid Build Coastguard Worker size_t new_read_count;
852*795d594fSAndroid Build Coastguard Worker InternTable::UnorderedSet new_set(new_data, /*make_copy_of_data=*/ false, &new_read_count);
853*795d594fSAndroid Build Coastguard Worker for (const auto& old_space : old_spaces) {
854*795d594fSAndroid Build Coastguard Worker const ImageSection& old_interns = old_space->GetImageHeader().GetInternedStringsSection();
855*795d594fSAndroid Build Coastguard Worker if (old_interns.Size() != 0u) {
856*795d594fSAndroid Build Coastguard Worker const uint8_t* old_data = old_space->Begin() + old_interns.Offset();
857*795d594fSAndroid Build Coastguard Worker size_t old_read_count;
858*795d594fSAndroid Build Coastguard Worker InternTable::UnorderedSet old_set(
859*795d594fSAndroid Build Coastguard Worker old_data, /*make_copy_of_data=*/ false, &old_read_count);
860*795d594fSAndroid Build Coastguard Worker RemoveDuplicates(old_set, &new_set, intern_remap);
861*795d594fSAndroid Build Coastguard Worker }
862*795d594fSAndroid Build Coastguard Worker }
863*795d594fSAndroid Build Coastguard Worker }
864*795d594fSAndroid Build Coastguard Worker }
865*795d594fSAndroid Build Coastguard Worker
RemapInternedStringDuplicates(const SafeMap<mirror::String *,mirror::String * > & intern_remap,ImageSpace * new_space)866*795d594fSAndroid Build Coastguard Worker static void RemapInternedStringDuplicates(
867*795d594fSAndroid Build Coastguard Worker const SafeMap<mirror::String*, mirror::String*>& intern_remap,
868*795d594fSAndroid Build Coastguard Worker ImageSpace* new_space) REQUIRES_SHARED(Locks::mutator_lock_) {
869*795d594fSAndroid Build Coastguard Worker RemapInternedStringsVisitor visitor(intern_remap);
870*795d594fSAndroid Build Coastguard Worker static_assert(IsAligned<kObjectAlignment>(sizeof(ImageHeader)), "Header alignment check");
871*795d594fSAndroid Build Coastguard Worker uint32_t objects_end = new_space->GetImageHeader().GetObjectsSection().Size();
872*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(objects_end, kObjectAlignment);
873*795d594fSAndroid Build Coastguard Worker for (uint32_t pos = sizeof(ImageHeader); pos != objects_end; ) {
874*795d594fSAndroid Build Coastguard Worker mirror::Object* object = reinterpret_cast<mirror::Object*>(new_space->Begin() + pos);
875*795d594fSAndroid Build Coastguard Worker object->VisitReferences</*kVisitNativeRoots=*/ false,
876*795d594fSAndroid Build Coastguard Worker kVerifyNone,
877*795d594fSAndroid Build Coastguard Worker kWithoutReadBarrier>(visitor, visitor);
878*795d594fSAndroid Build Coastguard Worker pos += RoundUp(object->SizeOf<kVerifyNone>(), kObjectAlignment);
879*795d594fSAndroid Build Coastguard Worker }
880*795d594fSAndroid Build Coastguard Worker }
881*795d594fSAndroid Build Coastguard Worker
882*795d594fSAndroid Build Coastguard Worker private:
883*795d594fSAndroid Build Coastguard Worker // Remove duplicates found in the `old_set` from the `new_set`.
884*795d594fSAndroid Build Coastguard Worker // Record the removed Strings for remapping. No read barriers are needed as the
885*795d594fSAndroid Build Coastguard Worker // tables are either just being loaded and not yet a part of the heap, or boot
886*795d594fSAndroid Build Coastguard Worker // image intern tables with non-moveable Strings used when loading an app image.
RemoveDuplicates(const InternTable::UnorderedSet & old_set,InternTable::UnorderedSet * new_set,SafeMap<mirror::String *,mirror::String * > * intern_remap)887*795d594fSAndroid Build Coastguard Worker static void RemoveDuplicates(const InternTable::UnorderedSet& old_set,
888*795d594fSAndroid Build Coastguard Worker /*inout*/InternTable::UnorderedSet* new_set,
889*795d594fSAndroid Build Coastguard Worker /*inout*/SafeMap<mirror::String*, mirror::String*>* intern_remap)
890*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
891*795d594fSAndroid Build Coastguard Worker if (old_set.size() < new_set->size()) {
892*795d594fSAndroid Build Coastguard Worker for (const GcRoot<mirror::String>& old_s : old_set) {
893*795d594fSAndroid Build Coastguard Worker auto new_it = new_set->find(old_s);
894*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(new_it != new_set->end())) {
895*795d594fSAndroid Build Coastguard Worker intern_remap->Put(new_it->Read<kWithoutReadBarrier>(), old_s.Read<kWithoutReadBarrier>());
896*795d594fSAndroid Build Coastguard Worker new_set->erase(new_it);
897*795d594fSAndroid Build Coastguard Worker }
898*795d594fSAndroid Build Coastguard Worker }
899*795d594fSAndroid Build Coastguard Worker } else {
900*795d594fSAndroid Build Coastguard Worker for (auto new_it = new_set->begin(), end = new_set->end(); new_it != end; ) {
901*795d594fSAndroid Build Coastguard Worker auto old_it = old_set.find(*new_it);
902*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(old_it != old_set.end())) {
903*795d594fSAndroid Build Coastguard Worker intern_remap->Put(new_it->Read<kWithoutReadBarrier>(),
904*795d594fSAndroid Build Coastguard Worker old_it->Read<kWithoutReadBarrier>());
905*795d594fSAndroid Build Coastguard Worker new_it = new_set->erase(new_it);
906*795d594fSAndroid Build Coastguard Worker } else {
907*795d594fSAndroid Build Coastguard Worker ++new_it;
908*795d594fSAndroid Build Coastguard Worker }
909*795d594fSAndroid Build Coastguard Worker }
910*795d594fSAndroid Build Coastguard Worker }
911*795d594fSAndroid Build Coastguard Worker }
912*795d594fSAndroid Build Coastguard Worker
ValidateBootImageChecksum(const char * image_filename,const ImageHeader & image_header,const OatFile * oat_file,ArrayRef<ImageSpace * const> boot_image_spaces,size_t * boot_image_space_dependencies,std::string * error_msg)913*795d594fSAndroid Build Coastguard Worker static bool ValidateBootImageChecksum(const char* image_filename,
914*795d594fSAndroid Build Coastguard Worker const ImageHeader& image_header,
915*795d594fSAndroid Build Coastguard Worker const OatFile* oat_file,
916*795d594fSAndroid Build Coastguard Worker ArrayRef<ImageSpace* const> boot_image_spaces,
917*795d594fSAndroid Build Coastguard Worker /*out*/size_t* boot_image_space_dependencies,
918*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
919*795d594fSAndroid Build Coastguard Worker // Use the boot image component count to calculate the checksum from
920*795d594fSAndroid Build Coastguard Worker // the appropriate number of boot image chunks.
921*795d594fSAndroid Build Coastguard Worker uint32_t boot_image_component_count = image_header.GetBootImageComponentCount();
922*795d594fSAndroid Build Coastguard Worker size_t expected_image_component_count = ImageSpace::GetNumberOfComponents(boot_image_spaces);
923*795d594fSAndroid Build Coastguard Worker if (boot_image_component_count > expected_image_component_count) {
924*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Too many boot image dependencies (%u > %zu) in image %s",
925*795d594fSAndroid Build Coastguard Worker boot_image_component_count,
926*795d594fSAndroid Build Coastguard Worker expected_image_component_count,
927*795d594fSAndroid Build Coastguard Worker image_filename);
928*795d594fSAndroid Build Coastguard Worker return false;
929*795d594fSAndroid Build Coastguard Worker }
930*795d594fSAndroid Build Coastguard Worker uint32_t checksum = 0u;
931*795d594fSAndroid Build Coastguard Worker size_t chunk_count = 0u;
932*795d594fSAndroid Build Coastguard Worker size_t space_pos = 0u;
933*795d594fSAndroid Build Coastguard Worker uint64_t boot_image_size = 0u;
934*795d594fSAndroid Build Coastguard Worker for (size_t component_count = 0u; component_count != boot_image_component_count; ) {
935*795d594fSAndroid Build Coastguard Worker const ImageHeader& current_header = boot_image_spaces[space_pos]->GetImageHeader();
936*795d594fSAndroid Build Coastguard Worker if (current_header.GetComponentCount() > boot_image_component_count - component_count) {
937*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Boot image component count in %s ends in the middle of a chunk, "
938*795d594fSAndroid Build Coastguard Worker "%u is between %zu and %zu",
939*795d594fSAndroid Build Coastguard Worker image_filename,
940*795d594fSAndroid Build Coastguard Worker boot_image_component_count,
941*795d594fSAndroid Build Coastguard Worker component_count,
942*795d594fSAndroid Build Coastguard Worker component_count + current_header.GetComponentCount());
943*795d594fSAndroid Build Coastguard Worker return false;
944*795d594fSAndroid Build Coastguard Worker }
945*795d594fSAndroid Build Coastguard Worker component_count += current_header.GetComponentCount();
946*795d594fSAndroid Build Coastguard Worker checksum ^= current_header.GetImageChecksum();
947*795d594fSAndroid Build Coastguard Worker chunk_count += 1u;
948*795d594fSAndroid Build Coastguard Worker space_pos += current_header.GetImageSpaceCount();
949*795d594fSAndroid Build Coastguard Worker boot_image_size += current_header.GetImageReservationSize();
950*795d594fSAndroid Build Coastguard Worker }
951*795d594fSAndroid Build Coastguard Worker if (image_header.GetBootImageChecksum() != checksum) {
952*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Boot image checksum mismatch (0x%08x != 0x%08x) in image %s",
953*795d594fSAndroid Build Coastguard Worker image_header.GetBootImageChecksum(),
954*795d594fSAndroid Build Coastguard Worker checksum,
955*795d594fSAndroid Build Coastguard Worker image_filename);
956*795d594fSAndroid Build Coastguard Worker return false;
957*795d594fSAndroid Build Coastguard Worker }
958*795d594fSAndroid Build Coastguard Worker if (image_header.GetBootImageSize() != boot_image_size) {
959*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Boot image size mismatch (0x%08x != 0x%08" PRIx64 ") in image %s",
960*795d594fSAndroid Build Coastguard Worker image_header.GetBootImageSize(),
961*795d594fSAndroid Build Coastguard Worker boot_image_size,
962*795d594fSAndroid Build Coastguard Worker image_filename);
963*795d594fSAndroid Build Coastguard Worker return false;
964*795d594fSAndroid Build Coastguard Worker }
965*795d594fSAndroid Build Coastguard Worker // Oat checksums, if present, have already been validated, so we know that
966*795d594fSAndroid Build Coastguard Worker // they match the loaded image spaces. Therefore, we just verify that they
967*795d594fSAndroid Build Coastguard Worker // are consistent in the number of boot image chunks they list by looking
968*795d594fSAndroid Build Coastguard Worker // for the kImageChecksumPrefix at the start of each component.
969*795d594fSAndroid Build Coastguard Worker const char* oat_boot_class_path_checksums =
970*795d594fSAndroid Build Coastguard Worker oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
971*795d594fSAndroid Build Coastguard Worker if (oat_boot_class_path_checksums != nullptr) {
972*795d594fSAndroid Build Coastguard Worker size_t oat_bcp_chunk_count = 0u;
973*795d594fSAndroid Build Coastguard Worker while (*oat_boot_class_path_checksums == kImageChecksumPrefix) {
974*795d594fSAndroid Build Coastguard Worker oat_bcp_chunk_count += 1u;
975*795d594fSAndroid Build Coastguard Worker // Find the start of the next component if any.
976*795d594fSAndroid Build Coastguard Worker const char* separator = strchr(oat_boot_class_path_checksums, ':');
977*795d594fSAndroid Build Coastguard Worker oat_boot_class_path_checksums = (separator != nullptr) ? separator + 1u : "";
978*795d594fSAndroid Build Coastguard Worker }
979*795d594fSAndroid Build Coastguard Worker if (oat_bcp_chunk_count != chunk_count) {
980*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Boot image chunk count mismatch (%zu != %zu) in image %s",
981*795d594fSAndroid Build Coastguard Worker oat_bcp_chunk_count,
982*795d594fSAndroid Build Coastguard Worker chunk_count,
983*795d594fSAndroid Build Coastguard Worker image_filename);
984*795d594fSAndroid Build Coastguard Worker return false;
985*795d594fSAndroid Build Coastguard Worker }
986*795d594fSAndroid Build Coastguard Worker }
987*795d594fSAndroid Build Coastguard Worker *boot_image_space_dependencies = space_pos;
988*795d594fSAndroid Build Coastguard Worker return true;
989*795d594fSAndroid Build Coastguard Worker }
990*795d594fSAndroid Build Coastguard Worker
LoadImageFile(const char * image_filename,const char * image_location,const ImageHeader & image_header,int fd,bool allow_direct_mapping,TimingLogger * logger,MemMap * image_reservation,std::string * error_msg)991*795d594fSAndroid Build Coastguard Worker static MemMap LoadImageFile(const char* image_filename,
992*795d594fSAndroid Build Coastguard Worker const char* image_location,
993*795d594fSAndroid Build Coastguard Worker const ImageHeader& image_header,
994*795d594fSAndroid Build Coastguard Worker int fd,
995*795d594fSAndroid Build Coastguard Worker bool allow_direct_mapping,
996*795d594fSAndroid Build Coastguard Worker TimingLogger* logger,
997*795d594fSAndroid Build Coastguard Worker /*inout*/MemMap* image_reservation,
998*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
999*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("MapImageFile", logger);
1000*795d594fSAndroid Build Coastguard Worker
1001*795d594fSAndroid Build Coastguard Worker // The runtime might not be available at this point if we're running dex2oat or oatdump, in
1002*795d594fSAndroid Build Coastguard Worker // which case we just truncate the madvise optimization limit completely.
1003*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
1004*795d594fSAndroid Build Coastguard Worker const size_t madvise_size_limit = runtime ? runtime->GetMadviseWillNeedSizeArt() : 0;
1005*795d594fSAndroid Build Coastguard Worker
1006*795d594fSAndroid Build Coastguard Worker const bool is_compressed = image_header.HasCompressedBlock();
1007*795d594fSAndroid Build Coastguard Worker if (!is_compressed && allow_direct_mapping) {
1008*795d594fSAndroid Build Coastguard Worker uint8_t* address = (image_reservation != nullptr) ? image_reservation->Begin() : nullptr;
1009*795d594fSAndroid Build Coastguard Worker // The reserved memory size is aligned up to kElfSegmentAlignment to ensure
1010*795d594fSAndroid Build Coastguard Worker // that the next reserved area will be aligned to the value.
1011*795d594fSAndroid Build Coastguard Worker MemMap map = MemMap::MapFileAtAddress(
1012*795d594fSAndroid Build Coastguard Worker address,
1013*795d594fSAndroid Build Coastguard Worker CondRoundUp<kPageSizeAgnostic>(image_header.GetImageSize(), kElfSegmentAlignment),
1014*795d594fSAndroid Build Coastguard Worker PROT_READ | PROT_WRITE,
1015*795d594fSAndroid Build Coastguard Worker MAP_PRIVATE,
1016*795d594fSAndroid Build Coastguard Worker fd,
1017*795d594fSAndroid Build Coastguard Worker /*start=*/0,
1018*795d594fSAndroid Build Coastguard Worker /*low_4gb=*/true,
1019*795d594fSAndroid Build Coastguard Worker image_filename,
1020*795d594fSAndroid Build Coastguard Worker /*reuse=*/false,
1021*795d594fSAndroid Build Coastguard Worker image_reservation,
1022*795d594fSAndroid Build Coastguard Worker error_msg);
1023*795d594fSAndroid Build Coastguard Worker if (map.IsValid()) {
1024*795d594fSAndroid Build Coastguard Worker Runtime::MadviseFileForRange(
1025*795d594fSAndroid Build Coastguard Worker madvise_size_limit, map.Size(), map.Begin(), map.End(), image_filename);
1026*795d594fSAndroid Build Coastguard Worker }
1027*795d594fSAndroid Build Coastguard Worker return map;
1028*795d594fSAndroid Build Coastguard Worker }
1029*795d594fSAndroid Build Coastguard Worker
1030*795d594fSAndroid Build Coastguard Worker // Reserve output and copy/decompress into it.
1031*795d594fSAndroid Build Coastguard Worker // The reserved memory size is aligned up to kElfSegmentAlignment to ensure
1032*795d594fSAndroid Build Coastguard Worker // that the next reserved area will be aligned to the value.
1033*795d594fSAndroid Build Coastguard Worker MemMap map = MemMap::MapAnonymous(image_location,
1034*795d594fSAndroid Build Coastguard Worker CondRoundUp<kPageSizeAgnostic>(image_header.GetImageSize(),
1035*795d594fSAndroid Build Coastguard Worker kElfSegmentAlignment),
1036*795d594fSAndroid Build Coastguard Worker PROT_READ | PROT_WRITE,
1037*795d594fSAndroid Build Coastguard Worker /*low_4gb=*/ true,
1038*795d594fSAndroid Build Coastguard Worker image_reservation,
1039*795d594fSAndroid Build Coastguard Worker error_msg);
1040*795d594fSAndroid Build Coastguard Worker if (map.IsValid()) {
1041*795d594fSAndroid Build Coastguard Worker const size_t stored_size = image_header.GetDataSize();
1042*795d594fSAndroid Build Coastguard Worker MemMap temp_map = MemMap::MapFile(sizeof(ImageHeader) + stored_size,
1043*795d594fSAndroid Build Coastguard Worker PROT_READ,
1044*795d594fSAndroid Build Coastguard Worker MAP_PRIVATE,
1045*795d594fSAndroid Build Coastguard Worker fd,
1046*795d594fSAndroid Build Coastguard Worker /*start=*/ 0,
1047*795d594fSAndroid Build Coastguard Worker /*low_4gb=*/ false,
1048*795d594fSAndroid Build Coastguard Worker image_filename,
1049*795d594fSAndroid Build Coastguard Worker error_msg);
1050*795d594fSAndroid Build Coastguard Worker if (!temp_map.IsValid()) {
1051*795d594fSAndroid Build Coastguard Worker DCHECK(error_msg == nullptr || !error_msg->empty());
1052*795d594fSAndroid Build Coastguard Worker return MemMap::Invalid();
1053*795d594fSAndroid Build Coastguard Worker }
1054*795d594fSAndroid Build Coastguard Worker
1055*795d594fSAndroid Build Coastguard Worker Runtime::MadviseFileForRange(
1056*795d594fSAndroid Build Coastguard Worker madvise_size_limit, temp_map.Size(), temp_map.Begin(), temp_map.End(), image_filename);
1057*795d594fSAndroid Build Coastguard Worker
1058*795d594fSAndroid Build Coastguard Worker if (is_compressed) {
1059*795d594fSAndroid Build Coastguard Worker memcpy(map.Begin(), &image_header, sizeof(ImageHeader));
1060*795d594fSAndroid Build Coastguard Worker
1061*795d594fSAndroid Build Coastguard Worker Runtime::ScopedThreadPoolUsage stpu;
1062*795d594fSAndroid Build Coastguard Worker ThreadPool* const pool = stpu.GetThreadPool();
1063*795d594fSAndroid Build Coastguard Worker const uint64_t start = NanoTime();
1064*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
1065*795d594fSAndroid Build Coastguard Worker static constexpr size_t kMinBlocks = 2u;
1066*795d594fSAndroid Build Coastguard Worker const bool use_parallel = pool != nullptr && image_header.GetBlockCount() >= kMinBlocks;
1067*795d594fSAndroid Build Coastguard Worker bool failed_decompression = false;
1068*795d594fSAndroid Build Coastguard Worker for (const ImageHeader::Block& block : image_header.GetBlocks(temp_map.Begin())) {
1069*795d594fSAndroid Build Coastguard Worker auto function = [&](Thread*) {
1070*795d594fSAndroid Build Coastguard Worker const uint64_t start2 = NanoTime();
1071*795d594fSAndroid Build Coastguard Worker ScopedTrace trace("LZ4 decompress block");
1072*795d594fSAndroid Build Coastguard Worker bool result = block.Decompress(/*out_ptr=*/map.Begin(),
1073*795d594fSAndroid Build Coastguard Worker /*in_ptr=*/temp_map.Begin(),
1074*795d594fSAndroid Build Coastguard Worker error_msg);
1075*795d594fSAndroid Build Coastguard Worker if (!result) {
1076*795d594fSAndroid Build Coastguard Worker failed_decompression = true;
1077*795d594fSAndroid Build Coastguard Worker if (error_msg != nullptr) {
1078*795d594fSAndroid Build Coastguard Worker *error_msg = "Failed to decompress image block " + *error_msg;
1079*795d594fSAndroid Build Coastguard Worker }
1080*795d594fSAndroid Build Coastguard Worker }
1081*795d594fSAndroid Build Coastguard Worker VLOG(image) << "Decompress block " << block.GetDataSize() << " -> "
1082*795d594fSAndroid Build Coastguard Worker << block.GetImageSize() << " in " << PrettyDuration(NanoTime() - start2);
1083*795d594fSAndroid Build Coastguard Worker };
1084*795d594fSAndroid Build Coastguard Worker if (use_parallel) {
1085*795d594fSAndroid Build Coastguard Worker pool->AddTask(self, new FunctionTask(std::move(function)));
1086*795d594fSAndroid Build Coastguard Worker } else {
1087*795d594fSAndroid Build Coastguard Worker function(self);
1088*795d594fSAndroid Build Coastguard Worker }
1089*795d594fSAndroid Build Coastguard Worker }
1090*795d594fSAndroid Build Coastguard Worker if (use_parallel) {
1091*795d594fSAndroid Build Coastguard Worker ScopedTrace trace("Waiting for workers");
1092*795d594fSAndroid Build Coastguard Worker pool->Wait(self, true, false);
1093*795d594fSAndroid Build Coastguard Worker }
1094*795d594fSAndroid Build Coastguard Worker const uint64_t time = NanoTime() - start;
1095*795d594fSAndroid Build Coastguard Worker // Add one 1 ns to prevent possible divide by 0.
1096*795d594fSAndroid Build Coastguard Worker VLOG(image) << "Decompressing image took " << PrettyDuration(time) << " ("
1097*795d594fSAndroid Build Coastguard Worker << PrettySize(static_cast<uint64_t>(map.Size()) * MsToNs(1000) / (time + 1))
1098*795d594fSAndroid Build Coastguard Worker << "/s)";
1099*795d594fSAndroid Build Coastguard Worker if (failed_decompression) {
1100*795d594fSAndroid Build Coastguard Worker DCHECK(error_msg == nullptr || !error_msg->empty());
1101*795d594fSAndroid Build Coastguard Worker return MemMap::Invalid();
1102*795d594fSAndroid Build Coastguard Worker }
1103*795d594fSAndroid Build Coastguard Worker } else {
1104*795d594fSAndroid Build Coastguard Worker DCHECK(!allow_direct_mapping);
1105*795d594fSAndroid Build Coastguard Worker // We do not allow direct mapping for boot image extensions compiled to a memfd.
1106*795d594fSAndroid Build Coastguard Worker // This prevents wasting memory by kernel keeping the contents of the file alive
1107*795d594fSAndroid Build Coastguard Worker // despite these contents being unreachable once the file descriptor is closed
1108*795d594fSAndroid Build Coastguard Worker // and mmapped memory is copied for all existing mappings.
1109*795d594fSAndroid Build Coastguard Worker //
1110*795d594fSAndroid Build Coastguard Worker // Most pages would be copied during relocation while there is only one mapping.
1111*795d594fSAndroid Build Coastguard Worker // We could use MAP_SHARED for relocation and then msync() and remap MAP_PRIVATE
1112*795d594fSAndroid Build Coastguard Worker // as required for forking from zygote, but there would still be some pages
1113*795d594fSAndroid Build Coastguard Worker // wasted anyway and we want to avoid that. (For example, static synchronized
1114*795d594fSAndroid Build Coastguard Worker // methods use the class object for locking and thus modify its lockword.)
1115*795d594fSAndroid Build Coastguard Worker
1116*795d594fSAndroid Build Coastguard Worker // No other process should race to overwrite the extension in memfd.
1117*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(memcmp(temp_map.Begin(), &image_header, sizeof(ImageHeader)), 0);
1118*795d594fSAndroid Build Coastguard Worker memcpy(map.Begin(), temp_map.Begin(), temp_map.Size());
1119*795d594fSAndroid Build Coastguard Worker }
1120*795d594fSAndroid Build Coastguard Worker }
1121*795d594fSAndroid Build Coastguard Worker
1122*795d594fSAndroid Build Coastguard Worker return map;
1123*795d594fSAndroid Build Coastguard Worker }
1124*795d594fSAndroid Build Coastguard Worker
1125*795d594fSAndroid Build Coastguard Worker class EmptyRange {
1126*795d594fSAndroid Build Coastguard Worker public:
InSource(uintptr_t) const1127*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE bool InSource(uintptr_t) const { return false; }
InDest(uintptr_t) const1128*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE bool InDest(uintptr_t) const { return false; }
ToDest(uintptr_t) const1129*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE uintptr_t ToDest(uintptr_t) const {
1130*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Unreachable";
1131*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
1132*795d594fSAndroid Build Coastguard Worker }
1133*795d594fSAndroid Build Coastguard Worker };
1134*795d594fSAndroid Build Coastguard Worker
1135*795d594fSAndroid Build Coastguard Worker template <typename Range0, typename Range1 = EmptyRange, typename Range2 = EmptyRange>
1136*795d594fSAndroid Build Coastguard Worker class ForwardAddress {
1137*795d594fSAndroid Build Coastguard Worker public:
ForwardAddress(const Range0 & range0=Range0 (),const Range1 & range1=Range1 (),const Range2 & range2=Range2 ())1138*795d594fSAndroid Build Coastguard Worker explicit ForwardAddress(const Range0& range0 = Range0(),
1139*795d594fSAndroid Build Coastguard Worker const Range1& range1 = Range1(),
1140*795d594fSAndroid Build Coastguard Worker const Range2& range2 = Range2())
1141*795d594fSAndroid Build Coastguard Worker : range0_(range0), range1_(range1), range2_(range2) {}
1142*795d594fSAndroid Build Coastguard Worker
1143*795d594fSAndroid Build Coastguard Worker // Return the relocated address of a heap object.
1144*795d594fSAndroid Build Coastguard Worker // Null checks must be performed in the caller (for performance reasons).
1145*795d594fSAndroid Build Coastguard Worker template <typename T>
operator ()(T * src) const1146*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE T* operator()(T* src) const {
1147*795d594fSAndroid Build Coastguard Worker DCHECK(src != nullptr);
1148*795d594fSAndroid Build Coastguard Worker const uintptr_t uint_src = reinterpret_cast<uintptr_t>(src);
1149*795d594fSAndroid Build Coastguard Worker if (range2_.InSource(uint_src)) {
1150*795d594fSAndroid Build Coastguard Worker return reinterpret_cast<T*>(range2_.ToDest(uint_src));
1151*795d594fSAndroid Build Coastguard Worker }
1152*795d594fSAndroid Build Coastguard Worker if (range1_.InSource(uint_src)) {
1153*795d594fSAndroid Build Coastguard Worker return reinterpret_cast<T*>(range1_.ToDest(uint_src));
1154*795d594fSAndroid Build Coastguard Worker }
1155*795d594fSAndroid Build Coastguard Worker CHECK(range0_.InSource(uint_src))
1156*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<const void*>(src) << " not in "
1157*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<const void*>(range0_.Source()) << "-"
1158*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<const void*>(range0_.Source() + range0_.Length());
1159*795d594fSAndroid Build Coastguard Worker return reinterpret_cast<T*>(range0_.ToDest(uint_src));
1160*795d594fSAndroid Build Coastguard Worker }
1161*795d594fSAndroid Build Coastguard Worker
1162*795d594fSAndroid Build Coastguard Worker private:
1163*795d594fSAndroid Build Coastguard Worker const Range0 range0_;
1164*795d594fSAndroid Build Coastguard Worker const Range1 range1_;
1165*795d594fSAndroid Build Coastguard Worker const Range2 range2_;
1166*795d594fSAndroid Build Coastguard Worker };
1167*795d594fSAndroid Build Coastguard Worker
1168*795d594fSAndroid Build Coastguard Worker template <typename Forward>
1169*795d594fSAndroid Build Coastguard Worker class FixupRootVisitor {
1170*795d594fSAndroid Build Coastguard Worker public:
1171*795d594fSAndroid Build Coastguard Worker template<typename... Args>
FixupRootVisitor(Args...args)1172*795d594fSAndroid Build Coastguard Worker explicit FixupRootVisitor(Args... args) : forward_(args...) {}
1173*795d594fSAndroid Build Coastguard Worker
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const1174*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
1175*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1176*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
1177*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
1178*795d594fSAndroid Build Coastguard Worker }
1179*795d594fSAndroid Build Coastguard Worker }
1180*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const1181*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
1182*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1183*795d594fSAndroid Build Coastguard Worker mirror::Object* ref = root->AsMirrorPtr();
1184*795d594fSAndroid Build Coastguard Worker mirror::Object* new_ref = forward_(ref);
1185*795d594fSAndroid Build Coastguard Worker if (ref != new_ref) {
1186*795d594fSAndroid Build Coastguard Worker root->Assign(new_ref);
1187*795d594fSAndroid Build Coastguard Worker }
1188*795d594fSAndroid Build Coastguard Worker }
1189*795d594fSAndroid Build Coastguard Worker
1190*795d594fSAndroid Build Coastguard Worker private:
1191*795d594fSAndroid Build Coastguard Worker Forward forward_;
1192*795d594fSAndroid Build Coastguard Worker };
1193*795d594fSAndroid Build Coastguard Worker
1194*795d594fSAndroid Build Coastguard Worker template <typename Forward>
1195*795d594fSAndroid Build Coastguard Worker class FixupObjectVisitor {
1196*795d594fSAndroid Build Coastguard Worker public:
FixupObjectVisitor(gc::accounting::ContinuousSpaceBitmap * visited,const Forward & forward)1197*795d594fSAndroid Build Coastguard Worker explicit FixupObjectVisitor(gc::accounting::ContinuousSpaceBitmap* visited,
1198*795d594fSAndroid Build Coastguard Worker const Forward& forward)
1199*795d594fSAndroid Build Coastguard Worker : visited_(visited), forward_(forward) {}
1200*795d594fSAndroid Build Coastguard Worker
1201*795d594fSAndroid Build Coastguard Worker // Fix up separately since we also need to fix up method entrypoints.
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const1202*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void VisitRootIfNonNull(
1203*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] mirror::CompressedReference<mirror::Object>* root) const {}
1204*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const1205*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void VisitRoot(
1206*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] mirror::CompressedReference<mirror::Object>* root) const {}
1207*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Object> obj,MemberOffset offset,bool is_static) const1208*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(ObjPtr<mirror::Object> obj,
1209*795d594fSAndroid Build Coastguard Worker MemberOffset offset,
1210*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] bool is_static) const NO_THREAD_SAFETY_ANALYSIS {
1211*795d594fSAndroid Build Coastguard Worker // Space is not yet added to the heap, don't do a read barrier.
1212*795d594fSAndroid Build Coastguard Worker mirror::Object* ref = obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(
1213*795d594fSAndroid Build Coastguard Worker offset);
1214*795d594fSAndroid Build Coastguard Worker if (ref != nullptr) {
1215*795d594fSAndroid Build Coastguard Worker // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the
1216*795d594fSAndroid Build Coastguard Worker // image.
1217*795d594fSAndroid Build Coastguard Worker obj->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(offset, forward_(ref));
1218*795d594fSAndroid Build Coastguard Worker }
1219*795d594fSAndroid Build Coastguard Worker }
1220*795d594fSAndroid Build Coastguard Worker
1221*795d594fSAndroid Build Coastguard Worker // java.lang.ref.Reference visitor.
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const1222*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const
1223*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
1224*795d594fSAndroid Build Coastguard Worker DCHECK(klass->IsTypeOfReferenceClass());
1225*795d594fSAndroid Build Coastguard Worker this->operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false);
1226*795d594fSAndroid Build Coastguard Worker }
1227*795d594fSAndroid Build Coastguard Worker
operator ()(mirror::Object * obj) const1228*795d594fSAndroid Build Coastguard Worker void operator()(mirror::Object* obj) const
1229*795d594fSAndroid Build Coastguard Worker NO_THREAD_SAFETY_ANALYSIS {
1230*795d594fSAndroid Build Coastguard Worker if (!visited_->Set(obj)) {
1231*795d594fSAndroid Build Coastguard Worker // Not already visited.
1232*795d594fSAndroid Build Coastguard Worker obj->VisitReferences</*visit native roots*/false, kVerifyNone, kWithoutReadBarrier>(
1233*795d594fSAndroid Build Coastguard Worker *this,
1234*795d594fSAndroid Build Coastguard Worker *this);
1235*795d594fSAndroid Build Coastguard Worker CHECK(!obj->IsClass());
1236*795d594fSAndroid Build Coastguard Worker }
1237*795d594fSAndroid Build Coastguard Worker }
1238*795d594fSAndroid Build Coastguard Worker
1239*795d594fSAndroid Build Coastguard Worker private:
1240*795d594fSAndroid Build Coastguard Worker gc::accounting::ContinuousSpaceBitmap* const visited_;
1241*795d594fSAndroid Build Coastguard Worker Forward forward_;
1242*795d594fSAndroid Build Coastguard Worker };
1243*795d594fSAndroid Build Coastguard Worker
1244*795d594fSAndroid Build Coastguard Worker // Relocate an image space mapped at target_base which possibly used to be at a different base
1245*795d594fSAndroid Build Coastguard Worker // address. In place means modifying a single ImageSpace in place rather than relocating from
1246*795d594fSAndroid Build Coastguard Worker // one ImageSpace to another.
1247*795d594fSAndroid Build Coastguard Worker template <PointerSize kPointerSize>
RelocateInPlace(uint32_t boot_image_begin,uint8_t * target_base,accounting::ContinuousSpaceBitmap * bitmap,const OatFile * app_oat_file,std::string * error_msg)1248*795d594fSAndroid Build Coastguard Worker static bool RelocateInPlace(uint32_t boot_image_begin,
1249*795d594fSAndroid Build Coastguard Worker uint8_t* target_base,
1250*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* bitmap,
1251*795d594fSAndroid Build Coastguard Worker const OatFile* app_oat_file,
1252*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
1253*795d594fSAndroid Build Coastguard Worker DCHECK(error_msg != nullptr);
1254*795d594fSAndroid Build Coastguard Worker // Set up sections.
1255*795d594fSAndroid Build Coastguard Worker ImageHeader* image_header = reinterpret_cast<ImageHeader*>(target_base);
1256*795d594fSAndroid Build Coastguard Worker const uint32_t boot_image_size = image_header->GetBootImageSize();
1257*795d594fSAndroid Build Coastguard Worker const ImageSection& objects_section = image_header->GetObjectsSection();
1258*795d594fSAndroid Build Coastguard Worker // Where the app image objects are mapped to.
1259*795d594fSAndroid Build Coastguard Worker uint8_t* objects_location = target_base + objects_section.Offset();
1260*795d594fSAndroid Build Coastguard Worker TimingLogger logger(__FUNCTION__, true, false);
1261*795d594fSAndroid Build Coastguard Worker RelocationRange boot_image(image_header->GetBootImageBegin(),
1262*795d594fSAndroid Build Coastguard Worker boot_image_begin,
1263*795d594fSAndroid Build Coastguard Worker boot_image_size);
1264*795d594fSAndroid Build Coastguard Worker // Metadata is everything after the objects section, use exclusion to be safe.
1265*795d594fSAndroid Build Coastguard Worker RelocationRange app_image_metadata(
1266*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(image_header->GetImageBegin()) + objects_section.End(),
1267*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(target_base) + objects_section.End(),
1268*795d594fSAndroid Build Coastguard Worker image_header->GetImageSize() - objects_section.End());
1269*795d594fSAndroid Build Coastguard Worker // App image heap objects, may be mapped in the heap.
1270*795d594fSAndroid Build Coastguard Worker RelocationRange app_image_objects(
1271*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(image_header->GetImageBegin()) + objects_section.Offset(),
1272*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(objects_location),
1273*795d594fSAndroid Build Coastguard Worker objects_section.Size());
1274*795d594fSAndroid Build Coastguard Worker // Use the oat data section since this is where the OatFile::Begin is.
1275*795d594fSAndroid Build Coastguard Worker RelocationRange app_oat(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
1276*795d594fSAndroid Build Coastguard Worker // Not necessarily in low 4GB.
1277*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(app_oat_file->Begin()),
1278*795d594fSAndroid Build Coastguard Worker image_header->GetOatDataEnd() - image_header->GetOatDataBegin());
1279*795d594fSAndroid Build Coastguard Worker VLOG(image) << "App image metadata " << app_image_metadata;
1280*795d594fSAndroid Build Coastguard Worker VLOG(image) << "App image objects " << app_image_objects;
1281*795d594fSAndroid Build Coastguard Worker VLOG(image) << "App oat " << app_oat;
1282*795d594fSAndroid Build Coastguard Worker VLOG(image) << "Boot image " << boot_image;
1283*795d594fSAndroid Build Coastguard Worker // True if we need to fixup any heap pointers.
1284*795d594fSAndroid Build Coastguard Worker const bool fixup_image = boot_image.Delta() != 0 || app_image_metadata.Delta() != 0 ||
1285*795d594fSAndroid Build Coastguard Worker app_image_objects.Delta() != 0;
1286*795d594fSAndroid Build Coastguard Worker if (!fixup_image) {
1287*795d594fSAndroid Build Coastguard Worker // Nothing to fix up.
1288*795d594fSAndroid Build Coastguard Worker return true;
1289*795d594fSAndroid Build Coastguard Worker }
1290*795d594fSAndroid Build Coastguard Worker
1291*795d594fSAndroid Build Coastguard Worker // TODO: Assert that the app image does not contain any Method, Constructor,
1292*795d594fSAndroid Build Coastguard Worker // FieldVarHandle or StaticFieldVarHandle. These require extra relocation
1293*795d594fSAndroid Build Coastguard Worker // for the `ArtMethod*` and `ArtField*` pointers they contain.
1294*795d594fSAndroid Build Coastguard Worker
1295*795d594fSAndroid Build Coastguard Worker using ForwardObject = ForwardAddress<RelocationRange, RelocationRange>;
1296*795d594fSAndroid Build Coastguard Worker ForwardObject forward_object(boot_image, app_image_objects);
1297*795d594fSAndroid Build Coastguard Worker ForwardObject forward_metadata(boot_image, app_image_metadata);
1298*795d594fSAndroid Build Coastguard Worker using ForwardCode = ForwardAddress<RelocationRange, RelocationRange>;
1299*795d594fSAndroid Build Coastguard Worker ForwardCode forward_code(boot_image, app_oat);
1300*795d594fSAndroid Build Coastguard Worker PatchObjectVisitor<kPointerSize, ForwardObject, ForwardCode> patch_object_visitor(
1301*795d594fSAndroid Build Coastguard Worker forward_object,
1302*795d594fSAndroid Build Coastguard Worker forward_metadata);
1303*795d594fSAndroid Build Coastguard Worker if (fixup_image) {
1304*795d594fSAndroid Build Coastguard Worker // Two pass approach, fix up all classes first, then fix up non class-objects.
1305*795d594fSAndroid Build Coastguard Worker // The visited bitmap is used to ensure that pointer arrays are not forwarded twice.
1306*795d594fSAndroid Build Coastguard Worker gc::accounting::ContinuousSpaceBitmap visited_bitmap(
1307*795d594fSAndroid Build Coastguard Worker gc::accounting::ContinuousSpaceBitmap::Create("Relocate bitmap",
1308*795d594fSAndroid Build Coastguard Worker target_base,
1309*795d594fSAndroid Build Coastguard Worker image_header->GetImageSize()));
1310*795d594fSAndroid Build Coastguard Worker {
1311*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("Fixup classes", &logger);
1312*795d594fSAndroid Build Coastguard Worker const auto& class_table_section = image_header->GetClassTableSection();
1313*795d594fSAndroid Build Coastguard Worker if (class_table_section.Size() > 0u) {
1314*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
1315*795d594fSAndroid Build Coastguard Worker ScopedDebugDisallowReadBarriers sddrb(Thread::Current());
1316*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::ObjectArray<mirror::Object>> image_roots = app_image_objects.ToDest(
1317*795d594fSAndroid Build Coastguard Worker image_header->GetImageRoots<kWithoutReadBarrier>().Ptr());
1318*795d594fSAndroid Build Coastguard Worker int32_t class_roots_index = enum_cast<int32_t>(ImageHeader::kClassRoots);
1319*795d594fSAndroid Build Coastguard Worker DCHECK_LT(class_roots_index, image_roots->GetLength<kVerifyNone>());
1320*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
1321*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast(boot_image.ToDest(
1322*795d594fSAndroid Build Coastguard Worker image_roots->GetWithoutChecks<kVerifyNone,
1323*795d594fSAndroid Build Coastguard Worker kWithoutReadBarrier>(class_roots_index).Ptr()));
1324*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> class_class =
1325*795d594fSAndroid Build Coastguard Worker GetClassRoot<mirror::Class, kWithoutReadBarrier>(class_roots);
1326*795d594fSAndroid Build Coastguard Worker ClassTableVisitor class_table_visitor(forward_object);
1327*795d594fSAndroid Build Coastguard Worker size_t read_count = 0u;
1328*795d594fSAndroid Build Coastguard Worker const uint8_t* data = target_base + class_table_section.Offset();
1329*795d594fSAndroid Build Coastguard Worker // We avoid making a copy of the data since we want modifications to be propagated to the
1330*795d594fSAndroid Build Coastguard Worker // memory map.
1331*795d594fSAndroid Build Coastguard Worker ClassTable::ClassSet temp_set(data, /*make_copy_of_data=*/ false, &read_count);
1332*795d594fSAndroid Build Coastguard Worker for (ClassTable::TableSlot& slot : temp_set) {
1333*795d594fSAndroid Build Coastguard Worker slot.VisitRoot(class_table_visitor);
1334*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> klass = slot.Read<kWithoutReadBarrier>();
1335*795d594fSAndroid Build Coastguard Worker if (!app_image_objects.InDest(klass.Ptr())) {
1336*795d594fSAndroid Build Coastguard Worker continue;
1337*795d594fSAndroid Build Coastguard Worker }
1338*795d594fSAndroid Build Coastguard Worker const bool already_marked = visited_bitmap.Set(klass.Ptr());
1339*795d594fSAndroid Build Coastguard Worker CHECK(!already_marked) << "App image class already visited";
1340*795d594fSAndroid Build Coastguard Worker patch_object_visitor.VisitClass(klass, class_class);
1341*795d594fSAndroid Build Coastguard Worker // Then patch the non-embedded vtable and iftable.
1342*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::PointerArray> vtable =
1343*795d594fSAndroid Build Coastguard Worker klass->GetVTable<kVerifyNone, kWithoutReadBarrier>();
1344*795d594fSAndroid Build Coastguard Worker if (vtable != nullptr &&
1345*795d594fSAndroid Build Coastguard Worker app_image_objects.InDest(vtable.Ptr()) &&
1346*795d594fSAndroid Build Coastguard Worker !visited_bitmap.Set(vtable.Ptr())) {
1347*795d594fSAndroid Build Coastguard Worker patch_object_visitor.VisitPointerArray(vtable);
1348*795d594fSAndroid Build Coastguard Worker }
1349*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::IfTable> iftable = klass->GetIfTable<kVerifyNone, kWithoutReadBarrier>();
1350*795d594fSAndroid Build Coastguard Worker if (iftable != nullptr && app_image_objects.InDest(iftable.Ptr())) {
1351*795d594fSAndroid Build Coastguard Worker // Avoid processing the fields of iftable since we will process them later anyways
1352*795d594fSAndroid Build Coastguard Worker // below.
1353*795d594fSAndroid Build Coastguard Worker int32_t ifcount = klass->GetIfTableCount<kVerifyNone>();
1354*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0; i != ifcount; ++i) {
1355*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::PointerArray> unpatched_ifarray =
1356*795d594fSAndroid Build Coastguard Worker iftable->GetMethodArrayOrNull<kVerifyNone, kWithoutReadBarrier>(i);
1357*795d594fSAndroid Build Coastguard Worker if (unpatched_ifarray != nullptr) {
1358*795d594fSAndroid Build Coastguard Worker // The iftable has not been patched, so we need to explicitly adjust the pointer.
1359*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::PointerArray> ifarray = forward_object(unpatched_ifarray.Ptr());
1360*795d594fSAndroid Build Coastguard Worker if (app_image_objects.InDest(ifarray.Ptr()) &&
1361*795d594fSAndroid Build Coastguard Worker !visited_bitmap.Set(ifarray.Ptr())) {
1362*795d594fSAndroid Build Coastguard Worker patch_object_visitor.VisitPointerArray(ifarray);
1363*795d594fSAndroid Build Coastguard Worker }
1364*795d594fSAndroid Build Coastguard Worker }
1365*795d594fSAndroid Build Coastguard Worker }
1366*795d594fSAndroid Build Coastguard Worker }
1367*795d594fSAndroid Build Coastguard Worker }
1368*795d594fSAndroid Build Coastguard Worker }
1369*795d594fSAndroid Build Coastguard Worker }
1370*795d594fSAndroid Build Coastguard Worker
1371*795d594fSAndroid Build Coastguard Worker // Fixup objects may read fields in the boot image so we hold the mutator lock (although it is
1372*795d594fSAndroid Build Coastguard Worker // probably not required).
1373*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("Fixup objects", &logger);
1374*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
1375*795d594fSAndroid Build Coastguard Worker ScopedDebugDisallowReadBarriers sddrb(Thread::Current());
1376*795d594fSAndroid Build Coastguard Worker // Need to update the image to be at the target base.
1377*795d594fSAndroid Build Coastguard Worker uintptr_t objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset());
1378*795d594fSAndroid Build Coastguard Worker uintptr_t objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End());
1379*795d594fSAndroid Build Coastguard Worker FixupObjectVisitor<ForwardObject> fixup_object_visitor(&visited_bitmap, forward_object);
1380*795d594fSAndroid Build Coastguard Worker bitmap->VisitMarkedRange(objects_begin, objects_end, fixup_object_visitor);
1381*795d594fSAndroid Build Coastguard Worker // Fixup image roots.
1382*795d594fSAndroid Build Coastguard Worker CHECK(app_image_objects.InSource(reinterpret_cast<uintptr_t>(
1383*795d594fSAndroid Build Coastguard Worker image_header->GetImageRoots<kWithoutReadBarrier>().Ptr())));
1384*795d594fSAndroid Build Coastguard Worker image_header->RelocateImageReferences(app_image_objects.Delta());
1385*795d594fSAndroid Build Coastguard Worker image_header->RelocateBootImageReferences(boot_image.Delta());
1386*795d594fSAndroid Build Coastguard Worker CHECK_EQ(image_header->GetImageBegin(), target_base);
1387*795d594fSAndroid Build Coastguard Worker
1388*795d594fSAndroid Build Coastguard Worker // Fix up dex cache arrays.
1389*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches =
1390*795d594fSAndroid Build Coastguard Worker image_header->GetImageRoot<kWithoutReadBarrier>(ImageHeader::kDexCaches)
1391*795d594fSAndroid Build Coastguard Worker ->AsObjectArray<mirror::DexCache, kVerifyNone>();
1392*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0, count = dex_caches->GetLength(); i < count; ++i) {
1393*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::DexCache> dex_cache =
1394*795d594fSAndroid Build Coastguard Worker dex_caches->GetWithoutChecks<kVerifyNone, kWithoutReadBarrier>(i);
1395*795d594fSAndroid Build Coastguard Worker patch_object_visitor.VisitDexCacheArrays(dex_cache);
1396*795d594fSAndroid Build Coastguard Worker }
1397*795d594fSAndroid Build Coastguard Worker }
1398*795d594fSAndroid Build Coastguard Worker {
1399*795d594fSAndroid Build Coastguard Worker // Only touches objects in the app image, no need for mutator lock.
1400*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("Fixup methods", &logger);
1401*795d594fSAndroid Build Coastguard Worker ScopedDebugDisallowReadBarriers sddrb(Thread::Current());
1402*795d594fSAndroid Build Coastguard Worker image_header->VisitPackedArtMethods([&](ArtMethod& method) NO_THREAD_SAFETY_ANALYSIS {
1403*795d594fSAndroid Build Coastguard Worker // TODO: Consider a separate visitor for runtime vs normal methods.
1404*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(method.IsRuntimeMethod())) {
1405*795d594fSAndroid Build Coastguard Worker ImtConflictTable* table = method.GetImtConflictTable(kPointerSize);
1406*795d594fSAndroid Build Coastguard Worker if (table != nullptr) {
1407*795d594fSAndroid Build Coastguard Worker ImtConflictTable* new_table = forward_metadata(table);
1408*795d594fSAndroid Build Coastguard Worker if (table != new_table) {
1409*795d594fSAndroid Build Coastguard Worker method.SetImtConflictTable(new_table, kPointerSize);
1410*795d594fSAndroid Build Coastguard Worker }
1411*795d594fSAndroid Build Coastguard Worker }
1412*795d594fSAndroid Build Coastguard Worker } else {
1413*795d594fSAndroid Build Coastguard Worker patch_object_visitor.PatchGcRoot(&method.DeclaringClassRoot());
1414*795d594fSAndroid Build Coastguard Worker if (method.IsNative()) {
1415*795d594fSAndroid Build Coastguard Worker const void* old_native_code = method.GetEntryPointFromJniPtrSize(kPointerSize);
1416*795d594fSAndroid Build Coastguard Worker const void* new_native_code = forward_code(old_native_code);
1417*795d594fSAndroid Build Coastguard Worker if (old_native_code != new_native_code) {
1418*795d594fSAndroid Build Coastguard Worker method.SetEntryPointFromJniPtrSize(new_native_code, kPointerSize);
1419*795d594fSAndroid Build Coastguard Worker }
1420*795d594fSAndroid Build Coastguard Worker }
1421*795d594fSAndroid Build Coastguard Worker }
1422*795d594fSAndroid Build Coastguard Worker const void* old_code = method.GetEntryPointFromQuickCompiledCodePtrSize(kPointerSize);
1423*795d594fSAndroid Build Coastguard Worker const void* new_code = forward_code(old_code);
1424*795d594fSAndroid Build Coastguard Worker if (old_code != new_code) {
1425*795d594fSAndroid Build Coastguard Worker method.SetEntryPointFromQuickCompiledCode(new_code);
1426*795d594fSAndroid Build Coastguard Worker }
1427*795d594fSAndroid Build Coastguard Worker }, target_base, kPointerSize);
1428*795d594fSAndroid Build Coastguard Worker }
1429*795d594fSAndroid Build Coastguard Worker if (fixup_image) {
1430*795d594fSAndroid Build Coastguard Worker {
1431*795d594fSAndroid Build Coastguard Worker // Only touches objects in the app image, no need for mutator lock.
1432*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("Fixup fields", &logger);
1433*795d594fSAndroid Build Coastguard Worker ScopedDebugDisallowReadBarriers sddrb(Thread::Current());
1434*795d594fSAndroid Build Coastguard Worker image_header->VisitPackedArtFields([&](ArtField& field) NO_THREAD_SAFETY_ANALYSIS {
1435*795d594fSAndroid Build Coastguard Worker patch_object_visitor.template PatchGcRoot</*kMayBeNull=*/ false>(
1436*795d594fSAndroid Build Coastguard Worker &field.DeclaringClassRoot());
1437*795d594fSAndroid Build Coastguard Worker }, target_base);
1438*795d594fSAndroid Build Coastguard Worker }
1439*795d594fSAndroid Build Coastguard Worker {
1440*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("Fixup imt", &logger);
1441*795d594fSAndroid Build Coastguard Worker ScopedDebugDisallowReadBarriers sddrb(Thread::Current());
1442*795d594fSAndroid Build Coastguard Worker image_header->VisitPackedImTables(forward_metadata, target_base, kPointerSize);
1443*795d594fSAndroid Build Coastguard Worker }
1444*795d594fSAndroid Build Coastguard Worker {
1445*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("Fixup conflict tables", &logger);
1446*795d594fSAndroid Build Coastguard Worker ScopedDebugDisallowReadBarriers sddrb(Thread::Current());
1447*795d594fSAndroid Build Coastguard Worker image_header->VisitPackedImtConflictTables(forward_metadata, target_base, kPointerSize);
1448*795d594fSAndroid Build Coastguard Worker }
1449*795d594fSAndroid Build Coastguard Worker // Fix up the intern table.
1450*795d594fSAndroid Build Coastguard Worker const auto& intern_table_section = image_header->GetInternedStringsSection();
1451*795d594fSAndroid Build Coastguard Worker if (intern_table_section.Size() > 0u) {
1452*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("Fixup intern table", &logger);
1453*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
1454*795d594fSAndroid Build Coastguard Worker ScopedDebugDisallowReadBarriers sddrb(Thread::Current());
1455*795d594fSAndroid Build Coastguard Worker // Fixup the pointers in the newly written intern table to contain image addresses.
1456*795d594fSAndroid Build Coastguard Worker InternTable temp_intern_table;
1457*795d594fSAndroid Build Coastguard Worker // Note that we require that ReadFromMemory does not make an internal copy of the elements
1458*795d594fSAndroid Build Coastguard Worker // so that the VisitRoots() will update the memory directly rather than the copies.
1459*795d594fSAndroid Build Coastguard Worker temp_intern_table.AddTableFromMemory(target_base + intern_table_section.Offset(),
1460*795d594fSAndroid Build Coastguard Worker [&](InternTable::UnorderedSet& strings)
1461*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1462*795d594fSAndroid Build Coastguard Worker for (GcRoot<mirror::String>& root : strings) {
1463*795d594fSAndroid Build Coastguard Worker root = GcRoot<mirror::String>(forward_object(root.Read<kWithoutReadBarrier>()));
1464*795d594fSAndroid Build Coastguard Worker }
1465*795d594fSAndroid Build Coastguard Worker }, /*is_boot_image=*/ false);
1466*795d594fSAndroid Build Coastguard Worker }
1467*795d594fSAndroid Build Coastguard Worker }
1468*795d594fSAndroid Build Coastguard Worker if (VLOG_IS_ON(image)) {
1469*795d594fSAndroid Build Coastguard Worker logger.Dump(LOG_STREAM(INFO));
1470*795d594fSAndroid Build Coastguard Worker }
1471*795d594fSAndroid Build Coastguard Worker return true;
1472*795d594fSAndroid Build Coastguard Worker }
1473*795d594fSAndroid Build Coastguard Worker };
1474*795d594fSAndroid Build Coastguard Worker
AppendImageChecksum(uint32_t component_count,uint32_t checksum,std::string * checksums)1475*795d594fSAndroid Build Coastguard Worker void ImageSpace::AppendImageChecksum(uint32_t component_count,
1476*795d594fSAndroid Build Coastguard Worker uint32_t checksum,
1477*795d594fSAndroid Build Coastguard Worker /*inout*/ std::string* checksums) {
1478*795d594fSAndroid Build Coastguard Worker static_assert(ImageSpace::kImageChecksumPrefix == 'i', "Format prefix check.");
1479*795d594fSAndroid Build Coastguard Worker StringAppendF(checksums, "i;%u/%08x", component_count, checksum);
1480*795d594fSAndroid Build Coastguard Worker }
1481*795d594fSAndroid Build Coastguard Worker
CheckAndRemoveImageChecksum(uint32_t component_count,uint32_t checksum,std::string_view * oat_checksums,std::string * error_msg)1482*795d594fSAndroid Build Coastguard Worker static bool CheckAndRemoveImageChecksum(uint32_t component_count,
1483*795d594fSAndroid Build Coastguard Worker uint32_t checksum,
1484*795d594fSAndroid Build Coastguard Worker /*inout*/std::string_view* oat_checksums,
1485*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
1486*795d594fSAndroid Build Coastguard Worker std::string image_checksum;
1487*795d594fSAndroid Build Coastguard Worker ImageSpace::AppendImageChecksum(component_count, checksum, &image_checksum);
1488*795d594fSAndroid Build Coastguard Worker if (!oat_checksums->starts_with(image_checksum)) {
1489*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Image checksum mismatch, expected %s to start with %s",
1490*795d594fSAndroid Build Coastguard Worker std::string(*oat_checksums).c_str(),
1491*795d594fSAndroid Build Coastguard Worker image_checksum.c_str());
1492*795d594fSAndroid Build Coastguard Worker return false;
1493*795d594fSAndroid Build Coastguard Worker }
1494*795d594fSAndroid Build Coastguard Worker oat_checksums->remove_prefix(image_checksum.size());
1495*795d594fSAndroid Build Coastguard Worker return true;
1496*795d594fSAndroid Build Coastguard Worker }
1497*795d594fSAndroid Build Coastguard Worker
GetPrimaryImageLocation()1498*795d594fSAndroid Build Coastguard Worker std::string ImageSpace::BootImageLayout::GetPrimaryImageLocation() {
1499*795d594fSAndroid Build Coastguard Worker DCHECK(!image_locations_.empty());
1500*795d594fSAndroid Build Coastguard Worker std::string location = image_locations_[0];
1501*795d594fSAndroid Build Coastguard Worker size_t profile_separator_pos = location.find(kProfileSeparator);
1502*795d594fSAndroid Build Coastguard Worker if (profile_separator_pos != std::string::npos) {
1503*795d594fSAndroid Build Coastguard Worker location.resize(profile_separator_pos);
1504*795d594fSAndroid Build Coastguard Worker }
1505*795d594fSAndroid Build Coastguard Worker if (location.find('/') == std::string::npos) {
1506*795d594fSAndroid Build Coastguard Worker // No path, so use the path from the first boot class path component.
1507*795d594fSAndroid Build Coastguard Worker size_t slash_pos = boot_class_path_.empty()
1508*795d594fSAndroid Build Coastguard Worker ? std::string::npos
1509*795d594fSAndroid Build Coastguard Worker : boot_class_path_[0].rfind('/');
1510*795d594fSAndroid Build Coastguard Worker if (slash_pos == std::string::npos) {
1511*795d594fSAndroid Build Coastguard Worker return std::string();
1512*795d594fSAndroid Build Coastguard Worker }
1513*795d594fSAndroid Build Coastguard Worker location.insert(0u, boot_class_path_[0].substr(0u, slash_pos + 1u));
1514*795d594fSAndroid Build Coastguard Worker }
1515*795d594fSAndroid Build Coastguard Worker return location;
1516*795d594fSAndroid Build Coastguard Worker }
1517*795d594fSAndroid Build Coastguard Worker
VerifyImageLocation(ArrayRef<const std::string> components,size_t * named_components_count,std::string * error_msg)1518*795d594fSAndroid Build Coastguard Worker bool ImageSpace::BootImageLayout::VerifyImageLocation(
1519*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> components,
1520*795d594fSAndroid Build Coastguard Worker /*out*/size_t* named_components_count,
1521*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
1522*795d594fSAndroid Build Coastguard Worker DCHECK(named_components_count != nullptr);
1523*795d594fSAndroid Build Coastguard Worker
1524*795d594fSAndroid Build Coastguard Worker // Validate boot class path. Require a path and non-empty name in each component.
1525*795d594fSAndroid Build Coastguard Worker for (const std::string& bcp_component : boot_class_path_) {
1526*795d594fSAndroid Build Coastguard Worker size_t bcp_slash_pos = bcp_component.rfind('/');
1527*795d594fSAndroid Build Coastguard Worker if (bcp_slash_pos == std::string::npos || bcp_slash_pos == bcp_component.size() - 1u) {
1528*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Invalid boot class path component: %s", bcp_component.c_str());
1529*795d594fSAndroid Build Coastguard Worker return false;
1530*795d594fSAndroid Build Coastguard Worker }
1531*795d594fSAndroid Build Coastguard Worker }
1532*795d594fSAndroid Build Coastguard Worker
1533*795d594fSAndroid Build Coastguard Worker // Validate the format of image location components.
1534*795d594fSAndroid Build Coastguard Worker size_t components_size = components.size();
1535*795d594fSAndroid Build Coastguard Worker if (components_size == 0u) {
1536*795d594fSAndroid Build Coastguard Worker *error_msg = "Empty image location.";
1537*795d594fSAndroid Build Coastguard Worker return false;
1538*795d594fSAndroid Build Coastguard Worker }
1539*795d594fSAndroid Build Coastguard Worker size_t wildcards_start = components_size; // No wildcards.
1540*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i != components_size; ++i) {
1541*795d594fSAndroid Build Coastguard Worker const std::string& component = components[i];
1542*795d594fSAndroid Build Coastguard Worker DCHECK(!component.empty()); // Guaranteed by Split().
1543*795d594fSAndroid Build Coastguard Worker std::vector<std::string> parts = android::base::Split(component, {kProfileSeparator});
1544*795d594fSAndroid Build Coastguard Worker size_t wildcard_pos = component.find('*');
1545*795d594fSAndroid Build Coastguard Worker if (wildcard_pos == std::string::npos) {
1546*795d594fSAndroid Build Coastguard Worker if (wildcards_start != components.size()) {
1547*795d594fSAndroid Build Coastguard Worker *error_msg =
1548*795d594fSAndroid Build Coastguard Worker StringPrintf("Image component without wildcard after component with wildcard: %s",
1549*795d594fSAndroid Build Coastguard Worker component.c_str());
1550*795d594fSAndroid Build Coastguard Worker return false;
1551*795d594fSAndroid Build Coastguard Worker }
1552*795d594fSAndroid Build Coastguard Worker for (size_t j = 0; j < parts.size(); j++) {
1553*795d594fSAndroid Build Coastguard Worker if (parts[j].empty()) {
1554*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Missing component and/or profile name in %s",
1555*795d594fSAndroid Build Coastguard Worker component.c_str());
1556*795d594fSAndroid Build Coastguard Worker return false;
1557*795d594fSAndroid Build Coastguard Worker }
1558*795d594fSAndroid Build Coastguard Worker if (parts[j].back() == '/') {
1559*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("%s name ends with path separator: %s",
1560*795d594fSAndroid Build Coastguard Worker j == 0 ? "Image component" : "Profile",
1561*795d594fSAndroid Build Coastguard Worker component.c_str());
1562*795d594fSAndroid Build Coastguard Worker return false;
1563*795d594fSAndroid Build Coastguard Worker }
1564*795d594fSAndroid Build Coastguard Worker }
1565*795d594fSAndroid Build Coastguard Worker } else {
1566*795d594fSAndroid Build Coastguard Worker if (parts.size() > 1) {
1567*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Unsupproted wildcard (*) and profile delimiter (!) in %s",
1568*795d594fSAndroid Build Coastguard Worker component.c_str());
1569*795d594fSAndroid Build Coastguard Worker return false;
1570*795d594fSAndroid Build Coastguard Worker }
1571*795d594fSAndroid Build Coastguard Worker if (wildcards_start == components_size) {
1572*795d594fSAndroid Build Coastguard Worker wildcards_start = i;
1573*795d594fSAndroid Build Coastguard Worker }
1574*795d594fSAndroid Build Coastguard Worker // Wildcard must be the last character.
1575*795d594fSAndroid Build Coastguard Worker if (wildcard_pos != component.size() - 1u) {
1576*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Unsupported wildcard (*) position in %s", component.c_str());
1577*795d594fSAndroid Build Coastguard Worker return false;
1578*795d594fSAndroid Build Coastguard Worker }
1579*795d594fSAndroid Build Coastguard Worker // And it must be either plain wildcard or preceded by a path separator.
1580*795d594fSAndroid Build Coastguard Worker if (component.size() != 1u && component[wildcard_pos - 1u] != '/') {
1581*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Non-plain wildcard (*) not preceded by path separator '/': %s",
1582*795d594fSAndroid Build Coastguard Worker component.c_str());
1583*795d594fSAndroid Build Coastguard Worker return false;
1584*795d594fSAndroid Build Coastguard Worker }
1585*795d594fSAndroid Build Coastguard Worker if (i == 0) {
1586*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Primary component contains wildcard (*): %s", component.c_str());
1587*795d594fSAndroid Build Coastguard Worker return false;
1588*795d594fSAndroid Build Coastguard Worker }
1589*795d594fSAndroid Build Coastguard Worker }
1590*795d594fSAndroid Build Coastguard Worker }
1591*795d594fSAndroid Build Coastguard Worker
1592*795d594fSAndroid Build Coastguard Worker *named_components_count = wildcards_start;
1593*795d594fSAndroid Build Coastguard Worker return true;
1594*795d594fSAndroid Build Coastguard Worker }
1595*795d594fSAndroid Build Coastguard Worker
MatchNamedComponents(ArrayRef<const std::string> named_components,std::vector<NamedComponentLocation> * named_component_locations,std::string * error_msg)1596*795d594fSAndroid Build Coastguard Worker bool ImageSpace::BootImageLayout::MatchNamedComponents(
1597*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> named_components,
1598*795d594fSAndroid Build Coastguard Worker /*out*/std::vector<NamedComponentLocation>* named_component_locations,
1599*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
1600*795d594fSAndroid Build Coastguard Worker DCHECK(!named_components.empty());
1601*795d594fSAndroid Build Coastguard Worker DCHECK(named_component_locations->empty());
1602*795d594fSAndroid Build Coastguard Worker named_component_locations->reserve(named_components.size());
1603*795d594fSAndroid Build Coastguard Worker size_t bcp_component_count = boot_class_path_.size();
1604*795d594fSAndroid Build Coastguard Worker size_t bcp_pos = 0;
1605*795d594fSAndroid Build Coastguard Worker std::string base_name;
1606*795d594fSAndroid Build Coastguard Worker for (size_t i = 0, size = named_components.size(); i != size; ++i) {
1607*795d594fSAndroid Build Coastguard Worker std::string component = named_components[i];
1608*795d594fSAndroid Build Coastguard Worker std::vector<std::string> profile_filenames; // Empty.
1609*795d594fSAndroid Build Coastguard Worker std::vector<std::string> parts = android::base::Split(component, {kProfileSeparator});
1610*795d594fSAndroid Build Coastguard Worker for (size_t j = 0; j < parts.size(); j++) {
1611*795d594fSAndroid Build Coastguard Worker if (j == 0) {
1612*795d594fSAndroid Build Coastguard Worker component = std::move(parts[j]);
1613*795d594fSAndroid Build Coastguard Worker DCHECK(!component.empty()); // Checked by VerifyImageLocation()
1614*795d594fSAndroid Build Coastguard Worker } else {
1615*795d594fSAndroid Build Coastguard Worker profile_filenames.push_back(std::move(parts[j]));
1616*795d594fSAndroid Build Coastguard Worker DCHECK(!profile_filenames.back().empty()); // Checked by VerifyImageLocation()
1617*795d594fSAndroid Build Coastguard Worker }
1618*795d594fSAndroid Build Coastguard Worker }
1619*795d594fSAndroid Build Coastguard Worker size_t slash_pos = component.rfind('/');
1620*795d594fSAndroid Build Coastguard Worker std::string base_location;
1621*795d594fSAndroid Build Coastguard Worker if (i == 0u) {
1622*795d594fSAndroid Build Coastguard Worker // The primary boot image name is taken as provided. It forms the base
1623*795d594fSAndroid Build Coastguard Worker // for expanding the extension filenames.
1624*795d594fSAndroid Build Coastguard Worker if (slash_pos != std::string::npos) {
1625*795d594fSAndroid Build Coastguard Worker base_name = component.substr(slash_pos + 1u);
1626*795d594fSAndroid Build Coastguard Worker base_location = component;
1627*795d594fSAndroid Build Coastguard Worker } else {
1628*795d594fSAndroid Build Coastguard Worker base_name = component;
1629*795d594fSAndroid Build Coastguard Worker base_location = GetBcpComponentPath(0u) + component;
1630*795d594fSAndroid Build Coastguard Worker }
1631*795d594fSAndroid Build Coastguard Worker } else {
1632*795d594fSAndroid Build Coastguard Worker std::string to_match;
1633*795d594fSAndroid Build Coastguard Worker if (slash_pos != std::string::npos) {
1634*795d594fSAndroid Build Coastguard Worker // If we have the full path, we just need to match the filename to the BCP component.
1635*795d594fSAndroid Build Coastguard Worker base_location = component.substr(0u, slash_pos + 1u) + base_name;
1636*795d594fSAndroid Build Coastguard Worker to_match = component;
1637*795d594fSAndroid Build Coastguard Worker }
1638*795d594fSAndroid Build Coastguard Worker while (true) {
1639*795d594fSAndroid Build Coastguard Worker if (slash_pos == std::string::npos) {
1640*795d594fSAndroid Build Coastguard Worker // If we do not have a full path, we need to update the path based on the BCP location.
1641*795d594fSAndroid Build Coastguard Worker std::string path = GetBcpComponentPath(bcp_pos);
1642*795d594fSAndroid Build Coastguard Worker to_match = path + component;
1643*795d594fSAndroid Build Coastguard Worker base_location = path + base_name;
1644*795d594fSAndroid Build Coastguard Worker }
1645*795d594fSAndroid Build Coastguard Worker if (ExpandLocation(base_location, bcp_pos) == to_match) {
1646*795d594fSAndroid Build Coastguard Worker break;
1647*795d594fSAndroid Build Coastguard Worker }
1648*795d594fSAndroid Build Coastguard Worker ++bcp_pos;
1649*795d594fSAndroid Build Coastguard Worker if (bcp_pos == bcp_component_count) {
1650*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Image component %s does not match a boot class path component",
1651*795d594fSAndroid Build Coastguard Worker component.c_str());
1652*795d594fSAndroid Build Coastguard Worker return false;
1653*795d594fSAndroid Build Coastguard Worker }
1654*795d594fSAndroid Build Coastguard Worker }
1655*795d594fSAndroid Build Coastguard Worker }
1656*795d594fSAndroid Build Coastguard Worker for (std::string& profile_filename : profile_filenames) {
1657*795d594fSAndroid Build Coastguard Worker if (profile_filename.find('/') == std::string::npos) {
1658*795d594fSAndroid Build Coastguard Worker profile_filename.insert(/*pos*/ 0u, GetBcpComponentPath(bcp_pos));
1659*795d594fSAndroid Build Coastguard Worker }
1660*795d594fSAndroid Build Coastguard Worker }
1661*795d594fSAndroid Build Coastguard Worker NamedComponentLocation location;
1662*795d594fSAndroid Build Coastguard Worker location.base_location = base_location;
1663*795d594fSAndroid Build Coastguard Worker location.bcp_index = bcp_pos;
1664*795d594fSAndroid Build Coastguard Worker location.profile_filenames = profile_filenames;
1665*795d594fSAndroid Build Coastguard Worker named_component_locations->push_back(location);
1666*795d594fSAndroid Build Coastguard Worker ++bcp_pos;
1667*795d594fSAndroid Build Coastguard Worker }
1668*795d594fSAndroid Build Coastguard Worker return true;
1669*795d594fSAndroid Build Coastguard Worker }
1670*795d594fSAndroid Build Coastguard Worker
ValidateBootImageChecksum(const char * file_description,const ImageHeader & header,std::string * error_msg)1671*795d594fSAndroid Build Coastguard Worker bool ImageSpace::BootImageLayout::ValidateBootImageChecksum(const char* file_description,
1672*795d594fSAndroid Build Coastguard Worker const ImageHeader& header,
1673*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
1674*795d594fSAndroid Build Coastguard Worker uint32_t boot_image_component_count = header.GetBootImageComponentCount();
1675*795d594fSAndroid Build Coastguard Worker if (chunks_.empty() != (boot_image_component_count == 0u)) {
1676*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Unexpected boot image component count in %s: %u, %s",
1677*795d594fSAndroid Build Coastguard Worker file_description,
1678*795d594fSAndroid Build Coastguard Worker boot_image_component_count,
1679*795d594fSAndroid Build Coastguard Worker chunks_.empty() ? "should be 0" : "should not be 0");
1680*795d594fSAndroid Build Coastguard Worker return false;
1681*795d594fSAndroid Build Coastguard Worker }
1682*795d594fSAndroid Build Coastguard Worker uint32_t component_count = 0u;
1683*795d594fSAndroid Build Coastguard Worker uint32_t composite_checksum = 0u;
1684*795d594fSAndroid Build Coastguard Worker uint64_t boot_image_size = 0u;
1685*795d594fSAndroid Build Coastguard Worker for (const ImageChunk& chunk : chunks_) {
1686*795d594fSAndroid Build Coastguard Worker if (component_count == boot_image_component_count) {
1687*795d594fSAndroid Build Coastguard Worker break; // Hit the component count.
1688*795d594fSAndroid Build Coastguard Worker }
1689*795d594fSAndroid Build Coastguard Worker if (chunk.start_index != component_count) {
1690*795d594fSAndroid Build Coastguard Worker break; // End of contiguous chunks, fail below; same as reaching end of `chunks_`.
1691*795d594fSAndroid Build Coastguard Worker }
1692*795d594fSAndroid Build Coastguard Worker if (chunk.component_count > boot_image_component_count - component_count) {
1693*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Boot image component count in %s ends in the middle of a chunk, "
1694*795d594fSAndroid Build Coastguard Worker "%u is between %u and %u",
1695*795d594fSAndroid Build Coastguard Worker file_description,
1696*795d594fSAndroid Build Coastguard Worker boot_image_component_count,
1697*795d594fSAndroid Build Coastguard Worker component_count,
1698*795d594fSAndroid Build Coastguard Worker component_count + chunk.component_count);
1699*795d594fSAndroid Build Coastguard Worker return false;
1700*795d594fSAndroid Build Coastguard Worker }
1701*795d594fSAndroid Build Coastguard Worker component_count += chunk.component_count;
1702*795d594fSAndroid Build Coastguard Worker composite_checksum ^= chunk.checksum;
1703*795d594fSAndroid Build Coastguard Worker boot_image_size += chunk.reservation_size;
1704*795d594fSAndroid Build Coastguard Worker }
1705*795d594fSAndroid Build Coastguard Worker DCHECK_LE(component_count, boot_image_component_count);
1706*795d594fSAndroid Build Coastguard Worker if (component_count != boot_image_component_count) {
1707*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Missing boot image components for checksum in %s: %u > %u",
1708*795d594fSAndroid Build Coastguard Worker file_description,
1709*795d594fSAndroid Build Coastguard Worker boot_image_component_count,
1710*795d594fSAndroid Build Coastguard Worker component_count);
1711*795d594fSAndroid Build Coastguard Worker return false;
1712*795d594fSAndroid Build Coastguard Worker }
1713*795d594fSAndroid Build Coastguard Worker if (composite_checksum != header.GetBootImageChecksum()) {
1714*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Boot image checksum mismatch in %s: 0x%08x != 0x%08x",
1715*795d594fSAndroid Build Coastguard Worker file_description,
1716*795d594fSAndroid Build Coastguard Worker header.GetBootImageChecksum(),
1717*795d594fSAndroid Build Coastguard Worker composite_checksum);
1718*795d594fSAndroid Build Coastguard Worker return false;
1719*795d594fSAndroid Build Coastguard Worker }
1720*795d594fSAndroid Build Coastguard Worker if (boot_image_size != header.GetBootImageSize()) {
1721*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Boot image size mismatch in %s: 0x%08x != 0x%08" PRIx64,
1722*795d594fSAndroid Build Coastguard Worker file_description,
1723*795d594fSAndroid Build Coastguard Worker header.GetBootImageSize(),
1724*795d594fSAndroid Build Coastguard Worker boot_image_size);
1725*795d594fSAndroid Build Coastguard Worker return false;
1726*795d594fSAndroid Build Coastguard Worker }
1727*795d594fSAndroid Build Coastguard Worker return true;
1728*795d594fSAndroid Build Coastguard Worker }
1729*795d594fSAndroid Build Coastguard Worker
ValidateHeader(const ImageHeader & header,size_t bcp_index,const char * file_description,std::string * error_msg)1730*795d594fSAndroid Build Coastguard Worker bool ImageSpace::BootImageLayout::ValidateHeader(const ImageHeader& header,
1731*795d594fSAndroid Build Coastguard Worker size_t bcp_index,
1732*795d594fSAndroid Build Coastguard Worker const char* file_description,
1733*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
1734*795d594fSAndroid Build Coastguard Worker size_t bcp_component_count = boot_class_path_.size();
1735*795d594fSAndroid Build Coastguard Worker DCHECK_LT(bcp_index, bcp_component_count);
1736*795d594fSAndroid Build Coastguard Worker size_t allowed_component_count = bcp_component_count - bcp_index;
1737*795d594fSAndroid Build Coastguard Worker DCHECK_LE(total_reservation_size_, kMaxTotalImageReservationSize);
1738*795d594fSAndroid Build Coastguard Worker size_t allowed_reservation_size = kMaxTotalImageReservationSize - total_reservation_size_;
1739*795d594fSAndroid Build Coastguard Worker
1740*795d594fSAndroid Build Coastguard Worker if (header.GetComponentCount() == 0u ||
1741*795d594fSAndroid Build Coastguard Worker header.GetComponentCount() > allowed_component_count) {
1742*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Unexpected component count in %s, received %u, "
1743*795d594fSAndroid Build Coastguard Worker "expected non-zero and <= %zu",
1744*795d594fSAndroid Build Coastguard Worker file_description,
1745*795d594fSAndroid Build Coastguard Worker header.GetComponentCount(),
1746*795d594fSAndroid Build Coastguard Worker allowed_component_count);
1747*795d594fSAndroid Build Coastguard Worker return false;
1748*795d594fSAndroid Build Coastguard Worker }
1749*795d594fSAndroid Build Coastguard Worker if (header.GetImageReservationSize() > allowed_reservation_size) {
1750*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Reservation size too big in %s: %u > %zu",
1751*795d594fSAndroid Build Coastguard Worker file_description,
1752*795d594fSAndroid Build Coastguard Worker header.GetImageReservationSize(),
1753*795d594fSAndroid Build Coastguard Worker allowed_reservation_size);
1754*795d594fSAndroid Build Coastguard Worker return false;
1755*795d594fSAndroid Build Coastguard Worker }
1756*795d594fSAndroid Build Coastguard Worker if (!ValidateBootImageChecksum(file_description, header, error_msg)) {
1757*795d594fSAndroid Build Coastguard Worker return false;
1758*795d594fSAndroid Build Coastguard Worker }
1759*795d594fSAndroid Build Coastguard Worker
1760*795d594fSAndroid Build Coastguard Worker return true;
1761*795d594fSAndroid Build Coastguard Worker }
1762*795d594fSAndroid Build Coastguard Worker
ValidateOatFile(const std::string & base_location,const std::string & base_filename,size_t bcp_index,size_t component_count,std::string * error_msg)1763*795d594fSAndroid Build Coastguard Worker bool ImageSpace::BootImageLayout::ValidateOatFile(
1764*795d594fSAndroid Build Coastguard Worker const std::string& base_location,
1765*795d594fSAndroid Build Coastguard Worker const std::string& base_filename,
1766*795d594fSAndroid Build Coastguard Worker size_t bcp_index,
1767*795d594fSAndroid Build Coastguard Worker size_t component_count,
1768*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
1769*795d594fSAndroid Build Coastguard Worker std::string art_filename = ExpandLocation(base_filename, bcp_index);
1770*795d594fSAndroid Build Coastguard Worker std::string art_location = ExpandLocation(base_location, bcp_index);
1771*795d594fSAndroid Build Coastguard Worker std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(art_filename);
1772*795d594fSAndroid Build Coastguard Worker std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(art_location);
1773*795d594fSAndroid Build Coastguard Worker int oat_fd = bcp_index < boot_class_path_oat_files_.size()
1774*795d594fSAndroid Build Coastguard Worker ? boot_class_path_oat_files_[bcp_index].Fd()
1775*795d594fSAndroid Build Coastguard Worker : -1;
1776*795d594fSAndroid Build Coastguard Worker int vdex_fd = bcp_index < boot_class_path_vdex_files_.size()
1777*795d594fSAndroid Build Coastguard Worker ? boot_class_path_vdex_files_[bcp_index].Fd()
1778*795d594fSAndroid Build Coastguard Worker : -1;
1779*795d594fSAndroid Build Coastguard Worker auto dex_filenames =
1780*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(boot_class_path_).SubArray(bcp_index, component_count);
1781*795d594fSAndroid Build Coastguard Worker ArrayRef<File> dex_files =
1782*795d594fSAndroid Build Coastguard Worker bcp_index + component_count < boot_class_path_files_.size() ?
1783*795d594fSAndroid Build Coastguard Worker ArrayRef<File>(boot_class_path_files_).SubArray(bcp_index, component_count) :
1784*795d594fSAndroid Build Coastguard Worker ArrayRef<File>();
1785*795d594fSAndroid Build Coastguard Worker // We open the oat file here only for validating that it's up-to-date. We don't open it as
1786*795d594fSAndroid Build Coastguard Worker // executable or mmap it to a reserved space. This `OatFile` object will be dropped after
1787*795d594fSAndroid Build Coastguard Worker // validation, and will not go into the `ImageSpace`.
1788*795d594fSAndroid Build Coastguard Worker std::unique_ptr<OatFile> oat_file;
1789*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(oat_fd >= 0, vdex_fd >= 0);
1790*795d594fSAndroid Build Coastguard Worker if (oat_fd >= 0) {
1791*795d594fSAndroid Build Coastguard Worker oat_file.reset(OatFile::Open(
1792*795d594fSAndroid Build Coastguard Worker /*zip_fd=*/ -1,
1793*795d594fSAndroid Build Coastguard Worker vdex_fd,
1794*795d594fSAndroid Build Coastguard Worker oat_fd,
1795*795d594fSAndroid Build Coastguard Worker oat_location,
1796*795d594fSAndroid Build Coastguard Worker /*executable=*/ false,
1797*795d594fSAndroid Build Coastguard Worker /*low_4gb=*/ false,
1798*795d594fSAndroid Build Coastguard Worker dex_filenames,
1799*795d594fSAndroid Build Coastguard Worker dex_files,
1800*795d594fSAndroid Build Coastguard Worker /*reservation=*/ nullptr,
1801*795d594fSAndroid Build Coastguard Worker error_msg));
1802*795d594fSAndroid Build Coastguard Worker } else {
1803*795d594fSAndroid Build Coastguard Worker oat_file.reset(OatFile::Open(
1804*795d594fSAndroid Build Coastguard Worker /*zip_fd=*/ -1,
1805*795d594fSAndroid Build Coastguard Worker oat_filename,
1806*795d594fSAndroid Build Coastguard Worker oat_location,
1807*795d594fSAndroid Build Coastguard Worker /*executable=*/ false,
1808*795d594fSAndroid Build Coastguard Worker /*low_4gb=*/ false,
1809*795d594fSAndroid Build Coastguard Worker dex_filenames,
1810*795d594fSAndroid Build Coastguard Worker dex_files,
1811*795d594fSAndroid Build Coastguard Worker /*reservation=*/ nullptr,
1812*795d594fSAndroid Build Coastguard Worker error_msg));
1813*795d594fSAndroid Build Coastguard Worker }
1814*795d594fSAndroid Build Coastguard Worker if (oat_file == nullptr) {
1815*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to open oat file '%s' when validating it for image '%s': %s",
1816*795d594fSAndroid Build Coastguard Worker oat_filename.c_str(),
1817*795d594fSAndroid Build Coastguard Worker art_location.c_str(),
1818*795d594fSAndroid Build Coastguard Worker error_msg->c_str());
1819*795d594fSAndroid Build Coastguard Worker return false;
1820*795d594fSAndroid Build Coastguard Worker }
1821*795d594fSAndroid Build Coastguard Worker if (!ImageSpace::ValidateOatFile(
1822*795d594fSAndroid Build Coastguard Worker *oat_file, error_msg, dex_filenames, dex_files, apex_versions_)) {
1823*795d594fSAndroid Build Coastguard Worker return false;
1824*795d594fSAndroid Build Coastguard Worker }
1825*795d594fSAndroid Build Coastguard Worker return true;
1826*795d594fSAndroid Build Coastguard Worker }
1827*795d594fSAndroid Build Coastguard Worker
ReadHeader(const std::string & base_location,const std::string & base_filename,size_t bcp_index,std::string * error_msg)1828*795d594fSAndroid Build Coastguard Worker bool ImageSpace::BootImageLayout::ReadHeader(const std::string& base_location,
1829*795d594fSAndroid Build Coastguard Worker const std::string& base_filename,
1830*795d594fSAndroid Build Coastguard Worker size_t bcp_index,
1831*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
1832*795d594fSAndroid Build Coastguard Worker DCHECK_LE(next_bcp_index_, bcp_index);
1833*795d594fSAndroid Build Coastguard Worker DCHECK_LT(bcp_index, boot_class_path_.size());
1834*795d594fSAndroid Build Coastguard Worker
1835*795d594fSAndroid Build Coastguard Worker std::string actual_filename = ExpandLocation(base_filename, bcp_index);
1836*795d594fSAndroid Build Coastguard Worker int bcp_image_fd = bcp_index < boot_class_path_image_files_.size() ?
1837*795d594fSAndroid Build Coastguard Worker boot_class_path_image_files_[bcp_index].Fd() :
1838*795d594fSAndroid Build Coastguard Worker -1;
1839*795d594fSAndroid Build Coastguard Worker ImageHeader header;
1840*795d594fSAndroid Build Coastguard Worker // When BCP image is provided as FD, it needs to be dup'ed (since it's stored in unique_fd) so
1841*795d594fSAndroid Build Coastguard Worker // that it can later be used in LoadComponents.
1842*795d594fSAndroid Build Coastguard Worker auto image_file = bcp_image_fd >= 0
1843*795d594fSAndroid Build Coastguard Worker ? std::make_unique<File>(DupCloexec(bcp_image_fd), actual_filename, /*check_usage=*/ false)
1844*795d594fSAndroid Build Coastguard Worker : std::unique_ptr<File>(OS::OpenFileForReading(actual_filename.c_str()));
1845*795d594fSAndroid Build Coastguard Worker if (!image_file || !image_file->IsOpened()) {
1846*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Unable to open file \"%s\" for reading image header",
1847*795d594fSAndroid Build Coastguard Worker actual_filename.c_str());
1848*795d594fSAndroid Build Coastguard Worker return false;
1849*795d594fSAndroid Build Coastguard Worker }
1850*795d594fSAndroid Build Coastguard Worker if (!ReadSpecificImageHeader(image_file.get(), actual_filename.c_str(), &header, error_msg)) {
1851*795d594fSAndroid Build Coastguard Worker return false;
1852*795d594fSAndroid Build Coastguard Worker }
1853*795d594fSAndroid Build Coastguard Worker const char* file_description = actual_filename.c_str();
1854*795d594fSAndroid Build Coastguard Worker if (!ValidateHeader(header, bcp_index, file_description, error_msg)) {
1855*795d594fSAndroid Build Coastguard Worker return false;
1856*795d594fSAndroid Build Coastguard Worker }
1857*795d594fSAndroid Build Coastguard Worker
1858*795d594fSAndroid Build Coastguard Worker // Validate oat files. We do it here so that the boot image will be re-compiled in memory if it's
1859*795d594fSAndroid Build Coastguard Worker // outdated.
1860*795d594fSAndroid Build Coastguard Worker size_t component_count = (header.GetImageSpaceCount() == 1u) ? header.GetComponentCount() : 1u;
1861*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < header.GetImageSpaceCount(); i++) {
1862*795d594fSAndroid Build Coastguard Worker if (!ValidateOatFile(base_location, base_filename, bcp_index + i, component_count, error_msg)) {
1863*795d594fSAndroid Build Coastguard Worker return false;
1864*795d594fSAndroid Build Coastguard Worker }
1865*795d594fSAndroid Build Coastguard Worker }
1866*795d594fSAndroid Build Coastguard Worker
1867*795d594fSAndroid Build Coastguard Worker if (chunks_.empty()) {
1868*795d594fSAndroid Build Coastguard Worker base_address_ = reinterpret_cast32<uint32_t>(header.GetImageBegin());
1869*795d594fSAndroid Build Coastguard Worker }
1870*795d594fSAndroid Build Coastguard Worker ImageChunk chunk;
1871*795d594fSAndroid Build Coastguard Worker chunk.base_location = base_location;
1872*795d594fSAndroid Build Coastguard Worker chunk.base_filename = base_filename;
1873*795d594fSAndroid Build Coastguard Worker chunk.start_index = bcp_index;
1874*795d594fSAndroid Build Coastguard Worker chunk.component_count = header.GetComponentCount();
1875*795d594fSAndroid Build Coastguard Worker chunk.image_space_count = header.GetImageSpaceCount();
1876*795d594fSAndroid Build Coastguard Worker chunk.reservation_size = header.GetImageReservationSize();
1877*795d594fSAndroid Build Coastguard Worker chunk.checksum = header.GetImageChecksum();
1878*795d594fSAndroid Build Coastguard Worker chunk.boot_image_component_count = header.GetBootImageComponentCount();
1879*795d594fSAndroid Build Coastguard Worker chunk.boot_image_checksum = header.GetBootImageChecksum();
1880*795d594fSAndroid Build Coastguard Worker chunk.boot_image_size = header.GetBootImageSize();
1881*795d594fSAndroid Build Coastguard Worker chunks_.push_back(std::move(chunk));
1882*795d594fSAndroid Build Coastguard Worker next_bcp_index_ = bcp_index + header.GetComponentCount();
1883*795d594fSAndroid Build Coastguard Worker total_component_count_ += header.GetComponentCount();
1884*795d594fSAndroid Build Coastguard Worker total_reservation_size_ += header.GetImageReservationSize();
1885*795d594fSAndroid Build Coastguard Worker return true;
1886*795d594fSAndroid Build Coastguard Worker }
1887*795d594fSAndroid Build Coastguard Worker
CompileBootclasspathElements(const std::string & base_location,const std::string & base_filename,size_t bcp_index,const std::vector<std::string> & profile_filenames,ArrayRef<const std::string> dependencies,std::string * error_msg)1888*795d594fSAndroid Build Coastguard Worker bool ImageSpace::BootImageLayout::CompileBootclasspathElements(
1889*795d594fSAndroid Build Coastguard Worker const std::string& base_location,
1890*795d594fSAndroid Build Coastguard Worker const std::string& base_filename,
1891*795d594fSAndroid Build Coastguard Worker size_t bcp_index,
1892*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>& profile_filenames,
1893*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> dependencies,
1894*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
1895*795d594fSAndroid Build Coastguard Worker DCHECK_LE(total_component_count_, next_bcp_index_);
1896*795d594fSAndroid Build Coastguard Worker DCHECK_LE(next_bcp_index_, bcp_index);
1897*795d594fSAndroid Build Coastguard Worker size_t bcp_component_count = boot_class_path_.size();
1898*795d594fSAndroid Build Coastguard Worker DCHECK_LT(bcp_index, bcp_component_count);
1899*795d594fSAndroid Build Coastguard Worker DCHECK(!profile_filenames.empty());
1900*795d594fSAndroid Build Coastguard Worker if (total_component_count_ != bcp_index) {
1901*795d594fSAndroid Build Coastguard Worker // We require all previous BCP components to have a boot image space (primary or extension).
1902*795d594fSAndroid Build Coastguard Worker *error_msg = "Cannot compile extension because of missing dependencies.";
1903*795d594fSAndroid Build Coastguard Worker return false;
1904*795d594fSAndroid Build Coastguard Worker }
1905*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
1906*795d594fSAndroid Build Coastguard Worker if (!runtime->IsImageDex2OatEnabled()) {
1907*795d594fSAndroid Build Coastguard Worker *error_msg = "Cannot compile bootclasspath because dex2oat for image compilation is disabled.";
1908*795d594fSAndroid Build Coastguard Worker return false;
1909*795d594fSAndroid Build Coastguard Worker }
1910*795d594fSAndroid Build Coastguard Worker
1911*795d594fSAndroid Build Coastguard Worker // Check dependencies.
1912*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(dependencies.empty(), bcp_index == 0);
1913*795d594fSAndroid Build Coastguard Worker size_t dependency_component_count = 0;
1914*795d594fSAndroid Build Coastguard Worker for (size_t i = 0, size = dependencies.size(); i != size; ++i) {
1915*795d594fSAndroid Build Coastguard Worker if (chunks_.size() == i || chunks_[i].start_index != dependency_component_count) {
1916*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Missing extension dependency \"%s\"", dependencies[i].c_str());
1917*795d594fSAndroid Build Coastguard Worker return false;
1918*795d594fSAndroid Build Coastguard Worker }
1919*795d594fSAndroid Build Coastguard Worker dependency_component_count += chunks_[i].component_count;
1920*795d594fSAndroid Build Coastguard Worker }
1921*795d594fSAndroid Build Coastguard Worker
1922*795d594fSAndroid Build Coastguard Worker // Collect locations from the profile.
1923*795d594fSAndroid Build Coastguard Worker std::set<std::string> dex_locations;
1924*795d594fSAndroid Build Coastguard Worker for (const std::string& profile_filename : profile_filenames) {
1925*795d594fSAndroid Build Coastguard Worker std::unique_ptr<File> profile_file(OS::OpenFileForReading(profile_filename.c_str()));
1926*795d594fSAndroid Build Coastguard Worker if (profile_file == nullptr) {
1927*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to open profile file \"%s\" for reading, error: %s",
1928*795d594fSAndroid Build Coastguard Worker profile_filename.c_str(),
1929*795d594fSAndroid Build Coastguard Worker strerror(errno));
1930*795d594fSAndroid Build Coastguard Worker return false;
1931*795d594fSAndroid Build Coastguard Worker }
1932*795d594fSAndroid Build Coastguard Worker
1933*795d594fSAndroid Build Coastguard Worker // TODO: Rewrite ProfileCompilationInfo to provide a better interface and
1934*795d594fSAndroid Build Coastguard Worker // to store the dex locations in uncompressed section of the file.
1935*795d594fSAndroid Build Coastguard Worker auto collect_fn = [&dex_locations](const std::string& dex_location,
1936*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] uint32_t checksum) {
1937*795d594fSAndroid Build Coastguard Worker dex_locations.insert(dex_location); // Just collect locations.
1938*795d594fSAndroid Build Coastguard Worker return false; // Do not read the profile data.
1939*795d594fSAndroid Build Coastguard Worker };
1940*795d594fSAndroid Build Coastguard Worker ProfileCompilationInfo info(/*for_boot_image=*/ true);
1941*795d594fSAndroid Build Coastguard Worker if (!info.Load(profile_file->Fd(), /*merge_classes=*/ true, collect_fn)) {
1942*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to scan profile from %s", profile_filename.c_str());
1943*795d594fSAndroid Build Coastguard Worker return false;
1944*795d594fSAndroid Build Coastguard Worker }
1945*795d594fSAndroid Build Coastguard Worker }
1946*795d594fSAndroid Build Coastguard Worker
1947*795d594fSAndroid Build Coastguard Worker // Match boot class path components to locations from profile.
1948*795d594fSAndroid Build Coastguard Worker // Note that the profile records only filenames without paths.
1949*795d594fSAndroid Build Coastguard Worker size_t bcp_end = bcp_index;
1950*795d594fSAndroid Build Coastguard Worker for (; bcp_end != bcp_component_count; ++bcp_end) {
1951*795d594fSAndroid Build Coastguard Worker const std::string& bcp_component = boot_class_path_locations_[bcp_end];
1952*795d594fSAndroid Build Coastguard Worker size_t slash_pos = bcp_component.rfind('/');
1953*795d594fSAndroid Build Coastguard Worker DCHECK_NE(slash_pos, std::string::npos);
1954*795d594fSAndroid Build Coastguard Worker std::string bcp_component_name = bcp_component.substr(slash_pos + 1u);
1955*795d594fSAndroid Build Coastguard Worker if (dex_locations.count(bcp_component_name) == 0u) {
1956*795d594fSAndroid Build Coastguard Worker break; // Did not find the current location in dex file.
1957*795d594fSAndroid Build Coastguard Worker }
1958*795d594fSAndroid Build Coastguard Worker }
1959*795d594fSAndroid Build Coastguard Worker
1960*795d594fSAndroid Build Coastguard Worker if (bcp_end == bcp_index) {
1961*795d594fSAndroid Build Coastguard Worker // No data for the first (requested) component.
1962*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("The profile does not contain data for %s",
1963*795d594fSAndroid Build Coastguard Worker boot_class_path_locations_[bcp_index].c_str());
1964*795d594fSAndroid Build Coastguard Worker return false;
1965*795d594fSAndroid Build Coastguard Worker }
1966*795d594fSAndroid Build Coastguard Worker
1967*795d594fSAndroid Build Coastguard Worker // Create in-memory files.
1968*795d594fSAndroid Build Coastguard Worker std::string art_filename = ExpandLocation(base_filename, bcp_index);
1969*795d594fSAndroid Build Coastguard Worker std::string vdex_filename = ImageHeader::GetVdexLocationFromImageLocation(art_filename);
1970*795d594fSAndroid Build Coastguard Worker std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(art_filename);
1971*795d594fSAndroid Build Coastguard Worker android::base::unique_fd art_fd(memfd_create_compat(art_filename.c_str(), /*flags=*/ 0));
1972*795d594fSAndroid Build Coastguard Worker android::base::unique_fd vdex_fd(memfd_create_compat(vdex_filename.c_str(), /*flags=*/ 0));
1973*795d594fSAndroid Build Coastguard Worker android::base::unique_fd oat_fd(memfd_create_compat(oat_filename.c_str(), /*flags=*/ 0));
1974*795d594fSAndroid Build Coastguard Worker if (art_fd.get() == -1 || vdex_fd.get() == -1 || oat_fd.get() == -1) {
1975*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to create memfd handles for compiling bootclasspath for %s",
1976*795d594fSAndroid Build Coastguard Worker boot_class_path_locations_[bcp_index].c_str());
1977*795d594fSAndroid Build Coastguard Worker return false;
1978*795d594fSAndroid Build Coastguard Worker }
1979*795d594fSAndroid Build Coastguard Worker
1980*795d594fSAndroid Build Coastguard Worker // Construct the dex2oat command line.
1981*795d594fSAndroid Build Coastguard Worker std::string dex2oat = runtime->GetCompilerExecutable();
1982*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> head_bcp =
1983*795d594fSAndroid Build Coastguard Worker boot_class_path_.SubArray(/*pos=*/ 0u, /*length=*/ dependency_component_count);
1984*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> head_bcp_locations =
1985*795d594fSAndroid Build Coastguard Worker boot_class_path_locations_.SubArray(/*pos=*/ 0u, /*length=*/ dependency_component_count);
1986*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> bcp_to_compile =
1987*795d594fSAndroid Build Coastguard Worker boot_class_path_.SubArray(/*pos=*/ bcp_index, /*length=*/ bcp_end - bcp_index);
1988*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> bcp_to_compile_locations =
1989*795d594fSAndroid Build Coastguard Worker boot_class_path_locations_.SubArray(/*pos=*/ bcp_index, /*length=*/ bcp_end - bcp_index);
1990*795d594fSAndroid Build Coastguard Worker std::string boot_class_path = head_bcp.empty() ?
1991*795d594fSAndroid Build Coastguard Worker Join(bcp_to_compile, ':') :
1992*795d594fSAndroid Build Coastguard Worker Join(head_bcp, ':') + ':' + Join(bcp_to_compile, ':');
1993*795d594fSAndroid Build Coastguard Worker std::string boot_class_path_locations =
1994*795d594fSAndroid Build Coastguard Worker head_bcp_locations.empty() ?
1995*795d594fSAndroid Build Coastguard Worker Join(bcp_to_compile_locations, ':') :
1996*795d594fSAndroid Build Coastguard Worker Join(head_bcp_locations, ':') + ':' + Join(bcp_to_compile_locations, ':');
1997*795d594fSAndroid Build Coastguard Worker
1998*795d594fSAndroid Build Coastguard Worker std::vector<std::string> args;
1999*795d594fSAndroid Build Coastguard Worker args.push_back(dex2oat);
2000*795d594fSAndroid Build Coastguard Worker args.push_back("--runtime-arg");
2001*795d594fSAndroid Build Coastguard Worker args.push_back("-Xbootclasspath:" + boot_class_path);
2002*795d594fSAndroid Build Coastguard Worker args.push_back("--runtime-arg");
2003*795d594fSAndroid Build Coastguard Worker args.push_back("-Xbootclasspath-locations:" + boot_class_path_locations);
2004*795d594fSAndroid Build Coastguard Worker if (dependencies.empty()) {
2005*795d594fSAndroid Build Coastguard Worker args.push_back(android::base::StringPrintf("--base=0x%08x", ART_BASE_ADDRESS));
2006*795d594fSAndroid Build Coastguard Worker } else {
2007*795d594fSAndroid Build Coastguard Worker args.push_back("--boot-image=" + Join(dependencies, kComponentSeparator));
2008*795d594fSAndroid Build Coastguard Worker }
2009*795d594fSAndroid Build Coastguard Worker for (size_t i = bcp_index; i != bcp_end; ++i) {
2010*795d594fSAndroid Build Coastguard Worker args.push_back("--dex-file=" + boot_class_path_[i]);
2011*795d594fSAndroid Build Coastguard Worker args.push_back("--dex-location=" + boot_class_path_locations_[i]);
2012*795d594fSAndroid Build Coastguard Worker }
2013*795d594fSAndroid Build Coastguard Worker args.push_back("--image-fd=" + std::to_string(art_fd.get()));
2014*795d594fSAndroid Build Coastguard Worker args.push_back("--output-vdex-fd=" + std::to_string(vdex_fd.get()));
2015*795d594fSAndroid Build Coastguard Worker args.push_back("--oat-fd=" + std::to_string(oat_fd.get()));
2016*795d594fSAndroid Build Coastguard Worker args.push_back("--oat-location=" + ImageHeader::GetOatLocationFromImageLocation(base_filename));
2017*795d594fSAndroid Build Coastguard Worker args.push_back("--single-image");
2018*795d594fSAndroid Build Coastguard Worker args.push_back("--image-format=uncompressed");
2019*795d594fSAndroid Build Coastguard Worker
2020*795d594fSAndroid Build Coastguard Worker // We currently cannot guarantee that the boot class path has no verification failures.
2021*795d594fSAndroid Build Coastguard Worker // And we do not want to compile anything, compilation should be done by JIT in zygote.
2022*795d594fSAndroid Build Coastguard Worker args.push_back("--compiler-filter=verify");
2023*795d594fSAndroid Build Coastguard Worker
2024*795d594fSAndroid Build Coastguard Worker // Pass the profiles.
2025*795d594fSAndroid Build Coastguard Worker for (const std::string& profile_filename : profile_filenames) {
2026*795d594fSAndroid Build Coastguard Worker args.push_back("--profile-file=" + profile_filename);
2027*795d594fSAndroid Build Coastguard Worker }
2028*795d594fSAndroid Build Coastguard Worker
2029*795d594fSAndroid Build Coastguard Worker // Do not let the file descriptor numbers change the compilation output.
2030*795d594fSAndroid Build Coastguard Worker args.push_back("--avoid-storing-invocation");
2031*795d594fSAndroid Build Coastguard Worker
2032*795d594fSAndroid Build Coastguard Worker runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&args);
2033*795d594fSAndroid Build Coastguard Worker
2034*795d594fSAndroid Build Coastguard Worker if (!kIsTargetBuild) {
2035*795d594fSAndroid Build Coastguard Worker args.push_back("--host");
2036*795d594fSAndroid Build Coastguard Worker }
2037*795d594fSAndroid Build Coastguard Worker
2038*795d594fSAndroid Build Coastguard Worker // Image compiler options go last to allow overriding above args, such as --compiler-filter.
2039*795d594fSAndroid Build Coastguard Worker for (const std::string& compiler_option : runtime->GetImageCompilerOptions()) {
2040*795d594fSAndroid Build Coastguard Worker args.push_back(compiler_option);
2041*795d594fSAndroid Build Coastguard Worker }
2042*795d594fSAndroid Build Coastguard Worker
2043*795d594fSAndroid Build Coastguard Worker // Compile.
2044*795d594fSAndroid Build Coastguard Worker VLOG(image) << "Compiling boot bootclasspath for " << (bcp_end - bcp_index)
2045*795d594fSAndroid Build Coastguard Worker << " components, starting from " << boot_class_path_locations_[bcp_index];
2046*795d594fSAndroid Build Coastguard Worker if (!Exec(args, error_msg)) {
2047*795d594fSAndroid Build Coastguard Worker return false;
2048*795d594fSAndroid Build Coastguard Worker }
2049*795d594fSAndroid Build Coastguard Worker
2050*795d594fSAndroid Build Coastguard Worker // Read and validate the image header.
2051*795d594fSAndroid Build Coastguard Worker ImageHeader header;
2052*795d594fSAndroid Build Coastguard Worker {
2053*795d594fSAndroid Build Coastguard Worker File image_file(art_fd.release(), /*check_usage=*/ false);
2054*795d594fSAndroid Build Coastguard Worker if (!ReadSpecificImageHeader(&image_file, "compiled image file", &header, error_msg)) {
2055*795d594fSAndroid Build Coastguard Worker return false;
2056*795d594fSAndroid Build Coastguard Worker }
2057*795d594fSAndroid Build Coastguard Worker art_fd.reset(image_file.Release());
2058*795d594fSAndroid Build Coastguard Worker }
2059*795d594fSAndroid Build Coastguard Worker const char* file_description = "compiled image file";
2060*795d594fSAndroid Build Coastguard Worker if (!ValidateHeader(header, bcp_index, file_description, error_msg)) {
2061*795d594fSAndroid Build Coastguard Worker return false;
2062*795d594fSAndroid Build Coastguard Worker }
2063*795d594fSAndroid Build Coastguard Worker
2064*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(chunks_.empty(), dependencies.empty());
2065*795d594fSAndroid Build Coastguard Worker ImageChunk chunk;
2066*795d594fSAndroid Build Coastguard Worker chunk.base_location = base_location;
2067*795d594fSAndroid Build Coastguard Worker chunk.base_filename = base_filename;
2068*795d594fSAndroid Build Coastguard Worker chunk.profile_files = profile_filenames;
2069*795d594fSAndroid Build Coastguard Worker chunk.start_index = bcp_index;
2070*795d594fSAndroid Build Coastguard Worker chunk.component_count = header.GetComponentCount();
2071*795d594fSAndroid Build Coastguard Worker chunk.image_space_count = header.GetImageSpaceCount();
2072*795d594fSAndroid Build Coastguard Worker chunk.reservation_size = header.GetImageReservationSize();
2073*795d594fSAndroid Build Coastguard Worker chunk.checksum = header.GetImageChecksum();
2074*795d594fSAndroid Build Coastguard Worker chunk.boot_image_component_count = header.GetBootImageComponentCount();
2075*795d594fSAndroid Build Coastguard Worker chunk.boot_image_checksum = header.GetBootImageChecksum();
2076*795d594fSAndroid Build Coastguard Worker chunk.boot_image_size = header.GetBootImageSize();
2077*795d594fSAndroid Build Coastguard Worker chunk.art_fd.reset(art_fd.release());
2078*795d594fSAndroid Build Coastguard Worker chunk.vdex_fd.reset(vdex_fd.release());
2079*795d594fSAndroid Build Coastguard Worker chunk.oat_fd.reset(oat_fd.release());
2080*795d594fSAndroid Build Coastguard Worker chunks_.push_back(std::move(chunk));
2081*795d594fSAndroid Build Coastguard Worker next_bcp_index_ = bcp_index + header.GetComponentCount();
2082*795d594fSAndroid Build Coastguard Worker total_component_count_ += header.GetComponentCount();
2083*795d594fSAndroid Build Coastguard Worker total_reservation_size_ += header.GetImageReservationSize();
2084*795d594fSAndroid Build Coastguard Worker return true;
2085*795d594fSAndroid Build Coastguard Worker }
2086*795d594fSAndroid Build Coastguard Worker
2087*795d594fSAndroid Build Coastguard Worker template <typename FilenameFn>
Load(FilenameFn && filename_fn,bool allow_in_memory_compilation,std::string * error_msg)2088*795d594fSAndroid Build Coastguard Worker bool ImageSpace::BootImageLayout::Load(FilenameFn&& filename_fn,
2089*795d594fSAndroid Build Coastguard Worker bool allow_in_memory_compilation,
2090*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) {
2091*795d594fSAndroid Build Coastguard Worker DCHECK(GetChunks().empty());
2092*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(GetBaseAddress(), 0u);
2093*795d594fSAndroid Build Coastguard Worker
2094*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> components = image_locations_;
2095*795d594fSAndroid Build Coastguard Worker size_t named_components_count = 0u;
2096*795d594fSAndroid Build Coastguard Worker if (!VerifyImageLocation(components, &named_components_count, error_msg)) {
2097*795d594fSAndroid Build Coastguard Worker return false;
2098*795d594fSAndroid Build Coastguard Worker }
2099*795d594fSAndroid Build Coastguard Worker
2100*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> named_components =
2101*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(components).SubArray(/*pos=*/ 0u, named_components_count);
2102*795d594fSAndroid Build Coastguard Worker
2103*795d594fSAndroid Build Coastguard Worker std::vector<NamedComponentLocation> named_component_locations;
2104*795d594fSAndroid Build Coastguard Worker if (!MatchNamedComponents(named_components, &named_component_locations, error_msg)) {
2105*795d594fSAndroid Build Coastguard Worker return false;
2106*795d594fSAndroid Build Coastguard Worker }
2107*795d594fSAndroid Build Coastguard Worker
2108*795d594fSAndroid Build Coastguard Worker // Load the image headers of named components.
2109*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(named_component_locations.size(), named_components.size());
2110*795d594fSAndroid Build Coastguard Worker const size_t bcp_component_count = boot_class_path_.size();
2111*795d594fSAndroid Build Coastguard Worker size_t bcp_pos = 0u;
2112*795d594fSAndroid Build Coastguard Worker for (size_t i = 0, size = named_components.size(); i != size; ++i) {
2113*795d594fSAndroid Build Coastguard Worker const std::string& base_location = named_component_locations[i].base_location;
2114*795d594fSAndroid Build Coastguard Worker size_t bcp_index = named_component_locations[i].bcp_index;
2115*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>& profile_filenames =
2116*795d594fSAndroid Build Coastguard Worker named_component_locations[i].profile_filenames;
2117*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(i == 0, bcp_index == 0);
2118*795d594fSAndroid Build Coastguard Worker if (bcp_index < bcp_pos) {
2119*795d594fSAndroid Build Coastguard Worker DCHECK_NE(i, 0u);
2120*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Named image component already covered by previous image: " << base_location;
2121*795d594fSAndroid Build Coastguard Worker continue;
2122*795d594fSAndroid Build Coastguard Worker }
2123*795d594fSAndroid Build Coastguard Worker std::string local_error_msg;
2124*795d594fSAndroid Build Coastguard Worker std::string base_filename;
2125*795d594fSAndroid Build Coastguard Worker if (!filename_fn(base_location, &base_filename, &local_error_msg) ||
2126*795d594fSAndroid Build Coastguard Worker !ReadHeader(base_location, base_filename, bcp_index, &local_error_msg)) {
2127*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Error reading named image component header for " << base_location
2128*795d594fSAndroid Build Coastguard Worker << ", error: " << local_error_msg;
2129*795d594fSAndroid Build Coastguard Worker // If the primary boot image is invalid, we generate a single full image. This is faster than
2130*795d594fSAndroid Build Coastguard Worker // generating the primary boot image and the extension separately.
2131*795d594fSAndroid Build Coastguard Worker if (bcp_index == 0) {
2132*795d594fSAndroid Build Coastguard Worker if (!allow_in_memory_compilation) {
2133*795d594fSAndroid Build Coastguard Worker // The boot image is unusable and we can't continue by generating a boot image in memory.
2134*795d594fSAndroid Build Coastguard Worker // All we can do is to return.
2135*795d594fSAndroid Build Coastguard Worker *error_msg = std::move(local_error_msg);
2136*795d594fSAndroid Build Coastguard Worker return false;
2137*795d594fSAndroid Build Coastguard Worker }
2138*795d594fSAndroid Build Coastguard Worker // We must at least have profiles for the core libraries.
2139*795d594fSAndroid Build Coastguard Worker if (profile_filenames.empty()) {
2140*795d594fSAndroid Build Coastguard Worker *error_msg = "Full boot image cannot be compiled because no profile is provided.";
2141*795d594fSAndroid Build Coastguard Worker return false;
2142*795d594fSAndroid Build Coastguard Worker }
2143*795d594fSAndroid Build Coastguard Worker std::vector<std::string> all_profiles;
2144*795d594fSAndroid Build Coastguard Worker for (const NamedComponentLocation& named_component_location : named_component_locations) {
2145*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>& profiles = named_component_location.profile_filenames;
2146*795d594fSAndroid Build Coastguard Worker all_profiles.insert(all_profiles.end(), profiles.begin(), profiles.end());
2147*795d594fSAndroid Build Coastguard Worker }
2148*795d594fSAndroid Build Coastguard Worker if (!CompileBootclasspathElements(base_location,
2149*795d594fSAndroid Build Coastguard Worker base_filename,
2150*795d594fSAndroid Build Coastguard Worker /*bcp_index=*/ 0,
2151*795d594fSAndroid Build Coastguard Worker all_profiles,
2152*795d594fSAndroid Build Coastguard Worker /*dependencies=*/ ArrayRef<const std::string>{},
2153*795d594fSAndroid Build Coastguard Worker &local_error_msg)) {
2154*795d594fSAndroid Build Coastguard Worker *error_msg =
2155*795d594fSAndroid Build Coastguard Worker StringPrintf("Full boot image cannot be compiled: %s", local_error_msg.c_str());
2156*795d594fSAndroid Build Coastguard Worker return false;
2157*795d594fSAndroid Build Coastguard Worker }
2158*795d594fSAndroid Build Coastguard Worker // No extensions are needed.
2159*795d594fSAndroid Build Coastguard Worker return true;
2160*795d594fSAndroid Build Coastguard Worker }
2161*795d594fSAndroid Build Coastguard Worker bool should_compile_extension = allow_in_memory_compilation && !profile_filenames.empty();
2162*795d594fSAndroid Build Coastguard Worker if (!should_compile_extension ||
2163*795d594fSAndroid Build Coastguard Worker !CompileBootclasspathElements(base_location,
2164*795d594fSAndroid Build Coastguard Worker base_filename,
2165*795d594fSAndroid Build Coastguard Worker bcp_index,
2166*795d594fSAndroid Build Coastguard Worker profile_filenames,
2167*795d594fSAndroid Build Coastguard Worker components.SubArray(/*pos=*/ 0, /*length=*/ 1),
2168*795d594fSAndroid Build Coastguard Worker &local_error_msg)) {
2169*795d594fSAndroid Build Coastguard Worker if (should_compile_extension) {
2170*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Error compiling boot image extension for " << boot_class_path_[bcp_index]
2171*795d594fSAndroid Build Coastguard Worker << ", error: " << local_error_msg;
2172*795d594fSAndroid Build Coastguard Worker }
2173*795d594fSAndroid Build Coastguard Worker bcp_pos = bcp_index + 1u; // Skip at least this component.
2174*795d594fSAndroid Build Coastguard Worker DCHECK_GT(bcp_pos, GetNextBcpIndex());
2175*795d594fSAndroid Build Coastguard Worker continue;
2176*795d594fSAndroid Build Coastguard Worker }
2177*795d594fSAndroid Build Coastguard Worker }
2178*795d594fSAndroid Build Coastguard Worker bcp_pos = GetNextBcpIndex();
2179*795d594fSAndroid Build Coastguard Worker }
2180*795d594fSAndroid Build Coastguard Worker
2181*795d594fSAndroid Build Coastguard Worker // Look for remaining components if there are any wildcard specifications.
2182*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> search_paths = components.SubArray(/*pos=*/ named_components_count);
2183*795d594fSAndroid Build Coastguard Worker if (!search_paths.empty()) {
2184*795d594fSAndroid Build Coastguard Worker const std::string& primary_base_location = named_component_locations[0].base_location;
2185*795d594fSAndroid Build Coastguard Worker size_t base_slash_pos = primary_base_location.rfind('/');
2186*795d594fSAndroid Build Coastguard Worker DCHECK_NE(base_slash_pos, std::string::npos);
2187*795d594fSAndroid Build Coastguard Worker std::string base_name = primary_base_location.substr(base_slash_pos + 1u);
2188*795d594fSAndroid Build Coastguard Worker DCHECK(!base_name.empty());
2189*795d594fSAndroid Build Coastguard Worker while (bcp_pos != bcp_component_count) {
2190*795d594fSAndroid Build Coastguard Worker const std::string& bcp_component = boot_class_path_[bcp_pos];
2191*795d594fSAndroid Build Coastguard Worker bool found = false;
2192*795d594fSAndroid Build Coastguard Worker for (const std::string& path : search_paths) {
2193*795d594fSAndroid Build Coastguard Worker std::string base_location;
2194*795d594fSAndroid Build Coastguard Worker if (path.size() == 1u) {
2195*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(path, "*");
2196*795d594fSAndroid Build Coastguard Worker size_t slash_pos = bcp_component.rfind('/');
2197*795d594fSAndroid Build Coastguard Worker DCHECK_NE(slash_pos, std::string::npos);
2198*795d594fSAndroid Build Coastguard Worker base_location = bcp_component.substr(0u, slash_pos + 1u) + base_name;
2199*795d594fSAndroid Build Coastguard Worker } else {
2200*795d594fSAndroid Build Coastguard Worker DCHECK(path.ends_with("/*"));
2201*795d594fSAndroid Build Coastguard Worker base_location = path.substr(0u, path.size() - 1u) + base_name;
2202*795d594fSAndroid Build Coastguard Worker }
2203*795d594fSAndroid Build Coastguard Worker std::string err_msg; // Ignored.
2204*795d594fSAndroid Build Coastguard Worker std::string base_filename;
2205*795d594fSAndroid Build Coastguard Worker if (filename_fn(base_location, &base_filename, &err_msg) &&
2206*795d594fSAndroid Build Coastguard Worker ReadHeader(base_location, base_filename, bcp_pos, &err_msg)) {
2207*795d594fSAndroid Build Coastguard Worker VLOG(image) << "Found image extension for " << ExpandLocation(base_location, bcp_pos);
2208*795d594fSAndroid Build Coastguard Worker bcp_pos = GetNextBcpIndex();
2209*795d594fSAndroid Build Coastguard Worker found = true;
2210*795d594fSAndroid Build Coastguard Worker break;
2211*795d594fSAndroid Build Coastguard Worker }
2212*795d594fSAndroid Build Coastguard Worker }
2213*795d594fSAndroid Build Coastguard Worker if (!found) {
2214*795d594fSAndroid Build Coastguard Worker ++bcp_pos;
2215*795d594fSAndroid Build Coastguard Worker }
2216*795d594fSAndroid Build Coastguard Worker }
2217*795d594fSAndroid Build Coastguard Worker }
2218*795d594fSAndroid Build Coastguard Worker
2219*795d594fSAndroid Build Coastguard Worker return true;
2220*795d594fSAndroid Build Coastguard Worker }
2221*795d594fSAndroid Build Coastguard Worker
LoadFromSystem(InstructionSet image_isa,bool allow_in_memory_compilation,std::string * error_msg)2222*795d594fSAndroid Build Coastguard Worker bool ImageSpace::BootImageLayout::LoadFromSystem(InstructionSet image_isa,
2223*795d594fSAndroid Build Coastguard Worker bool allow_in_memory_compilation,
2224*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) {
2225*795d594fSAndroid Build Coastguard Worker auto filename_fn = [image_isa](const std::string& location,
2226*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* filename,
2227*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] /*out*/ std::string* err_msg) {
2228*795d594fSAndroid Build Coastguard Worker *filename = GetSystemImageFilename(location.c_str(), image_isa);
2229*795d594fSAndroid Build Coastguard Worker return true;
2230*795d594fSAndroid Build Coastguard Worker };
2231*795d594fSAndroid Build Coastguard Worker return Load(filename_fn, allow_in_memory_compilation, error_msg);
2232*795d594fSAndroid Build Coastguard Worker }
2233*795d594fSAndroid Build Coastguard Worker
2234*795d594fSAndroid Build Coastguard Worker class ImageSpace::BootImageLoader {
2235*795d594fSAndroid Build Coastguard Worker public:
2236*795d594fSAndroid Build Coastguard Worker // Creates an instance.
2237*795d594fSAndroid Build Coastguard Worker // `apex_versions` is created from `Runtime::GetApexVersions` and must outlive this instance.
BootImageLoader(const std::vector<std::string> & boot_class_path,const std::vector<std::string> & boot_class_path_locations,ArrayRef<File> boot_class_path_files,ArrayRef<File> boot_class_path_image_files,ArrayRef<File> boot_class_path_vdex_files,ArrayRef<File> boot_class_path_oat_files,const std::vector<std::string> & image_locations,InstructionSet image_isa,bool relocate,bool executable,const std::string * apex_versions)2238*795d594fSAndroid Build Coastguard Worker BootImageLoader(const std::vector<std::string>& boot_class_path,
2239*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>& boot_class_path_locations,
2240*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_files,
2241*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_image_files,
2242*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_vdex_files,
2243*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_oat_files,
2244*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>& image_locations,
2245*795d594fSAndroid Build Coastguard Worker InstructionSet image_isa,
2246*795d594fSAndroid Build Coastguard Worker bool relocate,
2247*795d594fSAndroid Build Coastguard Worker bool executable,
2248*795d594fSAndroid Build Coastguard Worker const std::string* apex_versions)
2249*795d594fSAndroid Build Coastguard Worker : boot_class_path_(boot_class_path),
2250*795d594fSAndroid Build Coastguard Worker boot_class_path_locations_(boot_class_path_locations),
2251*795d594fSAndroid Build Coastguard Worker boot_class_path_files_(boot_class_path_files),
2252*795d594fSAndroid Build Coastguard Worker boot_class_path_image_files_(boot_class_path_image_files),
2253*795d594fSAndroid Build Coastguard Worker boot_class_path_vdex_files_(boot_class_path_vdex_files),
2254*795d594fSAndroid Build Coastguard Worker boot_class_path_oat_files_(boot_class_path_oat_files),
2255*795d594fSAndroid Build Coastguard Worker image_locations_(image_locations),
2256*795d594fSAndroid Build Coastguard Worker image_isa_(image_isa),
2257*795d594fSAndroid Build Coastguard Worker relocate_(relocate),
2258*795d594fSAndroid Build Coastguard Worker executable_(executable),
2259*795d594fSAndroid Build Coastguard Worker has_system_(false),
2260*795d594fSAndroid Build Coastguard Worker apex_versions_(apex_versions) {}
2261*795d594fSAndroid Build Coastguard Worker
FindImageFiles()2262*795d594fSAndroid Build Coastguard Worker void FindImageFiles() {
2263*795d594fSAndroid Build Coastguard Worker BootImageLayout layout(image_locations_,
2264*795d594fSAndroid Build Coastguard Worker boot_class_path_,
2265*795d594fSAndroid Build Coastguard Worker boot_class_path_locations_,
2266*795d594fSAndroid Build Coastguard Worker boot_class_path_files_,
2267*795d594fSAndroid Build Coastguard Worker boot_class_path_image_files_,
2268*795d594fSAndroid Build Coastguard Worker boot_class_path_vdex_files_,
2269*795d594fSAndroid Build Coastguard Worker boot_class_path_oat_files_,
2270*795d594fSAndroid Build Coastguard Worker apex_versions_);
2271*795d594fSAndroid Build Coastguard Worker std::string image_location = layout.GetPrimaryImageLocation();
2272*795d594fSAndroid Build Coastguard Worker std::string system_filename;
2273*795d594fSAndroid Build Coastguard Worker bool found_image = FindImageFilenameImpl(image_location.c_str(),
2274*795d594fSAndroid Build Coastguard Worker image_isa_,
2275*795d594fSAndroid Build Coastguard Worker &has_system_,
2276*795d594fSAndroid Build Coastguard Worker &system_filename);
2277*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(found_image, has_system_);
2278*795d594fSAndroid Build Coastguard Worker }
2279*795d594fSAndroid Build Coastguard Worker
HasSystem() const2280*795d594fSAndroid Build Coastguard Worker bool HasSystem() const { return has_system_; }
2281*795d594fSAndroid Build Coastguard Worker
2282*795d594fSAndroid Build Coastguard Worker bool LoadFromSystem(size_t extra_reservation_size,
2283*795d594fSAndroid Build Coastguard Worker bool allow_in_memory_compilation,
2284*795d594fSAndroid Build Coastguard Worker /*out*/std::vector<std::unique_ptr<ImageSpace>>* boot_image_spaces,
2285*795d594fSAndroid Build Coastguard Worker /*out*/MemMap* extra_reservation,
2286*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) REQUIRES_SHARED(Locks::mutator_lock_);
2287*795d594fSAndroid Build Coastguard Worker
2288*795d594fSAndroid Build Coastguard Worker private:
LoadImage(const BootImageLayout & layout,bool validate_oat_file,size_t extra_reservation_size,TimingLogger * logger,std::vector<std::unique_ptr<ImageSpace>> * boot_image_spaces,MemMap * extra_reservation,std::string * error_msg)2289*795d594fSAndroid Build Coastguard Worker bool LoadImage(
2290*795d594fSAndroid Build Coastguard Worker const BootImageLayout& layout,
2291*795d594fSAndroid Build Coastguard Worker bool validate_oat_file,
2292*795d594fSAndroid Build Coastguard Worker size_t extra_reservation_size,
2293*795d594fSAndroid Build Coastguard Worker TimingLogger* logger,
2294*795d594fSAndroid Build Coastguard Worker /*out*/std::vector<std::unique_ptr<ImageSpace>>* boot_image_spaces,
2295*795d594fSAndroid Build Coastguard Worker /*out*/MemMap* extra_reservation,
2296*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) REQUIRES_SHARED(Locks::mutator_lock_) {
2297*795d594fSAndroid Build Coastguard Worker ArrayRef<const BootImageLayout::ImageChunk> chunks = layout.GetChunks();
2298*795d594fSAndroid Build Coastguard Worker DCHECK(!chunks.empty());
2299*795d594fSAndroid Build Coastguard Worker const uint32_t base_address = layout.GetBaseAddress();
2300*795d594fSAndroid Build Coastguard Worker const size_t image_component_count = layout.GetTotalComponentCount();
2301*795d594fSAndroid Build Coastguard Worker const size_t image_reservation_size = layout.GetTotalReservationSize();
2302*795d594fSAndroid Build Coastguard Worker
2303*795d594fSAndroid Build Coastguard Worker DCHECK_LE(image_reservation_size, kMaxTotalImageReservationSize);
2304*795d594fSAndroid Build Coastguard Worker static_assert(kMaxTotalImageReservationSize < std::numeric_limits<uint32_t>::max());
2305*795d594fSAndroid Build Coastguard Worker if (extra_reservation_size > std::numeric_limits<uint32_t>::max() - image_reservation_size) {
2306*795d594fSAndroid Build Coastguard Worker // Since the `image_reservation_size` is limited to kMaxTotalImageReservationSize,
2307*795d594fSAndroid Build Coastguard Worker // the `extra_reservation_size` would have to be really excessive to fail this check.
2308*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Excessive extra reservation size: %zu", extra_reservation_size);
2309*795d594fSAndroid Build Coastguard Worker return false;
2310*795d594fSAndroid Build Coastguard Worker }
2311*795d594fSAndroid Build Coastguard Worker
2312*795d594fSAndroid Build Coastguard Worker // Reserve address space. If relocating, choose a random address for ALSR.
2313*795d594fSAndroid Build Coastguard Worker uint8_t* addr = reinterpret_cast<uint8_t*>(
2314*795d594fSAndroid Build Coastguard Worker relocate_ ? ART_BASE_ADDRESS + ChooseRelocationOffsetDelta() : base_address);
2315*795d594fSAndroid Build Coastguard Worker MemMap image_reservation =
2316*795d594fSAndroid Build Coastguard Worker ReserveBootImageMemory(addr, image_reservation_size + extra_reservation_size, error_msg);
2317*795d594fSAndroid Build Coastguard Worker if (!image_reservation.IsValid()) {
2318*795d594fSAndroid Build Coastguard Worker return false;
2319*795d594fSAndroid Build Coastguard Worker }
2320*795d594fSAndroid Build Coastguard Worker
2321*795d594fSAndroid Build Coastguard Worker // Load components.
2322*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<ImageSpace>> spaces;
2323*795d594fSAndroid Build Coastguard Worker spaces.reserve(image_component_count);
2324*795d594fSAndroid Build Coastguard Worker size_t max_image_space_dependencies = 0u;
2325*795d594fSAndroid Build Coastguard Worker for (size_t i = 0, num_chunks = chunks.size(); i != num_chunks; ++i) {
2326*795d594fSAndroid Build Coastguard Worker const BootImageLayout::ImageChunk& chunk = chunks[i];
2327*795d594fSAndroid Build Coastguard Worker std::string extension_error_msg;
2328*795d594fSAndroid Build Coastguard Worker uint8_t* old_reservation_begin = image_reservation.Begin();
2329*795d594fSAndroid Build Coastguard Worker size_t old_reservation_size = image_reservation.Size();
2330*795d594fSAndroid Build Coastguard Worker DCHECK_LE(chunk.reservation_size, old_reservation_size);
2331*795d594fSAndroid Build Coastguard Worker if (!LoadComponents(chunk,
2332*795d594fSAndroid Build Coastguard Worker validate_oat_file,
2333*795d594fSAndroid Build Coastguard Worker max_image_space_dependencies,
2334*795d594fSAndroid Build Coastguard Worker logger,
2335*795d594fSAndroid Build Coastguard Worker &spaces,
2336*795d594fSAndroid Build Coastguard Worker &image_reservation,
2337*795d594fSAndroid Build Coastguard Worker (i == 0) ? error_msg : &extension_error_msg)) {
2338*795d594fSAndroid Build Coastguard Worker // Failed to load the chunk. If this is the primary boot image, report the error.
2339*795d594fSAndroid Build Coastguard Worker if (i == 0) {
2340*795d594fSAndroid Build Coastguard Worker return false;
2341*795d594fSAndroid Build Coastguard Worker }
2342*795d594fSAndroid Build Coastguard Worker // For extension, shrink the reservation (and remap if needed, see below).
2343*795d594fSAndroid Build Coastguard Worker size_t new_reservation_size = old_reservation_size - chunk.reservation_size;
2344*795d594fSAndroid Build Coastguard Worker if (new_reservation_size == 0u) {
2345*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(extra_reservation_size, 0u);
2346*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(i + 1u, num_chunks);
2347*795d594fSAndroid Build Coastguard Worker image_reservation.Reset();
2348*795d594fSAndroid Build Coastguard Worker } else if (old_reservation_begin != image_reservation.Begin()) {
2349*795d594fSAndroid Build Coastguard Worker // Part of the image reservation has been used and then unmapped when
2350*795d594fSAndroid Build Coastguard Worker // rollling back the partial boot image extension load. Try to remap
2351*795d594fSAndroid Build Coastguard Worker // the image reservation. As this should be running single-threaded,
2352*795d594fSAndroid Build Coastguard Worker // the address range should still be available to mmap().
2353*795d594fSAndroid Build Coastguard Worker image_reservation.Reset();
2354*795d594fSAndroid Build Coastguard Worker std::string remap_error_msg;
2355*795d594fSAndroid Build Coastguard Worker image_reservation = ReserveBootImageMemory(old_reservation_begin,
2356*795d594fSAndroid Build Coastguard Worker new_reservation_size,
2357*795d594fSAndroid Build Coastguard Worker &remap_error_msg);
2358*795d594fSAndroid Build Coastguard Worker if (!image_reservation.IsValid()) {
2359*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to remap boot image reservation after failing "
2360*795d594fSAndroid Build Coastguard Worker "to load boot image extension (%s: %s): %s",
2361*795d594fSAndroid Build Coastguard Worker boot_class_path_locations_[chunk.start_index].c_str(),
2362*795d594fSAndroid Build Coastguard Worker extension_error_msg.c_str(),
2363*795d594fSAndroid Build Coastguard Worker remap_error_msg.c_str());
2364*795d594fSAndroid Build Coastguard Worker return false;
2365*795d594fSAndroid Build Coastguard Worker }
2366*795d594fSAndroid Build Coastguard Worker } else {
2367*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(old_reservation_size, image_reservation.Size());
2368*795d594fSAndroid Build Coastguard Worker image_reservation.SetSize(new_reservation_size);
2369*795d594fSAndroid Build Coastguard Worker }
2370*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Failed to load boot image extension "
2371*795d594fSAndroid Build Coastguard Worker << boot_class_path_locations_[chunk.start_index] << ": " << extension_error_msg;
2372*795d594fSAndroid Build Coastguard Worker }
2373*795d594fSAndroid Build Coastguard Worker // Update `max_image_space_dependencies` if all previous BCP components
2374*795d594fSAndroid Build Coastguard Worker // were covered and loading the current chunk succeeded.
2375*795d594fSAndroid Build Coastguard Worker size_t total_component_count = 0;
2376*795d594fSAndroid Build Coastguard Worker for (const std::unique_ptr<ImageSpace>& space : spaces) {
2377*795d594fSAndroid Build Coastguard Worker total_component_count += space->GetComponentCount();
2378*795d594fSAndroid Build Coastguard Worker }
2379*795d594fSAndroid Build Coastguard Worker if (max_image_space_dependencies == chunk.start_index &&
2380*795d594fSAndroid Build Coastguard Worker total_component_count == chunk.start_index + chunk.component_count) {
2381*795d594fSAndroid Build Coastguard Worker max_image_space_dependencies = chunk.start_index + chunk.component_count;
2382*795d594fSAndroid Build Coastguard Worker }
2383*795d594fSAndroid Build Coastguard Worker }
2384*795d594fSAndroid Build Coastguard Worker
2385*795d594fSAndroid Build Coastguard Worker MemMap local_extra_reservation;
2386*795d594fSAndroid Build Coastguard Worker if (!RemapExtraReservation(extra_reservation_size,
2387*795d594fSAndroid Build Coastguard Worker &image_reservation,
2388*795d594fSAndroid Build Coastguard Worker &local_extra_reservation,
2389*795d594fSAndroid Build Coastguard Worker error_msg)) {
2390*795d594fSAndroid Build Coastguard Worker return false;
2391*795d594fSAndroid Build Coastguard Worker }
2392*795d594fSAndroid Build Coastguard Worker
2393*795d594fSAndroid Build Coastguard Worker MaybeRelocateSpaces(spaces, logger);
2394*795d594fSAndroid Build Coastguard Worker DeduplicateInternedStrings(ArrayRef<const std::unique_ptr<ImageSpace>>(spaces), logger);
2395*795d594fSAndroid Build Coastguard Worker boot_image_spaces->swap(spaces);
2396*795d594fSAndroid Build Coastguard Worker *extra_reservation = std::move(local_extra_reservation);
2397*795d594fSAndroid Build Coastguard Worker return true;
2398*795d594fSAndroid Build Coastguard Worker }
2399*795d594fSAndroid Build Coastguard Worker
2400*795d594fSAndroid Build Coastguard Worker private:
2401*795d594fSAndroid Build Coastguard Worker class SimpleRelocateVisitor {
2402*795d594fSAndroid Build Coastguard Worker public:
SimpleRelocateVisitor(uint32_t diff,uint32_t begin,uint32_t size)2403*795d594fSAndroid Build Coastguard Worker SimpleRelocateVisitor(uint32_t diff, uint32_t begin, uint32_t size)
2404*795d594fSAndroid Build Coastguard Worker : diff_(diff), begin_(begin), size_(size) {}
2405*795d594fSAndroid Build Coastguard Worker
2406*795d594fSAndroid Build Coastguard Worker // Adapter taking the same arguments as SplitRangeRelocateVisitor
2407*795d594fSAndroid Build Coastguard Worker // to simplify constructing the various visitors in DoRelocateSpaces().
SimpleRelocateVisitor(uint32_t base_diff,uint32_t current_diff,uint32_t bound,uint32_t begin,uint32_t size)2408*795d594fSAndroid Build Coastguard Worker SimpleRelocateVisitor(uint32_t base_diff,
2409*795d594fSAndroid Build Coastguard Worker uint32_t current_diff,
2410*795d594fSAndroid Build Coastguard Worker uint32_t bound,
2411*795d594fSAndroid Build Coastguard Worker uint32_t begin,
2412*795d594fSAndroid Build Coastguard Worker uint32_t size)
2413*795d594fSAndroid Build Coastguard Worker : SimpleRelocateVisitor(base_diff, begin, size) {
2414*795d594fSAndroid Build Coastguard Worker // Check arguments unused by this class.
2415*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(base_diff, current_diff);
2416*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(bound, begin);
2417*795d594fSAndroid Build Coastguard Worker }
2418*795d594fSAndroid Build Coastguard Worker
2419*795d594fSAndroid Build Coastguard Worker template <typename T>
operator ()(T * src) const2420*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE T* operator()(T* src) const {
2421*795d594fSAndroid Build Coastguard Worker DCHECK(InSource(src));
2422*795d594fSAndroid Build Coastguard Worker uint32_t raw_src = reinterpret_cast32<uint32_t>(src);
2423*795d594fSAndroid Build Coastguard Worker return reinterpret_cast32<T*>(raw_src + diff_);
2424*795d594fSAndroid Build Coastguard Worker }
2425*795d594fSAndroid Build Coastguard Worker
2426*795d594fSAndroid Build Coastguard Worker template <typename T>
InSource(T * ptr) const2427*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE bool InSource(T* ptr) const {
2428*795d594fSAndroid Build Coastguard Worker uint32_t raw_ptr = reinterpret_cast32<uint32_t>(ptr);
2429*795d594fSAndroid Build Coastguard Worker return raw_ptr - begin_ < size_;
2430*795d594fSAndroid Build Coastguard Worker }
2431*795d594fSAndroid Build Coastguard Worker
2432*795d594fSAndroid Build Coastguard Worker template <typename T>
InDest(T * ptr) const2433*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE bool InDest(T* ptr) const {
2434*795d594fSAndroid Build Coastguard Worker uint32_t raw_ptr = reinterpret_cast32<uint32_t>(ptr);
2435*795d594fSAndroid Build Coastguard Worker uint32_t src_ptr = raw_ptr - diff_;
2436*795d594fSAndroid Build Coastguard Worker return src_ptr - begin_ < size_;
2437*795d594fSAndroid Build Coastguard Worker }
2438*795d594fSAndroid Build Coastguard Worker
2439*795d594fSAndroid Build Coastguard Worker private:
2440*795d594fSAndroid Build Coastguard Worker const uint32_t diff_;
2441*795d594fSAndroid Build Coastguard Worker const uint32_t begin_;
2442*795d594fSAndroid Build Coastguard Worker const uint32_t size_;
2443*795d594fSAndroid Build Coastguard Worker };
2444*795d594fSAndroid Build Coastguard Worker
2445*795d594fSAndroid Build Coastguard Worker class SplitRangeRelocateVisitor {
2446*795d594fSAndroid Build Coastguard Worker public:
SplitRangeRelocateVisitor(uint32_t base_diff,uint32_t current_diff,uint32_t bound,uint32_t begin,uint32_t size)2447*795d594fSAndroid Build Coastguard Worker SplitRangeRelocateVisitor(uint32_t base_diff,
2448*795d594fSAndroid Build Coastguard Worker uint32_t current_diff,
2449*795d594fSAndroid Build Coastguard Worker uint32_t bound,
2450*795d594fSAndroid Build Coastguard Worker uint32_t begin,
2451*795d594fSAndroid Build Coastguard Worker uint32_t size)
2452*795d594fSAndroid Build Coastguard Worker : base_diff_(base_diff),
2453*795d594fSAndroid Build Coastguard Worker current_diff_(current_diff),
2454*795d594fSAndroid Build Coastguard Worker bound_(bound),
2455*795d594fSAndroid Build Coastguard Worker begin_(begin),
2456*795d594fSAndroid Build Coastguard Worker size_(size) {
2457*795d594fSAndroid Build Coastguard Worker DCHECK_NE(begin_, bound_);
2458*795d594fSAndroid Build Coastguard Worker // The bound separates the boot image range and the extension range.
2459*795d594fSAndroid Build Coastguard Worker DCHECK_LT(bound_ - begin_, size_);
2460*795d594fSAndroid Build Coastguard Worker }
2461*795d594fSAndroid Build Coastguard Worker
2462*795d594fSAndroid Build Coastguard Worker template <typename T>
operator ()(T * src) const2463*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE T* operator()(T* src) const {
2464*795d594fSAndroid Build Coastguard Worker DCHECK(InSource(src));
2465*795d594fSAndroid Build Coastguard Worker uint32_t raw_src = reinterpret_cast32<uint32_t>(src);
2466*795d594fSAndroid Build Coastguard Worker uint32_t diff = (raw_src < bound_) ? base_diff_ : current_diff_;
2467*795d594fSAndroid Build Coastguard Worker return reinterpret_cast32<T*>(raw_src + diff);
2468*795d594fSAndroid Build Coastguard Worker }
2469*795d594fSAndroid Build Coastguard Worker
2470*795d594fSAndroid Build Coastguard Worker template <typename T>
InSource(T * ptr) const2471*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE bool InSource(T* ptr) const {
2472*795d594fSAndroid Build Coastguard Worker uint32_t raw_ptr = reinterpret_cast32<uint32_t>(ptr);
2473*795d594fSAndroid Build Coastguard Worker return raw_ptr - begin_ < size_;
2474*795d594fSAndroid Build Coastguard Worker }
2475*795d594fSAndroid Build Coastguard Worker
2476*795d594fSAndroid Build Coastguard Worker private:
2477*795d594fSAndroid Build Coastguard Worker const uint32_t base_diff_;
2478*795d594fSAndroid Build Coastguard Worker const uint32_t current_diff_;
2479*795d594fSAndroid Build Coastguard Worker const uint32_t bound_;
2480*795d594fSAndroid Build Coastguard Worker const uint32_t begin_;
2481*795d594fSAndroid Build Coastguard Worker const uint32_t size_;
2482*795d594fSAndroid Build Coastguard Worker };
2483*795d594fSAndroid Build Coastguard Worker
PointerAddress(ArtMethod * method,MemberOffset offset)2484*795d594fSAndroid Build Coastguard Worker static void** PointerAddress(ArtMethod* method, MemberOffset offset) {
2485*795d594fSAndroid Build Coastguard Worker return reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(method) + offset.Uint32Value());
2486*795d594fSAndroid Build Coastguard Worker }
2487*795d594fSAndroid Build Coastguard Worker
2488*795d594fSAndroid Build Coastguard Worker template <PointerSize kPointerSize>
DoRelocateSpaces(ArrayRef<const std::unique_ptr<ImageSpace>> & spaces,int64_t base_diff64)2489*795d594fSAndroid Build Coastguard Worker static void DoRelocateSpaces(ArrayRef<const std::unique_ptr<ImageSpace>>& spaces,
2490*795d594fSAndroid Build Coastguard Worker int64_t base_diff64) REQUIRES_SHARED(Locks::mutator_lock_) {
2491*795d594fSAndroid Build Coastguard Worker DCHECK(!spaces.empty());
2492*795d594fSAndroid Build Coastguard Worker gc::accounting::ContinuousSpaceBitmap patched_objects(
2493*795d594fSAndroid Build Coastguard Worker gc::accounting::ContinuousSpaceBitmap::Create(
2494*795d594fSAndroid Build Coastguard Worker "Marked objects",
2495*795d594fSAndroid Build Coastguard Worker spaces.front()->Begin(),
2496*795d594fSAndroid Build Coastguard Worker spaces.back()->End() - spaces.front()->Begin()));
2497*795d594fSAndroid Build Coastguard Worker const ImageHeader& base_header = spaces[0]->GetImageHeader();
2498*795d594fSAndroid Build Coastguard Worker size_t base_image_space_count = base_header.GetImageSpaceCount();
2499*795d594fSAndroid Build Coastguard Worker DCHECK_LE(base_image_space_count, spaces.size());
2500*795d594fSAndroid Build Coastguard Worker DoRelocateSpaces<kPointerSize, /*kExtension=*/ false>(
2501*795d594fSAndroid Build Coastguard Worker spaces.SubArray(/*pos=*/ 0u, base_image_space_count),
2502*795d594fSAndroid Build Coastguard Worker base_diff64,
2503*795d594fSAndroid Build Coastguard Worker &patched_objects);
2504*795d594fSAndroid Build Coastguard Worker
2505*795d594fSAndroid Build Coastguard Worker for (size_t i = base_image_space_count, size = spaces.size(); i != size; ) {
2506*795d594fSAndroid Build Coastguard Worker const ImageHeader& ext_header = spaces[i]->GetImageHeader();
2507*795d594fSAndroid Build Coastguard Worker size_t ext_image_space_count = ext_header.GetImageSpaceCount();
2508*795d594fSAndroid Build Coastguard Worker DCHECK_LE(ext_image_space_count, size - i);
2509*795d594fSAndroid Build Coastguard Worker DoRelocateSpaces<kPointerSize, /*kExtension=*/ true>(
2510*795d594fSAndroid Build Coastguard Worker spaces.SubArray(/*pos=*/ i, ext_image_space_count),
2511*795d594fSAndroid Build Coastguard Worker base_diff64,
2512*795d594fSAndroid Build Coastguard Worker &patched_objects);
2513*795d594fSAndroid Build Coastguard Worker i += ext_image_space_count;
2514*795d594fSAndroid Build Coastguard Worker }
2515*795d594fSAndroid Build Coastguard Worker }
2516*795d594fSAndroid Build Coastguard Worker
2517*795d594fSAndroid Build Coastguard Worker template <PointerSize kPointerSize, bool kExtension>
DoRelocateSpaces(ArrayRef<const std::unique_ptr<ImageSpace>> spaces,int64_t base_diff64,gc::accounting::ContinuousSpaceBitmap * patched_objects)2518*795d594fSAndroid Build Coastguard Worker static void DoRelocateSpaces(ArrayRef<const std::unique_ptr<ImageSpace>> spaces,
2519*795d594fSAndroid Build Coastguard Worker int64_t base_diff64,
2520*795d594fSAndroid Build Coastguard Worker gc::accounting::ContinuousSpaceBitmap* patched_objects)
2521*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2522*795d594fSAndroid Build Coastguard Worker DCHECK(!spaces.empty());
2523*795d594fSAndroid Build Coastguard Worker const ImageHeader& first_header = spaces.front()->GetImageHeader();
2524*795d594fSAndroid Build Coastguard Worker uint32_t image_begin = reinterpret_cast32<uint32_t>(first_header.GetImageBegin());
2525*795d594fSAndroid Build Coastguard Worker uint32_t image_size = first_header.GetImageReservationSize();
2526*795d594fSAndroid Build Coastguard Worker DCHECK_NE(image_size, 0u);
2527*795d594fSAndroid Build Coastguard Worker uint32_t source_begin = kExtension ? first_header.GetBootImageBegin() : image_begin;
2528*795d594fSAndroid Build Coastguard Worker uint32_t source_size = kExtension ? first_header.GetBootImageSize() + image_size : image_size;
2529*795d594fSAndroid Build Coastguard Worker if (kExtension) {
2530*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(first_header.GetBootImageBegin() + first_header.GetBootImageSize(), image_begin);
2531*795d594fSAndroid Build Coastguard Worker }
2532*795d594fSAndroid Build Coastguard Worker int64_t current_diff64 = kExtension
2533*795d594fSAndroid Build Coastguard Worker ? static_cast<int64_t>(reinterpret_cast32<uint32_t>(spaces.front()->Begin())) -
2534*795d594fSAndroid Build Coastguard Worker static_cast<int64_t>(image_begin)
2535*795d594fSAndroid Build Coastguard Worker : base_diff64;
2536*795d594fSAndroid Build Coastguard Worker if (base_diff64 == 0 && current_diff64 == 0) {
2537*795d594fSAndroid Build Coastguard Worker return;
2538*795d594fSAndroid Build Coastguard Worker }
2539*795d594fSAndroid Build Coastguard Worker uint32_t base_diff = static_cast<uint32_t>(base_diff64);
2540*795d594fSAndroid Build Coastguard Worker uint32_t current_diff = static_cast<uint32_t>(current_diff64);
2541*795d594fSAndroid Build Coastguard Worker
2542*795d594fSAndroid Build Coastguard Worker // For boot image the main visitor is a SimpleRelocateVisitor. For the boot image extension we
2543*795d594fSAndroid Build Coastguard Worker // mostly use a SplitRelocationVisitor but some work can still use the SimpleRelocationVisitor.
2544*795d594fSAndroid Build Coastguard Worker using MainRelocateVisitor = typename std::conditional<
2545*795d594fSAndroid Build Coastguard Worker kExtension, SplitRangeRelocateVisitor, SimpleRelocateVisitor>::type;
2546*795d594fSAndroid Build Coastguard Worker SimpleRelocateVisitor simple_relocate_visitor(current_diff, image_begin, image_size);
2547*795d594fSAndroid Build Coastguard Worker MainRelocateVisitor main_relocate_visitor(
2548*795d594fSAndroid Build Coastguard Worker base_diff, current_diff, /*bound=*/ image_begin, source_begin, source_size);
2549*795d594fSAndroid Build Coastguard Worker
2550*795d594fSAndroid Build Coastguard Worker using MainPatchRelocateVisitor =
2551*795d594fSAndroid Build Coastguard Worker PatchObjectVisitor<kPointerSize, MainRelocateVisitor, MainRelocateVisitor>;
2552*795d594fSAndroid Build Coastguard Worker using SimplePatchRelocateVisitor =
2553*795d594fSAndroid Build Coastguard Worker PatchObjectVisitor<kPointerSize, SimpleRelocateVisitor, SimpleRelocateVisitor>;
2554*795d594fSAndroid Build Coastguard Worker MainPatchRelocateVisitor main_patch_object_visitor(main_relocate_visitor,
2555*795d594fSAndroid Build Coastguard Worker main_relocate_visitor);
2556*795d594fSAndroid Build Coastguard Worker SimplePatchRelocateVisitor simple_patch_object_visitor(simple_relocate_visitor,
2557*795d594fSAndroid Build Coastguard Worker simple_relocate_visitor);
2558*795d594fSAndroid Build Coastguard Worker
2559*795d594fSAndroid Build Coastguard Worker // Retrieve the Class.class, Method.class and Constructor.class needed in the loops below.
2560*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots;
2561*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> class_class;
2562*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> method_class;
2563*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> constructor_class;
2564*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> field_var_handle_class;
2565*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> static_field_var_handle_class;
2566*795d594fSAndroid Build Coastguard Worker {
2567*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::ObjectArray<mirror::Object>> image_roots =
2568*795d594fSAndroid Build Coastguard Worker simple_relocate_visitor(first_header.GetImageRoots<kWithoutReadBarrier>().Ptr());
2569*795d594fSAndroid Build Coastguard Worker DCHECK(!patched_objects->Test(image_roots.Ptr()));
2570*795d594fSAndroid Build Coastguard Worker
2571*795d594fSAndroid Build Coastguard Worker SimpleRelocateVisitor base_relocate_visitor(
2572*795d594fSAndroid Build Coastguard Worker base_diff,
2573*795d594fSAndroid Build Coastguard Worker source_begin,
2574*795d594fSAndroid Build Coastguard Worker kExtension ? source_size - image_size : image_size);
2575*795d594fSAndroid Build Coastguard Worker int32_t class_roots_index = enum_cast<int32_t>(ImageHeader::kClassRoots);
2576*795d594fSAndroid Build Coastguard Worker DCHECK_LT(class_roots_index, image_roots->GetLength<kVerifyNone>());
2577*795d594fSAndroid Build Coastguard Worker class_roots = ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast(base_relocate_visitor(
2578*795d594fSAndroid Build Coastguard Worker image_roots->GetWithoutChecks<kVerifyNone,
2579*795d594fSAndroid Build Coastguard Worker kWithoutReadBarrier>(class_roots_index).Ptr()));
2580*795d594fSAndroid Build Coastguard Worker if (kExtension) {
2581*795d594fSAndroid Build Coastguard Worker // Class roots must have been visited if we relocated the primary boot image.
2582*795d594fSAndroid Build Coastguard Worker DCHECK(base_diff == 0 || patched_objects->Test(class_roots.Ptr()));
2583*795d594fSAndroid Build Coastguard Worker class_class = GetClassRoot<mirror::Class, kWithoutReadBarrier>(class_roots);
2584*795d594fSAndroid Build Coastguard Worker method_class = GetClassRoot<mirror::Method, kWithoutReadBarrier>(class_roots);
2585*795d594fSAndroid Build Coastguard Worker constructor_class = GetClassRoot<mirror::Constructor, kWithoutReadBarrier>(class_roots);
2586*795d594fSAndroid Build Coastguard Worker field_var_handle_class =
2587*795d594fSAndroid Build Coastguard Worker GetClassRoot<mirror::FieldVarHandle, kWithoutReadBarrier>(class_roots);
2588*795d594fSAndroid Build Coastguard Worker static_field_var_handle_class =
2589*795d594fSAndroid Build Coastguard Worker GetClassRoot<mirror::StaticFieldVarHandle, kWithoutReadBarrier>(class_roots);
2590*795d594fSAndroid Build Coastguard Worker } else {
2591*795d594fSAndroid Build Coastguard Worker DCHECK(!patched_objects->Test(class_roots.Ptr()));
2592*795d594fSAndroid Build Coastguard Worker class_class = simple_relocate_visitor(
2593*795d594fSAndroid Build Coastguard Worker GetClassRoot<mirror::Class, kWithoutReadBarrier>(class_roots).Ptr());
2594*795d594fSAndroid Build Coastguard Worker method_class = simple_relocate_visitor(
2595*795d594fSAndroid Build Coastguard Worker GetClassRoot<mirror::Method, kWithoutReadBarrier>(class_roots).Ptr());
2596*795d594fSAndroid Build Coastguard Worker constructor_class = simple_relocate_visitor(
2597*795d594fSAndroid Build Coastguard Worker GetClassRoot<mirror::Constructor, kWithoutReadBarrier>(class_roots).Ptr());
2598*795d594fSAndroid Build Coastguard Worker field_var_handle_class = simple_relocate_visitor(
2599*795d594fSAndroid Build Coastguard Worker GetClassRoot<mirror::FieldVarHandle, kWithoutReadBarrier>(class_roots).Ptr());
2600*795d594fSAndroid Build Coastguard Worker static_field_var_handle_class = simple_relocate_visitor(
2601*795d594fSAndroid Build Coastguard Worker GetClassRoot<mirror::StaticFieldVarHandle, kWithoutReadBarrier>(class_roots).Ptr());
2602*795d594fSAndroid Build Coastguard Worker }
2603*795d594fSAndroid Build Coastguard Worker }
2604*795d594fSAndroid Build Coastguard Worker
2605*795d594fSAndroid Build Coastguard Worker for (const std::unique_ptr<ImageSpace>& space : spaces) {
2606*795d594fSAndroid Build Coastguard Worker // First patch the image header.
2607*795d594fSAndroid Build Coastguard Worker reinterpret_cast<ImageHeader*>(space->Begin())->RelocateImageReferences(current_diff64);
2608*795d594fSAndroid Build Coastguard Worker reinterpret_cast<ImageHeader*>(space->Begin())->RelocateBootImageReferences(base_diff64);
2609*795d594fSAndroid Build Coastguard Worker
2610*795d594fSAndroid Build Coastguard Worker // Patch fields and methods.
2611*795d594fSAndroid Build Coastguard Worker const ImageHeader& image_header = space->GetImageHeader();
2612*795d594fSAndroid Build Coastguard Worker image_header.VisitPackedArtFields([&](ArtField& field) REQUIRES_SHARED(Locks::mutator_lock_) {
2613*795d594fSAndroid Build Coastguard Worker // Fields always reference class in the current image.
2614*795d594fSAndroid Build Coastguard Worker simple_patch_object_visitor.template PatchGcRoot</*kMayBeNull=*/ false>(
2615*795d594fSAndroid Build Coastguard Worker &field.DeclaringClassRoot());
2616*795d594fSAndroid Build Coastguard Worker }, space->Begin());
2617*795d594fSAndroid Build Coastguard Worker image_header.VisitPackedArtMethods([&](ArtMethod& method)
2618*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2619*795d594fSAndroid Build Coastguard Worker main_patch_object_visitor.PatchGcRoot(&method.DeclaringClassRoot());
2620*795d594fSAndroid Build Coastguard Worker if (!method.HasCodeItem()) {
2621*795d594fSAndroid Build Coastguard Worker void** data_address = PointerAddress(&method, ArtMethod::DataOffset(kPointerSize));
2622*795d594fSAndroid Build Coastguard Worker main_patch_object_visitor.PatchNativePointer(data_address);
2623*795d594fSAndroid Build Coastguard Worker }
2624*795d594fSAndroid Build Coastguard Worker void** entrypoint_address =
2625*795d594fSAndroid Build Coastguard Worker PointerAddress(&method, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kPointerSize));
2626*795d594fSAndroid Build Coastguard Worker main_patch_object_visitor.PatchNativePointer(entrypoint_address);
2627*795d594fSAndroid Build Coastguard Worker }, space->Begin(), kPointerSize);
2628*795d594fSAndroid Build Coastguard Worker auto method_table_visitor = [&](ArtMethod* method) {
2629*795d594fSAndroid Build Coastguard Worker DCHECK(method != nullptr);
2630*795d594fSAndroid Build Coastguard Worker return main_relocate_visitor(method);
2631*795d594fSAndroid Build Coastguard Worker };
2632*795d594fSAndroid Build Coastguard Worker image_header.VisitPackedImTables(method_table_visitor, space->Begin(), kPointerSize);
2633*795d594fSAndroid Build Coastguard Worker image_header.VisitPackedImtConflictTables(method_table_visitor, space->Begin(), kPointerSize);
2634*795d594fSAndroid Build Coastguard Worker image_header.VisitJniStubMethods</*kUpdate=*/ true>(method_table_visitor,
2635*795d594fSAndroid Build Coastguard Worker space->Begin(),
2636*795d594fSAndroid Build Coastguard Worker kPointerSize);
2637*795d594fSAndroid Build Coastguard Worker
2638*795d594fSAndroid Build Coastguard Worker // Patch the intern table.
2639*795d594fSAndroid Build Coastguard Worker if (image_header.GetInternedStringsSection().Size() != 0u) {
2640*795d594fSAndroid Build Coastguard Worker const uint8_t* data = space->Begin() + image_header.GetInternedStringsSection().Offset();
2641*795d594fSAndroid Build Coastguard Worker size_t read_count;
2642*795d594fSAndroid Build Coastguard Worker InternTable::UnorderedSet temp_set(data, /*make_copy_of_data=*/ false, &read_count);
2643*795d594fSAndroid Build Coastguard Worker for (GcRoot<mirror::String>& slot : temp_set) {
2644*795d594fSAndroid Build Coastguard Worker // The intern table contains only strings in the current image.
2645*795d594fSAndroid Build Coastguard Worker simple_patch_object_visitor.template PatchGcRoot</*kMayBeNull=*/ false>(&slot);
2646*795d594fSAndroid Build Coastguard Worker }
2647*795d594fSAndroid Build Coastguard Worker }
2648*795d594fSAndroid Build Coastguard Worker
2649*795d594fSAndroid Build Coastguard Worker // Patch the class table and classes, so that we can traverse class hierarchy to
2650*795d594fSAndroid Build Coastguard Worker // determine the types of other objects when we visit them later.
2651*795d594fSAndroid Build Coastguard Worker if (image_header.GetClassTableSection().Size() != 0u) {
2652*795d594fSAndroid Build Coastguard Worker uint8_t* data = space->Begin() + image_header.GetClassTableSection().Offset();
2653*795d594fSAndroid Build Coastguard Worker size_t read_count;
2654*795d594fSAndroid Build Coastguard Worker ClassTable::ClassSet temp_set(data, /*make_copy_of_data=*/ false, &read_count);
2655*795d594fSAndroid Build Coastguard Worker DCHECK(!temp_set.empty());
2656*795d594fSAndroid Build Coastguard Worker // The class table contains only classes in the current image.
2657*795d594fSAndroid Build Coastguard Worker ClassTableVisitor class_table_visitor(simple_relocate_visitor);
2658*795d594fSAndroid Build Coastguard Worker for (ClassTable::TableSlot& slot : temp_set) {
2659*795d594fSAndroid Build Coastguard Worker slot.VisitRoot(class_table_visitor);
2660*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> klass = slot.Read<kWithoutReadBarrier>();
2661*795d594fSAndroid Build Coastguard Worker DCHECK(klass != nullptr);
2662*795d594fSAndroid Build Coastguard Worker DCHECK(!patched_objects->Test(klass.Ptr()));
2663*795d594fSAndroid Build Coastguard Worker patched_objects->Set(klass.Ptr());
2664*795d594fSAndroid Build Coastguard Worker main_patch_object_visitor.VisitClass(klass, class_class);
2665*795d594fSAndroid Build Coastguard Worker // Then patch the non-embedded vtable and iftable.
2666*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::PointerArray> vtable =
2667*795d594fSAndroid Build Coastguard Worker klass->GetVTable<kVerifyNone, kWithoutReadBarrier>();
2668*795d594fSAndroid Build Coastguard Worker if ((kExtension ? simple_relocate_visitor.InDest(vtable.Ptr()) : vtable != nullptr) &&
2669*795d594fSAndroid Build Coastguard Worker !patched_objects->Set(vtable.Ptr())) {
2670*795d594fSAndroid Build Coastguard Worker main_patch_object_visitor.VisitPointerArray(vtable);
2671*795d594fSAndroid Build Coastguard Worker }
2672*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::IfTable> iftable = klass->GetIfTable<kVerifyNone, kWithoutReadBarrier>();
2673*795d594fSAndroid Build Coastguard Worker if (kExtension ? simple_relocate_visitor.InDest(iftable.Ptr()) : iftable != nullptr) {
2674*795d594fSAndroid Build Coastguard Worker int32_t ifcount = iftable->Count<kVerifyNone>();
2675*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0; i != ifcount; ++i) {
2676*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::PointerArray> unpatched_ifarray =
2677*795d594fSAndroid Build Coastguard Worker iftable->GetMethodArrayOrNull<kVerifyNone, kWithoutReadBarrier>(i);
2678*795d594fSAndroid Build Coastguard Worker if (kExtension ? simple_relocate_visitor.InSource(unpatched_ifarray.Ptr())
2679*795d594fSAndroid Build Coastguard Worker : unpatched_ifarray != nullptr) {
2680*795d594fSAndroid Build Coastguard Worker // The iftable has not been patched, so we need to explicitly adjust the pointer.
2681*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::PointerArray> ifarray =
2682*795d594fSAndroid Build Coastguard Worker simple_relocate_visitor(unpatched_ifarray.Ptr());
2683*795d594fSAndroid Build Coastguard Worker if (!patched_objects->Set(ifarray.Ptr())) {
2684*795d594fSAndroid Build Coastguard Worker main_patch_object_visitor.VisitPointerArray(ifarray);
2685*795d594fSAndroid Build Coastguard Worker }
2686*795d594fSAndroid Build Coastguard Worker }
2687*795d594fSAndroid Build Coastguard Worker }
2688*795d594fSAndroid Build Coastguard Worker }
2689*795d594fSAndroid Build Coastguard Worker }
2690*795d594fSAndroid Build Coastguard Worker }
2691*795d594fSAndroid Build Coastguard Worker }
2692*795d594fSAndroid Build Coastguard Worker
2693*795d594fSAndroid Build Coastguard Worker for (const std::unique_ptr<ImageSpace>& space : spaces) {
2694*795d594fSAndroid Build Coastguard Worker const ImageHeader& image_header = space->GetImageHeader();
2695*795d594fSAndroid Build Coastguard Worker
2696*795d594fSAndroid Build Coastguard Worker static_assert(IsAligned<kObjectAlignment>(sizeof(ImageHeader)), "Header alignment check");
2697*795d594fSAndroid Build Coastguard Worker uint32_t objects_end = image_header.GetObjectsSection().Size();
2698*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(objects_end, kObjectAlignment);
2699*795d594fSAndroid Build Coastguard Worker for (uint32_t pos = sizeof(ImageHeader); pos != objects_end; ) {
2700*795d594fSAndroid Build Coastguard Worker mirror::Object* object = reinterpret_cast<mirror::Object*>(space->Begin() + pos);
2701*795d594fSAndroid Build Coastguard Worker // Note: use Test() rather than Set() as this is the last time we're checking this object.
2702*795d594fSAndroid Build Coastguard Worker if (!patched_objects->Test(object)) {
2703*795d594fSAndroid Build Coastguard Worker // This is the last pass over objects, so we do not need to Set().
2704*795d594fSAndroid Build Coastguard Worker main_patch_object_visitor.VisitObject(object);
2705*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> klass = object->GetClass<kVerifyNone, kWithoutReadBarrier>();
2706*795d594fSAndroid Build Coastguard Worker if (klass == method_class || klass == constructor_class) {
2707*795d594fSAndroid Build Coastguard Worker // Patch the ArtMethod* in the mirror::Executable subobject.
2708*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Executable> as_executable =
2709*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Executable>::DownCast(object);
2710*795d594fSAndroid Build Coastguard Worker ArtMethod* unpatched_method = as_executable->GetArtMethod<kVerifyNone>();
2711*795d594fSAndroid Build Coastguard Worker ArtMethod* patched_method = main_relocate_visitor(unpatched_method);
2712*795d594fSAndroid Build Coastguard Worker as_executable->SetArtMethod</*kTransactionActive=*/ false,
2713*795d594fSAndroid Build Coastguard Worker /*kCheckTransaction=*/ true,
2714*795d594fSAndroid Build Coastguard Worker kVerifyNone>(patched_method);
2715*795d594fSAndroid Build Coastguard Worker } else if (klass == field_var_handle_class || klass == static_field_var_handle_class) {
2716*795d594fSAndroid Build Coastguard Worker // Patch the ArtField* in the mirror::FieldVarHandle subobject.
2717*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::FieldVarHandle> as_field_var_handle =
2718*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::FieldVarHandle>::DownCast(object);
2719*795d594fSAndroid Build Coastguard Worker ArtField* unpatched_field = as_field_var_handle->GetArtField<kVerifyNone>();
2720*795d594fSAndroid Build Coastguard Worker ArtField* patched_field = main_relocate_visitor(unpatched_field);
2721*795d594fSAndroid Build Coastguard Worker as_field_var_handle->SetArtField<kVerifyNone>(patched_field);
2722*795d594fSAndroid Build Coastguard Worker }
2723*795d594fSAndroid Build Coastguard Worker }
2724*795d594fSAndroid Build Coastguard Worker pos += RoundUp(object->SizeOf<kVerifyNone>(), kObjectAlignment);
2725*795d594fSAndroid Build Coastguard Worker }
2726*795d594fSAndroid Build Coastguard Worker }
2727*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && !kExtension) {
2728*795d594fSAndroid Build Coastguard Worker // We used just Test() instead of Set() above but we need to use Set()
2729*795d594fSAndroid Build Coastguard Worker // for class roots to satisfy a DCHECK() for extensions.
2730*795d594fSAndroid Build Coastguard Worker DCHECK(!patched_objects->Test(class_roots.Ptr()));
2731*795d594fSAndroid Build Coastguard Worker patched_objects->Set(class_roots.Ptr());
2732*795d594fSAndroid Build Coastguard Worker }
2733*795d594fSAndroid Build Coastguard Worker }
2734*795d594fSAndroid Build Coastguard Worker
MaybeRelocateSpaces(const std::vector<std::unique_ptr<ImageSpace>> & spaces,TimingLogger * logger)2735*795d594fSAndroid Build Coastguard Worker void MaybeRelocateSpaces(const std::vector<std::unique_ptr<ImageSpace>>& spaces,
2736*795d594fSAndroid Build Coastguard Worker TimingLogger* logger)
2737*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2738*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("MaybeRelocateSpaces", logger);
2739*795d594fSAndroid Build Coastguard Worker ImageSpace* first_space = spaces.front().get();
2740*795d594fSAndroid Build Coastguard Worker const ImageHeader& first_space_header = first_space->GetImageHeader();
2741*795d594fSAndroid Build Coastguard Worker int64_t base_diff64 =
2742*795d594fSAndroid Build Coastguard Worker static_cast<int64_t>(reinterpret_cast32<uint32_t>(first_space->Begin())) -
2743*795d594fSAndroid Build Coastguard Worker static_cast<int64_t>(reinterpret_cast32<uint32_t>(first_space_header.GetImageBegin()));
2744*795d594fSAndroid Build Coastguard Worker if (!relocate_) {
2745*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(base_diff64, 0);
2746*795d594fSAndroid Build Coastguard Worker }
2747*795d594fSAndroid Build Coastguard Worker
2748*795d594fSAndroid Build Coastguard Worker // While `Thread::Current()` is null, the `ScopedDebugDisallowReadBarriers`
2749*795d594fSAndroid Build Coastguard Worker // cannot be used but the class `ReadBarrier` shall not allow read barriers anyway.
2750*795d594fSAndroid Build Coastguard Worker // For some gtests we actually have an initialized `Thread:Current()`.
2751*795d594fSAndroid Build Coastguard Worker std::optional<ScopedDebugDisallowReadBarriers> sddrb(std::nullopt);
2752*795d594fSAndroid Build Coastguard Worker if (kCheckDebugDisallowReadBarrierCount && Thread::Current() != nullptr) {
2753*795d594fSAndroid Build Coastguard Worker sddrb.emplace(Thread::Current());
2754*795d594fSAndroid Build Coastguard Worker }
2755*795d594fSAndroid Build Coastguard Worker
2756*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::unique_ptr<ImageSpace>> spaces_ref(spaces);
2757*795d594fSAndroid Build Coastguard Worker PointerSize pointer_size = first_space_header.GetPointerSize();
2758*795d594fSAndroid Build Coastguard Worker if (pointer_size == PointerSize::k64) {
2759*795d594fSAndroid Build Coastguard Worker DoRelocateSpaces<PointerSize::k64>(spaces_ref, base_diff64);
2760*795d594fSAndroid Build Coastguard Worker } else {
2761*795d594fSAndroid Build Coastguard Worker DoRelocateSpaces<PointerSize::k32>(spaces_ref, base_diff64);
2762*795d594fSAndroid Build Coastguard Worker }
2763*795d594fSAndroid Build Coastguard Worker }
2764*795d594fSAndroid Build Coastguard Worker
DeduplicateInternedStrings(ArrayRef<const std::unique_ptr<ImageSpace>> spaces,TimingLogger * logger)2765*795d594fSAndroid Build Coastguard Worker void DeduplicateInternedStrings(ArrayRef<const std::unique_ptr<ImageSpace>> spaces,
2766*795d594fSAndroid Build Coastguard Worker TimingLogger* logger) REQUIRES_SHARED(Locks::mutator_lock_) {
2767*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("DeduplicateInternedStrings", logger);
2768*795d594fSAndroid Build Coastguard Worker DCHECK(!spaces.empty());
2769*795d594fSAndroid Build Coastguard Worker size_t num_spaces = spaces.size();
2770*795d594fSAndroid Build Coastguard Worker const ImageHeader& primary_header = spaces.front()->GetImageHeader();
2771*795d594fSAndroid Build Coastguard Worker size_t primary_image_count = primary_header.GetImageSpaceCount();
2772*795d594fSAndroid Build Coastguard Worker size_t primary_image_component_count = primary_header.GetComponentCount();
2773*795d594fSAndroid Build Coastguard Worker DCHECK_LE(primary_image_count, num_spaces);
2774*795d594fSAndroid Build Coastguard Worker // The primary boot image can be generated with `--single-image` on device, when generated
2775*795d594fSAndroid Build Coastguard Worker // in-memory or with odrefresh.
2776*795d594fSAndroid Build Coastguard Worker DCHECK(primary_image_count == primary_image_component_count || primary_image_count == 1);
2777*795d594fSAndroid Build Coastguard Worker size_t component_count = primary_image_component_count;
2778*795d594fSAndroid Build Coastguard Worker size_t space_pos = primary_image_count;
2779*795d594fSAndroid Build Coastguard Worker while (space_pos != num_spaces) {
2780*795d594fSAndroid Build Coastguard Worker const ImageHeader& current_header = spaces[space_pos]->GetImageHeader();
2781*795d594fSAndroid Build Coastguard Worker size_t image_space_count = current_header.GetImageSpaceCount();
2782*795d594fSAndroid Build Coastguard Worker DCHECK_LE(image_space_count, num_spaces - space_pos);
2783*795d594fSAndroid Build Coastguard Worker size_t dependency_component_count = current_header.GetBootImageComponentCount();
2784*795d594fSAndroid Build Coastguard Worker DCHECK_LE(dependency_component_count, component_count);
2785*795d594fSAndroid Build Coastguard Worker if (dependency_component_count < component_count) {
2786*795d594fSAndroid Build Coastguard Worker // There shall be no duplicate strings with the components that this space depends on.
2787*795d594fSAndroid Build Coastguard Worker // Find the end of the dependencies, i.e. start of non-dependency images.
2788*795d594fSAndroid Build Coastguard Worker size_t start_component_count = primary_image_component_count;
2789*795d594fSAndroid Build Coastguard Worker size_t start_pos = primary_image_count;
2790*795d594fSAndroid Build Coastguard Worker while (start_component_count != dependency_component_count) {
2791*795d594fSAndroid Build Coastguard Worker const ImageHeader& dependency_header = spaces[start_pos]->GetImageHeader();
2792*795d594fSAndroid Build Coastguard Worker DCHECK_LE(dependency_header.GetComponentCount(),
2793*795d594fSAndroid Build Coastguard Worker dependency_component_count - start_component_count);
2794*795d594fSAndroid Build Coastguard Worker start_component_count += dependency_header.GetComponentCount();
2795*795d594fSAndroid Build Coastguard Worker start_pos += dependency_header.GetImageSpaceCount();
2796*795d594fSAndroid Build Coastguard Worker }
2797*795d594fSAndroid Build Coastguard Worker // Remove duplicates from all intern tables belonging to the chunk.
2798*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::unique_ptr<ImageSpace>> old_spaces =
2799*795d594fSAndroid Build Coastguard Worker spaces.SubArray(/*pos=*/ start_pos, space_pos - start_pos);
2800*795d594fSAndroid Build Coastguard Worker SafeMap<mirror::String*, mirror::String*> intern_remap;
2801*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i != image_space_count; ++i) {
2802*795d594fSAndroid Build Coastguard Worker ImageSpace* new_space = spaces[space_pos + i].get();
2803*795d594fSAndroid Build Coastguard Worker Loader::RemoveInternTableDuplicates(old_spaces, new_space, &intern_remap);
2804*795d594fSAndroid Build Coastguard Worker }
2805*795d594fSAndroid Build Coastguard Worker // Remap string for all spaces belonging to the chunk.
2806*795d594fSAndroid Build Coastguard Worker if (!intern_remap.empty()) {
2807*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i != image_space_count; ++i) {
2808*795d594fSAndroid Build Coastguard Worker ImageSpace* new_space = spaces[space_pos + i].get();
2809*795d594fSAndroid Build Coastguard Worker Loader::RemapInternedStringDuplicates(intern_remap, new_space);
2810*795d594fSAndroid Build Coastguard Worker }
2811*795d594fSAndroid Build Coastguard Worker }
2812*795d594fSAndroid Build Coastguard Worker }
2813*795d594fSAndroid Build Coastguard Worker component_count += current_header.GetComponentCount();
2814*795d594fSAndroid Build Coastguard Worker space_pos += image_space_count;
2815*795d594fSAndroid Build Coastguard Worker }
2816*795d594fSAndroid Build Coastguard Worker }
2817*795d594fSAndroid Build Coastguard Worker
Load(const std::string & image_location,const std::string & image_filename,const std::vector<std::string> & profile_files,android::base::unique_fd art_fd,TimingLogger * logger,MemMap * image_reservation,std::string * error_msg)2818*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ImageSpace> Load(const std::string& image_location,
2819*795d594fSAndroid Build Coastguard Worker const std::string& image_filename,
2820*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>& profile_files,
2821*795d594fSAndroid Build Coastguard Worker android::base::unique_fd art_fd,
2822*795d594fSAndroid Build Coastguard Worker TimingLogger* logger,
2823*795d594fSAndroid Build Coastguard Worker /*inout*/MemMap* image_reservation,
2824*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg)
2825*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2826*795d594fSAndroid Build Coastguard Worker if (art_fd.get() != -1) {
2827*795d594fSAndroid Build Coastguard Worker VLOG(startup) << "Using image file " << image_filename.c_str() << " for image location "
2828*795d594fSAndroid Build Coastguard Worker << image_location << " for compiled extension";
2829*795d594fSAndroid Build Coastguard Worker
2830*795d594fSAndroid Build Coastguard Worker File image_file(art_fd.release(), image_filename, /*check_usage=*/ false);
2831*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ImageSpace> result = Loader::Init(&image_file,
2832*795d594fSAndroid Build Coastguard Worker image_filename.c_str(),
2833*795d594fSAndroid Build Coastguard Worker image_location.c_str(),
2834*795d594fSAndroid Build Coastguard Worker profile_files,
2835*795d594fSAndroid Build Coastguard Worker /*allow_direct_mapping=*/ false,
2836*795d594fSAndroid Build Coastguard Worker logger,
2837*795d594fSAndroid Build Coastguard Worker image_reservation,
2838*795d594fSAndroid Build Coastguard Worker error_msg);
2839*795d594fSAndroid Build Coastguard Worker // Note: We're closing the image file descriptor here when we destroy
2840*795d594fSAndroid Build Coastguard Worker // the `image_file` as we no longer need it.
2841*795d594fSAndroid Build Coastguard Worker return result;
2842*795d594fSAndroid Build Coastguard Worker }
2843*795d594fSAndroid Build Coastguard Worker
2844*795d594fSAndroid Build Coastguard Worker VLOG(startup) << "Using image file " << image_filename.c_str() << " for image location "
2845*795d594fSAndroid Build Coastguard Worker << image_location;
2846*795d594fSAndroid Build Coastguard Worker
2847*795d594fSAndroid Build Coastguard Worker // If we are in /system we can assume the image is good. We can also
2848*795d594fSAndroid Build Coastguard Worker // assume this if we are using a relocated image (i.e. image checksum
2849*795d594fSAndroid Build Coastguard Worker // matches) since this is only different by the offset. We need this to
2850*795d594fSAndroid Build Coastguard Worker // make sure that host tests continue to work.
2851*795d594fSAndroid Build Coastguard Worker // Since we are the boot image, pass null since we load the oat file from the boot image oat
2852*795d594fSAndroid Build Coastguard Worker // file name.
2853*795d594fSAndroid Build Coastguard Worker return Loader::Init(image_filename.c_str(),
2854*795d594fSAndroid Build Coastguard Worker image_location.c_str(),
2855*795d594fSAndroid Build Coastguard Worker logger,
2856*795d594fSAndroid Build Coastguard Worker image_reservation,
2857*795d594fSAndroid Build Coastguard Worker error_msg);
2858*795d594fSAndroid Build Coastguard Worker }
2859*795d594fSAndroid Build Coastguard Worker
OpenOatFile(ImageSpace * space,android::base::unique_fd vdex_fd,android::base::unique_fd oat_fd,ArrayRef<const std::string> dex_filenames,ArrayRef<File> dex_files,bool validate_oat_file,ArrayRef<const std::unique_ptr<ImageSpace>> dependencies,TimingLogger * logger,MemMap * image_reservation,std::string * error_msg)2860*795d594fSAndroid Build Coastguard Worker bool OpenOatFile(ImageSpace* space,
2861*795d594fSAndroid Build Coastguard Worker android::base::unique_fd vdex_fd,
2862*795d594fSAndroid Build Coastguard Worker android::base::unique_fd oat_fd,
2863*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> dex_filenames,
2864*795d594fSAndroid Build Coastguard Worker ArrayRef<File> dex_files,
2865*795d594fSAndroid Build Coastguard Worker bool validate_oat_file,
2866*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::unique_ptr<ImageSpace>> dependencies,
2867*795d594fSAndroid Build Coastguard Worker TimingLogger* logger,
2868*795d594fSAndroid Build Coastguard Worker /*inout*/ MemMap* image_reservation,
2869*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) {
2870*795d594fSAndroid Build Coastguard Worker // VerifyImageAllocations() will be called later in Runtime::Init()
2871*795d594fSAndroid Build Coastguard Worker // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
2872*795d594fSAndroid Build Coastguard Worker // and ArtField::java_lang_reflect_ArtField_, which are used from
2873*795d594fSAndroid Build Coastguard Worker // Object::SizeOf() which VerifyImageAllocations() calls, are not
2874*795d594fSAndroid Build Coastguard Worker // set yet at this point.
2875*795d594fSAndroid Build Coastguard Worker DCHECK(image_reservation != nullptr);
2876*795d594fSAndroid Build Coastguard Worker std::unique_ptr<OatFile> oat_file;
2877*795d594fSAndroid Build Coastguard Worker {
2878*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("OpenOatFile", logger);
2879*795d594fSAndroid Build Coastguard Worker std::string oat_filename =
2880*795d594fSAndroid Build Coastguard Worker ImageHeader::GetOatLocationFromImageLocation(space->GetImageFilename());
2881*795d594fSAndroid Build Coastguard Worker std::string oat_location =
2882*795d594fSAndroid Build Coastguard Worker ImageHeader::GetOatLocationFromImageLocation(space->GetImageLocation());
2883*795d594fSAndroid Build Coastguard Worker
2884*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(vdex_fd.get() != -1, oat_fd.get() != -1);
2885*795d594fSAndroid Build Coastguard Worker if (vdex_fd.get() == -1) {
2886*795d594fSAndroid Build Coastguard Worker oat_file.reset(OatFile::Open(/*zip_fd=*/-1,
2887*795d594fSAndroid Build Coastguard Worker oat_filename,
2888*795d594fSAndroid Build Coastguard Worker oat_location,
2889*795d594fSAndroid Build Coastguard Worker executable_,
2890*795d594fSAndroid Build Coastguard Worker /*low_4gb=*/false,
2891*795d594fSAndroid Build Coastguard Worker dex_filenames,
2892*795d594fSAndroid Build Coastguard Worker dex_files,
2893*795d594fSAndroid Build Coastguard Worker image_reservation,
2894*795d594fSAndroid Build Coastguard Worker error_msg));
2895*795d594fSAndroid Build Coastguard Worker } else {
2896*795d594fSAndroid Build Coastguard Worker oat_file.reset(OatFile::Open(/*zip_fd=*/-1,
2897*795d594fSAndroid Build Coastguard Worker vdex_fd.get(),
2898*795d594fSAndroid Build Coastguard Worker oat_fd.get(),
2899*795d594fSAndroid Build Coastguard Worker oat_location,
2900*795d594fSAndroid Build Coastguard Worker executable_,
2901*795d594fSAndroid Build Coastguard Worker /*low_4gb=*/false,
2902*795d594fSAndroid Build Coastguard Worker dex_filenames,
2903*795d594fSAndroid Build Coastguard Worker dex_files,
2904*795d594fSAndroid Build Coastguard Worker image_reservation,
2905*795d594fSAndroid Build Coastguard Worker error_msg));
2906*795d594fSAndroid Build Coastguard Worker // We no longer need the file descriptors and they will be closed by
2907*795d594fSAndroid Build Coastguard Worker // the unique_fd destructor when we leave this function.
2908*795d594fSAndroid Build Coastguard Worker }
2909*795d594fSAndroid Build Coastguard Worker
2910*795d594fSAndroid Build Coastguard Worker if (oat_file == nullptr) {
2911*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
2912*795d594fSAndroid Build Coastguard Worker oat_filename.c_str(),
2913*795d594fSAndroid Build Coastguard Worker space->GetName(),
2914*795d594fSAndroid Build Coastguard Worker error_msg->c_str());
2915*795d594fSAndroid Build Coastguard Worker return false;
2916*795d594fSAndroid Build Coastguard Worker }
2917*795d594fSAndroid Build Coastguard Worker const ImageHeader& image_header = space->GetImageHeader();
2918*795d594fSAndroid Build Coastguard Worker uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
2919*795d594fSAndroid Build Coastguard Worker uint32_t image_oat_checksum = image_header.GetOatChecksum();
2920*795d594fSAndroid Build Coastguard Worker if (oat_checksum != image_oat_checksum) {
2921*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum"
2922*795d594fSAndroid Build Coastguard Worker " 0x%x in image %s",
2923*795d594fSAndroid Build Coastguard Worker oat_checksum,
2924*795d594fSAndroid Build Coastguard Worker image_oat_checksum,
2925*795d594fSAndroid Build Coastguard Worker space->GetName());
2926*795d594fSAndroid Build Coastguard Worker return false;
2927*795d594fSAndroid Build Coastguard Worker }
2928*795d594fSAndroid Build Coastguard Worker const char* oat_boot_class_path =
2929*795d594fSAndroid Build Coastguard Worker oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey);
2930*795d594fSAndroid Build Coastguard Worker oat_boot_class_path = (oat_boot_class_path != nullptr) ? oat_boot_class_path : "";
2931*795d594fSAndroid Build Coastguard Worker const char* oat_boot_class_path_checksums =
2932*795d594fSAndroid Build Coastguard Worker oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
2933*795d594fSAndroid Build Coastguard Worker oat_boot_class_path_checksums =
2934*795d594fSAndroid Build Coastguard Worker (oat_boot_class_path_checksums != nullptr) ? oat_boot_class_path_checksums : "";
2935*795d594fSAndroid Build Coastguard Worker size_t component_count = image_header.GetComponentCount();
2936*795d594fSAndroid Build Coastguard Worker if (component_count == 0u) {
2937*795d594fSAndroid Build Coastguard Worker if (oat_boot_class_path[0] != 0 || oat_boot_class_path_checksums[0] != 0) {
2938*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Unexpected non-empty boot class path %s and/or checksums %s"
2939*795d594fSAndroid Build Coastguard Worker " in image %s",
2940*795d594fSAndroid Build Coastguard Worker oat_boot_class_path,
2941*795d594fSAndroid Build Coastguard Worker oat_boot_class_path_checksums,
2942*795d594fSAndroid Build Coastguard Worker space->GetName());
2943*795d594fSAndroid Build Coastguard Worker return false;
2944*795d594fSAndroid Build Coastguard Worker }
2945*795d594fSAndroid Build Coastguard Worker } else if (dependencies.empty()) {
2946*795d594fSAndroid Build Coastguard Worker std::string expected_boot_class_path = Join(ArrayRef<const std::string>(
2947*795d594fSAndroid Build Coastguard Worker boot_class_path_locations_).SubArray(0u, component_count), ':');
2948*795d594fSAndroid Build Coastguard Worker if (expected_boot_class_path != oat_boot_class_path) {
2949*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to match oat boot class path %s to expected "
2950*795d594fSAndroid Build Coastguard Worker "boot class path %s in image %s",
2951*795d594fSAndroid Build Coastguard Worker oat_boot_class_path,
2952*795d594fSAndroid Build Coastguard Worker expected_boot_class_path.c_str(),
2953*795d594fSAndroid Build Coastguard Worker space->GetName());
2954*795d594fSAndroid Build Coastguard Worker return false;
2955*795d594fSAndroid Build Coastguard Worker }
2956*795d594fSAndroid Build Coastguard Worker } else {
2957*795d594fSAndroid Build Coastguard Worker std::string local_error_msg;
2958*795d594fSAndroid Build Coastguard Worker if (!VerifyBootClassPathChecksums(
2959*795d594fSAndroid Build Coastguard Worker oat_boot_class_path_checksums,
2960*795d594fSAndroid Build Coastguard Worker oat_boot_class_path,
2961*795d594fSAndroid Build Coastguard Worker dependencies,
2962*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(boot_class_path_locations_),
2963*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(boot_class_path_),
2964*795d594fSAndroid Build Coastguard Worker &local_error_msg)) {
2965*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to verify BCP %s with checksums %s in image %s: %s",
2966*795d594fSAndroid Build Coastguard Worker oat_boot_class_path,
2967*795d594fSAndroid Build Coastguard Worker oat_boot_class_path_checksums,
2968*795d594fSAndroid Build Coastguard Worker space->GetName(),
2969*795d594fSAndroid Build Coastguard Worker local_error_msg.c_str());
2970*795d594fSAndroid Build Coastguard Worker return false;
2971*795d594fSAndroid Build Coastguard Worker }
2972*795d594fSAndroid Build Coastguard Worker }
2973*795d594fSAndroid Build Coastguard Worker ptrdiff_t relocation_diff = space->Begin() - image_header.GetImageBegin();
2974*795d594fSAndroid Build Coastguard Worker CHECK(image_header.GetOatDataBegin() != nullptr);
2975*795d594fSAndroid Build Coastguard Worker uint8_t* oat_data_begin = image_header.GetOatDataBegin() + relocation_diff;
2976*795d594fSAndroid Build Coastguard Worker if (oat_file->Begin() != oat_data_begin) {
2977*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Oat file '%s' referenced from image %s has unexpected begin"
2978*795d594fSAndroid Build Coastguard Worker " %p v. %p",
2979*795d594fSAndroid Build Coastguard Worker oat_filename.c_str(),
2980*795d594fSAndroid Build Coastguard Worker space->GetName(),
2981*795d594fSAndroid Build Coastguard Worker oat_file->Begin(),
2982*795d594fSAndroid Build Coastguard Worker oat_data_begin);
2983*795d594fSAndroid Build Coastguard Worker return false;
2984*795d594fSAndroid Build Coastguard Worker }
2985*795d594fSAndroid Build Coastguard Worker }
2986*795d594fSAndroid Build Coastguard Worker if (validate_oat_file) {
2987*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming timing("ValidateOatFile", logger);
2988*795d594fSAndroid Build Coastguard Worker if (!ImageSpace::ValidateOatFile(*oat_file, error_msg)) {
2989*795d594fSAndroid Build Coastguard Worker DCHECK(!error_msg->empty());
2990*795d594fSAndroid Build Coastguard Worker return false;
2991*795d594fSAndroid Build Coastguard Worker }
2992*795d594fSAndroid Build Coastguard Worker }
2993*795d594fSAndroid Build Coastguard Worker
2994*795d594fSAndroid Build Coastguard Worker // As an optimization, madvise the oat file into memory if it's being used
2995*795d594fSAndroid Build Coastguard Worker // for execution with an active runtime. This can significantly improve
2996*795d594fSAndroid Build Coastguard Worker // ZygoteInit class preload performance.
2997*795d594fSAndroid Build Coastguard Worker if (executable_) {
2998*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
2999*795d594fSAndroid Build Coastguard Worker if (runtime != nullptr) {
3000*795d594fSAndroid Build Coastguard Worker Runtime::MadviseFileForRange(runtime->GetMadviseWillNeedSizeOdex(),
3001*795d594fSAndroid Build Coastguard Worker oat_file->Size(),
3002*795d594fSAndroid Build Coastguard Worker oat_file->Begin(),
3003*795d594fSAndroid Build Coastguard Worker oat_file->End(),
3004*795d594fSAndroid Build Coastguard Worker oat_file->GetLocation());
3005*795d594fSAndroid Build Coastguard Worker }
3006*795d594fSAndroid Build Coastguard Worker }
3007*795d594fSAndroid Build Coastguard Worker
3008*795d594fSAndroid Build Coastguard Worker space->oat_file_ = std::move(oat_file);
3009*795d594fSAndroid Build Coastguard Worker space->oat_file_non_owned_ = space->oat_file_.get();
3010*795d594fSAndroid Build Coastguard Worker
3011*795d594fSAndroid Build Coastguard Worker return true;
3012*795d594fSAndroid Build Coastguard Worker }
3013*795d594fSAndroid Build Coastguard Worker
LoadComponents(const BootImageLayout::ImageChunk & chunk,bool validate_oat_file,size_t max_image_space_dependencies,TimingLogger * logger,std::vector<std::unique_ptr<ImageSpace>> * spaces,MemMap * image_reservation,std::string * error_msg)3014*795d594fSAndroid Build Coastguard Worker bool LoadComponents(const BootImageLayout::ImageChunk& chunk,
3015*795d594fSAndroid Build Coastguard Worker bool validate_oat_file,
3016*795d594fSAndroid Build Coastguard Worker size_t max_image_space_dependencies,
3017*795d594fSAndroid Build Coastguard Worker TimingLogger* logger,
3018*795d594fSAndroid Build Coastguard Worker /*inout*/std::vector<std::unique_ptr<ImageSpace>>* spaces,
3019*795d594fSAndroid Build Coastguard Worker /*inout*/MemMap* image_reservation,
3020*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg)
3021*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
3022*795d594fSAndroid Build Coastguard Worker // Make sure we destroy the spaces we created if we're returning an error.
3023*795d594fSAndroid Build Coastguard Worker // Note that this can unmap part of the original `image_reservation`.
3024*795d594fSAndroid Build Coastguard Worker class Guard {
3025*795d594fSAndroid Build Coastguard Worker public:
3026*795d594fSAndroid Build Coastguard Worker explicit Guard(std::vector<std::unique_ptr<ImageSpace>>* spaces_in)
3027*795d594fSAndroid Build Coastguard Worker : spaces_(spaces_in), committed_(spaces_->size()) {}
3028*795d594fSAndroid Build Coastguard Worker void Commit() {
3029*795d594fSAndroid Build Coastguard Worker DCHECK_LT(committed_, spaces_->size());
3030*795d594fSAndroid Build Coastguard Worker committed_ = spaces_->size();
3031*795d594fSAndroid Build Coastguard Worker }
3032*795d594fSAndroid Build Coastguard Worker ~Guard() {
3033*795d594fSAndroid Build Coastguard Worker DCHECK_LE(committed_, spaces_->size());
3034*795d594fSAndroid Build Coastguard Worker spaces_->resize(committed_);
3035*795d594fSAndroid Build Coastguard Worker }
3036*795d594fSAndroid Build Coastguard Worker private:
3037*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<ImageSpace>>* const spaces_;
3038*795d594fSAndroid Build Coastguard Worker size_t committed_;
3039*795d594fSAndroid Build Coastguard Worker };
3040*795d594fSAndroid Build Coastguard Worker Guard guard(spaces);
3041*795d594fSAndroid Build Coastguard Worker
3042*795d594fSAndroid Build Coastguard Worker bool is_extension = (chunk.start_index != 0u);
3043*795d594fSAndroid Build Coastguard Worker DCHECK_NE(spaces->empty(), is_extension);
3044*795d594fSAndroid Build Coastguard Worker if (max_image_space_dependencies < chunk.boot_image_component_count) {
3045*795d594fSAndroid Build Coastguard Worker DCHECK(is_extension);
3046*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Missing dependencies for extension component %s, %zu < %u",
3047*795d594fSAndroid Build Coastguard Worker boot_class_path_locations_[chunk.start_index].c_str(),
3048*795d594fSAndroid Build Coastguard Worker max_image_space_dependencies,
3049*795d594fSAndroid Build Coastguard Worker chunk.boot_image_component_count);
3050*795d594fSAndroid Build Coastguard Worker return false;
3051*795d594fSAndroid Build Coastguard Worker }
3052*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> requested_bcp_locations =
3053*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(boot_class_path_locations_).SubArray(
3054*795d594fSAndroid Build Coastguard Worker chunk.start_index, chunk.image_space_count);
3055*795d594fSAndroid Build Coastguard Worker std::vector<std::string> locations =
3056*795d594fSAndroid Build Coastguard Worker ExpandMultiImageLocations(requested_bcp_locations, chunk.base_location, is_extension);
3057*795d594fSAndroid Build Coastguard Worker std::vector<std::string> filenames =
3058*795d594fSAndroid Build Coastguard Worker ExpandMultiImageLocations(requested_bcp_locations, chunk.base_filename, is_extension);
3059*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(locations.size(), filenames.size());
3060*795d594fSAndroid Build Coastguard Worker size_t max_dependency_count = spaces->size();
3061*795d594fSAndroid Build Coastguard Worker for (size_t i = 0u, size = locations.size(); i != size; ++i) {
3062*795d594fSAndroid Build Coastguard Worker android::base::unique_fd image_fd;
3063*795d594fSAndroid Build Coastguard Worker if (chunk.art_fd.get() >= 0) {
3064*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(locations.size(), 1u);
3065*795d594fSAndroid Build Coastguard Worker image_fd = std::move(chunk.art_fd);
3066*795d594fSAndroid Build Coastguard Worker } else {
3067*795d594fSAndroid Build Coastguard Worker size_t pos = chunk.start_index + i;
3068*795d594fSAndroid Build Coastguard Worker int arg_image_fd =
3069*795d594fSAndroid Build Coastguard Worker pos < boot_class_path_image_files_.size() ? boot_class_path_image_files_[pos].Fd() : -1;
3070*795d594fSAndroid Build Coastguard Worker if (arg_image_fd >= 0) {
3071*795d594fSAndroid Build Coastguard Worker image_fd.reset(DupCloexec(arg_image_fd));
3072*795d594fSAndroid Build Coastguard Worker }
3073*795d594fSAndroid Build Coastguard Worker }
3074*795d594fSAndroid Build Coastguard Worker spaces->push_back(Load(locations[i],
3075*795d594fSAndroid Build Coastguard Worker filenames[i],
3076*795d594fSAndroid Build Coastguard Worker chunk.profile_files,
3077*795d594fSAndroid Build Coastguard Worker std::move(image_fd),
3078*795d594fSAndroid Build Coastguard Worker logger,
3079*795d594fSAndroid Build Coastguard Worker image_reservation,
3080*795d594fSAndroid Build Coastguard Worker error_msg));
3081*795d594fSAndroid Build Coastguard Worker const ImageSpace* space = spaces->back().get();
3082*795d594fSAndroid Build Coastguard Worker if (space == nullptr) {
3083*795d594fSAndroid Build Coastguard Worker return false;
3084*795d594fSAndroid Build Coastguard Worker }
3085*795d594fSAndroid Build Coastguard Worker uint32_t expected_component_count = (i == 0u) ? chunk.component_count : 0u;
3086*795d594fSAndroid Build Coastguard Worker uint32_t expected_reservation_size = (i == 0u) ? chunk.reservation_size : 0u;
3087*795d594fSAndroid Build Coastguard Worker if (!Loader::CheckImageReservationSize(*space, expected_reservation_size, error_msg) ||
3088*795d594fSAndroid Build Coastguard Worker !Loader::CheckImageComponentCount(*space, expected_component_count, error_msg)) {
3089*795d594fSAndroid Build Coastguard Worker return false;
3090*795d594fSAndroid Build Coastguard Worker }
3091*795d594fSAndroid Build Coastguard Worker const ImageHeader& header = space->GetImageHeader();
3092*795d594fSAndroid Build Coastguard Worker if (i == 0 && (chunk.checksum != header.GetImageChecksum() ||
3093*795d594fSAndroid Build Coastguard Worker chunk.image_space_count != header.GetImageSpaceCount() ||
3094*795d594fSAndroid Build Coastguard Worker chunk.boot_image_component_count != header.GetBootImageComponentCount() ||
3095*795d594fSAndroid Build Coastguard Worker chunk.boot_image_checksum != header.GetBootImageChecksum() ||
3096*795d594fSAndroid Build Coastguard Worker chunk.boot_image_size != header.GetBootImageSize())) {
3097*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Image header modified since previously read from %s; "
3098*795d594fSAndroid Build Coastguard Worker "checksum: 0x%08x -> 0x%08x,"
3099*795d594fSAndroid Build Coastguard Worker "image_space_count: %u -> %u"
3100*795d594fSAndroid Build Coastguard Worker "boot_image_component_count: %u -> %u, "
3101*795d594fSAndroid Build Coastguard Worker "boot_image_checksum: 0x%08x -> 0x%08x"
3102*795d594fSAndroid Build Coastguard Worker "boot_image_size: 0x%08x -> 0x%08x",
3103*795d594fSAndroid Build Coastguard Worker space->GetImageFilename().c_str(),
3104*795d594fSAndroid Build Coastguard Worker chunk.checksum,
3105*795d594fSAndroid Build Coastguard Worker chunk.image_space_count,
3106*795d594fSAndroid Build Coastguard Worker header.GetImageSpaceCount(),
3107*795d594fSAndroid Build Coastguard Worker header.GetImageChecksum(),
3108*795d594fSAndroid Build Coastguard Worker chunk.boot_image_component_count,
3109*795d594fSAndroid Build Coastguard Worker header.GetBootImageComponentCount(),
3110*795d594fSAndroid Build Coastguard Worker chunk.boot_image_checksum,
3111*795d594fSAndroid Build Coastguard Worker header.GetBootImageChecksum(),
3112*795d594fSAndroid Build Coastguard Worker chunk.boot_image_size,
3113*795d594fSAndroid Build Coastguard Worker header.GetBootImageSize());
3114*795d594fSAndroid Build Coastguard Worker return false;
3115*795d594fSAndroid Build Coastguard Worker }
3116*795d594fSAndroid Build Coastguard Worker }
3117*795d594fSAndroid Build Coastguard Worker DCHECK_GE(max_image_space_dependencies, chunk.boot_image_component_count);
3118*795d594fSAndroid Build Coastguard Worker size_t dependency_count = 0;
3119*795d594fSAndroid Build Coastguard Worker size_t dependency_component_count = 0;
3120*795d594fSAndroid Build Coastguard Worker while (dependency_component_count < chunk.boot_image_component_count &&
3121*795d594fSAndroid Build Coastguard Worker dependency_count < max_dependency_count) {
3122*795d594fSAndroid Build Coastguard Worker const ImageHeader& current_header = (*spaces)[dependency_count]->GetImageHeader();
3123*795d594fSAndroid Build Coastguard Worker dependency_component_count += current_header.GetComponentCount();
3124*795d594fSAndroid Build Coastguard Worker dependency_count += current_header.GetImageSpaceCount();
3125*795d594fSAndroid Build Coastguard Worker }
3126*795d594fSAndroid Build Coastguard Worker if (dependency_component_count != chunk.boot_image_component_count) {
3127*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf(
3128*795d594fSAndroid Build Coastguard Worker "Unable to find dependencies from image spaces; boot_image_component_count: %u",
3129*795d594fSAndroid Build Coastguard Worker chunk.boot_image_component_count);
3130*795d594fSAndroid Build Coastguard Worker return false;
3131*795d594fSAndroid Build Coastguard Worker }
3132*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::unique_ptr<ImageSpace>> dependencies =
3133*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::unique_ptr<ImageSpace>>(*spaces).SubArray(
3134*795d594fSAndroid Build Coastguard Worker /*pos=*/ 0u, dependency_count);
3135*795d594fSAndroid Build Coastguard Worker for (size_t i = 0u, size = locations.size(); i != size; ++i) {
3136*795d594fSAndroid Build Coastguard Worker ImageSpace* space = (*spaces)[spaces->size() - chunk.image_space_count + i].get();
3137*795d594fSAndroid Build Coastguard Worker size_t bcp_chunk_size = (chunk.image_space_count == 1u) ? chunk.component_count : 1u;
3138*795d594fSAndroid Build Coastguard Worker
3139*795d594fSAndroid Build Coastguard Worker size_t pos = chunk.start_index + i;
3140*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_files =
3141*795d594fSAndroid Build Coastguard Worker boot_class_path_files_.empty() ?
3142*795d594fSAndroid Build Coastguard Worker ArrayRef<File>() :
3143*795d594fSAndroid Build Coastguard Worker boot_class_path_files_.SubArray(/*pos=*/pos, bcp_chunk_size);
3144*795d594fSAndroid Build Coastguard Worker
3145*795d594fSAndroid Build Coastguard Worker // Select vdex and oat FD if any exists.
3146*795d594fSAndroid Build Coastguard Worker android::base::unique_fd vdex_fd;
3147*795d594fSAndroid Build Coastguard Worker android::base::unique_fd oat_fd;
3148*795d594fSAndroid Build Coastguard Worker if (chunk.vdex_fd.get() >= 0) {
3149*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(locations.size(), 1u);
3150*795d594fSAndroid Build Coastguard Worker vdex_fd = std::move(chunk.vdex_fd);
3151*795d594fSAndroid Build Coastguard Worker } else {
3152*795d594fSAndroid Build Coastguard Worker int arg_vdex_fd =
3153*795d594fSAndroid Build Coastguard Worker pos < boot_class_path_vdex_files_.size() ? boot_class_path_vdex_files_[pos].Fd() : -1;
3154*795d594fSAndroid Build Coastguard Worker if (arg_vdex_fd >= 0) {
3155*795d594fSAndroid Build Coastguard Worker vdex_fd.reset(DupCloexec(arg_vdex_fd));
3156*795d594fSAndroid Build Coastguard Worker }
3157*795d594fSAndroid Build Coastguard Worker }
3158*795d594fSAndroid Build Coastguard Worker if (chunk.oat_fd.get() >= 0) {
3159*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(locations.size(), 1u);
3160*795d594fSAndroid Build Coastguard Worker oat_fd = std::move(chunk.oat_fd);
3161*795d594fSAndroid Build Coastguard Worker } else {
3162*795d594fSAndroid Build Coastguard Worker int arg_oat_fd =
3163*795d594fSAndroid Build Coastguard Worker pos < boot_class_path_oat_files_.size() ? boot_class_path_oat_files_[pos].Fd() : -1;
3164*795d594fSAndroid Build Coastguard Worker if (arg_oat_fd >= 0) {
3165*795d594fSAndroid Build Coastguard Worker oat_fd.reset(DupCloexec(arg_oat_fd));
3166*795d594fSAndroid Build Coastguard Worker }
3167*795d594fSAndroid Build Coastguard Worker }
3168*795d594fSAndroid Build Coastguard Worker
3169*795d594fSAndroid Build Coastguard Worker if (!OpenOatFile(space,
3170*795d594fSAndroid Build Coastguard Worker std::move(vdex_fd),
3171*795d594fSAndroid Build Coastguard Worker std::move(oat_fd),
3172*795d594fSAndroid Build Coastguard Worker boot_class_path_.SubArray(/*pos=*/pos, bcp_chunk_size),
3173*795d594fSAndroid Build Coastguard Worker boot_class_path_files,
3174*795d594fSAndroid Build Coastguard Worker validate_oat_file,
3175*795d594fSAndroid Build Coastguard Worker dependencies,
3176*795d594fSAndroid Build Coastguard Worker logger,
3177*795d594fSAndroid Build Coastguard Worker image_reservation,
3178*795d594fSAndroid Build Coastguard Worker error_msg)) {
3179*795d594fSAndroid Build Coastguard Worker return false;
3180*795d594fSAndroid Build Coastguard Worker }
3181*795d594fSAndroid Build Coastguard Worker }
3182*795d594fSAndroid Build Coastguard Worker
3183*795d594fSAndroid Build Coastguard Worker guard.Commit();
3184*795d594fSAndroid Build Coastguard Worker return true;
3185*795d594fSAndroid Build Coastguard Worker }
3186*795d594fSAndroid Build Coastguard Worker
ReserveBootImageMemory(uint8_t * addr,uint32_t reservation_size,std::string * error_msg)3187*795d594fSAndroid Build Coastguard Worker MemMap ReserveBootImageMemory(uint8_t* addr,
3188*795d594fSAndroid Build Coastguard Worker uint32_t reservation_size,
3189*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
3190*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(reservation_size, kElfSegmentAlignment);
3191*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(addr, kElfSegmentAlignment);
3192*795d594fSAndroid Build Coastguard Worker return MemMap::MapAnonymous("Boot image reservation",
3193*795d594fSAndroid Build Coastguard Worker addr,
3194*795d594fSAndroid Build Coastguard Worker reservation_size,
3195*795d594fSAndroid Build Coastguard Worker PROT_NONE,
3196*795d594fSAndroid Build Coastguard Worker /*low_4gb=*/ true,
3197*795d594fSAndroid Build Coastguard Worker /*reuse=*/ false,
3198*795d594fSAndroid Build Coastguard Worker /*reservation=*/ nullptr,
3199*795d594fSAndroid Build Coastguard Worker error_msg);
3200*795d594fSAndroid Build Coastguard Worker }
3201*795d594fSAndroid Build Coastguard Worker
RemapExtraReservation(size_t extra_reservation_size,MemMap * image_reservation,MemMap * extra_reservation,std::string * error_msg)3202*795d594fSAndroid Build Coastguard Worker bool RemapExtraReservation(size_t extra_reservation_size,
3203*795d594fSAndroid Build Coastguard Worker /*inout*/MemMap* image_reservation,
3204*795d594fSAndroid Build Coastguard Worker /*out*/MemMap* extra_reservation,
3205*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
3206*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(extra_reservation_size, kElfSegmentAlignment);
3207*795d594fSAndroid Build Coastguard Worker DCHECK(!extra_reservation->IsValid());
3208*795d594fSAndroid Build Coastguard Worker size_t expected_size = image_reservation->IsValid() ? image_reservation->Size() : 0u;
3209*795d594fSAndroid Build Coastguard Worker if (extra_reservation_size != expected_size) {
3210*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Image reservation mismatch after loading boot image: %zu != %zu",
3211*795d594fSAndroid Build Coastguard Worker extra_reservation_size,
3212*795d594fSAndroid Build Coastguard Worker expected_size);
3213*795d594fSAndroid Build Coastguard Worker return false;
3214*795d594fSAndroid Build Coastguard Worker }
3215*795d594fSAndroid Build Coastguard Worker if (extra_reservation_size != 0u) {
3216*795d594fSAndroid Build Coastguard Worker DCHECK(image_reservation->IsValid());
3217*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(extra_reservation_size, image_reservation->Size());
3218*795d594fSAndroid Build Coastguard Worker *extra_reservation = image_reservation->RemapAtEnd(image_reservation->Begin(),
3219*795d594fSAndroid Build Coastguard Worker "Boot image extra reservation",
3220*795d594fSAndroid Build Coastguard Worker PROT_NONE,
3221*795d594fSAndroid Build Coastguard Worker error_msg);
3222*795d594fSAndroid Build Coastguard Worker if (!extra_reservation->IsValid()) {
3223*795d594fSAndroid Build Coastguard Worker return false;
3224*795d594fSAndroid Build Coastguard Worker }
3225*795d594fSAndroid Build Coastguard Worker }
3226*795d594fSAndroid Build Coastguard Worker DCHECK(!image_reservation->IsValid());
3227*795d594fSAndroid Build Coastguard Worker return true;
3228*795d594fSAndroid Build Coastguard Worker }
3229*795d594fSAndroid Build Coastguard Worker
3230*795d594fSAndroid Build Coastguard Worker const ArrayRef<const std::string> boot_class_path_;
3231*795d594fSAndroid Build Coastguard Worker const ArrayRef<const std::string> boot_class_path_locations_;
3232*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_files_;
3233*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_image_files_;
3234*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_vdex_files_;
3235*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_oat_files_;
3236*795d594fSAndroid Build Coastguard Worker const ArrayRef<const std::string> image_locations_;
3237*795d594fSAndroid Build Coastguard Worker const InstructionSet image_isa_;
3238*795d594fSAndroid Build Coastguard Worker const bool relocate_;
3239*795d594fSAndroid Build Coastguard Worker const bool executable_;
3240*795d594fSAndroid Build Coastguard Worker bool has_system_;
3241*795d594fSAndroid Build Coastguard Worker const std::string* apex_versions_;
3242*795d594fSAndroid Build Coastguard Worker };
3243*795d594fSAndroid Build Coastguard Worker
LoadFromSystem(size_t extra_reservation_size,bool allow_in_memory_compilation,std::vector<std::unique_ptr<ImageSpace>> * boot_image_spaces,MemMap * extra_reservation,std::string * error_msg)3244*795d594fSAndroid Build Coastguard Worker bool ImageSpace::BootImageLoader::LoadFromSystem(
3245*795d594fSAndroid Build Coastguard Worker size_t extra_reservation_size,
3246*795d594fSAndroid Build Coastguard Worker bool allow_in_memory_compilation,
3247*795d594fSAndroid Build Coastguard Worker /*out*/std::vector<std::unique_ptr<ImageSpace>>* boot_image_spaces,
3248*795d594fSAndroid Build Coastguard Worker /*out*/MemMap* extra_reservation,
3249*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
3250*795d594fSAndroid Build Coastguard Worker TimingLogger logger(__PRETTY_FUNCTION__, /*precise=*/ true, VLOG_IS_ON(image));
3251*795d594fSAndroid Build Coastguard Worker
3252*795d594fSAndroid Build Coastguard Worker if (gPageSize != kMinPageSize) {
3253*795d594fSAndroid Build Coastguard Worker *error_msg = "Loading boot image is only supported on devices with 4K page size";
3254*795d594fSAndroid Build Coastguard Worker return false;
3255*795d594fSAndroid Build Coastguard Worker }
3256*795d594fSAndroid Build Coastguard Worker
3257*795d594fSAndroid Build Coastguard Worker BootImageLayout layout(image_locations_,
3258*795d594fSAndroid Build Coastguard Worker boot_class_path_,
3259*795d594fSAndroid Build Coastguard Worker boot_class_path_locations_,
3260*795d594fSAndroid Build Coastguard Worker boot_class_path_files_,
3261*795d594fSAndroid Build Coastguard Worker boot_class_path_image_files_,
3262*795d594fSAndroid Build Coastguard Worker boot_class_path_vdex_files_,
3263*795d594fSAndroid Build Coastguard Worker boot_class_path_oat_files_,
3264*795d594fSAndroid Build Coastguard Worker apex_versions_);
3265*795d594fSAndroid Build Coastguard Worker if (!layout.LoadFromSystem(image_isa_, allow_in_memory_compilation, error_msg)) {
3266*795d594fSAndroid Build Coastguard Worker return false;
3267*795d594fSAndroid Build Coastguard Worker }
3268*795d594fSAndroid Build Coastguard Worker
3269*795d594fSAndroid Build Coastguard Worker // Load the image. We don't validate oat files in this stage because they have been validated
3270*795d594fSAndroid Build Coastguard Worker // before.
3271*795d594fSAndroid Build Coastguard Worker if (!LoadImage(layout,
3272*795d594fSAndroid Build Coastguard Worker /*validate_oat_file=*/ false,
3273*795d594fSAndroid Build Coastguard Worker extra_reservation_size,
3274*795d594fSAndroid Build Coastguard Worker &logger,
3275*795d594fSAndroid Build Coastguard Worker boot_image_spaces,
3276*795d594fSAndroid Build Coastguard Worker extra_reservation,
3277*795d594fSAndroid Build Coastguard Worker error_msg)) {
3278*795d594fSAndroid Build Coastguard Worker return false;
3279*795d594fSAndroid Build Coastguard Worker }
3280*795d594fSAndroid Build Coastguard Worker
3281*795d594fSAndroid Build Coastguard Worker if (VLOG_IS_ON(image)) {
3282*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "ImageSpace::BootImageLoader::LoadFromSystem exiting "
3283*795d594fSAndroid Build Coastguard Worker << *boot_image_spaces->front();
3284*795d594fSAndroid Build Coastguard Worker logger.Dump(LOG_STREAM(INFO));
3285*795d594fSAndroid Build Coastguard Worker }
3286*795d594fSAndroid Build Coastguard Worker return true;
3287*795d594fSAndroid Build Coastguard Worker }
3288*795d594fSAndroid Build Coastguard Worker
IsBootClassPathOnDisk(InstructionSet image_isa)3289*795d594fSAndroid Build Coastguard Worker bool ImageSpace::IsBootClassPathOnDisk(InstructionSet image_isa) {
3290*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
3291*795d594fSAndroid Build Coastguard Worker BootImageLayout layout(ArrayRef<const std::string>(runtime->GetImageLocations()),
3292*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(runtime->GetBootClassPath()),
3293*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(runtime->GetBootClassPathLocations()),
3294*795d594fSAndroid Build Coastguard Worker runtime->GetBootClassPathFiles(),
3295*795d594fSAndroid Build Coastguard Worker runtime->GetBootClassPathImageFiles(),
3296*795d594fSAndroid Build Coastguard Worker runtime->GetBootClassPathVdexFiles(),
3297*795d594fSAndroid Build Coastguard Worker runtime->GetBootClassPathOatFiles(),
3298*795d594fSAndroid Build Coastguard Worker &runtime->GetApexVersions());
3299*795d594fSAndroid Build Coastguard Worker const std::string image_location = layout.GetPrimaryImageLocation();
3300*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ImageHeader> image_header;
3301*795d594fSAndroid Build Coastguard Worker std::string error_msg;
3302*795d594fSAndroid Build Coastguard Worker
3303*795d594fSAndroid Build Coastguard Worker std::string system_filename;
3304*795d594fSAndroid Build Coastguard Worker bool has_system = false;
3305*795d594fSAndroid Build Coastguard Worker
3306*795d594fSAndroid Build Coastguard Worker if (FindImageFilename(image_location.c_str(),
3307*795d594fSAndroid Build Coastguard Worker image_isa,
3308*795d594fSAndroid Build Coastguard Worker &system_filename,
3309*795d594fSAndroid Build Coastguard Worker &has_system)) {
3310*795d594fSAndroid Build Coastguard Worker DCHECK(has_system);
3311*795d594fSAndroid Build Coastguard Worker image_header = ReadSpecificImageHeader(system_filename.c_str(), &error_msg);
3312*795d594fSAndroid Build Coastguard Worker }
3313*795d594fSAndroid Build Coastguard Worker
3314*795d594fSAndroid Build Coastguard Worker return image_header != nullptr;
3315*795d594fSAndroid Build Coastguard Worker }
3316*795d594fSAndroid Build Coastguard Worker
LoadBootImage(const std::vector<std::string> & boot_class_path,const std::vector<std::string> & boot_class_path_locations,ArrayRef<File> boot_class_path_files,ArrayRef<File> boot_class_path_image_files,ArrayRef<File> boot_class_path_vdex_files,ArrayRef<File> boot_class_path_odex_files,const std::vector<std::string> & image_locations,const InstructionSet image_isa,bool relocate,bool executable,size_t extra_reservation_size,bool allow_in_memory_compilation,const std::string & apex_versions,std::vector<std::unique_ptr<ImageSpace>> * boot_image_spaces,MemMap * extra_reservation)3317*795d594fSAndroid Build Coastguard Worker bool ImageSpace::LoadBootImage(const std::vector<std::string>& boot_class_path,
3318*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>& boot_class_path_locations,
3319*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_files,
3320*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_image_files,
3321*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_vdex_files,
3322*795d594fSAndroid Build Coastguard Worker ArrayRef<File> boot_class_path_odex_files,
3323*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>& image_locations,
3324*795d594fSAndroid Build Coastguard Worker const InstructionSet image_isa,
3325*795d594fSAndroid Build Coastguard Worker bool relocate,
3326*795d594fSAndroid Build Coastguard Worker bool executable,
3327*795d594fSAndroid Build Coastguard Worker size_t extra_reservation_size,
3328*795d594fSAndroid Build Coastguard Worker bool allow_in_memory_compilation,
3329*795d594fSAndroid Build Coastguard Worker const std::string& apex_versions,
3330*795d594fSAndroid Build Coastguard Worker /*out*/ std::vector<std::unique_ptr<ImageSpace>>* boot_image_spaces,
3331*795d594fSAndroid Build Coastguard Worker /*out*/ MemMap* extra_reservation) {
3332*795d594fSAndroid Build Coastguard Worker ScopedTrace trace(__FUNCTION__);
3333*795d594fSAndroid Build Coastguard Worker
3334*795d594fSAndroid Build Coastguard Worker DCHECK(boot_image_spaces != nullptr);
3335*795d594fSAndroid Build Coastguard Worker DCHECK(boot_image_spaces->empty());
3336*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(extra_reservation_size, kElfSegmentAlignment);
3337*795d594fSAndroid Build Coastguard Worker DCHECK(extra_reservation != nullptr);
3338*795d594fSAndroid Build Coastguard Worker DCHECK_NE(image_isa, InstructionSet::kNone);
3339*795d594fSAndroid Build Coastguard Worker
3340*795d594fSAndroid Build Coastguard Worker if (image_locations.empty()) {
3341*795d594fSAndroid Build Coastguard Worker return false;
3342*795d594fSAndroid Build Coastguard Worker }
3343*795d594fSAndroid Build Coastguard Worker
3344*795d594fSAndroid Build Coastguard Worker BootImageLoader loader(boot_class_path,
3345*795d594fSAndroid Build Coastguard Worker boot_class_path_locations,
3346*795d594fSAndroid Build Coastguard Worker boot_class_path_files,
3347*795d594fSAndroid Build Coastguard Worker boot_class_path_image_files,
3348*795d594fSAndroid Build Coastguard Worker boot_class_path_vdex_files,
3349*795d594fSAndroid Build Coastguard Worker boot_class_path_odex_files,
3350*795d594fSAndroid Build Coastguard Worker image_locations,
3351*795d594fSAndroid Build Coastguard Worker image_isa,
3352*795d594fSAndroid Build Coastguard Worker relocate,
3353*795d594fSAndroid Build Coastguard Worker executable,
3354*795d594fSAndroid Build Coastguard Worker &apex_versions);
3355*795d594fSAndroid Build Coastguard Worker loader.FindImageFiles();
3356*795d594fSAndroid Build Coastguard Worker
3357*795d594fSAndroid Build Coastguard Worker std::string error_msg;
3358*795d594fSAndroid Build Coastguard Worker if (loader.LoadFromSystem(extra_reservation_size,
3359*795d594fSAndroid Build Coastguard Worker allow_in_memory_compilation,
3360*795d594fSAndroid Build Coastguard Worker boot_image_spaces,
3361*795d594fSAndroid Build Coastguard Worker extra_reservation,
3362*795d594fSAndroid Build Coastguard Worker &error_msg)) {
3363*795d594fSAndroid Build Coastguard Worker return true;
3364*795d594fSAndroid Build Coastguard Worker }
3365*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Could not create image space with image file '"
3366*795d594fSAndroid Build Coastguard Worker << Join(image_locations, kComponentSeparator)
3367*795d594fSAndroid Build Coastguard Worker << "'. Attempting to fall back to imageless running. Error was: "
3368*795d594fSAndroid Build Coastguard Worker << error_msg;
3369*795d594fSAndroid Build Coastguard Worker
3370*795d594fSAndroid Build Coastguard Worker return false;
3371*795d594fSAndroid Build Coastguard Worker }
3372*795d594fSAndroid Build Coastguard Worker
~ImageSpace()3373*795d594fSAndroid Build Coastguard Worker ImageSpace::~ImageSpace() {
3374*795d594fSAndroid Build Coastguard Worker // Everything done by member destructors. Classes forward-declared in header are now defined.
3375*795d594fSAndroid Build Coastguard Worker }
3376*795d594fSAndroid Build Coastguard Worker
CreateFromAppImage(const char * image,const OatFile * oat_file,std::string * error_msg)3377*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ImageSpace> ImageSpace::CreateFromAppImage(const char* image,
3378*795d594fSAndroid Build Coastguard Worker const OatFile* oat_file,
3379*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
3380*795d594fSAndroid Build Coastguard Worker // Note: The oat file has already been validated.
3381*795d594fSAndroid Build Coastguard Worker const std::vector<ImageSpace*>& boot_image_spaces =
3382*795d594fSAndroid Build Coastguard Worker Runtime::Current()->GetHeap()->GetBootImageSpaces();
3383*795d594fSAndroid Build Coastguard Worker return CreateFromAppImage(image,
3384*795d594fSAndroid Build Coastguard Worker oat_file,
3385*795d594fSAndroid Build Coastguard Worker ArrayRef<ImageSpace* const>(boot_image_spaces),
3386*795d594fSAndroid Build Coastguard Worker error_msg);
3387*795d594fSAndroid Build Coastguard Worker }
3388*795d594fSAndroid Build Coastguard Worker
CreateFromAppImage(const char * image,const OatFile * oat_file,ArrayRef<ImageSpace * const> boot_image_spaces,std::string * error_msg)3389*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ImageSpace> ImageSpace::CreateFromAppImage(
3390*795d594fSAndroid Build Coastguard Worker const char* image,
3391*795d594fSAndroid Build Coastguard Worker const OatFile* oat_file,
3392*795d594fSAndroid Build Coastguard Worker ArrayRef<ImageSpace* const> boot_image_spaces,
3393*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
3394*795d594fSAndroid Build Coastguard Worker return Loader::InitAppImage(image,
3395*795d594fSAndroid Build Coastguard Worker image,
3396*795d594fSAndroid Build Coastguard Worker oat_file,
3397*795d594fSAndroid Build Coastguard Worker boot_image_spaces,
3398*795d594fSAndroid Build Coastguard Worker error_msg);
3399*795d594fSAndroid Build Coastguard Worker }
3400*795d594fSAndroid Build Coastguard Worker
GetOatFile() const3401*795d594fSAndroid Build Coastguard Worker const OatFile* ImageSpace::GetOatFile() const {
3402*795d594fSAndroid Build Coastguard Worker return oat_file_non_owned_;
3403*795d594fSAndroid Build Coastguard Worker }
3404*795d594fSAndroid Build Coastguard Worker
ReleaseOatFile()3405*795d594fSAndroid Build Coastguard Worker std::unique_ptr<const OatFile> ImageSpace::ReleaseOatFile() {
3406*795d594fSAndroid Build Coastguard Worker CHECK(oat_file_ != nullptr);
3407*795d594fSAndroid Build Coastguard Worker return std::move(oat_file_);
3408*795d594fSAndroid Build Coastguard Worker }
3409*795d594fSAndroid Build Coastguard Worker
Dump(std::ostream & os) const3410*795d594fSAndroid Build Coastguard Worker void ImageSpace::Dump(std::ostream& os) const {
3411*795d594fSAndroid Build Coastguard Worker os << GetType()
3412*795d594fSAndroid Build Coastguard Worker << " begin=" << reinterpret_cast<void*>(Begin())
3413*795d594fSAndroid Build Coastguard Worker << ",end=" << reinterpret_cast<void*>(End())
3414*795d594fSAndroid Build Coastguard Worker << ",size=" << PrettySize(Size())
3415*795d594fSAndroid Build Coastguard Worker << ",name=\"" << GetName() << "\"]";
3416*795d594fSAndroid Build Coastguard Worker }
3417*795d594fSAndroid Build Coastguard Worker
ValidateApexVersions(const OatHeader & oat_header,const std::string & apex_versions,const std::string & file_location,std::string * error_msg)3418*795d594fSAndroid Build Coastguard Worker bool ImageSpace::ValidateApexVersions(const OatHeader& oat_header,
3419*795d594fSAndroid Build Coastguard Worker const std::string& apex_versions,
3420*795d594fSAndroid Build Coastguard Worker const std::string& file_location,
3421*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
3422*795d594fSAndroid Build Coastguard Worker // For a boot image, the key value store only exists in the first OAT file. Skip other OAT files.
3423*795d594fSAndroid Build Coastguard Worker if (oat_header.GetKeyValueStoreSize() == 0) {
3424*795d594fSAndroid Build Coastguard Worker return true;
3425*795d594fSAndroid Build Coastguard Worker }
3426*795d594fSAndroid Build Coastguard Worker
3427*795d594fSAndroid Build Coastguard Worker const char* oat_apex_versions = oat_header.GetStoreValueByKey(OatHeader::kApexVersionsKey);
3428*795d594fSAndroid Build Coastguard Worker if (oat_apex_versions == nullptr) {
3429*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("ValidateApexVersions failed to get APEX versions from oat file '%s'",
3430*795d594fSAndroid Build Coastguard Worker file_location.c_str());
3431*795d594fSAndroid Build Coastguard Worker return false;
3432*795d594fSAndroid Build Coastguard Worker }
3433*795d594fSAndroid Build Coastguard Worker // For a boot image, it can be generated from a subset of the bootclasspath.
3434*795d594fSAndroid Build Coastguard Worker // For an app image, some dex files get compiled with a subset of the bootclasspath.
3435*795d594fSAndroid Build Coastguard Worker // For such cases, the OAT APEX versions will be a prefix of the runtime APEX versions.
3436*795d594fSAndroid Build Coastguard Worker if (!apex_versions.starts_with(oat_apex_versions)) {
3437*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf(
3438*795d594fSAndroid Build Coastguard Worker "ValidateApexVersions found APEX versions mismatch between oat file '%s' and the runtime "
3439*795d594fSAndroid Build Coastguard Worker "(Oat file: '%s', Runtime: '%s')",
3440*795d594fSAndroid Build Coastguard Worker file_location.c_str(),
3441*795d594fSAndroid Build Coastguard Worker oat_apex_versions,
3442*795d594fSAndroid Build Coastguard Worker apex_versions.c_str());
3443*795d594fSAndroid Build Coastguard Worker return false;
3444*795d594fSAndroid Build Coastguard Worker }
3445*795d594fSAndroid Build Coastguard Worker return true;
3446*795d594fSAndroid Build Coastguard Worker }
3447*795d594fSAndroid Build Coastguard Worker
ValidateOatFile(const OatFile & oat_file,std::string * error_msg)3448*795d594fSAndroid Build Coastguard Worker bool ImageSpace::ValidateOatFile(const OatFile& oat_file, std::string* error_msg) {
3449*795d594fSAndroid Build Coastguard Worker DCHECK(Runtime::Current() != nullptr);
3450*795d594fSAndroid Build Coastguard Worker return ValidateOatFile(oat_file, error_msg, {}, {}, Runtime::Current()->GetApexVersions());
3451*795d594fSAndroid Build Coastguard Worker }
3452*795d594fSAndroid Build Coastguard Worker
ValidateOatFile(const OatFile & oat_file,std::string * error_msg,ArrayRef<const std::string> dex_filenames,ArrayRef<File> dex_files,const std::string & apex_versions)3453*795d594fSAndroid Build Coastguard Worker bool ImageSpace::ValidateOatFile(const OatFile& oat_file,
3454*795d594fSAndroid Build Coastguard Worker std::string* error_msg,
3455*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> dex_filenames,
3456*795d594fSAndroid Build Coastguard Worker ArrayRef<File> dex_files,
3457*795d594fSAndroid Build Coastguard Worker const std::string& apex_versions) {
3458*795d594fSAndroid Build Coastguard Worker if (!ValidateApexVersions(oat_file.GetOatHeader(),
3459*795d594fSAndroid Build Coastguard Worker apex_versions,
3460*795d594fSAndroid Build Coastguard Worker oat_file.GetLocation(),
3461*795d594fSAndroid Build Coastguard Worker error_msg)) {
3462*795d594fSAndroid Build Coastguard Worker return false;
3463*795d594fSAndroid Build Coastguard Worker }
3464*795d594fSAndroid Build Coastguard Worker
3465*795d594fSAndroid Build Coastguard Worker // For a boot image, the key value store only exists in the first OAT file. Skip other OAT files.
3466*795d594fSAndroid Build Coastguard Worker if (oat_file.GetOatHeader().GetKeyValueStoreSize() != 0 &&
3467*795d594fSAndroid Build Coastguard Worker oat_file.GetOatHeader().IsConcurrentCopying() != gUseReadBarrier) {
3468*795d594fSAndroid Build Coastguard Worker *error_msg =
3469*795d594fSAndroid Build Coastguard Worker ART_FORMAT("ValidateOatFile found read barrier state mismatch (oat file: {}, runtime: {})",
3470*795d594fSAndroid Build Coastguard Worker oat_file.GetOatHeader().IsConcurrentCopying(),
3471*795d594fSAndroid Build Coastguard Worker gUseReadBarrier);
3472*795d594fSAndroid Build Coastguard Worker return false;
3473*795d594fSAndroid Build Coastguard Worker }
3474*795d594fSAndroid Build Coastguard Worker
3475*795d594fSAndroid Build Coastguard Worker size_t dex_file_index = 0; // Counts only primary dex files.
3476*795d594fSAndroid Build Coastguard Worker const std::vector<const OatDexFile*>& oat_dex_files = oat_file.GetOatDexFiles();
3477*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < oat_dex_files.size();) {
3478*795d594fSAndroid Build Coastguard Worker DCHECK(dex_filenames.empty() || dex_file_index < dex_filenames.size());
3479*795d594fSAndroid Build Coastguard Worker const std::string& dex_file_location = dex_filenames.empty() ?
3480*795d594fSAndroid Build Coastguard Worker oat_dex_files[i]->GetDexFileLocation() :
3481*795d594fSAndroid Build Coastguard Worker dex_filenames[dex_file_index];
3482*795d594fSAndroid Build Coastguard Worker File no_file; // Invalid object.
3483*795d594fSAndroid Build Coastguard Worker File& dex_file = dex_file_index < dex_files.size() ? dex_files[dex_file_index] : no_file;
3484*795d594fSAndroid Build Coastguard Worker dex_file_index++;
3485*795d594fSAndroid Build Coastguard Worker
3486*795d594fSAndroid Build Coastguard Worker if (DexFileLoader::IsMultiDexLocation(oat_dex_files[i]->GetDexFileLocation())) {
3487*795d594fSAndroid Build Coastguard Worker return false; // Expected primary dex file.
3488*795d594fSAndroid Build Coastguard Worker }
3489*795d594fSAndroid Build Coastguard Worker uint32_t oat_checksum = DexFileLoader::GetMultiDexChecksum(oat_dex_files, &i);
3490*795d594fSAndroid Build Coastguard Worker
3491*795d594fSAndroid Build Coastguard Worker // Original checksum.
3492*795d594fSAndroid Build Coastguard Worker std::optional<uint32_t> dex_checksum;
3493*795d594fSAndroid Build Coastguard Worker ArtDexFileLoader dex_loader(&dex_file, dex_file_location);
3494*795d594fSAndroid Build Coastguard Worker bool ok = dex_loader.GetMultiDexChecksum(&dex_checksum, error_msg);
3495*795d594fSAndroid Build Coastguard Worker if (!ok) {
3496*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf(
3497*795d594fSAndroid Build Coastguard Worker "ValidateOatFile failed to get checksum of dex file '%s' "
3498*795d594fSAndroid Build Coastguard Worker "referenced by oat file %s: %s",
3499*795d594fSAndroid Build Coastguard Worker dex_file_location.c_str(),
3500*795d594fSAndroid Build Coastguard Worker oat_file.GetLocation().c_str(),
3501*795d594fSAndroid Build Coastguard Worker error_msg->c_str());
3502*795d594fSAndroid Build Coastguard Worker return false;
3503*795d594fSAndroid Build Coastguard Worker }
3504*795d594fSAndroid Build Coastguard Worker CHECK(dex_checksum.has_value());
3505*795d594fSAndroid Build Coastguard Worker
3506*795d594fSAndroid Build Coastguard Worker if (oat_checksum != dex_checksum) {
3507*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf(
3508*795d594fSAndroid Build Coastguard Worker "ValidateOatFile found checksum mismatch between oat file "
3509*795d594fSAndroid Build Coastguard Worker "'%s' and dex file '%s' (0x%x != 0x%x)",
3510*795d594fSAndroid Build Coastguard Worker oat_file.GetLocation().c_str(),
3511*795d594fSAndroid Build Coastguard Worker dex_file_location.c_str(),
3512*795d594fSAndroid Build Coastguard Worker oat_checksum,
3513*795d594fSAndroid Build Coastguard Worker dex_checksum.value());
3514*795d594fSAndroid Build Coastguard Worker return false;
3515*795d594fSAndroid Build Coastguard Worker }
3516*795d594fSAndroid Build Coastguard Worker }
3517*795d594fSAndroid Build Coastguard Worker return true;
3518*795d594fSAndroid Build Coastguard Worker }
3519*795d594fSAndroid Build Coastguard Worker
GetBootClassPathChecksums(ArrayRef<ImageSpace * const> image_spaces,ArrayRef<const DexFile * const> boot_class_path)3520*795d594fSAndroid Build Coastguard Worker std::string ImageSpace::GetBootClassPathChecksums(
3521*795d594fSAndroid Build Coastguard Worker ArrayRef<ImageSpace* const> image_spaces,
3522*795d594fSAndroid Build Coastguard Worker ArrayRef<const DexFile* const> boot_class_path) {
3523*795d594fSAndroid Build Coastguard Worker DCHECK(!boot_class_path.empty());
3524*795d594fSAndroid Build Coastguard Worker size_t bcp_pos = 0u;
3525*795d594fSAndroid Build Coastguard Worker std::string boot_image_checksum;
3526*795d594fSAndroid Build Coastguard Worker
3527*795d594fSAndroid Build Coastguard Worker for (size_t image_pos = 0u, size = image_spaces.size(); image_pos != size; ) {
3528*795d594fSAndroid Build Coastguard Worker const ImageSpace* main_space = image_spaces[image_pos];
3529*795d594fSAndroid Build Coastguard Worker // Caller must make sure that the image spaces correspond to the head of the BCP.
3530*795d594fSAndroid Build Coastguard Worker DCHECK_NE(main_space->oat_file_non_owned_->GetOatDexFiles().size(), 0u);
3531*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(main_space->oat_file_non_owned_->GetOatDexFiles()[0]->GetDexFileLocation(),
3532*795d594fSAndroid Build Coastguard Worker boot_class_path[bcp_pos]->GetLocation());
3533*795d594fSAndroid Build Coastguard Worker const ImageHeader& current_header = main_space->GetImageHeader();
3534*795d594fSAndroid Build Coastguard Worker uint32_t image_space_count = current_header.GetImageSpaceCount();
3535*795d594fSAndroid Build Coastguard Worker DCHECK_NE(image_space_count, 0u);
3536*795d594fSAndroid Build Coastguard Worker DCHECK_LE(image_space_count, image_spaces.size() - image_pos);
3537*795d594fSAndroid Build Coastguard Worker if (image_pos != 0u) {
3538*795d594fSAndroid Build Coastguard Worker boot_image_checksum += ':';
3539*795d594fSAndroid Build Coastguard Worker }
3540*795d594fSAndroid Build Coastguard Worker uint32_t component_count = current_header.GetComponentCount();
3541*795d594fSAndroid Build Coastguard Worker AppendImageChecksum(component_count, current_header.GetImageChecksum(), &boot_image_checksum);
3542*795d594fSAndroid Build Coastguard Worker for (size_t space_index = 0; space_index != image_space_count; ++space_index) {
3543*795d594fSAndroid Build Coastguard Worker const ImageSpace* space = image_spaces[image_pos + space_index];
3544*795d594fSAndroid Build Coastguard Worker const OatFile* oat_file = space->oat_file_non_owned_;
3545*795d594fSAndroid Build Coastguard Worker size_t num_dex_files = oat_file->GetOatDexFiles().size();
3546*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
3547*795d594fSAndroid Build Coastguard Worker CHECK_NE(num_dex_files, 0u);
3548*795d594fSAndroid Build Coastguard Worker CHECK_LE(oat_file->GetOatDexFiles().size(), boot_class_path.size() - bcp_pos);
3549*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i != num_dex_files; ++i) {
3550*795d594fSAndroid Build Coastguard Worker CHECK_EQ(oat_file->GetOatDexFiles()[i]->GetDexFileLocation(),
3551*795d594fSAndroid Build Coastguard Worker boot_class_path[bcp_pos + i]->GetLocation());
3552*795d594fSAndroid Build Coastguard Worker }
3553*795d594fSAndroid Build Coastguard Worker }
3554*795d594fSAndroid Build Coastguard Worker bcp_pos += num_dex_files;
3555*795d594fSAndroid Build Coastguard Worker }
3556*795d594fSAndroid Build Coastguard Worker image_pos += image_space_count;
3557*795d594fSAndroid Build Coastguard Worker }
3558*795d594fSAndroid Build Coastguard Worker
3559*795d594fSAndroid Build Coastguard Worker ArrayRef<const DexFile* const> boot_class_path_tail =
3560*795d594fSAndroid Build Coastguard Worker ArrayRef<const DexFile* const>(boot_class_path).SubArray(bcp_pos);
3561*795d594fSAndroid Build Coastguard Worker DCHECK(boot_class_path_tail.empty() ||
3562*795d594fSAndroid Build Coastguard Worker !DexFileLoader::IsMultiDexLocation(boot_class_path_tail.front()->GetLocation()));
3563*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < boot_class_path_tail.size();) {
3564*795d594fSAndroid Build Coastguard Worker uint32_t checksum = DexFileLoader::GetMultiDexChecksum(boot_class_path_tail, &i);
3565*795d594fSAndroid Build Coastguard Worker if (!boot_image_checksum.empty()) {
3566*795d594fSAndroid Build Coastguard Worker boot_image_checksum += ':';
3567*795d594fSAndroid Build Coastguard Worker }
3568*795d594fSAndroid Build Coastguard Worker boot_image_checksum += kDexFileChecksumPrefix;
3569*795d594fSAndroid Build Coastguard Worker StringAppendF(&boot_image_checksum, "/%08x", checksum);
3570*795d594fSAndroid Build Coastguard Worker }
3571*795d594fSAndroid Build Coastguard Worker return boot_image_checksum;
3572*795d594fSAndroid Build Coastguard Worker }
3573*795d594fSAndroid Build Coastguard Worker
GetNumberOfComponents(ArrayRef<ImageSpace * const> image_spaces)3574*795d594fSAndroid Build Coastguard Worker size_t ImageSpace::GetNumberOfComponents(ArrayRef<ImageSpace* const> image_spaces) {
3575*795d594fSAndroid Build Coastguard Worker size_t n = 0;
3576*795d594fSAndroid Build Coastguard Worker for (auto&& is : image_spaces) {
3577*795d594fSAndroid Build Coastguard Worker n += is->GetComponentCount();
3578*795d594fSAndroid Build Coastguard Worker }
3579*795d594fSAndroid Build Coastguard Worker return n;
3580*795d594fSAndroid Build Coastguard Worker }
3581*795d594fSAndroid Build Coastguard Worker
CheckAndCountBCPComponents(std::string_view oat_boot_class_path,ArrayRef<const std::string> boot_class_path,std::string * error_msg)3582*795d594fSAndroid Build Coastguard Worker size_t ImageSpace::CheckAndCountBCPComponents(std::string_view oat_boot_class_path,
3583*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> boot_class_path,
3584*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
3585*795d594fSAndroid Build Coastguard Worker // Check that the oat BCP is a prefix of current BCP locations and count components.
3586*795d594fSAndroid Build Coastguard Worker size_t component_count = 0u;
3587*795d594fSAndroid Build Coastguard Worker std::string_view remaining_bcp(oat_boot_class_path);
3588*795d594fSAndroid Build Coastguard Worker bool bcp_ok = false;
3589*795d594fSAndroid Build Coastguard Worker for (const std::string& location : boot_class_path) {
3590*795d594fSAndroid Build Coastguard Worker if (!remaining_bcp.starts_with(location)) {
3591*795d594fSAndroid Build Coastguard Worker break;
3592*795d594fSAndroid Build Coastguard Worker }
3593*795d594fSAndroid Build Coastguard Worker remaining_bcp.remove_prefix(location.size());
3594*795d594fSAndroid Build Coastguard Worker ++component_count;
3595*795d594fSAndroid Build Coastguard Worker if (remaining_bcp.empty()) {
3596*795d594fSAndroid Build Coastguard Worker bcp_ok = true;
3597*795d594fSAndroid Build Coastguard Worker break;
3598*795d594fSAndroid Build Coastguard Worker }
3599*795d594fSAndroid Build Coastguard Worker if (!remaining_bcp.starts_with(":")) {
3600*795d594fSAndroid Build Coastguard Worker break;
3601*795d594fSAndroid Build Coastguard Worker }
3602*795d594fSAndroid Build Coastguard Worker remaining_bcp.remove_prefix(1u);
3603*795d594fSAndroid Build Coastguard Worker }
3604*795d594fSAndroid Build Coastguard Worker if (!bcp_ok) {
3605*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Oat boot class path (%s) is not a prefix of"
3606*795d594fSAndroid Build Coastguard Worker " runtime boot class path (%s)",
3607*795d594fSAndroid Build Coastguard Worker std::string(oat_boot_class_path).c_str(),
3608*795d594fSAndroid Build Coastguard Worker Join(boot_class_path, ':').c_str());
3609*795d594fSAndroid Build Coastguard Worker return static_cast<size_t>(-1);
3610*795d594fSAndroid Build Coastguard Worker }
3611*795d594fSAndroid Build Coastguard Worker return component_count;
3612*795d594fSAndroid Build Coastguard Worker }
3613*795d594fSAndroid Build Coastguard Worker
VerifyBootClassPathChecksums(std::string_view oat_checksums,std::string_view oat_boot_class_path,ArrayRef<const std::unique_ptr<ImageSpace>> image_spaces,ArrayRef<const std::string> boot_class_path_locations,ArrayRef<const std::string> boot_class_path,std::string * error_msg)3614*795d594fSAndroid Build Coastguard Worker bool ImageSpace::VerifyBootClassPathChecksums(
3615*795d594fSAndroid Build Coastguard Worker std::string_view oat_checksums,
3616*795d594fSAndroid Build Coastguard Worker std::string_view oat_boot_class_path,
3617*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::unique_ptr<ImageSpace>> image_spaces,
3618*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> boot_class_path_locations,
3619*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> boot_class_path,
3620*795d594fSAndroid Build Coastguard Worker /*out*/std::string* error_msg) {
3621*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(boot_class_path.size(), boot_class_path_locations.size());
3622*795d594fSAndroid Build Coastguard Worker DCHECK_GE(boot_class_path_locations.size(), image_spaces.size());
3623*795d594fSAndroid Build Coastguard Worker if (oat_checksums.empty() || oat_boot_class_path.empty()) {
3624*795d594fSAndroid Build Coastguard Worker *error_msg = oat_checksums.empty() ? "Empty checksums." : "Empty boot class path.";
3625*795d594fSAndroid Build Coastguard Worker return false;
3626*795d594fSAndroid Build Coastguard Worker }
3627*795d594fSAndroid Build Coastguard Worker
3628*795d594fSAndroid Build Coastguard Worker size_t oat_bcp_size =
3629*795d594fSAndroid Build Coastguard Worker CheckAndCountBCPComponents(oat_boot_class_path, boot_class_path_locations, error_msg);
3630*795d594fSAndroid Build Coastguard Worker if (oat_bcp_size == static_cast<size_t>(-1)) {
3631*795d594fSAndroid Build Coastguard Worker DCHECK(!error_msg->empty());
3632*795d594fSAndroid Build Coastguard Worker return false;
3633*795d594fSAndroid Build Coastguard Worker }
3634*795d594fSAndroid Build Coastguard Worker const size_t num_image_spaces = image_spaces.size();
3635*795d594fSAndroid Build Coastguard Worker size_t dependency_component_count = 0;
3636*795d594fSAndroid Build Coastguard Worker for (const std::unique_ptr<ImageSpace>& space : image_spaces) {
3637*795d594fSAndroid Build Coastguard Worker dependency_component_count += space->GetComponentCount();
3638*795d594fSAndroid Build Coastguard Worker }
3639*795d594fSAndroid Build Coastguard Worker if (dependency_component_count != oat_bcp_size) {
3640*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Image header records %s dependencies (%zu) than BCP (%zu)",
3641*795d594fSAndroid Build Coastguard Worker dependency_component_count < oat_bcp_size ? "less" : "more",
3642*795d594fSAndroid Build Coastguard Worker dependency_component_count,
3643*795d594fSAndroid Build Coastguard Worker oat_bcp_size);
3644*795d594fSAndroid Build Coastguard Worker return false;
3645*795d594fSAndroid Build Coastguard Worker }
3646*795d594fSAndroid Build Coastguard Worker
3647*795d594fSAndroid Build Coastguard Worker // Verify image checksums.
3648*795d594fSAndroid Build Coastguard Worker size_t bcp_pos = 0u;
3649*795d594fSAndroid Build Coastguard Worker size_t image_pos = 0u;
3650*795d594fSAndroid Build Coastguard Worker while (image_pos != num_image_spaces && oat_checksums.starts_with("i")) {
3651*795d594fSAndroid Build Coastguard Worker // Verify the current image checksum.
3652*795d594fSAndroid Build Coastguard Worker const ImageHeader& current_header = image_spaces[image_pos]->GetImageHeader();
3653*795d594fSAndroid Build Coastguard Worker uint32_t image_space_count = current_header.GetImageSpaceCount();
3654*795d594fSAndroid Build Coastguard Worker DCHECK_NE(image_space_count, 0u);
3655*795d594fSAndroid Build Coastguard Worker DCHECK_LE(image_space_count, image_spaces.size() - image_pos);
3656*795d594fSAndroid Build Coastguard Worker uint32_t component_count = current_header.GetComponentCount();
3657*795d594fSAndroid Build Coastguard Worker uint32_t checksum = current_header.GetImageChecksum();
3658*795d594fSAndroid Build Coastguard Worker if (!CheckAndRemoveImageChecksum(component_count, checksum, &oat_checksums, error_msg)) {
3659*795d594fSAndroid Build Coastguard Worker DCHECK(!error_msg->empty());
3660*795d594fSAndroid Build Coastguard Worker return false;
3661*795d594fSAndroid Build Coastguard Worker }
3662*795d594fSAndroid Build Coastguard Worker
3663*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
3664*795d594fSAndroid Build Coastguard Worker for (size_t space_index = 0; space_index != image_space_count; ++space_index) {
3665*795d594fSAndroid Build Coastguard Worker const OatFile* oat_file = image_spaces[image_pos + space_index]->oat_file_non_owned_;
3666*795d594fSAndroid Build Coastguard Worker size_t num_dex_files = oat_file->GetOatDexFiles().size();
3667*795d594fSAndroid Build Coastguard Worker CHECK_NE(num_dex_files, 0u);
3668*795d594fSAndroid Build Coastguard Worker const std::string main_location = oat_file->GetOatDexFiles()[0]->GetDexFileLocation();
3669*795d594fSAndroid Build Coastguard Worker CHECK_EQ(main_location, boot_class_path_locations[bcp_pos + space_index]);
3670*795d594fSAndroid Build Coastguard Worker CHECK(!DexFileLoader::IsMultiDexLocation(main_location));
3671*795d594fSAndroid Build Coastguard Worker size_t num_base_locations = 1u;
3672*795d594fSAndroid Build Coastguard Worker for (size_t i = 1u; i != num_dex_files; ++i) {
3673*795d594fSAndroid Build Coastguard Worker if (!DexFileLoader::IsMultiDexLocation(
3674*795d594fSAndroid Build Coastguard Worker oat_file->GetOatDexFiles()[i]->GetDexFileLocation())) {
3675*795d594fSAndroid Build Coastguard Worker CHECK_EQ(image_space_count, 1u); // We can find base locations only for --single-image.
3676*795d594fSAndroid Build Coastguard Worker ++num_base_locations;
3677*795d594fSAndroid Build Coastguard Worker }
3678*795d594fSAndroid Build Coastguard Worker }
3679*795d594fSAndroid Build Coastguard Worker if (image_space_count == 1u) {
3680*795d594fSAndroid Build Coastguard Worker CHECK_EQ(num_base_locations, component_count);
3681*795d594fSAndroid Build Coastguard Worker }
3682*795d594fSAndroid Build Coastguard Worker }
3683*795d594fSAndroid Build Coastguard Worker }
3684*795d594fSAndroid Build Coastguard Worker
3685*795d594fSAndroid Build Coastguard Worker image_pos += image_space_count;
3686*795d594fSAndroid Build Coastguard Worker bcp_pos += component_count;
3687*795d594fSAndroid Build Coastguard Worker
3688*795d594fSAndroid Build Coastguard Worker if (!oat_checksums.starts_with(":")) {
3689*795d594fSAndroid Build Coastguard Worker // Check that we've reached the end of checksums and BCP.
3690*795d594fSAndroid Build Coastguard Worker if (!oat_checksums.empty()) {
3691*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Expected ':' separator or end of checksums, remaining %s.",
3692*795d594fSAndroid Build Coastguard Worker std::string(oat_checksums).c_str());
3693*795d594fSAndroid Build Coastguard Worker return false;
3694*795d594fSAndroid Build Coastguard Worker }
3695*795d594fSAndroid Build Coastguard Worker if (bcp_pos != oat_bcp_size) {
3696*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Component count mismatch between checksums (%zu) and BCP (%zu)",
3697*795d594fSAndroid Build Coastguard Worker bcp_pos,
3698*795d594fSAndroid Build Coastguard Worker oat_bcp_size);
3699*795d594fSAndroid Build Coastguard Worker return false;
3700*795d594fSAndroid Build Coastguard Worker }
3701*795d594fSAndroid Build Coastguard Worker return true;
3702*795d594fSAndroid Build Coastguard Worker }
3703*795d594fSAndroid Build Coastguard Worker oat_checksums.remove_prefix(1u);
3704*795d594fSAndroid Build Coastguard Worker }
3705*795d594fSAndroid Build Coastguard Worker
3706*795d594fSAndroid Build Coastguard Worker // We do not allow dependencies of extensions on dex files. That would require
3707*795d594fSAndroid Build Coastguard Worker // interleaving the loading of the images with opening the other BCP dex files.
3708*795d594fSAndroid Build Coastguard Worker return false;
3709*795d594fSAndroid Build Coastguard Worker }
3710*795d594fSAndroid Build Coastguard Worker
ExpandMultiImageLocations(ArrayRef<const std::string> dex_locations,const std::string & image_location,bool boot_image_extension)3711*795d594fSAndroid Build Coastguard Worker std::vector<std::string> ImageSpace::ExpandMultiImageLocations(
3712*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string> dex_locations,
3713*795d594fSAndroid Build Coastguard Worker const std::string& image_location,
3714*795d594fSAndroid Build Coastguard Worker bool boot_image_extension) {
3715*795d594fSAndroid Build Coastguard Worker DCHECK(!dex_locations.empty());
3716*795d594fSAndroid Build Coastguard Worker
3717*795d594fSAndroid Build Coastguard Worker // Find the path.
3718*795d594fSAndroid Build Coastguard Worker size_t last_slash = image_location.rfind('/');
3719*795d594fSAndroid Build Coastguard Worker CHECK_NE(last_slash, std::string::npos);
3720*795d594fSAndroid Build Coastguard Worker
3721*795d594fSAndroid Build Coastguard Worker // We also need to honor path components that were encoded through '@'. Otherwise the loading
3722*795d594fSAndroid Build Coastguard Worker // code won't be able to find the images.
3723*795d594fSAndroid Build Coastguard Worker if (image_location.find('@', last_slash) != std::string::npos) {
3724*795d594fSAndroid Build Coastguard Worker last_slash = image_location.rfind('@');
3725*795d594fSAndroid Build Coastguard Worker }
3726*795d594fSAndroid Build Coastguard Worker
3727*795d594fSAndroid Build Coastguard Worker // Find the dot separating the primary image name from the extension.
3728*795d594fSAndroid Build Coastguard Worker size_t last_dot = image_location.rfind('.');
3729*795d594fSAndroid Build Coastguard Worker // Extract the extension and base (the path and primary image name).
3730*795d594fSAndroid Build Coastguard Worker std::string extension;
3731*795d594fSAndroid Build Coastguard Worker std::string base = image_location;
3732*795d594fSAndroid Build Coastguard Worker if (last_dot != std::string::npos && last_dot > last_slash) {
3733*795d594fSAndroid Build Coastguard Worker extension = image_location.substr(last_dot); // Including the dot.
3734*795d594fSAndroid Build Coastguard Worker base.resize(last_dot);
3735*795d594fSAndroid Build Coastguard Worker }
3736*795d594fSAndroid Build Coastguard Worker // For non-empty primary image name, add '-' to the `base`.
3737*795d594fSAndroid Build Coastguard Worker if (last_slash + 1u != base.size()) {
3738*795d594fSAndroid Build Coastguard Worker base += '-';
3739*795d594fSAndroid Build Coastguard Worker }
3740*795d594fSAndroid Build Coastguard Worker
3741*795d594fSAndroid Build Coastguard Worker std::vector<std::string> locations;
3742*795d594fSAndroid Build Coastguard Worker locations.reserve(dex_locations.size());
3743*795d594fSAndroid Build Coastguard Worker size_t start_index = 0u;
3744*795d594fSAndroid Build Coastguard Worker if (!boot_image_extension) {
3745*795d594fSAndroid Build Coastguard Worker start_index = 1u;
3746*795d594fSAndroid Build Coastguard Worker locations.push_back(image_location);
3747*795d594fSAndroid Build Coastguard Worker }
3748*795d594fSAndroid Build Coastguard Worker
3749*795d594fSAndroid Build Coastguard Worker // Now create the other names. Use a counted loop to skip the first one if needed.
3750*795d594fSAndroid Build Coastguard Worker for (size_t i = start_index; i < dex_locations.size(); ++i) {
3751*795d594fSAndroid Build Coastguard Worker // Replace path with `base` (i.e. image path and prefix) and replace the original
3752*795d594fSAndroid Build Coastguard Worker // extension (if any) with `extension`.
3753*795d594fSAndroid Build Coastguard Worker std::string name = dex_locations[i];
3754*795d594fSAndroid Build Coastguard Worker size_t last_dex_slash = name.rfind('/');
3755*795d594fSAndroid Build Coastguard Worker if (last_dex_slash != std::string::npos) {
3756*795d594fSAndroid Build Coastguard Worker name = name.substr(last_dex_slash + 1);
3757*795d594fSAndroid Build Coastguard Worker }
3758*795d594fSAndroid Build Coastguard Worker size_t last_dex_dot = name.rfind('.');
3759*795d594fSAndroid Build Coastguard Worker if (last_dex_dot != std::string::npos) {
3760*795d594fSAndroid Build Coastguard Worker name.resize(last_dex_dot);
3761*795d594fSAndroid Build Coastguard Worker }
3762*795d594fSAndroid Build Coastguard Worker locations.push_back(ART_FORMAT("{}{}{}", base, name, extension));
3763*795d594fSAndroid Build Coastguard Worker }
3764*795d594fSAndroid Build Coastguard Worker return locations;
3765*795d594fSAndroid Build Coastguard Worker }
3766*795d594fSAndroid Build Coastguard Worker
DumpSections(std::ostream & os) const3767*795d594fSAndroid Build Coastguard Worker void ImageSpace::DumpSections(std::ostream& os) const {
3768*795d594fSAndroid Build Coastguard Worker const uint8_t* base = Begin();
3769*795d594fSAndroid Build Coastguard Worker const ImageHeader& header = GetImageHeader();
3770*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
3771*795d594fSAndroid Build Coastguard Worker auto section_type = static_cast<ImageHeader::ImageSections>(i);
3772*795d594fSAndroid Build Coastguard Worker const ImageSection& section = header.GetImageSection(section_type);
3773*795d594fSAndroid Build Coastguard Worker os << section_type << " " << reinterpret_cast<const void*>(base + section.Offset())
3774*795d594fSAndroid Build Coastguard Worker << "-" << reinterpret_cast<const void*>(base + section.End()) << "\n";
3775*795d594fSAndroid Build Coastguard Worker }
3776*795d594fSAndroid Build Coastguard Worker }
3777*795d594fSAndroid Build Coastguard Worker
ReleaseMetadata()3778*795d594fSAndroid Build Coastguard Worker void ImageSpace::ReleaseMetadata() {
3779*795d594fSAndroid Build Coastguard Worker const ImageSection& metadata = GetImageHeader().GetMetadataSection();
3780*795d594fSAndroid Build Coastguard Worker VLOG(image) << "Releasing " << metadata.Size() << " image metadata bytes";
3781*795d594fSAndroid Build Coastguard Worker // Avoid using ZeroAndReleasePages since the zero fill might not be word atomic.
3782*795d594fSAndroid Build Coastguard Worker uint8_t* const page_begin = AlignUp(Begin() + metadata.Offset(), gPageSize);
3783*795d594fSAndroid Build Coastguard Worker uint8_t* const page_end = AlignDown(Begin() + metadata.End(), gPageSize);
3784*795d594fSAndroid Build Coastguard Worker if (page_begin < page_end) {
3785*795d594fSAndroid Build Coastguard Worker CHECK_NE(madvise(page_begin, page_end - page_begin, MADV_DONTNEED), -1) << "madvise failed";
3786*795d594fSAndroid Build Coastguard Worker }
3787*795d594fSAndroid Build Coastguard Worker }
3788*795d594fSAndroid Build Coastguard Worker
3789*795d594fSAndroid Build Coastguard Worker } // namespace space
3790*795d594fSAndroid Build Coastguard Worker } // namespace gc
3791*795d594fSAndroid Build Coastguard Worker } // namespace art
3792