Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [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 | */ |
| 16 | |
| 17 | #include "image_space.h" |
| 18 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 19 | #include "base/stl_util.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 20 | #include "base/unix_file/fd_file.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 21 | #include "base/scoped_flock.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 22 | #include "gc/accounting/space_bitmap-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 23 | #include "mirror/art_method.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 24 | #include "mirror/class-inl.h" |
| 25 | #include "mirror/object-inl.h" |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 26 | #include "oat_file.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 27 | #include "os.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 28 | #include "space-inl.h" |
| 29 | #include "utils.h" |
| 30 | |
| 31 | namespace art { |
| 32 | namespace gc { |
| 33 | namespace space { |
| 34 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 35 | Atomic<uint32_t> ImageSpace::bitmap_index_(0); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 36 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 37 | ImageSpace::ImageSpace(const std::string& image_filename, const char* image_location, |
| 38 | MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap) |
| 39 | : MemMapSpace(image_filename, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(), |
| 40 | kGcRetentionPolicyNeverCollect), |
| 41 | image_location_(image_location) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 42 | DCHECK(live_bitmap != nullptr); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 43 | live_bitmap_.reset(live_bitmap); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 46 | static bool GenerateImage(const std::string& image_filename, std::string* error_msg) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 47 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
| 48 | std::vector<std::string> boot_class_path; |
| 49 | Split(boot_class_path_string, ':', boot_class_path); |
| 50 | if (boot_class_path.empty()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 51 | *error_msg = "Failed to generate image because no boot class path specified"; |
| 52 | return false; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 55 | std::vector<std::string> arg_vector; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 56 | |
Tsu Chiang Chuang | 12e6d74 | 2014-05-22 10:22:25 -0700 | [diff] [blame] | 57 | std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); |
Mathieu Chartier | 08d7d44 | 2013-07-31 18:08:51 -0700 | [diff] [blame] | 58 | arg_vector.push_back(dex2oat); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 59 | |
| 60 | std::string image_option_string("--image="); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 61 | image_option_string += image_filename; |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 62 | arg_vector.push_back(image_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 63 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 64 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 65 | arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | std::string oat_file_option_string("--oat-file="); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 69 | oat_file_option_string += image_filename; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 70 | oat_file_option_string.erase(oat_file_option_string.size() - 3); |
| 71 | oat_file_option_string += "oat"; |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 72 | arg_vector.push_back(oat_file_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 73 | |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 74 | Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector); |
| 75 | |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 76 | arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS)); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 77 | |
| 78 | if (kIsTargetBuild) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 79 | arg_vector.push_back("--image-classes-zip=/system/framework/framework.jar"); |
| 80 | arg_vector.push_back("--image-classes=preloaded-classes"); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 81 | } else { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 82 | arg_vector.push_back("--host"); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 85 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 86 | for (size_t i = 0; i < compiler_options.size(); ++i) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 87 | arg_vector.push_back(compiler_options[i].c_str()); |
| 88 | } |
| 89 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 90 | std::string command_line(Join(arg_vector, ' ')); |
| 91 | LOG(INFO) << "GenerateImage: " << command_line; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 92 | return Exec(arg_vector, error_msg); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 95 | bool ImageSpace::FindImageFilename(const char* image_location, |
| 96 | const InstructionSet image_isa, |
| 97 | std::string* image_filename, |
| 98 | bool *is_system) { |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 99 | // image_location = /system/framework/boot.art |
| 100 | // system_image_location = /system/framework/<image_isa>/boot.art |
| 101 | std::string system_image_filename(GetSystemImageFilename(image_location, image_isa)); |
| 102 | if (OS::FileExists(system_image_filename.c_str())) { |
| 103 | *image_filename = system_image_filename; |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 104 | *is_system = true; |
| 105 | return true; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 106 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 107 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 108 | const std::string dalvik_cache = GetDalvikCacheOrDie(GetInstructionSetString(image_isa)); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 109 | |
| 110 | // Always set output location even if it does not exist, |
| 111 | // so that the caller knows where to create the image. |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 112 | // |
| 113 | // image_location = /system/framework/boot.art |
| 114 | // *image_filename = /data/dalvik-cache/<image_isa>/boot.art |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 115 | *image_filename = GetDalvikCacheFilenameOrDie(image_location, dalvik_cache.c_str()); |
| 116 | *is_system = false; |
| 117 | return OS::FileExists(image_filename->c_str()); |
| 118 | } |
| 119 | |
| 120 | ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location, |
| 121 | const InstructionSet image_isa) { |
| 122 | std::string image_filename; |
| 123 | bool is_system = false; |
| 124 | if (FindImageFilename(image_location, image_isa, &image_filename, &is_system)) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 125 | std::unique_ptr<File> image_file(OS::OpenFileForReading(image_filename.c_str())); |
| 126 | std::unique_ptr<ImageHeader> image_header(new ImageHeader); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 127 | const bool success = image_file->ReadFully(image_header.get(), sizeof(ImageHeader)); |
| 128 | if (!success || !image_header->IsValid()) { |
| 129 | LOG(FATAL) << "Invalid Image header for: " << image_filename; |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
| 133 | return image_header.release(); |
| 134 | } |
| 135 | |
| 136 | LOG(FATAL) << "Unable to find image file for: " << image_location; |
| 137 | return nullptr; |
| 138 | } |
| 139 | |
| 140 | ImageSpace* ImageSpace::Create(const char* image_location, |
| 141 | const InstructionSet image_isa) { |
| 142 | std::string image_filename; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 143 | std::string error_msg; |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 144 | bool is_system = false; |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 145 | const bool found_image = FindImageFilename(image_location, image_isa, &image_filename, |
| 146 | &is_system); |
| 147 | |
| 148 | // Note that we must not use the file descriptor associated with |
| 149 | // ScopedFlock::GetFile to Init the image file. We want the file |
| 150 | // descriptor (and the associated exclusive lock) to be released when |
| 151 | // we leave Create. |
| 152 | ScopedFlock image_lock; |
| 153 | image_lock.Init(image_filename.c_str(), &error_msg); |
| 154 | |
| 155 | if (found_image) { |
Ian Rogers | b9beb2e | 2014-05-09 16:57:40 -0700 | [diff] [blame] | 156 | ImageSpace* space = ImageSpace::Init(image_filename.c_str(), image_location, !is_system, |
| 157 | &error_msg); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 158 | if (space != nullptr) { |
| 159 | return space; |
| 160 | } |
| 161 | |
| 162 | // If the /system file exists, it should be up-to-date, don't try to generate it. |
| 163 | // If it's not the /system file, log a warning and fall through to GenerateImage. |
| 164 | if (is_system) { |
| 165 | LOG(FATAL) << "Failed to load image '" << image_filename << "': " << error_msg; |
| 166 | return nullptr; |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 167 | } else { |
| 168 | LOG(WARNING) << error_msg; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 171 | |
| 172 | CHECK(GenerateImage(image_filename, &error_msg)) |
| 173 | << "Failed to generate image '" << image_filename << "': " << error_msg; |
| 174 | ImageSpace* space = ImageSpace::Init(image_filename.c_str(), image_location, true, &error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 175 | if (space == nullptr) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 176 | LOG(FATAL) << "Failed to load image '" << image_filename << "': " << error_msg; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 177 | } |
| 178 | return space; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 181 | void ImageSpace::VerifyImageAllocations() { |
| 182 | byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
| 183 | while (current < End()) { |
| 184 | DCHECK_ALIGNED(current, kObjectAlignment); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 185 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(current); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 186 | CHECK(live_bitmap_->Test(obj)); |
| 187 | CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class"; |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 188 | if (kUseBakerOrBrooksReadBarrier) { |
| 189 | obj->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 190 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 191 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
| 192 | } |
| 193 | } |
| 194 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 195 | ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location, |
| 196 | bool validate_oat_file, std::string* error_msg) { |
| 197 | CHECK(image_filename != nullptr); |
| 198 | CHECK(image_location != nullptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 199 | |
| 200 | uint64_t start_time = 0; |
| 201 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 202 | start_time = NanoTime(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 203 | LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 206 | std::unique_ptr<File> file(OS::OpenFileForReading(image_filename)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 207 | if (file.get() == NULL) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 208 | *error_msg = StringPrintf("Failed to open '%s'", image_filename); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 209 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 210 | } |
| 211 | ImageHeader image_header; |
| 212 | bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 213 | if (!success || !image_header.IsValid()) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 214 | *error_msg = StringPrintf("Invalid image header in '%s'", image_filename); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 215 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 216 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 217 | |
| 218 | // Note: The image header is part of the image due to mmap page alignment required of offset. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 219 | std::unique_ptr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(), |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 220 | image_header.GetImageSize(), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 221 | PROT_READ | PROT_WRITE, |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 222 | MAP_PRIVATE, |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 223 | file->Fd(), |
| 224 | 0, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 225 | false, |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 226 | image_filename, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 227 | error_msg)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 228 | if (map.get() == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 229 | DCHECK(!error_msg->empty()); |
| 230 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 231 | } |
| 232 | CHECK_EQ(image_header.GetImageBegin(), map->Begin()); |
| 233 | DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader))); |
| 234 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 235 | std::unique_ptr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(), |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 236 | PROT_READ, MAP_PRIVATE, |
| 237 | file->Fd(), image_header.GetBitmapOffset(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 238 | false, |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 239 | image_filename, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 240 | error_msg)); |
| 241 | if (image_map.get() == nullptr) { |
| 242 | *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str()); |
| 243 | return nullptr; |
| 244 | } |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 245 | uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 246 | std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 247 | bitmap_index)); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 248 | std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap( |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 249 | accounting::ContinuousSpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(), |
| 250 | reinterpret_cast<byte*>(map->Begin()), |
| 251 | map->Size())); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 252 | if (bitmap.get() == nullptr) { |
| 253 | *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str()); |
| 254 | return nullptr; |
| 255 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 256 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 257 | std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, image_location, |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 258 | map.release(), bitmap.release())); |
Hiroshi Yamauchi | bd0fb61 | 2014-05-20 13:46:00 -0700 | [diff] [blame] | 259 | |
| 260 | // VerifyImageAllocations() will be called later in Runtime::Init() |
| 261 | // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_ |
| 262 | // and ArtField::java_lang_reflect_ArtField_, which are used from |
| 263 | // Object::SizeOf() which VerifyImageAllocations() calls, are not |
| 264 | // set yet at this point. |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 265 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 266 | space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 267 | if (space->oat_file_.get() == nullptr) { |
| 268 | DCHECK(!error_msg->empty()); |
| 269 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 270 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 271 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 272 | if (validate_oat_file && !space->ValidateOatFile(error_msg)) { |
| 273 | DCHECK(!error_msg->empty()); |
| 274 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 277 | Runtime* runtime = Runtime::Current(); |
| 278 | runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet()); |
| 279 | |
| 280 | mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod); |
| 281 | runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method)); |
| 282 | mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod); |
| 283 | runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method)); |
| 284 | mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt); |
| 285 | runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt)); |
| 286 | |
| 287 | mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod); |
| 288 | runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kSaveAll); |
| 289 | callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod); |
| 290 | runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsOnly); |
| 291 | callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod); |
| 292 | runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsAndArgs); |
| 293 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 294 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 295 | LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time) |
| 296 | << ") " << *space.get(); |
| 297 | } |
| 298 | return space.release(); |
| 299 | } |
| 300 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 301 | OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 302 | const ImageHeader& image_header = GetImageHeader(); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 303 | std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path); |
| 304 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 305 | OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 306 | !Runtime::Current()->IsCompiler(), error_msg); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 307 | if (oat_file == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 308 | *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s", |
| 309 | oat_filename.c_str(), GetName(), error_msg->c_str()); |
| 310 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 311 | } |
| 312 | uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum(); |
| 313 | uint32_t image_oat_checksum = image_header.GetOatChecksum(); |
| 314 | if (oat_checksum != image_oat_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 315 | *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x" |
| 316 | " in image %s", oat_checksum, image_oat_checksum, GetName()); |
| 317 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 318 | } |
| 319 | return oat_file; |
| 320 | } |
| 321 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 322 | bool ImageSpace::ValidateOatFile(std::string* error_msg) const { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 323 | CHECK(oat_file_.get() != NULL); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 324 | for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 325 | const std::string& dex_file_location = oat_dex_file->GetDexFileLocation(); |
| 326 | uint32_t dex_file_location_checksum; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 327 | if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) { |
| 328 | *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: " |
| 329 | "%s", dex_file_location.c_str(), GetName(), error_msg->c_str()); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 333 | *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and " |
| 334 | "dex file '%s' (0x%x != 0x%x)", |
| 335 | oat_file_->GetLocation().c_str(), dex_file_location.c_str(), |
| 336 | oat_dex_file->GetDexFileLocationChecksum(), |
| 337 | dex_file_location_checksum); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | } |
| 341 | return true; |
| 342 | } |
| 343 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame^] | 344 | const OatFile* ImageSpace::GetOatFile() const { |
| 345 | return oat_file_.get(); |
| 346 | } |
| 347 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 348 | OatFile* ImageSpace::ReleaseOatFile() { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 349 | CHECK(oat_file_.get() != NULL); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 350 | return oat_file_.release(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 353 | void ImageSpace::Dump(std::ostream& os) const { |
| 354 | os << GetType() |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 355 | << " begin=" << reinterpret_cast<void*>(Begin()) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 356 | << ",end=" << reinterpret_cast<void*>(End()) |
| 357 | << ",size=" << PrettySize(Size()) |
| 358 | << ",name=\"" << GetName() << "\"]"; |
| 359 | } |
| 360 | |
| 361 | } // namespace space |
| 362 | } // namespace gc |
| 363 | } // namespace art |