Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1566e17e authored by John Wu's avatar John Wu
Browse files

[HostStubGen] Multiple improvements

1. Consolidate SubstituteAnd[Stub/Keep] into a single Substitute policy.
   There is little reason to support SubstituteKeep, so make Substitute
   implicitly Stub. In production, methods are never annotated with both
   Stub/Keep + Substitute anyways.
2. Update ClassFilter to support nested classes.
3. Rewrite AnnotationFilter with the following improvements:
   - Previously, when a method is annotated as Substitute, the method
     itself will get the Remove policy, and the replacement method will
     get the Stub/Keep policy. In the new implementation, the annotated
     method will preserve its Substitute policy, which will not only be
     useful for future improvements, but also be consistent with how
     text policy filter is implemented.
   - Process the entire class all at once and cache its results; the
     previous implementation re-run the same operations multiple times.
   - Previously, the annotation allowlist was not properly enforced when
     Substitute annotation is used.

Bug: 292141694
Flag: EXEMPT host test change only
Test: atest hoststubgen-test-tiny-test
Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh
Change-Id: Iee223c5d0814351eccdcfc884da64bc507af83b7
parent 10bf2e16
Loading
Loading
Loading
Loading
+1 −30
Original line number Diff line number Diff line
@@ -9,17 +9,13 @@ com.android.internal.logging.testing.FakeMetricsLogger
com.android.internal.logging.testing.UiEventLoggerFake
com.android.internal.os.AndroidPrintStream
com.android.internal.os.BatteryStatsHistory
com.android.internal.os.BatteryStatsHistory$TraceDelegate
com.android.internal.os.BatteryStatsHistory$VarintParceler
com.android.internal.os.BatteryStatsHistoryIterator
com.android.internal.os.Clock
com.android.internal.os.LongArrayMultiStateCounter
com.android.internal.os.LongArrayMultiStateCounter$LongArrayContainer
com.android.internal.os.LongMultiStateCounter
com.android.internal.os.MonotonicClock
com.android.internal.os.PowerProfile
com.android.internal.os.PowerStats
com.android.internal.os.PowerStats$Descriptor
com.android.internal.os.RuntimeInit
com.android.internal.power.EnergyConsumerStats
com.android.internal.power.ModemPowerProfile
@@ -123,15 +119,9 @@ android.os.BadTypeParcelableException
android.os.BaseBundle
android.os.BatteryConsumer
android.os.BatteryStats
android.os.BatteryStats$HistoryItem
android.os.BatteryStats$HistoryStepDetails
android.os.BatteryStats$HistoryTag
android.os.BatteryStats$LongCounter
android.os.BatteryStats$ProcessStateChange
android.os.BatteryUsageStats
android.os.BatteryUsageStatsQuery
android.os.Binder
android.os.Binder$IdentitySupplier
android.os.BluetoothBatteryStats
android.os.Broadcaster
android.os.Build
@@ -142,7 +132,6 @@ android.os.ConditionVariable
android.os.DeadObjectException
android.os.DeadSystemException
android.os.FileUtils
android.os.FileUtils$MemoryPipe
android.os.Handler
android.os.HandlerExecutor
android.os.HandlerThread
@@ -154,8 +143,6 @@ android.os.MessageQueue
android.os.PackageTagsList
android.os.Parcel
android.os.ParcelFileDescriptor
android.os.ParcelFileDescriptor$AutoCloseInputStream
android.os.ParcelFileDescriptor$AutoCloseOutputStream
android.os.ParcelFormatException
android.os.ParcelUuid
android.os.Parcelable
@@ -169,7 +156,6 @@ android.os.RemoteCallbackList
android.os.RemoteException
android.os.ResultReceiver
android.os.ServiceManager
android.os.ServiceManager$ServiceNotFoundException
android.os.ServiceSpecificException
android.os.StrictMode
android.os.SystemClock
@@ -180,18 +166,14 @@ android.os.TimestampedValue
android.os.Trace
android.os.TransactionTooLargeException
android.os.UserBatteryConsumer
android.os.UserBatteryConsumer$Builder
android.os.UidBatteryConsumer
android.os.UidBatteryConsumer$Builder
android.os.UserHandle
android.os.UserManager
android.os.VibrationAttributes
android.os.VibrationAttributes$Builder
android.os.WakeLockStats
android.os.WorkSource

android.content.ClipData
android.content.ClipData$Item
android.content.ClipDescription
android.content.ClipboardManager
android.content.ComponentName
@@ -208,11 +190,7 @@ android.content.pm.ApplicationInfo
android.content.pm.ComponentInfo
android.content.pm.PackageInfo
android.content.pm.PackageItemInfo
android.content.pm.PackageManager$Flags
android.content.pm.PackageManager$PackageInfoFlags
android.content.pm.PackageManager$ApplicationInfoFlags
android.content.pm.PackageManager$ComponentInfoFlags
android.content.pm.PackageManager$ResolveInfoFlags
android.content.pm.PackageManager
android.content.pm.PathPermission
android.content.pm.ProviderInfo
android.content.pm.ResolveInfo
@@ -223,7 +201,6 @@ android.content.pm.UserInfo
android.content.res.ApkAssets
android.content.res.AssetFileDescriptor
android.content.res.AssetManager
android.content.res.AssetManager$Builder
android.content.res.ColorStateList
android.content.res.ConfigurationBoundResourceCache
android.content.res.Configuration
@@ -237,7 +214,6 @@ android.content.res.FontScaleConverter
android.content.res.FontScaleConverterImpl
android.content.res.FontScaleConverterFactory
android.content.res.Resources
android.content.res.Resources$Theme
android.content.res.ResourceId
android.content.res.ResourcesImpl
android.content.res.ResourcesKey
@@ -260,7 +236,6 @@ android.database.CursorWrapper
android.database.DataSetObservable
android.database.DataSetObserver
android.database.MatrixCursor
android.database.MatrixCursor$RowBuilder
android.database.MergeCursor
android.database.Observable
android.database.SQLException
@@ -268,7 +243,6 @@ android.database.sqlite.SQLiteClosable
android.database.sqlite.SQLiteException

android.text.TextUtils
android.text.TextUtils$SimpleStringSplitter

android.accounts.Account

@@ -298,14 +272,11 @@ android.app.ComponentOptions
android.app.Instrumentation
android.app.LocaleConfig
android.app.ResourcesManager
android.app.ResourcesManager$UpdateHandler
android.app.WindowConfiguration

android.metrics.LogMaker

android.view.Display
android.view.Display$HdrCapabilities
android.view.Display$Mode
android.view.DisplayAdjustments
android.view.DisplayInfo
android.view.inputmethod.InputBinding
+199 −290

File changed.

Preview size limit exceeded, changes collapsed.

+11 −20
Original line number Diff line number Diff line
@@ -25,24 +25,15 @@ import com.android.hoststubgen.HostStubGenInternalException
 *
 * @param policy the policy. Cannot be a "substitute" policy.
 */
class ConstantFilter(
        policy: FilterPolicy,
        val reason: String
) : OutputFilter() {
    val classPolicy: FilterPolicy
    val fieldPolicy: FilterPolicy
    val methodPolicy: FilterPolicy
class ConstantFilter(policy: FilterPolicy, private val reason: String) : OutputFilter() {

    private val classPolicy: FilterPolicy
    private val fieldPolicy: FilterPolicy
    private val methodPolicy: FilterPolicy

    init {
        if (policy.isSubstitute) {
            throw HostStubGenInternalException(
                    "ConstantFilter doesn't allow substitution policies.")
        }
        if (policy.isClassWidePolicy) {
            // We prevent it, because there's no point in using class-wide policies because
            // all members get othe same policy too anyway.
            throw HostStubGenInternalException(
                    "ConstantFilter doesn't allow class-wide policies.")
        if (!policy.isUsableWithDefault) {
            throw HostStubGenInternalException("ConstantFilter doesn't support $policy.")
        }
        methodPolicy = policy

+14 −21
Original line number Diff line number Diff line
@@ -40,14 +40,9 @@ enum class FilterPolicy {
    KeepClass,

    /**
     * Same as [Stub], but replace it with a "substitution" method. Only usable with methods.
     * Only usable with methods. Replace a method with a "substitution" method.
     */
    SubstituteAndStub,

    /**
     * Same as [Keep], but replace it with a "substitution" method. Only usable with methods.
     */
    SubstituteAndKeep,
    Substitute,

    /**
     * Only usable with methods. The item will be kept in the impl jar file, but when called,
@@ -57,7 +52,7 @@ enum class FilterPolicy {

    /**
     * Only usable with methods. The item will be kept in the impl jar file, but when called,
     * it'll no-op.  Currently only supported for methods returning `void`.
     * it'll no-op.
     */
    Ignore,

@@ -66,11 +61,8 @@ enum class FilterPolicy {
     */
    Remove;

    val isSubstitute: Boolean
        get() = this == SubstituteAndStub || this == SubstituteAndKeep

    val needsInStub: Boolean
        get() = this == Stub || this == StubClass || this == SubstituteAndStub || this == Ignore
        get() = this == Stub || this == StubClass || this == Substitute || this == Ignore

    val needsInImpl: Boolean
        get() = this != Remove
@@ -102,6 +94,15 @@ enum class FilterPolicy {
            }
        }

    /** Returns whether a policy can be used as default policy. */
    val isUsableWithDefault: Boolean
        get() {
            return when (this) {
                Stub, Keep, Throw, Remove -> true
                else -> false
            }
        }

    /** Returns whether a policy is a class-wide one. */
    val isClassWidePolicy: Boolean
        get() {
@@ -116,19 +117,11 @@ enum class FilterPolicy {
        get() {
            return when (this) {
                // TODO: handle native method with no substitution as being unsupported
                Stub, StubClass, Keep, KeepClass, SubstituteAndStub, SubstituteAndKeep -> true
                Stub, StubClass, Keep, KeepClass, Substitute -> true
                else -> false
            }
        }

    fun getSubstitutionBasePolicy(): FilterPolicy {
        return when (this) {
            SubstituteAndKeep -> Keep
            SubstituteAndStub -> Stub
            else -> this
        }
    }

    /**
     * Convert {Stub,Keep}Class to the corresponding Stub or Keep.
     */
+4 −7
Original line number Diff line number Diff line
@@ -240,7 +240,7 @@ fun createFilterFromTextPolicyFile(

                            imf.setPolicyForMethod(className, name, signature,
                                    policy.withReason(FILTER_REASON))
                            if (policy.isSubstitute) {
                            if (policy == FilterPolicy.Substitute) {
                                val fromName = fields[3].substring(1)

                                if (fromName == name) {
@@ -250,8 +250,7 @@ fun createFilterFromTextPolicyFile(

                                // Set the policy for the "from" method.
                                imf.setPolicyForMethod(className, fromName, signature,
                                        policy.getSubstitutionBasePolicy()
                                                .withReason(FILTER_REASON))
                                    FilterPolicy.Stub.withReason(FILTER_REASON))

                                val classAndMethod = splitWithLastPeriod(fromName)
                                if (classAndMethod != null) {
@@ -355,9 +354,7 @@ private fun parsePolicy(s: String): FilterPolicy {
        "i", "ignore" -> FilterPolicy.Ignore
        else -> {
            if (s.startsWith("@")) {
                FilterPolicy.SubstituteAndStub
            } else if (s.startsWith("%")) {
                FilterPolicy.SubstituteAndKeep
                FilterPolicy.Substitute
            } else {
                throw ParseException("Invalid policy \"$s\"")
            }
Loading