blob: ce64b103647496d18e7ebb47a77c3d6b7f585e03 [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>
Andreas Gampe70be1fb2014-10-31 16:45:19 -070020#include <sys/statvfs.h>
Alex Light25396132014-08-27 15:37:23 -070021#include <sys/types.h>
Narayan Kamath5a2be3f2015-02-16 13:51:51 +000022#include <unistd.h>
Alex Light25396132014-08-27 15:37:23 -070023
Alex Lighta59dd802014-07-02 16:28:08 -070024#include <random>
25
Mathieu Chartiere401d142015-04-22 13:56:20 -070026#include "art_method.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070027#include "base/macros.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070028#include "base/stl_util.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010029#include "base/scoped_flock.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010030#include "base/time_utils.h"
31#include "base/unix_file/fd_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "mirror/class-inl.h"
34#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070035#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "os.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037#include "space-inl.h"
38#include "utils.h"
39
40namespace art {
41namespace gc {
42namespace space {
43
Ian Rogersef7d42f2014-01-06 12:55:46 -080044Atomic<uint32_t> ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070045
Narayan Kamath52f84882014-05-02 10:10:39 +010046ImageSpace::ImageSpace(const std::string& image_filename, const char* image_location,
Mathieu Chartierc7853442015-03-27 14:35:38 -070047 MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap,
48 uint8_t* end)
49 : MemMapSpace(image_filename, mem_map, mem_map->Begin(), end, end,
Narayan Kamath52f84882014-05-02 10:10:39 +010050 kGcRetentionPolicyNeverCollect),
51 image_location_(image_location) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070052 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070053 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070054}
55
Alex Lightcf4bf382014-07-24 11:29:14 -070056static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
57 CHECK_ALIGNED(min_delta, kPageSize);
58 CHECK_ALIGNED(max_delta, kPageSize);
59 CHECK_LT(min_delta, max_delta);
60
61 std::default_random_engine generator;
62 generator.seed(NanoTime() * getpid());
63 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
64 int32_t r = distribution(generator);
65 if (r % 2 == 0) {
66 r = RoundUp(r, kPageSize);
67 } else {
68 r = RoundDown(r, kPageSize);
69 }
70 CHECK_LE(min_delta, r);
71 CHECK_GE(max_delta, r);
72 CHECK_ALIGNED(r, kPageSize);
73 return r;
74}
75
Alex Light25396132014-08-27 15:37:23 -070076// We are relocating or generating the core image. We should get rid of everything. It is all
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080077// out-of-date. We also don't really care if this fails since it is just a convenience.
Alex Light25396132014-08-27 15:37:23 -070078// Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
79// Note this should only be used during first boot.
Narayan Kamath28bc9872014-11-07 17:46:28 +000080static void RealPruneDalvikCache(const std::string& cache_dir_path);
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080081
Narayan Kamath28bc9872014-11-07 17:46:28 +000082static void PruneDalvikCache(InstructionSet isa) {
Alex Light25396132014-08-27 15:37:23 -070083 CHECK_NE(isa, kNone);
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080084 // Prune the base /data/dalvik-cache.
Narayan Kamath28bc9872014-11-07 17:46:28 +000085 RealPruneDalvikCache(GetDalvikCacheOrDie(".", false));
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080086 // Prune /data/dalvik-cache/<isa>.
Narayan Kamath28bc9872014-11-07 17:46:28 +000087 RealPruneDalvikCache(GetDalvikCacheOrDie(GetInstructionSetString(isa), false));
Alex Light25396132014-08-27 15:37:23 -070088}
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080089
Narayan Kamath28bc9872014-11-07 17:46:28 +000090static void RealPruneDalvikCache(const std::string& cache_dir_path) {
Alex Light25396132014-08-27 15:37:23 -070091 if (!OS::DirectoryExists(cache_dir_path.c_str())) {
92 return;
93 }
94 DIR* cache_dir = opendir(cache_dir_path.c_str());
95 if (cache_dir == nullptr) {
96 PLOG(WARNING) << "Unable to open " << cache_dir_path << " to delete it's contents";
97 return;
98 }
Alex Light25396132014-08-27 15:37:23 -070099
100 for (struct dirent* de = readdir(cache_dir); de != nullptr; de = readdir(cache_dir)) {
101 const char* name = de->d_name;
102 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
103 continue;
104 }
Andreas Gampe8db9dcd2014-11-09 18:14:30 -0800105 // We only want to delete regular files and symbolic links.
106 if (de->d_type != DT_REG && de->d_type != DT_LNK) {
Alex Light25396132014-08-27 15:37:23 -0700107 if (de->d_type != DT_DIR) {
108 // We do expect some directories (namely the <isa> for pruning the base dalvik-cache).
109 LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
110 }
111 continue;
112 }
Brian Carlstromdebdda02014-08-28 22:17:13 -0700113 std::string cache_file(cache_dir_path);
114 cache_file += '/';
115 cache_file += name;
116 if (TEMP_FAILURE_RETRY(unlink(cache_file.c_str())) != 0) {
117 PLOG(ERROR) << "Unable to unlink " << cache_file;
Alex Light25396132014-08-27 15:37:23 -0700118 continue;
119 }
120 }
121 CHECK_EQ(0, TEMP_FAILURE_RETRY(closedir(cache_dir))) << "Unable to close directory.";
122}
123
Narayan Kamath28bc9872014-11-07 17:46:28 +0000124// We write out an empty file to the zygote's ISA specific cache dir at the start of
125// every zygote boot and delete it when the boot completes. If we find a file already
126// present, it usually means the boot didn't complete. We wipe the entire dalvik
127// cache if that's the case.
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000128static void MarkZygoteStart(const InstructionSet isa, const uint32_t max_failed_boots) {
Narayan Kamath28bc9872014-11-07 17:46:28 +0000129 const std::string isa_subdir = GetDalvikCacheOrDie(GetInstructionSetString(isa), false);
130 const std::string boot_marker = isa_subdir + "/.booting";
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000131 const char* file_name = boot_marker.c_str();
Narayan Kamath28bc9872014-11-07 17:46:28 +0000132
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000133 uint32_t num_failed_boots = 0;
134 std::unique_ptr<File> file(OS::OpenFileReadWrite(file_name));
135 if (file.get() == nullptr) {
136 file.reset(OS::CreateEmptyFile(file_name));
137
138 if (file.get() == nullptr) {
139 PLOG(WARNING) << "Failed to create boot marker.";
140 return;
141 }
142 } else {
143 if (!file->ReadFully(&num_failed_boots, sizeof(num_failed_boots))) {
144 PLOG(WARNING) << "Failed to read boot marker.";
145 file->Erase();
146 return;
147 }
148 }
149
150 if (max_failed_boots != 0 && num_failed_boots > max_failed_boots) {
Narayan Kamath28bc9872014-11-07 17:46:28 +0000151 LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache";
152 RealPruneDalvikCache(isa_subdir);
153 }
154
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000155 ++num_failed_boots;
156 VLOG(startup) << "Number of failed boots on : " << boot_marker << " = " << num_failed_boots;
157
158 if (lseek(file->Fd(), 0, SEEK_SET) == -1) {
159 PLOG(WARNING) << "Failed to write boot marker.";
160 file->Erase();
161 return;
162 }
163
164 if (!file->WriteFully(&num_failed_boots, sizeof(num_failed_boots))) {
165 PLOG(WARNING) << "Failed to write boot marker.";
166 file->Erase();
167 return;
168 }
169
170 if (file->FlushCloseOrErase() != 0) {
171 PLOG(WARNING) << "Failed to flush boot marker.";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000172 }
173}
174
Alex Light25396132014-08-27 15:37:23 -0700175static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa,
176 std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700177 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
178 std::vector<std::string> boot_class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700179 Split(boot_class_path_string, ':', &boot_class_path);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700180 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700181 *error_msg = "Failed to generate image because no boot class path specified";
182 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700183 }
Alex Light25396132014-08-27 15:37:23 -0700184 // We should clean up so we are more likely to have room for the image.
185 if (Runtime::Current()->IsZygote()) {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700186 LOG(INFO) << "Pruning dalvik-cache since we are generating an image and will need to recompile";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000187 PruneDalvikCache(image_isa);
Alex Light25396132014-08-27 15:37:23 -0700188 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700189
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700190 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700191
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700192 std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
Mathieu Chartier08d7d442013-07-31 18:08:51 -0700193 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700194
195 std::string image_option_string("--image=");
Narayan Kamath52f84882014-05-02 10:10:39 +0100196 image_option_string += image_filename;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700197 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700198
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700199 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700200 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700201 }
202
203 std::string oat_file_option_string("--oat-file=");
Brian Carlstrom2f1e15c2014-10-27 16:27:06 -0700204 oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename);
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700205 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700206
Sebastien Hertz0de11332015-05-13 12:14:05 +0200207 // Note: we do not generate a fully debuggable boot image so we do not pass the
208 // compiler flag --debuggable here.
209
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700210 Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700211 CHECK_EQ(image_isa, kRuntimeISA)
212 << "We should always be generating an image for the current isa.";
Ian Rogers8afeb852014-04-02 14:55:49 -0700213
Alex Lightcf4bf382014-07-24 11:29:14 -0700214 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
215 ART_BASE_ADDRESS_MAX_DELTA);
216 LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default "
217 << "art base address of 0x" << std::hex << ART_BASE_ADDRESS;
218 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700219
Brian Carlstrom57309db2014-07-30 15:13:25 -0700220 if (!kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700221 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700222 }
223
Brian Carlstrom6449c622014-02-10 23:48:36 -0800224 const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800225 for (size_t i = 0; i < compiler_options.size(); ++i) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800226 arg_vector.push_back(compiler_options[i].c_str());
227 }
228
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700229 std::string command_line(Join(arg_vector, ' '));
230 LOG(INFO) << "GenerateImage: " << command_line;
Brian Carlstrom6449c622014-02-10 23:48:36 -0800231 return Exec(arg_vector, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700232}
233
Narayan Kamath52f84882014-05-02 10:10:39 +0100234bool ImageSpace::FindImageFilename(const char* image_location,
235 const InstructionSet image_isa,
Alex Lighta59dd802014-07-02 16:28:08 -0700236 std::string* system_filename,
237 bool* has_system,
238 std::string* cache_filename,
239 bool* dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700240 bool* has_cache,
241 bool* is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -0700242 *has_system = false;
243 *has_cache = false;
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700244 // image_location = /system/framework/boot.art
245 // system_image_location = /system/framework/<image_isa>/boot.art
246 std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
247 if (OS::FileExists(system_image_filename.c_str())) {
Alex Lighta59dd802014-07-02 16:28:08 -0700248 *system_filename = system_image_filename;
249 *has_system = true;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700250 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100251
Alex Lighta59dd802014-07-02 16:28:08 -0700252 bool have_android_data = false;
253 *dalvik_cache_exists = false;
254 std::string dalvik_cache;
255 GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700256 &have_android_data, dalvik_cache_exists, is_global_cache);
Narayan Kamath52f84882014-05-02 10:10:39 +0100257
Alex Lighta59dd802014-07-02 16:28:08 -0700258 if (have_android_data && *dalvik_cache_exists) {
259 // Always set output location even if it does not exist,
260 // so that the caller knows where to create the image.
261 //
262 // image_location = /system/framework/boot.art
263 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
264 std::string error_msg;
265 if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) {
266 LOG(WARNING) << error_msg;
267 return *has_system;
268 }
269 *has_cache = OS::FileExists(cache_filename->c_str());
270 }
271 return *has_system || *has_cache;
272}
273
274static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) {
275 std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
276 if (image_file.get() == nullptr) {
277 return false;
278 }
279 const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader));
280 if (!success || !image_header->IsValid()) {
281 return false;
282 }
283 return true;
284}
285
Alex Light6e183f22014-07-18 14:57:04 -0700286// Relocate the image at image_location to dest_filename and relocate it by a random amount.
287static bool RelocateImage(const char* image_location, const char* dest_filename,
Alex Lighta59dd802014-07-02 16:28:08 -0700288 InstructionSet isa, std::string* error_msg) {
Alex Light25396132014-08-27 15:37:23 -0700289 // We should clean up so we are more likely to have room for the image.
290 if (Runtime::Current()->IsZygote()) {
291 LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000292 PruneDalvikCache(isa);
Alex Light25396132014-08-27 15:37:23 -0700293 }
294
Alex Lighta59dd802014-07-02 16:28:08 -0700295 std::string patchoat(Runtime::Current()->GetPatchoatExecutable());
296
297 std::string input_image_location_arg("--input-image-location=");
298 input_image_location_arg += image_location;
299
300 std::string output_image_filename_arg("--output-image-file=");
301 output_image_filename_arg += dest_filename;
302
303 std::string input_oat_location_arg("--input-oat-location=");
304 input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location);
305
306 std::string output_oat_filename_arg("--output-oat-file=");
307 output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename);
308
309 std::string instruction_set_arg("--instruction-set=");
310 instruction_set_arg += GetInstructionSetString(isa);
311
312 std::string base_offset_arg("--base-offset-delta=");
313 StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
314 ART_BASE_ADDRESS_MAX_DELTA));
315
316 std::vector<std::string> argv;
317 argv.push_back(patchoat);
318
319 argv.push_back(input_image_location_arg);
320 argv.push_back(output_image_filename_arg);
321
322 argv.push_back(input_oat_location_arg);
323 argv.push_back(output_oat_filename_arg);
324
325 argv.push_back(instruction_set_arg);
326 argv.push_back(base_offset_arg);
327
328 std::string command_line(Join(argv, ' '));
329 LOG(INFO) << "RelocateImage: " << command_line;
330 return Exec(argv, error_msg);
331}
332
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700333static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700334 std::unique_ptr<ImageHeader> hdr(new ImageHeader);
335 if (!ReadSpecificImageHeader(filename, hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700336 *error_msg = StringPrintf("Unable to read image header for %s", filename);
Alex Lighta59dd802014-07-02 16:28:08 -0700337 return nullptr;
338 }
339 return hdr.release();
Narayan Kamath52f84882014-05-02 10:10:39 +0100340}
341
342ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location,
343 const InstructionSet image_isa) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700344 std::string error_msg;
345 ImageHeader* image_header = ReadImageHeader(image_location, image_isa, &error_msg);
346 if (image_header == nullptr) {
347 LOG(FATAL) << error_msg;
348 }
349 return image_header;
350}
351
352ImageHeader* ImageSpace::ReadImageHeader(const char* image_location,
353 const InstructionSet image_isa,
354 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700355 std::string system_filename;
356 bool has_system = false;
357 std::string cache_filename;
358 bool has_cache = false;
359 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700360 bool is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -0700361 if (FindImageFilename(image_location, image_isa, &system_filename, &has_system,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700362 &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700363 if (Runtime::Current()->ShouldRelocate()) {
364 if (has_system && has_cache) {
365 std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader);
366 std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader);
367 if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700368 *error_msg = StringPrintf("Unable to read image header for %s at %s",
369 image_location, system_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700370 return nullptr;
371 }
372 if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700373 *error_msg = StringPrintf("Unable to read image header for %s at %s",
374 image_location, cache_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700375 return nullptr;
376 }
377 if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700378 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
379 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700380 return nullptr;
381 }
382 return cache_hdr.release();
383 } else if (!has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700384 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
385 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700386 return nullptr;
387 } else if (!has_system && has_cache) {
388 // This can probably just use the cache one.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700389 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700390 }
391 } else {
392 // We don't want to relocate, Just pick the appropriate one if we have it and return.
393 if (has_system && has_cache) {
394 // We want the cache if the checksum matches, otherwise the system.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700395 std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(),
396 error_msg));
397 std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(),
398 error_msg));
Alex Lighta59dd802014-07-02 16:28:08 -0700399 if (system.get() == nullptr ||
400 (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) {
401 return cache.release();
402 } else {
403 return system.release();
404 }
405 } else if (has_system) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700406 return ReadSpecificImageHeader(system_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700407 } else if (has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700408 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700409 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100410 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100411 }
412
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700413 *error_msg = StringPrintf("Unable to find image file for %s", image_location);
Narayan Kamath52f84882014-05-02 10:10:39 +0100414 return nullptr;
415}
416
Alex Lighta59dd802014-07-02 16:28:08 -0700417static bool ChecksumsMatch(const char* image_a, const char* image_b) {
418 ImageHeader hdr_a;
419 ImageHeader hdr_b;
420 return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b)
421 && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum();
422}
423
Andreas Gampe3c13a792014-09-18 20:56:04 -0700424static bool ImageCreationAllowed(bool is_global_cache, std::string* error_msg) {
425 // Anyone can write into a "local" cache.
426 if (!is_global_cache) {
427 return true;
428 }
429
430 // Only the zygote is allowed to create the global boot image.
431 if (Runtime::Current()->IsZygote()) {
432 return true;
433 }
434
435 *error_msg = "Only the zygote can create the global boot image.";
436 return false;
437}
438
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700439static constexpr uint64_t kLowSpaceValue = 50 * MB;
440static constexpr uint64_t kTmpFsSentinelValue = 384 * MB;
441
442// Read the free space of the cache partition and make a decision whether to keep the generated
443// image. This is to try to mitigate situations where the system might run out of space later.
444static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) {
445 // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes.
446 struct statvfs buf;
447
448 int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf));
449 if (res != 0) {
450 // Could not stat. Conservatively tell the system to delete the image.
451 *error_msg = "Could not stat the filesystem, assuming low-memory situation.";
452 return false;
453 }
454
455 uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks);
456 // Zygote is privileged, but other things are not. Use bavail.
457 uint64_t fs_free_size = buf.f_bsize * static_cast<uint64_t>(buf.f_bavail);
458
459 // Take the overall size as an indicator for a tmpfs, which is being used for the decryption
460 // environment. We do not want to fail quickening the boot image there, as it is beneficial
461 // for time-to-UI.
462 if (fs_overall_size > kTmpFsSentinelValue) {
463 if (fs_free_size < kLowSpaceValue) {
464 *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available after image"
465 " generation, need at least %" PRIu64 ".",
466 static_cast<double>(fs_free_size) / MB,
467 kLowSpaceValue / MB);
468 return false;
469 }
470 }
471 return true;
472}
473
Narayan Kamath52f84882014-05-02 10:10:39 +0100474ImageSpace* ImageSpace::Create(const char* image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700475 const InstructionSet image_isa,
476 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700477 std::string system_filename;
478 bool has_system = false;
479 std::string cache_filename;
480 bool has_cache = false;
481 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700482 bool is_global_cache = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700483 const bool found_image = FindImageFilename(image_location, image_isa, &system_filename,
484 &has_system, &cache_filename, &dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700485 &has_cache, &is_global_cache);
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100486
Narayan Kamath28bc9872014-11-07 17:46:28 +0000487 if (Runtime::Current()->IsZygote()) {
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000488 MarkZygoteStart(image_isa, Runtime::Current()->GetZygoteMaxFailedBoots());
Narayan Kamath28bc9872014-11-07 17:46:28 +0000489 }
490
Alex Lighta59dd802014-07-02 16:28:08 -0700491 ImageSpace* space;
492 bool relocate = Runtime::Current()->ShouldRelocate();
Alex Light64ad14d2014-08-19 14:23:13 -0700493 bool can_compile = Runtime::Current()->IsImageDex2OatEnabled();
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100494 if (found_image) {
Alex Lighta59dd802014-07-02 16:28:08 -0700495 const std::string* image_filename;
496 bool is_system = false;
497 bool relocated_version_used = false;
498 if (relocate) {
Alex Light64ad14d2014-08-19 14:23:13 -0700499 if (!dalvik_cache_exists) {
500 *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have "
501 "any dalvik_cache to find/place it in.",
502 image_location, system_filename.c_str());
503 return nullptr;
504 }
Alex Lighta59dd802014-07-02 16:28:08 -0700505 if (has_system) {
506 if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
507 // We already have a relocated version
508 image_filename = &cache_filename;
509 relocated_version_used = true;
510 } else {
511 // We cannot have a relocated version, Relocate the system one and use it.
Andreas Gampe3c13a792014-09-18 20:56:04 -0700512
513 std::string reason;
514 bool success;
515
516 // Check whether we are allowed to relocate.
517 if (!can_compile) {
518 reason = "Image dex2oat disabled by -Xnoimage-dex2oat.";
519 success = false;
520 } else if (!ImageCreationAllowed(is_global_cache, &reason)) {
521 // Whether we can write to the cache.
522 success = false;
523 } else {
524 // Try to relocate.
525 success = RelocateImage(image_location, cache_filename.c_str(), image_isa, &reason);
526 }
527
528 if (success) {
Alex Lighta59dd802014-07-02 16:28:08 -0700529 relocated_version_used = true;
530 image_filename = &cache_filename;
531 } else {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700532 *error_msg = StringPrintf("Unable to relocate image '%s' from '%s' to '%s': %s",
Alex Light64ad14d2014-08-19 14:23:13 -0700533 image_location, system_filename.c_str(),
534 cache_filename.c_str(), reason.c_str());
Brian Carlstrome9105f72014-10-28 15:53:43 -0700535 // We failed to create files, remove any possibly garbage output.
536 // Since ImageCreationAllowed was true above, we are the zygote
537 // and therefore the only process expected to generate these for
538 // the device.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000539 PruneDalvikCache(image_isa);
Alex Lighta59dd802014-07-02 16:28:08 -0700540 return nullptr;
541 }
542 }
543 } else {
544 CHECK(has_cache);
545 // We can just use cache's since it should be fine. This might or might not be relocated.
546 image_filename = &cache_filename;
547 }
548 } else {
549 if (has_system && has_cache) {
550 // Check they have the same cksum. If they do use the cache. Otherwise system.
551 if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
552 image_filename = &cache_filename;
553 relocated_version_used = true;
554 } else {
555 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700556 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700557 }
558 } else if (has_system) {
559 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700560 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700561 } else {
562 CHECK(has_cache);
563 image_filename = &cache_filename;
564 }
565 }
566 {
567 // Note that we must not use the file descriptor associated with
568 // ScopedFlock::GetFile to Init the image file. We want the file
569 // descriptor (and the associated exclusive lock) to be released when
570 // we leave Create.
571 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700572 image_lock.Init(image_filename->c_str(), error_msg);
Alex Lightb6cabc12014-08-21 09:45:00 -0700573 VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location "
574 << image_location;
Alex Lightb93637a2014-07-31 10:48:46 -0700575 // If we are in /system we can assume the image is good. We can also
576 // assume this if we are using a relocated image (i.e. image checksum
577 // matches) since this is only different by the offset. We need this to
578 // make sure that host tests continue to work.
Alex Lighta59dd802014-07-02 16:28:08 -0700579 space = ImageSpace::Init(image_filename->c_str(), image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700580 !(is_system || relocated_version_used), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700581 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100582 if (space != nullptr) {
583 return space;
584 }
585
Alex Lighta59dd802014-07-02 16:28:08 -0700586 if (relocated_version_used) {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700587 // Something is wrong with the relocated copy (even though checksums match). Cleanup.
588 // This can happen if the .oat is corrupt, since the above only checks the .art checksums.
589 // TODO: Check the oat file validity earlier.
590 *error_msg = StringPrintf("Attempted to use relocated version of %s at %s generated from %s "
591 "but image failed to load: %s",
592 image_location, cache_filename.c_str(), system_filename.c_str(),
593 error_msg->c_str());
Narayan Kamath28bc9872014-11-07 17:46:28 +0000594 PruneDalvikCache(image_isa);
Alex Lighta59dd802014-07-02 16:28:08 -0700595 return nullptr;
596 } else if (is_system) {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700597 // If the /system file exists, it should be up-to-date, don't try to generate it.
Alex Light64ad14d2014-08-19 14:23:13 -0700598 *error_msg = StringPrintf("Failed to load /system image '%s': %s",
599 image_filename->c_str(), error_msg->c_str());
Narayan Kamath52f84882014-05-02 10:10:39 +0100600 return nullptr;
Mathieu Chartierc7cb1902014-03-05 14:41:03 -0800601 } else {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700602 // Otherwise, log a warning and fall through to GenerateImage.
Alex Light64ad14d2014-08-19 14:23:13 -0700603 LOG(WARNING) << *error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700604 }
605 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100606
Alex Light64ad14d2014-08-19 14:23:13 -0700607 if (!can_compile) {
608 *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat";
609 return nullptr;
610 } else if (!dalvik_cache_exists) {
611 *error_msg = StringPrintf("No place to put generated image.");
612 return nullptr;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700613 } else if (!ImageCreationAllowed(is_global_cache, error_msg)) {
614 return nullptr;
Alex Light25396132014-08-27 15:37:23 -0700615 } else if (!GenerateImage(cache_filename, image_isa, error_msg)) {
Alex Light64ad14d2014-08-19 14:23:13 -0700616 *error_msg = StringPrintf("Failed to generate image '%s': %s",
617 cache_filename.c_str(), error_msg->c_str());
Brian Carlstrome9105f72014-10-28 15:53:43 -0700618 // We failed to create files, remove any possibly garbage output.
619 // Since ImageCreationAllowed was true above, we are the zygote
620 // and therefore the only process expected to generate these for
621 // the device.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000622 PruneDalvikCache(image_isa);
Alex Light64ad14d2014-08-19 14:23:13 -0700623 return nullptr;
624 } else {
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700625 // Check whether there is enough space left over after we have generated the image.
626 if (!CheckSpace(cache_filename, error_msg)) {
627 // No. Delete the generated image and try to run out of the dex files.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000628 PruneDalvikCache(image_isa);
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700629 return nullptr;
630 }
631
Alex Lighta59dd802014-07-02 16:28:08 -0700632 // Note that we must not use the file descriptor associated with
633 // ScopedFlock::GetFile to Init the image file. We want the file
634 // descriptor (and the associated exclusive lock) to be released when
635 // we leave Create.
636 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700637 image_lock.Init(cache_filename.c_str(), error_msg);
638 space = ImageSpace::Init(cache_filename.c_str(), image_location, true, error_msg);
639 if (space == nullptr) {
640 *error_msg = StringPrintf("Failed to load generated image '%s': %s",
641 cache_filename.c_str(), error_msg->c_str());
642 }
643 return space;
Alex Lighta59dd802014-07-02 16:28:08 -0700644 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700645}
646
Mathieu Chartier31e89252013-08-28 11:29:12 -0700647void ImageSpace::VerifyImageAllocations() {
Ian Rogers13735952014-10-08 12:43:28 -0700648 uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700649 while (current < End()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700650 CHECK_ALIGNED(current, kObjectAlignment);
651 auto* obj = reinterpret_cast<mirror::Object*>(current);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700652 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Mathieu Chartierc7853442015-03-27 14:35:38 -0700653 CHECK(live_bitmap_->Test(obj)) << PrettyTypeOf(obj);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700654 if (kUseBakerOrBrooksReadBarrier) {
655 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800656 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700657 current += RoundUp(obj->SizeOf(), kObjectAlignment);
658 }
659}
660
Narayan Kamath52f84882014-05-02 10:10:39 +0100661ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location,
662 bool validate_oat_file, std::string* error_msg) {
663 CHECK(image_filename != nullptr);
664 CHECK(image_location != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700665
666 uint64_t start_time = 0;
667 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
668 start_time = NanoTime();
Narayan Kamath52f84882014-05-02 10:10:39 +0100669 LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename;
Ian Rogers1d54e732013-05-02 21:10:01 -0700670 }
671
Ian Rogers700a4022014-05-19 16:49:03 -0700672 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700673 if (file.get() == nullptr) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100674 *error_msg = StringPrintf("Failed to open '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700675 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700676 }
677 ImageHeader image_header;
678 bool success = file->ReadFully(&image_header, sizeof(image_header));
679 if (!success || !image_header.IsValid()) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100680 *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700681 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700682 }
Andreas Gampe6c8b49f2015-02-19 11:42:36 -0800683 // Check that the file is large enough.
684 uint64_t image_file_size = static_cast<uint64_t>(file->GetLength());
685 if (image_header.GetImageSize() > image_file_size) {
686 *error_msg = StringPrintf("Image file too small for image heap: %" PRIu64 " vs. %zu.",
687 image_file_size, image_header.GetImageSize());
688 return nullptr;
689 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700690
691 if (kIsDebugBuild) {
692 LOG(INFO) << "Dumping image sections";
693 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
694 const auto section_idx = static_cast<ImageHeader::ImageSections>(i);
695 auto& section = image_header.GetImageSection(section_idx);
696 LOG(INFO) << section_idx << " start="
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700697 << reinterpret_cast<void*>(image_header.GetImageBegin() + section.Offset()) << " "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700698 << section;
699 }
700 }
701
702 const auto& bitmap_section = image_header.GetImageSection(ImageHeader::kSectionImageBitmap);
703 auto end_of_bitmap = static_cast<size_t>(bitmap_section.End());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700704 if (end_of_bitmap != image_file_size) {
705 *error_msg = StringPrintf(
706 "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.", image_file_size,
707 end_of_bitmap);
Andreas Gampe6c8b49f2015-02-19 11:42:36 -0800708 return nullptr;
709 }
710
Mathieu Chartier31e89252013-08-28 11:29:12 -0700711 // Note: The image header is part of the image due to mmap page alignment required of offset.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700712 std::unique_ptr<MemMap> map(MemMap::MapFileAtAddress(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700713 image_header.GetImageBegin(), image_header.GetImageSize(),
Mathieu Chartierc7853442015-03-27 14:35:38 -0700714 PROT_READ | PROT_WRITE, MAP_PRIVATE, file->Fd(), 0, false, image_filename, error_msg));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700715 if (map.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700716 DCHECK(!error_msg->empty());
717 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700718 }
719 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
720 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
721
Mathieu Chartiere401d142015-04-22 13:56:20 -0700722 std::unique_ptr<MemMap> image_map(MemMap::MapFileAtAddress(
723 nullptr, bitmap_section.Size(), PROT_READ, MAP_PRIVATE, file->Fd(),
724 bitmap_section.Offset(), false, image_filename, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700725 if (image_map.get() == nullptr) {
726 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
727 return nullptr;
728 }
Ian Rogers3e5cf302014-05-20 16:40:37 -0700729 uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1);
Narayan Kamath52f84882014-05-02 10:10:39 +0100730 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700731 bitmap_index));
Ian Rogers700a4022014-05-19 16:49:03 -0700732 std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700733 accounting::ContinuousSpaceBitmap::CreateFromMemMap(
734 bitmap_name, image_map.release(), reinterpret_cast<uint8_t*>(map->Begin()),
735 accounting::ContinuousSpaceBitmap::ComputeHeapSize(bitmap_section.Size())));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700736 if (bitmap.get() == nullptr) {
737 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
738 return nullptr;
739 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700740
Mathieu Chartiere401d142015-04-22 13:56:20 -0700741 // We only want the mirror object, not the ArtFields and ArtMethods.
742 uint8_t* const image_end =
743 map->Begin() + image_header.GetImageSection(ImageHeader::kSectionObjects).End();
Ian Rogers700a4022014-05-19 16:49:03 -0700744 std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, image_location,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700745 map.release(), bitmap.release(), image_end));
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700746
747 // VerifyImageAllocations() will be called later in Runtime::Init()
748 // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
749 // and ArtField::java_lang_reflect_ArtField_, which are used from
750 // Object::SizeOf() which VerifyImageAllocations() calls, are not
751 // set yet at this point.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700752
Narayan Kamath52f84882014-05-02 10:10:39 +0100753 space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700754 if (space->oat_file_.get() == nullptr) {
755 DCHECK(!error_msg->empty());
756 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700757 }
Andreas Gampe88da3b02015-06-12 20:38:49 -0700758 space->oat_file_non_owned_ = space->oat_file_.get();
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700759
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700760 if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
761 DCHECK(!error_msg->empty());
762 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700763 }
764
Vladimir Marko7624d252014-05-02 14:40:15 +0100765 Runtime* runtime = Runtime::Current();
766 runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet());
767
Mathieu Chartiere401d142015-04-22 13:56:20 -0700768 runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod));
769 runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod));
770 runtime->SetImtUnimplementedMethod(
771 image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod));
772 runtime->SetCalleeSaveMethod(
773 image_header.GetImageMethod(ImageHeader::kCalleeSaveMethod), Runtime::kSaveAll);
774 runtime->SetCalleeSaveMethod(
775 image_header.GetImageMethod(ImageHeader::kRefsOnlySaveMethod), Runtime::kRefsOnly);
776 runtime->SetCalleeSaveMethod(
777 image_header.GetImageMethod(ImageHeader::kRefsAndArgsSaveMethod), Runtime::kRefsAndArgs);
Vladimir Marko7624d252014-05-02 14:40:15 +0100778
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700779 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
780 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
781 << ") " << *space.get();
782 }
783 return space.release();
784}
785
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000786OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700787 const ImageHeader& image_header = GetImageHeader();
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000788 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
789
Igor Murashkin46774762014-10-22 11:37:02 -0700790 CHECK(image_header.GetOatDataBegin() != nullptr);
791
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700792 OatFile* oat_file = OatFile::Open(oat_filename,
793 oat_filename,
794 image_header.GetOatDataBegin(),
Igor Murashkin46774762014-10-22 11:37:02 -0700795 image_header.GetOatFileBegin(),
Richard Uhlere5fed032015-03-18 08:21:11 -0700796 !Runtime::Current()->IsAotCompiler(),
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700797 nullptr,
798 error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700799 if (oat_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700800 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
801 oat_filename.c_str(), GetName(), error_msg->c_str());
802 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700803 }
804 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
805 uint32_t image_oat_checksum = image_header.GetOatChecksum();
806 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700807 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
808 " in image %s", oat_checksum, image_oat_checksum, GetName());
809 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700810 }
Alex Lighta59dd802014-07-02 16:28:08 -0700811 int32_t image_patch_delta = image_header.GetPatchDelta();
812 int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta();
Igor Murashkin46774762014-10-22 11:37:02 -0700813 if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700814 // We should have already relocated by this point. Bail out.
815 *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d "
816 "in image %s", oat_patch_delta, image_patch_delta, GetName());
817 return nullptr;
818 }
819
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700820 return oat_file;
821}
822
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700823bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700824 CHECK(oat_file_.get() != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700825 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700826 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
827 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700828 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
829 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
830 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700831 return false;
832 }
833 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700834 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
835 "dex file '%s' (0x%x != 0x%x)",
836 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
837 oat_dex_file->GetDexFileLocationChecksum(),
838 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700839 return false;
840 }
841 }
842 return true;
843}
844
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700845const OatFile* ImageSpace::GetOatFile() const {
Andreas Gampe88da3b02015-06-12 20:38:49 -0700846 return oat_file_non_owned_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700847}
848
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700849std::unique_ptr<const OatFile> ImageSpace::ReleaseOatFile() {
850 CHECK(oat_file_ != nullptr);
851 return std::move(oat_file_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700852}
853
Ian Rogers1d54e732013-05-02 21:10:01 -0700854void ImageSpace::Dump(std::ostream& os) const {
855 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700856 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -0700857 << ",end=" << reinterpret_cast<void*>(End())
858 << ",size=" << PrettySize(Size())
859 << ",name=\"" << GetName() << "\"]";
860}
861
862} // namespace space
863} // namespace gc
864} // namespace art