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