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' }; |
Vladimir Marko | 7f958e3 | 2019-10-24 09:03:58 +0000 | [diff] [blame] | 32 | const uint8_t ImageHeader::kImageVersion[] = { '0', '7', '9', '\0' }; // FP16ToHalf intrinsic |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 33 | |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 34 | ImageHeader::ImageHeader(uint32_t image_reservation_size, |
| 35 | uint32_t component_count, |
| 36 | uint32_t image_begin, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 37 | uint32_t image_size, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 38 | ImageSection* sections, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 44 | uint32_t oat_file_end, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 45 | uint32_t boot_image_begin, |
| 46 | uint32_t boot_image_size, |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 47 | uint32_t pointer_size) |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 48 | : image_reservation_size_(image_reservation_size), |
| 49 | component_count_(component_count), |
| 50 | image_begin_(image_begin), |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 51 | image_size_(image_size), |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 52 | image_checksum_(0u), |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | 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 Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 58 | boot_image_begin_(boot_image_begin), |
| 59 | boot_image_size_(boot_image_size), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 60 | image_roots_(image_roots), |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 61 | pointer_size_(pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 62 | 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 69 | CHECK(ValidPointerSize(pointer_size_)) << pointer_size_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | memcpy(magic_, kImageMagic, sizeof(kImageMagic)); |
| 71 | memcpy(version_, kImageVersion, sizeof(kImageVersion)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 72 | std::copy_n(sections, kSectionCount, sections_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Vladimir Marko | c0b30c9 | 2019-07-23 14:58:25 +0100 | [diff] [blame] | 75 | void ImageHeader::RelocateImageReferences(int64_t delta) { |
| 76 | CHECK_ALIGNED(delta, kPageSize) << "relocation delta must be page aligned"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 77 | oat_file_begin_ += delta; |
| 78 | oat_data_begin_ += delta; |
| 79 | oat_data_end_ += delta; |
| 80 | oat_file_end_ += delta; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 81 | image_begin_ += delta; |
| 82 | image_roots_ += delta; |
| 83 | } |
| 84 | |
Vladimir Marko | c0b30c9 | 2019-07-23 14:58:25 +0100 | [diff] [blame] | 85 | void 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 91 | for (size_t i = 0; i < kImageMethodsCount; ++i) { |
| 92 | image_methods_[i] += delta; |
| 93 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 96 | bool ImageHeader::IsValid() const { |
| 97 | if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) { |
| 98 | return false; |
| 99 | } |
| 100 | if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) { |
| 101 | return false; |
| 102 | } |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 103 | if (!IsAligned<kPageSize>(image_reservation_size_)) { |
| 104 | return false; |
| 105 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 106 | // Unsigned so wraparound is well defined. |
| 107 | if (image_begin_ >= image_begin_ + image_size_) { |
| 108 | return false; |
| 109 | } |
| 110 | if (oat_file_begin_ > oat_file_end_) { |
| 111 | return false; |
| 112 | } |
| 113 | if (oat_data_begin_ > oat_data_end_) { |
| 114 | return false; |
| 115 | } |
| 116 | if (oat_file_begin_ >= oat_data_begin_) { |
| 117 | return false; |
| 118 | } |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
| 122 | const char* ImageHeader::GetMagic() const { |
| 123 | CHECK(IsValid()); |
| 124 | return reinterpret_cast<const char*>(magic_); |
| 125 | } |
| 126 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 127 | ArtMethod* ImageHeader::GetImageMethod(ImageMethod index) const { |
| 128 | CHECK_LT(static_cast<size_t>(index), kImageMethodsCount); |
| 129 | return reinterpret_cast<ArtMethod*>(image_methods_[index]); |
| 130 | } |
| 131 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 132 | std::ostream& operator<<(std::ostream& os, const ImageSection& section) { |
| 133 | return os << "size=" << section.Size() << " range=" << section.Offset() << "-" << section.End(); |
| 134 | } |
| 135 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 136 | void ImageHeader::VisitObjects(ObjectVisitor* visitor, |
| 137 | uint8_t* base, |
| 138 | PointerSize pointer_size) const { |
| 139 | DCHECK_EQ(pointer_size, GetPointerSize()); |
| 140 | const ImageSection& objects = GetObjectsSection(); |
| 141 | static const size_t kStartPos = RoundUp(sizeof(ImageHeader), kObjectAlignment); |
| 142 | for (size_t pos = kStartPos; pos < objects.Size(); ) { |
| 143 | mirror::Object* object = reinterpret_cast<mirror::Object*>(base + objects.Offset() + pos); |
| 144 | visitor->Visit(object); |
| 145 | pos += RoundUp(object->SizeOf(), kObjectAlignment); |
| 146 | } |
| 147 | } |
| 148 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 149 | PointerSize ImageHeader::GetPointerSize() const { |
| 150 | return ConvertToPointerSize(pointer_size_); |
| 151 | } |
| 152 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 153 | bool ImageHeader::Block::Decompress(uint8_t* out_ptr, |
| 154 | const uint8_t* in_ptr, |
| 155 | std::string* error_msg) const { |
| 156 | switch (storage_mode_) { |
| 157 | case kStorageModeUncompressed: { |
| 158 | CHECK_EQ(image_size_, data_size_); |
| 159 | memcpy(out_ptr + image_offset_, in_ptr + data_offset_, data_size_); |
| 160 | break; |
| 161 | } |
| 162 | case kStorageModeLZ4: |
| 163 | case kStorageModeLZ4HC: { |
| 164 | // LZ4HC and LZ4 have same internal format, both use LZ4_decompress. |
| 165 | const size_t decompressed_size = LZ4_decompress_safe( |
| 166 | reinterpret_cast<const char*>(in_ptr) + data_offset_, |
| 167 | reinterpret_cast<char*>(out_ptr) + image_offset_, |
| 168 | data_size_, |
| 169 | image_size_); |
| 170 | CHECK_EQ(decompressed_size, image_size_); |
| 171 | break; |
| 172 | } |
| 173 | default: { |
| 174 | if (error_msg != nullptr) { |
| 175 | *error_msg = (std::ostringstream() << "Invalid image format " << storage_mode_).str(); |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 183 | } // namespace art |