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.ProfilePath.PrebuiltProfilePath; 22 import static com.android.server.art.ProfilePath.PrimaryCurProfilePath; 23 import static com.android.server.art.ProfilePath.PrimaryRefProfilePath; 24 import static com.android.server.art.ProfilePath.SecondaryCurProfilePath; 25 import static com.android.server.art.ProfilePath.SecondaryRefProfilePath; 26 import static com.android.server.art.ProfilePath.TmpProfilePath; 27 import static com.android.server.art.ProfilePath.WritableProfilePath; 28 29 import android.annotation.NonNull; 30 import android.annotation.Nullable; 31 32 /** @hide */ 33 public final class AidlUtils { AidlUtils()34 private AidlUtils() {} 35 36 @NonNull buildArtifactsPath( @onNull String dexPath, @NonNull String isa, boolean isInDalvikCache)37 public static ArtifactsPath buildArtifactsPath( 38 @NonNull String dexPath, @NonNull String isa, boolean isInDalvikCache) { 39 var artifactsPath = new ArtifactsPath(); 40 artifactsPath.dexPath = dexPath; 41 artifactsPath.isa = isa; 42 artifactsPath.isInDalvikCache = isInDalvikCache; 43 return artifactsPath; 44 } 45 46 @NonNull buildFsPermission( int uid, int gid, boolean isOtherReadable, boolean isOtherExecutable)47 public static FsPermission buildFsPermission( 48 int uid, int gid, boolean isOtherReadable, boolean isOtherExecutable) { 49 var fsPermission = new FsPermission(); 50 fsPermission.uid = uid; 51 fsPermission.gid = gid; 52 fsPermission.isOtherReadable = isOtherReadable; 53 fsPermission.isOtherExecutable = isOtherExecutable; 54 return fsPermission; 55 } 56 57 @NonNull buildFsPermission(int uid, int gid, boolean isOtherReadable)58 public static FsPermission buildFsPermission(int uid, int gid, boolean isOtherReadable) { 59 return buildFsPermission(uid, gid, isOtherReadable, false /* isOtherExecutable */); 60 } 61 62 @NonNull buildDexMetadataPath(@onNull String dexPath)63 public static DexMetadataPath buildDexMetadataPath(@NonNull String dexPath) { 64 var dexMetadataPath = new DexMetadataPath(); 65 dexMetadataPath.dexPath = dexPath; 66 return dexMetadataPath; 67 } 68 69 @NonNull buildPermissionSettings(@onNull FsPermission dirFsPermission, @NonNull FsPermission fileFsPermission, @Nullable SeContext seContext)70 public static PermissionSettings buildPermissionSettings(@NonNull FsPermission dirFsPermission, 71 @NonNull FsPermission fileFsPermission, @Nullable SeContext seContext) { 72 var permissionSettings = new PermissionSettings(); 73 permissionSettings.dirFsPermission = dirFsPermission; 74 permissionSettings.fileFsPermission = fileFsPermission; 75 permissionSettings.seContext = seContext; 76 return permissionSettings; 77 } 78 79 @NonNull buildOutputArtifacts(@onNull String dexPath, @NonNull String isa, boolean isInDalvikCache, @NonNull PermissionSettings permissionSettings)80 public static OutputArtifacts buildOutputArtifacts(@NonNull String dexPath, @NonNull String isa, 81 boolean isInDalvikCache, @NonNull PermissionSettings permissionSettings) { 82 var outputArtifacts = new OutputArtifacts(); 83 outputArtifacts.artifactsPath = buildArtifactsPath(dexPath, isa, isInDalvikCache); 84 outputArtifacts.permissionSettings = permissionSettings; 85 return outputArtifacts; 86 } 87 88 @NonNull buildPrimaryRefProfilePath( @onNull String packageName, @NonNull String profileName)89 public static PrimaryRefProfilePath buildPrimaryRefProfilePath( 90 @NonNull String packageName, @NonNull String profileName) { 91 var primaryRefProfilePath = new PrimaryRefProfilePath(); 92 primaryRefProfilePath.packageName = packageName; 93 primaryRefProfilePath.profileName = profileName; 94 return primaryRefProfilePath; 95 } 96 97 @NonNull buildSecondaryRefProfilePath(@onNull String dexPath)98 public static SecondaryRefProfilePath buildSecondaryRefProfilePath(@NonNull String dexPath) { 99 var secondaryRefProfilePath = new SecondaryRefProfilePath(); 100 secondaryRefProfilePath.dexPath = dexPath; 101 return secondaryRefProfilePath; 102 } 103 104 @NonNull buildProfilePathForPrimaryRef( @onNull String packageName, @NonNull String profileName)105 public static ProfilePath buildProfilePathForPrimaryRef( 106 @NonNull String packageName, @NonNull String profileName) { 107 return ProfilePath.primaryRefProfilePath( 108 buildPrimaryRefProfilePath(packageName, profileName)); 109 } 110 111 @NonNull buildProfilePathForPrebuilt(@onNull String dexPath)112 public static ProfilePath buildProfilePathForPrebuilt(@NonNull String dexPath) { 113 var prebuiltProfilePath = new PrebuiltProfilePath(); 114 prebuiltProfilePath.dexPath = dexPath; 115 return ProfilePath.prebuiltProfilePath(prebuiltProfilePath); 116 } 117 118 @NonNull buildProfilePathForDm(@onNull String dexPath)119 public static ProfilePath buildProfilePathForDm(@NonNull String dexPath) { 120 return ProfilePath.dexMetadataPath(buildDexMetadataPath(dexPath)); 121 } 122 123 @NonNull buildProfilePathForPrimaryCur( int userId, @NonNull String packageName, @NonNull String profileName)124 public static ProfilePath buildProfilePathForPrimaryCur( 125 int userId, @NonNull String packageName, @NonNull String profileName) { 126 var primaryCurProfilePath = new PrimaryCurProfilePath(); 127 primaryCurProfilePath.userId = userId; 128 primaryCurProfilePath.packageName = packageName; 129 primaryCurProfilePath.profileName = profileName; 130 return ProfilePath.primaryCurProfilePath(primaryCurProfilePath); 131 } 132 133 @NonNull buildProfilePathForSecondaryRef(@onNull String dexPath)134 public static ProfilePath buildProfilePathForSecondaryRef(@NonNull String dexPath) { 135 return ProfilePath.secondaryRefProfilePath(buildSecondaryRefProfilePath(dexPath)); 136 } 137 138 @NonNull buildProfilePathForSecondaryCur(@onNull String dexPath)139 public static ProfilePath buildProfilePathForSecondaryCur(@NonNull String dexPath) { 140 var secondaryCurProfilePath = new SecondaryCurProfilePath(); 141 secondaryCurProfilePath.dexPath = dexPath; 142 return ProfilePath.secondaryCurProfilePath(secondaryCurProfilePath); 143 } 144 145 @NonNull buildOutputProfile( @onNull WritableProfilePath finalPath, int uid, int gid, boolean isPublic)146 private static OutputProfile buildOutputProfile( 147 @NonNull WritableProfilePath finalPath, int uid, int gid, boolean isPublic) { 148 var outputProfile = new OutputProfile(); 149 outputProfile.profilePath = new TmpProfilePath(); 150 outputProfile.profilePath.finalPath = finalPath; 151 outputProfile.profilePath.id = ""; // Will be filled by artd. 152 outputProfile.profilePath.tmpPath = ""; // Will be filled by artd. 153 outputProfile.fsPermission = buildFsPermission(uid, gid, isPublic); 154 return outputProfile; 155 } 156 157 @NonNull buildOutputProfileForPrimary(@onNull String packageName, @NonNull String profileName, int uid, int gid, boolean isPublic)158 public static OutputProfile buildOutputProfileForPrimary(@NonNull String packageName, 159 @NonNull String profileName, int uid, int gid, boolean isPublic) { 160 return buildOutputProfile(WritableProfilePath.forPrimary( 161 buildPrimaryRefProfilePath(packageName, profileName)), 162 uid, gid, isPublic); 163 } 164 165 @NonNull buildOutputProfileForSecondary( @onNull String dexPath, int uid, int gid, boolean isPublic)166 public static OutputProfile buildOutputProfileForSecondary( 167 @NonNull String dexPath, int uid, int gid, boolean isPublic) { 168 return buildOutputProfile( 169 WritableProfilePath.forSecondary(buildSecondaryRefProfilePath(dexPath)), uid, gid, 170 isPublic); 171 } 172 173 @NonNull buildSeContext(@onNull String seInfo, int uid)174 public static SeContext buildSeContext(@NonNull String seInfo, int uid) { 175 var seContext = new SeContext(); 176 seContext.seInfo = seInfo; 177 seContext.uid = uid; 178 return seContext; 179 } 180 181 @NonNull toString(@onNull PrimaryRefProfilePath profile)182 public static String toString(@NonNull PrimaryRefProfilePath profile) { 183 return String.format("PrimaryRefProfilePath[packageName = %s, profileName = %s]", 184 profile.packageName, profile.profileName); 185 } 186 187 @NonNull toString(@onNull SecondaryRefProfilePath profile)188 public static String toString(@NonNull SecondaryRefProfilePath profile) { 189 return String.format("SecondaryRefProfilePath[dexPath = %s]", profile.dexPath); 190 } 191 192 @NonNull toString(@onNull PrebuiltProfilePath profile)193 public static String toString(@NonNull PrebuiltProfilePath profile) { 194 return String.format("PrebuiltProfilePath[dexPath = %s]", profile.dexPath); 195 } 196 197 @NonNull toString(@onNull DexMetadataPath profile)198 public static String toString(@NonNull DexMetadataPath profile) { 199 return String.format("DexMetadataPath[dexPath = %s]", profile.dexPath); 200 } 201 202 @NonNull toString(@onNull WritableProfilePath profile)203 public static String toString(@NonNull WritableProfilePath profile) { 204 switch (profile.getTag()) { 205 case WritableProfilePath.forPrimary: 206 return toString(profile.getForPrimary()); 207 case WritableProfilePath.forSecondary: 208 return toString(profile.getForSecondary()); 209 default: 210 throw new IllegalStateException( 211 "Unknown WritableProfilePath tag " + profile.getTag()); 212 } 213 } 214 215 @NonNull toString(@onNull ProfilePath profile)216 public static String toString(@NonNull ProfilePath profile) { 217 switch (profile.getTag()) { 218 case ProfilePath.primaryRefProfilePath: 219 return toString(profile.getPrimaryRefProfilePath()); 220 case ProfilePath.secondaryRefProfilePath: 221 return toString(profile.getSecondaryRefProfilePath()); 222 case ProfilePath.prebuiltProfilePath: 223 return toString(profile.getPrebuiltProfilePath()); 224 case ProfilePath.dexMetadataPath: 225 return toString(profile.getDexMetadataPath()); 226 default: 227 throw new UnsupportedOperationException( 228 "Only a subset of profile paths are supported to be converted to string, " 229 + "got " + profile.getTag()); 230 } 231 } 232 } 233