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

Commit b3b07312 authored by Pete Gillin's avatar Pete Gillin
Browse files

Add new 'explicit GC' policy to StrictMode.

This change adds the policy but offers no public way to enable it. A
follow-up change will expose the detect/permit methods in the API and
change detectAll to enable it.

This new policy can only be triggered through the libcore BlockGuard API.

Bug: 3400644
Test: cts-tradefed run cts-dev -m CtsLibcoreTestCases
Test: cts-tradefed run cts-dev -m CtsOsTestCases
(cherry picked from commit c92f9edf53d09952fe9ab3d8066aed7fa7ecc852)

Change-Id: Ieebe4db747902246968d6382bbc9cee0e539af85
parent 2b6ba64c
Loading
Loading
Loading
Loading
+42 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.os.strictmode.ContentUriWithoutPermissionViolation;
import android.os.strictmode.CustomViolation;
import android.os.strictmode.DiskReadViolation;
import android.os.strictmode.DiskWriteViolation;
import android.os.strictmode.ExplicitGcViolation;
import android.os.strictmode.FileUriExposedViolation;
import android.os.strictmode.InstanceCountViolation;
import android.os.strictmode.IntentReceiverLeakedViolation;
@@ -212,13 +213,17 @@ public final class StrictMode {
    /** @hide */
    @TestApi public static final int DETECT_UNBUFFERED_IO = 0x20; // for ThreadPolicy

    /** @hide  */
    public static final int DETECT_EXPLICIT_GC = 0x40;  // for ThreadPolicy

    private static final int ALL_THREAD_DETECT_BITS =
            DETECT_DISK_WRITE
                    | DETECT_DISK_READ
                    | DETECT_NETWORK
                    | DETECT_CUSTOM
                    | DETECT_RESOURCE_MISMATCH
                    | DETECT_UNBUFFERED_IO;
                    | DETECT_UNBUFFERED_IO
                    | DETECT_EXPLICIT_GC;

    // Byte 2: Process-policy

@@ -581,6 +586,29 @@ public final class StrictMode {
                return disable(DETECT_DISK_WRITE);
            }

            /**
             * Detect explicit GC requests, i.e. calls to Runtime.gc().
             *
             * @hide
             */
            public Builder detectExplicitGc() {
                // TODO(b/3400644): Un-hide this for next API update
                // TODO(b/3400644): Un-hide ExplicitGcViolation for next API update
                // TODO(b/3400644): Make DETECT_EXPLICIT_GC a @TestApi for next API update
                // TODO(b/3400644): Call this from detectAll in next API update
                return enable(DETECT_EXPLICIT_GC);
            }

            /**
             * Disable detection of explicit GC requests, i.e. calls to Runtime.gc().
             *
             * @hide
             */
            public Builder permitExplicitGc() {
                // TODO(b/3400644): Un-hide this for next API update
                return disable(DETECT_EXPLICIT_GC);
            }

            /**
             * Show an annoying dialog to the developer on detected violations, rate-limited to be
             * only a little annoying.
@@ -1479,6 +1507,17 @@ public final class StrictMode {
            startHandlingViolationException(new NetworkViolation());
        }

        // Part of BlockGuard.Policy interface:
        public void onExplicitGc() {
            if ((mPolicyMask & DETECT_EXPLICIT_GC) == 0) {
                return;
            }
            if (tooManyViolationsThisLoop()) {
                return;
            }
            startHandlingViolationException(new ExplicitGcViolation());
        }

        public void setPolicyMask(int policyMask) {
            mPolicyMask = policyMask;
        }
@@ -2574,6 +2613,8 @@ public final class StrictMode {
                return DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION;
            } else if (mViolation instanceof UntaggedSocketViolation) {
                return DETECT_VM_UNTAGGED_SOCKET;
            } else if (mViolation instanceof ExplicitGcViolation) {
                return DETECT_EXPLICIT_GC;
            }
            throw new IllegalStateException("missing violation bit");
        }
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.strictmode;

/**
 * See #{@link android.os.StrictMode.ThreadPolicy.Builder#detectExplicitGc()}.
 *
 * @hide
 */
public final class ExplicitGcViolation extends Violation {
    /** @hide */
    public ExplicitGcViolation() {
        super(null);
    }
}