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 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame^] | 19 | #include "base/bit_utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #include "mirror/object_array.h" |
| 21 | #include "mirror/object_array-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 22 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 26 | const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' }; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 27 | const uint8_t ImageHeader::kImageVersion[] = { '0', '1', '5', '\0' }; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 28 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | ImageHeader::ImageHeader(uint32_t image_begin, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 30 | uint32_t image_size, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 31 | uint32_t art_fields_offset, |
| 32 | uint32_t art_fields_size, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 33 | uint32_t image_bitmap_offset, |
| 34 | uint32_t image_bitmap_size, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | uint32_t image_roots, |
| 36 | uint32_t oat_checksum, |
| 37 | uint32_t oat_file_begin, |
| 38 | uint32_t oat_data_begin, |
| 39 | uint32_t oat_data_end, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 40 | uint32_t oat_file_end, |
| 41 | bool compile_pic) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 42 | : image_begin_(image_begin), |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 43 | image_size_(image_size), |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 44 | art_fields_offset_(art_fields_offset), |
| 45 | art_fields_size_(art_fields_size), |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 46 | image_bitmap_offset_(image_bitmap_offset), |
| 47 | image_bitmap_size_(image_bitmap_size), |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | oat_checksum_(oat_checksum), |
| 49 | oat_file_begin_(oat_file_begin), |
| 50 | oat_data_begin_(oat_data_begin), |
| 51 | oat_data_end_(oat_data_end), |
| 52 | oat_file_end_(oat_file_end), |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 53 | patch_delta_(0), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 54 | image_roots_(image_roots), |
| 55 | compile_pic_(compile_pic) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize)); |
| 57 | CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize)); |
| 58 | CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize)); |
| 59 | CHECK_LT(image_begin, image_roots); |
| 60 | CHECK_LT(image_roots, oat_file_begin); |
| 61 | CHECK_LE(oat_file_begin, oat_data_begin); |
| 62 | CHECK_LT(oat_data_begin, oat_data_end); |
| 63 | CHECK_LE(oat_data_end, oat_file_end); |
| 64 | memcpy(magic_, kImageMagic, sizeof(kImageMagic)); |
| 65 | memcpy(version_, kImageVersion, sizeof(kImageVersion)); |
| 66 | } |
| 67 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 68 | void ImageHeader::RelocateImage(off_t delta) { |
| 69 | CHECK_ALIGNED(delta, kPageSize) << " patch delta must be page aligned"; |
| 70 | image_begin_ += delta; |
| 71 | oat_file_begin_ += delta; |
| 72 | oat_data_begin_ += delta; |
| 73 | oat_data_end_ += delta; |
| 74 | oat_file_end_ += delta; |
| 75 | image_roots_ += delta; |
| 76 | patch_delta_ += delta; |
| 77 | } |
| 78 | |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 79 | bool ImageHeader::IsValid() const { |
| 80 | if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) { |
| 81 | return false; |
| 82 | } |
| 83 | if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) { |
| 84 | return false; |
| 85 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 86 | // Unsigned so wraparound is well defined. |
| 87 | if (image_begin_ >= image_begin_ + image_size_) { |
| 88 | return false; |
| 89 | } |
| 90 | if (oat_file_begin_ > oat_file_end_) { |
| 91 | return false; |
| 92 | } |
| 93 | if (oat_data_begin_ > oat_data_end_) { |
| 94 | return false; |
| 95 | } |
| 96 | if (oat_file_begin_ >= oat_data_begin_) { |
| 97 | return false; |
| 98 | } |
| 99 | if (image_roots_ <= image_begin_ || oat_file_begin_ <= image_roots_) { |
| 100 | return false; |
| 101 | } |
| 102 | if (!IsAligned<kPageSize>(patch_delta_)) { |
| 103 | return false; |
| 104 | } |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
| 108 | const char* ImageHeader::GetMagic() const { |
| 109 | CHECK(IsValid()); |
| 110 | return reinterpret_cast<const char*>(magic_); |
| 111 | } |
| 112 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 113 | mirror::Object* ImageHeader::GetImageRoot(ImageRoot image_root) const { |
| 114 | return GetImageRoots()->Get(image_root); |
| 115 | } |
| 116 | |
| 117 | mirror::ObjectArray<mirror::Object>* ImageHeader::GetImageRoots() const { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 118 | // Need a read barrier as it's not visited during root scan. |
| 119 | // Pass in the address of the local variable to the read barrier |
| 120 | // rather than image_roots_ because it won't move (asserted below) |
| 121 | // and it's a const member. |
| 122 | mirror::ObjectArray<mirror::Object>* image_roots = |
| 123 | reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(image_roots_); |
| 124 | mirror::ObjectArray<mirror::Object>* result = |
| 125 | ReadBarrier::BarrierForRoot<mirror::ObjectArray<mirror::Object>, kWithReadBarrier, true>( |
| 126 | &image_roots); |
| 127 | DCHECK_EQ(image_roots, result); |
| 128 | return result; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 131 | } // namespace art |