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 */
16
17 #include <string.h>
18 #include <vector>
19
20 #include "image_test.h"
21
22 #include "image.h"
23 #include "scoped_thread_state_change-inl.h"
24 #include "thread.h"
25
26 namespace art {
27
TEST_F(ImageTest,TestImageLayout)28 TEST_F(ImageTest, TestImageLayout) {
29 std::vector<size_t> image_sizes;
30 std::vector<size_t> image_sizes_extra;
31 // Compile multi-image with ImageLayoutA being the last image.
32 {
33 CompilationHelper helper;
34 Compile(ImageHeader::kStorageModeUncompressed, helper, "ImageLayoutA", {"LMyClass;"});
35 image_sizes = helper.GetImageObjectSectionSizes();
36 }
37 TearDown();
38 runtime_.reset();
39 SetUp();
40 // Compile multi-image with ImageLayoutB being the last image.
41 {
42 CompilationHelper helper;
43 Compile(ImageHeader::kStorageModeUncompressed, helper, "ImageLayoutB", {"LMyClass;"});
44 image_sizes_extra = helper.GetImageObjectSectionSizes();
45 }
46 // Make sure that the new stuff in the clinit in ImageLayoutB is in the last image and not in the
47 // first two images.
48 ASSERT_EQ(image_sizes.size(), image_sizes.size());
49 // Sizes of the images should be the same. These sizes are for the whole image unrounded.
50 for (size_t i = 0; i < image_sizes.size() - 1; ++i) {
51 EXPECT_EQ(image_sizes[i], image_sizes_extra[i]);
52 }
53 // Last image should be larger since it has a hash map and a string.
54 EXPECT_LT(image_sizes.back(), image_sizes_extra.back());
55 }
56
TEST_F(ImageTest,ImageHeaderIsValid)57 TEST_F(ImageTest, ImageHeaderIsValid) {
58 uint32_t image_begin = ART_BASE_ADDRESS;
59 uint32_t image_size_ = 16 * KB;
60 uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB);
61 uint32_t oat_checksum = 0;
62 uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned
63 uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned
64 uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB);
65 uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB);
66 ImageSection sections[ImageHeader::kSectionCount];
67 ImageHeader image_header(image_begin,
68 image_size_,
69 sections,
70 image_roots,
71 oat_checksum,
72 oat_file_begin,
73 oat_data_begin,
74 oat_data_end,
75 oat_file_end,
76 /*boot_image_begin*/0U,
77 /*boot_image_size*/0U,
78 /*boot_oat_begin*/0U,
79 /*boot_oat_size_*/0U,
80 sizeof(void*),
81 /*compile_pic*/false,
82 /*is_pic*/false,
83 ImageHeader::kDefaultStorageMode,
84 /*data_size*/0u);
85 ASSERT_TRUE(image_header.IsValid());
86 ASSERT_TRUE(!image_header.IsAppImage());
87
88 char* magic = const_cast<char*>(image_header.GetMagic());
89 strcpy(magic, ""); // bad magic
90 ASSERT_FALSE(image_header.IsValid());
91 strcpy(magic, "art\n000"); // bad version
92 ASSERT_FALSE(image_header.IsValid());
93 }
94
95 // Test that pointer to quick code is the same in
96 // a default method of an interface and in a copied method
97 // of a class which implements the interface. This should be true
98 // only if the copied method and the origin method are located in the
99 // same oat file.
TEST_F(ImageTest,TestDefaultMethods)100 TEST_F(ImageTest, TestDefaultMethods) {
101 CompilationHelper helper;
102 Compile(ImageHeader::kStorageModeUncompressed,
103 helper,
104 "DefaultMethods",
105 {"LIface;", "LImpl;", "LIterableBase;"});
106
107 PointerSize pointer_size = class_linker_->GetImagePointerSize();
108 Thread* self = Thread::Current();
109 ScopedObjectAccess soa(self);
110
111 // Test the pointer to quick code is the same in origin method
112 // and in the copied method form the same oat file.
113 mirror::Class* iface_klass = class_linker_->LookupClass(
114 self, "LIface;", ObjPtr<mirror::ClassLoader>());
115 ASSERT_NE(nullptr, iface_klass);
116 ArtMethod* origin = iface_klass->FindInterfaceMethod("defaultMethod", "()V", pointer_size);
117 ASSERT_NE(nullptr, origin);
118 ASSERT_TRUE(origin->GetDeclaringClass() == iface_klass);
119 const void* code = origin->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
120 // The origin method should have a pointer to quick code
121 ASSERT_NE(nullptr, code);
122 ASSERT_FALSE(class_linker_->IsQuickToInterpreterBridge(code));
123 mirror::Class* impl_klass = class_linker_->LookupClass(
124 self, "LImpl;", ObjPtr<mirror::ClassLoader>());
125 ASSERT_NE(nullptr, impl_klass);
126 ArtMethod* copied = FindCopiedMethod(origin, impl_klass);
127 ASSERT_NE(nullptr, copied);
128 // the copied method should have pointer to the same quick code as the origin method
129 ASSERT_EQ(code, copied->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size));
130
131 // Test the origin method has pointer to quick code
132 // but the copied method has pointer to interpreter
133 // because these methods are in different oat files.
134 mirror::Class* iterable_klass = class_linker_->LookupClass(
135 self, "Ljava/lang/Iterable;", ObjPtr<mirror::ClassLoader>());
136 ASSERT_NE(nullptr, iterable_klass);
137 origin = iterable_klass->FindClassMethod(
138 "forEach", "(Ljava/util/function/Consumer;)V", pointer_size);
139 ASSERT_NE(nullptr, origin);
140 ASSERT_FALSE(origin->IsDirect());
141 ASSERT_TRUE(origin->GetDeclaringClass() == iterable_klass);
142 code = origin->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
143 // the origin method should have a pointer to quick code
144 ASSERT_NE(nullptr, code);
145 ASSERT_FALSE(class_linker_->IsQuickToInterpreterBridge(code));
146 mirror::Class* iterablebase_klass = class_linker_->LookupClass(
147 self, "LIterableBase;", ObjPtr<mirror::ClassLoader>());
148 ASSERT_NE(nullptr, iterablebase_klass);
149 copied = FindCopiedMethod(origin, iterablebase_klass);
150 ASSERT_NE(nullptr, copied);
151 code = copied->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
152 // the copied method should have a pointer to interpreter
153 ASSERT_TRUE(class_linker_->IsQuickToInterpreterBridge(code));
154 }
155
156 } // namespace art
157