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

Commit 77754502 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "[Ravenwood] Cleanup and update RavenwoodEnvironment" into main

parents ebb29524 41adec23
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -36,8 +36,6 @@ import android.util.ArraySet;
import android.util.Slog;
import android.view.View;

import com.android.internal.ravenwood.RavenwoodEnvironment;

import dalvik.system.VMRuntime;

import java.util.ArrayList;
@@ -51,10 +49,6 @@ import java.util.stream.Collectors;
 */
@RavenwoodKeepWholeClass
public class Build {
    static {
        // Set up the default system properties.
        RavenwoodEnvironment.ensureRavenwoodInitialized();
    }
    private static final String TAG = "Build";

    /** Value used for when a build property is unknown. */
+0 −8
Original line number Diff line number Diff line
@@ -41,7 +41,6 @@ import android.content.ContentResolver;
import android.net.Uri;
import android.os.MessageQueue.OnFileDescriptorEventListener;
import android.ravenwood.annotation.RavenwoodKeepWholeClass;
import android.ravenwood.annotation.RavenwoodReplace;
import android.ravenwood.annotation.RavenwoodThrow;
import android.system.ErrnoException;
import android.system.Os;
@@ -51,8 +50,6 @@ import android.util.CloseGuard;
import android.util.Log;
import android.util.Slog;

import com.android.internal.ravenwood.RavenwoodEnvironment;

import dalvik.system.VMRuntime;

import libcore.io.IoUtils;
@@ -1254,15 +1251,10 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
        }
    }

    @RavenwoodReplace
    private static boolean isAtLeastQ() {
        return (VMRuntime.getRuntime().getTargetSdkVersion() >= Build.VERSION_CODES.Q);
    }

    private static boolean isAtLeastQ$ravenwood() {
        return RavenwoodEnvironment.workaround().isTargetSdkAtLeastQ();
    }

    private static int ifAtLeastQ(int value) {
        return isAtLeastQ() ? value : 0;
    }
+27 −97
Original line number Diff line number Diff line
@@ -29,6 +29,11 @@ import android.annotation.TestApi;
import android.annotation.UptimeMillisLong;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.Build.VERSION_CODES;
import android.ravenwood.annotation.RavenwoodKeep;
import android.ravenwood.annotation.RavenwoodKeepPartialClass;
import android.ravenwood.annotation.RavenwoodRedirect;
import android.ravenwood.annotation.RavenwoodRedirectionClass;
import android.ravenwood.annotation.RavenwoodReplace;
import android.sysprop.MemoryProperties;
import android.system.ErrnoException;
import android.system.Os;
@@ -37,8 +42,6 @@ import android.system.StructPollfd;
import android.util.Pair;
import android.webkit.WebViewZygote;

import com.android.internal.os.SomeArgs;
import com.android.internal.util.Preconditions;
import com.android.sdksandbox.flags.Flags;

import dalvik.system.VMDebug;
@@ -55,6 +58,8 @@ import java.util.concurrent.TimeoutException;
/**
 * Tools for managing OS processes.
 */
@RavenwoodKeepPartialClass
@RavenwoodRedirectionClass("Process_ravenwood")
public class Process {
    private static final String LOG_TAG = "Process";

@@ -672,7 +677,6 @@ public class Process {
     */
    public static final ZygoteProcess ZYGOTE_PROCESS = new ZygoteProcess();


    /**
     * The process name set via {@link #setArgV0(String)}.
     */
@@ -846,47 +850,20 @@ public class Process {
    /**
     * Returns true if the current process is a 64-bit runtime.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static final boolean is64Bit() {
        return VMRuntime.getRuntime().is64Bit();
    }

    private static volatile ThreadLocal<SomeArgs> sIdentity$ravenwood;

    /** @hide */
    @android.ravenwood.annotation.RavenwoodKeep
    public static void init$ravenwood(final int uid, final int pid) {
        sIdentity$ravenwood = ThreadLocal.withInitial(() -> {
            final SomeArgs args = SomeArgs.obtain();
            args.argi1 = uid;
            args.argi2 = pid;
            args.argi3 = Long.hashCode(Thread.currentThread().getId());
            args.argi4 = THREAD_PRIORITY_DEFAULT;
            args.arg1 = Boolean.TRUE; // backgroundOk
            return args;
        });
    }

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

    /**
     * Returns the identifier of this process, which can be used with
     * {@link #killProcess} and {@link #sendSignal}.
     */
    @android.ravenwood.annotation.RavenwoodReplace
    @RavenwoodKeep
    public static final int myPid() {
        return Os.getpid();
    }

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

    /**
     * Returns the identifier of this process' parent.
     * @hide
@@ -900,39 +877,29 @@ public class Process {
     * Returns the identifier of the calling thread, which be used with
     * {@link #setThreadPriority(int, int)}.
     */
    @android.ravenwood.annotation.RavenwoodReplace
    @RavenwoodKeep
    public static final int myTid() {
        return Os.gettid();
    }

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

    /**
     * Returns the identifier of this process's uid.  This is the kernel uid
     * that the process is running under, which is the identity of its
     * app-specific sandbox.  It is different from {@link #myUserHandle} in that
     * a uid identifies a specific app sandbox in a specific user.
     */
    @android.ravenwood.annotation.RavenwoodReplace
    @RavenwoodKeep
    public static final int myUid() {
        return Os.getuid();
    }

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

    /**
     * Returns this process's user handle.  This is the
     * user the process is running under.  It is distinct from
     * {@link #myUid()} in that a particular user will have multiple
     * distinct apps running under it each with their own uid.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static UserHandle myUserHandle() {
        return UserHandle.of(UserHandle.getUserId(myUid()));
    }
@@ -941,7 +908,7 @@ public class Process {
     * Returns whether the given uid belongs to a system core component or not.
     * @hide
     */
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static boolean isCoreUid(int uid) {
        return UserHandle.isCore(uid);
    }
@@ -952,7 +919,7 @@ public class Process {
     * @return Whether the uid corresponds to an application sandbox running in
     *     a specific user.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static boolean isApplicationUid(int uid) {
        return UserHandle.isApp(uid);
    }
@@ -960,7 +927,7 @@ public class Process {
    /**
     * Returns whether the current process is in an isolated sandbox.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static final boolean isIsolated() {
        return isIsolated(myUid());
    }
@@ -972,7 +939,7 @@ public class Process {
    @Deprecated
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.TIRAMISU,
            publicAlternatives = "Use {@link #isIsolatedUid(int)} instead.")
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static final boolean isIsolated(int uid) {
        return isIsolatedUid(uid);
    }
@@ -980,7 +947,7 @@ public class Process {
    /**
     * Returns whether the process with the given {@code uid} is an isolated sandbox.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static final boolean isIsolatedUid(int uid) {
        uid = UserHandle.getAppId(uid);
        return (uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID)
@@ -992,7 +959,7 @@ public class Process {
     * @see android.app.sdksandbox.SdkSandboxManager
     */
    @SuppressLint("UnflaggedApi") // promoting from @SystemApi.
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static final boolean isSdkSandboxUid(int uid) {
        uid = UserHandle.getAppId(uid);
        return (uid >= FIRST_SDK_SANDBOX_UID && uid <= LAST_SDK_SANDBOX_UID);
@@ -1008,7 +975,7 @@ public class Process {
     * @throws IllegalArgumentException if input is not an sdk sandbox uid
     */
    @SuppressLint("UnflaggedApi") // promoting from @SystemApi.
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static final int getAppUidForSdkSandboxUid(int uid) {
        if (!isSdkSandboxUid(uid)) {
            throw new IllegalArgumentException("Input UID is not an SDK sandbox UID");
@@ -1024,7 +991,7 @@ public class Process {
     */
    @SystemApi(client = MODULE_LIBRARIES)
    @TestApi
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    // TODO(b/318651609): Deprecate once Process#getSdkSandboxUidForAppUid is rolled out to 100%
    public static final int toSdkSandboxUid(int uid) {
        return uid + (FIRST_SDK_SANDBOX_UID - FIRST_APPLICATION_UID);
@@ -1040,7 +1007,7 @@ public class Process {
     * @throws IllegalArgumentException if input is not an app uid
     */
    @FlaggedApi(Flags.FLAG_SDK_SANDBOX_UID_TO_APP_UID_API)
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static final int getSdkSandboxUidForAppUid(int uid) {
        if (!isApplicationUid(uid)) {
            throw new IllegalArgumentException("Input UID is not an app UID");
@@ -1051,7 +1018,7 @@ public class Process {
    /**
     * Returns whether the current process is a sdk sandbox process.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    @RavenwoodKeep
    public static final boolean isSdkSandbox() {
        return isSdkSandboxUid(myUid());
    }
@@ -1128,28 +1095,11 @@ public class Process {
     * not have permission to modify the given thread, or to use the given
     * priority.
     */
    @android.ravenwood.annotation.RavenwoodReplace
    @RavenwoodRedirect
    public static final native void setThreadPriority(int tid,
            @IntRange(from = -20, to = THREAD_PRIORITY_LOWEST) int priority)
            throws IllegalArgumentException, SecurityException;

    /** @hide */
    public static final void setThreadPriority$ravenwood(int tid, int priority) {
        final SomeArgs args =
                Preconditions.requireNonNullViaRavenwoodRule(sIdentity$ravenwood).get();
        if (args.argi3 == tid) {
            boolean backgroundOk = (args.arg1 == Boolean.TRUE);
            if (priority >= THREAD_PRIORITY_BACKGROUND && !backgroundOk) {
                throw new IllegalArgumentException(
                        "Priority " + priority + " blocked by setCanSelfBackground()");
            }
            args.argi4 = priority;
        } else {
            throw new UnsupportedOperationException(
                    "Cross-thread priority management not yet available in Ravenwood");
        }
    }

    /**
     * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
     * throw an exception if passed a background-level thread priority.  This is only
@@ -1157,16 +1107,9 @@ public class Process {
     *
     * @hide
     */
    @android.ravenwood.annotation.RavenwoodReplace
    @RavenwoodRedirect
    public static final native void setCanSelfBackground(boolean backgroundOk);

    /** @hide */
    public static final void setCanSelfBackground$ravenwood(boolean backgroundOk) {
        final SomeArgs args =
                Preconditions.requireNonNullViaRavenwoodRule(sIdentity$ravenwood).get();
        args.arg1 = Boolean.valueOf(backgroundOk);
    }

    /**
     * Sets the scheduling group for a thread.
     * @hide
@@ -1295,13 +1238,12 @@ public class Process {
     *
     * @see #setThreadPriority(int, int)
     */
    @android.ravenwood.annotation.RavenwoodReplace
    @RavenwoodReplace
    public static final native void setThreadPriority(
            @IntRange(from = -20, to = THREAD_PRIORITY_LOWEST) int priority)
            throws IllegalArgumentException, SecurityException;

    /** @hide */
    public static final void setThreadPriority$ravenwood(int priority) {
    private static void setThreadPriority$ravenwood(int priority) {
        setThreadPriority(myTid(), priority);
    }

@@ -1318,23 +1260,11 @@ public class Process {
     * @throws IllegalArgumentException Throws IllegalArgumentException if
     * <var>tid</var> does not exist.
     */
    @android.ravenwood.annotation.RavenwoodReplace
    @RavenwoodRedirect
    @IntRange(from = -20, to = THREAD_PRIORITY_LOWEST)
    public static final native int getThreadPriority(int tid)
            throws IllegalArgumentException;

    /** @hide */
    public static final int getThreadPriority$ravenwood(int tid) {
        final SomeArgs args =
                Preconditions.requireNonNullViaRavenwoodRule(sIdentity$ravenwood).get();
        if (args.argi3 == tid) {
            return args.argi4;
        } else {
            throw new UnsupportedOperationException(
                    "Cross-thread priority management not yet available in Ravenwood");
        }
    }

    /**
     * Return the current scheduling policy of a thread, based on Linux.
     *
+2 −49
Original line number Diff line number Diff line
@@ -28,19 +28,9 @@ import android.ravenwood.annotation.RavenwoodReplace;
public final class RavenwoodEnvironment {
    public static final String TAG = "RavenwoodEnvironment";

    private static final RavenwoodEnvironment sInstance;
    private static final Workaround sWorkaround;
    private static RavenwoodEnvironment sInstance = new RavenwoodEnvironment();

    private RavenwoodEnvironment() {
    }

    static {
        sInstance = new RavenwoodEnvironment();
        sWorkaround = new Workaround();
        ensureRavenwoodInitialized();
    }

    public static RuntimeException notSupportedOnDevice() {
    private static RuntimeException notSupportedOnDevice() {
        return new UnsupportedOperationException("This method can only be used on Ravenwood");
    }

@@ -51,15 +41,6 @@ public final class RavenwoodEnvironment {
        return sInstance;
    }

    /**
     * Initialize the ravenwood environment if it hasn't happened already, if running on Ravenwood.
     *
     * No-op if called on the device side.
     */
    @RavenwoodRedirect
    public static void ensureRavenwoodInitialized() {
    }

    /**
     * USE IT SPARINGLY! Returns true if it's running on Ravenwood, hostside test environment.
     *
@@ -90,18 +71,6 @@ public final class RavenwoodEnvironment {
        throw notSupportedOnDevice();
    }

    /**
     * See {@link Workaround}. It's only usable on Ravenwood.
     */
    @RavenwoodReplace
    public static Workaround workaround() {
        throw notSupportedOnDevice();
    }

    private static Workaround workaround$ravenwood() {
        return sWorkaround;
    }

    /**
     * @return the "ravenwood-runtime" directory.
     */
@@ -109,20 +78,4 @@ public final class RavenwoodEnvironment {
    public String getRavenwoodRuntimePath() {
        throw notSupportedOnDevice();
    }

    /**
     * A set of APIs used to work around missing features on Ravenwood. Ideally, this class should
     * be empty, and all its APIs should be able to be implemented properly.
     */
    public static class Workaround {
        Workaround() {
        }

        /**
         * @return whether the app's target SDK level is at least Q.
         */
        public boolean isTargetSdkAtLeastQ() {
            return true;
        }
    }
}
+7 −3
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Process_ravenwood;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.provider.DeviceConfig_host;
@@ -52,6 +53,7 @@ import androidx.test.platform.app.InstrumentationRegistry;
import com.android.hoststubgen.hosthelper.HostTestUtils;
import com.android.internal.os.RuntimeInit;
import com.android.ravenwood.RavenwoodRuntimeNative;
import com.android.ravenwood.RavenwoodRuntimeState;
import com.android.ravenwood.common.RavenwoodCommonUtils;
import com.android.ravenwood.common.RavenwoodRuntimeException;
import com.android.ravenwood.common.SneakyThrow;
@@ -223,7 +225,9 @@ public class RavenwoodRuntimeEnvironmentController {
            Thread.setDefaultUncaughtExceptionHandler(sUncaughtExceptionHandler);
        }

        android.os.Process.init$ravenwood(config.mUid, config.mPid);
        RavenwoodRuntimeState.sUid = config.mUid;
        RavenwoodRuntimeState.sPid = config.mPid;
        RavenwoodRuntimeState.sTargetSdkLevel = config.mTargetSdkLevel;
        sOriginalIdentityToken = Binder.clearCallingIdentity();
        reinit();
        setSystemProperties(config.mSystemProperties);
@@ -350,8 +354,8 @@ public class RavenwoodRuntimeEnvironmentController {
        if (sOriginalIdentityToken != -1) {
            Binder.restoreCallingIdentity(sOriginalIdentityToken);
        }
        android.os.Process.reset$ravenwood();

        RavenwoodRuntimeState.reset();
        Process_ravenwood.reset();
        DeviceConfig_host.reset();

        try {
Loading