blob: 9a2815a1df338e0996cf8b38eb76fc991c4b3252 [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 "base/stl_util.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070020#include "base/unix_file/fd_file.h"
21#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070022#include "mirror/art_method.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "mirror/class-inl.h"
24#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070025#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "os.h"
27#include "runtime.h"
28#include "space-inl.h"
29#include "utils.h"
30
31namespace art {
32namespace gc {
33namespace space {
34
Ian Rogersef7d42f2014-01-06 12:55:46 -080035Atomic<uint32_t> ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070036
Mathieu Chartier31e89252013-08-28 11:29:12 -070037ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map,
38 accounting::SpaceBitmap* live_bitmap)
Mathieu Chartier590fee92013-09-13 13:46:47 -070039 : MemMapSpace(name, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(),
40 kGcRetentionPolicyNeverCollect) {
41 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070042 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070043}
44
Ian Rogers8d31bbd2013-10-13 10:44:14 -070045static bool GenerateImage(const std::string& image_file_name, std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070046 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 Rogers8d31bbd2013-10-13 10:44:14 -070050 *error_msg = "Failed to generate image because no boot class path specified";
51 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070052 }
53
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070054 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070055
Mathieu Chartiere5426c92013-08-01 13:55:42 -070056 std::string dex2oat(GetAndroidRoot());
Mathieu Chartier08d7d442013-07-31 18:08:51 -070057 dex2oat += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat");
58 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070059
60 std::string image_option_string("--image=");
61 image_option_string += image_file_name;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070062 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070063
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070064 arg_vector.push_back("--runtime-arg");
65 arg_vector.push_back("-Xms64m");
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("-Xmx64m");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070069
Dave Allisonb373e092014-02-20 16:06:36 -080070 arg_vector.push_back("--runtime-arg");
71 std::string checkstr = "-implicit-checks";
72 int nchecks = 0;
73 char checksep = ':';
74
75 if (!Runtime::Current()->ExplicitNullChecks()) {
76 checkstr += checksep;
77 checksep = ',';
78 checkstr += "null";
79 ++nchecks;
80 }
81 if (!Runtime::Current()->ExplicitSuspendChecks()) {
82 checkstr += checksep;
83 checksep = ',';
84 checkstr += "suspend";
85 ++nchecks;
86 }
87
88 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
89 checkstr += checksep;
90 checksep = ',';
91 checkstr += "stack";
92 ++nchecks;
93 }
94
95 if (nchecks == 0) {
96 checkstr += ":none";
97 }
98
99 arg_vector.push_back(checkstr);
100
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700101 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700102 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700103 }
104
105 std::string oat_file_option_string("--oat-file=");
106 oat_file_option_string += image_file_name;
107 oat_file_option_string.erase(oat_file_option_string.size() - 3);
108 oat_file_option_string += "oat";
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700109 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700110
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700111 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700112
113 if (kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700114 arg_vector.push_back("--image-classes-zip=/system/framework/framework.jar");
115 arg_vector.push_back("--image-classes=preloaded-classes");
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700116 } else {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700117 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700118 }
119
Brian Carlstrom6449c622014-02-10 23:48:36 -0800120 const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800121 for (size_t i = 0; i < compiler_options.size(); ++i) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800122 arg_vector.push_back(compiler_options[i].c_str());
123 }
124
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700125 std::string command_line(Join(arg_vector, ' '));
126 LOG(INFO) << "GenerateImage: " << command_line;
Brian Carlstrom6449c622014-02-10 23:48:36 -0800127 return Exec(arg_vector, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700128}
129
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700130ImageSpace* ImageSpace::Create(const char* original_image_file_name) {
131 if (OS::FileExists(original_image_file_name)) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700132 // If the /system file exists, it should be up-to-date, don't try to generate
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700133 std::string error_msg;
134 ImageSpace* space = ImageSpace::Init(original_image_file_name, false, &error_msg);
135 if (space == nullptr) {
136 LOG(FATAL) << "Failed to load image '" << original_image_file_name << "': " << error_msg;
137 }
138 return space;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700139 }
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));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700144 std::string error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700145 if (OS::FileExists(image_file_name.c_str())) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700146 space::ImageSpace* image_space = ImageSpace::Init(image_file_name.c_str(), true, &error_msg);
147 if (image_space != nullptr) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700148 return image_space;
Mathieu Chartierc7cb1902014-03-05 14:41:03 -0800149 } else {
150 LOG(WARNING) << error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700151 }
152 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700153 CHECK(GenerateImage(image_file_name, &error_msg))
154 << "Failed to generate image '" << image_file_name << "': " << error_msg;
155 ImageSpace* space = ImageSpace::Init(image_file_name.c_str(), true, &error_msg);
156 if (space == nullptr) {
157 LOG(FATAL) << "Failed to load image '" << original_image_file_name << "': " << error_msg;
158 }
159 return space;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700160}
161
Mathieu Chartier31e89252013-08-28 11:29:12 -0700162void ImageSpace::VerifyImageAllocations() {
163 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
164 while (current < End()) {
165 DCHECK_ALIGNED(current, kObjectAlignment);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800166 mirror::Object* obj = reinterpret_cast<mirror::Object*>(current);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700167 CHECK(live_bitmap_->Test(obj));
168 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700169 if (kUseBakerOrBrooksReadBarrier) {
170 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800171 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700172 current += RoundUp(obj->SizeOf(), kObjectAlignment);
173 }
174}
175
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700176ImageSpace* ImageSpace::Init(const char* image_file_name, bool validate_oat_file,
177 std::string* error_msg) {
178 CHECK(image_file_name != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700179
180 uint64_t start_time = 0;
181 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
182 start_time = NanoTime();
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700183 LOG(INFO) << "ImageSpace::Init entering image_file_name=" << image_file_name;
Ian Rogers1d54e732013-05-02 21:10:01 -0700184 }
185
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700186 UniquePtr<File> file(OS::OpenFileForReading(image_file_name));
Ian Rogers1d54e732013-05-02 21:10:01 -0700187 if (file.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700188 *error_msg = StringPrintf("Failed to open '%s'", image_file_name);
189 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700190 }
191 ImageHeader image_header;
192 bool success = file->ReadFully(&image_header, sizeof(image_header));
193 if (!success || !image_header.IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700194 *error_msg = StringPrintf("Invalid image header in '%s'", image_file_name);
195 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700196 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700197
198 // Note: The image header is part of the image due to mmap page alignment required of offset.
Ian Rogers1d54e732013-05-02 21:10:01 -0700199 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700200 image_header.GetImageSize(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700201 PROT_READ | PROT_WRITE,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -0700202 MAP_PRIVATE,
Ian Rogers1d54e732013-05-02 21:10:01 -0700203 file->Fd(),
204 0,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700205 false,
206 image_file_name,
207 error_msg));
Ian Rogers1d54e732013-05-02 21:10:01 -0700208 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700209 DCHECK(!error_msg->empty());
210 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700211 }
212 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
213 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
214
Mathieu Chartier31e89252013-08-28 11:29:12 -0700215 UniquePtr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
216 PROT_READ, MAP_PRIVATE,
217 file->Fd(), image_header.GetBitmapOffset(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700218 false,
219 image_file_name,
220 error_msg));
221 if (image_map.get() == nullptr) {
222 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
223 return nullptr;
224 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800225 uint32_t bitmap_index = bitmap_index_.FetchAndAdd(1);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700226 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_file_name,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700227 bitmap_index));
228 UniquePtr<accounting::SpaceBitmap> bitmap(
229 accounting::SpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
230 reinterpret_cast<byte*>(map->Begin()),
231 map->Size()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700232 if (bitmap.get() == nullptr) {
233 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
234 return nullptr;
235 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700236
Ian Rogers1d54e732013-05-02 21:10:01 -0700237 Runtime* runtime = Runtime::Current();
238 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700239 runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
Jeff Hao88474b42013-10-23 16:24:40 -0700240 mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod);
241 runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method));
242 mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt);
243 runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt));
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
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000257 space->oat_file_.reset(space->OpenOatFile(image_file_name, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700258 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
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000275OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700276 const ImageHeader& image_header = GetImageHeader();
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000277 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
278
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700279 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700280 !Runtime::Current()->IsCompiler(), error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700281 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700282 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
283 oat_filename.c_str(), GetName(), error_msg->c_str());
284 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700285 }
286 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
287 uint32_t image_oat_checksum = image_header.GetOatChecksum();
288 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700289 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
290 " in image %s", oat_checksum, image_oat_checksum, GetName());
291 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700292 }
293 return oat_file;
294}
295
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700296bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700297 CHECK(oat_file_.get() != NULL);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700298 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700299 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
300 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700301 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
302 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
303 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700304 return false;
305 }
306 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700307 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
308 "dex file '%s' (0x%x != 0x%x)",
309 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
310 oat_dex_file->GetDexFileLocationChecksum(),
311 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700312 return false;
313 }
314 }
315 return true;
316}
317
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700318OatFile* ImageSpace::ReleaseOatFile() {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700319 CHECK(oat_file_.get() != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700320 return oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700321}
322
Ian Rogers1d54e732013-05-02 21:10:01 -0700323void ImageSpace::Dump(std::ostream& os) const {
324 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700325 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -0700326 << ",end=" << reinterpret_cast<void*>(End())
327 << ",size=" << PrettySize(Size())
328 << ",name=\"" << GetName() << "\"]";
329}
330
331} // namespace space
332} // namespace gc
333} // namespace art