Debian Codegraph @

libartservice/service/java/com/android/server/art/SecondaryDexopter.java

raw
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 package com.android.server.art;
18
19 import static com.android.server.art.DexUseManagerLocal.DetailedSecondaryDexInfo;
20 import static com.android.server.art.OutputArtifacts.PermissionSettings;
21 import static com.android.server.art.OutputArtifacts.PermissionSettings.SeContext;
22 import static com.android.server.art.Utils.Abi;
23
24 import android.annotation.NonNull;
25 import android.annotation.Nullable;
26 import android.content.Context;
27 import android.os.Build;
28 import android.os.CancellationSignal;
29
30 import androidx.annotation.RequiresApi;
31
32 import com.android.internal.annotations.VisibleForTesting;
33 import com.android.server.art.model.DexoptParams;
34 import com.android.server.pm.pkg.AndroidPackage;
35 import com.android.server.pm.pkg.PackageState;
36
37 import java.util.List;
38
39 /** @hide */
40 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
41 public class SecondaryDexopter extends Dexopter<DetailedSecondaryDexInfo> {
42 private static final String TAG = ArtManagerLocal.TAG;
43
44 public SecondaryDexopter(@NonNull Context context, @NonNull PackageState pkgState,
45 @NonNull AndroidPackage pkg, @NonNull DexoptParams params,
46 @NonNull CancellationSignal cancellationSignal) {
47 this(new Injector(context), pkgState, pkg, params, cancellationSignal);
48 }
49
50 @VisibleForTesting
51 public SecondaryDexopter(@NonNull Injector injector, @NonNull PackageState pkgState,
52 @NonNull AndroidPackage pkg, @NonNull DexoptParams params,
53 @NonNull CancellationSignal cancellationSignal) {
54 super(injector, pkgState, pkg, params, cancellationSignal);
55 }
56
57 @Override
58 protected boolean isInDalvikCache() {
59 // A secondary dex file is added by the app, so it's always in a writable location and hence
60 // never uses dalvik-cache.
61 return false;
62 }
63
64 @Override
65 @NonNull
66 protected List<DetailedSecondaryDexInfo> getDexInfoList() {
67 return mInjector.getDexUseManager().getFilteredDetailedSecondaryDexInfo(
68 mPkgState.getPackageName());
69 }
70
71 @Override
72 protected boolean isDexoptable(@NonNull DetailedSecondaryDexInfo dexInfo) {
73 return true;
74 }
75
76 @Override
77 protected boolean needsToBeShared(@NonNull DetailedSecondaryDexInfo dexInfo) {
78 return dexInfo.isUsedByOtherApps();
79 }
80
81 @Override
82 protected boolean isDexFilePublic(@NonNull DetailedSecondaryDexInfo dexInfo) {
83 return dexInfo.isDexFilePublic();
84 }
85
86 @Override
87 @NonNull
88 protected List<ProfilePath> getExternalProfiles(@NonNull DetailedSecondaryDexInfo dexInfo) {
89 // A secondary dex file doesn't have any external profile to use.
90 return List.of();
91 }
92
93 @Override
94 @NonNull
95 protected PermissionSettings getPermissionSettings(
96 @NonNull DetailedSecondaryDexInfo dexInfo, boolean canBePublic) {
97 int uid = getUid(dexInfo);
98 // We need the "execute" bit for "others" even though `canBePublic` is false because the
99 // directory can contain other artifacts that needs to be public.
100 // We don't need the "read" bit for "others" on the directories because others only need to
101 // access the files in the directories, but they don't need to "ls" the directories.
102 FsPermission dirFsPermission = AidlUtils.buildFsPermission(uid /* uid */, uid /* gid */,
103 false /* isOtherReadable */, true /* isOtherExecutable */);
104 FsPermission fileFsPermission =
105 AidlUtils.buildFsPermission(uid /* uid */, uid /* gid */, canBePublic);
106 SeContext seContext = AidlUtils.buildSeContext(mPkgState.getSeInfo(), uid);
107 return AidlUtils.buildPermissionSettings(dirFsPermission, fileFsPermission, seContext);
108 }
109
110 @Override
111 @NonNull
112 protected List<Abi> getAllAbis(@NonNull DetailedSecondaryDexInfo dexInfo) {
113 return Utils.getAllAbisForNames(dexInfo.abiNames(), mPkgState);
114 }
115
116 @Override
117 @NonNull
118 protected ProfilePath buildRefProfilePath(@NonNull DetailedSecondaryDexInfo dexInfo) {
119 return AidlUtils.buildProfilePathForSecondaryRef(dexInfo.dexPath());
120 }
121
122 @Override
123 protected boolean isAppImageAllowed(@NonNull DetailedSecondaryDexInfo dexInfo) {
124 // The runtime can only load the app image of the base APK.
125 return false;
126 }
127
128 @Override
129 @NonNull
130 protected OutputProfile buildOutputProfile(
131 @NonNull DetailedSecondaryDexInfo dexInfo, boolean isPublic) {
132 int uid = getUid(dexInfo);
133 return AidlUtils.buildOutputProfileForSecondary(dexInfo.dexPath(), uid, uid, isPublic);
134 }
135
136 @Override
137 @NonNull
138 protected List<ProfilePath> getCurProfiles(@NonNull DetailedSecondaryDexInfo dexInfo) {
139 // A secondary dex file can only be loaded by one user, so there is only one profile.
140 return List.of(AidlUtils.buildProfilePathForSecondaryCur(dexInfo.dexPath()));
141 }
142
143 @Override
144 @Nullable
145 protected DexMetadataPath buildDmPath(@NonNull DetailedSecondaryDexInfo dexInfo) {
146 return null;
147 }
148
149 private int getUid(@NonNull DetailedSecondaryDexInfo dexInfo) {
150 return dexInfo.userHandle().getUid(mPkgState.getAppId());
151 }
152 }