blob: 3de1ba4f08e36fb0d7d462ef4b0cdd6df796bc34 [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"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "space-inl.h"
28#include "utils.h"
29
30namespace art {
31namespace gc {
32namespace space {
33
Ian Rogersef7d42f2014-01-06 12:55:46 -080034Atomic<uint32_t> ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070035
Mathieu Chartier31e89252013-08-28 11:29:12 -070036ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map,
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070037 accounting::ContinuousSpaceBitmap* live_bitmap)
Mathieu Chartier590fee92013-09-13 13:46:47 -070038 : MemMapSpace(name, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(),
39 kGcRetentionPolicyNeverCollect) {
40 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070041 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070042}
43
Ian Rogers8d31bbd2013-10-13 10:44:14 -070044static bool GenerateImage(const std::string& image_file_name, std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070045 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
46 std::vector<std::string> boot_class_path;
47 Split(boot_class_path_string, ':', boot_class_path);
48 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070049 *error_msg = "Failed to generate image because no boot class path specified";
50 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070051 }
52
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070053 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070054
Mathieu Chartiere5426c92013-08-01 13:55:42 -070055 std::string dex2oat(GetAndroidRoot());
Mathieu Chartier08d7d442013-07-31 18:08:51 -070056 dex2oat += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat");
57 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070058
59 std::string image_option_string("--image=");
60 image_option_string += image_file_name;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070061 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070062
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070063 arg_vector.push_back("--runtime-arg");
64 arg_vector.push_back("-Xms64m");
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("-Xmx64m");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070068
Dave Allisonb373e092014-02-20 16:06:36 -080069
Brian Carlstrom56d947f2013-07-15 13:14:23 -070070 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070071 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070072 }
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 Chartier8bbc8c02013-07-31 16:27:01 -070078 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070079
Ian Rogers8afeb852014-04-02 14:55:49 -070080 Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
81
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
Brian Carlstrom6449c622014-02-10 23:48:36 -080091 const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
Brian Carlstrom2ec65202014-03-03 15:16:37 -080092 for (size_t i = 0; i < compiler_options.size(); ++i) {
Brian Carlstrom6449c622014-02-10 23:48:36 -080093 arg_vector.push_back(compiler_options[i].c_str());
94 }
95
Brian Carlstrom56d947f2013-07-15 13:14:23 -070096 std::string command_line(Join(arg_vector, ' '));
97 LOG(INFO) << "GenerateImage: " << command_line;
Brian Carlstrom6449c622014-02-10 23:48:36 -080098 return Exec(arg_vector, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070099}
100
Narayan Kamath11d9f062014-04-23 20:24:57 +0100101ImageSpace* ImageSpace::Create(const char* original_image_file_name,
102 const InstructionSet image_isa) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700103 if (OS::FileExists(original_image_file_name)) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700104 // If the /system file exists, it should be up-to-date, don't try to generate
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700105 std::string error_msg;
106 ImageSpace* space = ImageSpace::Init(original_image_file_name, false, &error_msg);
107 if (space == nullptr) {
108 LOG(FATAL) << "Failed to load image '" << original_image_file_name << "': " << error_msg;
109 }
110 return space;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700111 }
112 // If the /system file didn't exist, we need to use one from the dalvik-cache.
113 // If the cache file exists, try to open, but if it fails, regenerate.
114 // If it does not exist, generate.
Narayan Kamath11d9f062014-04-23 20:24:57 +0100115 const std::string dalvik_cache = GetDalvikCacheOrDie(GetInstructionSetString(image_isa));
116 std::string image_file_name(GetDalvikCacheFilenameOrDie(original_image_file_name,
117 dalvik_cache.c_str()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700118 std::string error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700119 if (OS::FileExists(image_file_name.c_str())) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700120 space::ImageSpace* image_space = ImageSpace::Init(image_file_name.c_str(), true, &error_msg);
121 if (image_space != nullptr) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700122 return image_space;
Mathieu Chartierc7cb1902014-03-05 14:41:03 -0800123 } else {
124 LOG(WARNING) << error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700125 }
126 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 CHECK(GenerateImage(image_file_name, &error_msg))
128 << "Failed to generate image '" << image_file_name << "': " << error_msg;
129 ImageSpace* space = ImageSpace::Init(image_file_name.c_str(), true, &error_msg);
130 if (space == nullptr) {
131 LOG(FATAL) << "Failed to load image '" << original_image_file_name << "': " << error_msg;
132 }
133 return space;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700134}
135
Mathieu Chartier31e89252013-08-28 11:29:12 -0700136void ImageSpace::VerifyImageAllocations() {
137 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
138 while (current < End()) {
139 DCHECK_ALIGNED(current, kObjectAlignment);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800140 mirror::Object* obj = reinterpret_cast<mirror::Object*>(current);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700141 CHECK(live_bitmap_->Test(obj));
142 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700143 if (kUseBakerOrBrooksReadBarrier) {
144 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800145 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700146 current += RoundUp(obj->SizeOf(), kObjectAlignment);
147 }
148}
149
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700150ImageSpace* ImageSpace::Init(const char* image_file_name, bool validate_oat_file,
151 std::string* error_msg) {
152 CHECK(image_file_name != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700153
154 uint64_t start_time = 0;
155 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
156 start_time = NanoTime();
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700157 LOG(INFO) << "ImageSpace::Init entering image_file_name=" << image_file_name;
Ian Rogers1d54e732013-05-02 21:10:01 -0700158 }
159
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700160 UniquePtr<File> file(OS::OpenFileForReading(image_file_name));
Ian Rogers1d54e732013-05-02 21:10:01 -0700161 if (file.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700162 *error_msg = StringPrintf("Failed to open '%s'", image_file_name);
163 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700164 }
165 ImageHeader image_header;
166 bool success = file->ReadFully(&image_header, sizeof(image_header));
167 if (!success || !image_header.IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700168 *error_msg = StringPrintf("Invalid image header in '%s'", image_file_name);
169 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700170 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700171
172 // Note: The image header is part of the image due to mmap page alignment required of offset.
Ian Rogers1d54e732013-05-02 21:10:01 -0700173 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700174 image_header.GetImageSize(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700175 PROT_READ | PROT_WRITE,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -0700176 MAP_PRIVATE,
Ian Rogers1d54e732013-05-02 21:10:01 -0700177 file->Fd(),
178 0,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700179 false,
180 image_file_name,
181 error_msg));
Ian Rogers1d54e732013-05-02 21:10:01 -0700182 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700183 DCHECK(!error_msg->empty());
184 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700185 }
186 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
187 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
188
Mathieu Chartier31e89252013-08-28 11:29:12 -0700189 UniquePtr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
190 PROT_READ, MAP_PRIVATE,
191 file->Fd(), image_header.GetBitmapOffset(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700192 false,
193 image_file_name,
194 error_msg));
195 if (image_map.get() == nullptr) {
196 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
197 return nullptr;
198 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800199 uint32_t bitmap_index = bitmap_index_.FetchAndAdd(1);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700200 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_file_name,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700201 bitmap_index));
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700202 UniquePtr<accounting::ContinuousSpaceBitmap> bitmap(
203 accounting::ContinuousSpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
204 reinterpret_cast<byte*>(map->Begin()),
205 map->Size()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700206 if (bitmap.get() == nullptr) {
207 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
208 return nullptr;
209 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700210
Ian Rogers1d54e732013-05-02 21:10:01 -0700211 Runtime* runtime = Runtime::Current();
212 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700213 runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
Jeff Hao88474b42013-10-23 16:24:40 -0700214 mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod);
215 runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method));
216 mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt);
217 runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt));
Ian Rogers1d54e732013-05-02 21:10:01 -0700218
219 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700220 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kSaveAll);
Ian Rogers1d54e732013-05-02 21:10:01 -0700221 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700222 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsOnly);
Ian Rogers1d54e732013-05-02 21:10:01 -0700223 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -0700224 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsAndArgs);
Ian Rogers1d54e732013-05-02 21:10:01 -0700225
Mathieu Chartier31e89252013-08-28 11:29:12 -0700226 UniquePtr<ImageSpace> space(new ImageSpace(image_file_name, map.release(), bitmap.release()));
227 if (kIsDebugBuild) {
228 space->VerifyImageAllocations();
229 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700230
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000231 space->oat_file_.reset(space->OpenOatFile(image_file_name, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700232 if (space->oat_file_.get() == nullptr) {
233 DCHECK(!error_msg->empty());
234 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700235 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700236
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700237 if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
238 DCHECK(!error_msg->empty());
239 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700240 }
241
242 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
243 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
244 << ") " << *space.get();
245 }
246 return space.release();
247}
248
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000249OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700250 const ImageHeader& image_header = GetImageHeader();
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000251 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
252
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700253 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700254 !Runtime::Current()->IsCompiler(), error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700255 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700256 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
257 oat_filename.c_str(), GetName(), error_msg->c_str());
258 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700259 }
260 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
261 uint32_t image_oat_checksum = image_header.GetOatChecksum();
262 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700263 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
264 " in image %s", oat_checksum, image_oat_checksum, GetName());
265 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700266 }
267 return oat_file;
268}
269
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700270bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700271 CHECK(oat_file_.get() != NULL);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700272 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700273 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
274 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700275 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
276 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
277 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700278 return false;
279 }
280 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700281 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
282 "dex file '%s' (0x%x != 0x%x)",
283 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
284 oat_dex_file->GetDexFileLocationChecksum(),
285 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700286 return false;
287 }
288 }
289 return true;
290}
291
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700292OatFile* ImageSpace::ReleaseOatFile() {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700293 CHECK(oat_file_.get() != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700294 return oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700295}
296
Ian Rogers1d54e732013-05-02 21:10:01 -0700297void ImageSpace::Dump(std::ostream& os) const {
298 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700299 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -0700300 << ",end=" << reinterpret_cast<void*>(End())
301 << ",size=" << PrettySize(Size())
302 << ",name=\"" << GetName() << "\"]";
303}
304
305} // namespace space
306} // namespace gc
307} // namespace art