Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 16 | |
| 17 | #include "image.h" |
| 18 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 19 | #include <lz4.h> |
| 20 | #include <sstream> |
| 21 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 22 | #include "base/bit_utils.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 23 | #include "base/length_prefixed_array.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 24 | #include "base/utils.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | #include "mirror/object-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 26 | #include "mirror/object_array-inl.h" |
| 27 | #include "mirror/object_array.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 31 | const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' }; |
Shalini Salomi Bodapati | 6545ee3 | 2021-11-02 20:01:06 +0530 | [diff] [blame] | 32 | // Last change: Math.fma(double, double, double) intrinsic. |
Nikita Iashchenko | 3fa6e46 | 2021-09-10 17:30:04 +0100 | [diff] [blame] | 33 | const uint8_t ImageHeader::kImageVersion[] = { '1', '0', '6', '\0' }; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 34 | |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 35 | ImageHeader::ImageHeader(uint32_t image_reservation_size, |
| 36 | uint32_t component_count, |
| 37 | uint32_t image_begin, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 38 | uint32_t image_size, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 39 | ImageSection* sections, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | uint32_t image_roots, |
| 41 | uint32_t oat_checksum, |
| 42 | uint32_t oat_file_begin, |
| 43 | uint32_t oat_data_begin, |
| 44 | uint32_t oat_data_end, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 45 | uint32_t oat_file_end, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 46 | uint32_t boot_image_begin, |
| 47 | uint32_t boot_image_size, |
Vladimir Marko | 92eec3a | 2019-11-05 10:59:36 +0000 | [diff] [blame] | 48 | uint32_t boot_image_component_count, |
| 49 | uint32_t boot_image_checksum, |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 50 | uint32_t pointer_size) |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 51 | : image_reservation_size_(image_reservation_size), |
| 52 | component_count_(component_count), |
| 53 | image_begin_(image_begin), |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 54 | image_size_(image_size), |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 55 | image_checksum_(0u), |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | oat_checksum_(oat_checksum), |
| 57 | oat_file_begin_(oat_file_begin), |
| 58 | oat_data_begin_(oat_data_begin), |
| 59 | oat_data_end_(oat_data_end), |
| 60 | oat_file_end_(oat_file_end), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 61 | boot_image_begin_(boot_image_begin), |
| 62 | boot_image_size_(boot_image_size), |
Vladimir Marko | 92eec3a | 2019-11-05 10:59:36 +0000 | [diff] [blame] | 63 | boot_image_component_count_(boot_image_component_count), |
| 64 | boot_image_checksum_(boot_image_checksum), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 65 | image_roots_(image_roots), |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 66 | pointer_size_(pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 67 | CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize)); |
| 68 | CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize)); |
| 69 | CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | CHECK_LT(image_roots, oat_file_begin); |
| 71 | CHECK_LE(oat_file_begin, oat_data_begin); |
| 72 | CHECK_LT(oat_data_begin, oat_data_end); |
| 73 | CHECK_LE(oat_data_end, oat_file_end); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 74 | CHECK(ValidPointerSize(pointer_size_)) << pointer_size_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 75 | memcpy(magic_, kImageMagic, sizeof(kImageMagic)); |
| 76 | memcpy(version_, kImageVersion, sizeof(kImageVersion)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 77 | std::copy_n(sections, kSectionCount, sections_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Vladimir Marko | c0b30c9 | 2019-07-23 14:58:25 +0100 | [diff] [blame] | 80 | void ImageHeader::RelocateImageReferences(int64_t delta) { |
| 81 | CHECK_ALIGNED(delta, kPageSize) << "relocation delta must be page aligned"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 82 | oat_file_begin_ += delta; |
| 83 | oat_data_begin_ += delta; |
| 84 | oat_data_end_ += delta; |
| 85 | oat_file_end_ += delta; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 86 | image_begin_ += delta; |
| 87 | image_roots_ += delta; |
| 88 | } |
| 89 | |
Vladimir Marko | c0b30c9 | 2019-07-23 14:58:25 +0100 | [diff] [blame] | 90 | void ImageHeader::RelocateBootImageReferences(int64_t delta) { |
| 91 | CHECK_ALIGNED(delta, kPageSize) << "relocation delta must be page aligned"; |
| 92 | DCHECK_EQ(boot_image_begin_ != 0u, boot_image_size_ != 0u); |
| 93 | if (boot_image_begin_ != 0u) { |
| 94 | boot_image_begin_ += delta; |
| 95 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 96 | for (size_t i = 0; i < kImageMethodsCount; ++i) { |
| 97 | image_methods_[i] += delta; |
| 98 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 101 | bool ImageHeader::IsAppImage() const { |
| 102 | // Unlike boot image and boot image extensions which include address space for |
| 103 | // oat files in their reservation size, app images are loaded separately from oat |
| 104 | // files and their reservation size is the image size rounded up to full page. |
| 105 | return image_reservation_size_ == RoundUp(image_size_, kPageSize); |
| 106 | } |
| 107 | |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 108 | uint32_t ImageHeader::GetImageSpaceCount() const { |
| 109 | DCHECK(!IsAppImage()); |
| 110 | DCHECK_NE(component_count_, 0u); // Must be the header for the first component. |
| 111 | // For images compiled with --single-image, there is only one oat file. To detect |
| 112 | // that, check whether the reservation ends at the end of the first oat file. |
| 113 | return (image_begin_ + image_reservation_size_ == oat_file_end_) ? 1u : component_count_; |
| 114 | } |
| 115 | |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 116 | bool ImageHeader::IsValid() const { |
| 117 | if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) { |
| 118 | return false; |
| 119 | } |
| 120 | if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) { |
| 121 | return false; |
| 122 | } |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 123 | if (!IsAligned<kPageSize>(image_reservation_size_)) { |
| 124 | return false; |
| 125 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 126 | // Unsigned so wraparound is well defined. |
| 127 | if (image_begin_ >= image_begin_ + image_size_) { |
| 128 | return false; |
| 129 | } |
| 130 | if (oat_file_begin_ > oat_file_end_) { |
| 131 | return false; |
| 132 | } |
| 133 | if (oat_data_begin_ > oat_data_end_) { |
| 134 | return false; |
| 135 | } |
| 136 | if (oat_file_begin_ >= oat_data_begin_) { |
| 137 | return false; |
| 138 | } |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 139 | return true; |
| 140 | } |
| 141 | |
| 142 | const char* ImageHeader::GetMagic() const { |
| 143 | CHECK(IsValid()); |
| 144 | return reinterpret_cast<const char*>(magic_); |
| 145 | } |
| 146 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 147 | ArtMethod* ImageHeader::GetImageMethod(ImageMethod index) const { |
| 148 | CHECK_LT(static_cast<size_t>(index), kImageMethodsCount); |
| 149 | return reinterpret_cast<ArtMethod*>(image_methods_[index]); |
| 150 | } |
| 151 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 152 | std::ostream& operator<<(std::ostream& os, const ImageSection& section) { |
| 153 | return os << "size=" << section.Size() << " range=" << section.Offset() << "-" << section.End(); |
| 154 | } |
| 155 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 156 | void ImageHeader::VisitObjects(ObjectVisitor* visitor, |
| 157 | uint8_t* base, |
| 158 | PointerSize pointer_size) const { |
| 159 | DCHECK_EQ(pointer_size, GetPointerSize()); |
| 160 | const ImageSection& objects = GetObjectsSection(); |
| 161 | static const size_t kStartPos = RoundUp(sizeof(ImageHeader), kObjectAlignment); |
| 162 | for (size_t pos = kStartPos; pos < objects.Size(); ) { |
| 163 | mirror::Object* object = reinterpret_cast<mirror::Object*>(base + objects.Offset() + pos); |
| 164 | visitor->Visit(object); |
| 165 | pos += RoundUp(object->SizeOf(), kObjectAlignment); |
| 166 | } |
| 167 | } |
| 168 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 169 | PointerSize ImageHeader::GetPointerSize() const { |
| 170 | return ConvertToPointerSize(pointer_size_); |
| 171 | } |
| 172 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 173 | bool ImageHeader::Block::Decompress(uint8_t* out_ptr, |
| 174 | const uint8_t* in_ptr, |
| 175 | std::string* error_msg) const { |
| 176 | switch (storage_mode_) { |
| 177 | case kStorageModeUncompressed: { |
| 178 | CHECK_EQ(image_size_, data_size_); |
| 179 | memcpy(out_ptr + image_offset_, in_ptr + data_offset_, data_size_); |
| 180 | break; |
| 181 | } |
| 182 | case kStorageModeLZ4: |
| 183 | case kStorageModeLZ4HC: { |
| 184 | // LZ4HC and LZ4 have same internal format, both use LZ4_decompress. |
| 185 | const size_t decompressed_size = LZ4_decompress_safe( |
| 186 | reinterpret_cast<const char*>(in_ptr) + data_offset_, |
| 187 | reinterpret_cast<char*>(out_ptr) + image_offset_, |
| 188 | data_size_, |
| 189 | image_size_); |
| 190 | CHECK_EQ(decompressed_size, image_size_); |
| 191 | break; |
| 192 | } |
| 193 | default: { |
| 194 | if (error_msg != nullptr) { |
| 195 | *error_msg = (std::ostringstream() << "Invalid image format " << storage_mode_).str(); |
| 196 | } |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
David Srbecky | bc1748f | 2021-02-22 10:51:32 +0000 | [diff] [blame] | 203 | const char* ImageHeader::GetImageSectionName(ImageSections index) { |
| 204 | switch (index) { |
| 205 | case kSectionObjects: return "Objects"; |
| 206 | case kSectionArtFields: return "ArtFields"; |
| 207 | case kSectionArtMethods: return "ArtMethods"; |
| 208 | case kSectionRuntimeMethods: return "RuntimeMethods"; |
| 209 | case kSectionImTables: return "ImTables"; |
| 210 | case kSectionIMTConflictTables: return "IMTConflictTables"; |
| 211 | case kSectionInternedStrings: return "InternedStrings"; |
| 212 | case kSectionClassTable: return "ClassTable"; |
| 213 | case kSectionStringReferenceOffsets: return "StringReferenceOffsets"; |
| 214 | case kSectionMetadata: return "Metadata"; |
| 215 | case kSectionImageBitmap: return "ImageBitmap"; |
| 216 | case kSectionCount: return nullptr; |
| 217 | } |
| 218 | } |
| 219 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 220 | } // namespace art |