blob: fa28642c3e546ae1a879e5cc8c67d41bb69474be [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)
42 : MemMapSpace(name, mem_map, mem_map->Size(), kGcRetentionPolicyNeverCollect) {
43 DCHECK(live_bitmap != NULL);
44 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070045}
46
Ian Rogers8d31bbd2013-10-13 10:44:14 -070047static bool GenerateImage(const std::string& image_file_name, std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070048 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
49 std::vector<std::string> boot_class_path;
50 Split(boot_class_path_string, ':', boot_class_path);
51 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070052 *error_msg = "Failed to generate image because no boot class path specified";
53 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070054 }
55
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070056 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070057
Mathieu Chartiere5426c92013-08-01 13:55:42 -070058 std::string dex2oat(GetAndroidRoot());
Mathieu Chartier08d7d442013-07-31 18:08:51 -070059 dex2oat += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat");
60 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070061
62 std::string image_option_string("--image=");
63 image_option_string += image_file_name;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070064 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070065
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070066 arg_vector.push_back("--runtime-arg");
67 arg_vector.push_back("-Xms64m");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070068
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070069 arg_vector.push_back("--runtime-arg");
70 arg_vector.push_back("-Xmx64m");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070071
72 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070073 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070074 }
75
76 std::string oat_file_option_string("--oat-file=");
77 oat_file_option_string += image_file_name;
78 oat_file_option_string.erase(oat_file_option_string.size() - 3);
79 oat_file_option_string += "oat";
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070080 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070081
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070082 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS));
Brian Carlstrom56d947f2013-07-15 13:14:23 -070083
84 if (kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070085 arg_vector.push_back("--image-classes-zip=/system/framework/framework.jar");
86 arg_vector.push_back("--image-classes=preloaded-classes");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070087 } else {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070088 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070089 }
90
91 std::string command_line(Join(arg_vector, ' '));
92 LOG(INFO) << "GenerateImage: " << command_line;
93
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070094 // Convert the args to char pointers.
95 std::vector<char*> char_args;
96 for (std::vector<std::string>::iterator it = arg_vector.begin(); it != arg_vector.end();
97 ++it) {
98 char_args.push_back(const_cast<char*>(it->c_str()));
99 }
100 char_args.push_back(NULL);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700101
102 // fork and exec dex2oat
103 pid_t pid = fork();
104 if (pid == 0) {
105 // no allocation allowed between fork and exec
106
107 // change process groups, so we don't get reaped by ProcessManager
108 setpgid(0, 0);
109
Mathieu Chartier08d7d442013-07-31 18:08:51 -0700110 execv(dex2oat.c_str(), &char_args[0]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700111
112 PLOG(FATAL) << "execv(" << dex2oat << ") failed";
113 return false;
114 } else {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700115 if (pid == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700116 *error_msg = StringPrintf("Failed to generate image '%s' because fork failed: %s",
117 image_file_name.c_str(), strerror(errno));
118 return false;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700119 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700120
121 // wait for dex2oat to finish
122 int status;
123 pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
124 if (got_pid != pid) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700125 *error_msg = StringPrintf("Failed to generate image '%s' because waitpid failed: "
126 "wanted %d, got %d: %s",
127 image_file_name.c_str(), pid, got_pid, strerror(errno));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700128 return false;
129 }
130 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700131 *error_msg = StringPrintf("Failed to generate image '%s' because dex2oat failed: %s",
132 image_file_name.c_str(), command_line.c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700133 return false;
134 }
135 }
136 return true;
137}
138
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700139ImageSpace* ImageSpace::Create(const char* original_image_file_name) {
140 if (OS::FileExists(original_image_file_name)) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700141 // If the /system file exists, it should be up-to-date, don't try to generate
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700142 std::string error_msg;
143 ImageSpace* space = ImageSpace::Init(original_image_file_name, false, &error_msg);
144 if (space == nullptr) {
145 LOG(FATAL) << "Failed to load image '" << original_image_file_name << "': " << error_msg;
146 }
147 return space;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700148 }
149 // If the /system file didn't exist, we need to use one from the dalvik-cache.
150 // If the cache file exists, try to open, but if it fails, regenerate.
151 // If it does not exist, generate.
152 std::string image_file_name(GetDalvikCacheFilenameOrDie(original_image_file_name));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700153 std::string error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700154 if (OS::FileExists(image_file_name.c_str())) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700155 space::ImageSpace* image_space = ImageSpace::Init(image_file_name.c_str(), true, &error_msg);
156 if (image_space != nullptr) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700157 return image_space;
158 }
159 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700160 CHECK(GenerateImage(image_file_name, &error_msg))
161 << "Failed to generate image '" << image_file_name << "': " << error_msg;
162 ImageSpace* space = ImageSpace::Init(image_file_name.c_str(), true, &error_msg);
163 if (space == nullptr) {
164 LOG(FATAL) << "Failed to load image '" << original_image_file_name << "': " << error_msg;
165 }
166 return space;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700167}
168
Mathieu Chartier31e89252013-08-28 11:29:12 -0700169void ImageSpace::VerifyImageAllocations() {
170 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
171 while (current < End()) {
172 DCHECK_ALIGNED(current, kObjectAlignment);
173 const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(current);
174 CHECK(live_bitmap_->Test(obj));
175 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
176 current += RoundUp(obj->SizeOf(), kObjectAlignment);
177 }
178}
179
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700180ImageSpace* ImageSpace::Init(const char* image_file_name, bool validate_oat_file,
181 std::string* error_msg) {
182 CHECK(image_file_name != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700183
184 uint64_t start_time = 0;
185 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
186 start_time = NanoTime();
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700187 LOG(INFO) << "ImageSpace::Init entering image_file_name=" << image_file_name;
Ian Rogers1d54e732013-05-02 21:10:01 -0700188 }
189
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700190 UniquePtr<File> file(OS::OpenFileForReading(image_file_name));
Ian Rogers1d54e732013-05-02 21:10:01 -0700191 if (file.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700192 *error_msg = StringPrintf("Failed to open '%s'", image_file_name);
193 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700194 }
195 ImageHeader image_header;
196 bool success = file->ReadFully(&image_header, sizeof(image_header));
197 if (!success || !image_header.IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700198 *error_msg = StringPrintf("Invalid image header in '%s'", image_file_name);
199 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700200 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700201
202 // Note: The image header is part of the image due to mmap page alignment required of offset.
Ian Rogers1d54e732013-05-02 21:10:01 -0700203 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700204 image_header.GetImageSize(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700205 PROT_READ | PROT_WRITE,
206 MAP_PRIVATE | MAP_FIXED,
207 file->Fd(),
208 0,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700209 false,
210 image_file_name,
211 error_msg));
Ian Rogers1d54e732013-05-02 21:10:01 -0700212 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700213 DCHECK(!error_msg->empty());
214 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700215 }
216 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
217 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
218
Mathieu Chartier31e89252013-08-28 11:29:12 -0700219 UniquePtr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
220 PROT_READ, MAP_PRIVATE,
221 file->Fd(), image_header.GetBitmapOffset(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700222 false,
223 image_file_name,
224 error_msg));
225 if (image_map.get() == nullptr) {
226 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
227 return nullptr;
228 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700229 size_t bitmap_index = bitmap_index_.fetch_add(1);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700230 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_file_name,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700231 bitmap_index));
232 UniquePtr<accounting::SpaceBitmap> bitmap(
233 accounting::SpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
234 reinterpret_cast<byte*>(map->Begin()),
235 map->Size()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700236 if (bitmap.get() == nullptr) {
237 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
238 return nullptr;
239 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700240
Ian Rogers1d54e732013-05-02 21:10:01 -0700241 Runtime* runtime = Runtime::Current();
242 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700243 runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
Ian Rogers1d54e732013-05-02 21:10:01 -0700244
245 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700246 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kSaveAll);
Ian Rogers1d54e732013-05-02 21:10:01 -0700247 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700248 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsOnly);
Ian Rogers1d54e732013-05-02 21:10:01 -0700249 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700250 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsAndArgs);
Ian Rogers1d54e732013-05-02 21:10:01 -0700251
Mathieu Chartier31e89252013-08-28 11:29:12 -0700252 UniquePtr<ImageSpace> space(new ImageSpace(image_file_name, map.release(), bitmap.release()));
253 if (kIsDebugBuild) {
254 space->VerifyImageAllocations();
255 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700256
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700257 space->oat_file_.reset(space->OpenOatFile(error_msg));
258 if (space->oat_file_.get() == nullptr) {
259 DCHECK(!error_msg->empty());
260 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700261 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700262
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700263 if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
264 DCHECK(!error_msg->empty());
265 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700266 }
267
268 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
269 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
270 << ") " << *space.get();
271 }
272 return space.release();
273}
274
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700275OatFile* ImageSpace::OpenOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700276 const Runtime* runtime = Runtime::Current();
277 const ImageHeader& image_header = GetImageHeader();
278 // Grab location but don't use Object::AsString as we haven't yet initialized the roots to
279 // check the down cast
280 mirror::String* oat_location =
281 down_cast<mirror::String*>(image_header.GetImageRoot(ImageHeader::kOatLocation));
282 std::string oat_filename;
283 oat_filename += runtime->GetHostPrefix();
284 oat_filename += oat_location->ToModifiedUtf8();
285 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700286 !Runtime::Current()->IsCompiler(), error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700287 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700288 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
289 oat_filename.c_str(), GetName(), error_msg->c_str());
290 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700291 }
292 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
293 uint32_t image_oat_checksum = image_header.GetOatChecksum();
294 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700295 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
296 " in image %s", oat_checksum, image_oat_checksum, GetName());
297 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700298 }
299 return oat_file;
300}
301
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700302bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700303 CHECK(oat_file_.get() != NULL);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700304 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700305 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
306 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700307 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
308 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
309 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700310 return false;
311 }
312 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700313 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
314 "dex file '%s' (0x%x != 0x%x)",
315 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
316 oat_dex_file->GetDexFileLocationChecksum(),
317 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700318 return false;
319 }
320 }
321 return true;
322}
323
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700324OatFile* ImageSpace::ReleaseOatFile() {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700325 CHECK(oat_file_.get() != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700326 return oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700327}
328
Ian Rogers1d54e732013-05-02 21:10:01 -0700329void ImageSpace::Dump(std::ostream& os) const {
330 os << GetType()
331 << "begin=" << reinterpret_cast<void*>(Begin())
332 << ",end=" << reinterpret_cast<void*>(End())
333 << ",size=" << PrettySize(Size())
334 << ",name=\"" << GetName() << "\"]";
335}
336
337} // namespace space
338} // namespace gc
339} // namespace art