blob: 1d10af2e634c348ae2fc9fcb5cbaf61f22e75f4e [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 Lighta59dd802014-07-02 16:28:08 -070019#include <random>
20
Brian Carlstrom56d947f2013-07-15 13:14:23 -070021#include "base/stl_util.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "base/unix_file/fd_file.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010023#include "base/scoped_flock.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "mirror/art_method.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "mirror/class-inl.h"
27#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070028#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "os.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "space-inl.h"
31#include "utils.h"
32
33namespace art {
34namespace gc {
35namespace space {
36
Ian Rogersef7d42f2014-01-06 12:55:46 -080037Atomic<uint32_t> ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070038
Narayan Kamath52f84882014-05-02 10:10:39 +010039ImageSpace::ImageSpace(const std::string& image_filename, const char* image_location,
40 MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap)
41 : MemMapSpace(image_filename, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(),
42 kGcRetentionPolicyNeverCollect),
43 image_location_(image_location) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070044 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070045 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070046}
47
Alex Lightcf4bf382014-07-24 11:29:14 -070048static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
49 CHECK_ALIGNED(min_delta, kPageSize);
50 CHECK_ALIGNED(max_delta, kPageSize);
51 CHECK_LT(min_delta, max_delta);
52
53 std::default_random_engine generator;
54 generator.seed(NanoTime() * getpid());
55 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
56 int32_t r = distribution(generator);
57 if (r % 2 == 0) {
58 r = RoundUp(r, kPageSize);
59 } else {
60 r = RoundDown(r, kPageSize);
61 }
62 CHECK_LE(min_delta, r);
63 CHECK_GE(max_delta, r);
64 CHECK_ALIGNED(r, kPageSize);
65 return r;
66}
67
Narayan Kamath52f84882014-05-02 10:10:39 +010068static bool GenerateImage(const std::string& image_filename, std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070069 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
70 std::vector<std::string> boot_class_path;
71 Split(boot_class_path_string, ':', boot_class_path);
72 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070073 *error_msg = "Failed to generate image because no boot class path specified";
74 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070075 }
76
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070077 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070078
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -070079 std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
Mathieu Chartier08d7d442013-07-31 18:08:51 -070080 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070081
82 std::string image_option_string("--image=");
Narayan Kamath52f84882014-05-02 10:10:39 +010083 image_option_string += image_filename;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070084 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070085
Brian Carlstrom56d947f2013-07-15 13:14:23 -070086 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070087 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070088 }
89
90 std::string oat_file_option_string("--oat-file=");
Narayan Kamath52f84882014-05-02 10:10:39 +010091 oat_file_option_string += image_filename;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070092 oat_file_option_string.erase(oat_file_option_string.size() - 3);
93 oat_file_option_string += "oat";
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070094 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070095
Ian Rogers8afeb852014-04-02 14:55:49 -070096 Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
97
Alex Lightcf4bf382014-07-24 11:29:14 -070098 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
99 ART_BASE_ADDRESS_MAX_DELTA);
100 LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default "
101 << "art base address of 0x" << std::hex << ART_BASE_ADDRESS;
102 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700103
Brian Carlstrom57309db2014-07-30 15:13:25 -0700104 if (!kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700105 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700106 }
107
Brian Carlstrom6449c622014-02-10 23:48:36 -0800108 const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800109 for (size_t i = 0; i < compiler_options.size(); ++i) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800110 arg_vector.push_back(compiler_options[i].c_str());
111 }
112
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700113 std::string command_line(Join(arg_vector, ' '));
114 LOG(INFO) << "GenerateImage: " << command_line;
Brian Carlstrom6449c622014-02-10 23:48:36 -0800115 return Exec(arg_vector, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700116}
117
Narayan Kamath52f84882014-05-02 10:10:39 +0100118bool ImageSpace::FindImageFilename(const char* image_location,
119 const InstructionSet image_isa,
Alex Lighta59dd802014-07-02 16:28:08 -0700120 std::string* system_filename,
121 bool* has_system,
122 std::string* cache_filename,
123 bool* dalvik_cache_exists,
124 bool* has_cache) {
125 *has_system = false;
126 *has_cache = false;
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700127 // image_location = /system/framework/boot.art
128 // system_image_location = /system/framework/<image_isa>/boot.art
129 std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
130 if (OS::FileExists(system_image_filename.c_str())) {
Alex Lighta59dd802014-07-02 16:28:08 -0700131 *system_filename = system_image_filename;
132 *has_system = true;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700133 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100134
Alex Lighta59dd802014-07-02 16:28:08 -0700135 bool have_android_data = false;
136 *dalvik_cache_exists = false;
137 std::string dalvik_cache;
138 GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache,
139 &have_android_data, dalvik_cache_exists);
Narayan Kamath52f84882014-05-02 10:10:39 +0100140
Alex Lighta59dd802014-07-02 16:28:08 -0700141 if (have_android_data && *dalvik_cache_exists) {
142 // Always set output location even if it does not exist,
143 // so that the caller knows where to create the image.
144 //
145 // image_location = /system/framework/boot.art
146 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
147 std::string error_msg;
148 if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) {
149 LOG(WARNING) << error_msg;
150 return *has_system;
151 }
152 *has_cache = OS::FileExists(cache_filename->c_str());
153 }
154 return *has_system || *has_cache;
155}
156
157static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) {
158 std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
159 if (image_file.get() == nullptr) {
160 return false;
161 }
162 const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader));
163 if (!success || !image_header->IsValid()) {
164 return false;
165 }
166 return true;
167}
168
Alex Light6e183f22014-07-18 14:57:04 -0700169// Relocate the image at image_location to dest_filename and relocate it by a random amount.
170static bool RelocateImage(const char* image_location, const char* dest_filename,
Alex Lighta59dd802014-07-02 16:28:08 -0700171 InstructionSet isa, std::string* error_msg) {
172 std::string patchoat(Runtime::Current()->GetPatchoatExecutable());
173
174 std::string input_image_location_arg("--input-image-location=");
175 input_image_location_arg += image_location;
176
177 std::string output_image_filename_arg("--output-image-file=");
178 output_image_filename_arg += dest_filename;
179
180 std::string input_oat_location_arg("--input-oat-location=");
181 input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location);
182
183 std::string output_oat_filename_arg("--output-oat-file=");
184 output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename);
185
186 std::string instruction_set_arg("--instruction-set=");
187 instruction_set_arg += GetInstructionSetString(isa);
188
189 std::string base_offset_arg("--base-offset-delta=");
190 StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
191 ART_BASE_ADDRESS_MAX_DELTA));
192
193 std::vector<std::string> argv;
194 argv.push_back(patchoat);
195
196 argv.push_back(input_image_location_arg);
197 argv.push_back(output_image_filename_arg);
198
199 argv.push_back(input_oat_location_arg);
200 argv.push_back(output_oat_filename_arg);
201
202 argv.push_back(instruction_set_arg);
203 argv.push_back(base_offset_arg);
204
205 std::string command_line(Join(argv, ' '));
206 LOG(INFO) << "RelocateImage: " << command_line;
207 return Exec(argv, error_msg);
208}
209
210static ImageHeader* ReadSpecificImageHeaderOrDie(const char* filename) {
211 std::unique_ptr<ImageHeader> hdr(new ImageHeader);
212 if (!ReadSpecificImageHeader(filename, hdr.get())) {
213 LOG(FATAL) << "Unable to read image header for " << filename;
214 return nullptr;
215 }
216 return hdr.release();
Narayan Kamath52f84882014-05-02 10:10:39 +0100217}
218
219ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location,
220 const InstructionSet image_isa) {
Alex Lighta59dd802014-07-02 16:28:08 -0700221 std::string system_filename;
222 bool has_system = false;
223 std::string cache_filename;
224 bool has_cache = false;
225 bool dalvik_cache_exists = false;
226 if (FindImageFilename(image_location, image_isa, &system_filename, &has_system,
227 &cache_filename, &dalvik_cache_exists, &has_cache)) {
228 if (Runtime::Current()->ShouldRelocate()) {
229 if (has_system && has_cache) {
230 std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader);
231 std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader);
232 if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) {
233 LOG(FATAL) << "Unable to read image header for " << image_location << " at "
234 << system_filename;
235 return nullptr;
236 }
237 if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) {
238 LOG(FATAL) << "Unable to read image header for " << image_location << " at "
239 << cache_filename;
240 return nullptr;
241 }
242 if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) {
243 LOG(FATAL) << "Unable to find a relocated version of image file " << image_location;
244 return nullptr;
245 }
246 return cache_hdr.release();
247 } else if (!has_cache) {
248 LOG(FATAL) << "Unable to find a relocated version of image file " << image_location;
249 return nullptr;
250 } else if (!has_system && has_cache) {
251 // This can probably just use the cache one.
252 return ReadSpecificImageHeaderOrDie(cache_filename.c_str());
253 }
254 } else {
255 // We don't want to relocate, Just pick the appropriate one if we have it and return.
256 if (has_system && has_cache) {
257 // We want the cache if the checksum matches, otherwise the system.
258 std::unique_ptr<ImageHeader> system(ReadSpecificImageHeaderOrDie(system_filename.c_str()));
259 std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeaderOrDie(cache_filename.c_str()));
260 if (system.get() == nullptr ||
261 (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) {
262 return cache.release();
263 } else {
264 return system.release();
265 }
266 } else if (has_system) {
267 return ReadSpecificImageHeaderOrDie(system_filename.c_str());
268 } else if (has_cache) {
269 return ReadSpecificImageHeaderOrDie(cache_filename.c_str());
270 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100271 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100272 }
273
274 LOG(FATAL) << "Unable to find image file for: " << image_location;
275 return nullptr;
276}
277
Alex Lighta59dd802014-07-02 16:28:08 -0700278static bool ChecksumsMatch(const char* image_a, const char* image_b) {
279 ImageHeader hdr_a;
280 ImageHeader hdr_b;
281 return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b)
282 && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum();
283}
284
Narayan Kamath52f84882014-05-02 10:10:39 +0100285ImageSpace* ImageSpace::Create(const char* image_location,
286 const InstructionSet image_isa) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700287 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -0700288 std::string system_filename;
289 bool has_system = false;
290 std::string cache_filename;
291 bool has_cache = false;
292 bool dalvik_cache_exists = false;
293 const bool found_image = FindImageFilename(image_location, image_isa, &system_filename,
294 &has_system, &cache_filename, &dalvik_cache_exists,
295 &has_cache);
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100296
Alex Lighta59dd802014-07-02 16:28:08 -0700297 ImageSpace* space;
298 bool relocate = Runtime::Current()->ShouldRelocate();
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100299 if (found_image) {
Alex Lighta59dd802014-07-02 16:28:08 -0700300 const std::string* image_filename;
301 bool is_system = false;
302 bool relocated_version_used = false;
303 if (relocate) {
304 CHECK(dalvik_cache_exists) << "Requiring relocation for image " << image_location << " "
305 << "at " << system_filename << " but we do not have any "
306 << "dalvik_cache to find/place it in.";
307 if (has_system) {
308 if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
309 // We already have a relocated version
310 image_filename = &cache_filename;
311 relocated_version_used = true;
312 } else {
313 // We cannot have a relocated version, Relocate the system one and use it.
314 if (RelocateImage(image_location, cache_filename.c_str(), image_isa,
315 &error_msg)) {
316 relocated_version_used = true;
317 image_filename = &cache_filename;
318 } else {
319 LOG(FATAL) << "Unable to relocate image " << image_location << " "
320 << "from " << system_filename << " to " << cache_filename << ": "
321 << error_msg;
322 return nullptr;
323 }
324 }
325 } else {
326 CHECK(has_cache);
327 // We can just use cache's since it should be fine. This might or might not be relocated.
328 image_filename = &cache_filename;
329 }
330 } else {
331 if (has_system && has_cache) {
332 // Check they have the same cksum. If they do use the cache. Otherwise system.
333 if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
334 image_filename = &cache_filename;
335 relocated_version_used = true;
336 } else {
337 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700338 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700339 }
340 } else if (has_system) {
341 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700342 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700343 } else {
344 CHECK(has_cache);
345 image_filename = &cache_filename;
346 }
347 }
348 {
349 // Note that we must not use the file descriptor associated with
350 // ScopedFlock::GetFile to Init the image file. We want the file
351 // descriptor (and the associated exclusive lock) to be released when
352 // we leave Create.
353 ScopedFlock image_lock;
354 image_lock.Init(image_filename->c_str(), &error_msg);
355 LOG(INFO) << "Using image file " << image_filename->c_str() << " for image location "
356 << image_location;
Alex Lightb93637a2014-07-31 10:48:46 -0700357 // If we are in /system we can assume the image is good. We can also
358 // assume this if we are using a relocated image (i.e. image checksum
359 // matches) since this is only different by the offset. We need this to
360 // make sure that host tests continue to work.
Alex Lighta59dd802014-07-02 16:28:08 -0700361 space = ImageSpace::Init(image_filename->c_str(), image_location,
Alex Lightb93637a2014-07-31 10:48:46 -0700362 !(is_system || relocated_version_used), &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700363 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100364 if (space != nullptr) {
365 return space;
366 }
367
Alex Lighta59dd802014-07-02 16:28:08 -0700368 // If the /system file exists, it should be up-to-date, don't try to generate it. Same if it is
369 // a relocated copy from something in /system (i.e. checksum's match).
370 // Otherwise, log a warning and fall through to GenerateImage.
371 if (relocated_version_used) {
372 LOG(FATAL) << "Attempted to use relocated version of " << image_location << " "
373 << "at " << cache_filename << " generated from " << system_filename << " "
374 << "but image failed to load: " << error_msg;
375 return nullptr;
376 } else if (is_system) {
377 LOG(FATAL) << "Failed to load /system image '" << *image_filename << "': " << error_msg;
Narayan Kamath52f84882014-05-02 10:10:39 +0100378 return nullptr;
Mathieu Chartierc7cb1902014-03-05 14:41:03 -0800379 } else {
380 LOG(WARNING) << error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700381 }
382 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100383
Alex Lighta59dd802014-07-02 16:28:08 -0700384 CHECK(dalvik_cache_exists) << "No place to put generated image.";
385 CHECK(GenerateImage(cache_filename, &error_msg))
386 << "Failed to generate image '" << cache_filename << "': " << error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -0700387 {
388 // Note that we must not use the file descriptor associated with
389 // ScopedFlock::GetFile to Init the image file. We want the file
390 // descriptor (and the associated exclusive lock) to be released when
391 // we leave Create.
392 ScopedFlock image_lock;
393 image_lock.Init(cache_filename.c_str(), &error_msg);
394 space = ImageSpace::Init(cache_filename.c_str(), image_location, true, &error_msg);
395 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700396 if (space == nullptr) {
Alex Lighta59dd802014-07-02 16:28:08 -0700397 LOG(FATAL) << "Failed to load generated image '" << cache_filename << "': " << error_msg;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700398 }
399 return space;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700400}
401
Mathieu Chartier31e89252013-08-28 11:29:12 -0700402void ImageSpace::VerifyImageAllocations() {
403 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
404 while (current < End()) {
405 DCHECK_ALIGNED(current, kObjectAlignment);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800406 mirror::Object* obj = reinterpret_cast<mirror::Object*>(current);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700407 CHECK(live_bitmap_->Test(obj));
408 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700409 if (kUseBakerOrBrooksReadBarrier) {
410 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800411 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700412 current += RoundUp(obj->SizeOf(), kObjectAlignment);
413 }
414}
415
Narayan Kamath52f84882014-05-02 10:10:39 +0100416ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location,
417 bool validate_oat_file, std::string* error_msg) {
418 CHECK(image_filename != nullptr);
419 CHECK(image_location != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700420
421 uint64_t start_time = 0;
422 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
423 start_time = NanoTime();
Narayan Kamath52f84882014-05-02 10:10:39 +0100424 LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename;
Ian Rogers1d54e732013-05-02 21:10:01 -0700425 }
426
Ian Rogers700a4022014-05-19 16:49:03 -0700427 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename));
Ian Rogers1d54e732013-05-02 21:10:01 -0700428 if (file.get() == NULL) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100429 *error_msg = StringPrintf("Failed to open '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700430 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700431 }
432 ImageHeader image_header;
433 bool success = file->ReadFully(&image_header, sizeof(image_header));
434 if (!success || !image_header.IsValid()) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100435 *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700436 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700437 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700438
439 // Note: The image header is part of the image due to mmap page alignment required of offset.
Ian Rogers700a4022014-05-19 16:49:03 -0700440 std::unique_ptr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700441 image_header.GetImageSize(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700442 PROT_READ | PROT_WRITE,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -0700443 MAP_PRIVATE,
Ian Rogers1d54e732013-05-02 21:10:01 -0700444 file->Fd(),
445 0,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700446 false,
Narayan Kamath52f84882014-05-02 10:10:39 +0100447 image_filename,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700448 error_msg));
Ian Rogers1d54e732013-05-02 21:10:01 -0700449 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700450 DCHECK(!error_msg->empty());
451 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700452 }
453 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
454 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
455
Ian Rogers700a4022014-05-19 16:49:03 -0700456 std::unique_ptr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700457 PROT_READ, MAP_PRIVATE,
458 file->Fd(), image_header.GetBitmapOffset(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700459 false,
Narayan Kamath52f84882014-05-02 10:10:39 +0100460 image_filename,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700461 error_msg));
462 if (image_map.get() == nullptr) {
463 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
464 return nullptr;
465 }
Ian Rogers3e5cf302014-05-20 16:40:37 -0700466 uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1);
Narayan Kamath52f84882014-05-02 10:10:39 +0100467 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700468 bitmap_index));
Ian Rogers700a4022014-05-19 16:49:03 -0700469 std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700470 accounting::ContinuousSpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
471 reinterpret_cast<byte*>(map->Begin()),
472 map->Size()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700473 if (bitmap.get() == nullptr) {
474 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
475 return nullptr;
476 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700477
Ian Rogers700a4022014-05-19 16:49:03 -0700478 std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, image_location,
Narayan Kamath52f84882014-05-02 10:10:39 +0100479 map.release(), bitmap.release()));
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700480
481 // VerifyImageAllocations() will be called later in Runtime::Init()
482 // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
483 // and ArtField::java_lang_reflect_ArtField_, which are used from
484 // Object::SizeOf() which VerifyImageAllocations() calls, are not
485 // set yet at this point.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700486
Narayan Kamath52f84882014-05-02 10:10:39 +0100487 space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700488 if (space->oat_file_.get() == nullptr) {
489 DCHECK(!error_msg->empty());
490 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700491 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700492
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700493 if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
494 DCHECK(!error_msg->empty());
495 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700496 }
497
Vladimir Marko7624d252014-05-02 14:40:15 +0100498 Runtime* runtime = Runtime::Current();
499 runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet());
500
501 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
502 runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
503 mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod);
504 runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method));
505 mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt);
506 runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt));
507
508 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
509 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kSaveAll);
510 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
511 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsOnly);
512 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
513 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsAndArgs);
514
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700515 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
516 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
517 << ") " << *space.get();
518 }
519 return space.release();
520}
521
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000522OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700523 const ImageHeader& image_header = GetImageHeader();
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000524 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
525
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700526 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700527 !Runtime::Current()->IsCompiler(), error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700528 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700529 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
530 oat_filename.c_str(), GetName(), error_msg->c_str());
531 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700532 }
533 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
534 uint32_t image_oat_checksum = image_header.GetOatChecksum();
535 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700536 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
537 " in image %s", oat_checksum, image_oat_checksum, GetName());
538 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700539 }
Alex Lighta59dd802014-07-02 16:28:08 -0700540 int32_t image_patch_delta = image_header.GetPatchDelta();
541 int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta();
542 if (oat_patch_delta != image_patch_delta) {
543 // We should have already relocated by this point. Bail out.
544 *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d "
545 "in image %s", oat_patch_delta, image_patch_delta, GetName());
546 return nullptr;
547 }
548
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700549 return oat_file;
550}
551
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700552bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700553 CHECK(oat_file_.get() != NULL);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700554 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700555 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
556 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700557 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
558 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
559 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700560 return false;
561 }
562 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700563 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
564 "dex file '%s' (0x%x != 0x%x)",
565 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
566 oat_dex_file->GetDexFileLocationChecksum(),
567 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700568 return false;
569 }
570 }
571 return true;
572}
573
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700574const OatFile* ImageSpace::GetOatFile() const {
575 return oat_file_.get();
576}
577
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700578OatFile* ImageSpace::ReleaseOatFile() {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700579 CHECK(oat_file_.get() != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700580 return oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700581}
582
Ian Rogers1d54e732013-05-02 21:10:01 -0700583void ImageSpace::Dump(std::ostream& os) const {
584 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700585 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -0700586 << ",end=" << reinterpret_cast<void*>(End())
587 << ",size=" << PrettySize(Size())
588 << ",name=\"" << GetName() << "\"]";
589}
590
591} // namespace space
592} // namespace gc
593} // namespace art