image.cc revision 2cd334ae2d4287216523882f0d298cf3901b7ab1
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 "mirror/object_array.h"
20#include "mirror/object_array-inl.h"
21#include "mirror/object-inl.h"
22#include "utils.h"
23
24namespace art {
25
26const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' };
27const uint8_t ImageHeader::kImageVersion[] = { '0', '1', '3', '\0' };
28
29ImageHeader::ImageHeader(uint32_t image_begin,
30                         uint32_t image_size,
31                         uint32_t image_bitmap_offset,
32                         uint32_t image_bitmap_size,
33                         uint32_t image_roots,
34                         uint32_t oat_checksum,
35                         uint32_t oat_file_begin,
36                         uint32_t oat_data_begin,
37                         uint32_t oat_data_end,
38                         uint32_t oat_file_end,
39                         bool compile_pic)
40  : image_begin_(image_begin),
41    image_size_(image_size),
42    image_bitmap_offset_(image_bitmap_offset),
43    image_bitmap_size_(image_bitmap_size),
44    oat_checksum_(oat_checksum),
45    oat_file_begin_(oat_file_begin),
46    oat_data_begin_(oat_data_begin),
47    oat_data_end_(oat_data_end),
48    oat_file_end_(oat_file_end),
49    patch_delta_(0),
50    image_roots_(image_roots),
51    compile_pic_(compile_pic) {
52  CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize));
53  CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize));
54  CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize));
55  CHECK_LT(image_begin, image_roots);
56  CHECK_LT(image_roots, oat_file_begin);
57  CHECK_LE(oat_file_begin, oat_data_begin);
58  CHECK_LT(oat_data_begin, oat_data_end);
59  CHECK_LE(oat_data_end, oat_file_end);
60  memcpy(magic_, kImageMagic, sizeof(kImageMagic));
61  memcpy(version_, kImageVersion, sizeof(kImageVersion));
62}
63
64void ImageHeader::RelocateImage(off_t delta) {
65  CHECK_ALIGNED(delta, kPageSize) << " patch delta must be page aligned";
66  image_begin_ += delta;
67  oat_file_begin_ += delta;
68  oat_data_begin_ += delta;
69  oat_data_end_ += delta;
70  oat_file_end_ += delta;
71  image_roots_ += delta;
72  patch_delta_ += delta;
73}
74
75bool ImageHeader::IsValid() const {
76  if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) {
77    return false;
78  }
79  if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) {
80    return false;
81  }
82  // Unsigned so wraparound is well defined.
83  if (image_begin_ >= image_begin_ + image_size_) {
84    return false;
85  }
86  if (oat_file_begin_ > oat_file_end_) {
87    return false;
88  }
89  if (oat_data_begin_ > oat_data_end_) {
90    return false;
91  }
92  if (oat_file_begin_ >= oat_data_begin_) {
93    return false;
94  }
95  if (image_roots_ <= image_begin_ || oat_file_begin_ <= image_roots_) {
96    return false;
97  }
98  if (!IsAligned<kPageSize>(patch_delta_)) {
99    return false;
100  }
101  return true;
102}
103
104const char* ImageHeader::GetMagic() const {
105  CHECK(IsValid());
106  return reinterpret_cast<const char*>(magic_);
107}
108
109mirror::Object* ImageHeader::GetImageRoot(ImageRoot image_root) const {
110  return GetImageRoots()->Get(image_root);
111}
112
113mirror::ObjectArray<mirror::Object>* ImageHeader::GetImageRoots() const {
114  // Need a read barrier as it's not visited during root scan.
115  // Pass in the address of the local variable to the read barrier
116  // rather than image_roots_ because it won't move (asserted below)
117  // and it's a const member.
118  mirror::ObjectArray<mirror::Object>* image_roots =
119      reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(image_roots_);
120  mirror::ObjectArray<mirror::Object>* result =
121      ReadBarrier::BarrierForRoot<mirror::ObjectArray<mirror::Object>, kWithReadBarrier, true>(
122          &image_roots);
123  DCHECK_EQ(image_roots, result);
124  return result;
125}
126
127}  // namespace art
128