1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2022 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "oat_file_assistant_context.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <memory>
20*795d594fSAndroid Build Coastguard Worker #include <optional>
21*795d594fSAndroid Build Coastguard Worker #include <string>
22*795d594fSAndroid Build Coastguard Worker #include <vector>
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
25*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
26*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
27*795d594fSAndroid Build Coastguard Worker #include "base/array_ref.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
29*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"
30*795d594fSAndroid Build Coastguard Worker #include "base/mem_map.h"
31*795d594fSAndroid Build Coastguard Worker #include "class_linker.h"
32*795d594fSAndroid Build Coastguard Worker #include "dex/art_dex_file_loader.h"
33*795d594fSAndroid Build Coastguard Worker #include "gc/heap.h"
34*795d594fSAndroid Build Coastguard Worker #include "gc/space/image_space.h"
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
37*795d594fSAndroid Build Coastguard Worker
38*795d594fSAndroid Build Coastguard Worker using ::android::base::StringPrintf;
39*795d594fSAndroid Build Coastguard Worker using ::art::gc::space::ImageSpace;
40*795d594fSAndroid Build Coastguard Worker
OatFileAssistantContext(std::unique_ptr<OatFileAssistantContext::RuntimeOptions> runtime_options)41*795d594fSAndroid Build Coastguard Worker OatFileAssistantContext::OatFileAssistantContext(
42*795d594fSAndroid Build Coastguard Worker std::unique_ptr<OatFileAssistantContext::RuntimeOptions> runtime_options)
43*795d594fSAndroid Build Coastguard Worker : runtime_options_(std::move(runtime_options)) {
44*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(runtime_options_->boot_class_path.size(),
45*795d594fSAndroid Build Coastguard Worker runtime_options_->boot_class_path_locations.size());
46*795d594fSAndroid Build Coastguard Worker DCHECK_IMPLIES(
47*795d594fSAndroid Build Coastguard Worker runtime_options_->boot_class_path_files.has_value(),
48*795d594fSAndroid Build Coastguard Worker runtime_options_->boot_class_path.size() == runtime_options_->boot_class_path_files->size());
49*795d594fSAndroid Build Coastguard Worker // Opening dex files and boot images require MemMap.
50*795d594fSAndroid Build Coastguard Worker MemMap::Init();
51*795d594fSAndroid Build Coastguard Worker }
52*795d594fSAndroid Build Coastguard Worker
OatFileAssistantContext(Runtime * runtime)53*795d594fSAndroid Build Coastguard Worker OatFileAssistantContext::OatFileAssistantContext(Runtime* runtime)
54*795d594fSAndroid Build Coastguard Worker : OatFileAssistantContext(std::make_unique<OatFileAssistantContext::RuntimeOptions>(
55*795d594fSAndroid Build Coastguard Worker OatFileAssistantContext::RuntimeOptions{
56*795d594fSAndroid Build Coastguard Worker .image_locations = runtime->GetImageLocations(),
57*795d594fSAndroid Build Coastguard Worker .boot_class_path = runtime->GetBootClassPath(),
58*795d594fSAndroid Build Coastguard Worker .boot_class_path_locations = runtime->GetBootClassPathLocations(),
59*795d594fSAndroid Build Coastguard Worker .boot_class_path_files = !runtime->GetBootClassPathFiles().empty() ?
60*795d594fSAndroid Build Coastguard Worker runtime->GetBootClassPathFiles() :
61*795d594fSAndroid Build Coastguard Worker std::optional<ArrayRef<File>>(),
62*795d594fSAndroid Build Coastguard Worker .deny_art_apex_data_files = runtime->DenyArtApexDataFiles(),
63*795d594fSAndroid Build Coastguard Worker })) {
64*795d594fSAndroid Build Coastguard Worker // Fetch boot image info from the runtime.
65*795d594fSAndroid Build Coastguard Worker std::vector<BootImageInfo>& boot_image_info_list =
66*795d594fSAndroid Build Coastguard Worker boot_image_info_list_by_isa_[kRuntimeQuickCodeISA];
67*795d594fSAndroid Build Coastguard Worker for (const ImageSpace* image_space : runtime->GetHeap()->GetBootImageSpaces()) {
68*795d594fSAndroid Build Coastguard Worker // We only need the checksum of the first component for each boot image. They are in image
69*795d594fSAndroid Build Coastguard Worker // spaces that have a non-zero component count.
70*795d594fSAndroid Build Coastguard Worker if (image_space->GetComponentCount() > 0) {
71*795d594fSAndroid Build Coastguard Worker BootImageInfo& boot_image_info = boot_image_info_list.emplace_back();
72*795d594fSAndroid Build Coastguard Worker boot_image_info.component_count = image_space->GetComponentCount();
73*795d594fSAndroid Build Coastguard Worker ImageSpace::AppendImageChecksum(image_space->GetComponentCount(),
74*795d594fSAndroid Build Coastguard Worker image_space->GetImageHeader().GetImageChecksum(),
75*795d594fSAndroid Build Coastguard Worker &boot_image_info.checksum);
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker }
78*795d594fSAndroid Build Coastguard Worker
79*795d594fSAndroid Build Coastguard Worker // Fetch BCP checksums from the runtime.
80*795d594fSAndroid Build Coastguard Worker size_t bcp_index = 0;
81*795d594fSAndroid Build Coastguard Worker std::vector<std::string>* current_bcp_checksums = nullptr;
82*795d594fSAndroid Build Coastguard Worker const std::vector<const DexFile*>& bcp_dex_files = runtime->GetClassLinker()->GetBootClassPath();
83*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < bcp_dex_files.size();) {
84*795d594fSAndroid Build Coastguard Worker uint32_t checksum = DexFileLoader::GetMultiDexChecksum(bcp_dex_files, &i);
85*795d594fSAndroid Build Coastguard Worker DCHECK_LT(bcp_index, runtime_options_->boot_class_path.size());
86*795d594fSAndroid Build Coastguard Worker current_bcp_checksums = &bcp_checksums_by_index_[bcp_index++];
87*795d594fSAndroid Build Coastguard Worker DCHECK_NE(current_bcp_checksums, nullptr);
88*795d594fSAndroid Build Coastguard Worker current_bcp_checksums->push_back(StringPrintf("/%08x", checksum));
89*795d594fSAndroid Build Coastguard Worker }
90*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(bcp_index, runtime_options_->boot_class_path.size());
91*795d594fSAndroid Build Coastguard Worker
92*795d594fSAndroid Build Coastguard Worker // Fetch APEX versions from the runtime.
93*795d594fSAndroid Build Coastguard Worker apex_versions_ = runtime->GetApexVersions();
94*795d594fSAndroid Build Coastguard Worker }
95*795d594fSAndroid Build Coastguard Worker
GetRuntimeOptions() const96*795d594fSAndroid Build Coastguard Worker const OatFileAssistantContext::RuntimeOptions& OatFileAssistantContext::GetRuntimeOptions() const {
97*795d594fSAndroid Build Coastguard Worker return *runtime_options_;
98*795d594fSAndroid Build Coastguard Worker }
99*795d594fSAndroid Build Coastguard Worker
FetchAll(std::string * error_msg)100*795d594fSAndroid Build Coastguard Worker bool OatFileAssistantContext::FetchAll(std::string* error_msg) {
101*795d594fSAndroid Build Coastguard Worker std::vector<InstructionSet> isas = GetSupportedInstructionSets(error_msg);
102*795d594fSAndroid Build Coastguard Worker if (isas.empty()) {
103*795d594fSAndroid Build Coastguard Worker return false;
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker for (InstructionSet isa : isas) {
106*795d594fSAndroid Build Coastguard Worker GetBootImageInfoList(isa);
107*795d594fSAndroid Build Coastguard Worker }
108*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < runtime_options_->boot_class_path.size(); i++) {
109*795d594fSAndroid Build Coastguard Worker if (GetBcpChecksums(i, error_msg) == nullptr) {
110*795d594fSAndroid Build Coastguard Worker return false;
111*795d594fSAndroid Build Coastguard Worker }
112*795d594fSAndroid Build Coastguard Worker }
113*795d594fSAndroid Build Coastguard Worker GetApexVersions();
114*795d594fSAndroid Build Coastguard Worker return true;
115*795d594fSAndroid Build Coastguard Worker }
116*795d594fSAndroid Build Coastguard Worker
117*795d594fSAndroid Build Coastguard Worker const std::vector<OatFileAssistantContext::BootImageInfo>&
GetBootImageInfoList(InstructionSet isa)118*795d594fSAndroid Build Coastguard Worker OatFileAssistantContext::GetBootImageInfoList(InstructionSet isa) {
119*795d594fSAndroid Build Coastguard Worker if (auto it = boot_image_info_list_by_isa_.find(isa); it != boot_image_info_list_by_isa_.end()) {
120*795d594fSAndroid Build Coastguard Worker return it->second;
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker
123*795d594fSAndroid Build Coastguard Worker ImageSpace::BootImageLayout layout(
124*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(runtime_options_->image_locations),
125*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(runtime_options_->boot_class_path),
126*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(runtime_options_->boot_class_path_locations),
127*795d594fSAndroid Build Coastguard Worker runtime_options_->boot_class_path_files.value_or(ArrayRef<File>()),
128*795d594fSAndroid Build Coastguard Worker /*boot_class_path_image_files=*/{},
129*795d594fSAndroid Build Coastguard Worker /*boot_class_path_vdex_files=*/{},
130*795d594fSAndroid Build Coastguard Worker /*boot_class_path_oat_files=*/{},
131*795d594fSAndroid Build Coastguard Worker &GetApexVersions());
132*795d594fSAndroid Build Coastguard Worker
133*795d594fSAndroid Build Coastguard Worker std::string error_msg;
134*795d594fSAndroid Build Coastguard Worker if (!layout.LoadFromSystem(isa, /*allow_in_memory_compilation=*/false, &error_msg)) {
135*795d594fSAndroid Build Coastguard Worker // At this point, `layout` contains nothing.
136*795d594fSAndroid Build Coastguard Worker VLOG(oat) << "Some error occurred when loading boot images for oat file validation: "
137*795d594fSAndroid Build Coastguard Worker << error_msg;
138*795d594fSAndroid Build Coastguard Worker // Create an empty entry so that we don't have to retry when the function is called again.
139*795d594fSAndroid Build Coastguard Worker return boot_image_info_list_by_isa_[isa];
140*795d594fSAndroid Build Coastguard Worker }
141*795d594fSAndroid Build Coastguard Worker
142*795d594fSAndroid Build Coastguard Worker std::vector<BootImageInfo>& boot_image_info_list = boot_image_info_list_by_isa_[isa];
143*795d594fSAndroid Build Coastguard Worker for (const ImageSpace::BootImageLayout::ImageChunk& chunk : layout.GetChunks()) {
144*795d594fSAndroid Build Coastguard Worker BootImageInfo& boot_image_info = boot_image_info_list.emplace_back();
145*795d594fSAndroid Build Coastguard Worker boot_image_info.component_count = chunk.component_count;
146*795d594fSAndroid Build Coastguard Worker ImageSpace::AppendImageChecksum(
147*795d594fSAndroid Build Coastguard Worker chunk.component_count, chunk.checksum, &boot_image_info.checksum);
148*795d594fSAndroid Build Coastguard Worker }
149*795d594fSAndroid Build Coastguard Worker return boot_image_info_list;
150*795d594fSAndroid Build Coastguard Worker }
151*795d594fSAndroid Build Coastguard Worker
GetBcpChecksums(size_t bcp_index,std::string * error_msg)152*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>* OatFileAssistantContext::GetBcpChecksums(size_t bcp_index,
153*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
154*795d594fSAndroid Build Coastguard Worker DCHECK_LT(bcp_index, runtime_options_->boot_class_path.size());
155*795d594fSAndroid Build Coastguard Worker
156*795d594fSAndroid Build Coastguard Worker if (auto it = bcp_checksums_by_index_.find(bcp_index); it != bcp_checksums_by_index_.end()) {
157*795d594fSAndroid Build Coastguard Worker return &it->second;
158*795d594fSAndroid Build Coastguard Worker }
159*795d594fSAndroid Build Coastguard Worker
160*795d594fSAndroid Build Coastguard Worker std::optional<ArrayRef<File>> bcp_files = runtime_options_->boot_class_path_files;
161*795d594fSAndroid Build Coastguard Worker File noFile;
162*795d594fSAndroid Build Coastguard Worker File* file = bcp_files.has_value() ? &(*bcp_files)[bcp_index] : &noFile;
163*795d594fSAndroid Build Coastguard Worker ArtDexFileLoader dex_loader(file, runtime_options_->boot_class_path[bcp_index]);
164*795d594fSAndroid Build Coastguard Worker std::optional<uint32_t> checksum;
165*795d594fSAndroid Build Coastguard Worker bool ok = dex_loader.GetMultiDexChecksum(&checksum, error_msg);
166*795d594fSAndroid Build Coastguard Worker if (!ok) {
167*795d594fSAndroid Build Coastguard Worker return nullptr;
168*795d594fSAndroid Build Coastguard Worker }
169*795d594fSAndroid Build Coastguard Worker
170*795d594fSAndroid Build Coastguard Worker DCHECK(checksum.has_value());
171*795d594fSAndroid Build Coastguard Worker std::vector<std::string>& bcp_checksums = bcp_checksums_by_index_[bcp_index];
172*795d594fSAndroid Build Coastguard Worker if (checksum.has_value()) {
173*795d594fSAndroid Build Coastguard Worker bcp_checksums.push_back(StringPrintf("/%08x", checksum.value()));
174*795d594fSAndroid Build Coastguard Worker }
175*795d594fSAndroid Build Coastguard Worker return &bcp_checksums;
176*795d594fSAndroid Build Coastguard Worker }
177*795d594fSAndroid Build Coastguard Worker
GetApexVersions()178*795d594fSAndroid Build Coastguard Worker const std::string& OatFileAssistantContext::GetApexVersions() {
179*795d594fSAndroid Build Coastguard Worker if (apex_versions_.has_value()) {
180*795d594fSAndroid Build Coastguard Worker return apex_versions_.value();
181*795d594fSAndroid Build Coastguard Worker }
182*795d594fSAndroid Build Coastguard Worker
183*795d594fSAndroid Build Coastguard Worker apex_versions_ = Runtime::GetApexVersions(
184*795d594fSAndroid Build Coastguard Worker ArrayRef<const std::string>(runtime_options_->boot_class_path_locations));
185*795d594fSAndroid Build Coastguard Worker return apex_versions_.value();
186*795d594fSAndroid Build Coastguard Worker }
187*795d594fSAndroid Build Coastguard Worker
188*795d594fSAndroid Build Coastguard Worker } // namespace art
189