blob: c6177bd01d131a23ffb8db5ddda91dc0b7e6781c [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
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 Carlstrom56d947f2013-07-15 13:14:23 -070019#include <sys/types.h>
20#include <sys/wait.h>
21
22#include "base/stl_util.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "base/unix_file/fd_file.h"
24#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "mirror/art_method.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "mirror/class-inl.h"
27#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070028#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "os.h"
30#include "runtime.h"
31#include "space-inl.h"
32#include "utils.h"
33
34namespace art {
35namespace gc {
36namespace space {
37
Mathieu Chartier31e89252013-08-28 11:29:12 -070038AtomicInteger ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070039
Mathieu Chartier31e89252013-08-28 11:29:12 -070040ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map,
41 accounting::SpaceBitmap* live_bitmap)
Mathieu Chartier590fee92013-09-13 13:46:47 -070042 : MemMapSpace(name, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(),
43 kGcRetentionPolicyNeverCollect) {
44 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070045 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070046}
47
Ian Rogers8d31bbd2013-10-13 10:44:14 -070048static bool GenerateImage(const std::string& image_file_name, std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070049 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
50 std::vector<std::string> boot_class_path;
51 Split(boot_class_path_string, ':', boot_class_path);
52 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070053 *error_msg = "Failed to generate image because no boot class path specified";
54 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070055 }
56
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070057 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070058
Mathieu Chartiere5426c92013-08-01 13:55:42 -070059 std::string dex2oat(GetAndroidRoot());
Mathieu Chartier08d7d442013-07-31 18:08:51 -070060 dex2oat += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat");
61 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070062
63 std::string image_option_string("--image=");
64 image_option_string += image_file_name;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070065 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070066
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070067 arg_vector.push_back("--runtime-arg");
68 arg_vector.push_back("-Xms64m");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070069
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070070 arg_vector.push_back("--runtime-arg");
71 arg_vector.push_back("-Xmx64m");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070072
73 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070074 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070075 }
76
77 std::string oat_file_option_string("--oat-file=");
78 oat_file_option_string += image_file_name;
79 oat_file_option_string.erase(oat_file_option_string.size() - 3);
80 oat_file_option_string += "oat";
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070081 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070082
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070083 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS));
Brian Carlstrom56d947f2013-07-15 13:14:23 -070084
85 if (kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070086 arg_vector.push_back("--image-classes-zip=/system/framework/framework.jar");
87 arg_vector.push_back("--image-classes=preloaded-classes");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070088 } else {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070089 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070090 }
91
92 std::string command_line(Join(arg_vector, ' '));
93 LOG(INFO) << "GenerateImage: " << command_line;
94
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070095 // Convert the args to char pointers.
96 std::vector<char*> char_args;
97 for (std::vector<std::string>::iterator it = arg_vector.begin(); it != arg_vector.end();
98 ++it) {
99 char_args.push_back(const_cast<char*>(it->c_str()));
100 }
101 char_args.push_back(NULL);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700102
103 // fork and exec dex2oat
104 pid_t pid = fork();
105 if (pid == 0) {
106 // no allocation allowed between fork and exec
107
108 // change process groups, so we don't get reaped by ProcessManager
109 setpgid(0, 0);
110
Mathieu Chartier08d7d442013-07-31 18:08:51 -0700111 execv(dex2oat.c_str(), &char_args[0]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700112
113 PLOG(FATAL) << "execv(" << dex2oat << ") failed";
114 return false;
115 } else {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700116 if (pid == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117 *error_msg = StringPrintf("Failed to generate image '%s' because fork failed: %s",
118 image_file_name.c_str(), strerror(errno));
119 return false;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700120 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700121
122 // wait for dex2oat to finish
123 int status;
124 pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
125 if (got_pid != pid) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700126 *error_msg = StringPrintf("Failed to generate image '%s' because waitpid failed: "
127 "wanted %d, got %d: %s",
128 image_file_name.c_str(), pid, got_pid, strerror(errno));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700129 return false;
130 }
131 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700132 *error_msg = StringPrintf("Failed to generate image '%s' because dex2oat failed: %s",
133 image_file_name.c_str(), command_line.c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700134 return false;
135 }
136 }
137 return true;
138}
139
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700140ImageSpace* ImageSpace::Create(const char* original_image_file_name) {
141 if (OS::FileExists(original_image_file_name)) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700142 // If the /system file exists, it should be up-to-date, don't try to generate
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700143 std::string error_msg;
144 ImageSpace* space = ImageSpace::Init(original_image_file_name, false, &error_msg);
145 if (space == nullptr) {
146 LOG(FATAL) << "Failed to load image '" << original_image_file_name << "': " << error_msg;
147 }
148 return space;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700149 }
150 // If the /system file didn't exist, we need to use one from the dalvik-cache.
151 // If the cache file exists, try to open, but if it fails, regenerate.
152 // If it does not exist, generate.
153 std::string image_file_name(GetDalvikCacheFilenameOrDie(original_image_file_name));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700154 std::string error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700155 if (OS::FileExists(image_file_name.c_str())) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700156 space::ImageSpace* image_space = ImageSpace::Init(image_file_name.c_str(), true, &error_msg);
157 if (image_space != nullptr) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700158 return image_space;
159 }
160 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700161 CHECK(GenerateImage(image_file_name, &error_msg))
162 << "Failed to generate image '" << image_file_name << "': " << error_msg;
163 ImageSpace* space = ImageSpace::Init(image_file_name.c_str(), true, &error_msg);
164 if (space == nullptr) {
165 LOG(FATAL) << "Failed to load image '" << original_image_file_name << "': " << error_msg;
166 }
167 return space;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700168}
169
Mathieu Chartier31e89252013-08-28 11:29:12 -0700170void ImageSpace::VerifyImageAllocations() {
171 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
172 while (current < End()) {
173 DCHECK_ALIGNED(current, kObjectAlignment);
174 const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(current);
175 CHECK(live_bitmap_->Test(obj));
176 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
177 current += RoundUp(obj->SizeOf(), kObjectAlignment);
178 }
179}
180
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700181ImageSpace* ImageSpace::Init(const char* image_file_name, bool validate_oat_file,
182 std::string* error_msg) {
183 CHECK(image_file_name != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700184
185 uint64_t start_time = 0;
186 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
187 start_time = NanoTime();
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700188 LOG(INFO) << "ImageSpace::Init entering image_file_name=" << image_file_name;
Ian Rogers1d54e732013-05-02 21:10:01 -0700189 }
190
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700191 UniquePtr<File> file(OS::OpenFileForReading(image_file_name));
Ian Rogers1d54e732013-05-02 21:10:01 -0700192 if (file.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700193 *error_msg = StringPrintf("Failed to open '%s'", image_file_name);
194 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700195 }
196 ImageHeader image_header;
197 bool success = file->ReadFully(&image_header, sizeof(image_header));
198 if (!success || !image_header.IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700199 *error_msg = StringPrintf("Invalid image header in '%s'", image_file_name);
200 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700201 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700202
203 // Note: The image header is part of the image due to mmap page alignment required of offset.
Ian Rogers1d54e732013-05-02 21:10:01 -0700204 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700205 image_header.GetImageSize(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700206 PROT_READ | PROT_WRITE,
207 MAP_PRIVATE | MAP_FIXED,
208 file->Fd(),
209 0,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700210 false,
211 image_file_name,
212 error_msg));
Ian Rogers1d54e732013-05-02 21:10:01 -0700213 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700214 DCHECK(!error_msg->empty());
215 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700216 }
217 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
218 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
219
Mathieu Chartier31e89252013-08-28 11:29:12 -0700220 UniquePtr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
221 PROT_READ, MAP_PRIVATE,
222 file->Fd(), image_header.GetBitmapOffset(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700223 false,
224 image_file_name,
225 error_msg));
226 if (image_map.get() == nullptr) {
227 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
228 return nullptr;
229 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700230 size_t bitmap_index = bitmap_index_.fetch_add(1);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700231 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_file_name,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700232 bitmap_index));
233 UniquePtr<accounting::SpaceBitmap> bitmap(
234 accounting::SpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
235 reinterpret_cast<byte*>(map->Begin()),
236 map->Size()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700237 if (bitmap.get() == nullptr) {
238 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
239 return nullptr;
240 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700241
Ian Rogers1d54e732013-05-02 21:10:01 -0700242 Runtime* runtime = Runtime::Current();
243 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700244 runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
Jeff Hao88474b42013-10-23 16:24:40 -0700245 mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod);
246 runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method));
247 mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt);
248 runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt));
Ian Rogers1d54e732013-05-02 21:10:01 -0700249
250 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700251 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kSaveAll);
Ian Rogers1d54e732013-05-02 21:10:01 -0700252 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700253 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsOnly);
Ian Rogers1d54e732013-05-02 21:10:01 -0700254 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700255 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsAndArgs);
Ian Rogers1d54e732013-05-02 21:10:01 -0700256
Mathieu Chartier31e89252013-08-28 11:29:12 -0700257 UniquePtr<ImageSpace> space(new ImageSpace(image_file_name, map.release(), bitmap.release()));
258 if (kIsDebugBuild) {
259 space->VerifyImageAllocations();
260 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700261
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700262 space->oat_file_.reset(space->OpenOatFile(error_msg));
263 if (space->oat_file_.get() == nullptr) {
264 DCHECK(!error_msg->empty());
265 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700266 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700267
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700268 if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
269 DCHECK(!error_msg->empty());
270 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700271 }
272
273 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
274 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
275 << ") " << *space.get();
276 }
277 return space.release();
278}
279
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700280OatFile* ImageSpace::OpenOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700281 const Runtime* runtime = Runtime::Current();
282 const ImageHeader& image_header = GetImageHeader();
283 // Grab location but don't use Object::AsString as we haven't yet initialized the roots to
284 // check the down cast
285 mirror::String* oat_location =
286 down_cast<mirror::String*>(image_header.GetImageRoot(ImageHeader::kOatLocation));
287 std::string oat_filename;
288 oat_filename += runtime->GetHostPrefix();
289 oat_filename += oat_location->ToModifiedUtf8();
290 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700291 !Runtime::Current()->IsCompiler(), error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700292 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700293 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
294 oat_filename.c_str(), GetName(), error_msg->c_str());
295 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700296 }
297 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
298 uint32_t image_oat_checksum = image_header.GetOatChecksum();
299 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700300 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
301 " in image %s", oat_checksum, image_oat_checksum, GetName());
302 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700303 }
304 return oat_file;
305}
306
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700307bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700308 CHECK(oat_file_.get() != NULL);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700309 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700310 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
311 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700312 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
313 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
314 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700315 return false;
316 }
317 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700318 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
319 "dex file '%s' (0x%x != 0x%x)",
320 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
321 oat_dex_file->GetDexFileLocationChecksum(),
322 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700323 return false;
324 }
325 }
326 return true;
327}
328
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700329OatFile* ImageSpace::ReleaseOatFile() {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700330 CHECK(oat_file_.get() != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700331 return oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700332}
333
Ian Rogers1d54e732013-05-02 21:10:01 -0700334void ImageSpace::Dump(std::ostream& os) const {
335 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700336 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -0700337 << ",end=" << reinterpret_cast<void*>(End())
338 << ",size=" << PrettySize(Size())
339 << ",name=\"" << GetName() << "\"]";
340}
341
342} // namespace space
343} // namespace gc
344} // namespace art