1 /*
2  * Copyright (C) 2019 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_boot_info.h"
18 
19 #include <unistd.h>
20 
21 #include <vector>
22 
23 #include "dex/dex_file.h"
24 #include "profile_helpers.h"
25 
26 
27 namespace art {
28 
Add(const DexFile * dex_file,uint32_t method_index)29 void ProfileBootInfo::Add(const DexFile* dex_file, uint32_t method_index) {
30   auto it = std::find(dex_files_.begin(), dex_files_.end(), dex_file);
31   uint32_t index = 0;
32   if (it == dex_files_.end()) {
33     index = dex_files_.size();
34     dex_files_.push_back(dex_file);
35   } else {
36     index = std::distance(dex_files_.begin(), it);
37   }
38   methods_.push_back(std::make_pair(index, method_index));
39 }
40 
Save(int fd) const41 bool ProfileBootInfo::Save(int fd) const {
42   std::vector<uint8_t> buffer;
43   // Store dex file locations.
44   for (const DexFile* dex_file : dex_files_) {
45     AddUintToBuffer(&buffer, static_cast<uint8_t>(dex_file->GetLocation().size()));
46     AddStringToBuffer(&buffer, dex_file->GetLocation());
47   }
48   // Store marker between dex file locations and methods.
49   AddUintToBuffer(&buffer, static_cast<uint8_t>(0));
50 
51   // Store pairs of <dex file index, method id>, in compilation order.
52   for (const std::pair<uint32_t, uint32_t>& pair : methods_) {
53     AddUintToBuffer(&buffer, pair.first);
54     AddUintToBuffer(&buffer, pair.second);
55   }
56   if (!WriteBuffer(fd, buffer.data(), buffer.size())) {
57     return false;
58   }
59   return true;
60 }
61 
Load(int fd,const std::vector<const DexFile * > & dex_files)62 bool ProfileBootInfo::Load(int fd, const std::vector<const DexFile*>& dex_files) {
63   // Read dex file locations.
64   do {
65     uint8_t string_length;
66     int bytes_read = TEMP_FAILURE_RETRY(read(fd, &string_length, sizeof(uint8_t)));
67     if (bytes_read < 0) {
68       PLOG(ERROR) << "Unexpected error reading profile";
69       return false;
70     } else if (bytes_read == 0) {
71       if (dex_files.empty()) {
72         // If no dex files have been passed, that's expected.
73         return true;
74       } else {
75         LOG(ERROR) << "Unexpected end of file for length";
76         return false;
77       }
78     }
79     if (string_length == 0) {
80       break;
81     }
82     std::unique_ptr<char[]> data(new char[string_length]);
83     bytes_read = TEMP_FAILURE_RETRY(read(fd, data.get(), string_length));
84     if (bytes_read < 0) {
85       PLOG(WARNING) << "Unexpected error reading profile";
86       return false;
87     } else if (bytes_read == 0) {
88       LOG(ERROR) << "Unexpected end of file for name";
89       return false;
90     }
91     // Map the location to an instance of dex file in `dex_files`.
92     auto it = std::find_if(dex_files.begin(),
93                            dex_files.end(),
94                            [string_length, &data](const DexFile* file) {
95       std::string dex_location = file->GetLocation();
96       return dex_location.size() == string_length &&
97           (strncmp(data.get(), dex_location.data(), string_length) == 0);
98     });
99     if (it != dex_files.end()) {
100       dex_files_.push_back(*it);
101     } else {
102       LOG(ERROR) << "Couldn't find " << std::string(data.get(), string_length);
103       return false;
104     }
105   } while (true);
106 
107   // Read methods.
108   do {
109     uint32_t dex_file_index;
110     uint32_t method_id;
111     int bytes_read = TEMP_FAILURE_RETRY(read(fd, &dex_file_index, sizeof(dex_file_index)));
112     if (bytes_read <= 0) {
113       break;
114     }
115     bytes_read = TEMP_FAILURE_RETRY(read(fd, &method_id, sizeof(method_id)));
116     if (bytes_read <= 0) {
117       LOG(ERROR) << "Didn't get a method id";
118       return false;
119     }
120     methods_.push_back(std::make_pair(dex_file_index, method_id));
121   } while (true);
122   return true;
123 }
124 
125 }  // namespace art
126