| 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.OutputArtifacts.PermissionSettings; |
| 20 |
import static com.android.server.art.OutputArtifacts.PermissionSettings.SeContext; |
| 21 |
import static com.android.server.art.PrimaryDexUtils.DetailedPrimaryDexInfo; |
| 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 |
import android.os.Process; |
| 30 |
import android.os.RemoteException; |
| 31 |
import android.os.ServiceSpecificException; |
| 32 |
import android.os.UserHandle; |
| 33 |
import android.text.TextUtils; |
| 34 |
import android.util.Log; |
| 35 |
|
| 36 |
import androidx.annotation.RequiresApi; |
| 37 |
|
| 38 |
import com.android.internal.annotations.VisibleForTesting; |
| 39 |
import com.android.modules.utils.pm.PackageStateModulesUtils; |
| 40 |
import com.android.server.art.model.ArtFlags; |
| 41 |
import com.android.server.art.model.DexoptParams; |
| 42 |
import com.android.server.art.model.DexoptResult; |
| 43 |
import com.android.server.pm.PackageManagerLocal; |
| 44 |
import com.android.server.pm.pkg.AndroidPackage; |
| 45 |
import com.android.server.pm.pkg.PackageState; |
| 46 |
|
| 47 |
import dalvik.system.DexFile; |
| 48 |
|
| 49 |
import com.google.auto.value.AutoValue; |
| 50 |
|
| 51 |
import java.util.ArrayList; |
| 52 |
import java.util.List; |
| 53 |
import java.util.Objects; |
| 54 |
|
| 55 |
/** @hide */ |
| 56 |
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
| 57 |
public class PrimaryDexopter extends Dexopter<DetailedPrimaryDexInfo> { |
| 58 |
private static final String TAG = ArtManagerLocal.TAG; |
| 59 |
|
| 60 |
private final int mSharedGid; |
| 61 |
|
| 62 |
public PrimaryDexopter(@NonNull Context context, @NonNull PackageState pkgState, |
| 63 |
@NonNull AndroidPackage pkg, @NonNull DexoptParams params, |
| 64 |
@NonNull CancellationSignal cancellationSignal) { |
| 65 |
this(new Injector(context), pkgState, pkg, params, cancellationSignal); |
| 66 |
} |
| 67 |
|
| 68 |
@VisibleForTesting |
| 69 |
public PrimaryDexopter(@NonNull Injector injector, @NonNull PackageState pkgState, |
| 70 |
@NonNull AndroidPackage pkg, @NonNull DexoptParams params, |
| 71 |
@NonNull CancellationSignal cancellationSignal) { |
| 72 |
super(injector, pkgState, pkg, params, cancellationSignal); |
| 73 |
|
| 74 |
mSharedGid = UserHandle.getSharedAppGid(pkgState.getAppId()); |
| 75 |
if (mSharedGid < 0) { |
| 76 |
throw new IllegalStateException( |
| 77 |
String.format("Unable to get shared gid for package '%s' (app ID: %d)", |
| 78 |
pkgState.getPackageName(), pkgState.getAppId())); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
protected boolean isInDalvikCache() throws RemoteException { |
| 84 |
return Utils.isInDalvikCache(mPkgState, mInjector.getArtd()); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
@NonNull |
| 89 |
protected List<DetailedPrimaryDexInfo> getDexInfoList() { |
| 90 |
return PrimaryDexUtils.getDetailedDexInfo(mPkgState, mPkg); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
protected boolean isDexoptable(@NonNull DetailedPrimaryDexInfo dexInfo) { |
| 95 |
if (!dexInfo.hasCode()) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
if ((mParams.getFlags() & ArtFlags.FLAG_FOR_SINGLE_SPLIT) != 0) { |
| 99 |
return Objects.equals(mParams.getSplitName(), dexInfo.splitName()); |
| 100 |
} |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
protected boolean needsToBeShared(@NonNull DetailedPrimaryDexInfo dexInfo) { |
| 106 |
return isSharedLibrary() |
| 107 |
|| mInjector.getDexUseManager().isPrimaryDexUsedByOtherApps( |
| 108 |
mPkgState.getPackageName(), dexInfo.dexPath()); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
protected boolean isDexFilePublic(@NonNull DetailedPrimaryDexInfo dexInfo) { |
| 113 |
// The filesystem permission of a primary dex file always has the S_IROTH bit. In practice, |
| 114 |
// the accessibility is enforced by Application Sandbox, not filesystem permission. |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
@NonNull |
| 120 |
protected List<ProfilePath> getExternalProfiles(@NonNull DetailedPrimaryDexInfo dexInfo) { |
| 121 |
return PrimaryDexUtils.getExternalProfiles(dexInfo); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
@NonNull |
| 126 |
protected PermissionSettings getPermissionSettings( |
| 127 |
@NonNull DetailedPrimaryDexInfo dexInfo, boolean canBePublic) { |
| 128 |
// The files and directories should belong to the system so that Package Manager can manage |
| 129 |
// them (e.g., move them around). |
| 130 |
// We don't need the "read" bit for "others" on the directories because others only need to |
| 131 |
// access the files in the directories, but they don't need to "ls" the directories. |
| 132 |
FsPermission dirFsPermission = AidlUtils.buildFsPermission(Process.SYSTEM_UID /* uid */, |
| 133 |
Process.SYSTEM_UID /* gid */, false /* isOtherReadable */, |
| 134 |
true /* isOtherExecutable */); |
| 135 |
FsPermission fileFsPermission = AidlUtils.buildFsPermission( |
| 136 |
Process.SYSTEM_UID /* uid */, mSharedGid /* gid */, canBePublic); |
| 137 |
// For primary dex, we can use the default SELinux context. |
| 138 |
SeContext seContext = null; |
| 139 |
return AidlUtils.buildPermissionSettings(dirFsPermission, fileFsPermission, seContext); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
@NonNull |
| 144 |
protected List<Abi> getAllAbis(@NonNull DetailedPrimaryDexInfo dexInfo) { |
| 145 |
return Utils.getAllAbis(mPkgState); |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
@NonNull |
| 150 |
protected ProfilePath buildRefProfilePath(@NonNull DetailedPrimaryDexInfo dexInfo) { |
| 151 |
return PrimaryDexUtils.buildRefProfilePath(mPkgState, dexInfo); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
protected boolean isAppImageAllowed(@NonNull DetailedPrimaryDexInfo dexInfo) { |
| 156 |
// Only allow app image for the base APK because having multiple app images is not |
| 157 |
// supported. |
| 158 |
// Additionally, disable app images if the app requests for the splits to be loaded in |
| 159 |
// isolation because app images are unsupported for multiple class loaders (b/72696798). |
| 160 |
// TODO(jiakaiz): Investigate whether this is still the best choice today. |
| 161 |
return dexInfo.splitName() == null && !PrimaryDexUtils.isIsolatedSplitLoading(mPkg); |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
@NonNull |
| 166 |
protected OutputProfile buildOutputProfile( |
| 167 |
@NonNull DetailedPrimaryDexInfo dexInfo, boolean isPublic) { |
| 168 |
return PrimaryDexUtils.buildOutputProfile( |
| 169 |
mPkgState, dexInfo, Process.SYSTEM_UID, mSharedGid, isPublic); |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
@NonNull |
| 174 |
protected List<ProfilePath> getCurProfiles(@NonNull DetailedPrimaryDexInfo dexInfo) { |
| 175 |
return PrimaryDexUtils.getCurProfiles(mInjector.getUserManager(), mPkgState, dexInfo); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
@Nullable |
| 180 |
protected DexMetadataPath buildDmPath(@NonNull DetailedPrimaryDexInfo dexInfo) { |
| 181 |
return AidlUtils.buildDexMetadataPath(dexInfo.dexPath()); |
| 182 |
} |
| 183 |
|
| 184 |
private boolean isSharedLibrary() { |
| 185 |
return PackageStateModulesUtils.isLoadableInOtherProcesses(mPkgState, true /* codeOnly */); |
| 186 |
} |
| 187 |
} |