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

Commit 69204e06 authored by Jeff Sharkey's avatar Jeff Sharkey Committed by Android (Google) Code Review
Browse files

Merge "Baseline `StrictMode` on Ravenwood." into main

parents fb9474ec 7de862f4
Loading
Loading
Loading
Loading
+0 −9
Original line number Original line Diff line number Diff line
@@ -800,7 +800,6 @@ public final class FileUtils {
     *
     *
     * @hide
     * @hide
     */
     */
    @android.ravenwood.annotation.RavenwoodReplace
    public static void bytesToFile(String filename, byte[] content) throws IOException {
    public static void bytesToFile(String filename, byte[] content) throws IOException {
        if (filename.startsWith("/proc/")) {
        if (filename.startsWith("/proc/")) {
            final int oldMask = StrictMode.allowThreadDiskWritesMask();
            final int oldMask = StrictMode.allowThreadDiskWritesMask();
@@ -816,14 +815,6 @@ public final class FileUtils {
        }
        }
    }
    }


    /** @hide */
    public static void bytesToFile$ravenwood(String filename, byte[] content) throws IOException {
        // No StrictMode support, so we can just directly write
        try (FileOutputStream fos = new FileOutputStream(filename)) {
            fos.write(content);
        }
    }

    /**
    /**
     * Writes string to file. Basically same as "echo -n $string > $filename"
     * Writes string to file. Basically same as "echo -n $string > $filename"
     *
     *
+18 −0
Original line number Original line Diff line number Diff line
@@ -77,6 +77,7 @@ import com.android.internal.os.BackgroundThread;
import com.android.internal.os.RuntimeInit;
import com.android.internal.os.RuntimeInit;
import com.android.internal.util.FastPrintWriter;
import com.android.internal.util.FastPrintWriter;
import com.android.internal.util.HexDump;
import com.android.internal.util.HexDump;
import com.android.internal.util.Preconditions;


import dalvik.system.BlockGuard;
import dalvik.system.BlockGuard;
import dalvik.system.CloseGuard;
import dalvik.system.CloseGuard;
@@ -154,6 +155,7 @@ import java.util.function.Consumer;
 * android.os.Binder} calls, it's still ultimately a best effort mechanism. Notably, disk or network
 * android.os.Binder} calls, it's still ultimately a best effort mechanism. Notably, disk or network
 * access from JNI calls won't necessarily trigger it.
 * access from JNI calls won't necessarily trigger it.
 */
 */
@android.ravenwood.annotation.RavenwoodKeepPartialClass
public final class StrictMode {
public final class StrictMode {
    private static final String TAG = "StrictMode";
    private static final String TAG = "StrictMode";
    private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE);
    private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE);
@@ -1267,6 +1269,7 @@ public final class StrictMode {
    }
    }


    /** @hide */
    /** @hide */
    @android.ravenwood.annotation.RavenwoodReplace
    public static void setThreadPolicyMask(@ThreadPolicyMask int threadPolicyMask) {
    public static void setThreadPolicyMask(@ThreadPolicyMask int threadPolicyMask) {
        // In addition to the Java-level thread-local in Dalvik's
        // In addition to the Java-level thread-local in Dalvik's
        // BlockGuard, we also need to keep a native thread-local in
        // BlockGuard, we also need to keep a native thread-local in
@@ -1279,6 +1282,12 @@ public final class StrictMode {
        Binder.setThreadStrictModePolicy(threadPolicyMask);
        Binder.setThreadStrictModePolicy(threadPolicyMask);
    }
    }


    /** @hide */
    public static void setThreadPolicyMask$ravenwood(@ThreadPolicyMask int threadPolicyMask) {
        // Ravenwood currently doesn't support any detection modes
        Preconditions.checkFlagsArgument(threadPolicyMask, 0);
    }

    // Sets the policy in Dalvik/libcore (BlockGuard)
    // Sets the policy in Dalvik/libcore (BlockGuard)
    private static void setBlockGuardPolicy(@ThreadPolicyMask int threadPolicyMask) {
    private static void setBlockGuardPolicy(@ThreadPolicyMask int threadPolicyMask) {
        if (threadPolicyMask == 0) {
        if (threadPolicyMask == 0) {
@@ -1321,6 +1330,7 @@ public final class StrictMode {
     * @hide
     * @hide
     */
     */
    @UnsupportedAppUsage
    @UnsupportedAppUsage
    @android.ravenwood.annotation.RavenwoodReplace
    public static @ThreadPolicyMask int getThreadPolicyMask() {
    public static @ThreadPolicyMask int getThreadPolicyMask() {
        final BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
        final BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
        if (policy instanceof AndroidBlockGuardPolicy) {
        if (policy instanceof AndroidBlockGuardPolicy) {
@@ -1330,6 +1340,12 @@ public final class StrictMode {
        }
        }
    }
    }


    /** @hide */
    public static @ThreadPolicyMask int getThreadPolicyMask$ravenwood() {
        // Ravenwood currently doesn't support any detection modes
        return 0;
    }

    /** Returns the current thread's policy. */
    /** Returns the current thread's policy. */
    public static ThreadPolicy getThreadPolicy() {
    public static ThreadPolicy getThreadPolicy() {
        // TODO: this was a last minute Gingerbread API change (to
        // TODO: this was a last minute Gingerbread API change (to
@@ -1359,6 +1375,7 @@ public final class StrictMode {
    }
    }


    /** @hide */
    /** @hide */
    @android.ravenwood.annotation.RavenwoodKeep
    public static @ThreadPolicyMask int allowThreadDiskWritesMask() {
    public static @ThreadPolicyMask int allowThreadDiskWritesMask() {
        int oldPolicyMask = getThreadPolicyMask();
        int oldPolicyMask = getThreadPolicyMask();
        int newPolicyMask = oldPolicyMask & ~(DETECT_THREAD_DISK_WRITE | DETECT_THREAD_DISK_READ);
        int newPolicyMask = oldPolicyMask & ~(DETECT_THREAD_DISK_WRITE | DETECT_THREAD_DISK_READ);
@@ -1383,6 +1400,7 @@ public final class StrictMode {
    }
    }


    /** @hide */
    /** @hide */
    @android.ravenwood.annotation.RavenwoodKeep
    public static @ThreadPolicyMask int allowThreadDiskReadsMask() {
    public static @ThreadPolicyMask int allowThreadDiskReadsMask() {
        int oldPolicyMask = getThreadPolicyMask();
        int oldPolicyMask = getThreadPolicyMask();
        int newPolicyMask = oldPolicyMask & ~(DETECT_THREAD_DISK_READ);
        int newPolicyMask = oldPolicyMask & ~(DETECT_THREAD_DISK_READ);
+2 −8
Original line number Original line Diff line number Diff line
@@ -93,23 +93,17 @@ public final class ProcStatsUtil {
     * seen, or at the end of the file
     * seen, or at the end of the file
     */
     */
    @Nullable
    @Nullable
    @android.ravenwood.annotation.RavenwoodReplace
    public static String readTerminatedProcFile(String path, byte terminator) {
    public static String readTerminatedProcFile(String path, byte terminator) {
        // Permit disk reads here, as /proc isn't really "on disk" and should be fast.
        // Permit disk reads here, as /proc isn't really "on disk" and should be fast.
        // TODO: make BlockGuard ignore /proc/ and /sys/ files perhaps?
        // TODO: make BlockGuard ignore /proc/ and /sys/ files perhaps?
        final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
        final int savedPolicy = StrictMode.allowThreadDiskReadsMask();
        try {
        try {
            return readTerminatedProcFileInternal(path, terminator);
            return readTerminatedProcFileInternal(path, terminator);
        } finally {
        } finally {
            StrictMode.setThreadPolicy(savedPolicy);
            StrictMode.setThreadPolicyMask(savedPolicy);
        }
        }
    }
    }


    public static String readTerminatedProcFile$ravenwood(String path, byte terminator) {
        // No StrictMode under Ravenwood
        return readTerminatedProcFileInternal(path, terminator);
    }

    private static String readTerminatedProcFileInternal(String path, byte terminator) {
    private static String readTerminatedProcFileInternal(String path, byte terminator) {
        try (FileInputStream is = new FileInputStream(path)) {
        try (FileInputStream is = new FileInputStream(path)) {
            ByteArrayOutputStream byteStream = null;
            ByteArrayOutputStream byteStream = null;
+0 −5
Original line number Original line Diff line number Diff line
@@ -74,7 +74,6 @@ public class StoragedUidIoStatsReader {
     *
     *
     * @param callback The callback to invoke for each line of the proc file.
     * @param callback The callback to invoke for each line of the proc file.
     */
     */
    @android.ravenwood.annotation.RavenwoodReplace
    public void readAbsolute(Callback callback) {
    public void readAbsolute(Callback callback) {
        final int oldMask = StrictMode.allowThreadDiskReadsMask();
        final int oldMask = StrictMode.allowThreadDiskReadsMask();
        try {
        try {
@@ -84,10 +83,6 @@ public class StoragedUidIoStatsReader {
        }
        }
    }
    }


    public void readAbsolute$ravenwood(Callback callback) {
        readAbsoluteInternal(callback);
    }

    private void readAbsoluteInternal(Callback callback) {
    private void readAbsoluteInternal(Callback callback) {
        File file = new File(sUidIoFile);
        File file = new File(sUidIoFile);
        try (BufferedReader reader = Files.newBufferedReader(file.toPath())) {
        try (BufferedReader reader = Files.newBufferedReader(file.toPath())) {
+63 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os;

import static org.junit.Assert.assertEquals;

import androidx.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;

@RunWith(AndroidJUnit4.class)
public class StrictModeTest {
    private File mFile;

    @Before
    public void setUp() throws Exception {
        mFile = File.createTempFile("StrictModeTest", "tmp");
    }

    @Test
    public void testAllowThreadDiskReadsMask() throws Exception {
        final int mask = StrictMode.allowThreadDiskReadsMask();
        try {
            mFile.exists();
        } finally {
            StrictMode.setThreadPolicyMask(mask);
        }
    }

    @Test
    public void testAllowThreadDiskWritesMask() throws Exception {
        final int mask = StrictMode.allowThreadDiskReadsMask();
        try {
            mFile.delete();
        } finally {
            StrictMode.setThreadPolicyMask(mask);
        }
    }

    @Test
    public void testThreadMask() throws Exception {
        StrictMode.setThreadPolicyMask(0);
        assertEquals(0, StrictMode.getThreadPolicyMask());
    }
}
Loading