1 /*
2  * Copyright (C) 2021 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 package com.android.server.art;
18 
19 /** @hide */
20 interface IArtd {
21     // Test to see if the artd service is available.
isAlive()22     boolean isAlive();
23 
24     /**
25      * Deletes dexopt artifacts and returns the released space, in bytes.
26      *
27      * Note that this method doesn't delete runtime artifacts. To delete them, call
28      * `deleteRuntimeArtifacts`.
29      *
30      * Not supported in Pre-reboot Dexopt mode.
31      *
32      * Throws fatal errors. Logs and ignores non-fatal errors.
33      */
deleteArtifacts(in com.android.server.art.ArtifactsPath artifactsPath)34     long deleteArtifacts(in com.android.server.art.ArtifactsPath artifactsPath);
35 
36     /**
37      * Returns the dexopt status of a dex file.
38      *
39      * Not supported in Pre-reboot Dexopt mode.
40      *
41      * Throws fatal and non-fatal errors.
42      */
getDexoptStatus( @tf8InCpp String dexFile, @utf8InCpp String instructionSet, @nullable @utf8InCpp String classLoaderContext)43     com.android.server.art.GetDexoptStatusResult getDexoptStatus(
44             @utf8InCpp String dexFile, @utf8InCpp String instructionSet,
45             @nullable @utf8InCpp String classLoaderContext);
46 
47     /**
48      * Returns true if the profile exists and contains entries for the given dex file.
49      *
50      * Throws fatal and non-fatal errors.
51      */
isProfileUsable(in com.android.server.art.ProfilePath profile, @utf8InCpp String dexFile)52     boolean isProfileUsable(in com.android.server.art.ProfilePath profile,
53             @utf8InCpp String dexFile);
54 
55     /**
56      * Copies the profile and rewrites it for the given dex file. Returns `SUCCESS` and fills
57      * `dst.profilePath.id` if the operation succeeds and `src` exists and contains entries that
58      * match the given dex file.
59      *
60      * Throws fatal and non-fatal errors, except if the input is a bad profile.
61      */
copyAndRewriteProfile( in com.android.server.art.ProfilePath src, inout com.android.server.art.OutputProfile dst, @utf8InCpp String dexFile)62     com.android.server.art.CopyAndRewriteProfileResult copyAndRewriteProfile(
63             in com.android.server.art.ProfilePath src,
64             inout com.android.server.art.OutputProfile dst, @utf8InCpp String dexFile);
65 
66     /**
67      * Similar to above. The difference is that the profile is not taken from a separate file but
68      * taken from `dexFile` itself. Specifically, if `dexFile` is a zip file, the profile is taken
69      * from `assets/art-profile/baseline.prof` in the zip. Returns `NO_PROFILE` if `dexFile` is not
70      * a zip file or it doesn't contain a profile.
71      */
copyAndRewriteEmbeddedProfile( inout com.android.server.art.OutputProfile dst, @utf8InCpp String dexFile)72     com.android.server.art.CopyAndRewriteProfileResult copyAndRewriteEmbeddedProfile(
73             inout com.android.server.art.OutputProfile dst, @utf8InCpp String dexFile);
74 
75     /**
76      * Moves the temporary profile to the permanent location.
77      *
78      * Throws fatal and non-fatal errors.
79      */
commitTmpProfile(in com.android.server.art.ProfilePath.TmpProfilePath profile)80     void commitTmpProfile(in com.android.server.art.ProfilePath.TmpProfilePath profile);
81 
82     /**
83      * Deletes the profile. Does nothing of the profile doesn't exist.
84      *
85      * Operates on the whole DM file if given one.
86      *
87      * Throws fatal errors. Logs and ignores non-fatal errors.
88      */
deleteProfile(in com.android.server.art.ProfilePath profile)89     void deleteProfile(in com.android.server.art.ProfilePath profile);
90 
91     /**
92      * Returns the visibility of the profile.
93      *
94      * Operates on the whole DM file if given one.
95      *
96      * Throws fatal and non-fatal errors.
97      */
getProfileVisibility( in com.android.server.art.ProfilePath profile)98     com.android.server.art.FileVisibility getProfileVisibility(
99             in com.android.server.art.ProfilePath profile);
100 
101     /**
102      * Merges profiles. Both `profiles` and `referenceProfile` are inputs, while the difference is
103      * that `referenceProfile` is also used as the reference to calculate the diff. `profiles` that
104      * don't exist are skipped, while `referenceProfile`, if provided, must exist. Returns true,
105      * writes the merge result to `outputProfile` and fills `outputProfile.profilePath.id` and
106      * `outputProfile.profilePath.tmpPath` if a merge has been performed.
107      *
108      * When `options.forceMerge`, `options.dumpOnly`, or `options.dumpClassesAndMethods` is set,
109      * `referenceProfile` must not be set. I.e., all inputs must be provided by `profiles`. This is
110      * because the merge will always happen, and hence no reference profile is needed to calculate
111      * the diff.
112      *
113      * Throws fatal and non-fatal errors.
114      */
mergeProfiles(in List<com.android.server.art.ProfilePath> profiles, in @nullable com.android.server.art.ProfilePath referenceProfile, inout com.android.server.art.OutputProfile outputProfile, in @utf8InCpp List<String> dexFiles, in com.android.server.art.MergeProfileOptions options)115     boolean mergeProfiles(in List<com.android.server.art.ProfilePath> profiles,
116             in @nullable com.android.server.art.ProfilePath referenceProfile,
117             inout com.android.server.art.OutputProfile outputProfile,
118             in @utf8InCpp List<String> dexFiles,
119             in com.android.server.art.MergeProfileOptions options);
120 
121     /**
122      * Returns the visibility of the artifacts.
123      *
124      * Throws fatal and non-fatal errors.
125      */
getArtifactsVisibility( in com.android.server.art.ArtifactsPath artifactsPath)126     com.android.server.art.FileVisibility getArtifactsVisibility(
127             in com.android.server.art.ArtifactsPath artifactsPath);
128 
129     /**
130      * Returns the visibility of the dex file.
131      *
132      * Throws fatal and non-fatal errors.
133      */
getDexFileVisibility(@tf8InCpp String dexFile)134     com.android.server.art.FileVisibility getDexFileVisibility(@utf8InCpp String dexFile);
135 
136     /**
137      * Returns the visibility of the DM file.
138      *
139      * Throws fatal and non-fatal errors.
140      */
getDmFileVisibility( in com.android.server.art.DexMetadataPath dmFile)141     com.android.server.art.FileVisibility getDmFileVisibility(
142             in com.android.server.art.DexMetadataPath dmFile);
143 
144     /**
145      * Returns true if dexopt is needed. `dexoptTrigger` is a bit field that consists of values
146      * defined in `com.android.server.art.DexoptTrigger`.
147      *
148      * Throws fatal and non-fatal errors.
149      */
getDexoptNeeded( @tf8InCpp String dexFile, @utf8InCpp String instructionSet, @nullable @utf8InCpp String classLoaderContext, @utf8InCpp String compilerFilter, int dexoptTrigger)150     com.android.server.art.GetDexoptNeededResult getDexoptNeeded(
151             @utf8InCpp String dexFile, @utf8InCpp String instructionSet,
152             @nullable @utf8InCpp String classLoaderContext, @utf8InCpp String compilerFilter,
153             int dexoptTrigger);
154 
155     /**
156      * Dexopts a dex file for the given instruction set.
157      *
158      * Throws fatal and non-fatal errors.
159      */
dexopt( in com.android.server.art.OutputArtifacts outputArtifacts, @utf8InCpp String dexFile, @utf8InCpp String instructionSet, @nullable @utf8InCpp String classLoaderContext, @utf8InCpp String compilerFilter, in @nullable com.android.server.art.ProfilePath profile, in @nullable com.android.server.art.VdexPath inputVdex, in @nullable com.android.server.art.DexMetadataPath dmFile, com.android.server.art.PriorityClass priorityClass, in com.android.server.art.DexoptOptions dexoptOptions, in com.android.server.art.IArtdCancellationSignal cancellationSignal)160     com.android.server.art.ArtdDexoptResult dexopt(
161             in com.android.server.art.OutputArtifacts outputArtifacts,
162             @utf8InCpp String dexFile, @utf8InCpp String instructionSet,
163             @nullable @utf8InCpp String classLoaderContext, @utf8InCpp String compilerFilter,
164             in @nullable com.android.server.art.ProfilePath profile,
165             in @nullable com.android.server.art.VdexPath inputVdex,
166             in @nullable com.android.server.art.DexMetadataPath dmFile,
167             com.android.server.art.PriorityClass priorityClass,
168             in com.android.server.art.DexoptOptions dexoptOptions,
169             in com.android.server.art.IArtdCancellationSignal cancellationSignal);
170 
171     /**
172      * Returns a cancellation signal which can be used to cancel {@code dexopt} calls.
173      */
createCancellationSignal()174     com.android.server.art.IArtdCancellationSignal createCancellationSignal();
175 
176     /**
177      * Deletes all files that are managed by artd, except those specified in the arguments. Returns
178      * the size of the freed space, in bytes.
179      *
180      * For each entry in `artifactsToKeep`, all three kinds of artifacts (ODEX, VDEX, ART) are
181      * kept. For each entry in `vdexFilesToKeep`, only the VDEX file will be kept. Note that VDEX
182      * files included in `artifactsToKeep` don't have to be listed in `vdexFilesToKeep`.
183      *
184      * Not supported in Pre-reboot Dexopt mode.
185      *
186      * Throws fatal errors. Logs and ignores non-fatal errors.
187      */
cleanup(in List<com.android.server.art.ProfilePath> profilesToKeep, in List<com.android.server.art.ArtifactsPath> artifactsToKeep, in List<com.android.server.art.VdexPath> vdexFilesToKeep, in List<com.android.server.art.RuntimeArtifactsPath> runtimeArtifactsToKeep, boolean keepPreRebootStagedFiles)188     long cleanup(in List<com.android.server.art.ProfilePath> profilesToKeep,
189             in List<com.android.server.art.ArtifactsPath> artifactsToKeep,
190             in List<com.android.server.art.VdexPath> vdexFilesToKeep,
191             in List<com.android.server.art.RuntimeArtifactsPath> runtimeArtifactsToKeep,
192             boolean keepPreRebootStagedFiles);
193 
194     /**
195      * Deletes all Pre-reboot staged files.
196      *
197      * Not supported in Pre-reboot Dexopt mode.
198      *
199      * Throws fatal errors. Logs and ignores non-fatal errors.
200      */
cleanUpPreRebootStagedFiles()201     void cleanUpPreRebootStagedFiles();
202 
203     /**
204      * Returns whether the artifacts of the primary dex files should be in the global dalvik-cache
205      * directory.
206      *
207      * Throws fatal and non-fatal errors.
208      */
isInDalvikCache(@tf8InCpp String dexFile)209     boolean isInDalvikCache(@utf8InCpp String dexFile);
210 
211     /**
212      * Deletes runtime artifacts and returns the released space, in bytes.
213      *
214      * Not supported in Pre-reboot Dexopt mode.
215      *
216      * Throws fatal errors. Logs and ignores non-fatal errors.
217      */
deleteRuntimeArtifacts( in com.android.server.art.RuntimeArtifactsPath runtimeArtifactsPath)218     long deleteRuntimeArtifacts(
219             in com.android.server.art.RuntimeArtifactsPath runtimeArtifactsPath);
220 
221     /**
222      * Returns the size of the dexopt artifacts, in bytes, or 0 if they don't exist or a non-fatal
223      * error occurred.
224      *
225      * Not supported in Pre-reboot Dexopt mode.
226      *
227      * Throws fatal errors. Logs and ignores non-fatal errors.
228      */
getArtifactsSize(in com.android.server.art.ArtifactsPath artifactsPath)229     long getArtifactsSize(in com.android.server.art.ArtifactsPath artifactsPath);
230 
231     /**
232      * Returns the size of the vdex file, in bytes, or 0 if it doesn't exist or a non-fatal error
233      * occurred.
234      *
235      * Not supported in Pre-reboot Dexopt mode.
236      *
237      * Throws fatal errors. Logs and ignores non-fatal errors.
238      */
getVdexFileSize(in com.android.server.art.VdexPath vdexPath)239     long getVdexFileSize(in com.android.server.art.VdexPath vdexPath);
240 
241     /**
242      * Returns the size of the runtime artifacts, in bytes, or 0 if they don't exist or a non-fatal
243      * error occurred.
244      *
245      * Not supported in Pre-reboot Dexopt mode.
246      *
247      * Throws fatal errors. Logs and ignores non-fatal errors.
248      */
getRuntimeArtifactsSize( in com.android.server.art.RuntimeArtifactsPath runtimeArtifactsPath)249     long getRuntimeArtifactsSize(
250             in com.android.server.art.RuntimeArtifactsPath runtimeArtifactsPath);
251 
252     /**
253      * Returns the size of the profile, in bytes, or 0 if it doesn't exist or a non-fatal error
254      * occurred.
255      *
256      * Operates on the whole DM file if given one.
257      *
258      * Not supported in Pre-reboot Dexopt mode.
259      *
260      * Throws fatal errors. Logs and ignores non-fatal errors.
261      */
getProfileSize(in com.android.server.art.ProfilePath profile)262     long getProfileSize(in com.android.server.art.ProfilePath profile);
263 
264     /**
265      * Moves the staged files of the given artifacts and profiles to the permanent locations,
266      * replacing old files if they exist. Removes the staged files and restores the old files at
267      * best effort if any error occurs.
268      *
269      * This is intended to be called for a superset of the packages that we actually expect to have
270      * staged files, so missing files are expected.
271      *
272      * Not supported in Pre-reboot Dexopt mode.
273      *
274      * Throws fatal and non-fatal errors.
275      *
276      * @return true if any file has been committed.
277      */
commitPreRebootStagedFiles( in List<com.android.server.art.ArtifactsPath> artifacts, in List<com.android.server.art.ProfilePath.WritableProfilePath> profiles)278     boolean commitPreRebootStagedFiles(
279             in List<com.android.server.art.ArtifactsPath> artifacts,
280             in List<com.android.server.art.ProfilePath.WritableProfilePath> profiles);
281 
282     /**
283      * Returns whether the old system and the new system meet the requirements to run Pre-reboot
284      * Dexopt. This method can only be called with a chroot dir set up by
285      * {@link IDexoptChrootSetup#setUp}.
286      *
287      * Not supported in Pre-reboot Dexopt mode.
288      *
289      * Throws fatal and non-fatal errors.
290      */
checkPreRebootSystemRequirements(@tf8InCpp String chrootDir)291     boolean checkPreRebootSystemRequirements(@utf8InCpp String chrootDir);
292 
293     // The methods below are only for Pre-reboot Dexopt and only supported in Pre-reboot Dexopt
294     // mode.
295 
296     /**
297      * Initializes the environment for Pre-reboot Dexopt. This operation includes initializing
298      * environment variables and boot images. Returns true on success, or false on cancellation.
299      * Throws on failure.
300      *
301      * Note that this method results in a non-persistent state change, so it must be called every
302      * time a new instance of artd is started for Pre-reboot Dexopt.
303      *
304      * On the first call to this method, a cancellation signal must be passed through the {@code
305      * cancellationSignal} parameter. The cancellation signal can then be used for cancelling the
306      * first call. On subsequent calls to this method, the {@code cancellationSignal} parameter is
307      * ignored.
308      *
309      * After cancellation or failure, the environment will not be usable for Pre-reboot Dexopt, and
310      * this operation cannot be retried.
311      *
312      * Throws fatal and non-fatal errors.
313      */
preRebootInit( in @ullable com.android.server.art.IArtdCancellationSignal cancellationSignal)314     boolean preRebootInit(
315             in @nullable com.android.server.art.IArtdCancellationSignal cancellationSignal);
316 
317     /** For Pre-reboot Dexopt use. See {@link ArtJni#validateDexPath}. */
validateDexPath(@tf8InCpp String dexFile)318     @nullable @utf8InCpp String validateDexPath(@utf8InCpp String dexFile);
319 
320     /** For Pre-reboot Dexopt use. See {@link ArtJni#validateClassLoaderContext}. */
validateClassLoaderContext( @tf8InCpp String dexFile, @utf8InCpp String classLoaderContext)321     @nullable @utf8InCpp String validateClassLoaderContext(
322             @utf8InCpp String dexFile, @utf8InCpp String classLoaderContext);
323 }
324