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 <sys/types.h> |
| 20 | #include <sys/wait.h> |
| 21 | |
| 22 | #include "base/stl_util.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 23 | #include "base/unix_file/fd_file.h" |
| 24 | #include "gc/accounting/space_bitmap-inl.h" |
| 25 | #include "mirror/abstract_method.h" |
| 26 | #include "mirror/class-inl.h" |
| 27 | #include "mirror/object-inl.h" |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame^] | 28 | #include "oat_file.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | #include "os.h" |
| 30 | #include "runtime.h" |
| 31 | #include "space-inl.h" |
| 32 | #include "utils.h" |
| 33 | |
| 34 | namespace art { |
| 35 | namespace gc { |
| 36 | namespace space { |
| 37 | |
| 38 | size_t ImageSpace::bitmap_index_ = 0; |
| 39 | |
| 40 | ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map) |
| 41 | : MemMapSpace(name, mem_map, mem_map->Size(), kGcRetentionPolicyNeverCollect) { |
| 42 | const size_t bitmap_index = bitmap_index_++; |
| 43 | live_bitmap_.reset(accounting::SpaceBitmap::Create( |
| 44 | StringPrintf("imagespace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)), |
| 45 | Begin(), Capacity())); |
| 46 | DCHECK(live_bitmap_.get() != NULL) << "could not create imagespace live bitmap #" << bitmap_index; |
| 47 | } |
| 48 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame^] | 49 | static bool GenerateImage(const std::string& image_file_name) { |
| 50 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
| 51 | std::vector<std::string> boot_class_path; |
| 52 | Split(boot_class_path_string, ':', boot_class_path); |
| 53 | if (boot_class_path.empty()) { |
| 54 | LOG(FATAL) << "Failed to generate image because no boot class path specified"; |
| 55 | } |
| 56 | |
| 57 | std::vector<char*> arg_vector; |
| 58 | |
| 59 | std::string dex2oat_string(GetAndroidRoot()); |
| 60 | dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); |
| 61 | const char* dex2oat = dex2oat_string.c_str(); |
| 62 | arg_vector.push_back(strdup(dex2oat)); |
| 63 | |
| 64 | std::string image_option_string("--image="); |
| 65 | image_option_string += image_file_name; |
| 66 | const char* image_option = image_option_string.c_str(); |
| 67 | arg_vector.push_back(strdup(image_option)); |
| 68 | |
| 69 | arg_vector.push_back(strdup("--runtime-arg")); |
| 70 | arg_vector.push_back(strdup("-Xms64m")); |
| 71 | |
| 72 | arg_vector.push_back(strdup("--runtime-arg")); |
| 73 | arg_vector.push_back(strdup("-Xmx64m")); |
| 74 | |
| 75 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 76 | std::string dex_file_option_string("--dex-file="); |
| 77 | dex_file_option_string += boot_class_path[i]; |
| 78 | const char* dex_file_option = dex_file_option_string.c_str(); |
| 79 | arg_vector.push_back(strdup(dex_file_option)); |
| 80 | } |
| 81 | |
| 82 | std::string oat_file_option_string("--oat-file="); |
| 83 | oat_file_option_string += image_file_name; |
| 84 | oat_file_option_string.erase(oat_file_option_string.size() - 3); |
| 85 | oat_file_option_string += "oat"; |
| 86 | const char* oat_file_option = oat_file_option_string.c_str(); |
| 87 | arg_vector.push_back(strdup(oat_file_option)); |
| 88 | |
| 89 | std::string base_option_string(StringPrintf("--base=0x%x", ART_BASE_ADDRESS)); |
| 90 | arg_vector.push_back(strdup(base_option_string.c_str())); |
| 91 | |
| 92 | if (kIsTargetBuild) { |
| 93 | arg_vector.push_back(strdup("--image-classes-zip=/system/framework/framework.jar")); |
| 94 | arg_vector.push_back(strdup("--image-classes=preloaded-classes")); |
| 95 | } else { |
| 96 | arg_vector.push_back(strdup("--host")); |
| 97 | } |
| 98 | |
| 99 | std::string command_line(Join(arg_vector, ' ')); |
| 100 | LOG(INFO) << "GenerateImage: " << command_line; |
| 101 | |
| 102 | arg_vector.push_back(NULL); |
| 103 | char** argv = &arg_vector[0]; |
| 104 | |
| 105 | // fork and exec dex2oat |
| 106 | pid_t pid = fork(); |
| 107 | if (pid == 0) { |
| 108 | // no allocation allowed between fork and exec |
| 109 | |
| 110 | // change process groups, so we don't get reaped by ProcessManager |
| 111 | setpgid(0, 0); |
| 112 | |
| 113 | execv(dex2oat, argv); |
| 114 | |
| 115 | PLOG(FATAL) << "execv(" << dex2oat << ") failed"; |
| 116 | return false; |
| 117 | } else { |
| 118 | STLDeleteElements(&arg_vector); |
| 119 | |
| 120 | // wait for dex2oat to finish |
| 121 | int status; |
| 122 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 123 | if (got_pid != pid) { |
| 124 | PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 125 | return false; |
| 126 | } |
| 127 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 128 | LOG(ERROR) << dex2oat << " failed: " << command_line; |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | ImageSpace* ImageSpace::Create(const std::string& original_image_file_name) { |
| 136 | if (OS::FileExists(original_image_file_name.c_str())) { |
| 137 | // If the /system file exists, it should be up-to-date, don't try to generate |
| 138 | return space::ImageSpace::Init(original_image_file_name, false); |
| 139 | } |
| 140 | // If the /system file didn't exist, we need to use one from the dalvik-cache. |
| 141 | // If the cache file exists, try to open, but if it fails, regenerate. |
| 142 | // If it does not exist, generate. |
| 143 | std::string image_file_name(GetDalvikCacheFilenameOrDie(original_image_file_name)); |
| 144 | if (OS::FileExists(image_file_name.c_str())) { |
| 145 | space::ImageSpace* image_space = space::ImageSpace::Init(image_file_name, true); |
| 146 | if (image_space != NULL) { |
| 147 | return image_space; |
| 148 | } |
| 149 | } |
| 150 | CHECK(GenerateImage(image_file_name)) << "Failed to generate image: " << image_file_name; |
| 151 | return space::ImageSpace::Init(image_file_name, true); |
| 152 | } |
| 153 | |
| 154 | ImageSpace* ImageSpace::Init(const std::string& image_file_name, bool validate_oat_file) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 155 | CHECK(!image_file_name.empty()); |
| 156 | |
| 157 | uint64_t start_time = 0; |
| 158 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 159 | start_time = NanoTime(); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame^] | 160 | LOG(INFO) << "ImageSpace::Init entering image_file_name=" << image_file_name; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | UniquePtr<File> file(OS::OpenFile(image_file_name.c_str(), false)); |
| 164 | if (file.get() == NULL) { |
| 165 | LOG(ERROR) << "Failed to open " << image_file_name; |
| 166 | return NULL; |
| 167 | } |
| 168 | ImageHeader image_header; |
| 169 | bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 170 | if (!success || !image_header.IsValid()) { |
| 171 | LOG(ERROR) << "Invalid image header " << image_file_name; |
| 172 | return NULL; |
| 173 | } |
| 174 | UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(), |
| 175 | file->GetLength(), |
| 176 | PROT_READ | PROT_WRITE, |
| 177 | MAP_PRIVATE | MAP_FIXED, |
| 178 | file->Fd(), |
| 179 | 0, |
| 180 | false)); |
| 181 | if (map.get() == NULL) { |
| 182 | LOG(ERROR) << "Failed to map " << image_file_name; |
| 183 | return NULL; |
| 184 | } |
| 185 | CHECK_EQ(image_header.GetImageBegin(), map->Begin()); |
| 186 | DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader))); |
| 187 | |
| 188 | Runtime* runtime = Runtime::Current(); |
| 189 | mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod); |
| 190 | runtime->SetResolutionMethod(down_cast<mirror::AbstractMethod*>(resolution_method)); |
| 191 | |
| 192 | mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod); |
| 193 | runtime->SetCalleeSaveMethod(down_cast<mirror::AbstractMethod*>(callee_save_method), Runtime::kSaveAll); |
| 194 | callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod); |
| 195 | runtime->SetCalleeSaveMethod(down_cast<mirror::AbstractMethod*>(callee_save_method), Runtime::kRefsOnly); |
| 196 | callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod); |
| 197 | runtime->SetCalleeSaveMethod(down_cast<mirror::AbstractMethod*>(callee_save_method), Runtime::kRefsAndArgs); |
| 198 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame^] | 199 | UniquePtr<ImageSpace> space(new ImageSpace(image_file_name, map.release())); |
| 200 | |
| 201 | space->oat_file_.reset(space->OpenOatFile()); |
| 202 | if (space->oat_file_.get() == NULL) { |
| 203 | LOG(ERROR) << "Failed to open oat file for image: " << image_file_name; |
| 204 | return NULL; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 205 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame^] | 206 | |
| 207 | if (validate_oat_file && !space->ValidateOatFile()) { |
| 208 | LOG(WARNING) << "Failed to validate oat file for image: " << image_file_name; |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 213 | LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time) |
| 214 | << ") " << *space.get(); |
| 215 | } |
| 216 | return space.release(); |
| 217 | } |
| 218 | |
| 219 | OatFile* ImageSpace::OpenOatFile() const { |
| 220 | const Runtime* runtime = Runtime::Current(); |
| 221 | const ImageHeader& image_header = GetImageHeader(); |
| 222 | // Grab location but don't use Object::AsString as we haven't yet initialized the roots to |
| 223 | // check the down cast |
| 224 | mirror::String* oat_location = |
| 225 | down_cast<mirror::String*>(image_header.GetImageRoot(ImageHeader::kOatLocation)); |
| 226 | std::string oat_filename; |
| 227 | oat_filename += runtime->GetHostPrefix(); |
| 228 | oat_filename += oat_location->ToModifiedUtf8(); |
| 229 | OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(), |
| 230 | !Runtime::Current()->IsCompiler()); |
| 231 | if (oat_file == NULL) { |
| 232 | LOG(ERROR) << "Failed to open oat file " << oat_filename << " referenced from image."; |
| 233 | return NULL; |
| 234 | } |
| 235 | uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum(); |
| 236 | uint32_t image_oat_checksum = image_header.GetOatChecksum(); |
| 237 | if (oat_checksum != image_oat_checksum) { |
| 238 | LOG(ERROR) << "Failed to match oat file checksum " << std::hex << oat_checksum |
| 239 | << " to expected oat checksum " << std::hex << image_oat_checksum |
| 240 | << " in image"; |
| 241 | return NULL; |
| 242 | } |
| 243 | return oat_file; |
| 244 | } |
| 245 | |
| 246 | bool ImageSpace::ValidateOatFile() const { |
| 247 | CHECK(oat_file_.get() != NULL); |
| 248 | std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles(); |
| 249 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 250 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i]; |
| 251 | const std::string& dex_file_location = oat_dex_file->GetDexFileLocation(); |
| 252 | uint32_t dex_file_location_checksum; |
| 253 | if (!DexFile::GetChecksum(dex_file_location.c_str(), dex_file_location_checksum)) { |
| 254 | LOG(WARNING) << "ValidateOatFile could not find checksum for " << dex_file_location; |
| 255 | return false; |
| 256 | } |
| 257 | if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) { |
| 258 | LOG(WARNING) << "ValidateOatFile found checksum mismatch between oat file " |
| 259 | << oat_file_->GetLocation() << " and dex file " << dex_file_location |
| 260 | << " (" << oat_dex_file->GetDexFileLocationChecksum() << " != " |
| 261 | << dex_file_location_checksum << ")"; |
| 262 | return false; |
| 263 | } |
| 264 | } |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | OatFile& ImageSpace::ReleaseOatFile() { |
| 269 | CHECK(oat_file_.get() != NULL); |
| 270 | return *oat_file_.release(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void ImageSpace::RecordImageAllocations(accounting::SpaceBitmap* live_bitmap) const { |
| 274 | uint64_t start_time = 0; |
| 275 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 276 | LOG(INFO) << "ImageSpace::RecordImageAllocations entering"; |
| 277 | start_time = NanoTime(); |
| 278 | } |
| 279 | DCHECK(!Runtime::Current()->IsStarted()); |
| 280 | CHECK(live_bitmap != NULL); |
| 281 | byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
| 282 | byte* end = End(); |
| 283 | while (current < end) { |
| 284 | DCHECK_ALIGNED(current, kObjectAlignment); |
| 285 | const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(current); |
| 286 | live_bitmap->Set(obj); |
| 287 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
| 288 | } |
| 289 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 290 | LOG(INFO) << "ImageSpace::RecordImageAllocations exiting (" |
| 291 | << PrettyDuration(NanoTime() - start_time) << ")"; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void ImageSpace::Dump(std::ostream& os) const { |
| 296 | os << GetType() |
| 297 | << "begin=" << reinterpret_cast<void*>(Begin()) |
| 298 | << ",end=" << reinterpret_cast<void*>(End()) |
| 299 | << ",size=" << PrettySize(Size()) |
| 300 | << ",name=\"" << GetName() << "\"]"; |
| 301 | } |
| 302 | |
| 303 | } // namespace space |
| 304 | } // namespace gc |
| 305 | } // namespace art |