blob: 6e1639c8e46e0976a31beb8e2911b60e7bf2e625 [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
Alex Light25396132014-08-27 15:37:23 -070019#include <dirent.h>
20#include <sys/types.h>
21
Alex Lighta59dd802014-07-02 16:28:08 -070022#include <random>
23
Ian Rogersc7dd2952014-10-21 23:31:19 -070024#include "base/macros.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070025#include "base/stl_util.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "base/unix_file/fd_file.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010027#include "base/scoped_flock.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070029#include "mirror/art_method.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "mirror/class-inl.h"
31#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070032#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "os.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "space-inl.h"
35#include "utils.h"
36
37namespace art {
38namespace gc {
39namespace space {
40
Ian Rogersef7d42f2014-01-06 12:55:46 -080041Atomic<uint32_t> ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070042
Narayan Kamath52f84882014-05-02 10:10:39 +010043ImageSpace::ImageSpace(const std::string& image_filename, const char* image_location,
44 MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap)
45 : MemMapSpace(image_filename, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(),
46 kGcRetentionPolicyNeverCollect),
47 image_location_(image_location) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070048 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070049 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070050}
51
Alex Lightcf4bf382014-07-24 11:29:14 -070052static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
53 CHECK_ALIGNED(min_delta, kPageSize);
54 CHECK_ALIGNED(max_delta, kPageSize);
55 CHECK_LT(min_delta, max_delta);
56
57 std::default_random_engine generator;
58 generator.seed(NanoTime() * getpid());
59 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
60 int32_t r = distribution(generator);
61 if (r % 2 == 0) {
62 r = RoundUp(r, kPageSize);
63 } else {
64 r = RoundDown(r, kPageSize);
65 }
66 CHECK_LE(min_delta, r);
67 CHECK_GE(max_delta, r);
68 CHECK_ALIGNED(r, kPageSize);
69 return r;
70}
71
Alex Light25396132014-08-27 15:37:23 -070072// We are relocating or generating the core image. We should get rid of everything. It is all
73// out-of-date. We also don't really care if this fails since it is just a convienence.
74// Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
75// Note this should only be used during first boot.
76static void RealPruneDexCache(const std::string& cache_dir_path);
77static void PruneDexCache(InstructionSet isa) {
78 CHECK_NE(isa, kNone);
79 // Prune the base /data/dalvik-cache
80 RealPruneDexCache(GetDalvikCacheOrDie(".", false));
81 // prune /data/dalvik-cache/<isa>
82 RealPruneDexCache(GetDalvikCacheOrDie(GetInstructionSetString(isa), false));
83}
84static void RealPruneDexCache(const std::string& cache_dir_path) {
85 if (!OS::DirectoryExists(cache_dir_path.c_str())) {
86 return;
87 }
88 DIR* cache_dir = opendir(cache_dir_path.c_str());
89 if (cache_dir == nullptr) {
90 PLOG(WARNING) << "Unable to open " << cache_dir_path << " to delete it's contents";
91 return;
92 }
Alex Light25396132014-08-27 15:37:23 -070093
94 for (struct dirent* de = readdir(cache_dir); de != nullptr; de = readdir(cache_dir)) {
95 const char* name = de->d_name;
96 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
97 continue;
98 }
99 // We only want to delete regular files.
100 if (de->d_type != DT_REG) {
101 if (de->d_type != DT_DIR) {
102 // We do expect some directories (namely the <isa> for pruning the base dalvik-cache).
103 LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
104 }
105 continue;
106 }
Brian Carlstromdebdda02014-08-28 22:17:13 -0700107 std::string cache_file(cache_dir_path);
108 cache_file += '/';
109 cache_file += name;
110 if (TEMP_FAILURE_RETRY(unlink(cache_file.c_str())) != 0) {
111 PLOG(ERROR) << "Unable to unlink " << cache_file;
Alex Light25396132014-08-27 15:37:23 -0700112 continue;
113 }
114 }
115 CHECK_EQ(0, TEMP_FAILURE_RETRY(closedir(cache_dir))) << "Unable to close directory.";
116}
117
118static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa,
119 std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700120 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
121 std::vector<std::string> boot_class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700122 Split(boot_class_path_string, ':', &boot_class_path);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700123 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700124 *error_msg = "Failed to generate image because no boot class path specified";
125 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700126 }
Alex Light25396132014-08-27 15:37:23 -0700127 // We should clean up so we are more likely to have room for the image.
128 if (Runtime::Current()->IsZygote()) {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700129 LOG(INFO) << "Pruning dalvik-cache since we are generating an image and will need to recompile";
Alex Light25396132014-08-27 15:37:23 -0700130 PruneDexCache(image_isa);
131 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700132
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700133 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700134
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700135 std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
Mathieu Chartier08d7d442013-07-31 18:08:51 -0700136 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700137
138 std::string image_option_string("--image=");
Narayan Kamath52f84882014-05-02 10:10:39 +0100139 image_option_string += image_filename;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700140 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700141
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700142 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700143 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700144 }
145
146 std::string oat_file_option_string("--oat-file=");
Brian Carlstrom2f1e15c2014-10-27 16:27:06 -0700147 oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename);
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700148 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700149
Ian Rogers8afeb852014-04-02 14:55:49 -0700150 Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700151 CHECK_EQ(image_isa, kRuntimeISA)
152 << "We should always be generating an image for the current isa.";
Ian Rogers8afeb852014-04-02 14:55:49 -0700153
Alex Lightcf4bf382014-07-24 11:29:14 -0700154 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
155 ART_BASE_ADDRESS_MAX_DELTA);
156 LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default "
157 << "art base address of 0x" << std::hex << ART_BASE_ADDRESS;
158 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700159
Brian Carlstrom57309db2014-07-30 15:13:25 -0700160 if (!kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700161 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700162 }
163
Brian Carlstrom6449c622014-02-10 23:48:36 -0800164 const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800165 for (size_t i = 0; i < compiler_options.size(); ++i) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800166 arg_vector.push_back(compiler_options[i].c_str());
167 }
168
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700169 std::string command_line(Join(arg_vector, ' '));
170 LOG(INFO) << "GenerateImage: " << command_line;
Brian Carlstrom6449c622014-02-10 23:48:36 -0800171 return Exec(arg_vector, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700172}
173
Narayan Kamath52f84882014-05-02 10:10:39 +0100174bool ImageSpace::FindImageFilename(const char* image_location,
175 const InstructionSet image_isa,
Alex Lighta59dd802014-07-02 16:28:08 -0700176 std::string* system_filename,
177 bool* has_system,
178 std::string* cache_filename,
179 bool* dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700180 bool* has_cache,
181 bool* is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -0700182 *has_system = false;
183 *has_cache = false;
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700184 // image_location = /system/framework/boot.art
185 // system_image_location = /system/framework/<image_isa>/boot.art
186 std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
187 if (OS::FileExists(system_image_filename.c_str())) {
Alex Lighta59dd802014-07-02 16:28:08 -0700188 *system_filename = system_image_filename;
189 *has_system = true;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700190 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100191
Alex Lighta59dd802014-07-02 16:28:08 -0700192 bool have_android_data = false;
193 *dalvik_cache_exists = false;
194 std::string dalvik_cache;
195 GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700196 &have_android_data, dalvik_cache_exists, is_global_cache);
Narayan Kamath52f84882014-05-02 10:10:39 +0100197
Alex Lighta59dd802014-07-02 16:28:08 -0700198 if (have_android_data && *dalvik_cache_exists) {
199 // Always set output location even if it does not exist,
200 // so that the caller knows where to create the image.
201 //
202 // image_location = /system/framework/boot.art
203 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
204 std::string error_msg;
205 if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) {
206 LOG(WARNING) << error_msg;
207 return *has_system;
208 }
209 *has_cache = OS::FileExists(cache_filename->c_str());
210 }
211 return *has_system || *has_cache;
212}
213
214static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) {
215 std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
216 if (image_file.get() == nullptr) {
217 return false;
218 }
219 const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader));
220 if (!success || !image_header->IsValid()) {
221 return false;
222 }
223 return true;
224}
225
Alex Light6e183f22014-07-18 14:57:04 -0700226// Relocate the image at image_location to dest_filename and relocate it by a random amount.
227static bool RelocateImage(const char* image_location, const char* dest_filename,
Alex Lighta59dd802014-07-02 16:28:08 -0700228 InstructionSet isa, std::string* error_msg) {
Alex Light25396132014-08-27 15:37:23 -0700229 // We should clean up so we are more likely to have room for the image.
230 if (Runtime::Current()->IsZygote()) {
231 LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile";
232 PruneDexCache(isa);
233 }
234
Alex Lighta59dd802014-07-02 16:28:08 -0700235 std::string patchoat(Runtime::Current()->GetPatchoatExecutable());
236
237 std::string input_image_location_arg("--input-image-location=");
238 input_image_location_arg += image_location;
239
240 std::string output_image_filename_arg("--output-image-file=");
241 output_image_filename_arg += dest_filename;
242
243 std::string input_oat_location_arg("--input-oat-location=");
244 input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location);
245
246 std::string output_oat_filename_arg("--output-oat-file=");
247 output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename);
248
249 std::string instruction_set_arg("--instruction-set=");
250 instruction_set_arg += GetInstructionSetString(isa);
251
252 std::string base_offset_arg("--base-offset-delta=");
253 StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
254 ART_BASE_ADDRESS_MAX_DELTA));
255
256 std::vector<std::string> argv;
257 argv.push_back(patchoat);
258
259 argv.push_back(input_image_location_arg);
260 argv.push_back(output_image_filename_arg);
261
262 argv.push_back(input_oat_location_arg);
263 argv.push_back(output_oat_filename_arg);
264
265 argv.push_back(instruction_set_arg);
266 argv.push_back(base_offset_arg);
267
268 std::string command_line(Join(argv, ' '));
269 LOG(INFO) << "RelocateImage: " << command_line;
270 return Exec(argv, error_msg);
271}
272
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700273static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700274 std::unique_ptr<ImageHeader> hdr(new ImageHeader);
275 if (!ReadSpecificImageHeader(filename, hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700276 *error_msg = StringPrintf("Unable to read image header for %s", filename);
Alex Lighta59dd802014-07-02 16:28:08 -0700277 return nullptr;
278 }
279 return hdr.release();
Narayan Kamath52f84882014-05-02 10:10:39 +0100280}
281
282ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location,
283 const InstructionSet image_isa) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700284 std::string error_msg;
285 ImageHeader* image_header = ReadImageHeader(image_location, image_isa, &error_msg);
286 if (image_header == nullptr) {
287 LOG(FATAL) << error_msg;
288 }
289 return image_header;
290}
291
292ImageHeader* ImageSpace::ReadImageHeader(const char* image_location,
293 const InstructionSet image_isa,
294 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700295 std::string system_filename;
296 bool has_system = false;
297 std::string cache_filename;
298 bool has_cache = false;
299 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700300 bool is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -0700301 if (FindImageFilename(image_location, image_isa, &system_filename, &has_system,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700302 &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700303 if (Runtime::Current()->ShouldRelocate()) {
304 if (has_system && has_cache) {
305 std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader);
306 std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader);
307 if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700308 *error_msg = StringPrintf("Unable to read image header for %s at %s",
309 image_location, system_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700310 return nullptr;
311 }
312 if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700313 *error_msg = StringPrintf("Unable to read image header for %s at %s",
314 image_location, cache_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700315 return nullptr;
316 }
317 if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700318 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
319 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700320 return nullptr;
321 }
322 return cache_hdr.release();
323 } else if (!has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700324 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
325 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700326 return nullptr;
327 } else if (!has_system && has_cache) {
328 // This can probably just use the cache one.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700329 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700330 }
331 } else {
332 // We don't want to relocate, Just pick the appropriate one if we have it and return.
333 if (has_system && has_cache) {
334 // We want the cache if the checksum matches, otherwise the system.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700335 std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(),
336 error_msg));
337 std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(),
338 error_msg));
Alex Lighta59dd802014-07-02 16:28:08 -0700339 if (system.get() == nullptr ||
340 (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) {
341 return cache.release();
342 } else {
343 return system.release();
344 }
345 } else if (has_system) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700346 return ReadSpecificImageHeader(system_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700347 } else if (has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700348 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700349 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100350 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100351 }
352
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700353 *error_msg = StringPrintf("Unable to find image file for %s", image_location);
Narayan Kamath52f84882014-05-02 10:10:39 +0100354 return nullptr;
355}
356
Alex Lighta59dd802014-07-02 16:28:08 -0700357static bool ChecksumsMatch(const char* image_a, const char* image_b) {
358 ImageHeader hdr_a;
359 ImageHeader hdr_b;
360 return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b)
361 && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum();
362}
363
Andreas Gampe3c13a792014-09-18 20:56:04 -0700364static bool ImageCreationAllowed(bool is_global_cache, std::string* error_msg) {
365 // Anyone can write into a "local" cache.
366 if (!is_global_cache) {
367 return true;
368 }
369
370 // Only the zygote is allowed to create the global boot image.
371 if (Runtime::Current()->IsZygote()) {
372 return true;
373 }
374
375 *error_msg = "Only the zygote can create the global boot image.";
376 return false;
377}
378
Narayan Kamath52f84882014-05-02 10:10:39 +0100379ImageSpace* ImageSpace::Create(const char* image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700380 const InstructionSet image_isa,
381 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700382 std::string system_filename;
383 bool has_system = false;
384 std::string cache_filename;
385 bool has_cache = false;
386 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700387 bool is_global_cache = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700388 const bool found_image = FindImageFilename(image_location, image_isa, &system_filename,
389 &has_system, &cache_filename, &dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700390 &has_cache, &is_global_cache);
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100391
Alex Lighta59dd802014-07-02 16:28:08 -0700392 ImageSpace* space;
393 bool relocate = Runtime::Current()->ShouldRelocate();
Alex Light64ad14d2014-08-19 14:23:13 -0700394 bool can_compile = Runtime::Current()->IsImageDex2OatEnabled();
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100395 if (found_image) {
Alex Lighta59dd802014-07-02 16:28:08 -0700396 const std::string* image_filename;
397 bool is_system = false;
398 bool relocated_version_used = false;
399 if (relocate) {
Alex Light64ad14d2014-08-19 14:23:13 -0700400 if (!dalvik_cache_exists) {
401 *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have "
402 "any dalvik_cache to find/place it in.",
403 image_location, system_filename.c_str());
404 return nullptr;
405 }
Alex Lighta59dd802014-07-02 16:28:08 -0700406 if (has_system) {
407 if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
408 // We already have a relocated version
409 image_filename = &cache_filename;
410 relocated_version_used = true;
411 } else {
412 // We cannot have a relocated version, Relocate the system one and use it.
Andreas Gampe3c13a792014-09-18 20:56:04 -0700413
414 std::string reason;
415 bool success;
416
417 // Check whether we are allowed to relocate.
418 if (!can_compile) {
419 reason = "Image dex2oat disabled by -Xnoimage-dex2oat.";
420 success = false;
421 } else if (!ImageCreationAllowed(is_global_cache, &reason)) {
422 // Whether we can write to the cache.
423 success = false;
424 } else {
425 // Try to relocate.
426 success = RelocateImage(image_location, cache_filename.c_str(), image_isa, &reason);
427 }
428
429 if (success) {
Alex Lighta59dd802014-07-02 16:28:08 -0700430 relocated_version_used = true;
431 image_filename = &cache_filename;
432 } else {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700433 *error_msg = StringPrintf("Unable to relocate image '%s' from '%s' to '%s': %s",
Alex Light64ad14d2014-08-19 14:23:13 -0700434 image_location, system_filename.c_str(),
435 cache_filename.c_str(), reason.c_str());
Brian Carlstrome9105f72014-10-28 15:53:43 -0700436 // We failed to create files, remove any possibly garbage output.
437 // Since ImageCreationAllowed was true above, we are the zygote
438 // and therefore the only process expected to generate these for
439 // the device.
440 PruneDexCache(image_isa);
Alex Lighta59dd802014-07-02 16:28:08 -0700441 return nullptr;
442 }
443 }
444 } else {
445 CHECK(has_cache);
446 // We can just use cache's since it should be fine. This might or might not be relocated.
447 image_filename = &cache_filename;
448 }
449 } else {
450 if (has_system && has_cache) {
451 // Check they have the same cksum. If they do use the cache. Otherwise system.
452 if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
453 image_filename = &cache_filename;
454 relocated_version_used = true;
455 } else {
456 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700457 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700458 }
459 } else if (has_system) {
460 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700461 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700462 } else {
463 CHECK(has_cache);
464 image_filename = &cache_filename;
465 }
466 }
467 {
468 // Note that we must not use the file descriptor associated with
469 // ScopedFlock::GetFile to Init the image file. We want the file
470 // descriptor (and the associated exclusive lock) to be released when
471 // we leave Create.
472 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700473 image_lock.Init(image_filename->c_str(), error_msg);
Alex Lightb6cabc12014-08-21 09:45:00 -0700474 VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location "
475 << image_location;
Alex Lightb93637a2014-07-31 10:48:46 -0700476 // If we are in /system we can assume the image is good. We can also
477 // assume this if we are using a relocated image (i.e. image checksum
478 // matches) since this is only different by the offset. We need this to
479 // make sure that host tests continue to work.
Alex Lighta59dd802014-07-02 16:28:08 -0700480 space = ImageSpace::Init(image_filename->c_str(), image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700481 !(is_system || relocated_version_used), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700482 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100483 if (space != nullptr) {
484 return space;
485 }
486
Alex Lighta59dd802014-07-02 16:28:08 -0700487 if (relocated_version_used) {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700488 // Something is wrong with the relocated copy (even though checksums match). Cleanup.
489 // This can happen if the .oat is corrupt, since the above only checks the .art checksums.
490 // TODO: Check the oat file validity earlier.
491 *error_msg = StringPrintf("Attempted to use relocated version of %s at %s generated from %s "
492 "but image failed to load: %s",
493 image_location, cache_filename.c_str(), system_filename.c_str(),
494 error_msg->c_str());
495 PruneDexCache(image_isa);
Alex Lighta59dd802014-07-02 16:28:08 -0700496 return nullptr;
497 } else if (is_system) {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700498 // If the /system file exists, it should be up-to-date, don't try to generate it.
Alex Light64ad14d2014-08-19 14:23:13 -0700499 *error_msg = StringPrintf("Failed to load /system image '%s': %s",
500 image_filename->c_str(), error_msg->c_str());
Narayan Kamath52f84882014-05-02 10:10:39 +0100501 return nullptr;
Mathieu Chartierc7cb1902014-03-05 14:41:03 -0800502 } else {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700503 // Otherwise, log a warning and fall through to GenerateImage.
Alex Light64ad14d2014-08-19 14:23:13 -0700504 LOG(WARNING) << *error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700505 }
506 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100507
Alex Light64ad14d2014-08-19 14:23:13 -0700508 if (!can_compile) {
509 *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat";
510 return nullptr;
511 } else if (!dalvik_cache_exists) {
512 *error_msg = StringPrintf("No place to put generated image.");
513 return nullptr;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700514 } else if (!ImageCreationAllowed(is_global_cache, error_msg)) {
515 return nullptr;
Alex Light25396132014-08-27 15:37:23 -0700516 } else if (!GenerateImage(cache_filename, image_isa, error_msg)) {
Alex Light64ad14d2014-08-19 14:23:13 -0700517 *error_msg = StringPrintf("Failed to generate image '%s': %s",
518 cache_filename.c_str(), error_msg->c_str());
Brian Carlstrome9105f72014-10-28 15:53:43 -0700519 // We failed to create files, remove any possibly garbage output.
520 // Since ImageCreationAllowed was true above, we are the zygote
521 // and therefore the only process expected to generate these for
522 // the device.
523 PruneDexCache(image_isa);
Alex Light64ad14d2014-08-19 14:23:13 -0700524 return nullptr;
525 } else {
Alex Lighta59dd802014-07-02 16:28:08 -0700526 // Note that we must not use the file descriptor associated with
527 // ScopedFlock::GetFile to Init the image file. We want the file
528 // descriptor (and the associated exclusive lock) to be released when
529 // we leave Create.
530 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700531 image_lock.Init(cache_filename.c_str(), error_msg);
532 space = ImageSpace::Init(cache_filename.c_str(), image_location, true, error_msg);
533 if (space == nullptr) {
534 *error_msg = StringPrintf("Failed to load generated image '%s': %s",
535 cache_filename.c_str(), error_msg->c_str());
536 }
537 return space;
Alex Lighta59dd802014-07-02 16:28:08 -0700538 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700539}
540
Mathieu Chartier31e89252013-08-28 11:29:12 -0700541void ImageSpace::VerifyImageAllocations() {
Ian Rogers13735952014-10-08 12:43:28 -0700542 uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700543 while (current < End()) {
544 DCHECK_ALIGNED(current, kObjectAlignment);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800545 mirror::Object* obj = reinterpret_cast<mirror::Object*>(current);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700546 CHECK(live_bitmap_->Test(obj));
547 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700548 if (kUseBakerOrBrooksReadBarrier) {
549 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800550 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700551 current += RoundUp(obj->SizeOf(), kObjectAlignment);
552 }
553}
554
Narayan Kamath52f84882014-05-02 10:10:39 +0100555ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location,
556 bool validate_oat_file, std::string* error_msg) {
557 CHECK(image_filename != nullptr);
558 CHECK(image_location != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700559
560 uint64_t start_time = 0;
561 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
562 start_time = NanoTime();
Narayan Kamath52f84882014-05-02 10:10:39 +0100563 LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename;
Ian Rogers1d54e732013-05-02 21:10:01 -0700564 }
565
Ian Rogers700a4022014-05-19 16:49:03 -0700566 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename));
Ian Rogers1d54e732013-05-02 21:10:01 -0700567 if (file.get() == NULL) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100568 *error_msg = StringPrintf("Failed to open '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700569 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700570 }
571 ImageHeader image_header;
572 bool success = file->ReadFully(&image_header, sizeof(image_header));
573 if (!success || !image_header.IsValid()) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100574 *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700575 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700576 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700577
578 // Note: The image header is part of the image due to mmap page alignment required of offset.
Ian Rogers700a4022014-05-19 16:49:03 -0700579 std::unique_ptr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700580 image_header.GetImageSize(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700581 PROT_READ | PROT_WRITE,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -0700582 MAP_PRIVATE,
Ian Rogers1d54e732013-05-02 21:10:01 -0700583 file->Fd(),
584 0,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700585 false,
Narayan Kamath52f84882014-05-02 10:10:39 +0100586 image_filename,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700587 error_msg));
Ian Rogers1d54e732013-05-02 21:10:01 -0700588 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700589 DCHECK(!error_msg->empty());
590 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700591 }
592 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
593 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
594
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700595 std::unique_ptr<MemMap> image_map(
596 MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
597 PROT_READ, MAP_PRIVATE,
598 file->Fd(), image_header.GetBitmapOffset(),
599 false,
600 image_filename,
601 error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700602 if (image_map.get() == nullptr) {
603 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
604 return nullptr;
605 }
Ian Rogers3e5cf302014-05-20 16:40:37 -0700606 uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1);
Narayan Kamath52f84882014-05-02 10:10:39 +0100607 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700608 bitmap_index));
Ian Rogers700a4022014-05-19 16:49:03 -0700609 std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700610 accounting::ContinuousSpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
Ian Rogers13735952014-10-08 12:43:28 -0700611 reinterpret_cast<uint8_t*>(map->Begin()),
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700612 map->Size()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700613 if (bitmap.get() == nullptr) {
614 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
615 return nullptr;
616 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700617
Ian Rogers700a4022014-05-19 16:49:03 -0700618 std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, image_location,
Narayan Kamath52f84882014-05-02 10:10:39 +0100619 map.release(), bitmap.release()));
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700620
621 // VerifyImageAllocations() will be called later in Runtime::Init()
622 // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
623 // and ArtField::java_lang_reflect_ArtField_, which are used from
624 // Object::SizeOf() which VerifyImageAllocations() calls, are not
625 // set yet at this point.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700626
Narayan Kamath52f84882014-05-02 10:10:39 +0100627 space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700628 if (space->oat_file_.get() == nullptr) {
629 DCHECK(!error_msg->empty());
630 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700631 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700632
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700633 if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
634 DCHECK(!error_msg->empty());
635 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700636 }
637
Vladimir Marko7624d252014-05-02 14:40:15 +0100638 Runtime* runtime = Runtime::Current();
639 runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet());
640
641 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
642 runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
643 mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod);
644 runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method));
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700645 mirror::Object* imt_unimplemented_method =
646 image_header.GetImageRoot(ImageHeader::kImtUnimplementedMethod);
647 runtime->SetImtUnimplementedMethod(down_cast<mirror::ArtMethod*>(imt_unimplemented_method));
Vladimir Marko7624d252014-05-02 14:40:15 +0100648 mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt);
649 runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt));
650
651 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700652 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
653 Runtime::kSaveAll);
Vladimir Marko7624d252014-05-02 14:40:15 +0100654 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700655 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
656 Runtime::kRefsOnly);
Vladimir Marko7624d252014-05-02 14:40:15 +0100657 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700658 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
659 Runtime::kRefsAndArgs);
Vladimir Marko7624d252014-05-02 14:40:15 +0100660
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700661 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
662 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
663 << ") " << *space.get();
664 }
665 return space.release();
666}
667
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000668OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700669 const ImageHeader& image_header = GetImageHeader();
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000670 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
671
Igor Murashkin46774762014-10-22 11:37:02 -0700672 CHECK(image_header.GetOatDataBegin() != nullptr);
673
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700674 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
Igor Murashkin46774762014-10-22 11:37:02 -0700675 image_header.GetOatFileBegin(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700676 !Runtime::Current()->IsCompiler(), error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700677 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700678 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
679 oat_filename.c_str(), GetName(), error_msg->c_str());
680 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700681 }
682 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
683 uint32_t image_oat_checksum = image_header.GetOatChecksum();
684 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700685 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
686 " in image %s", oat_checksum, image_oat_checksum, GetName());
687 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700688 }
Alex Lighta59dd802014-07-02 16:28:08 -0700689 int32_t image_patch_delta = image_header.GetPatchDelta();
690 int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta();
Igor Murashkin46774762014-10-22 11:37:02 -0700691 if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700692 // We should have already relocated by this point. Bail out.
693 *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d "
694 "in image %s", oat_patch_delta, image_patch_delta, GetName());
695 return nullptr;
696 }
697
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700698 return oat_file;
699}
700
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700701bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700702 CHECK(oat_file_.get() != NULL);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700703 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700704 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
705 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700706 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
707 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
708 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700709 return false;
710 }
711 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700712 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
713 "dex file '%s' (0x%x != 0x%x)",
714 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
715 oat_dex_file->GetDexFileLocationChecksum(),
716 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700717 return false;
718 }
719 }
720 return true;
721}
722
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700723const OatFile* ImageSpace::GetOatFile() const {
724 return oat_file_.get();
725}
726
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700727OatFile* ImageSpace::ReleaseOatFile() {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700728 CHECK(oat_file_.get() != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700729 return oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700730}
731
Ian Rogers1d54e732013-05-02 21:10:01 -0700732void ImageSpace::Dump(std::ostream& os) const {
733 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700734 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -0700735 << ",end=" << reinterpret_cast<void*>(End())
736 << ",size=" << PrettySize(Size())
737 << ",name=\"" << GetName() << "\"]";
738}
739
740} // namespace space
741} // namespace gc
742} // namespace art