1 /*
2  * Copyright (C) 2015 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 "profile_compilation_info.h"
18 
19 #include <sys/file.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <zlib.h>
24 
25 #include <algorithm>
26 #include <cerrno>
27 #include <climits>
28 #include <cstdlib>
29 #include <iostream>
30 #include <numeric>
31 #include <random>
32 #include <string>
33 #include <vector>
34 
35 #include "android-base/file.h"
36 
37 #include "base/arena_allocator.h"
38 #include "base/dumpable.h"
39 #include "base/file_utils.h"
40 #include "base/logging.h"  // For VLOG.
41 #include "base/malloc_arena_pool.h"
42 #include "base/os.h"
43 #include "base/safe_map.h"
44 #include "base/scoped_flock.h"
45 #include "base/stl_util.h"
46 #include "base/systrace.h"
47 #include "base/time_utils.h"
48 #include "base/unix_file/fd_file.h"
49 #include "base/utils.h"
50 #include "base/zip_archive.h"
51 #include "dex/dex_file_loader.h"
52 
53 namespace art {
54 
55 const uint8_t ProfileCompilationInfo::kProfileMagic[] = { 'p', 'r', 'o', '\0' };
56 // Last profile version: merge profiles directly from the file without creating
57 // profile_compilation_info object. All the profile line headers are now placed together
58 // before corresponding method_encodings and class_ids.
59 const uint8_t ProfileCompilationInfo::kProfileVersion[] = { '0', '1', '0', '\0' };
60 const uint8_t ProfileCompilationInfo::kProfileVersionForBootImage[] = { '0', '1', '2', '\0' };
61 
62 static_assert(sizeof(ProfileCompilationInfo::kProfileVersion) == 4,
63               "Invalid profile version size");
64 static_assert(sizeof(ProfileCompilationInfo::kProfileVersionForBootImage) == 4,
65               "Invalid profile version size");
66 
67 // The name of the profile entry in the dex metadata file.
68 // DO NOT CHANGE THIS! (it's similar to classes.dex in the apk files).
69 const char ProfileCompilationInfo::kDexMetadataProfileEntry[] = "primary.prof";
70 
71 // A synthetic annotations that can be used to denote that no annotation should
72 // be associated with the profile samples. We use the empty string for the package name
73 // because that's an invalid package name and should never occur in practice.
74 const ProfileCompilationInfo::ProfileSampleAnnotation
75   ProfileCompilationInfo::ProfileSampleAnnotation::kNone =
76       ProfileCompilationInfo::ProfileSampleAnnotation("");
77 
78 static constexpr char kSampleMetadataSeparator = ':';
79 
80 static constexpr uint16_t kMaxDexFileKeyLength = PATH_MAX;
81 
82 // Debug flag to ignore checksums when testing if a method or a class is present in the profile.
83 // Used to facilitate testing profile guided compilation across a large number of apps
84 // using the same test profile.
85 static constexpr bool kDebugIgnoreChecksum = false;
86 
87 static constexpr uint8_t kIsMissingTypesEncoding = 6;
88 static constexpr uint8_t kIsMegamorphicEncoding = 7;
89 
90 static_assert(sizeof(ProfileCompilationInfo::kIndividualInlineCacheSize) == sizeof(uint8_t),
91               "InlineCache::kIndividualInlineCacheSize does not have the expect type size");
92 static_assert(ProfileCompilationInfo::kIndividualInlineCacheSize < kIsMegamorphicEncoding,
93               "InlineCache::kIndividualInlineCacheSize is larger than expected");
94 static_assert(ProfileCompilationInfo::kIndividualInlineCacheSize < kIsMissingTypesEncoding,
95               "InlineCache::kIndividualInlineCacheSize is larger than expected");
96 
97 static constexpr uint32_t kSizeWarningThresholdBytes = 500000U;
98 static constexpr uint32_t kSizeErrorThresholdBytes = 1500000U;
99 
100 static constexpr uint32_t kSizeWarningThresholdBootBytes = 25000000U;
101 static constexpr uint32_t kSizeErrorThresholdBootBytes = 100000000U;
102 
ChecksumMatch(uint32_t dex_file_checksum,uint32_t checksum)103 static bool ChecksumMatch(uint32_t dex_file_checksum, uint32_t checksum) {
104   return kDebugIgnoreChecksum || dex_file_checksum == checksum;
105 }
106 
ProfileCompilationInfo(ArenaPool * custom_arena_pool,bool for_boot_image)107 ProfileCompilationInfo::ProfileCompilationInfo(ArenaPool* custom_arena_pool, bool for_boot_image)
108     : default_arena_pool_(),
109       allocator_(custom_arena_pool),
110       info_(allocator_.Adapter(kArenaAllocProfile)),
111       profile_key_map_(std::less<const std::string>(), allocator_.Adapter(kArenaAllocProfile)) {
112   memcpy(version_,
113          for_boot_image ? kProfileVersionForBootImage : kProfileVersion,
114          kProfileVersionSize);
115 }
116 
ProfileCompilationInfo(ArenaPool * custom_arena_pool)117 ProfileCompilationInfo::ProfileCompilationInfo(ArenaPool* custom_arena_pool)
118     : ProfileCompilationInfo(custom_arena_pool, /*for_boot_image=*/ false) { }
119 
ProfileCompilationInfo()120 ProfileCompilationInfo::ProfileCompilationInfo()
121     : ProfileCompilationInfo(/*for_boot_image=*/ false) { }
122 
ProfileCompilationInfo(bool for_boot_image)123 ProfileCompilationInfo::ProfileCompilationInfo(bool for_boot_image)
124     : ProfileCompilationInfo(&default_arena_pool_, for_boot_image) { }
125 
~ProfileCompilationInfo()126 ProfileCompilationInfo::~ProfileCompilationInfo() {
127   VLOG(profiler) << Dumpable<MemStats>(allocator_.GetMemStats());
128   ClearData();
129 }
130 
AddClass(uint16_t dex_profile_idx,const dex::TypeIndex & type_idx)131 void ProfileCompilationInfo::DexPcData::AddClass(uint16_t dex_profile_idx,
132                                                  const dex::TypeIndex& type_idx) {
133   if (is_megamorphic || is_missing_types) {
134     return;
135   }
136 
137   // Perform an explicit lookup for the type instead of directly emplacing the
138   // element. We do this because emplace() allocates the node before doing the
139   // lookup and if it then finds an identical element, it shall deallocate the
140   // node. For Arena allocations, that's essentially a leak.
141   ClassReference ref(dex_profile_idx, type_idx);
142   auto it = classes.find(ref);
143   if (it != classes.end()) {
144     // The type index exists.
145     return;
146   }
147 
148   // Check if the adding the type will cause the cache to become megamorphic.
149   if (classes.size() + 1 >= ProfileCompilationInfo::kIndividualInlineCacheSize) {
150     is_megamorphic = true;
151     classes.clear();
152     return;
153   }
154 
155   // The type does not exist and the inline cache will not be megamorphic.
156   classes.insert(ref);
157 }
158 
159 // Transform the actual dex location into a key used to index the dex file in the profile.
160 // See ProfileCompilationInfo#GetProfileDexFileBaseKey as well.
GetProfileDexFileAugmentedKey(const std::string & dex_location,const ProfileSampleAnnotation & annotation)161 std::string ProfileCompilationInfo::GetProfileDexFileAugmentedKey(
162       const std::string& dex_location,
163       const ProfileSampleAnnotation& annotation) {
164   std::string base_key = GetProfileDexFileBaseKey(dex_location);
165   return annotation == ProfileSampleAnnotation::kNone
166       ? base_key
167       : base_key + kSampleMetadataSeparator + annotation.GetOriginPackageName();;
168 }
169 
170 // Transform the actual dex location into a base profile key (represented as relative paths).
171 // Note: this is OK because we don't store profiles of different apps into the same file.
172 // Apps with split apks don't cause trouble because each split has a different name and will not
173 // collide with other entries.
GetProfileDexFileBaseKey(const std::string & dex_location)174 std::string ProfileCompilationInfo::GetProfileDexFileBaseKey(const std::string& dex_location) {
175   DCHECK(!dex_location.empty());
176   size_t last_sep_index = dex_location.find_last_of('/');
177   if (last_sep_index == std::string::npos) {
178     return dex_location;
179   } else {
180     DCHECK(last_sep_index < dex_location.size());
181     return dex_location.substr(last_sep_index + 1);
182   }
183 }
184 
GetBaseKeyFromAugmentedKey(const std::string & profile_key)185 std::string ProfileCompilationInfo::GetBaseKeyFromAugmentedKey(
186     const std::string& profile_key) {
187   size_t pos = profile_key.rfind(kSampleMetadataSeparator);
188   return (pos == std::string::npos) ? profile_key : profile_key.substr(0, pos);
189 }
190 
MigrateAnnotationInfo(const std::string & base_key,const std::string & augmented_key)191 std::string ProfileCompilationInfo::MigrateAnnotationInfo(
192     const std::string& base_key,
193     const std::string& augmented_key) {
194   size_t pos = augmented_key.rfind(kSampleMetadataSeparator);
195   return (pos == std::string::npos)
196       ? base_key
197       : base_key + augmented_key.substr(pos);
198 }
199 
GetAnnotationFromKey(const std::string & augmented_key)200 ProfileCompilationInfo::ProfileSampleAnnotation ProfileCompilationInfo::GetAnnotationFromKey(
201      const std::string& augmented_key) {
202   size_t pos = augmented_key.rfind(kSampleMetadataSeparator);
203   return (pos == std::string::npos)
204       ? ProfileSampleAnnotation::kNone
205       : ProfileSampleAnnotation(augmented_key.substr(pos + 1));
206 }
207 
AddMethods(const std::vector<ProfileMethodInfo> & methods,MethodHotness::Flag flags,const ProfileSampleAnnotation & annotation)208 bool ProfileCompilationInfo::AddMethods(const std::vector<ProfileMethodInfo>& methods,
209                                         MethodHotness::Flag flags,
210                                         const ProfileSampleAnnotation& annotation) {
211   for (const ProfileMethodInfo& method : methods) {
212     if (!AddMethod(method, flags, annotation)) {
213       return false;
214     }
215   }
216   return true;
217 }
218 
MergeWith(const std::string & filename)219 bool ProfileCompilationInfo::MergeWith(const std::string& filename) {
220   std::string error;
221 #ifdef _WIN32
222   int flags = O_RDONLY;
223 #else
224   int flags = O_RDONLY | O_NOFOLLOW | O_CLOEXEC;
225 #endif
226   ScopedFlock profile_file =
227       LockedFile::Open(filename.c_str(), flags, /*block=*/false, &error);
228 
229   if (profile_file.get() == nullptr) {
230     LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error;
231     return false;
232   }
233 
234   int fd = profile_file->Fd();
235 
236   ProfileLoadStatus status = LoadInternal(fd, &error);
237   if (status == kProfileLoadSuccess) {
238     return true;
239   }
240 
241   LOG(WARNING) << "Could not load profile data from file " << filename << ": " << error;
242   return false;
243 }
244 
Load(const std::string & filename,bool clear_if_invalid)245 bool ProfileCompilationInfo::Load(const std::string& filename, bool clear_if_invalid) {
246   ScopedTrace trace(__PRETTY_FUNCTION__);
247   std::string error;
248 
249   if (!IsEmpty()) {
250     return false;
251   }
252 
253 #ifdef _WIN32
254   int flags = O_RDWR;
255 #else
256   int flags = O_RDWR | O_NOFOLLOW | O_CLOEXEC;
257 #endif
258   // There's no need to fsync profile data right away. We get many chances
259   // to write it again in case something goes wrong. We can rely on a simple
260   // close(), no sync, and let to the kernel decide when to write to disk.
261   ScopedFlock profile_file =
262       LockedFile::Open(filename.c_str(), flags, /*block=*/false, &error);
263 
264   if (profile_file.get() == nullptr) {
265     LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error;
266     return false;
267   }
268 
269   int fd = profile_file->Fd();
270 
271   ProfileLoadStatus status = LoadInternal(fd, &error);
272   if (status == kProfileLoadSuccess) {
273     return true;
274   }
275 
276   if (clear_if_invalid &&
277       ((status == kProfileLoadVersionMismatch) || (status == kProfileLoadBadData))) {
278     LOG(WARNING) << "Clearing bad or obsolete profile data from file "
279                  << filename << ": " << error;
280     if (profile_file->ClearContent()) {
281       return true;
282     } else {
283       PLOG(WARNING) << "Could not clear profile file: " << filename;
284       return false;
285     }
286   }
287 
288   LOG(WARNING) << "Could not load profile data from file " << filename << ": " << error;
289   return false;
290 }
291 
Save(const std::string & filename,uint64_t * bytes_written)292 bool ProfileCompilationInfo::Save(const std::string& filename, uint64_t* bytes_written) {
293   ScopedTrace trace(__PRETTY_FUNCTION__);
294   std::string error;
295 #ifdef _WIN32
296   int flags = O_WRONLY;
297 #else
298   int flags = O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
299 #endif
300   // There's no need to fsync profile data right away. We get many chances
301   // to write it again in case something goes wrong. We can rely on a simple
302   // close(), no sync, and let to the kernel decide when to write to disk.
303   ScopedFlock profile_file =
304       LockedFile::Open(filename.c_str(), flags, /*block=*/false, &error);
305   if (profile_file.get() == nullptr) {
306     LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error;
307     return false;
308   }
309 
310   int fd = profile_file->Fd();
311 
312   // We need to clear the data because we don't support appending to the profiles yet.
313   if (!profile_file->ClearContent()) {
314     PLOG(WARNING) << "Could not clear profile file: " << filename;
315     return false;
316   }
317 
318   // This doesn't need locking because we are trying to lock the file for exclusive
319   // access and fail immediately if we can't.
320   bool result = Save(fd);
321   if (result) {
322     int64_t size = OS::GetFileSizeBytes(filename.c_str());
323     if (size != -1) {
324       VLOG(profiler)
325         << "Successfully saved profile info to " << filename << " Size: "
326         << size;
327       if (bytes_written != nullptr) {
328         *bytes_written = static_cast<uint64_t>(size);
329       }
330     }
331   } else {
332     VLOG(profiler) << "Failed to save profile info to " << filename;
333   }
334   return result;
335 }
336 
337 // Returns true if all the bytes were successfully written to the file descriptor.
WriteBuffer(int fd,const uint8_t * buffer,size_t byte_count)338 static bool WriteBuffer(int fd, const uint8_t* buffer, size_t byte_count) {
339   while (byte_count > 0) {
340     int bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer, byte_count));
341     if (bytes_written == -1) {
342       return false;
343     }
344     byte_count -= bytes_written;  // Reduce the number of remaining bytes.
345     buffer += bytes_written;  // Move the buffer forward.
346   }
347   return true;
348 }
349 
350 // Add the string bytes to the buffer.
AddStringToBuffer(std::vector<uint8_t> * buffer,const std::string & value)351 static void AddStringToBuffer(std::vector<uint8_t>* buffer, const std::string& value) {
352   buffer->insert(buffer->end(), value.begin(), value.end());
353 }
354 
355 // Insert each byte, from low to high into the buffer.
356 template <typename T>
AddUintToBuffer(std::vector<uint8_t> * buffer,T value)357 static void AddUintToBuffer(std::vector<uint8_t>* buffer, T value) {
358   for (size_t i = 0; i < sizeof(T); i++) {
359     buffer->push_back((value >> (i * kBitsPerByte)) & 0xff);
360   }
361 }
362 
363 static constexpr size_t kLineHeaderSize =
364     2 * sizeof(uint16_t) +  // class_set.size + dex_location.size
365     3 * sizeof(uint32_t);   // method_map.size + checksum + num_method_ids
366 
367 /**
368  * Serialization format:
369  * [profile_header, zipped[[profile_line_header1, profile_line_header2...],[profile_line_data1,
370  *    profile_line_data2...]]
371  * profile_header:
372  *   magic,version,number_of_dex_files,uncompressed_size_of_zipped_data,compressed_data_size
373  * profile_line_header:
374  *   profile_key,number_of_classes,methods_region_size,dex_location_checksum,num_method_ids
375  * profile_line_data:
376  *   method_encoding_1,method_encoding_2...,class_id1,class_id2...,method_flags bitmap,
377  * The method_encoding is:
378  *    method_id,number_of_inline_caches,inline_cache1,inline_cache2...
379  * The inline_cache is:
380  *    dex_pc,[M|dex_map_size], dex_profile_index,class_id1,class_id2...,dex_profile_index2,...
381  *    dex_map_size is the number of dex_indeces that follows.
382  *       Classes are grouped per their dex files and the line
383  *       `dex_profile_index,class_id1,class_id2...,dex_profile_index2,...` encodes the
384  *       mapping from `dex_profile_index` to the set of classes `class_id1,class_id2...`
385  *    M stands for megamorphic or missing types and it's encoded as either
386  *    the byte kIsMegamorphicEncoding or kIsMissingTypesEncoding.
387  *    When present, there will be no class ids following.
388  **/
Save(int fd)389 bool ProfileCompilationInfo::Save(int fd) {
390   uint64_t start = NanoTime();
391   ScopedTrace trace(__PRETTY_FUNCTION__);
392   DCHECK_GE(fd, 0);
393 
394   // Use a vector wrapper to avoid keeping track of offsets when we add elements.
395   std::vector<uint8_t> buffer;
396   if (!WriteBuffer(fd, kProfileMagic, sizeof(kProfileMagic))) {
397     return false;
398   }
399   if (!WriteBuffer(fd, version_, sizeof(version_))) {
400     return false;
401   }
402 
403   DCHECK_LE(info_.size(), MaxProfileIndex());
404   WriteProfileIndex(&buffer, static_cast<ProfileIndexType>(info_.size()));
405 
406   uint32_t required_capacity = 0;
407   for (const DexFileData* dex_data_ptr : info_) {
408     const DexFileData& dex_data = *dex_data_ptr;
409     uint32_t methods_region_size = GetMethodsRegionSize(dex_data);
410     required_capacity += kLineHeaderSize +
411         dex_data.profile_key.size() +
412         sizeof(uint16_t) * dex_data.class_set.size() +
413         methods_region_size +
414         dex_data.bitmap_storage.size();
415   }
416   // Allow large profiles for non target builds for the case where we are merging many profiles
417   // to generate a boot image profile.
418   VLOG(profiler) << "Required capacity: " << required_capacity << " bytes.";
419   if (required_capacity > GetSizeErrorThresholdBytes()) {
420     LOG(ERROR) << "Profile data size exceeds "
421                << GetSizeErrorThresholdBytes()
422                << " bytes. Profile will not be written to disk."
423                << " It requires " << required_capacity << " bytes.";
424     return false;
425   }
426   AddUintToBuffer(&buffer, required_capacity);
427   if (!WriteBuffer(fd, buffer.data(), buffer.size())) {
428     return false;
429   }
430   // Make sure that the buffer has enough capacity to avoid repeated resizings
431   // while we add data.
432   buffer.reserve(required_capacity);
433   buffer.clear();
434 
435   // Dex files must be written in the order of their profile index. This
436   // avoids writing the index in the output file and simplifies the parsing logic.
437   // Write profile line headers.
438   for (const DexFileData* dex_data_ptr : info_) {
439     const DexFileData& dex_data = *dex_data_ptr;
440 
441     if (dex_data.profile_key.size() >= kMaxDexFileKeyLength) {
442       LOG(WARNING) << "DexFileKey exceeds allocated limit";
443       return false;
444     }
445 
446     uint32_t methods_region_size = GetMethodsRegionSize(dex_data);
447 
448     DCHECK_LE(dex_data.profile_key.size(), std::numeric_limits<uint16_t>::max());
449     DCHECK_LE(dex_data.class_set.size(), std::numeric_limits<uint16_t>::max());
450     // Write profile line header.
451     AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.profile_key.size()));
452     AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.class_set.size()));
453     AddUintToBuffer(&buffer, methods_region_size);  // uint32_t
454     AddUintToBuffer(&buffer, dex_data.checksum);  // uint32_t
455     AddUintToBuffer(&buffer, dex_data.num_method_ids);  // uint32_t
456 
457     AddStringToBuffer(&buffer, dex_data.profile_key);
458   }
459 
460   for (const DexFileData* dex_data_ptr : info_) {
461     const DexFileData& dex_data = *dex_data_ptr;
462 
463     // Note that we allow dex files without any methods or classes, so that
464     // inline caches can refer valid dex files.
465 
466     uint16_t last_method_index = 0;
467     for (const auto& method_it : dex_data.method_map) {
468       // Store the difference between the method indices. The SafeMap is ordered by
469       // method_id, so the difference will always be non negative.
470       DCHECK_GE(method_it.first, last_method_index);
471       uint16_t diff_with_last_method_index = method_it.first - last_method_index;
472       last_method_index = method_it.first;
473       AddUintToBuffer(&buffer, diff_with_last_method_index);
474       AddInlineCacheToBuffer(&buffer, method_it.second);
475     }
476 
477     uint16_t last_class_index = 0;
478     for (const auto& class_id : dex_data.class_set) {
479       // Store the difference between the class indices. The set is ordered by
480       // class_id, so the difference will always be non negative.
481       DCHECK_GE(class_id.index_, last_class_index);
482       uint16_t diff_with_last_class_index = class_id.index_ - last_class_index;
483       last_class_index = class_id.index_;
484       AddUintToBuffer(&buffer, diff_with_last_class_index);
485     }
486 
487     buffer.insert(buffer.end(),
488                   dex_data.bitmap_storage.begin(),
489                   dex_data.bitmap_storage.end());
490   }
491 
492   uint32_t output_size = 0;
493   std::unique_ptr<uint8_t[]> compressed_buffer = DeflateBuffer(buffer.data(),
494                                                                required_capacity,
495                                                                &output_size);
496 
497   if (output_size > GetSizeWarningThresholdBytes()) {
498     LOG(WARNING) << "Profile data size exceeds "
499         << GetSizeWarningThresholdBytes()
500         << " It has " << output_size << " bytes";
501   }
502 
503   buffer.clear();
504   AddUintToBuffer(&buffer, output_size);
505 
506   if (!WriteBuffer(fd, buffer.data(), buffer.size())) {
507     return false;
508   }
509   if (!WriteBuffer(fd, compressed_buffer.get(), output_size)) {
510     return false;
511   }
512   uint64_t total_time = NanoTime() - start;
513   VLOG(profiler) << "Compressed from "
514                  << std::to_string(required_capacity)
515                  << " to "
516                  << std::to_string(output_size);
517   VLOG(profiler) << "Time to save profile: " << std::to_string(total_time);
518   return true;
519 }
520 
AddInlineCacheToBuffer(std::vector<uint8_t> * buffer,const InlineCacheMap & inline_cache_map)521 void ProfileCompilationInfo::AddInlineCacheToBuffer(std::vector<uint8_t>* buffer,
522                                                     const InlineCacheMap& inline_cache_map) {
523   // Add inline cache map size.
524   AddUintToBuffer(buffer, static_cast<uint16_t>(inline_cache_map.size()));
525   if (inline_cache_map.size() == 0) {
526     return;
527   }
528   for (const auto& inline_cache_it : inline_cache_map) {
529     uint16_t dex_pc = inline_cache_it.first;
530     const DexPcData dex_pc_data = inline_cache_it.second;
531     const ClassSet& classes = dex_pc_data.classes;
532 
533     // Add the dex pc.
534     AddUintToBuffer(buffer, dex_pc);
535 
536     // Add the megamorphic/missing_types encoding if needed and continue.
537     // In either cases we don't add any classes to the profiles and so there's
538     // no point to continue.
539     // TODO(calin): in case we miss types there is still value to add the
540     // rest of the classes. They can be added without bumping the profile version.
541     if (dex_pc_data.is_missing_types) {
542       DCHECK(!dex_pc_data.is_megamorphic);  // at this point the megamorphic flag should not be set.
543       DCHECK_EQ(classes.size(), 0u);
544       AddUintToBuffer(buffer, kIsMissingTypesEncoding);
545       continue;
546     } else if (dex_pc_data.is_megamorphic) {
547       DCHECK_EQ(classes.size(), 0u);
548       AddUintToBuffer(buffer, kIsMegamorphicEncoding);
549       continue;
550     }
551 
552     DCHECK_LT(classes.size(), ProfileCompilationInfo::kIndividualInlineCacheSize);
553     DCHECK_NE(classes.size(), 0u) << "InlineCache contains a dex_pc with 0 classes";
554 
555     SafeMap<ProfileIndexType, std::vector<dex::TypeIndex>> dex_to_classes_map;
556     // Group the classes by dex. We expect that most of the classes will come from
557     // the same dex, so this will be more efficient than encoding the dex index
558     // for each class reference.
559     GroupClassesByDex(classes, &dex_to_classes_map);
560     // Add the dex map size.
561     AddUintToBuffer(buffer, static_cast<uint8_t>(dex_to_classes_map.size()));
562     for (const auto& dex_it : dex_to_classes_map) {
563       ProfileIndexType dex_profile_index = dex_it.first;
564       const std::vector<dex::TypeIndex>& dex_classes = dex_it.second;
565       // Add the dex profile index.
566       WriteProfileIndex(buffer, dex_profile_index);
567       // Add the the number of classes for each dex profile index.
568       AddUintToBuffer(buffer, static_cast<uint8_t>(dex_classes.size()));
569       for (size_t i = 0; i < dex_classes.size(); i++) {
570         // Add the type index of the classes.
571         AddUintToBuffer(buffer, dex_classes[i].index_);
572       }
573     }
574   }
575 }
576 
GetMethodsRegionSize(const DexFileData & dex_data)577 uint32_t ProfileCompilationInfo::GetMethodsRegionSize(const DexFileData& dex_data) {
578   // ((uint16_t)method index + (uint16_t)inline cache size) * number of methods
579   uint32_t size = 2 * sizeof(uint16_t) * dex_data.method_map.size();
580   for (const auto& method_it : dex_data.method_map) {
581     const InlineCacheMap& inline_cache = method_it.second;
582     size += sizeof(uint16_t) * inline_cache.size();  // dex_pc
583     for (const auto& inline_cache_it : inline_cache) {
584       const ClassSet& classes = inline_cache_it.second.classes;
585       SafeMap<ProfileIndexType, std::vector<dex::TypeIndex>> dex_to_classes_map;
586       GroupClassesByDex(classes, &dex_to_classes_map);
587       size += sizeof(uint8_t);  // dex_to_classes_map size
588       for (const auto& dex_it : dex_to_classes_map) {
589         size += SizeOfProfileIndexType();  // dex profile index
590         size += sizeof(uint8_t);  // number of classes
591         const std::vector<dex::TypeIndex>& dex_classes = dex_it.second;
592         size += sizeof(uint16_t) * dex_classes.size();  // the actual classes
593       }
594     }
595   }
596   return size;
597 }
598 
GroupClassesByDex(const ClassSet & classes,SafeMap<ProfileIndexType,std::vector<dex::TypeIndex>> * dex_to_classes_map)599 void ProfileCompilationInfo::GroupClassesByDex(
600     const ClassSet& classes,
601     /*out*/SafeMap<ProfileIndexType, std::vector<dex::TypeIndex>>* dex_to_classes_map) {
602   for (const auto& classes_it : classes) {
603     auto dex_it = dex_to_classes_map->FindOrAdd(classes_it.dex_profile_index);
604     dex_it->second.push_back(classes_it.type_index);
605   }
606 }
607 
GetOrAddDexFileData(const std::string & profile_key,uint32_t checksum,uint32_t num_method_ids)608 ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::GetOrAddDexFileData(
609     const std::string& profile_key,
610     uint32_t checksum,
611     uint32_t num_method_ids) {
612   const auto profile_index_it = profile_key_map_.FindOrAdd(profile_key, profile_key_map_.size());
613   if (profile_key_map_.size() > MaxProfileIndex()) {
614     // Allow only a limited number dex files to be profiled. This allows us to save bytes
615     // when encoding. For regular profiles this 2^8, and for boot profiles is 2^16
616     // (well above what we expect for normal applications).
617     if (kIsDebugBuild) {
618       LOG(ERROR) << "Exceeded the maximum number of dex file. Something went wrong";
619     }
620     profile_key_map_.erase(profile_key);
621     return nullptr;
622   }
623 
624   ProfileIndexType profile_index = profile_index_it->second;
625   if (info_.size() <= profile_index) {
626     // This is a new addition. Add it to the info_ array.
627     DexFileData* dex_file_data = new (&allocator_) DexFileData(
628         &allocator_,
629         profile_key,
630         checksum,
631         profile_index,
632         num_method_ids,
633         IsForBootImage());
634     info_.push_back(dex_file_data);
635   }
636   DexFileData* result = info_[profile_index];
637 
638   // Check that the checksum matches.
639   // This may different if for example the dex file was updated and we had a record of the old one.
640   if (result->checksum != checksum) {
641     LOG(WARNING) << "Checksum mismatch for dex " << profile_key;
642     return nullptr;
643   }
644 
645   // DCHECK that profile info map key is consistent with the one stored in the dex file data.
646   // This should always be the case since since the cache map is managed by ProfileCompilationInfo.
647   DCHECK_EQ(profile_key, result->profile_key);
648   DCHECK_EQ(profile_index, result->profile_index);
649 
650   if (num_method_ids != result->num_method_ids) {
651     // This should not happen... added to help investigating b/65812889.
652     LOG(ERROR) << "num_method_ids mismatch for dex " << profile_key
653         << ", expected=" << num_method_ids
654         << ", actual=" << result->num_method_ids;
655     return nullptr;
656   }
657 
658   return result;
659 }
660 
FindDexData(const std::string & profile_key,uint32_t checksum,bool verify_checksum) const661 const ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::FindDexData(
662       const std::string& profile_key,
663       uint32_t checksum,
664       bool verify_checksum) const {
665   const auto profile_index_it = profile_key_map_.find(profile_key);
666   if (profile_index_it == profile_key_map_.end()) {
667     return nullptr;
668   }
669 
670   ProfileIndexType profile_index = profile_index_it->second;
671   const DexFileData* result = info_[profile_index];
672   if (verify_checksum && !ChecksumMatch(result->checksum, checksum)) {
673     return nullptr;
674   }
675   DCHECK_EQ(profile_key, result->profile_key);
676   DCHECK_EQ(profile_index, result->profile_index);
677   return result;
678 }
679 
FindDexDataUsingAnnotations(const DexFile * dex_file,const ProfileSampleAnnotation & annotation) const680 const ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::FindDexDataUsingAnnotations(
681       const DexFile* dex_file,
682       const ProfileSampleAnnotation& annotation) const {
683   if (annotation == ProfileSampleAnnotation::kNone) {
684     std::string profile_key = GetProfileDexFileBaseKey(dex_file->GetLocation());
685     for (const DexFileData* dex_data : info_) {
686       if (profile_key == GetBaseKeyFromAugmentedKey(dex_data->profile_key)) {
687         if (!ChecksumMatch(dex_data->checksum, dex_file->GetLocationChecksum())) {
688           return nullptr;
689         }
690         return dex_data;
691       }
692     }
693   } else {
694     std::string profile_key = GetProfileDexFileAugmentedKey(dex_file->GetLocation(), annotation);
695     return FindDexData(profile_key, dex_file->GetLocationChecksum());
696   }
697 
698   return nullptr;
699 }
700 
FindAllDexData(const DexFile * dex_file,std::vector<const ProfileCompilationInfo::DexFileData * > * result) const701 void ProfileCompilationInfo::FindAllDexData(
702     const DexFile* dex_file,
703     /*out*/ std::vector<const ProfileCompilationInfo::DexFileData*>* result) const {
704   std::string profile_key = GetProfileDexFileBaseKey(dex_file->GetLocation());
705   for (const DexFileData* dex_data : info_) {
706     if (profile_key == GetBaseKeyFromAugmentedKey(dex_data->profile_key)) {
707       if (ChecksumMatch(dex_data->checksum, dex_file->GetLocationChecksum())) {
708         result->push_back(dex_data);
709       }
710     }
711   }
712 }
713 
AddMethod(const ProfileMethodInfo & pmi,MethodHotness::Flag flags,const ProfileSampleAnnotation & annotation)714 bool ProfileCompilationInfo::AddMethod(const ProfileMethodInfo& pmi,
715                                        MethodHotness::Flag flags,
716                                        const ProfileSampleAnnotation& annotation) {
717   DexFileData* const data = GetOrAddDexFileData(pmi.ref.dex_file, annotation);
718   if (data == nullptr) {  // checksum mismatch
719     return false;
720   }
721   if (!data->AddMethod(flags, pmi.ref.index)) {
722     return false;
723   }
724   if ((flags & MethodHotness::kFlagHot) == 0) {
725     // The method is not hot, do not add inline caches.
726     return true;
727   }
728 
729   // Add inline caches. Do this only for regular profiles. The boot image profiles don't use
730   // them and they take up useless space.
731   if (IsForBootImage()) {
732     return true;  // early success return.
733   }
734   InlineCacheMap* inline_cache = data->FindOrAddHotMethod(pmi.ref.index);
735   DCHECK(inline_cache != nullptr);
736 
737   for (const ProfileMethodInfo::ProfileInlineCache& cache : pmi.inline_caches) {
738     if (cache.is_missing_types) {
739       FindOrAddDexPc(inline_cache, cache.dex_pc)->SetIsMissingTypes();
740       continue;
741     }
742     for (const TypeReference& class_ref : cache.classes) {
743       DexFileData* class_dex_data = GetOrAddDexFileData(class_ref.dex_file, annotation);
744       if (class_dex_data == nullptr) {  // checksum mismatch
745         return false;
746       }
747       DexPcData* dex_pc_data = FindOrAddDexPc(inline_cache, cache.dex_pc);
748       if (dex_pc_data->is_missing_types) {
749         // Don't bother adding classes if we are missing types.
750         break;
751       }
752       dex_pc_data->AddClass(class_dex_data->profile_index, class_ref.TypeIndex());
753     }
754   }
755   return true;
756 }
757 
758 #define READ_UINT(type, buffer, dest, error)            \
759   do {                                                  \
760     if (!(buffer).ReadUintAndAdvance<type>(&(dest))) {  \
761       *(error) = "Could not read "#dest;                \
762       return false;                                     \
763     }                                                   \
764   }                                                     \
765   while (false)
766 
ReadInlineCache(SafeBuffer & buffer,ProfileIndexType number_of_dex_files,const SafeMap<ProfileIndexType,ProfileIndexType> & dex_profile_index_remap,InlineCacheMap * inline_cache,std::string * error)767 bool ProfileCompilationInfo::ReadInlineCache(
768     SafeBuffer& buffer,
769     ProfileIndexType number_of_dex_files,
770     const SafeMap<ProfileIndexType, ProfileIndexType>& dex_profile_index_remap,
771     /*out*/ InlineCacheMap* inline_cache,
772     /*out*/ std::string* error) {
773   uint16_t inline_cache_size;
774   READ_UINT(uint16_t, buffer, inline_cache_size, error);
775   for (; inline_cache_size > 0; inline_cache_size--) {
776     uint16_t dex_pc;
777     uint8_t dex_to_classes_map_size;
778     READ_UINT(uint16_t, buffer, dex_pc, error);
779     READ_UINT(uint8_t, buffer, dex_to_classes_map_size, error);
780     DexPcData* dex_pc_data = FindOrAddDexPc(inline_cache, dex_pc);
781     if (dex_to_classes_map_size == kIsMissingTypesEncoding) {
782       dex_pc_data->SetIsMissingTypes();
783       continue;
784     }
785     if (dex_to_classes_map_size == kIsMegamorphicEncoding) {
786       dex_pc_data->SetIsMegamorphic();
787       continue;
788     }
789     for (; dex_to_classes_map_size > 0; dex_to_classes_map_size--) {
790       ProfileIndexType dex_profile_index;
791       uint8_t dex_classes_size;
792       if (!ReadProfileIndex(buffer, &dex_profile_index)) {
793         *error = "Cannot read profile index";
794         return false;
795       }
796       READ_UINT(uint8_t, buffer, dex_classes_size, error);
797       if (dex_profile_index >= number_of_dex_files) {
798         *error = "dex_profile_index out of bounds ";
799         *error += std::to_string(dex_profile_index) + " " + std::to_string(number_of_dex_files);
800         return false;
801       }
802       for (; dex_classes_size > 0; dex_classes_size--) {
803         uint16_t type_index;
804         READ_UINT(uint16_t, buffer, type_index, error);
805         auto it = dex_profile_index_remap.find(dex_profile_index);
806         if (it == dex_profile_index_remap.end()) {
807           // If we don't have an index that's because the dex file was filtered out when loading.
808           // Set missing types on the dex pc data.
809           dex_pc_data->SetIsMissingTypes();
810         } else {
811           dex_pc_data->AddClass(it->second, dex::TypeIndex(type_index));
812         }
813       }
814     }
815   }
816   return true;
817 }
818 
ReadMethods(SafeBuffer & buffer,ProfileIndexType number_of_dex_files,const ProfileLineHeader & line_header,const SafeMap<ProfileIndexType,ProfileIndexType> & dex_profile_index_remap,std::string * error)819 bool ProfileCompilationInfo::ReadMethods(
820     SafeBuffer& buffer,
821     ProfileIndexType number_of_dex_files,
822     const ProfileLineHeader& line_header,
823     const SafeMap<ProfileIndexType, ProfileIndexType>& dex_profile_index_remap,
824     /*out*/std::string* error) {
825   uint32_t unread_bytes_before_operation = buffer.CountUnreadBytes();
826   if (unread_bytes_before_operation < line_header.method_region_size_bytes) {
827     *error += "Profile EOF reached prematurely for ReadMethod";
828     return false;
829   }
830   size_t expected_unread_bytes_after_operation = buffer.CountUnreadBytes()
831       - line_header.method_region_size_bytes;
832   uint16_t last_method_index = 0;
833   while (buffer.CountUnreadBytes() > expected_unread_bytes_after_operation) {
834     DexFileData* const data = GetOrAddDexFileData(line_header.profile_key,
835                                                   line_header.checksum,
836                                                   line_header.num_method_ids);
837     uint16_t diff_with_last_method_index;
838     READ_UINT(uint16_t, buffer, diff_with_last_method_index, error);
839     uint16_t method_index = last_method_index + diff_with_last_method_index;
840     last_method_index = method_index;
841     InlineCacheMap* inline_cache = data->FindOrAddHotMethod(method_index);
842     if (inline_cache == nullptr) {
843       return false;
844     }
845     if (!ReadInlineCache(buffer,
846                          number_of_dex_files,
847                          dex_profile_index_remap,
848                          inline_cache,
849                          error)) {
850       return false;
851     }
852   }
853   uint32_t total_bytes_read = unread_bytes_before_operation - buffer.CountUnreadBytes();
854   if (total_bytes_read != line_header.method_region_size_bytes) {
855     *error += "Profile data inconsistent for ReadMethods";
856     return false;
857   }
858   return true;
859 }
860 
ReadClasses(SafeBuffer & buffer,const ProfileLineHeader & line_header,std::string * error)861 bool ProfileCompilationInfo::ReadClasses(SafeBuffer& buffer,
862                                          const ProfileLineHeader& line_header,
863                                          /*out*/std::string* error) {
864   size_t unread_bytes_before_op = buffer.CountUnreadBytes();
865   if (unread_bytes_before_op < line_header.class_set_size) {
866     *error += "Profile EOF reached prematurely for ReadClasses";
867     return false;
868   }
869 
870   uint16_t last_class_index = 0;
871   for (uint16_t i = 0; i < line_header.class_set_size; i++) {
872     uint16_t diff_with_last_class_index;
873     READ_UINT(uint16_t, buffer, diff_with_last_class_index, error);
874     uint16_t type_index = last_class_index + diff_with_last_class_index;
875     last_class_index = type_index;
876 
877     DexFileData* const data = GetOrAddDexFileData(line_header.profile_key,
878                                                   line_header.checksum,
879                                                   line_header.num_method_ids);
880     if (data == nullptr) {
881        return false;
882     }
883     data->class_set.insert(dex::TypeIndex(type_index));
884   }
885   size_t total_bytes_read = unread_bytes_before_op - buffer.CountUnreadBytes();
886   uint32_t expected_bytes_read = line_header.class_set_size * sizeof(uint16_t);
887   if (total_bytes_read != expected_bytes_read) {
888     *error += "Profile data inconsistent for ReadClasses";
889     return false;
890   }
891   return true;
892 }
893 
894 // Tests for EOF by trying to read 1 byte from the descriptor.
895 // Returns:
896 //   0 if the descriptor is at the EOF,
897 //  -1 if there was an IO error
898 //   1 if the descriptor has more content to read
testEOF(int fd)899 static int testEOF(int fd) {
900   uint8_t buffer[1];
901   return TEMP_FAILURE_RETRY(read(fd, buffer, 1));
902 }
903 
904 // Reads an uint value previously written with AddUintToBuffer.
905 template <typename T>
ReadUintAndAdvance(T * value)906 bool ProfileCompilationInfo::SafeBuffer::ReadUintAndAdvance(/*out*/T* value) {
907   static_assert(std::is_unsigned<T>::value, "Type is not unsigned");
908   if (ptr_current_ + sizeof(T) > ptr_end_) {
909     return false;
910   }
911   *value = 0;
912   for (size_t i = 0; i < sizeof(T); i++) {
913     *value += ptr_current_[i] << (i * kBitsPerByte);
914   }
915   ptr_current_ += sizeof(T);
916   return true;
917 }
918 
CompareAndAdvance(const uint8_t * data,size_t data_size)919 bool ProfileCompilationInfo::SafeBuffer::CompareAndAdvance(const uint8_t* data, size_t data_size) {
920   if (ptr_current_ + data_size > ptr_end_) {
921     return false;
922   }
923   if (memcmp(ptr_current_, data, data_size) == 0) {
924     ptr_current_ += data_size;
925     return true;
926   }
927   return false;
928 }
929 
Fill(ProfileSource & source,const std::string & debug_stage,std::string * error)930 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::SafeBuffer::Fill(
931       ProfileSource& source,
932       const std::string& debug_stage,
933       /*out*/ std::string* error) {
934   size_t byte_count = (ptr_end_ - ptr_current_) * sizeof(*ptr_current_);
935   uint8_t* buffer = ptr_current_;
936   return source.Read(buffer, byte_count, debug_stage, error);
937 }
938 
CountUnreadBytes()939 size_t ProfileCompilationInfo::SafeBuffer::CountUnreadBytes() {
940   return (ptr_end_ - ptr_current_) * sizeof(*ptr_current_);
941 }
942 
GetCurrentPtr()943 const uint8_t* ProfileCompilationInfo::SafeBuffer::GetCurrentPtr() {
944   return ptr_current_;
945 }
946 
Advance(size_t data_size)947 void ProfileCompilationInfo::SafeBuffer::Advance(size_t data_size) {
948   ptr_current_ += data_size;
949 }
950 
ReadProfileHeader(ProfileSource & source,ProfileIndexType * number_of_dex_files,uint32_t * uncompressed_data_size,uint32_t * compressed_data_size,std::string * error)951 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadProfileHeader(
952       ProfileSource& source,
953       /*out*/ProfileIndexType* number_of_dex_files,
954       /*out*/uint32_t* uncompressed_data_size,
955       /*out*/uint32_t* compressed_data_size,
956       /*out*/std::string* error) {
957   // Read magic and version
958   const size_t kMagicVersionSize =
959     sizeof(kProfileMagic) +
960     kProfileVersionSize;
961   SafeBuffer safe_buffer_version(kMagicVersionSize);
962 
963   ProfileLoadStatus status = safe_buffer_version.Fill(source, "ReadProfileHeaderVersion", error);
964   if (status != kProfileLoadSuccess) {
965     return status;
966   }
967 
968   if (!safe_buffer_version.CompareAndAdvance(kProfileMagic, sizeof(kProfileMagic))) {
969     *error = "Profile missing magic";
970     return kProfileLoadVersionMismatch;
971   }
972   if (safe_buffer_version.CountUnreadBytes() < kProfileVersionSize) {
973      *error = "Cannot read profile version";
974      return kProfileLoadBadData;
975   }
976   memcpy(version_, safe_buffer_version.GetCurrentPtr(), kProfileVersionSize);
977   if ((memcmp(version_, kProfileVersion, kProfileVersionSize) != 0) &&
978       (memcmp(version_, kProfileVersionForBootImage, kProfileVersionSize) != 0)) {
979     *error = "Profile version mismatch";
980     return kProfileLoadVersionMismatch;
981   }
982 
983   const size_t kProfileHeaderDataSize =
984     SizeOfProfileIndexType() +  // number of dex files
985     sizeof(uint32_t) +  // size of uncompressed profile data
986     sizeof(uint32_t);  // size of compressed profile data
987   SafeBuffer safe_buffer_header_data(kProfileHeaderDataSize);
988 
989   status = safe_buffer_header_data.Fill(source, "ReadProfileHeaderData", error);
990   if (status != kProfileLoadSuccess) {
991     return status;
992   }
993 
994   if (!ReadProfileIndex(safe_buffer_header_data, number_of_dex_files)) {
995     *error = "Cannot read the number of dex files";
996     return kProfileLoadBadData;
997   }
998   if (!safe_buffer_header_data.ReadUintAndAdvance<uint32_t>(uncompressed_data_size)) {
999     *error = "Cannot read the size of uncompressed data";
1000     return kProfileLoadBadData;
1001   }
1002   if (!safe_buffer_header_data.ReadUintAndAdvance<uint32_t>(compressed_data_size)) {
1003     *error = "Cannot read the size of compressed data";
1004     return kProfileLoadBadData;
1005   }
1006   return kProfileLoadSuccess;
1007 }
1008 
ReadProfileLineHeaderElements(SafeBuffer & buffer,uint16_t * profile_key_size,ProfileLineHeader * line_header,std::string * error)1009 bool ProfileCompilationInfo::ReadProfileLineHeaderElements(SafeBuffer& buffer,
1010                                                            /*out*/uint16_t* profile_key_size,
1011                                                            /*out*/ProfileLineHeader* line_header,
1012                                                            /*out*/std::string* error) {
1013   READ_UINT(uint16_t, buffer, *profile_key_size, error);
1014   READ_UINT(uint16_t, buffer, line_header->class_set_size, error);
1015   READ_UINT(uint32_t, buffer, line_header->method_region_size_bytes, error);
1016   READ_UINT(uint32_t, buffer, line_header->checksum, error);
1017   READ_UINT(uint32_t, buffer, line_header->num_method_ids, error);
1018   return true;
1019 }
1020 
ReadProfileLineHeader(SafeBuffer & buffer,ProfileLineHeader * line_header,std::string * error)1021 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadProfileLineHeader(
1022     SafeBuffer& buffer,
1023     /*out*/ProfileLineHeader* line_header,
1024     /*out*/std::string* error) {
1025   if (buffer.CountUnreadBytes() < kLineHeaderSize) {
1026     *error += "Profile EOF reached prematurely for ReadProfileLineHeader";
1027     return kProfileLoadBadData;
1028   }
1029 
1030   uint16_t profile_key_size;
1031   if (!ReadProfileLineHeaderElements(buffer, &profile_key_size, line_header, error)) {
1032     return kProfileLoadBadData;
1033   }
1034 
1035   if (profile_key_size == 0 || profile_key_size > kMaxDexFileKeyLength) {
1036     *error = "ProfileKey has an invalid size: " +
1037         std::to_string(static_cast<uint32_t>(profile_key_size));
1038     return kProfileLoadBadData;
1039   }
1040 
1041   if (buffer.CountUnreadBytes() < profile_key_size) {
1042     *error += "Profile EOF reached prematurely for ReadProfileHeaderDexLocation";
1043     return kProfileLoadBadData;
1044   }
1045   const uint8_t* base_ptr = buffer.GetCurrentPtr();
1046   line_header->profile_key.assign(
1047       reinterpret_cast<const char*>(base_ptr), profile_key_size);
1048   buffer.Advance(profile_key_size);
1049   return kProfileLoadSuccess;
1050 }
1051 
ReadProfileLine(SafeBuffer & buffer,ProfileIndexType number_of_dex_files,const ProfileLineHeader & line_header,const SafeMap<ProfileIndexType,ProfileIndexType> & dex_profile_index_remap,bool merge_classes,std::string * error)1052 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadProfileLine(
1053       SafeBuffer& buffer,
1054       ProfileIndexType number_of_dex_files,
1055       const ProfileLineHeader& line_header,
1056       const SafeMap<ProfileIndexType, ProfileIndexType>& dex_profile_index_remap,
1057       bool merge_classes,
1058       /*out*/std::string* error) {
1059   DexFileData* data = GetOrAddDexFileData(line_header.profile_key,
1060                                           line_header.checksum,
1061                                           line_header.num_method_ids);
1062   if (data == nullptr) {
1063     *error = "Error when reading profile file line header: checksum mismatch for "
1064         + line_header.profile_key;
1065     return kProfileLoadBadData;
1066   }
1067 
1068   if (!ReadMethods(buffer, number_of_dex_files, line_header, dex_profile_index_remap, error)) {
1069     return kProfileLoadBadData;
1070   }
1071 
1072   if (merge_classes) {
1073     if (!ReadClasses(buffer, line_header, error)) {
1074       return kProfileLoadBadData;
1075     }
1076   }
1077 
1078   // Read method bitmap.
1079   const size_t bytes = data->bitmap_storage.size();
1080   if (buffer.CountUnreadBytes() < bytes) {
1081     *error += "Profile EOF reached prematurely for ReadProfileHeaderDexLocation";
1082     return kProfileLoadBadData;
1083   }
1084   const uint8_t* base_ptr = buffer.GetCurrentPtr();
1085   std::copy_n(base_ptr, bytes, data->bitmap_storage.data());
1086   buffer.Advance(bytes);
1087 
1088   return kProfileLoadSuccess;
1089 }
1090 
1091 // TODO(calin): Fix this API. ProfileCompilationInfo::Load should be static and
1092 // return a unique pointer to a ProfileCompilationInfo upon success.
Load(int fd,bool merge_classes,const ProfileLoadFilterFn & filter_fn)1093 bool ProfileCompilationInfo::Load(
1094     int fd, bool merge_classes, const ProfileLoadFilterFn& filter_fn) {
1095   std::string error;
1096 
1097   ProfileLoadStatus status = LoadInternal(fd, &error, merge_classes, filter_fn);
1098 
1099   if (status == kProfileLoadSuccess) {
1100     return true;
1101   } else {
1102     LOG(WARNING) << "Error when reading profile: " << error;
1103     return false;
1104   }
1105 }
1106 
VerifyProfileData(const std::vector<const DexFile * > & dex_files)1107 bool ProfileCompilationInfo::VerifyProfileData(const std::vector<const DexFile*>& dex_files) {
1108   std::unordered_map<std::string, const DexFile*> key_to_dex_file;
1109   for (const DexFile* dex_file : dex_files) {
1110     key_to_dex_file.emplace(GetProfileDexFileBaseKey(dex_file->GetLocation()), dex_file);
1111   }
1112   for (const DexFileData* dex_data : info_) {
1113     // We need to remove any annotation from the key during verification.
1114     const auto it = key_to_dex_file.find(GetBaseKeyFromAugmentedKey(dex_data->profile_key));
1115     if (it == key_to_dex_file.end()) {
1116       // It is okay if profile contains data for additional dex files.
1117       continue;
1118     }
1119     const DexFile* dex_file = it->second;
1120     const std::string& dex_location = dex_file->GetLocation();
1121     if (!ChecksumMatch(dex_data->checksum, dex_file->GetLocationChecksum())) {
1122       LOG(ERROR) << "Dex checksum mismatch while verifying profile "
1123                  << "dex location " << dex_location << " (checksum="
1124                  << dex_file->GetLocationChecksum() << ", profile checksum="
1125                  << dex_data->checksum;
1126       return false;
1127     }
1128 
1129     if (dex_data->num_method_ids != dex_file->NumMethodIds()) {
1130       LOG(ERROR) << "Number of method ids in dex file and profile don't match."
1131                  << "dex location " << dex_location << " NumMethodId in DexFile"
1132                  << dex_file->NumMethodIds() << ", NumMethodId in profile"
1133                  << dex_data->num_method_ids;
1134       return false;
1135     }
1136 
1137     // Verify method_encoding.
1138     for (const auto& method_it : dex_data->method_map) {
1139       size_t method_id = (size_t)(method_it.first);
1140       if (method_id >= dex_file->NumMethodIds()) {
1141         LOG(ERROR) << "Invalid method id in profile file. dex location="
1142                    << dex_location << " method_id=" << method_id << " NumMethodIds="
1143                    << dex_file->NumMethodIds();
1144         return false;
1145       }
1146 
1147       // Verify class indices of inline caches.
1148       const InlineCacheMap &inline_cache_map = method_it.second;
1149       for (const auto& inline_cache_it : inline_cache_map) {
1150         const DexPcData dex_pc_data = inline_cache_it.second;
1151         if (dex_pc_data.is_missing_types || dex_pc_data.is_megamorphic) {
1152           // No class indices to verify.
1153           continue;
1154         }
1155 
1156         const ClassSet &classes = dex_pc_data.classes;
1157         SafeMap<ProfileIndexType, std::vector<dex::TypeIndex>> dex_to_classes_map;
1158         // Group the classes by dex. We expect that most of the classes will come from
1159         // the same dex, so this will be more efficient than encoding the dex index
1160         // for each class reference.
1161         GroupClassesByDex(classes, &dex_to_classes_map);
1162         for (const auto &dex_it : dex_to_classes_map) {
1163           ProfileIndexType dex_profile_index = dex_it.first;
1164           const auto dex_file_inline_cache_it = key_to_dex_file.find(
1165               info_[dex_profile_index]->profile_key);
1166           if (dex_file_inline_cache_it == key_to_dex_file.end()) {
1167             // It is okay if profile contains data for additional dex files.
1168             continue;
1169           }
1170           const DexFile *dex_file_for_inline_cache_check = dex_file_inline_cache_it->second;
1171           const std::vector<dex::TypeIndex> &dex_classes = dex_it.second;
1172           for (size_t i = 0; i < dex_classes.size(); i++) {
1173             if (dex_classes[i].index_ >= dex_file_for_inline_cache_check->NumTypeIds()) {
1174               LOG(ERROR) << "Invalid inline cache in profile file. dex location="
1175                   << dex_location << " method_id=" << method_id
1176                   << " dex_profile_index="
1177                   << static_cast<uint16_t >(dex_profile_index) << " type_index="
1178                   << dex_classes[i].index_
1179                   << " NumTypeIds="
1180                   << dex_file_for_inline_cache_check->NumTypeIds();
1181               return false;
1182             }
1183           }
1184         }
1185       }
1186     }
1187     // Verify class_ids.
1188     for (const auto& class_id : dex_data->class_set) {
1189       if (class_id.index_ >= dex_file->NumTypeIds()) {
1190         LOG(ERROR) << "Invalid class id in profile file. dex_file location "
1191                    << dex_location << " class_id=" << class_id.index_ << " NumClassIds="
1192                    << dex_file->NumClassDefs();
1193         return false;
1194       }
1195     }
1196   }
1197   return true;
1198 }
1199 
OpenSource(int32_t fd,std::unique_ptr<ProfileSource> * source,std::string * error)1200 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::OpenSource(
1201     int32_t fd,
1202     /*out*/ std::unique_ptr<ProfileSource>* source,
1203     /*out*/ std::string* error) {
1204   if (IsProfileFile(fd)) {
1205     source->reset(ProfileSource::Create(fd));
1206     return kProfileLoadSuccess;
1207   } else {
1208     std::unique_ptr<ZipArchive> zip_archive(
1209         ZipArchive::OpenFromFd(DupCloexec(fd), "profile", error));
1210     if (zip_archive.get() == nullptr) {
1211       *error = "Could not open the profile zip archive";
1212       return kProfileLoadBadData;
1213     }
1214     std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(kDexMetadataProfileEntry, error));
1215     if (zip_entry == nullptr) {
1216       // Allow archives without the profile entry. In this case, create an empty profile.
1217       // This gives more flexible when ure-using archives that may miss the entry.
1218       // (e.g. dex metadata files)
1219       LOG(WARNING) << "Could not find entry " << kDexMetadataProfileEntry
1220           << " in the zip archive. Creating an empty profile.";
1221       source->reset(ProfileSource::Create(MemMap::Invalid()));
1222       return kProfileLoadSuccess;
1223     }
1224     if (zip_entry->GetUncompressedLength() == 0) {
1225       *error = "Empty profile entry in the zip archive.";
1226       return kProfileLoadBadData;
1227     }
1228 
1229     // TODO(calin) pass along file names to assist with debugging.
1230     MemMap map = zip_entry->MapDirectlyOrExtract(
1231         kDexMetadataProfileEntry, "profile file", error, alignof(ProfileSource));
1232 
1233     if (map.IsValid()) {
1234       source->reset(ProfileSource::Create(std::move(map)));
1235       return kProfileLoadSuccess;
1236     } else {
1237       return kProfileLoadBadData;
1238     }
1239   }
1240 }
1241 
Read(uint8_t * buffer,size_t byte_count,const std::string & debug_stage,std::string * error)1242 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ProfileSource::Read(
1243     uint8_t* buffer,
1244     size_t byte_count,
1245     const std::string& debug_stage,
1246     std::string* error) {
1247   if (IsMemMap()) {
1248     if (mem_map_cur_ + byte_count > mem_map_.Size()) {
1249       return kProfileLoadBadData;
1250     }
1251     for (size_t i = 0; i < byte_count; i++) {
1252       buffer[i] = *(mem_map_.Begin() + mem_map_cur_);
1253       mem_map_cur_++;
1254     }
1255   } else {
1256     while (byte_count > 0) {
1257       int bytes_read = TEMP_FAILURE_RETRY(read(fd_, buffer, byte_count));;
1258       if (bytes_read == 0) {
1259         *error += "Profile EOF reached prematurely for " + debug_stage;
1260         return kProfileLoadBadData;
1261       } else if (bytes_read < 0) {
1262         *error += "Profile IO error for " + debug_stage + strerror(errno);
1263         return kProfileLoadIOError;
1264       }
1265       byte_count -= bytes_read;
1266       buffer += bytes_read;
1267     }
1268   }
1269   return kProfileLoadSuccess;
1270 }
1271 
HasConsumedAllData() const1272 bool ProfileCompilationInfo::ProfileSource::HasConsumedAllData() const {
1273   return IsMemMap()
1274       ? (!mem_map_.IsValid() || mem_map_cur_ == mem_map_.Size())
1275       : (testEOF(fd_) == 0);
1276 }
1277 
HasEmptyContent() const1278 bool ProfileCompilationInfo::ProfileSource::HasEmptyContent() const {
1279   if (IsMemMap()) {
1280     return !mem_map_.IsValid() || mem_map_.Size() == 0;
1281   } else {
1282     struct stat stat_buffer;
1283     if (fstat(fd_, &stat_buffer) != 0) {
1284       return false;
1285     }
1286     return stat_buffer.st_size == 0;
1287   }
1288 }
1289 
1290 // TODO(calin): fail fast if the dex checksums don't match.
LoadInternal(int32_t fd,std::string * error,bool merge_classes,const ProfileLoadFilterFn & filter_fn)1291 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::LoadInternal(
1292       int32_t fd,
1293       std::string* error,
1294       bool merge_classes,
1295       const ProfileLoadFilterFn& filter_fn) {
1296   ScopedTrace trace(__PRETTY_FUNCTION__);
1297   DCHECK_GE(fd, 0);
1298 
1299   std::unique_ptr<ProfileSource> source;
1300   ProfileLoadStatus status = OpenSource(fd, &source, error);
1301   if (status != kProfileLoadSuccess) {
1302     return status;
1303   }
1304 
1305   // We allow empty profile files.
1306   // Profiles may be created by ActivityManager or installd before we manage to
1307   // process them in the runtime or profman.
1308   if (source->HasEmptyContent()) {
1309     return kProfileLoadSuccess;
1310   }
1311 
1312   // Read profile header: magic + version + number_of_dex_files.
1313   ProfileIndexType number_of_dex_files;
1314   uint32_t uncompressed_data_size;
1315   uint32_t compressed_data_size;
1316   status = ReadProfileHeader(*source,
1317                              &number_of_dex_files,
1318                              &uncompressed_data_size,
1319                              &compressed_data_size,
1320                              error);
1321 
1322   if (status != kProfileLoadSuccess) {
1323     return status;
1324   }
1325   // Allow large profiles for non target builds for the case where we are merging many profiles
1326   // to generate a boot image profile.
1327   if (uncompressed_data_size > GetSizeErrorThresholdBytes()) {
1328     LOG(ERROR) << "Profile data size exceeds "
1329                << GetSizeErrorThresholdBytes()
1330                << " bytes. It has " << uncompressed_data_size << " bytes.";
1331     return kProfileLoadBadData;
1332   }
1333   if (uncompressed_data_size > GetSizeWarningThresholdBytes()) {
1334     LOG(WARNING) << "Profile data size exceeds "
1335                  << GetSizeWarningThresholdBytes()
1336                  << " bytes. It has " << uncompressed_data_size << " bytes.";
1337   }
1338 
1339   std::unique_ptr<uint8_t[]> compressed_data(new uint8_t[compressed_data_size]);
1340   status = source->Read(compressed_data.get(), compressed_data_size, "ReadContent", error);
1341   if (status != kProfileLoadSuccess) {
1342     *error += "Unable to read compressed profile data";
1343     return status;
1344   }
1345 
1346   if (!source->HasConsumedAllData()) {
1347     *error += "Unexpected data in the profile file.";
1348     return kProfileLoadBadData;
1349   }
1350 
1351   SafeBuffer uncompressed_data(uncompressed_data_size);
1352 
1353   int ret = InflateBuffer(compressed_data.get(),
1354                           compressed_data_size,
1355                           uncompressed_data_size,
1356                           uncompressed_data.Get());
1357 
1358   if (ret != Z_STREAM_END) {
1359     *error += "Error reading uncompressed profile data";
1360     return kProfileLoadBadData;
1361   }
1362 
1363   std::vector<ProfileLineHeader> profile_line_headers;
1364   // Read profile line headers.
1365   for (ProfileIndexType k = 0; k < number_of_dex_files; k++) {
1366     ProfileLineHeader line_header;
1367 
1368     // First, read the line header to get the amount of data we need to read.
1369     status = ReadProfileLineHeader(uncompressed_data, &line_header, error);
1370     if (status != kProfileLoadSuccess) {
1371       return status;
1372     }
1373     profile_line_headers.push_back(line_header);
1374   }
1375 
1376   SafeMap<ProfileIndexType, ProfileIndexType> dex_profile_index_remap;
1377   if (!RemapProfileIndex(profile_line_headers, filter_fn, &dex_profile_index_remap)) {
1378     return kProfileLoadBadData;
1379   }
1380 
1381   for (ProfileIndexType k = 0; k < number_of_dex_files; k++) {
1382     if (!filter_fn(profile_line_headers[k].profile_key, profile_line_headers[k].checksum)) {
1383       // We have to skip the line. Advanced the current pointer of the buffer.
1384       size_t profile_line_size =
1385            profile_line_headers[k].class_set_size * sizeof(uint16_t) +
1386            profile_line_headers[k].method_region_size_bytes +
1387            DexFileData::ComputeBitmapStorage(IsForBootImage(),
1388               profile_line_headers[k].num_method_ids);
1389       uncompressed_data.Advance(profile_line_size);
1390     } else {
1391       // Now read the actual profile line.
1392       status = ReadProfileLine(uncompressed_data,
1393                                number_of_dex_files,
1394                                profile_line_headers[k],
1395                                dex_profile_index_remap,
1396                                merge_classes,
1397                                error);
1398       if (status != kProfileLoadSuccess) {
1399         return status;
1400       }
1401     }
1402   }
1403 
1404   // Check that we read everything and that profiles don't contain junk data.
1405   if (uncompressed_data.CountUnreadBytes() > 0) {
1406     *error = "Unexpected content in the profile file: " +
1407         std::to_string(uncompressed_data.CountUnreadBytes()) + " extra bytes";
1408     return kProfileLoadBadData;
1409   } else {
1410     return kProfileLoadSuccess;
1411   }
1412 }
1413 
RemapProfileIndex(const std::vector<ProfileLineHeader> & profile_line_headers,const ProfileLoadFilterFn & filter_fn,SafeMap<ProfileIndexType,ProfileIndexType> * dex_profile_index_remap)1414 bool ProfileCompilationInfo::RemapProfileIndex(
1415     const std::vector<ProfileLineHeader>& profile_line_headers,
1416     const ProfileLoadFilterFn& filter_fn,
1417     /*out*/SafeMap<ProfileIndexType, ProfileIndexType>* dex_profile_index_remap) {
1418   // First verify that all checksums match. This will avoid adding garbage to
1419   // the current profile info.
1420   // Note that the number of elements should be very small, so this should not
1421   // be a performance issue.
1422   for (const ProfileLineHeader& other_profile_line_header : profile_line_headers) {
1423     if (!filter_fn(other_profile_line_header.profile_key, other_profile_line_header.checksum)) {
1424       continue;
1425     }
1426     // verify_checksum is false because we want to differentiate between a missing dex data and
1427     // a mismatched checksum.
1428     const DexFileData* dex_data = FindDexData(other_profile_line_header.profile_key,
1429                                               /* checksum= */ 0u,
1430                                               /* verify_checksum= */ false);
1431     if ((dex_data != nullptr) && (dex_data->checksum != other_profile_line_header.checksum)) {
1432       LOG(WARNING) << "Checksum mismatch for dex " << other_profile_line_header.profile_key;
1433       return false;
1434     }
1435   }
1436   // All checksums match. Import the data.
1437   uint32_t num_dex_files = static_cast<uint32_t>(profile_line_headers.size());
1438   for (uint32_t i = 0; i < num_dex_files; i++) {
1439     if (!filter_fn(profile_line_headers[i].profile_key, profile_line_headers[i].checksum)) {
1440       continue;
1441     }
1442     const DexFileData* dex_data = GetOrAddDexFileData(profile_line_headers[i].profile_key,
1443                                                       profile_line_headers[i].checksum,
1444                                                       profile_line_headers[i].num_method_ids);
1445     if (dex_data == nullptr) {
1446       return false;  // Could happen if we exceed the number of allowed dex files.
1447     }
1448     dex_profile_index_remap->Put(i, dex_data->profile_index);
1449   }
1450   return true;
1451 }
1452 
DeflateBuffer(const uint8_t * in_buffer,uint32_t in_size,uint32_t * compressed_data_size)1453 std::unique_ptr<uint8_t[]> ProfileCompilationInfo::DeflateBuffer(const uint8_t* in_buffer,
1454                                                                  uint32_t in_size,
1455                                                                  uint32_t* compressed_data_size) {
1456   z_stream strm;
1457   strm.zalloc = Z_NULL;
1458   strm.zfree = Z_NULL;
1459   strm.opaque = Z_NULL;
1460   int ret = deflateInit(&strm, 1);
1461   if (ret != Z_OK) {
1462     return nullptr;
1463   }
1464 
1465   uint32_t out_size = deflateBound(&strm, in_size);
1466 
1467   std::unique_ptr<uint8_t[]> compressed_buffer(new uint8_t[out_size]);
1468   strm.avail_in = in_size;
1469   strm.next_in = const_cast<uint8_t*>(in_buffer);
1470   strm.avail_out = out_size;
1471   strm.next_out = &compressed_buffer[0];
1472   ret = deflate(&strm, Z_FINISH);
1473   if (ret == Z_STREAM_ERROR) {
1474     return nullptr;
1475   }
1476   *compressed_data_size = out_size - strm.avail_out;
1477   deflateEnd(&strm);
1478   return compressed_buffer;
1479 }
1480 
InflateBuffer(const uint8_t * in_buffer,uint32_t in_size,uint32_t expected_uncompressed_data_size,uint8_t * out_buffer)1481 int ProfileCompilationInfo::InflateBuffer(const uint8_t* in_buffer,
1482                                           uint32_t in_size,
1483                                           uint32_t expected_uncompressed_data_size,
1484                                           uint8_t* out_buffer) {
1485   z_stream strm;
1486 
1487   /* allocate inflate state */
1488   strm.zalloc = Z_NULL;
1489   strm.zfree = Z_NULL;
1490   strm.opaque = Z_NULL;
1491   strm.avail_in = in_size;
1492   strm.next_in = const_cast<uint8_t*>(in_buffer);
1493   strm.avail_out = expected_uncompressed_data_size;
1494   strm.next_out = out_buffer;
1495 
1496   int ret;
1497   inflateInit(&strm);
1498   ret = inflate(&strm, Z_NO_FLUSH);
1499 
1500   if (strm.avail_in != 0 || strm.avail_out != 0) {
1501     return Z_DATA_ERROR;
1502   }
1503   inflateEnd(&strm);
1504   return ret;
1505 }
1506 
MergeWith(const ProfileCompilationInfo & other,bool merge_classes)1507 bool ProfileCompilationInfo::MergeWith(const ProfileCompilationInfo& other,
1508                                        bool merge_classes) {
1509   if (!SameVersion(other)) {
1510     LOG(WARNING) << "Cannot merge different profile versions";
1511     return false;
1512   }
1513 
1514   // First verify that all checksums match. This will avoid adding garbage to
1515   // the current profile info.
1516   // Note that the number of elements should be very small, so this should not
1517   // be a performance issue.
1518   for (const DexFileData* other_dex_data : other.info_) {
1519     // verify_checksum is false because we want to differentiate between a missing dex data and
1520     // a mismatched checksum.
1521     const DexFileData* dex_data = FindDexData(other_dex_data->profile_key,
1522                                               /* checksum= */ 0u,
1523                                               /* verify_checksum= */ false);
1524     if ((dex_data != nullptr) && (dex_data->checksum != other_dex_data->checksum)) {
1525       LOG(WARNING) << "Checksum mismatch for dex " << other_dex_data->profile_key;
1526       return false;
1527     }
1528   }
1529   // All checksums match. Import the data.
1530 
1531   // The other profile might have a different indexing of dex files.
1532   // That is because each dex files gets a 'dex_profile_index' on a first come first served basis.
1533   // That means that the order in with the methods are added to the profile matters for the
1534   // actual indices.
1535   // The reason we cannot rely on the actual multidex index is that a single profile may store
1536   // data from multiple splits. This means that a profile may contain a classes2.dex from split-A
1537   // and one from split-B.
1538 
1539   // First, build a mapping from other_dex_profile_index to this_dex_profile_index.
1540   // This will make sure that the ClassReferences  will point to the correct dex file.
1541   SafeMap<ProfileIndexType, ProfileIndexType> dex_profile_index_remap;
1542   for (const DexFileData* other_dex_data : other.info_) {
1543     const DexFileData* dex_data = GetOrAddDexFileData(other_dex_data->profile_key,
1544                                                       other_dex_data->checksum,
1545                                                       other_dex_data->num_method_ids);
1546     if (dex_data == nullptr) {
1547       return false;  // Could happen if we exceed the number of allowed dex files.
1548     }
1549     dex_profile_index_remap.Put(other_dex_data->profile_index, dex_data->profile_index);
1550   }
1551 
1552   // Merge the actual profile data.
1553   for (const DexFileData* other_dex_data : other.info_) {
1554     DexFileData* dex_data = const_cast<DexFileData*>(FindDexData(other_dex_data->profile_key,
1555                                                                  other_dex_data->checksum));
1556     DCHECK(dex_data != nullptr);
1557 
1558     // Merge the classes.
1559     if (merge_classes) {
1560       dex_data->class_set.insert(other_dex_data->class_set.begin(),
1561                                  other_dex_data->class_set.end());
1562     }
1563 
1564     // Merge the methods and the inline caches.
1565     for (const auto& other_method_it : other_dex_data->method_map) {
1566       uint16_t other_method_index = other_method_it.first;
1567       InlineCacheMap* inline_cache = dex_data->FindOrAddHotMethod(other_method_index);
1568       if (inline_cache == nullptr) {
1569         return false;
1570       }
1571       const auto& other_inline_cache = other_method_it.second;
1572       for (const auto& other_ic_it : other_inline_cache) {
1573         uint16_t other_dex_pc = other_ic_it.first;
1574         const ClassSet& other_class_set = other_ic_it.second.classes;
1575         DexPcData* dex_pc_data = FindOrAddDexPc(inline_cache, other_dex_pc);
1576         if (other_ic_it.second.is_missing_types) {
1577           dex_pc_data->SetIsMissingTypes();
1578         } else if (other_ic_it.second.is_megamorphic) {
1579           dex_pc_data->SetIsMegamorphic();
1580         } else {
1581           for (const auto& class_it : other_class_set) {
1582             dex_pc_data->AddClass(dex_profile_index_remap.Get(
1583                 class_it.dex_profile_index), class_it.type_index);
1584           }
1585         }
1586       }
1587     }
1588 
1589     // Merge the method bitmaps.
1590     dex_data->MergeBitmap(*other_dex_data);
1591   }
1592 
1593   return true;
1594 }
1595 
GetMethodHotness(const MethodReference & method_ref,const ProfileSampleAnnotation & annotation) const1596 ProfileCompilationInfo::MethodHotness ProfileCompilationInfo::GetMethodHotness(
1597     const MethodReference& method_ref,
1598     const ProfileSampleAnnotation& annotation) const {
1599   const DexFileData* dex_data = FindDexDataUsingAnnotations(method_ref.dex_file, annotation);
1600   return dex_data != nullptr
1601       ? dex_data->GetHotnessInfo(method_ref.index)
1602       : MethodHotness();
1603 }
1604 
1605 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo>
GetHotMethodInfo(const MethodReference & method_ref,const ProfileSampleAnnotation & annotation) const1606 ProfileCompilationInfo::GetHotMethodInfo(const MethodReference& method_ref,
1607                                          const ProfileSampleAnnotation& annotation) const {
1608   MethodHotness hotness(GetMethodHotness(method_ref, annotation));
1609   if (!hotness.IsHot()) {
1610     return nullptr;
1611   }
1612   const InlineCacheMap* inline_caches = hotness.GetInlineCacheMap();
1613   DCHECK(inline_caches != nullptr);
1614   std::unique_ptr<OfflineProfileMethodInfo> pmi(new OfflineProfileMethodInfo(inline_caches));
1615 
1616   pmi->dex_references.resize(info_.size());
1617   for (const DexFileData* dex_data : info_) {
1618     pmi->dex_references[dex_data->profile_index].profile_key = dex_data->profile_key;
1619     pmi->dex_references[dex_data->profile_index].dex_checksum = dex_data->checksum;
1620     pmi->dex_references[dex_data->profile_index].num_method_ids = dex_data->num_method_ids;
1621   }
1622 
1623   return pmi;
1624 }
1625 
1626 
ContainsClass(const DexFile & dex_file,dex::TypeIndex type_idx,const ProfileSampleAnnotation & annotation) const1627 bool ProfileCompilationInfo::ContainsClass(const DexFile& dex_file,
1628                                            dex::TypeIndex type_idx,
1629                                            const ProfileSampleAnnotation& annotation) const {
1630   const DexFileData* dex_data = FindDexDataUsingAnnotations(&dex_file, annotation);
1631   return (dex_data != nullptr) && dex_data->ContainsClass(type_idx);
1632 }
1633 
GetNumberOfMethods() const1634 uint32_t ProfileCompilationInfo::GetNumberOfMethods() const {
1635   uint32_t total = 0;
1636   for (const DexFileData* dex_data : info_) {
1637     total += dex_data->method_map.size();
1638   }
1639   return total;
1640 }
1641 
GetNumberOfResolvedClasses() const1642 uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const {
1643   uint32_t total = 0;
1644   for (const DexFileData* dex_data : info_) {
1645     total += dex_data->class_set.size();
1646   }
1647   return total;
1648 }
1649 
DumpInfo(const std::vector<const DexFile * > & dex_files,bool print_full_dex_location) const1650 std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>& dex_files,
1651                                              bool print_full_dex_location) const {
1652   std::ostringstream os;
1653 
1654   os << "ProfileInfo [";
1655 
1656   for (size_t k = 0; k <  kProfileVersionSize - 1; k++) {
1657     // Iterate to 'kProfileVersionSize - 1' because the version_ ends with '\0'
1658     // which we don't want to print.
1659     os << static_cast<char>(version_[k]);
1660   }
1661   os << "]\n";
1662 
1663   if (info_.empty()) {
1664     os << "-empty-";
1665     return os.str();
1666   }
1667 
1668   const std::string kFirstDexFileKeySubstitute = "!classes.dex";
1669 
1670   for (const DexFileData* dex_data : info_) {
1671     os << "\n";
1672     if (print_full_dex_location) {
1673       os << dex_data->profile_key;
1674     } else {
1675       // Replace the (empty) multidex suffix of the first key with a substitute for easier reading.
1676       std::string multidex_suffix = DexFileLoader::GetMultiDexSuffix(
1677           GetBaseKeyFromAugmentedKey(dex_data->profile_key));
1678       os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix);
1679     }
1680     os << " [index=" << static_cast<uint32_t>(dex_data->profile_index) << "]";
1681     os << " [checksum=" << std::hex << dex_data->checksum << "]" << std::dec;
1682     const DexFile* dex_file = nullptr;
1683     for (const DexFile* current : dex_files) {
1684       if (GetBaseKeyFromAugmentedKey(dex_data->profile_key) == current->GetLocation() &&
1685           dex_data->checksum == current->GetLocationChecksum()) {
1686         dex_file = current;
1687       }
1688     }
1689     os << "\n\thot methods: ";
1690     for (const auto& method_it : dex_data->method_map) {
1691       if (dex_file != nullptr) {
1692         os << "\n\t\t" << dex_file->PrettyMethod(method_it.first, true);
1693       } else {
1694         os << method_it.first;
1695       }
1696 
1697       os << "[";
1698       for (const auto& inline_cache_it : method_it.second) {
1699         os << "{" << std::hex << inline_cache_it.first << std::dec << ":";
1700         if (inline_cache_it.second.is_missing_types) {
1701           os << "MT";
1702         } else if (inline_cache_it.second.is_megamorphic) {
1703           os << "MM";
1704         } else {
1705           for (const ClassReference& class_ref : inline_cache_it.second.classes) {
1706             os << "(" << static_cast<uint32_t>(class_ref.dex_profile_index)
1707                << "," << class_ref.type_index.index_ << ")";
1708           }
1709         }
1710         os << "}";
1711       }
1712       os << "], ";
1713     }
1714     bool startup = true;
1715     while (true) {
1716       os << "\n\t" << (startup ? "startup methods: " : "post startup methods: ");
1717       for (uint32_t method_idx = 0; method_idx < dex_data->num_method_ids; ++method_idx) {
1718         MethodHotness hotness_info(dex_data->GetHotnessInfo(method_idx));
1719         if (startup ? hotness_info.IsStartup() : hotness_info.IsPostStartup()) {
1720           if (dex_file != nullptr) {
1721             os << "\n\t\t" << dex_file->PrettyMethod(method_idx, true);
1722           } else {
1723             os << method_idx << ", ";
1724           }
1725         }
1726       }
1727       if (startup == false) {
1728         break;
1729       }
1730       startup = false;
1731     }
1732     os << "\n\tclasses: ";
1733     for (const auto class_it : dex_data->class_set) {
1734       if (dex_file != nullptr) {
1735         os << "\n\t\t" << dex_file->PrettyType(class_it);
1736       } else {
1737         os << class_it.index_ << ",";
1738       }
1739     }
1740   }
1741   return os.str();
1742 }
1743 
GetClassesAndMethods(const DexFile & dex_file,std::set<dex::TypeIndex> * class_set,std::set<uint16_t> * hot_method_set,std::set<uint16_t> * startup_method_set,std::set<uint16_t> * post_startup_method_method_set,const ProfileSampleAnnotation & annotation) const1744 bool ProfileCompilationInfo::GetClassesAndMethods(
1745     const DexFile& dex_file,
1746     /*out*/std::set<dex::TypeIndex>* class_set,
1747     /*out*/std::set<uint16_t>* hot_method_set,
1748     /*out*/std::set<uint16_t>* startup_method_set,
1749     /*out*/std::set<uint16_t>* post_startup_method_method_set,
1750     const ProfileSampleAnnotation& annotation) const {
1751   std::set<std::string> ret;
1752   const DexFileData* dex_data = FindDexDataUsingAnnotations(&dex_file, annotation);
1753   if (dex_data == nullptr) {
1754     return false;
1755   }
1756   for (const auto& it : dex_data->method_map) {
1757     hot_method_set->insert(it.first);
1758   }
1759   for (uint32_t method_idx = 0; method_idx < dex_data->num_method_ids; ++method_idx) {
1760     MethodHotness hotness = dex_data->GetHotnessInfo(method_idx);
1761     if (hotness.IsStartup()) {
1762       startup_method_set->insert(method_idx);
1763     }
1764     if (hotness.IsPostStartup()) {
1765       post_startup_method_method_set->insert(method_idx);
1766     }
1767   }
1768   for (const dex::TypeIndex& type_index : dex_data->class_set) {
1769     class_set->insert(type_index);
1770   }
1771   return true;
1772 }
1773 
SameVersion(const ProfileCompilationInfo & other) const1774 bool ProfileCompilationInfo::SameVersion(const ProfileCompilationInfo& other) const {
1775   return memcmp(version_, other.version_, kProfileVersionSize) == 0;
1776 }
1777 
Equals(const ProfileCompilationInfo & other)1778 bool ProfileCompilationInfo::Equals(const ProfileCompilationInfo& other) {
1779   // No need to compare profile_key_map_. That's only a cache for fast search.
1780   // All the information is already in the info_ vector.
1781   if (!SameVersion(other)) {
1782     return false;
1783   }
1784   if (info_.size() != other.info_.size()) {
1785     return false;
1786   }
1787   for (size_t i = 0; i < info_.size(); i++) {
1788     const DexFileData& dex_data = *info_[i];
1789     const DexFileData& other_dex_data = *other.info_[i];
1790     if (!(dex_data == other_dex_data)) {
1791       return false;
1792     }
1793   }
1794 
1795   return true;
1796 }
1797 
1798 // Naive implementation to generate a random profile file suitable for testing.
GenerateTestProfile(int fd,uint16_t number_of_dex_files,uint16_t method_percentage,uint16_t class_percentage,uint32_t random_seed)1799 bool ProfileCompilationInfo::GenerateTestProfile(int fd,
1800                                                  uint16_t number_of_dex_files,
1801                                                  uint16_t method_percentage,
1802                                                  uint16_t class_percentage,
1803                                                  uint32_t random_seed) {
1804   const std::string base_dex_location = "base.apk";
1805   ProfileCompilationInfo info;
1806   // The limits are defined by the dex specification.
1807   const uint16_t max_method = std::numeric_limits<uint16_t>::max();
1808   const uint16_t max_classes = std::numeric_limits<uint16_t>::max();
1809   uint16_t number_of_methods = max_method * method_percentage / 100;
1810   uint16_t number_of_classes = max_classes * class_percentage / 100;
1811 
1812   std::srand(random_seed);
1813 
1814   // Make sure we generate more samples with a low index value.
1815   // This makes it more likely to hit valid method/class indices in small apps.
1816   const uint16_t kFavorFirstN = 10000;
1817   const uint16_t kFavorSplit = 2;
1818 
1819   for (uint16_t i = 0; i < number_of_dex_files; i++) {
1820     std::string dex_location = DexFileLoader::GetMultiDexLocation(i, base_dex_location.c_str());
1821     std::string profile_key = info.GetProfileDexFileBaseKey(dex_location);
1822 
1823     DexFileData* const data = info.GetOrAddDexFileData(profile_key, /*checksum=*/ 0, max_method);
1824     for (uint16_t m = 0; m < number_of_methods; m++) {
1825       uint16_t method_idx = rand() % max_method;
1826       if (m < (number_of_methods / kFavorSplit)) {
1827         method_idx %= kFavorFirstN;
1828       }
1829       // Alternate between startup and post startup.
1830       uint32_t flags = MethodHotness::kFlagHot;
1831       flags |= ((m & 1) != 0) ? MethodHotness::kFlagPostStartup : MethodHotness::kFlagStartup;
1832       data->AddMethod(static_cast<MethodHotness::Flag>(flags), method_idx);
1833     }
1834 
1835     for (uint16_t c = 0; c < number_of_classes; c++) {
1836       uint16_t type_idx = rand() % max_classes;
1837       if (c < (number_of_classes / kFavorSplit)) {
1838         type_idx %= kFavorFirstN;
1839       }
1840       data->class_set.insert(dex::TypeIndex(type_idx));
1841     }
1842   }
1843   return info.Save(fd);
1844 }
1845 
1846 // Naive implementation to generate a random profile file suitable for testing.
1847 // Description of random selection:
1848 // * Select a random starting point S.
1849 // * For every index i, add (S+i) % (N - total number of methods/classes) to profile with the
1850 //   probably of 1/(N - i - number of methods/classes needed to add in profile).
GenerateTestProfile(int fd,std::vector<std::unique_ptr<const DexFile>> & dex_files,uint16_t method_percentage,uint16_t class_percentage,uint32_t random_seed)1851 bool ProfileCompilationInfo::GenerateTestProfile(
1852     int fd,
1853     std::vector<std::unique_ptr<const DexFile>>& dex_files,
1854     uint16_t method_percentage,
1855     uint16_t class_percentage,
1856     uint32_t random_seed) {
1857   ProfileCompilationInfo info;
1858   std::default_random_engine rng(random_seed);
1859   auto create_shuffled_range = [&rng](uint32_t take, uint32_t out_of) {
1860     CHECK_LE(take, out_of);
1861     std::vector<uint32_t> vec(out_of);
1862     std::iota(vec.begin(), vec.end(), 0u);
1863     std::shuffle(vec.begin(), vec.end(), rng);
1864     vec.erase(vec.begin() + take, vec.end());
1865     std::sort(vec.begin(), vec.end());
1866     return vec;
1867   };
1868   for (std::unique_ptr<const DexFile>& dex_file : dex_files) {
1869     const std::string& profile_key = dex_file->GetLocation();
1870     uint32_t checksum = dex_file->GetLocationChecksum();
1871 
1872     uint32_t number_of_classes = dex_file->NumClassDefs();
1873     uint32_t classes_required_in_profile = (number_of_classes * class_percentage) / 100;
1874 
1875     DexFileData* const data = info.GetOrAddDexFileData(
1876           profile_key, checksum, dex_file->NumMethodIds());
1877     for (uint32_t class_index : create_shuffled_range(classes_required_in_profile,
1878                                                       number_of_classes)) {
1879       data->class_set.insert(dex_file->GetClassDef(class_index).class_idx_);
1880     }
1881 
1882     uint32_t number_of_methods = dex_file->NumMethodIds();
1883     uint32_t methods_required_in_profile = (number_of_methods * method_percentage) / 100;
1884     for (uint32_t method_index : create_shuffled_range(methods_required_in_profile,
1885                                                        number_of_methods)) {
1886       // Alternate between startup and post startup.
1887       uint32_t flags = MethodHotness::kFlagHot;
1888       flags |= ((method_index & 1) != 0)
1889                    ? MethodHotness::kFlagPostStartup
1890                    : MethodHotness::kFlagStartup;
1891       data->AddMethod(static_cast<MethodHotness::Flag>(flags), method_index);
1892     }
1893   }
1894   return info.Save(fd);
1895 }
1896 
operator ==(const OfflineProfileMethodInfo & other) const1897 bool ProfileCompilationInfo::OfflineProfileMethodInfo::operator==(
1898       const OfflineProfileMethodInfo& other) const {
1899   if (inline_caches->size() != other.inline_caches->size()) {
1900     return false;
1901   }
1902 
1903   // We can't use a simple equality test because we need to match the dex files
1904   // of the inline caches which might have different profile indexes.
1905   for (const auto& inline_cache_it : *inline_caches) {
1906     uint16_t dex_pc = inline_cache_it.first;
1907     const DexPcData dex_pc_data = inline_cache_it.second;
1908     const auto& other_it = other.inline_caches->find(dex_pc);
1909     if (other_it == other.inline_caches->end()) {
1910       return false;
1911     }
1912     const DexPcData& other_dex_pc_data = other_it->second;
1913     if (dex_pc_data.is_megamorphic != other_dex_pc_data.is_megamorphic ||
1914         dex_pc_data.is_missing_types != other_dex_pc_data.is_missing_types) {
1915       return false;
1916     }
1917     for (const ClassReference& class_ref : dex_pc_data.classes) {
1918       bool found = false;
1919       for (const ClassReference& other_class_ref : other_dex_pc_data.classes) {
1920         CHECK_LE(class_ref.dex_profile_index, dex_references.size());
1921         CHECK_LE(other_class_ref.dex_profile_index, other.dex_references.size());
1922         const DexReference& dex_ref = dex_references[class_ref.dex_profile_index];
1923         const DexReference& other_dex_ref = other.dex_references[other_class_ref.dex_profile_index];
1924         if (class_ref.type_index == other_class_ref.type_index &&
1925             dex_ref == other_dex_ref) {
1926           found = true;
1927           break;
1928         }
1929       }
1930       if (!found) {
1931         return false;
1932       }
1933     }
1934   }
1935   return true;
1936 }
1937 
operator ==(const std::vector<ProfileMethodInfo::ProfileInlineCache> & runtime_caches) const1938 bool ProfileCompilationInfo::OfflineProfileMethodInfo::operator==(
1939       const std::vector<ProfileMethodInfo::ProfileInlineCache>& runtime_caches) const {
1940   if (inline_caches->size() != runtime_caches.size()) {
1941     return false;
1942   }
1943 
1944   for (const auto& inline_cache_it : *inline_caches) {
1945     uint16_t dex_pc = inline_cache_it.first;
1946     const DexPcData dex_pc_data = inline_cache_it.second;
1947 
1948     // Find the corresponding inline cahce.
1949     const ProfileMethodInfo::ProfileInlineCache* runtime_cache = nullptr;
1950     for (const ProfileMethodInfo::ProfileInlineCache& pic : runtime_caches) {
1951       if (pic.dex_pc == dex_pc) {
1952         runtime_cache = &pic;
1953         break;
1954       }
1955     }
1956     // If not found, returnb false.
1957     if (runtime_cache == nullptr) {
1958       return false;
1959     }
1960     // Check that the inline cache properties match up.
1961     if (dex_pc_data.is_missing_types) {
1962       if (!runtime_cache->is_missing_types) {
1963         return false;
1964       } else {
1965         // If the inline cache is megamorphic do not check the classes (they don't matter).
1966         continue;
1967       }
1968     }
1969 
1970     if (dex_pc_data.is_megamorphic) {
1971       if (runtime_cache->classes.size() < ProfileCompilationInfo::kIndividualInlineCacheSize) {
1972         return false;
1973       } else {
1974         // If the inline cache is megamorphic do not check the classes (they don't matter).
1975         continue;
1976       }
1977     }
1978 
1979     if (dex_pc_data.classes.size() != runtime_cache->classes.size()) {
1980       return false;
1981     }
1982     // Verify that all classes matches.
1983     for (const ClassReference& class_ref : dex_pc_data.classes) {
1984       bool found = false;
1985       const DexReference& dex_ref = dex_references[class_ref.dex_profile_index];
1986       for (const TypeReference& type_ref : runtime_cache->classes) {
1987         if (class_ref.type_index == type_ref.TypeIndex() &&
1988             dex_ref.MatchesDex(type_ref.dex_file)) {
1989           found = true;
1990           break;
1991         }
1992       }
1993       if (!found) {
1994         return false;
1995       }
1996     }
1997   }
1998   // If we didn't fail until now, then the two inline caches are equal.
1999   return true;
2000 }
2001 
IsEmpty() const2002 bool ProfileCompilationInfo::IsEmpty() const {
2003   DCHECK_EQ(info_.empty(), profile_key_map_.empty());
2004   return info_.empty();
2005 }
2006 
2007 ProfileCompilationInfo::InlineCacheMap*
FindOrAddHotMethod(uint16_t method_index)2008 ProfileCompilationInfo::DexFileData::FindOrAddHotMethod(uint16_t method_index) {
2009   if (method_index >= num_method_ids) {
2010     LOG(ERROR) << "Invalid method index " << method_index << ". num_method_ids=" << num_method_ids;
2011     return nullptr;
2012   }
2013   return &(method_map.FindOrAdd(
2014       method_index,
2015       InlineCacheMap(std::less<uint16_t>(), allocator_->Adapter(kArenaAllocProfile)))->second);
2016 }
2017 
2018 // Mark a method as executed at least once.
AddMethod(MethodHotness::Flag flags,size_t index)2019 bool ProfileCompilationInfo::DexFileData::AddMethod(MethodHotness::Flag flags, size_t index) {
2020   if (index >= num_method_ids) {
2021     LOG(ERROR) << "Invalid method index " << index << ". num_method_ids=" << num_method_ids;
2022     return false;
2023   }
2024 
2025   SetMethodHotness(index, flags);
2026 
2027   if ((flags & MethodHotness::kFlagHot) != 0) {
2028     ProfileCompilationInfo::InlineCacheMap* result = FindOrAddHotMethod(index);
2029     DCHECK(result != nullptr);
2030   }
2031   return true;
2032 }
2033 
SetMethodHotness(size_t index,MethodHotness::Flag flags)2034 void ProfileCompilationInfo::DexFileData::SetMethodHotness(size_t index,
2035                                                            MethodHotness::Flag flags) {
2036   DCHECK_LT(index, num_method_ids);
2037   uint32_t lastFlag = is_for_boot_image
2038       ? MethodHotness::kFlagLastBoot
2039       : MethodHotness::kFlagLastRegular;
2040   for (uint32_t flag = MethodHotness::kFlagFirst; flag <= lastFlag; flag = flag << 1) {
2041     if (flag == MethodHotness::kFlagHot) {
2042       // There's no bit for hotness in the bitmap.
2043       // We store the hotness by recording the method in the method list.
2044       continue;
2045     }
2046     if ((flags & flag) != 0) {
2047       method_bitmap.StoreBit(MethodFlagBitmapIndex(
2048           static_cast<MethodHotness::Flag>(flag), index), /*value=*/ true);
2049     }
2050   }
2051 }
2052 
GetHotnessInfo(uint32_t dex_method_index) const2053 ProfileCompilationInfo::MethodHotness ProfileCompilationInfo::DexFileData::GetHotnessInfo(
2054     uint32_t dex_method_index) const {
2055   MethodHotness ret;
2056   uint32_t lastFlag = is_for_boot_image
2057       ? MethodHotness::kFlagLastBoot
2058       : MethodHotness::kFlagLastRegular;
2059   for (uint32_t flag = MethodHotness::kFlagFirst; flag <= lastFlag; flag = flag << 1) {
2060     if (flag == MethodHotness::kFlagHot) {
2061       continue;
2062     }
2063     if (method_bitmap.LoadBit(MethodFlagBitmapIndex(
2064           static_cast<MethodHotness::Flag>(flag), dex_method_index))) {
2065       ret.AddFlag(static_cast<MethodHotness::Flag>(flag));
2066     }
2067   }
2068   auto it = method_map.find(dex_method_index);
2069   if (it != method_map.end()) {
2070     ret.SetInlineCacheMap(&it->second);
2071     ret.AddFlag(MethodHotness::kFlagHot);
2072   }
2073   return ret;
2074 }
2075 
2076 // To simplify the implementation we use the MethodHotness flag values as indexes into the internal
2077 // bitmap representation. As such, they should never change unless the profile version is updated
2078 // and the implementation changed accordingly.
2079 static_assert(ProfileCompilationInfo::MethodHotness::kFlagFirst == 1 << 0);
2080 static_assert(ProfileCompilationInfo::MethodHotness::kFlagHot == 1 << 0);
2081 static_assert(ProfileCompilationInfo::MethodHotness::kFlagStartup == 1 << 1);
2082 static_assert(ProfileCompilationInfo::MethodHotness::kFlagPostStartup == 1 << 2);
2083 static_assert(ProfileCompilationInfo::MethodHotness::kFlagLastRegular == 1 << 2);
2084 static_assert(ProfileCompilationInfo::MethodHotness::kFlag32bit == 1 << 3);
2085 static_assert(ProfileCompilationInfo::MethodHotness::kFlag64bit == 1 << 4);
2086 static_assert(ProfileCompilationInfo::MethodHotness::kFlagSensitiveThread == 1 << 5);
2087 static_assert(ProfileCompilationInfo::MethodHotness::kFlagAmStartup == 1 << 6);
2088 static_assert(ProfileCompilationInfo::MethodHotness::kFlagAmPostStartup == 1 << 7);
2089 static_assert(ProfileCompilationInfo::MethodHotness::kFlagBoot == 1 << 8);
2090 static_assert(ProfileCompilationInfo::MethodHotness::kFlagPostBoot == 1 << 9);
2091 static_assert(ProfileCompilationInfo::MethodHotness::kFlagStartupBin == 1 << 10);
2092 static_assert(ProfileCompilationInfo::MethodHotness::kFlagStartupMaxBin == 1 << 15);
2093 static_assert(ProfileCompilationInfo::MethodHotness::kFlagLastBoot == 1 << 15);
2094 
MethodFlagBitmapIndex(MethodHotness::Flag flag,size_t method_index) const2095 size_t ProfileCompilationInfo::DexFileData::MethodFlagBitmapIndex(
2096       MethodHotness::Flag flag, size_t method_index) const {
2097   DCHECK_LT(method_index, num_method_ids);
2098   // The format is [startup bitmap][post startup bitmap][AmStartup][...]
2099   // This compresses better than ([startup bit][post startup bit])*
2100   return method_index + FlagBitmapIndex(flag) * num_method_ids;
2101 }
2102 
FlagBitmapIndex(MethodHotness::Flag flag)2103 size_t ProfileCompilationInfo::DexFileData::FlagBitmapIndex(MethodHotness::Flag flag) {
2104   DCHECK(flag != MethodHotness::kFlagHot);
2105   DCHECK(IsPowerOfTwo(static_cast<uint32_t>(flag)));
2106   // We arrange the method flags in order, starting with the startup flag.
2107   // The kFlagHot is not encoded in the bitmap and thus not expected as an
2108   // argument here. Since all the other flags start at 1 we have to subtract
2109   // one for the power of 2.
2110   return WhichPowerOf2(static_cast<uint32_t>(flag)) - 1;
2111 }
2112 
2113 ProfileCompilationInfo::DexPcData*
FindOrAddDexPc(InlineCacheMap * inline_cache,uint32_t dex_pc)2114 ProfileCompilationInfo::FindOrAddDexPc(InlineCacheMap* inline_cache, uint32_t dex_pc) {
2115   return &(inline_cache->FindOrAdd(dex_pc, DexPcData(&allocator_))->second);
2116 }
2117 
GetClassDescriptors(const std::vector<const DexFile * > & dex_files,const ProfileSampleAnnotation & annotation)2118 HashSet<std::string> ProfileCompilationInfo::GetClassDescriptors(
2119     const std::vector<const DexFile*>& dex_files,
2120     const ProfileSampleAnnotation& annotation) {
2121   HashSet<std::string> ret;
2122   for (const DexFile* dex_file : dex_files) {
2123     const DexFileData* data = FindDexDataUsingAnnotations(dex_file, annotation);
2124     if (data != nullptr) {
2125       for (dex::TypeIndex type_idx : data->class_set) {
2126         if (!dex_file->IsTypeIndexValid(type_idx)) {
2127           // Something went bad. The profile is probably corrupted. Abort and return an emtpy set.
2128           LOG(WARNING) << "Corrupted profile: invalid type index "
2129               << type_idx.index_ << " in dex " << dex_file->GetLocation();
2130           return HashSet<std::string>();
2131         }
2132         const dex::TypeId& type_id = dex_file->GetTypeId(type_idx);
2133         ret.insert(dex_file->GetTypeDescriptor(type_id));
2134       }
2135     } else {
2136       VLOG(compiler) << "Failed to find profile data for " << dex_file->GetLocation();
2137     }
2138   }
2139   return ret;
2140 }
2141 
IsProfileFile(int fd)2142 bool ProfileCompilationInfo::IsProfileFile(int fd) {
2143   // First check if it's an empty file as we allow empty profile files.
2144   // Profiles may be created by ActivityManager or installd before we manage to
2145   // process them in the runtime or profman.
2146   struct stat stat_buffer;
2147   if (fstat(fd, &stat_buffer) != 0) {
2148     return false;
2149   }
2150 
2151   if (stat_buffer.st_size == 0) {
2152     return true;
2153   }
2154 
2155   // The files is not empty. Check if it contains the profile magic.
2156   size_t byte_count = sizeof(kProfileMagic);
2157   uint8_t buffer[sizeof(kProfileMagic)];
2158   if (!android::base::ReadFully(fd, buffer, byte_count)) {
2159     return false;
2160   }
2161 
2162   // Reset the offset to prepare the file for reading.
2163   off_t rc =  TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET));
2164   if (rc == static_cast<off_t>(-1)) {
2165     PLOG(ERROR) << "Failed to reset the offset";
2166     return false;
2167   }
2168 
2169   return memcmp(buffer, kProfileMagic, byte_count) == 0;
2170 }
2171 
UpdateProfileKeys(const std::vector<std::unique_ptr<const DexFile>> & dex_files)2172 bool ProfileCompilationInfo::UpdateProfileKeys(
2173       const std::vector<std::unique_ptr<const DexFile>>& dex_files) {
2174   for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
2175     for (DexFileData* dex_data : info_) {
2176       if (dex_data->checksum == dex_file->GetLocationChecksum()
2177           && dex_data->num_method_ids == dex_file->NumMethodIds()) {
2178         std::string new_profile_key = GetProfileDexFileBaseKey(dex_file->GetLocation());
2179         std::string dex_data_base_key = GetBaseKeyFromAugmentedKey(dex_data->profile_key);
2180         if (dex_data_base_key != new_profile_key) {
2181           if (profile_key_map_.find(new_profile_key) != profile_key_map_.end()) {
2182             // We can't update the key if the new key belongs to a different dex file.
2183             LOG(ERROR) << "Cannot update profile key to " << new_profile_key
2184                 << " because the new key belongs to another dex file.";
2185             return false;
2186           }
2187           profile_key_map_.erase(dex_data->profile_key);
2188           // Retain the annotation (if any) during the renaming by re-attaching the info
2189           // form the old key.
2190           profile_key_map_.Put(MigrateAnnotationInfo(new_profile_key, dex_data->profile_key),
2191                                dex_data->profile_index);
2192           dex_data->profile_key = new_profile_key;
2193         }
2194       }
2195     }
2196   }
2197   return true;
2198 }
2199 
ProfileFilterFnAcceptAll(const std::string & dex_location ATTRIBUTE_UNUSED,uint32_t checksum ATTRIBUTE_UNUSED)2200 bool ProfileCompilationInfo::ProfileFilterFnAcceptAll(
2201     const std::string& dex_location ATTRIBUTE_UNUSED,
2202     uint32_t checksum ATTRIBUTE_UNUSED) {
2203   return true;
2204 }
2205 
ClearData()2206 void ProfileCompilationInfo::ClearData() {
2207   for (DexFileData* data : info_) {
2208     delete data;
2209   }
2210   info_.clear();
2211   profile_key_map_.clear();
2212 }
2213 
ClearDataAndAdjustVersion(bool for_boot_image)2214 void ProfileCompilationInfo::ClearDataAndAdjustVersion(bool for_boot_image) {
2215   ClearData();
2216   memcpy(version_,
2217          for_boot_image ? kProfileVersionForBootImage : kProfileVersion,
2218          kProfileVersionSize);
2219 }
2220 
IsForBootImage() const2221 bool ProfileCompilationInfo::IsForBootImage() const {
2222   return memcmp(version_, kProfileVersionForBootImage, sizeof(kProfileVersionForBootImage)) == 0;
2223 }
2224 
GetVersion() const2225 const uint8_t* ProfileCompilationInfo::GetVersion() const {
2226   return version_;
2227 }
2228 
ContainsClass(const dex::TypeIndex type_index) const2229 bool ProfileCompilationInfo::DexFileData::ContainsClass(const dex::TypeIndex type_index) const {
2230   return class_set.find(type_index) != class_set.end();
2231 }
2232 
GetSizeWarningThresholdBytes() const2233 size_t ProfileCompilationInfo::GetSizeWarningThresholdBytes() const {
2234   return IsForBootImage() ?  kSizeWarningThresholdBootBytes : kSizeWarningThresholdBytes;
2235 }
2236 
GetSizeErrorThresholdBytes() const2237 size_t ProfileCompilationInfo::GetSizeErrorThresholdBytes() const {
2238   return IsForBootImage() ?  kSizeErrorThresholdBootBytes : kSizeErrorThresholdBytes;
2239 }
2240 
operator <<(std::ostream & stream,const ProfileCompilationInfo::DexReference & dex_ref)2241 std::ostream& operator<<(std::ostream& stream,
2242                          const ProfileCompilationInfo::DexReference& dex_ref) {
2243   stream << "[profile_key=" << dex_ref.profile_key
2244          << ",dex_checksum=" << std::hex << dex_ref.dex_checksum << std::dec
2245          << ",num_method_ids=" << dex_ref.num_method_ids
2246          << "]";
2247   return stream;
2248 }
2249 
operator ==(const ProfileSampleAnnotation & other) const2250 bool ProfileCompilationInfo::ProfileSampleAnnotation::operator==(
2251       const ProfileSampleAnnotation& other) const {
2252   return origin_package_name_ == other.origin_package_name_;
2253 }
2254 
WriteProfileIndex(std::vector<uint8_t> * buffer,ProfileIndexType value) const2255 void ProfileCompilationInfo::WriteProfileIndex(
2256     std::vector<uint8_t>* buffer, ProfileIndexType value) const {
2257   if (IsForBootImage()) {
2258     AddUintToBuffer(buffer, value);
2259   } else {
2260     AddUintToBuffer(buffer, static_cast<ProfileIndexTypeRegular>(value));
2261   }
2262 }
2263 
ReadProfileIndex(SafeBuffer & safe_buffer,ProfileIndexType * value) const2264 bool ProfileCompilationInfo::ReadProfileIndex(
2265     SafeBuffer& safe_buffer, ProfileIndexType* value) const {
2266   if (IsForBootImage()) {
2267     return safe_buffer.ReadUintAndAdvance<ProfileIndexType>(value);
2268   } else {
2269     ProfileIndexTypeRegular out;
2270     bool result = safe_buffer.ReadUintAndAdvance<ProfileIndexTypeRegular>(&out);
2271     *value = out;
2272     return result;
2273   }
2274 }
2275 
MaxProfileIndex() const2276 ProfileCompilationInfo::ProfileIndexType ProfileCompilationInfo::MaxProfileIndex() const {
2277   return IsForBootImage()
2278       ? std::numeric_limits<ProfileIndexType>::max()
2279       : std::numeric_limits<ProfileIndexTypeRegular>::max();
2280 }
2281 
SizeOfProfileIndexType() const2282 uint32_t ProfileCompilationInfo::SizeOfProfileIndexType() const {
2283   return IsForBootImage()
2284     ? sizeof(ProfileIndexType)
2285     : sizeof(ProfileIndexTypeRegular);
2286 }
2287 
FlattenProfileData()2288 FlattenProfileData::FlattenProfileData() :
2289     max_aggregation_for_methods_(0),
2290     max_aggregation_for_classes_(0) {
2291 }
2292 
ItemMetadata()2293 FlattenProfileData::ItemMetadata::ItemMetadata() :
2294     flags_(0) {
2295 }
2296 
ItemMetadata(const ItemMetadata & other)2297 FlattenProfileData::ItemMetadata::ItemMetadata(const ItemMetadata& other) :
2298     flags_(other.flags_),
2299     annotations_(other.annotations_) {
2300 }
2301 
ExtractProfileData(const std::vector<std::unique_ptr<const DexFile>> & dex_files) const2302 std::unique_ptr<FlattenProfileData> ProfileCompilationInfo::ExtractProfileData(
2303     const std::vector<std::unique_ptr<const DexFile>>& dex_files) const {
2304 
2305   std::unique_ptr<FlattenProfileData> result(new FlattenProfileData());
2306 
2307   auto create_metadata_fn = []() { return FlattenProfileData::ItemMetadata(); };
2308 
2309   // Iterate through all the dex files, find the methods/classes associated with each of them,
2310   // and add them to the flatten result.
2311   for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
2312     // Find all the dex data for the given dex file.
2313     // We may have multiple dex data if the methods or classes were added using
2314     // different annotations.
2315     std::vector<const DexFileData*> all_dex_data;
2316     FindAllDexData(dex_file.get(), &all_dex_data);
2317     for (const DexFileData* dex_data : all_dex_data) {
2318       // Extract the annotation from the key as we want to store it in the flatten result.
2319       ProfileSampleAnnotation annotation = GetAnnotationFromKey(dex_data->profile_key);
2320 
2321       // Check which methods from the current dex files are in the profile.
2322       for (uint32_t method_idx = 0; method_idx < dex_data->num_method_ids; ++method_idx) {
2323         MethodHotness hotness = dex_data->GetHotnessInfo(method_idx);
2324         if (!hotness.IsInProfile()) {
2325           // Not in the profile, continue.
2326           continue;
2327         }
2328         // The method is in the profile, create metadata item for it and added to the result.
2329         MethodReference ref(dex_file.get(), method_idx);
2330         FlattenProfileData::ItemMetadata& metadata =
2331             result->method_metadata_.GetOrCreate(ref, create_metadata_fn);
2332         metadata.flags_ |= hotness.flags_;
2333         metadata.annotations_.push_back(annotation);
2334         // Update the max aggregation counter for methods.
2335         // This is essentially a cache, to avoid traversing all the methods just to find out
2336         // this value.
2337         result->max_aggregation_for_methods_ = std::max(
2338             result->max_aggregation_for_methods_,
2339             static_cast<uint32_t>(metadata.annotations_.size()));
2340       }
2341 
2342       // Check which classes from the current dex files are in the profile.
2343       for (const dex::TypeIndex& type_index : dex_data->class_set) {
2344         TypeReference ref(dex_file.get(), type_index);
2345         FlattenProfileData::ItemMetadata& metadata =
2346             result->class_metadata_.GetOrCreate(ref, create_metadata_fn);
2347         metadata.annotations_.push_back(annotation);
2348         // Update the max aggregation counter for classes.
2349         result->max_aggregation_for_classes_ = std::max(
2350             result->max_aggregation_for_classes_,
2351             static_cast<uint32_t>(metadata.annotations_.size()));
2352       }
2353     }
2354   }
2355 
2356   return result;
2357 }
2358 
MergeData(const FlattenProfileData & other)2359 void FlattenProfileData::MergeData(const FlattenProfileData& other) {
2360   auto create_metadata_fn = []() { return FlattenProfileData::ItemMetadata(); };
2361   for (const auto& it : other.method_metadata_) {
2362     const MethodReference& otherRef = it.first;
2363     const FlattenProfileData::ItemMetadata otherData = it.second;
2364     const std::list<ProfileCompilationInfo::ProfileSampleAnnotation>& other_annotations =
2365         otherData.GetAnnotations();
2366 
2367     FlattenProfileData::ItemMetadata& metadata =
2368         method_metadata_.GetOrCreate(otherRef, create_metadata_fn);
2369     metadata.flags_ |= otherData.GetFlags();
2370     metadata.annotations_.insert(
2371         metadata.annotations_.end(), other_annotations.begin(), other_annotations.end());
2372 
2373     max_aggregation_for_methods_ = std::max(
2374           max_aggregation_for_methods_,
2375           static_cast<uint32_t>(metadata.annotations_.size()));
2376   }
2377   for (const auto& it : other.class_metadata_) {
2378     const TypeReference& otherRef = it.first;
2379     const FlattenProfileData::ItemMetadata otherData = it.second;
2380     const std::list<ProfileCompilationInfo::ProfileSampleAnnotation>& other_annotations =
2381         otherData.GetAnnotations();
2382 
2383     FlattenProfileData::ItemMetadata& metadata =
2384         class_metadata_.GetOrCreate(otherRef, create_metadata_fn);
2385     metadata.flags_ |= otherData.GetFlags();
2386     metadata.annotations_.insert(
2387         metadata.annotations_.end(), other_annotations.begin(), other_annotations.end());
2388 
2389     max_aggregation_for_classes_ = std::max(
2390           max_aggregation_for_classes_,
2391           static_cast<uint32_t>(metadata.annotations_.size()));
2392   }
2393 }
2394 
2395 }  // namespace art
2396