xref: /aosp_15_r20/art/runtime/oat/oat_file_assistant_context.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 #ifndef ART_RUNTIME_OAT_OAT_FILE_ASSISTANT_CONTEXT_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_OAT_OAT_FILE_ASSISTANT_CONTEXT_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <optional>
21*795d594fSAndroid Build Coastguard Worker #include <string>
22*795d594fSAndroid Build Coastguard Worker #include <unordered_map>
23*795d594fSAndroid Build Coastguard Worker #include <vector>
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
27*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker // A helper class for OatFileAssistant that fetches and caches information including boot image
32*795d594fSAndroid Build Coastguard Worker // checksums, bootclasspath checksums, and APEX versions. The same instance can be reused across
33*795d594fSAndroid Build Coastguard Worker // OatFileAssistant calls on different dex files for different instruction sets.
34*795d594fSAndroid Build Coastguard Worker // This class is not thread-safe until `FetchAll` is called.
35*795d594fSAndroid Build Coastguard Worker class OatFileAssistantContext {
36*795d594fSAndroid Build Coastguard Worker  public:
37*795d594fSAndroid Build Coastguard Worker   // Options that a runtime would take.
38*795d594fSAndroid Build Coastguard Worker   // Note that the struct only keeps references, so the caller must keep the objects alive during
39*795d594fSAndroid Build Coastguard Worker   // the lifetime of OatFileAssistant.
40*795d594fSAndroid Build Coastguard Worker   struct RuntimeOptions {
41*795d594fSAndroid Build Coastguard Worker     // Required. See `-Ximage`.
42*795d594fSAndroid Build Coastguard Worker     const std::vector<std::string>& image_locations;
43*795d594fSAndroid Build Coastguard Worker     // Required. See `-Xbootclasspath`.
44*795d594fSAndroid Build Coastguard Worker     const std::vector<std::string>& boot_class_path;
45*795d594fSAndroid Build Coastguard Worker     // Required. See `-Xbootclasspath-locations`.
46*795d594fSAndroid Build Coastguard Worker     const std::vector<std::string>& boot_class_path_locations;
47*795d594fSAndroid Build Coastguard Worker     // Optional. See `-Xbootclasspathfds`.
48*795d594fSAndroid Build Coastguard Worker     std::optional<ArrayRef<File>> boot_class_path_files = {};
49*795d594fSAndroid Build Coastguard Worker     // Optional. See `-Xdeny-art-apex-data-files`.
50*795d594fSAndroid Build Coastguard Worker     const bool deny_art_apex_data_files = false;
51*795d594fSAndroid Build Coastguard Worker   };
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker   // Information about a boot image.
54*795d594fSAndroid Build Coastguard Worker   struct BootImageInfo {
55*795d594fSAndroid Build Coastguard Worker     // Number of BCP jars covered by the boot image.
56*795d594fSAndroid Build Coastguard Worker     size_t component_count;
57*795d594fSAndroid Build Coastguard Worker     // Checksum of the boot image. The format is "i;<component_count>/<checksum_in_8_digit_hex>"
58*795d594fSAndroid Build Coastguard Worker     std::string checksum;
59*795d594fSAndroid Build Coastguard Worker   };
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   // Constructs OatFileAssistantContext from runtime options. Does not fetch information on
62*795d594fSAndroid Build Coastguard Worker   // construction. Information will be fetched from disk when needed.
63*795d594fSAndroid Build Coastguard Worker   EXPORT explicit OatFileAssistantContext(std::unique_ptr<RuntimeOptions> runtime_options);
64*795d594fSAndroid Build Coastguard Worker   // Constructs OatFileAssistantContext from a runtime instance. Fetches as much information as
65*795d594fSAndroid Build Coastguard Worker   // possible from the runtime. The rest information will be fetched from disk when needed.
66*795d594fSAndroid Build Coastguard Worker   explicit OatFileAssistantContext(Runtime* runtime);
67*795d594fSAndroid Build Coastguard Worker   // Returns runtime options.
68*795d594fSAndroid Build Coastguard Worker   const RuntimeOptions& GetRuntimeOptions() const;
69*795d594fSAndroid Build Coastguard Worker   // Fetches all information that hasn't been fetched from disk and caches it. All operations will
70*795d594fSAndroid Build Coastguard Worker   // be read-only after a successful call to this function.
71*795d594fSAndroid Build Coastguard Worker   EXPORT bool FetchAll(std::string* error_msg);
72*795d594fSAndroid Build Coastguard Worker   // Returns information about the boot image of the given instruction set.
73*795d594fSAndroid Build Coastguard Worker   EXPORT const std::vector<BootImageInfo>& GetBootImageInfoList(InstructionSet isa);
74*795d594fSAndroid Build Coastguard Worker   // Returns the checksums of the dex files in the BCP jar at the given index, or nullptr on error.
75*795d594fSAndroid Build Coastguard Worker   // The format of each checksum is "/<checksum_in_8_digit_hex>".
76*795d594fSAndroid Build Coastguard Worker   const std::vector<std::string>* GetBcpChecksums(size_t bcp_index, std::string* error_msg);
77*795d594fSAndroid Build Coastguard Worker   // Returns a string that represents the apex versions of boot classpath jars. See
78*795d594fSAndroid Build Coastguard Worker   // `Runtime::apex_versions_` for the encoding format.
79*795d594fSAndroid Build Coastguard Worker   const std::string& GetApexVersions();
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker  private:
82*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<RuntimeOptions> runtime_options_;
83*795d594fSAndroid Build Coastguard Worker   std::unordered_map<InstructionSet, std::vector<BootImageInfo>> boot_image_info_list_by_isa_;
84*795d594fSAndroid Build Coastguard Worker   std::unordered_map<size_t, std::vector<std::string>> bcp_checksums_by_index_;
85*795d594fSAndroid Build Coastguard Worker   std::optional<std::string> apex_versions_;
86*795d594fSAndroid Build Coastguard Worker };
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker }  // namespace art
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_OAT_OAT_FILE_ASSISTANT_CONTEXT_H_
91