blob: 08b81c11c9c23f6a79747cea99b9eb01f201958d [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070016
17#include "image.h"
18
Mathieu Chartier1a842962018-11-13 15:09:51 -080019#include <lz4.h>
20#include <sstream>
21
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080023#include "base/length_prefixed_array.h"
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/utils.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "mirror/object_array-inl.h"
27#include "mirror/object_array.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070029namespace art {
30
Ian Rogers13735952014-10-08 12:43:28 -070031const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' };
Vladimir Marko7f958e32019-10-24 09:03:58 +000032const uint8_t ImageHeader::kImageVersion[] = { '0', '7', '9', '\0' }; // FP16ToHalf intrinsic
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070033
Vladimir Marko7391c8c2018-11-21 17:58:44 +000034ImageHeader::ImageHeader(uint32_t image_reservation_size,
35 uint32_t component_count,
36 uint32_t image_begin,
Mathieu Chartier31e89252013-08-28 11:29:12 -070037 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -070038 ImageSection* sections,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039 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,
Igor Murashkin46774762014-10-22 11:37:02 -070044 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -080045 uint32_t boot_image_begin,
46 uint32_t boot_image_size,
Mathieu Chartier1a842962018-11-13 15:09:51 -080047 uint32_t pointer_size)
Vladimir Marko7391c8c2018-11-21 17:58:44 +000048 : image_reservation_size_(image_reservation_size),
49 component_count_(component_count),
50 image_begin_(image_begin),
Mathieu Chartier31e89252013-08-28 11:29:12 -070051 image_size_(image_size),
Vladimir Markoc10a0c62018-11-16 11:39:22 +000052 image_checksum_(0u),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053 oat_checksum_(oat_checksum),
54 oat_file_begin_(oat_file_begin),
55 oat_data_begin_(oat_data_begin),
56 oat_data_end_(oat_data_end),
57 oat_file_end_(oat_file_end),
Mathieu Chartierfbc31082016-01-24 11:59:56 -080058 boot_image_begin_(boot_image_begin),
59 boot_image_size_(boot_image_size),
Igor Murashkin46774762014-10-22 11:37:02 -070060 image_roots_(image_roots),
Mathieu Chartier1a842962018-11-13 15:09:51 -080061 pointer_size_(pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize));
63 CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize));
64 CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065 CHECK_LT(image_roots, oat_file_begin);
66 CHECK_LE(oat_file_begin, oat_data_begin);
67 CHECK_LT(oat_data_begin, oat_data_end);
68 CHECK_LE(oat_data_end, oat_file_end);
Mathieu Chartiere401d142015-04-22 13:56:20 -070069 CHECK(ValidPointerSize(pointer_size_)) << pointer_size_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 memcpy(magic_, kImageMagic, sizeof(kImageMagic));
71 memcpy(version_, kImageVersion, sizeof(kImageVersion));
Mathieu Chartiere401d142015-04-22 13:56:20 -070072 std::copy_n(sections, kSectionCount, sections_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073}
74
Vladimir Markoc0b30c92019-07-23 14:58:25 +010075void ImageHeader::RelocateImageReferences(int64_t delta) {
76 CHECK_ALIGNED(delta, kPageSize) << "relocation delta must be page aligned";
Alex Light53cb16b2014-06-12 11:26:29 -070077 oat_file_begin_ += delta;
78 oat_data_begin_ += delta;
79 oat_data_end_ += delta;
80 oat_file_end_ += delta;
Mathieu Chartierfbc31082016-01-24 11:59:56 -080081 image_begin_ += delta;
82 image_roots_ += delta;
83}
84
Vladimir Markoc0b30c92019-07-23 14:58:25 +010085void ImageHeader::RelocateBootImageReferences(int64_t delta) {
86 CHECK_ALIGNED(delta, kPageSize) << "relocation delta must be page aligned";
87 DCHECK_EQ(boot_image_begin_ != 0u, boot_image_size_ != 0u);
88 if (boot_image_begin_ != 0u) {
89 boot_image_begin_ += delta;
90 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070091 for (size_t i = 0; i < kImageMethodsCount; ++i) {
92 image_methods_[i] += delta;
93 }
Alex Light53cb16b2014-06-12 11:26:29 -070094}
95
Vladimir Marko21910692019-11-06 13:27:03 +000096bool ImageHeader::IsAppImage() const {
97 // Unlike boot image and boot image extensions which include address space for
98 // oat files in their reservation size, app images are loaded separately from oat
99 // files and their reservation size is the image size rounded up to full page.
100 return image_reservation_size_ == RoundUp(image_size_, kPageSize);
101}
102
Brian Carlstrom68708f52013-09-03 14:15:31 -0700103bool ImageHeader::IsValid() const {
104 if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) {
105 return false;
106 }
107 if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) {
108 return false;
109 }
Vladimir Marko7391c8c2018-11-21 17:58:44 +0000110 if (!IsAligned<kPageSize>(image_reservation_size_)) {
111 return false;
112 }
Alex Light53cb16b2014-06-12 11:26:29 -0700113 // Unsigned so wraparound is well defined.
114 if (image_begin_ >= image_begin_ + image_size_) {
115 return false;
116 }
117 if (oat_file_begin_ > oat_file_end_) {
118 return false;
119 }
120 if (oat_data_begin_ > oat_data_end_) {
121 return false;
122 }
123 if (oat_file_begin_ >= oat_data_begin_) {
124 return false;
125 }
Brian Carlstrom68708f52013-09-03 14:15:31 -0700126 return true;
127}
128
129const char* ImageHeader::GetMagic() const {
130 CHECK(IsValid());
131 return reinterpret_cast<const char*>(magic_);
132}
133
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134ArtMethod* ImageHeader::GetImageMethod(ImageMethod index) const {
135 CHECK_LT(static_cast<size_t>(index), kImageMethodsCount);
136 return reinterpret_cast<ArtMethod*>(image_methods_[index]);
137}
138
Mathieu Chartiere401d142015-04-22 13:56:20 -0700139std::ostream& operator<<(std::ostream& os, const ImageSection& section) {
140 return os << "size=" << section.Size() << " range=" << section.Offset() << "-" << section.End();
141}
142
David Sehra49e0532017-08-25 08:05:29 -0700143void ImageHeader::VisitObjects(ObjectVisitor* visitor,
144 uint8_t* base,
145 PointerSize pointer_size) const {
146 DCHECK_EQ(pointer_size, GetPointerSize());
147 const ImageSection& objects = GetObjectsSection();
148 static const size_t kStartPos = RoundUp(sizeof(ImageHeader), kObjectAlignment);
149 for (size_t pos = kStartPos; pos < objects.Size(); ) {
150 mirror::Object* object = reinterpret_cast<mirror::Object*>(base + objects.Offset() + pos);
151 visitor->Visit(object);
152 pos += RoundUp(object->SizeOf(), kObjectAlignment);
153 }
154}
155
Andreas Gampebda1d602016-08-29 17:43:45 -0700156PointerSize ImageHeader::GetPointerSize() const {
157 return ConvertToPointerSize(pointer_size_);
158}
159
Mathieu Chartier1a842962018-11-13 15:09:51 -0800160bool ImageHeader::Block::Decompress(uint8_t* out_ptr,
161 const uint8_t* in_ptr,
162 std::string* error_msg) const {
163 switch (storage_mode_) {
164 case kStorageModeUncompressed: {
165 CHECK_EQ(image_size_, data_size_);
166 memcpy(out_ptr + image_offset_, in_ptr + data_offset_, data_size_);
167 break;
168 }
169 case kStorageModeLZ4:
170 case kStorageModeLZ4HC: {
171 // LZ4HC and LZ4 have same internal format, both use LZ4_decompress.
172 const size_t decompressed_size = LZ4_decompress_safe(
173 reinterpret_cast<const char*>(in_ptr) + data_offset_,
174 reinterpret_cast<char*>(out_ptr) + image_offset_,
175 data_size_,
176 image_size_);
177 CHECK_EQ(decompressed_size, image_size_);
178 break;
179 }
180 default: {
181 if (error_msg != nullptr) {
182 *error_msg = (std::ostringstream() << "Invalid image format " << storage_mode_).str();
183 }
184 return false;
185 }
186 }
187 return true;
188}
189
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700190} // namespace art