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