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

Commit 5a3654c3 authored by John Wu's avatar John Wu Committed by Android (Google) Code Review
Browse files

Merge "[Ravenwood] Use native Parcel and Binder implementation" into main

parents 7df847de 43cd5945
Loading
Loading
Loading
Loading
+5 −121
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.app.AppOpsManager;
import android.compat.annotation.UnsupportedAppUsage;
import android.ravenwood.annotation.RavenwoodClassLoadHook;
import android.ravenwood.annotation.RavenwoodKeepWholeClass;
import android.util.ExceptionUtils;
import android.util.Log;
import android.util.Slog;
@@ -30,11 +32,9 @@ import com.android.internal.os.BinderCallHeavyHitterWatcher;
import com.android.internal.os.BinderCallHeavyHitterWatcher.BinderCallHeavyHitterListener;
import com.android.internal.os.BinderInternal;
import com.android.internal.os.BinderInternal.CallSession;
import com.android.internal.os.SomeArgs;
import com.android.internal.util.FastPrintWriter;
import com.android.internal.util.FunctionalUtils.ThrowingRunnable;
import com.android.internal.util.FunctionalUtils.ThrowingSupplier;
import com.android.internal.util.Preconditions;

import dalvik.annotation.optimization.CriticalNative;

@@ -48,7 +48,6 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Modifier;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.function.Supplier;

/**
 * Base class for a remotable object, the core part of a lightweight
@@ -82,6 +81,8 @@ import java.util.function.Supplier;
 *
 * @see IBinder
 */
@RavenwoodKeepWholeClass
@RavenwoodClassLoadHook(RavenwoodClassLoadHook.LIBANDROID_LOADING_HOOK)
public class Binder implements IBinder {
    /*
     * Set this flag to true to detect anonymous, local or member classes
@@ -292,33 +293,6 @@ public class Binder implements IBinder {
        sWarnOnBlockingOnCurrentThread.set(sWarnOnBlocking);
    }

    private static volatile ThreadLocal<SomeArgs> sIdentity$ravenwood;

    @android.ravenwood.annotation.RavenwoodKeepWholeClass
    private static class IdentitySupplier implements Supplier<SomeArgs> {
        @Override
        public SomeArgs get() {
            final SomeArgs args = SomeArgs.obtain();
            // Match IPCThreadState behavior
            args.arg1 = Boolean.FALSE;
            args.argi1 = android.os.Process.myUid();
            args.argi2 = android.os.Process.myPid();
            return args;
        }
    }

    /** @hide */
    @android.ravenwood.annotation.RavenwoodKeep
    public static void init$ravenwood() {
        sIdentity$ravenwood = ThreadLocal.withInitial(new IdentitySupplier());
    }

    /** @hide */
    @android.ravenwood.annotation.RavenwoodKeep
    public static void reset$ravenwood() {
        sIdentity$ravenwood = null;
    }

    /**
     * Raw native pointer to JavaBBinderHolder object. Owned by this Java object. Not null.
     */
@@ -346,14 +320,8 @@ public class Binder implements IBinder {
     * 0 for a synchronous call.
     */
    @CriticalNative
    @android.ravenwood.annotation.RavenwoodReplace
    public static final native int getCallingPid();

    /** @hide */
    public static final int getCallingPid$ravenwood() {
        return Preconditions.requireNonNullViaRavenwoodRule(sIdentity$ravenwood).get().argi2;
    }

    /**
     * Return the Linux UID assigned to the process that sent you the
     * current transaction that is being processed. This UID can be used with
@@ -362,14 +330,8 @@ public class Binder implements IBinder {
     * incoming transaction, then its own UID is returned.
     */
    @CriticalNative
    @android.ravenwood.annotation.RavenwoodReplace
    public static final native int getCallingUid();

    /** @hide */
    public static final int getCallingUid$ravenwood() {
        return Preconditions.requireNonNullViaRavenwoodRule(sIdentity$ravenwood).get().argi1;
    }

    /**
     * Returns {@code true} if the current thread is currently executing an
     * incoming transaction.
@@ -377,21 +339,13 @@ public class Binder implements IBinder {
     * @hide
     */
    @CriticalNative
    @android.ravenwood.annotation.RavenwoodReplace
    public static final native boolean isDirectlyHandlingTransactionNative();

    /** @hide */
    public static final boolean isDirectlyHandlingTransactionNative$ravenwood() {
        // Ravenwood doesn't support IPC
        return false;
    }

    private static boolean sIsHandlingBinderTransaction = false;

    /**
     * @hide
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public static final boolean isDirectlyHandlingTransaction() {
        return sIsHandlingBinderTransaction || isDirectlyHandlingTransactionNative();
    }
@@ -400,7 +354,6 @@ public class Binder implements IBinder {
     * This is Test API which will be used to override output of isDirectlyHandlingTransactionNative
     * @hide
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public static void setIsDirectlyHandlingTransactionOverride(boolean isInTransaction) {
        sIsHandlingBinderTransaction = isInTransaction;
    }
@@ -412,15 +365,8 @@ public class Binder implements IBinder {
    * @hide
    */
    @CriticalNative
    @android.ravenwood.annotation.RavenwoodReplace
    private static native boolean hasExplicitIdentity();

    /** @hide */
    private static boolean hasExplicitIdentity$ravenwood() {
        return Preconditions.requireNonNullViaRavenwoodRule(sIdentity$ravenwood).get().arg1
                == Boolean.TRUE;
    }

    /**
     * Return the Linux UID assigned to the process that sent the transaction
     * currently being processed.
@@ -429,7 +375,6 @@ public class Binder implements IBinder {
     * executing an incoming transaction and the calling identity has not been
     * explicitly set with {@link #clearCallingIdentity()}
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public static final int getCallingUidOrThrow() {
        if (!isDirectlyHandlingTransaction() && !hasExplicitIdentity()) {
            throw new IllegalStateException(
@@ -491,26 +436,8 @@ public class Binder implements IBinder {
     * @see #restoreCallingIdentity(long)
     */
    @CriticalNative
    @android.ravenwood.annotation.RavenwoodReplace
    public static final native long clearCallingIdentity();

    /** @hide */
    public static final long clearCallingIdentity$ravenwood() {
        final SomeArgs args = Preconditions.requireNonNullViaRavenwoodRule(
                sIdentity$ravenwood).get();
        long res = ((long) args.argi1 << 32) | args.argi2;
        if (args.arg1 == Boolean.TRUE) {
            res |= (0x1 << 30);
        } else {
            res &= ~(0x1 << 30);
        }
        // Match IPCThreadState behavior
        args.arg1 = Boolean.TRUE;
        args.argi1 = android.os.Process.myUid();
        args.argi2 = android.os.Process.myPid();
        return res;
    }

    /**
     * Restore the identity of the incoming IPC on the current thread
     * back to a previously identity that was returned by {@link
@@ -522,18 +449,8 @@ public class Binder implements IBinder {
     * @see #clearCallingIdentity
     */
    @CriticalNative
    @android.ravenwood.annotation.RavenwoodReplace
    public static final native void restoreCallingIdentity(long token);

    /** @hide */
    public static final void restoreCallingIdentity$ravenwood(long token) {
        final SomeArgs args = Preconditions.requireNonNullViaRavenwoodRule(
                sIdentity$ravenwood).get();
        args.arg1 = ((token & (0x1 << 30)) != 0) ? Boolean.TRUE : Boolean.FALSE;
        args.argi1 = (int) (token >> 32);
        args.argi2 = (int) (token & ~(0x1 << 30));
    }

    /**
     * Convenience method for running the provided action enclosed in
     * {@link #clearCallingIdentity}/{@link #restoreCallingIdentity}.
@@ -708,16 +625,9 @@ public class Binder implements IBinder {
     *
     * @hide
     */
    @android.ravenwood.annotation.RavenwoodReplace
    @SystemApi(client = SystemApi.Client.PRIVILEGED_APPS)
    public final native void markVintfStability();

    /** @hide */
    private void markVintfStability$ravenwood() {
        // This is not useful for Ravenwood which uses local binder.
        // TODO(b/361785059): Use real native libbinder.
    }

    /**
     * Use a VINTF-stability binder w/o VINTF requirements. Should be called
     * on a binder before it is sent out of process.
@@ -736,14 +646,8 @@ public class Binder implements IBinder {
     * in order to prevent the process from holding on to objects longer than
     * it needs to.
     */
    @android.ravenwood.annotation.RavenwoodReplace
    public static final native void flushPendingCommands();

    /** @hide */
    public static final void flushPendingCommands$ravenwood() {
        // Ravenwood doesn't support IPC; ignored
    }

    /**
     * Add the calling thread to the IPC thread pool. This function does
     * not return until the current process is exiting.
@@ -801,7 +705,6 @@ public class Binder implements IBinder {
     * <p>If you're creating a Binder token (a Binder object without an attached interface),
     * you should use {@link #Binder(String)} instead.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public Binder() {
        this(null);
    }
@@ -818,12 +721,9 @@ public class Binder implements IBinder {
     * Instead of creating multiple tokens with the same descriptor, consider adding a suffix to
     * help identify them.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public Binder(@Nullable String descriptor) {
        mObject = getNativeBBinderHolder();
        if (mObject != 0L) {
        NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mObject);
        }

        if (FIND_POTENTIAL_LEAKS) {
            final Class<? extends Binder> klass = getClass();
@@ -842,7 +742,6 @@ public class Binder implements IBinder {
     * will be implemented for you to return the given owner IInterface when
     * the corresponding descriptor is requested.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public void attachInterface(@Nullable IInterface owner, @Nullable String descriptor) {
        mOwner = owner;
        mDescriptor = descriptor;
@@ -851,7 +750,6 @@ public class Binder implements IBinder {
    /**
     * Default implementation returns an empty interface name.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public @Nullable String getInterfaceDescriptor() {
        return mDescriptor;
    }
@@ -860,7 +758,6 @@ public class Binder implements IBinder {
     * Default implementation always returns true -- if you got here,
     * the object is alive.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public boolean pingBinder() {
        return true;
    }
@@ -871,7 +768,6 @@ public class Binder implements IBinder {
     * Note that if you're calling on a local binder, this always returns true
     * because your process is alive if you're calling it.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public boolean isBinderAlive() {
        return true;
    }
@@ -881,7 +777,6 @@ public class Binder implements IBinder {
     * to return the associated {@link IInterface} if it matches the requested
     * descriptor.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public @Nullable IInterface queryLocalInterface(@NonNull String descriptor) {
        if (mDescriptor != null && mDescriptor.equals(descriptor)) {
            return mOwner;
@@ -1080,7 +975,6 @@ public class Binder implements IBinder {
     *
     * @hide
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public @Nullable String getTransactionName(int transactionCode) {
        return null;
    }
@@ -1089,7 +983,6 @@ public class Binder implements IBinder {
     * @hide
     */
    @VisibleForTesting
    @android.ravenwood.annotation.RavenwoodKeep
    public final @Nullable String getTransactionTraceName(int transactionCode) {
        final boolean isInterfaceUserDefined = getMaxTransactionId() == 0;
        if (mTransactionTraceNames == null) {
@@ -1127,7 +1020,6 @@ public class Binder implements IBinder {
        return transactionTraceName;
    }

    @android.ravenwood.annotation.RavenwoodKeep
    private @NonNull String getSimpleDescriptor() {
        String descriptor = mDescriptor;
        if (descriptor == null) {
@@ -1147,7 +1039,6 @@ public class Binder implements IBinder {
     * @return The highest user-defined transaction id of all transactions.
     * @hide
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public int getMaxTransactionId() {
        return 0;
    }
@@ -1359,14 +1250,12 @@ public class Binder implements IBinder {
    /**
     * Local implementation is a no-op.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public void linkToDeath(@NonNull DeathRecipient recipient, int flags) {
    }

    /**
     * Local implementation is a no-op.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public boolean unlinkToDeath(@NonNull DeathRecipient recipient, int flags) {
        return true;
    }
@@ -1394,13 +1283,8 @@ public class Binder implements IBinder {
        }
    }

    @android.ravenwood.annotation.RavenwoodReplace
    private static native long getNativeBBinderHolder();

    private static long getNativeBBinderHolder$ravenwood() {
        return 0L;
    }

    /**
     * By default, we use the calling UID since we can always trust it.
     */
+15 −65
Original line number Diff line number Diff line
@@ -27,9 +27,8 @@ import android.annotation.SuppressLint;
import android.annotation.TestApi;
import android.app.AppOpsManager;
import android.compat.annotation.UnsupportedAppUsage;
import android.ravenwood.annotation.RavenwoodClassLoadHook;
import android.ravenwood.annotation.RavenwoodKeepWholeClass;
import android.ravenwood.annotation.RavenwoodRedirect;
import android.ravenwood.annotation.RavenwoodRedirectionClass;
import android.ravenwood.annotation.RavenwoodReplace;
import android.ravenwood.annotation.RavenwoodThrow;
import android.text.TextUtils;
@@ -234,7 +233,7 @@ import java.util.function.IntFunction;
 * {@link #readSparseArray(ClassLoader, Class)}.
 */
@RavenwoodKeepWholeClass
@RavenwoodRedirectionClass("Parcel_host")
@RavenwoodClassLoadHook(RavenwoodClassLoadHook.LIBANDROID_LOADING_HOOK)
public final class Parcel {

    private static final boolean DEBUG_RECYCLE = false;
@@ -387,148 +386,98 @@ public final class Parcel {
    private static final int SIZE_COMPLEX_TYPE = 1;

    @CriticalNative
    @RavenwoodRedirect
    private static native void nativeMarkSensitive(long nativePtr);
    @FastNative
    @RavenwoodThrow
    private static native void nativeMarkForBinder(long nativePtr, IBinder binder);
    @CriticalNative
    @RavenwoodThrow
    private static native boolean nativeIsForRpc(long nativePtr);
    @CriticalNative
    @RavenwoodRedirect
    private static native int nativeDataSize(long nativePtr);
    @CriticalNative
    @RavenwoodRedirect
    private static native int nativeDataAvail(long nativePtr);
    @CriticalNative
    @RavenwoodRedirect
    private static native int nativeDataPosition(long nativePtr);
    @CriticalNative
    @RavenwoodRedirect
    private static native int nativeDataCapacity(long nativePtr);
    @FastNative
    @RavenwoodRedirect
    private static native void nativeSetDataSize(long nativePtr, int size);
    @CriticalNative
    @RavenwoodRedirect
    private static native void nativeSetDataPosition(long nativePtr, int pos);
    @FastNative
    @RavenwoodRedirect
    private static native void nativeSetDataCapacity(long nativePtr, int size);

    @CriticalNative
    @RavenwoodRedirect
    private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
    @CriticalNative
    @RavenwoodRedirect
    private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);

    @RavenwoodRedirect
    private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
    @RavenwoodRedirect
    private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
    @CriticalNative
    @RavenwoodRedirect
    private static native int nativeWriteInt(long nativePtr, int val);
    @CriticalNative
    @RavenwoodRedirect
    private static native int nativeWriteLong(long nativePtr, long val);
    @CriticalNative
    @RavenwoodRedirect
    private static native int nativeWriteFloat(long nativePtr, float val);
    @CriticalNative
    @RavenwoodRedirect
    private static native int nativeWriteDouble(long nativePtr, double val);
    @RavenwoodThrow
    private static native void nativeSignalExceptionForError(int error);
    @FastNative
    @RavenwoodRedirect
    private static native void nativeWriteString8(long nativePtr, String val);
    @FastNative
    @RavenwoodRedirect
    private static native void nativeWriteString16(long nativePtr, String val);
    @FastNative
    @RavenwoodThrow
    private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
    @FastNative
    @RavenwoodRedirect
    private static native void nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);

    @RavenwoodRedirect
    private static native byte[] nativeCreateByteArray(long nativePtr);
    @RavenwoodRedirect
    private static native boolean nativeReadByteArray(long nativePtr, byte[] dest, int destLen);
    @RavenwoodRedirect
    private static native byte[] nativeReadBlob(long nativePtr);
    @CriticalNative
    @RavenwoodRedirect
    private static native int nativeReadInt(long nativePtr);
    @CriticalNative
    @RavenwoodRedirect
    private static native long nativeReadLong(long nativePtr);
    @CriticalNative
    @RavenwoodRedirect
    private static native float nativeReadFloat(long nativePtr);
    @CriticalNative
    @RavenwoodRedirect
    private static native double nativeReadDouble(long nativePtr);
    @FastNative
    @RavenwoodRedirect
    private static native String nativeReadString8(long nativePtr);
    @FastNative
    @RavenwoodRedirect
    private static native String nativeReadString16(long nativePtr);
    @FastNative
    @RavenwoodThrow
    private static native IBinder nativeReadStrongBinder(long nativePtr);
    @FastNative
    @RavenwoodRedirect
    private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);

    @RavenwoodRedirect
    private static native long nativeCreate();
    @RavenwoodRedirect
    private static native void nativeFreeBuffer(long nativePtr);
    @RavenwoodRedirect
    private static native void nativeDestroy(long nativePtr);

    @RavenwoodRedirect
    private static native byte[] nativeMarshall(long nativePtr);
    @RavenwoodRedirect
    private static native void nativeUnmarshall(
            long nativePtr, byte[] data, int offset, int length);
    @RavenwoodRedirect
    private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
    @RavenwoodRedirect
    private static native boolean nativeCompareDataInRange(
            long ptrA, int offsetA, long ptrB, int offsetB, int length);
    @RavenwoodRedirect
    private static native void nativeAppendFrom(
            long thisNativePtr, long otherNativePtr, int offset, int length);
    @CriticalNative
    @RavenwoodRedirect
    private static native boolean nativeHasFileDescriptors(long nativePtr);
    @RavenwoodRedirect
    private static native boolean nativeHasFileDescriptorsInRange(
            long nativePtr, int offset, int length);

    @RavenwoodRedirect
    private static native boolean nativeHasBinders(long nativePtr);
    @RavenwoodRedirect
    private static native boolean nativeHasBindersInRange(
            long nativePtr, int offset, int length);
    @RavenwoodThrow
    private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
    @RavenwoodThrow
    private static native void nativeEnforceInterface(long nativePtr, String interfaceName);

    @CriticalNative
    @RavenwoodThrow
    private static native boolean nativeReplaceCallingWorkSourceUid(
            long nativePtr, int workSourceUid);
    @CriticalNative
    @RavenwoodThrow
    private static native int nativeReadCallingWorkSourceUid(long nativePtr);

    /** Last time exception with a stack trace was written */
@@ -537,7 +486,6 @@ public final class Parcel {
    private static final int WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS = 1000;

    @CriticalNative
    @RavenwoodThrow
    private static native long nativeGetOpenAshmemSize(long nativePtr);

    public final static Parcelable.Creator<String> STRING_CREATOR
@@ -701,12 +649,10 @@ public final class Parcel {

    /** @hide */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    @RavenwoodThrow
    public static native long getGlobalAllocSize();

    /** @hide */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    @RavenwoodThrow
    public static native long getGlobalAllocCount();

    /**
@@ -1298,6 +1244,7 @@ public final class Parcel {
     * @hide
     */
    @UnsupportedAppUsage
    @RavenwoodThrow(blockedBy = android.text.Spanned.class)
    public final void writeCharSequence(@Nullable CharSequence val) {
        TextUtils.writeToParcel(val, this, 0);
    }
@@ -3037,7 +2984,7 @@ public final class Parcel {
     * @see #writeNoException
     * @see #readException
     */
    @RavenwoodReplace
    @RavenwoodReplace(blockedBy = AppOpsManager.class)
    public final void writeException(@NonNull Exception e) {
        AppOpsManager.prefixParcelWithAppOpsIfNeeded(this);

@@ -3076,10 +3023,15 @@ public final class Parcel {
        }
    }

    /** @hide */
    public final void writeException$ravenwood(@NonNull Exception e) {
        // Ravenwood doesn't support IPC, no transaction headers needed
        writeInt(getExceptionCode(e));
    private void writeException$ravenwood(@NonNull Exception e) {
        int code = getExceptionCode(e);
        writeInt(code);
        if (code == 0) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            }
            throw new RuntimeException(e);
        }
        writeString(e.getMessage());
        writeInt(0);
    }
@@ -3137,7 +3089,7 @@ public final class Parcel {
     * @see #writeException
     * @see #readException
     */
    @RavenwoodReplace
    @RavenwoodReplace(blockedBy = AppOpsManager.class)
    public final void writeNoException() {
        AppOpsManager.prefixParcelWithAppOpsIfNeeded(this);

@@ -3168,9 +3120,7 @@ public final class Parcel {
        }
    }

    /** @hide */
    public final void writeNoException$ravenwood() {
        // Ravenwood doesn't support IPC, no transaction headers needed
    private void writeNoException$ravenwood() {
        writeInt(0);
    }

+5 −0
Original line number Diff line number Diff line
@@ -2605,10 +2605,15 @@ public final class StrictMode {
     * (Java) thread-local policy value.
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    @android.ravenwood.annotation.RavenwoodReplace
    private static void onBinderStrictModePolicyChange(@ThreadPolicyMask int newPolicy) {
        setBlockGuardPolicy(newPolicy);
    }

    private static void onBinderStrictModePolicyChange$ravenwood(@ThreadPolicyMask int newPolicy) {
        /* no-op */
    }

    /**
     * A tracked, critical time span. (e.g. during an animation.)
     *
+0 −2
Original line number Diff line number Diff line
@@ -224,7 +224,6 @@ public final class Slog {
    /**
     * Similar to {@link #wtf(String, String)}, but does not output anything to the log.
     */
    @android.ravenwood.annotation.RavenwoodThrow
    public static void wtfQuiet(@Nullable String tag, @NonNull String msg) {
        Log.wtfQuiet(Log.LOG_ID_SYSTEM, tag, msg, true);
    }
@@ -243,7 +242,6 @@ public final class Slog {
     * @see Log#wtfStack(String, String)
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    @android.ravenwood.annotation.RavenwoodThrow
    public static int wtfStack(@Nullable String tag, @NonNull String msg) {
        return Log.wtf(Log.LOG_ID_SYSTEM, tag, msg, null, true, true);
    }
+24 −21

File changed.

Preview size limit exceeded, changes collapsed.

Loading