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" |
| 21 | #include "gc/accounting/space_bitmap-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 22 | #include "mirror/art_method.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 23 | #include "mirror/class-inl.h" |
| 24 | #include "mirror/object-inl.h" |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 25 | #include "oat_file.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 26 | #include "os.h" |
| 27 | #include "runtime.h" |
| 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 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 37 | ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map, |
| 38 | accounting::SpaceBitmap* live_bitmap) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 39 | : MemMapSpace(name, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(), |
| 40 | kGcRetentionPolicyNeverCollect) { |
| 41 | DCHECK(live_bitmap != nullptr); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 42 | live_bitmap_.reset(live_bitmap); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 45 | static bool GenerateImage(const std::string& image_file_name, std::string* error_msg) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 46 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
| 47 | std::vector<std::string> boot_class_path; |
| 48 | Split(boot_class_path_string, ':', boot_class_path); |
| 49 | if (boot_class_path.empty()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 50 | *error_msg = "Failed to generate image because no boot class path specified"; |
| 51 | return false; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 54 | std::vector<std::string> arg_vector; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 55 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 56 | std::string dex2oat(GetAndroidRoot()); |
Mathieu Chartier | 08d7d44 | 2013-07-31 18:08:51 -0700 | [diff] [blame] | 57 | dex2oat += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); |
| 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="); |
| 61 | image_option_string += image_file_name; |
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 | |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 64 | arg_vector.push_back("--runtime-arg"); |
| 65 | arg_vector.push_back("-Xms64m"); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 66 | |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 67 | arg_vector.push_back("--runtime-arg"); |
| 68 | arg_vector.push_back("-Xmx64m"); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 69 | |
| 70 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 71 | arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | std::string oat_file_option_string("--oat-file="); |
| 75 | oat_file_option_string += image_file_name; |
| 76 | oat_file_option_string.erase(oat_file_option_string.size() - 3); |
| 77 | oat_file_option_string += "oat"; |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 78 | arg_vector.push_back(oat_file_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 79 | |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 80 | arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS)); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 81 | |
| 82 | if (kIsTargetBuild) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 83 | arg_vector.push_back("--image-classes-zip=/system/framework/framework.jar"); |
| 84 | arg_vector.push_back("--image-classes=preloaded-classes"); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 85 | } else { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 86 | arg_vector.push_back("--host"); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 89 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions(); |
| 90 | for (size_t i = 0; compiler_options.size(); ++i) { |
| 91 | arg_vector.push_back(compiler_options[i].c_str()); |
| 92 | } |
| 93 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 94 | std::string command_line(Join(arg_vector, ' ')); |
| 95 | LOG(INFO) << "GenerateImage: " << command_line; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 96 | return Exec(arg_vector, error_msg); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 99 | ImageSpace* ImageSpace::Create(const char* original_image_file_name) { |
| 100 | if (OS::FileExists(original_image_file_name)) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 101 | // If the /system file exists, it should be up-to-date, don't try to generate |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 102 | std::string error_msg; |
| 103 | ImageSpace* space = ImageSpace::Init(original_image_file_name, false, &error_msg); |
| 104 | if (space == nullptr) { |
| 105 | LOG(FATAL) << "Failed to load image '" << original_image_file_name << "': " << error_msg; |
| 106 | } |
| 107 | return space; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 108 | } |
| 109 | // If the /system file didn't exist, we need to use one from the dalvik-cache. |
| 110 | // If the cache file exists, try to open, but if it fails, regenerate. |
| 111 | // If it does not exist, generate. |
| 112 | std::string image_file_name(GetDalvikCacheFilenameOrDie(original_image_file_name)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 113 | std::string error_msg; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 114 | if (OS::FileExists(image_file_name.c_str())) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 115 | space::ImageSpace* image_space = ImageSpace::Init(image_file_name.c_str(), true, &error_msg); |
| 116 | if (image_space != nullptr) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 117 | return image_space; |
| 118 | } |
| 119 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 120 | CHECK(GenerateImage(image_file_name, &error_msg)) |
| 121 | << "Failed to generate image '" << image_file_name << "': " << error_msg; |
| 122 | ImageSpace* space = ImageSpace::Init(image_file_name.c_str(), true, &error_msg); |
| 123 | if (space == nullptr) { |
| 124 | LOG(FATAL) << "Failed to load image '" << original_image_file_name << "': " << error_msg; |
| 125 | } |
| 126 | return space; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 129 | void ImageSpace::VerifyImageAllocations() { |
| 130 | byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
| 131 | while (current < End()) { |
| 132 | DCHECK_ALIGNED(current, kObjectAlignment); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 133 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(current); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 134 | CHECK(live_bitmap_->Test(obj)); |
| 135 | CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class"; |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 136 | if (kUseBrooksPointer) { |
| 137 | CHECK(obj->GetBrooksPointer() == obj) |
| 138 | << "Bad Brooks pointer: obj=" << reinterpret_cast<void*>(obj) |
| 139 | << " brooks_ptr=" << reinterpret_cast<void*>(obj->GetBrooksPointer()); |
| 140 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 141 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
| 142 | } |
| 143 | } |
| 144 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 145 | ImageSpace* ImageSpace::Init(const char* image_file_name, bool validate_oat_file, |
| 146 | std::string* error_msg) { |
| 147 | CHECK(image_file_name != nullptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 148 | |
| 149 | uint64_t start_time = 0; |
| 150 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 151 | start_time = NanoTime(); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 152 | LOG(INFO) << "ImageSpace::Init entering image_file_name=" << image_file_name; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 155 | UniquePtr<File> file(OS::OpenFileForReading(image_file_name)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 156 | if (file.get() == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 157 | *error_msg = StringPrintf("Failed to open '%s'", image_file_name); |
| 158 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 159 | } |
| 160 | ImageHeader image_header; |
| 161 | bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 162 | if (!success || !image_header.IsValid()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 163 | *error_msg = StringPrintf("Invalid image header in '%s'", image_file_name); |
| 164 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 165 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 166 | |
| 167 | // Note: The image header is part of the image due to mmap page alignment required of offset. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 168 | UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(), |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 169 | image_header.GetImageSize(), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 170 | PROT_READ | PROT_WRITE, |
| 171 | MAP_PRIVATE | MAP_FIXED, |
| 172 | file->Fd(), |
| 173 | 0, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 174 | false, |
| 175 | image_file_name, |
| 176 | error_msg)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 177 | if (map.get() == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 178 | DCHECK(!error_msg->empty()); |
| 179 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 180 | } |
| 181 | CHECK_EQ(image_header.GetImageBegin(), map->Begin()); |
| 182 | DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader))); |
| 183 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 184 | UniquePtr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(), |
| 185 | PROT_READ, MAP_PRIVATE, |
| 186 | file->Fd(), image_header.GetBitmapOffset(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 187 | false, |
| 188 | image_file_name, |
| 189 | error_msg)); |
| 190 | if (image_map.get() == nullptr) { |
| 191 | *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str()); |
| 192 | return nullptr; |
| 193 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 194 | uint32_t bitmap_index = bitmap_index_.FetchAndAdd(1); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 195 | std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_file_name, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 196 | bitmap_index)); |
| 197 | UniquePtr<accounting::SpaceBitmap> bitmap( |
| 198 | accounting::SpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(), |
| 199 | reinterpret_cast<byte*>(map->Begin()), |
| 200 | map->Size())); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 201 | if (bitmap.get() == nullptr) { |
| 202 | *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str()); |
| 203 | return nullptr; |
| 204 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 205 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 206 | Runtime* runtime = Runtime::Current(); |
| 207 | mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 208 | runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method)); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 209 | mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod); |
| 210 | runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method)); |
| 211 | mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt); |
| 212 | runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 213 | |
| 214 | mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 215 | runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kSaveAll); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 216 | callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 217 | runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsOnly); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 218 | callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 219 | runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsAndArgs); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 220 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 221 | UniquePtr<ImageSpace> space(new ImageSpace(image_file_name, map.release(), bitmap.release())); |
| 222 | if (kIsDebugBuild) { |
| 223 | space->VerifyImageAllocations(); |
| 224 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 225 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 226 | space->oat_file_.reset(space->OpenOatFile(image_file_name, error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 227 | if (space->oat_file_.get() == nullptr) { |
| 228 | DCHECK(!error_msg->empty()); |
| 229 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 230 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 231 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 232 | if (validate_oat_file && !space->ValidateOatFile(error_msg)) { |
| 233 | DCHECK(!error_msg->empty()); |
| 234 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 238 | LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time) |
| 239 | << ") " << *space.get(); |
| 240 | } |
| 241 | return space.release(); |
| 242 | } |
| 243 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 244 | OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 245 | const ImageHeader& image_header = GetImageHeader(); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 246 | std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path); |
| 247 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 248 | OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 249 | !Runtime::Current()->IsCompiler(), error_msg); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 250 | if (oat_file == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 251 | *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s", |
| 252 | oat_filename.c_str(), GetName(), error_msg->c_str()); |
| 253 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 254 | } |
| 255 | uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum(); |
| 256 | uint32_t image_oat_checksum = image_header.GetOatChecksum(); |
| 257 | if (oat_checksum != image_oat_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 258 | *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x" |
| 259 | " in image %s", oat_checksum, image_oat_checksum, GetName()); |
| 260 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 261 | } |
| 262 | return oat_file; |
| 263 | } |
| 264 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 265 | bool ImageSpace::ValidateOatFile(std::string* error_msg) const { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 266 | CHECK(oat_file_.get() != NULL); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 267 | for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 268 | const std::string& dex_file_location = oat_dex_file->GetDexFileLocation(); |
| 269 | uint32_t dex_file_location_checksum; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 270 | if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) { |
| 271 | *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: " |
| 272 | "%s", dex_file_location.c_str(), GetName(), error_msg->c_str()); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 273 | return false; |
| 274 | } |
| 275 | if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 276 | *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and " |
| 277 | "dex file '%s' (0x%x != 0x%x)", |
| 278 | oat_file_->GetLocation().c_str(), dex_file_location.c_str(), |
| 279 | oat_dex_file->GetDexFileLocationChecksum(), |
| 280 | dex_file_location_checksum); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | return true; |
| 285 | } |
| 286 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 287 | OatFile* ImageSpace::ReleaseOatFile() { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 288 | CHECK(oat_file_.get() != NULL); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 289 | return oat_file_.release(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 292 | void ImageSpace::Dump(std::ostream& os) const { |
| 293 | os << GetType() |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 294 | << " begin=" << reinterpret_cast<void*>(Begin()) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 295 | << ",end=" << reinterpret_cast<void*>(End()) |
| 296 | << ",size=" << PrettySize(Size()) |
| 297 | << ",name=\"" << GetName() << "\"]"; |
| 298 | } |
| 299 | |
| 300 | } // namespace space |
| 301 | } // namespace gc |
| 302 | } // namespace art |