• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ARTD_ARTD_H_
18 #define ART_ARTD_ARTD_H_
19 
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
23 #include <csignal>
24 #include <cstdint>
25 #include <functional>
26 #include <memory>
27 #include <mutex>
28 #include <optional>
29 #include <string>
30 #include <unordered_map>
31 #include <unordered_set>
32 #include <utility>
33 #include <vector>
34 
35 #include "aidl/com/android/server/art/BnArtd.h"
36 #include "aidl/com/android/server/art/BnArtdCancellationSignal.h"
37 #include "android-base/result.h"
38 #include "android-base/thread_annotations.h"
39 #include "android/binder_auto_utils.h"
40 #include "base/os.h"
41 #include "exec_utils.h"
42 #include "oat_file_assistant_context.h"
43 #include "tools/cmdline_builder.h"
44 #include "tools/system_properties.h"
45 
46 namespace art {
47 namespace artd {
48 
49 class ArtdCancellationSignal : public aidl::com::android::server::art::BnArtdCancellationSignal {
50  public:
ArtdCancellationSignal(std::function<int (pid_t,int)> kill_func)51   explicit ArtdCancellationSignal(std::function<int(pid_t, int)> kill_func)
52       : kill_(std::move(kill_func)) {}
53 
54   ndk::ScopedAStatus cancel() override;
55 
56   ndk::ScopedAStatus getType(int64_t* _aidl_return) override;
57 
58  private:
59   std::mutex mu_;
60   // True if cancellation has been signaled.
61   bool is_cancelled_ GUARDED_BY(mu_) = false;
62   // The pids of currently running child processes that are bound to this signal.
63   std::unordered_set<pid_t> pids_ GUARDED_BY(mu_);
64 
65   std::function<int(pid_t, int)> kill_;
66 
67   friend class Artd;
68 };
69 
70 class Artd : public aidl::com::android::server::art::BnArtd {
71  public:
72   explicit Artd(std::unique_ptr<art::tools::SystemProperties> props =
73                     std::make_unique<art::tools::SystemProperties>(),
74                 std::unique_ptr<ExecUtils> exec_utils = std::make_unique<ExecUtils>(),
75                 std::function<int(pid_t, int)> kill_func = kill,
76                 std::function<int(int, struct stat*)> fstat_func = fstat)
props_(std::move (props))77       : props_(std::move(props)),
78         exec_utils_(std::move(exec_utils)),
79         kill_(std::move(kill_func)),
80         fstat_(std::move(fstat_func)) {}
81 
82   ndk::ScopedAStatus isAlive(bool* _aidl_return) override;
83 
84   ndk::ScopedAStatus deleteArtifacts(
85       const aidl::com::android::server::art::ArtifactsPath& in_artifactsPath,
86       int64_t* _aidl_return) override;
87 
88   ndk::ScopedAStatus getDexoptStatus(
89       const std::string& in_dexFile,
90       const std::string& in_instructionSet,
91       const std::optional<std::string>& in_classLoaderContext,
92       aidl::com::android::server::art::GetDexoptStatusResult* _aidl_return) override;
93 
94   ndk::ScopedAStatus isProfileUsable(const aidl::com::android::server::art::ProfilePath& in_profile,
95                                      const std::string& in_dexFile,
96                                      bool* _aidl_return) override;
97 
98   ndk::ScopedAStatus copyAndRewriteProfile(
99       const aidl::com::android::server::art::ProfilePath& in_src,
100       aidl::com::android::server::art::OutputProfile* in_dst,
101       const std::string& in_dexFile,
102       bool* _aidl_return) override;
103 
104   ndk::ScopedAStatus commitTmpProfile(
105       const aidl::com::android::server::art::ProfilePath::TmpProfilePath& in_profile) override;
106 
107   ndk::ScopedAStatus deleteProfile(
108       const aidl::com::android::server::art::ProfilePath& in_profile) override;
109 
110   ndk::ScopedAStatus getProfileVisibility(
111       const aidl::com::android::server::art::ProfilePath& in_profile,
112       aidl::com::android::server::art::FileVisibility* _aidl_return) override;
113 
114   ndk::ScopedAStatus mergeProfiles(
115       const std::vector<aidl::com::android::server::art::ProfilePath>& in_profiles,
116       const std::optional<aidl::com::android::server::art::ProfilePath>& in_referenceProfile,
117       aidl::com::android::server::art::OutputProfile* in_outputProfile,
118       const std::vector<std::string>& in_dexFiles,
119       const aidl::com::android::server::art::MergeProfileOptions& in_options,
120       bool* _aidl_return) override;
121 
122   ndk::ScopedAStatus getArtifactsVisibility(
123       const aidl::com::android::server::art::ArtifactsPath& in_artifactsPath,
124       aidl::com::android::server::art::FileVisibility* _aidl_return) override;
125 
126   ndk::ScopedAStatus getDexFileVisibility(
127       const std::string& in_dexFile,
128       aidl::com::android::server::art::FileVisibility* _aidl_return) override;
129 
130   ndk::ScopedAStatus getDmFileVisibility(
131       const aidl::com::android::server::art::DexMetadataPath& in_dmFile,
132       aidl::com::android::server::art::FileVisibility* _aidl_return) override;
133 
134   ndk::ScopedAStatus getDexoptNeeded(
135       const std::string& in_dexFile,
136       const std::string& in_instructionSet,
137       const std::optional<std::string>& in_classLoaderContext,
138       const std::string& in_compilerFilter,
139       int32_t in_dexoptTrigger,
140       aidl::com::android::server::art::GetDexoptNeededResult* _aidl_return) override;
141 
142   ndk::ScopedAStatus dexopt(
143       const aidl::com::android::server::art::OutputArtifacts& in_outputArtifacts,
144       const std::string& in_dexFile,
145       const std::string& in_instructionSet,
146       const std::optional<std::string>& in_classLoaderContext,
147       const std::string& in_compilerFilter,
148       const std::optional<aidl::com::android::server::art::ProfilePath>& in_profile,
149       const std::optional<aidl::com::android::server::art::VdexPath>& in_inputVdex,
150       const std::optional<aidl::com::android::server::art::DexMetadataPath>& in_dmFile,
151       aidl::com::android::server::art::PriorityClass in_priorityClass,
152       const aidl::com::android::server::art::DexoptOptions& in_dexoptOptions,
153       const std::shared_ptr<aidl::com::android::server::art::IArtdCancellationSignal>&
154           in_cancellationSignal,
155       aidl::com::android::server::art::ArtdDexoptResult* _aidl_return) override;
156 
157   ndk::ScopedAStatus createCancellationSignal(
158       std::shared_ptr<aidl::com::android::server::art::IArtdCancellationSignal>* _aidl_return)
159       override;
160 
161   ndk::ScopedAStatus cleanup(
162       const std::vector<aidl::com::android::server::art::ProfilePath>& in_profilesToKeep,
163       const std::vector<aidl::com::android::server::art::ArtifactsPath>& in_artifactsToKeep,
164       const std::vector<aidl::com::android::server::art::VdexPath>& in_vdexFilesToKeep,
165       int64_t* _aidl_return) override;
166 
167   ndk::ScopedAStatus isIncrementalFsPath(const std::string& in_dexFile,
168                                          bool* _aidl_return) override;
169 
170   android::base::Result<void> Start();
171 
172  private:
173   android::base::Result<OatFileAssistantContext*> GetOatFileAssistantContext()
174       EXCLUDES(ofa_context_mu_);
175 
176   android::base::Result<const std::vector<std::string>*> GetBootImageLocations()
177       EXCLUDES(cache_mu_);
178 
179   android::base::Result<const std::vector<std::string>*> GetBootClassPath() EXCLUDES(cache_mu_);
180 
181   bool UseJitZygote() EXCLUDES(cache_mu_);
182   bool UseJitZygoteLocked() REQUIRES(cache_mu_);
183 
184   const std::string& GetUserDefinedBootImageLocations() EXCLUDES(cache_mu_);
185   const std::string& GetUserDefinedBootImageLocationsLocked() REQUIRES(cache_mu_);
186 
187   bool DenyArtApexDataFiles() EXCLUDES(cache_mu_);
188   bool DenyArtApexDataFilesLocked() REQUIRES(cache_mu_);
189 
190   android::base::Result<int> ExecAndReturnCode(const std::vector<std::string>& arg_vector,
191                                                int timeout_sec,
192                                                const ExecCallbacks& callbacks = ExecCallbacks(),
193                                                ProcessStat* stat = nullptr) const;
194 
195   android::base::Result<std::string> GetProfman();
196 
197   android::base::Result<std::string> GetArtExec();
198 
199   bool ShouldUseDex2Oat64();
200 
201   android::base::Result<std::string> GetDex2Oat();
202 
203   bool ShouldCreateSwapFileForDexopt();
204 
205   void AddBootImageFlags(/*out*/ art::tools::CmdlineBuilder& args);
206 
207   void AddCompilerConfigFlags(const std::string& instruction_set,
208                               const std::string& compiler_filter,
209                               aidl::com::android::server::art::PriorityClass priority_class,
210                               const aidl::com::android::server::art::DexoptOptions& dexopt_options,
211                               /*out*/ art::tools::CmdlineBuilder& args);
212 
213   void AddPerfConfigFlags(aidl::com::android::server::art::PriorityClass priority_class,
214                           /*out*/ art::tools::CmdlineBuilder& art_exec_args,
215                           /*out*/ art::tools::CmdlineBuilder& args);
216 
217   android::base::Result<struct stat> Fstat(const art::File& file) const;
218 
219   std::mutex cache_mu_;
220   std::optional<std::vector<std::string>> cached_boot_image_locations_ GUARDED_BY(cache_mu_);
221   std::optional<std::vector<std::string>> cached_boot_class_path_ GUARDED_BY(cache_mu_);
222   std::optional<bool> cached_use_jit_zygote_ GUARDED_BY(cache_mu_);
223   std::optional<std::string> cached_user_defined_boot_image_locations_ GUARDED_BY(cache_mu_);
224   std::optional<bool> cached_deny_art_apex_data_files_ GUARDED_BY(cache_mu_);
225 
226   std::mutex ofa_context_mu_;
227   std::unique_ptr<OatFileAssistantContext> ofa_context_ GUARDED_BY(ofa_context_mu_);
228 
229   const std::unique_ptr<art::tools::SystemProperties> props_;
230   const std::unique_ptr<ExecUtils> exec_utils_;
231   const std::function<int(pid_t, int)> kill_;
232   const std::function<int(int, struct stat*)> fstat_;
233 };
234 
235 }  // namespace artd
236 }  // namespace art
237 
238 #endif  // ART_ARTD_ARTD_H_
239