blob: aba32a003f58abb79cf9cdb978ae84925c8374fc [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"
Igor Murashkina315f5c2015-07-31 17:35:52 -070028#include "base/out.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070029#include "base/stl_util.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010030#include "base/scoped_flock.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010031#include "base/time_utils.h"
32#include "base/unix_file/fd_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "mirror/class-inl.h"
35#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070036#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037#include "os.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070038#include "space-inl.h"
39#include "utils.h"
40
41namespace art {
42namespace gc {
43namespace space {
44
Ian Rogersef7d42f2014-01-06 12:55:46 -080045Atomic<uint32_t> ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070046
Narayan Kamath52f84882014-05-02 10:10:39 +010047ImageSpace::ImageSpace(const std::string& image_filename, const char* image_location,
Mathieu Chartierc7853442015-03-27 14:35:38 -070048 MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap,
49 uint8_t* end)
50 : MemMapSpace(image_filename, mem_map, mem_map->Begin(), end, end,
Narayan Kamath52f84882014-05-02 10:10:39 +010051 kGcRetentionPolicyNeverCollect),
52 image_location_(image_location) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070053 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070054 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070055}
56
Alex Lightcf4bf382014-07-24 11:29:14 -070057static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
58 CHECK_ALIGNED(min_delta, kPageSize);
59 CHECK_ALIGNED(max_delta, kPageSize);
60 CHECK_LT(min_delta, max_delta);
61
62 std::default_random_engine generator;
63 generator.seed(NanoTime() * getpid());
64 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
65 int32_t r = distribution(generator);
66 if (r % 2 == 0) {
67 r = RoundUp(r, kPageSize);
68 } else {
69 r = RoundDown(r, kPageSize);
70 }
71 CHECK_LE(min_delta, r);
72 CHECK_GE(max_delta, r);
73 CHECK_ALIGNED(r, kPageSize);
74 return r;
75}
76
Alex Light25396132014-08-27 15:37:23 -070077// We are relocating or generating the core image. We should get rid of everything. It is all
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080078// 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 -070079// Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
80// Note this should only be used during first boot.
Narayan Kamath28bc9872014-11-07 17:46:28 +000081static void RealPruneDalvikCache(const std::string& cache_dir_path);
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080082
Narayan Kamath28bc9872014-11-07 17:46:28 +000083static void PruneDalvikCache(InstructionSet isa) {
Alex Light25396132014-08-27 15:37:23 -070084 CHECK_NE(isa, kNone);
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080085 // Prune the base /data/dalvik-cache.
Narayan Kamath28bc9872014-11-07 17:46:28 +000086 RealPruneDalvikCache(GetDalvikCacheOrDie(".", false));
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080087 // Prune /data/dalvik-cache/<isa>.
Narayan Kamath28bc9872014-11-07 17:46:28 +000088 RealPruneDalvikCache(GetDalvikCacheOrDie(GetInstructionSetString(isa), false));
Alex Light25396132014-08-27 15:37:23 -070089}
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080090
Narayan Kamath28bc9872014-11-07 17:46:28 +000091static void RealPruneDalvikCache(const std::string& cache_dir_path) {
Alex Light25396132014-08-27 15:37:23 -070092 if (!OS::DirectoryExists(cache_dir_path.c_str())) {
93 return;
94 }
95 DIR* cache_dir = opendir(cache_dir_path.c_str());
96 if (cache_dir == nullptr) {
97 PLOG(WARNING) << "Unable to open " << cache_dir_path << " to delete it's contents";
98 return;
99 }
Alex Light25396132014-08-27 15:37:23 -0700100
101 for (struct dirent* de = readdir(cache_dir); de != nullptr; de = readdir(cache_dir)) {
102 const char* name = de->d_name;
103 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
104 continue;
105 }
Andreas Gampe8db9dcd2014-11-09 18:14:30 -0800106 // We only want to delete regular files and symbolic links.
107 if (de->d_type != DT_REG && de->d_type != DT_LNK) {
Alex Light25396132014-08-27 15:37:23 -0700108 if (de->d_type != DT_DIR) {
109 // We do expect some directories (namely the <isa> for pruning the base dalvik-cache).
110 LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
111 }
112 continue;
113 }
Brian Carlstromdebdda02014-08-28 22:17:13 -0700114 std::string cache_file(cache_dir_path);
115 cache_file += '/';
116 cache_file += name;
117 if (TEMP_FAILURE_RETRY(unlink(cache_file.c_str())) != 0) {
118 PLOG(ERROR) << "Unable to unlink " << cache_file;
Alex Light25396132014-08-27 15:37:23 -0700119 continue;
120 }
121 }
122 CHECK_EQ(0, TEMP_FAILURE_RETRY(closedir(cache_dir))) << "Unable to close directory.";
123}
124
Narayan Kamath28bc9872014-11-07 17:46:28 +0000125// We write out an empty file to the zygote's ISA specific cache dir at the start of
126// every zygote boot and delete it when the boot completes. If we find a file already
127// present, it usually means the boot didn't complete. We wipe the entire dalvik
128// cache if that's the case.
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000129static void MarkZygoteStart(const InstructionSet isa, const uint32_t max_failed_boots) {
Narayan Kamath28bc9872014-11-07 17:46:28 +0000130 const std::string isa_subdir = GetDalvikCacheOrDie(GetInstructionSetString(isa), false);
131 const std::string boot_marker = isa_subdir + "/.booting";
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000132 const char* file_name = boot_marker.c_str();
Narayan Kamath28bc9872014-11-07 17:46:28 +0000133
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000134 uint32_t num_failed_boots = 0;
135 std::unique_ptr<File> file(OS::OpenFileReadWrite(file_name));
136 if (file.get() == nullptr) {
137 file.reset(OS::CreateEmptyFile(file_name));
138
139 if (file.get() == nullptr) {
140 PLOG(WARNING) << "Failed to create boot marker.";
141 return;
142 }
143 } else {
144 if (!file->ReadFully(&num_failed_boots, sizeof(num_failed_boots))) {
145 PLOG(WARNING) << "Failed to read boot marker.";
146 file->Erase();
147 return;
148 }
149 }
150
151 if (max_failed_boots != 0 && num_failed_boots > max_failed_boots) {
Narayan Kamath28bc9872014-11-07 17:46:28 +0000152 LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache";
153 RealPruneDalvikCache(isa_subdir);
154 }
155
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000156 ++num_failed_boots;
157 VLOG(startup) << "Number of failed boots on : " << boot_marker << " = " << num_failed_boots;
158
159 if (lseek(file->Fd(), 0, SEEK_SET) == -1) {
160 PLOG(WARNING) << "Failed to write boot marker.";
161 file->Erase();
162 return;
163 }
164
165 if (!file->WriteFully(&num_failed_boots, sizeof(num_failed_boots))) {
166 PLOG(WARNING) << "Failed to write boot marker.";
167 file->Erase();
168 return;
169 }
170
171 if (file->FlushCloseOrErase() != 0) {
172 PLOG(WARNING) << "Failed to flush boot marker.";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000173 }
174}
175
Alex Light25396132014-08-27 15:37:23 -0700176static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa,
177 std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700178 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
179 std::vector<std::string> boot_class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700180 Split(boot_class_path_string, ':', &boot_class_path);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700181 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700182 *error_msg = "Failed to generate image because no boot class path specified";
183 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700184 }
Alex Light25396132014-08-27 15:37:23 -0700185 // We should clean up so we are more likely to have room for the image.
186 if (Runtime::Current()->IsZygote()) {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700187 LOG(INFO) << "Pruning dalvik-cache since we are generating an image and will need to recompile";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000188 PruneDalvikCache(image_isa);
Alex Light25396132014-08-27 15:37:23 -0700189 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700190
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700191 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700192
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700193 std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
Mathieu Chartier08d7d442013-07-31 18:08:51 -0700194 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700195
196 std::string image_option_string("--image=");
Narayan Kamath52f84882014-05-02 10:10:39 +0100197 image_option_string += image_filename;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700198 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700199
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700200 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700201 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700202 }
203
204 std::string oat_file_option_string("--oat-file=");
Brian Carlstrom2f1e15c2014-10-27 16:27:06 -0700205 oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename);
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700206 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700207
Sebastien Hertz0de11332015-05-13 12:14:05 +0200208 // Note: we do not generate a fully debuggable boot image so we do not pass the
209 // compiler flag --debuggable here.
210
Igor Murashkina315f5c2015-07-31 17:35:52 -0700211 Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(outof(arg_vector));
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700212 CHECK_EQ(image_isa, kRuntimeISA)
213 << "We should always be generating an image for the current isa.";
Ian Rogers8afeb852014-04-02 14:55:49 -0700214
Alex Lightcf4bf382014-07-24 11:29:14 -0700215 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
216 ART_BASE_ADDRESS_MAX_DELTA);
217 LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default "
218 << "art base address of 0x" << std::hex << ART_BASE_ADDRESS;
219 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700220
Brian Carlstrom57309db2014-07-30 15:13:25 -0700221 if (!kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700222 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700223 }
224
Brian Carlstrom6449c622014-02-10 23:48:36 -0800225 const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800226 for (size_t i = 0; i < compiler_options.size(); ++i) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800227 arg_vector.push_back(compiler_options[i].c_str());
228 }
229
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700230 std::string command_line(Join(arg_vector, ' '));
231 LOG(INFO) << "GenerateImage: " << command_line;
Brian Carlstrom6449c622014-02-10 23:48:36 -0800232 return Exec(arg_vector, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700233}
234
Narayan Kamath52f84882014-05-02 10:10:39 +0100235bool ImageSpace::FindImageFilename(const char* image_location,
236 const InstructionSet image_isa,
Alex Lighta59dd802014-07-02 16:28:08 -0700237 std::string* system_filename,
238 bool* has_system,
239 std::string* cache_filename,
240 bool* dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700241 bool* has_cache,
242 bool* is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -0700243 *has_system = false;
244 *has_cache = false;
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700245 // image_location = /system/framework/boot.art
246 // system_image_location = /system/framework/<image_isa>/boot.art
247 std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
248 if (OS::FileExists(system_image_filename.c_str())) {
Alex Lighta59dd802014-07-02 16:28:08 -0700249 *system_filename = system_image_filename;
250 *has_system = true;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700251 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100252
Alex Lighta59dd802014-07-02 16:28:08 -0700253 bool have_android_data = false;
254 *dalvik_cache_exists = false;
255 std::string dalvik_cache;
256 GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700257 &have_android_data, dalvik_cache_exists, is_global_cache);
Narayan Kamath52f84882014-05-02 10:10:39 +0100258
Alex Lighta59dd802014-07-02 16:28:08 -0700259 if (have_android_data && *dalvik_cache_exists) {
260 // Always set output location even if it does not exist,
261 // so that the caller knows where to create the image.
262 //
263 // image_location = /system/framework/boot.art
264 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
265 std::string error_msg;
266 if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) {
267 LOG(WARNING) << error_msg;
268 return *has_system;
269 }
270 *has_cache = OS::FileExists(cache_filename->c_str());
271 }
272 return *has_system || *has_cache;
273}
274
275static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) {
276 std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
277 if (image_file.get() == nullptr) {
278 return false;
279 }
280 const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader));
281 if (!success || !image_header->IsValid()) {
282 return false;
283 }
284 return true;
285}
286
Alex Light6e183f22014-07-18 14:57:04 -0700287// Relocate the image at image_location to dest_filename and relocate it by a random amount.
288static bool RelocateImage(const char* image_location, const char* dest_filename,
Alex Lighta59dd802014-07-02 16:28:08 -0700289 InstructionSet isa, std::string* error_msg) {
Alex Light25396132014-08-27 15:37:23 -0700290 // We should clean up so we are more likely to have room for the image.
291 if (Runtime::Current()->IsZygote()) {
292 LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000293 PruneDalvikCache(isa);
Alex Light25396132014-08-27 15:37:23 -0700294 }
295
Alex Lighta59dd802014-07-02 16:28:08 -0700296 std::string patchoat(Runtime::Current()->GetPatchoatExecutable());
297
298 std::string input_image_location_arg("--input-image-location=");
299 input_image_location_arg += image_location;
300
301 std::string output_image_filename_arg("--output-image-file=");
302 output_image_filename_arg += dest_filename;
303
304 std::string input_oat_location_arg("--input-oat-location=");
305 input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location);
306
307 std::string output_oat_filename_arg("--output-oat-file=");
308 output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename);
309
310 std::string instruction_set_arg("--instruction-set=");
311 instruction_set_arg += GetInstructionSetString(isa);
312
313 std::string base_offset_arg("--base-offset-delta=");
314 StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
315 ART_BASE_ADDRESS_MAX_DELTA));
316
317 std::vector<std::string> argv;
318 argv.push_back(patchoat);
319
320 argv.push_back(input_image_location_arg);
321 argv.push_back(output_image_filename_arg);
322
323 argv.push_back(input_oat_location_arg);
324 argv.push_back(output_oat_filename_arg);
325
326 argv.push_back(instruction_set_arg);
327 argv.push_back(base_offset_arg);
328
329 std::string command_line(Join(argv, ' '));
330 LOG(INFO) << "RelocateImage: " << command_line;
331 return Exec(argv, error_msg);
332}
333
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700334static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700335 std::unique_ptr<ImageHeader> hdr(new ImageHeader);
336 if (!ReadSpecificImageHeader(filename, hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700337 *error_msg = StringPrintf("Unable to read image header for %s", filename);
Alex Lighta59dd802014-07-02 16:28:08 -0700338 return nullptr;
339 }
340 return hdr.release();
Narayan Kamath52f84882014-05-02 10:10:39 +0100341}
342
343ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location,
344 const InstructionSet image_isa) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700345 std::string error_msg;
346 ImageHeader* image_header = ReadImageHeader(image_location, image_isa, &error_msg);
347 if (image_header == nullptr) {
348 LOG(FATAL) << error_msg;
349 }
350 return image_header;
351}
352
353ImageHeader* ImageSpace::ReadImageHeader(const char* image_location,
354 const InstructionSet image_isa,
355 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700356 std::string system_filename;
357 bool has_system = false;
358 std::string cache_filename;
359 bool has_cache = false;
360 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700361 bool is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -0700362 if (FindImageFilename(image_location, image_isa, &system_filename, &has_system,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700363 &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700364 if (Runtime::Current()->ShouldRelocate()) {
365 if (has_system && has_cache) {
366 std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader);
367 std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader);
368 if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700369 *error_msg = StringPrintf("Unable to read image header for %s at %s",
370 image_location, system_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700371 return nullptr;
372 }
373 if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700374 *error_msg = StringPrintf("Unable to read image header for %s at %s",
375 image_location, cache_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700376 return nullptr;
377 }
378 if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700379 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
380 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700381 return nullptr;
382 }
383 return cache_hdr.release();
384 } else if (!has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700385 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
386 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700387 return nullptr;
388 } else if (!has_system && has_cache) {
389 // This can probably just use the cache one.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700390 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700391 }
392 } else {
393 // We don't want to relocate, Just pick the appropriate one if we have it and return.
394 if (has_system && has_cache) {
395 // We want the cache if the checksum matches, otherwise the system.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700396 std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(),
397 error_msg));
398 std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(),
399 error_msg));
Alex Lighta59dd802014-07-02 16:28:08 -0700400 if (system.get() == nullptr ||
401 (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) {
402 return cache.release();
403 } else {
404 return system.release();
405 }
406 } else if (has_system) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700407 return ReadSpecificImageHeader(system_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700408 } else if (has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700409 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700410 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100411 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100412 }
413
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700414 *error_msg = StringPrintf("Unable to find image file for %s", image_location);
Narayan Kamath52f84882014-05-02 10:10:39 +0100415 return nullptr;
416}
417
Alex Lighta59dd802014-07-02 16:28:08 -0700418static bool ChecksumsMatch(const char* image_a, const char* image_b) {
419 ImageHeader hdr_a;
420 ImageHeader hdr_b;
421 return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b)
422 && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum();
423}
424
Andreas Gampe3c13a792014-09-18 20:56:04 -0700425static bool ImageCreationAllowed(bool is_global_cache, std::string* error_msg) {
426 // Anyone can write into a "local" cache.
427 if (!is_global_cache) {
428 return true;
429 }
430
431 // Only the zygote is allowed to create the global boot image.
432 if (Runtime::Current()->IsZygote()) {
433 return true;
434 }
435
436 *error_msg = "Only the zygote can create the global boot image.";
437 return false;
438}
439
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700440static constexpr uint64_t kLowSpaceValue = 50 * MB;
441static constexpr uint64_t kTmpFsSentinelValue = 384 * MB;
442
443// Read the free space of the cache partition and make a decision whether to keep the generated
444// image. This is to try to mitigate situations where the system might run out of space later.
445static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) {
446 // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes.
447 struct statvfs buf;
448
449 int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf));
450 if (res != 0) {
451 // Could not stat. Conservatively tell the system to delete the image.
452 *error_msg = "Could not stat the filesystem, assuming low-memory situation.";
453 return false;
454 }
455
456 uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks);
457 // Zygote is privileged, but other things are not. Use bavail.
458 uint64_t fs_free_size = buf.f_bsize * static_cast<uint64_t>(buf.f_bavail);
459
460 // Take the overall size as an indicator for a tmpfs, which is being used for the decryption
461 // environment. We do not want to fail quickening the boot image there, as it is beneficial
462 // for time-to-UI.
463 if (fs_overall_size > kTmpFsSentinelValue) {
464 if (fs_free_size < kLowSpaceValue) {
465 *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available after image"
466 " generation, need at least %" PRIu64 ".",
467 static_cast<double>(fs_free_size) / MB,
468 kLowSpaceValue / MB);
469 return false;
470 }
471 }
472 return true;
473}
474
Narayan Kamath52f84882014-05-02 10:10:39 +0100475ImageSpace* ImageSpace::Create(const char* image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700476 const InstructionSet image_isa,
477 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700478 std::string system_filename;
479 bool has_system = false;
480 std::string cache_filename;
481 bool has_cache = false;
482 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700483 bool is_global_cache = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700484 const bool found_image = FindImageFilename(image_location, image_isa, &system_filename,
485 &has_system, &cache_filename, &dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700486 &has_cache, &is_global_cache);
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100487
Narayan Kamath28bc9872014-11-07 17:46:28 +0000488 if (Runtime::Current()->IsZygote()) {
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000489 MarkZygoteStart(image_isa, Runtime::Current()->GetZygoteMaxFailedBoots());
Narayan Kamath28bc9872014-11-07 17:46:28 +0000490 }
491
Alex Lighta59dd802014-07-02 16:28:08 -0700492 ImageSpace* space;
493 bool relocate = Runtime::Current()->ShouldRelocate();
Alex Light64ad14d2014-08-19 14:23:13 -0700494 bool can_compile = Runtime::Current()->IsImageDex2OatEnabled();
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100495 if (found_image) {
Alex Lighta59dd802014-07-02 16:28:08 -0700496 const std::string* image_filename;
497 bool is_system = false;
498 bool relocated_version_used = false;
499 if (relocate) {
Alex Light64ad14d2014-08-19 14:23:13 -0700500 if (!dalvik_cache_exists) {
501 *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have "
502 "any dalvik_cache to find/place it in.",
503 image_location, system_filename.c_str());
504 return nullptr;
505 }
Alex Lighta59dd802014-07-02 16:28:08 -0700506 if (has_system) {
507 if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
508 // We already have a relocated version
509 image_filename = &cache_filename;
510 relocated_version_used = true;
511 } else {
512 // We cannot have a relocated version, Relocate the system one and use it.
Andreas Gampe3c13a792014-09-18 20:56:04 -0700513
514 std::string reason;
515 bool success;
516
517 // Check whether we are allowed to relocate.
518 if (!can_compile) {
519 reason = "Image dex2oat disabled by -Xnoimage-dex2oat.";
520 success = false;
521 } else if (!ImageCreationAllowed(is_global_cache, &reason)) {
522 // Whether we can write to the cache.
523 success = false;
524 } else {
525 // Try to relocate.
526 success = RelocateImage(image_location, cache_filename.c_str(), image_isa, &reason);
527 }
528
529 if (success) {
Alex Lighta59dd802014-07-02 16:28:08 -0700530 relocated_version_used = true;
531 image_filename = &cache_filename;
532 } else {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700533 *error_msg = StringPrintf("Unable to relocate image '%s' from '%s' to '%s': %s",
Alex Light64ad14d2014-08-19 14:23:13 -0700534 image_location, system_filename.c_str(),
535 cache_filename.c_str(), reason.c_str());
Brian Carlstrome9105f72014-10-28 15:53:43 -0700536 // We failed to create files, remove any possibly garbage output.
537 // Since ImageCreationAllowed was true above, we are the zygote
538 // and therefore the only process expected to generate these for
539 // the device.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000540 PruneDalvikCache(image_isa);
Alex Lighta59dd802014-07-02 16:28:08 -0700541 return nullptr;
542 }
543 }
544 } else {
545 CHECK(has_cache);
546 // We can just use cache's since it should be fine. This might or might not be relocated.
547 image_filename = &cache_filename;
548 }
549 } else {
550 if (has_system && has_cache) {
551 // Check they have the same cksum. If they do use the cache. Otherwise system.
552 if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
553 image_filename = &cache_filename;
554 relocated_version_used = true;
555 } else {
556 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700557 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700558 }
559 } else if (has_system) {
560 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700561 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700562 } else {
563 CHECK(has_cache);
564 image_filename = &cache_filename;
565 }
566 }
567 {
568 // Note that we must not use the file descriptor associated with
569 // ScopedFlock::GetFile to Init the image file. We want the file
570 // descriptor (and the associated exclusive lock) to be released when
571 // we leave Create.
572 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700573 image_lock.Init(image_filename->c_str(), error_msg);
Alex Lightb6cabc12014-08-21 09:45:00 -0700574 VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location "
575 << image_location;
Alex Lightb93637a2014-07-31 10:48:46 -0700576 // If we are in /system we can assume the image is good. We can also
577 // assume this if we are using a relocated image (i.e. image checksum
578 // matches) since this is only different by the offset. We need this to
579 // make sure that host tests continue to work.
Alex Lighta59dd802014-07-02 16:28:08 -0700580 space = ImageSpace::Init(image_filename->c_str(), image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700581 !(is_system || relocated_version_used), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700582 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100583 if (space != nullptr) {
584 return space;
585 }
586
Alex Lighta59dd802014-07-02 16:28:08 -0700587 if (relocated_version_used) {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700588 // Something is wrong with the relocated copy (even though checksums match). Cleanup.
589 // This can happen if the .oat is corrupt, since the above only checks the .art checksums.
590 // TODO: Check the oat file validity earlier.
591 *error_msg = StringPrintf("Attempted to use relocated version of %s at %s generated from %s "
592 "but image failed to load: %s",
593 image_location, cache_filename.c_str(), system_filename.c_str(),
594 error_msg->c_str());
Narayan Kamath28bc9872014-11-07 17:46:28 +0000595 PruneDalvikCache(image_isa);
Alex Lighta59dd802014-07-02 16:28:08 -0700596 return nullptr;
597 } else if (is_system) {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700598 // If the /system file exists, it should be up-to-date, don't try to generate it.
Alex Light64ad14d2014-08-19 14:23:13 -0700599 *error_msg = StringPrintf("Failed to load /system image '%s': %s",
600 image_filename->c_str(), error_msg->c_str());
Narayan Kamath52f84882014-05-02 10:10:39 +0100601 return nullptr;
Mathieu Chartierc7cb1902014-03-05 14:41:03 -0800602 } else {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700603 // Otherwise, log a warning and fall through to GenerateImage.
Alex Light64ad14d2014-08-19 14:23:13 -0700604 LOG(WARNING) << *error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700605 }
606 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100607
Alex Light64ad14d2014-08-19 14:23:13 -0700608 if (!can_compile) {
609 *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat";
610 return nullptr;
611 } else if (!dalvik_cache_exists) {
612 *error_msg = StringPrintf("No place to put generated image.");
613 return nullptr;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700614 } else if (!ImageCreationAllowed(is_global_cache, error_msg)) {
615 return nullptr;
Alex Light25396132014-08-27 15:37:23 -0700616 } else if (!GenerateImage(cache_filename, image_isa, error_msg)) {
Alex Light64ad14d2014-08-19 14:23:13 -0700617 *error_msg = StringPrintf("Failed to generate image '%s': %s",
618 cache_filename.c_str(), error_msg->c_str());
Brian Carlstrome9105f72014-10-28 15:53:43 -0700619 // We failed to create files, remove any possibly garbage output.
620 // Since ImageCreationAllowed was true above, we are the zygote
621 // and therefore the only process expected to generate these for
622 // the device.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000623 PruneDalvikCache(image_isa);
Alex Light64ad14d2014-08-19 14:23:13 -0700624 return nullptr;
625 } else {
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700626 // Check whether there is enough space left over after we have generated the image.
627 if (!CheckSpace(cache_filename, error_msg)) {
628 // No. Delete the generated image and try to run out of the dex files.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000629 PruneDalvikCache(image_isa);
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700630 return nullptr;
631 }
632
Alex Lighta59dd802014-07-02 16:28:08 -0700633 // Note that we must not use the file descriptor associated with
634 // ScopedFlock::GetFile to Init the image file. We want the file
635 // descriptor (and the associated exclusive lock) to be released when
636 // we leave Create.
637 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700638 image_lock.Init(cache_filename.c_str(), error_msg);
639 space = ImageSpace::Init(cache_filename.c_str(), image_location, true, error_msg);
640 if (space == nullptr) {
641 *error_msg = StringPrintf("Failed to load generated image '%s': %s",
642 cache_filename.c_str(), error_msg->c_str());
643 }
644 return space;
Alex Lighta59dd802014-07-02 16:28:08 -0700645 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700646}
647
Mathieu Chartier31e89252013-08-28 11:29:12 -0700648void ImageSpace::VerifyImageAllocations() {
Ian Rogers13735952014-10-08 12:43:28 -0700649 uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700650 while (current < End()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700651 CHECK_ALIGNED(current, kObjectAlignment);
652 auto* obj = reinterpret_cast<mirror::Object*>(current);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700653 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Mathieu Chartierc7853442015-03-27 14:35:38 -0700654 CHECK(live_bitmap_->Test(obj)) << PrettyTypeOf(obj);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700655 if (kUseBakerOrBrooksReadBarrier) {
656 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800657 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700658 current += RoundUp(obj->SizeOf(), kObjectAlignment);
659 }
660}
661
Narayan Kamath52f84882014-05-02 10:10:39 +0100662ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location,
663 bool validate_oat_file, std::string* error_msg) {
664 CHECK(image_filename != nullptr);
665 CHECK(image_location != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700666
667 uint64_t start_time = 0;
668 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
669 start_time = NanoTime();
Narayan Kamath52f84882014-05-02 10:10:39 +0100670 LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename;
Ian Rogers1d54e732013-05-02 21:10:01 -0700671 }
672
Ian Rogers700a4022014-05-19 16:49:03 -0700673 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700674 if (file.get() == nullptr) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100675 *error_msg = StringPrintf("Failed to open '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700676 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700677 }
678 ImageHeader image_header;
679 bool success = file->ReadFully(&image_header, sizeof(image_header));
680 if (!success || !image_header.IsValid()) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100681 *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700682 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700683 }
Andreas Gampe6c8b49f2015-02-19 11:42:36 -0800684 // Check that the file is large enough.
685 uint64_t image_file_size = static_cast<uint64_t>(file->GetLength());
686 if (image_header.GetImageSize() > image_file_size) {
687 *error_msg = StringPrintf("Image file too small for image heap: %" PRIu64 " vs. %zu.",
688 image_file_size, image_header.GetImageSize());
689 return nullptr;
690 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691
692 if (kIsDebugBuild) {
693 LOG(INFO) << "Dumping image sections";
694 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
695 const auto section_idx = static_cast<ImageHeader::ImageSections>(i);
696 auto& section = image_header.GetImageSection(section_idx);
697 LOG(INFO) << section_idx << " start="
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700698 << reinterpret_cast<void*>(image_header.GetImageBegin() + section.Offset()) << " "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700699 << section;
700 }
701 }
702
703 const auto& bitmap_section = image_header.GetImageSection(ImageHeader::kSectionImageBitmap);
704 auto end_of_bitmap = static_cast<size_t>(bitmap_section.End());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700705 if (end_of_bitmap != image_file_size) {
706 *error_msg = StringPrintf(
707 "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.", image_file_size,
708 end_of_bitmap);
Andreas Gampe6c8b49f2015-02-19 11:42:36 -0800709 return nullptr;
710 }
711
Mathieu Chartier31e89252013-08-28 11:29:12 -0700712 // Note: The image header is part of the image due to mmap page alignment required of offset.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700713 std::unique_ptr<MemMap> map(MemMap::MapFileAtAddress(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700714 image_header.GetImageBegin(), image_header.GetImageSize(),
Mathieu Chartierc7853442015-03-27 14:35:38 -0700715 PROT_READ | PROT_WRITE, MAP_PRIVATE, file->Fd(), 0, false, image_filename, error_msg));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700716 if (map.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700717 DCHECK(!error_msg->empty());
718 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700719 }
720 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
721 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
722
Mathieu Chartiere401d142015-04-22 13:56:20 -0700723 std::unique_ptr<MemMap> image_map(MemMap::MapFileAtAddress(
724 nullptr, bitmap_section.Size(), PROT_READ, MAP_PRIVATE, file->Fd(),
725 bitmap_section.Offset(), false, image_filename, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700726 if (image_map.get() == nullptr) {
727 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
728 return nullptr;
729 }
Ian Rogers3e5cf302014-05-20 16:40:37 -0700730 uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1);
Narayan Kamath52f84882014-05-02 10:10:39 +0100731 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700732 bitmap_index));
Ian Rogers700a4022014-05-19 16:49:03 -0700733 std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700734 accounting::ContinuousSpaceBitmap::CreateFromMemMap(
735 bitmap_name, image_map.release(), reinterpret_cast<uint8_t*>(map->Begin()),
736 accounting::ContinuousSpaceBitmap::ComputeHeapSize(bitmap_section.Size())));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700737 if (bitmap.get() == nullptr) {
738 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
739 return nullptr;
740 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700741
Mathieu Chartiere401d142015-04-22 13:56:20 -0700742 // We only want the mirror object, not the ArtFields and ArtMethods.
743 uint8_t* const image_end =
744 map->Begin() + image_header.GetImageSection(ImageHeader::kSectionObjects).End();
Ian Rogers700a4022014-05-19 16:49:03 -0700745 std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, image_location,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700746 map.release(), bitmap.release(), image_end));
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700747
748 // VerifyImageAllocations() will be called later in Runtime::Init()
749 // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
750 // and ArtField::java_lang_reflect_ArtField_, which are used from
751 // Object::SizeOf() which VerifyImageAllocations() calls, are not
752 // set yet at this point.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700753
Narayan Kamath52f84882014-05-02 10:10:39 +0100754 space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700755 if (space->oat_file_.get() == nullptr) {
756 DCHECK(!error_msg->empty());
757 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700758 }
Andreas Gampe88da3b02015-06-12 20:38:49 -0700759 space->oat_file_non_owned_ = space->oat_file_.get();
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700760
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700761 if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
762 DCHECK(!error_msg->empty());
763 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700764 }
765
Vladimir Marko7624d252014-05-02 14:40:15 +0100766 Runtime* runtime = Runtime::Current();
767 runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet());
768
Mathieu Chartiere401d142015-04-22 13:56:20 -0700769 runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod));
770 runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod));
771 runtime->SetImtUnimplementedMethod(
772 image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod));
773 runtime->SetCalleeSaveMethod(
774 image_header.GetImageMethod(ImageHeader::kCalleeSaveMethod), Runtime::kSaveAll);
775 runtime->SetCalleeSaveMethod(
776 image_header.GetImageMethod(ImageHeader::kRefsOnlySaveMethod), Runtime::kRefsOnly);
777 runtime->SetCalleeSaveMethod(
778 image_header.GetImageMethod(ImageHeader::kRefsAndArgsSaveMethod), Runtime::kRefsAndArgs);
Vladimir Marko7624d252014-05-02 14:40:15 +0100779
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700780 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
781 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
782 << ") " << *space.get();
783 }
784 return space.release();
785}
786
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000787OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700788 const ImageHeader& image_header = GetImageHeader();
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000789 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
790
Igor Murashkin46774762014-10-22 11:37:02 -0700791 CHECK(image_header.GetOatDataBegin() != nullptr);
792
Igor Murashkina315f5c2015-07-31 17:35:52 -0700793 OatFile* oat_file = OatFile::Open(oat_filename,
794 oat_filename,
795 image_header.GetOatDataBegin(),
Igor Murashkin46774762014-10-22 11:37:02 -0700796 image_header.GetOatFileBegin(),
Richard Uhlere5fed032015-03-18 08:21:11 -0700797 !Runtime::Current()->IsAotCompiler(),
Igor Murashkina315f5c2015-07-31 17:35:52 -0700798 nullptr /* no abs dex location */,
799 outof_ptr(error_msg));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700800 if (oat_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700801 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
802 oat_filename.c_str(), GetName(), error_msg->c_str());
803 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700804 }
805 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
806 uint32_t image_oat_checksum = image_header.GetOatChecksum();
807 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700808 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
809 " in image %s", oat_checksum, image_oat_checksum, GetName());
810 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700811 }
Alex Lighta59dd802014-07-02 16:28:08 -0700812 int32_t image_patch_delta = image_header.GetPatchDelta();
813 int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta();
Igor Murashkin46774762014-10-22 11:37:02 -0700814 if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700815 // We should have already relocated by this point. Bail out.
816 *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d "
817 "in image %s", oat_patch_delta, image_patch_delta, GetName());
818 return nullptr;
819 }
820
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700821 return oat_file;
822}
823
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700824bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700825 CHECK(oat_file_.get() != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700826 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700827 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
828 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700829 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
830 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
831 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700832 return false;
833 }
834 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700835 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
836 "dex file '%s' (0x%x != 0x%x)",
837 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
838 oat_dex_file->GetDexFileLocationChecksum(),
839 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700840 return false;
841 }
842 }
843 return true;
844}
845
Andreas Gampe88da3b02015-06-12 20:38:49 -0700846
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700847const OatFile* ImageSpace::GetOatFile() const {
Andreas Gampe88da3b02015-06-12 20:38:49 -0700848 return oat_file_non_owned_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700849}
850
Andreas Gampe88da3b02015-06-12 20:38:49 -0700851
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700852OatFile* ImageSpace::ReleaseOatFile() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700853 CHECK(oat_file_.get() != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700854 return oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700855}
856
Ian Rogers1d54e732013-05-02 21:10:01 -0700857void ImageSpace::Dump(std::ostream& os) const {
858 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700859 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -0700860 << ",end=" << reinterpret_cast<void*>(End())
861 << ",size=" << PrettySize(Size())
862 << ",name=\"" << GetName() << "\"]";
863}
864
865} // namespace space
866} // namespace gc
867} // namespace art