Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 20 | #include "common_test.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 21 | #include "file.h" |
| 22 | #include "image.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 23 | #include "image_writer.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 24 | #include "oat_writer.h" |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 25 | #include "signal_catcher.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 26 | #include "space.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 27 | #include "utils.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 28 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 31 | class ImageTest : public CommonTest {}; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 32 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 33 | TEST_F(ImageTest, WriteRead) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 34 | ScratchFile tmp_oat; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 35 | std::vector<const DexFile*> dex_files; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 36 | dex_files.push_back(java_lang_dex_file_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 37 | bool success_oat = OatWriter::Create(tmp_oat.GetFile(), NULL, dex_files, *compiler_.get()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 38 | ASSERT_TRUE(success_oat); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 39 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 40 | // Force all system classes into memory |
| 41 | for (size_t i = 0; i < java_lang_dex_file_->NumClassDefs(); i++) { |
| 42 | const DexFile::ClassDef& class_def = java_lang_dex_file_->GetClassDef(i); |
| 43 | const char* descriptor = java_lang_dex_file_->GetClassDescriptor(class_def); |
| 44 | Class* klass = class_linker_->FindSystemClass(descriptor); |
| 45 | EXPECT_TRUE(klass != NULL) << descriptor; |
| 46 | } |
| 47 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 48 | ImageWriter writer(NULL); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 49 | ScratchFile tmp_image; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 50 | const uintptr_t requested_image_base = 0x60000000; |
| 51 | bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base, |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 52 | tmp_oat.GetFilename(), tmp_oat.GetFilename()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 53 | ASSERT_TRUE(success_image); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 54 | |
| 55 | { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 56 | UniquePtr<File> file(OS::OpenFile(tmp_image.GetFilename().c_str(), false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 57 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 58 | ImageHeader image_header; |
| 59 | file->ReadFully(&image_header, sizeof(image_header)); |
| 60 | ASSERT_TRUE(image_header.IsValid()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 61 | |
| 62 | ASSERT_EQ(1U, Heap::GetSpaces().size()); |
| 63 | Space* space = Heap::GetSpaces()[0]; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 64 | ASSERT_FALSE(space->IsImageSpace()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(space != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 66 | ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length())); |
| 67 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 68 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 69 | // tear down old runtime before making a new one, clearing out misc state |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 70 | delete runtime_.release(); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 71 | java_lang_dex_file_ = NULL; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 72 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 73 | UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName(), GetLibCoreDexFileName())); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 74 | ASSERT_TRUE(dex.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 75 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 76 | Runtime::Options options; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 77 | std::string image("-Ximage:"); |
| 78 | image.append(tmp_image.GetFilename()); |
| 79 | options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 80 | |
| 81 | runtime_.reset(Runtime::Create(options, false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 82 | ASSERT_TRUE(runtime_.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 83 | class_linker_ = runtime_->GetClassLinker(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 84 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 85 | ASSERT_TRUE(runtime_->GetJniDlsymLookupStub() != NULL); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 86 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 87 | ASSERT_EQ(2U, Heap::GetSpaces().size()); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 88 | ASSERT_TRUE(Heap::GetSpaces()[0]->IsImageSpace()); |
| 89 | ASSERT_FALSE(Heap::GetSpaces()[1]->IsImageSpace()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 90 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 91 | ImageSpace* image_space = Heap::GetSpaces()[0]->AsImageSpace(); |
| 92 | byte* image_begin = image_space->Begin(); |
| 93 | byte* image_end = image_space->End(); |
| 94 | CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin)); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 95 | for (size_t i = 0; i < dex->NumClassDefs(); i++) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 96 | const DexFile::ClassDef& class_def = dex->GetClassDef(i); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 97 | const char* descriptor = dex->GetClassDescriptor(class_def); |
| 98 | Class* klass = class_linker_->FindSystemClass(descriptor); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 99 | EXPECT_TRUE(klass != NULL) << descriptor; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 100 | EXPECT_LT(image_begin, reinterpret_cast<byte*>(klass)) << descriptor; |
| 101 | EXPECT_LT(reinterpret_cast<byte*>(klass), image_end) << descriptor; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 102 | EXPECT_EQ(*klass->GetRawLockWordAddress(), 0); // address should have been removed from monitor |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 103 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace art |