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 "image_space.h"
18
19 #include <sys/types.h>
20 #include <sys/wait.h>
21
22 #include "base/stl_util.h"
23 #include "base/unix_file/fd_file.h"
24 #include "gc/accounting/space_bitmap-inl.h"
25 #include "mirror/art_method.h"
26 #include "mirror/class-inl.h"
27 #include "mirror/object-inl.h"
28 #include "oat_file.h"
29 #include "os.h"
30 #include "runtime.h"
31 #include "space-inl.h"
32 #include "utils.h"
33
34 namespace art {
35 namespace gc {
36 namespace space {
37
38 AtomicInteger ImageSpace::bitmap_index_(0);
39
ImageSpace(const std::string & name,MemMap * mem_map,accounting::SpaceBitmap * live_bitmap)40 ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map,
41 accounting::SpaceBitmap* live_bitmap)
42 : MemMapSpace(name, mem_map, mem_map->Size(), kGcRetentionPolicyNeverCollect) {
43 DCHECK(live_bitmap != NULL);
44 live_bitmap_.reset(live_bitmap);
45 }
46
GenerateImage(const std::string & image_file_name)47 static bool GenerateImage(const std::string& image_file_name) {
48 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
49 std::vector<std::string> boot_class_path;
50 Split(boot_class_path_string, ':', boot_class_path);
51 if (boot_class_path.empty()) {
52 LOG(FATAL) << "Failed to generate image because no boot class path specified";
53 }
54
55 std::vector<std::string> arg_vector;
56
57 std::string dex2oat(GetAndroidRoot());
58 dex2oat += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat");
59 arg_vector.push_back(dex2oat);
60
61 std::string image_option_string("--image=");
62 image_option_string += image_file_name;
63 arg_vector.push_back(image_option_string);
64
65 arg_vector.push_back("--runtime-arg");
66 arg_vector.push_back("-Xms64m");
67
68 arg_vector.push_back("--runtime-arg");
69 arg_vector.push_back("-Xmx64m");
70
71 for (size_t i = 0; i < boot_class_path.size(); i++) {
72 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
73 }
74
75 std::string oat_file_option_string("--oat-file=");
76 oat_file_option_string += image_file_name;
77 oat_file_option_string.erase(oat_file_option_string.size() - 3);
78 oat_file_option_string += "oat";
79 arg_vector.push_back(oat_file_option_string);
80
81 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS));
82
83 if (kIsTargetBuild) {
84 arg_vector.push_back("--image-classes-zip=/system/framework/framework.jar");
85 arg_vector.push_back("--image-classes=preloaded-classes");
86 } else {
87 arg_vector.push_back("--host");
88 }
89
90 std::string command_line(Join(arg_vector, ' '));
91 LOG(INFO) << "GenerateImage: " << command_line;
92
93 // Convert the args to char pointers.
94 std::vector<char*> char_args;
95 for (std::vector<std::string>::iterator it = arg_vector.begin(); it != arg_vector.end();
96 ++it) {
97 char_args.push_back(const_cast<char*>(it->c_str()));
98 }
99 char_args.push_back(NULL);
100
101 // fork and exec dex2oat
102 pid_t pid = fork();
103 if (pid == 0) {
104 // no allocation allowed between fork and exec
105
106 // change process groups, so we don't get reaped by ProcessManager
107 setpgid(0, 0);
108
109 execv(dex2oat.c_str(), &char_args[0]);
110
111 PLOG(FATAL) << "execv(" << dex2oat << ") failed";
112 return false;
113 } else {
114 if (pid == -1) {
115 PLOG(ERROR) << "fork failed";
116 }
117
118 // wait for dex2oat to finish
119 int status;
120 pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
121 if (got_pid != pid) {
122 PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid;
123 return false;
124 }
125 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
126 LOG(ERROR) << dex2oat << " failed: " << command_line;
127 return false;
128 }
129 }
130 return true;
131 }
132
Create(const std::string & original_image_file_name)133 ImageSpace* ImageSpace::Create(const std::string& original_image_file_name) {
134 if (OS::FileExists(original_image_file_name.c_str())) {
135 // If the /system file exists, it should be up-to-date, don't try to generate
136 return space::ImageSpace::Init(original_image_file_name, false);
137 }
138 // If the /system file didn't exist, we need to use one from the dalvik-cache.
139 // If the cache file exists, try to open, but if it fails, regenerate.
140 // If it does not exist, generate.
141 std::string image_file_name(GetDalvikCacheFilenameOrDie(original_image_file_name));
142 if (OS::FileExists(image_file_name.c_str())) {
143 space::ImageSpace* image_space = space::ImageSpace::Init(image_file_name, true);
144 if (image_space != NULL) {
145 return image_space;
146 }
147 }
148 CHECK(GenerateImage(image_file_name)) << "Failed to generate image: " << image_file_name;
149 return space::ImageSpace::Init(image_file_name, true);
150 }
151
VerifyImageAllocations()152 void ImageSpace::VerifyImageAllocations() {
153 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
154 while (current < End()) {
155 DCHECK_ALIGNED(current, kObjectAlignment);
156 const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(current);
157 CHECK(live_bitmap_->Test(obj));
158 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
159 current += RoundUp(obj->SizeOf(), kObjectAlignment);
160 }
161 }
162
Init(const std::string & image_file_name,bool validate_oat_file)163 ImageSpace* ImageSpace::Init(const std::string& image_file_name, bool validate_oat_file) {
164 CHECK(!image_file_name.empty());
165
166 uint64_t start_time = 0;
167 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
168 start_time = NanoTime();
169 LOG(INFO) << "ImageSpace::Init entering image_file_name=" << image_file_name;
170 }
171
172 UniquePtr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
173 if (file.get() == NULL) {
174 LOG(ERROR) << "Failed to open " << image_file_name;
175 return NULL;
176 }
177 ImageHeader image_header;
178 bool success = file->ReadFully(&image_header, sizeof(image_header));
179 if (!success || !image_header.IsValid()) {
180 LOG(ERROR) << "Invalid image header " << image_file_name;
181 return NULL;
182 }
183
184 // Note: The image header is part of the image due to mmap page alignment required of offset.
185 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
186 image_header.GetImageSize(),
187 PROT_READ | PROT_WRITE,
188 MAP_PRIVATE | MAP_FIXED,
189 file->Fd(),
190 0,
191 false));
192 if (map.get() == NULL) {
193 LOG(ERROR) << "Failed to map " << image_file_name;
194 return NULL;
195 }
196 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
197 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
198
199 UniquePtr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
200 PROT_READ, MAP_PRIVATE,
201 file->Fd(), image_header.GetBitmapOffset(),
202 false));
203 CHECK(image_map.get() != nullptr) << "failed to map image bitmap";
204 size_t bitmap_index = bitmap_index_.fetch_add(1);
205 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_file_name.c_str(),
206 bitmap_index));
207 UniquePtr<accounting::SpaceBitmap> bitmap(
208 accounting::SpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
209 reinterpret_cast<byte*>(map->Begin()),
210 map->Size()));
211 CHECK(bitmap.get() != nullptr) << "could not create " << bitmap_name;
212
213 Runtime* runtime = Runtime::Current();
214 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
215 runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
216
217 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
218 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kSaveAll);
219 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
220 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsOnly);
221 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
222 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsAndArgs);
223
224 UniquePtr<ImageSpace> space(new ImageSpace(image_file_name, map.release(), bitmap.release()));
225 if (kIsDebugBuild) {
226 space->VerifyImageAllocations();
227 }
228
229 space->oat_file_.reset(space->OpenOatFile());
230 if (space->oat_file_.get() == NULL) {
231 LOG(ERROR) << "Failed to open oat file for image: " << image_file_name;
232 return NULL;
233 }
234
235 if (validate_oat_file && !space->ValidateOatFile()) {
236 LOG(WARNING) << "Failed to validate oat file for image: " << image_file_name;
237 return NULL;
238 }
239
240 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
241 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
242 << ") " << *space.get();
243 }
244 return space.release();
245 }
246
OpenOatFile() const247 OatFile* ImageSpace::OpenOatFile() const {
248 const Runtime* runtime = Runtime::Current();
249 const ImageHeader& image_header = GetImageHeader();
250 // Grab location but don't use Object::AsString as we haven't yet initialized the roots to
251 // check the down cast
252 mirror::String* oat_location =
253 down_cast<mirror::String*>(image_header.GetImageRoot(ImageHeader::kOatLocation));
254 std::string oat_filename;
255 oat_filename += runtime->GetHostPrefix();
256 oat_filename += oat_location->ToModifiedUtf8();
257 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
258 !Runtime::Current()->IsCompiler());
259 if (oat_file == NULL) {
260 LOG(ERROR) << "Failed to open oat file " << oat_filename << " referenced from image.";
261 return NULL;
262 }
263 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
264 uint32_t image_oat_checksum = image_header.GetOatChecksum();
265 if (oat_checksum != image_oat_checksum) {
266 LOG(ERROR) << "Failed to match oat file checksum " << std::hex << oat_checksum
267 << " to expected oat checksum " << std::hex << image_oat_checksum
268 << " in image";
269 return NULL;
270 }
271 return oat_file;
272 }
273
ValidateOatFile() const274 bool ImageSpace::ValidateOatFile() const {
275 CHECK(oat_file_.get() != NULL);
276 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
277 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
278 uint32_t dex_file_location_checksum;
279 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum)) {
280 LOG(WARNING) << "ValidateOatFile could not find checksum for " << dex_file_location;
281 return false;
282 }
283 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
284 LOG(WARNING) << "ValidateOatFile found checksum mismatch between oat file "
285 << oat_file_->GetLocation() << " and dex file " << dex_file_location
286 << " (" << oat_dex_file->GetDexFileLocationChecksum() << " != "
287 << dex_file_location_checksum << ")";
288 return false;
289 }
290 }
291 return true;
292 }
293
ReleaseOatFile()294 OatFile& ImageSpace::ReleaseOatFile() {
295 CHECK(oat_file_.get() != NULL);
296 return *oat_file_.release();
297 }
298
Dump(std::ostream & os) const299 void ImageSpace::Dump(std::ostream& os) const {
300 os << GetType()
301 << "begin=" << reinterpret_cast<void*>(Begin())
302 << ",end=" << reinterpret_cast<void*>(End())
303 << ",size=" << PrettySize(Size())
304 << ",name=\"" << GetName() << "\"]";
305 }
306
307 } // namespace space
308 } // namespace gc
309 } // namespace art
310