1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "image.h"
18
19 #include <lz4.h>
20 #include <sstream>
21
22 #include "base/bit_utils.h"
23 #include "base/length_prefixed_array.h"
24 #include "base/utils.h"
25 #include "mirror/object-inl.h"
26 #include "mirror/object_array-inl.h"
27 #include "mirror/object_array.h"
28
29 namespace art {
30
31 const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' };
32 const uint8_t ImageHeader::kImageVersion[] = { '0', '8', '5', '\0' }; // Single-image.
33
ImageHeader(uint32_t image_reservation_size,uint32_t component_count,uint32_t image_begin,uint32_t image_size,ImageSection * sections,uint32_t image_roots,uint32_t oat_checksum,uint32_t oat_file_begin,uint32_t oat_data_begin,uint32_t oat_data_end,uint32_t oat_file_end,uint32_t boot_image_begin,uint32_t boot_image_size,uint32_t boot_image_component_count,uint32_t boot_image_checksum,uint32_t pointer_size)34 ImageHeader::ImageHeader(uint32_t image_reservation_size,
35 uint32_t component_count,
36 uint32_t image_begin,
37 uint32_t image_size,
38 ImageSection* sections,
39 uint32_t image_roots,
40 uint32_t oat_checksum,
41 uint32_t oat_file_begin,
42 uint32_t oat_data_begin,
43 uint32_t oat_data_end,
44 uint32_t oat_file_end,
45 uint32_t boot_image_begin,
46 uint32_t boot_image_size,
47 uint32_t boot_image_component_count,
48 uint32_t boot_image_checksum,
49 uint32_t pointer_size)
50 : image_reservation_size_(image_reservation_size),
51 component_count_(component_count),
52 image_begin_(image_begin),
53 image_size_(image_size),
54 image_checksum_(0u),
55 oat_checksum_(oat_checksum),
56 oat_file_begin_(oat_file_begin),
57 oat_data_begin_(oat_data_begin),
58 oat_data_end_(oat_data_end),
59 oat_file_end_(oat_file_end),
60 boot_image_begin_(boot_image_begin),
61 boot_image_size_(boot_image_size),
62 boot_image_component_count_(boot_image_component_count),
63 boot_image_checksum_(boot_image_checksum),
64 image_roots_(image_roots),
65 pointer_size_(pointer_size) {
66 CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize));
67 CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize));
68 CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize));
69 CHECK_LT(image_roots, oat_file_begin);
70 CHECK_LE(oat_file_begin, oat_data_begin);
71 CHECK_LT(oat_data_begin, oat_data_end);
72 CHECK_LE(oat_data_end, oat_file_end);
73 CHECK(ValidPointerSize(pointer_size_)) << pointer_size_;
74 memcpy(magic_, kImageMagic, sizeof(kImageMagic));
75 memcpy(version_, kImageVersion, sizeof(kImageVersion));
76 std::copy_n(sections, kSectionCount, sections_);
77 }
78
RelocateImageReferences(int64_t delta)79 void ImageHeader::RelocateImageReferences(int64_t delta) {
80 CHECK_ALIGNED(delta, kPageSize) << "relocation delta must be page aligned";
81 oat_file_begin_ += delta;
82 oat_data_begin_ += delta;
83 oat_data_end_ += delta;
84 oat_file_end_ += delta;
85 image_begin_ += delta;
86 image_roots_ += delta;
87 }
88
RelocateBootImageReferences(int64_t delta)89 void ImageHeader::RelocateBootImageReferences(int64_t delta) {
90 CHECK_ALIGNED(delta, kPageSize) << "relocation delta must be page aligned";
91 DCHECK_EQ(boot_image_begin_ != 0u, boot_image_size_ != 0u);
92 if (boot_image_begin_ != 0u) {
93 boot_image_begin_ += delta;
94 }
95 for (size_t i = 0; i < kImageMethodsCount; ++i) {
96 image_methods_[i] += delta;
97 }
98 }
99
IsAppImage() const100 bool ImageHeader::IsAppImage() const {
101 // Unlike boot image and boot image extensions which include address space for
102 // oat files in their reservation size, app images are loaded separately from oat
103 // files and their reservation size is the image size rounded up to full page.
104 return image_reservation_size_ == RoundUp(image_size_, kPageSize);
105 }
106
GetImageSpaceCount() const107 uint32_t ImageHeader::GetImageSpaceCount() const {
108 DCHECK(!IsAppImage());
109 DCHECK_NE(component_count_, 0u); // Must be the header for the first component.
110 // For images compiled with --single-image, there is only one oat file. To detect
111 // that, check whether the reservation ends at the end of the first oat file.
112 return (image_begin_ + image_reservation_size_ == oat_file_end_) ? 1u : component_count_;
113 }
114
IsValid() const115 bool ImageHeader::IsValid() const {
116 if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) {
117 return false;
118 }
119 if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) {
120 return false;
121 }
122 if (!IsAligned<kPageSize>(image_reservation_size_)) {
123 return false;
124 }
125 // Unsigned so wraparound is well defined.
126 if (image_begin_ >= image_begin_ + image_size_) {
127 return false;
128 }
129 if (oat_file_begin_ > oat_file_end_) {
130 return false;
131 }
132 if (oat_data_begin_ > oat_data_end_) {
133 return false;
134 }
135 if (oat_file_begin_ >= oat_data_begin_) {
136 return false;
137 }
138 return true;
139 }
140
GetMagic() const141 const char* ImageHeader::GetMagic() const {
142 CHECK(IsValid());
143 return reinterpret_cast<const char*>(magic_);
144 }
145
GetImageMethod(ImageMethod index) const146 ArtMethod* ImageHeader::GetImageMethod(ImageMethod index) const {
147 CHECK_LT(static_cast<size_t>(index), kImageMethodsCount);
148 return reinterpret_cast<ArtMethod*>(image_methods_[index]);
149 }
150
operator <<(std::ostream & os,const ImageSection & section)151 std::ostream& operator<<(std::ostream& os, const ImageSection& section) {
152 return os << "size=" << section.Size() << " range=" << section.Offset() << "-" << section.End();
153 }
154
VisitObjects(ObjectVisitor * visitor,uint8_t * base,PointerSize pointer_size) const155 void ImageHeader::VisitObjects(ObjectVisitor* visitor,
156 uint8_t* base,
157 PointerSize pointer_size) const {
158 DCHECK_EQ(pointer_size, GetPointerSize());
159 const ImageSection& objects = GetObjectsSection();
160 static const size_t kStartPos = RoundUp(sizeof(ImageHeader), kObjectAlignment);
161 for (size_t pos = kStartPos; pos < objects.Size(); ) {
162 mirror::Object* object = reinterpret_cast<mirror::Object*>(base + objects.Offset() + pos);
163 visitor->Visit(object);
164 pos += RoundUp(object->SizeOf(), kObjectAlignment);
165 }
166 }
167
GetPointerSize() const168 PointerSize ImageHeader::GetPointerSize() const {
169 return ConvertToPointerSize(pointer_size_);
170 }
171
Decompress(uint8_t * out_ptr,const uint8_t * in_ptr,std::string * error_msg) const172 bool ImageHeader::Block::Decompress(uint8_t* out_ptr,
173 const uint8_t* in_ptr,
174 std::string* error_msg) const {
175 switch (storage_mode_) {
176 case kStorageModeUncompressed: {
177 CHECK_EQ(image_size_, data_size_);
178 memcpy(out_ptr + image_offset_, in_ptr + data_offset_, data_size_);
179 break;
180 }
181 case kStorageModeLZ4:
182 case kStorageModeLZ4HC: {
183 // LZ4HC and LZ4 have same internal format, both use LZ4_decompress.
184 const size_t decompressed_size = LZ4_decompress_safe(
185 reinterpret_cast<const char*>(in_ptr) + data_offset_,
186 reinterpret_cast<char*>(out_ptr) + image_offset_,
187 data_size_,
188 image_size_);
189 CHECK_EQ(decompressed_size, image_size_);
190 break;
191 }
192 default: {
193 if (error_msg != nullptr) {
194 *error_msg = (std::ostringstream() << "Invalid image format " << storage_mode_).str();
195 }
196 return false;
197 }
198 }
199 return true;
200 }
201
202 } // namespace art
203