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

Commit cc5885f9 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
Change-Id: I2e7f34ce010c78d6a5a7ac85512c045bfb13d204
Merged-In: Ieebe4db747902246968d6382bbc9cee0e539af85
parent 919ff226
Loading
Loading
Loading
Loading
+51 −2
Original line number Diff line number Diff line
@@ -196,9 +196,14 @@ public final class StrictMode {
     */
    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_RESOURCE_MISMATCH | DETECT_UNBUFFERED_IO | DETECT_EXPLICIT_GC;

    // Byte 2: Process-policy

@@ -556,6 +561,27 @@ 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): 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.
@@ -1093,6 +1119,15 @@ public final class StrictMode {
        }
    }

    /**
     * @hide
     */
    private static class StrictModeExplicitGcViolation extends StrictModeViolation {
        StrictModeExplicitGcViolation(int policyMask) {
            super(policyMask, DETECT_EXPLICIT_GC, null);
        }
    }

    /**
     * Returns the bitmask of the current thread's policy.
     *
@@ -1414,7 +1449,7 @@ public final class StrictMode {
            startHandlingViolationException(e);
        }

        // Part of BlockGuard.Policy; just part of StrictMode:
        // Part of BlockGuard.Policy interface:
        public void onUnbufferedIO() {
            if ((mPolicyMask & DETECT_UNBUFFERED_IO) == 0) {
                return;
@@ -1457,6 +1492,20 @@ public final class StrictMode {
            startHandlingViolationException(e);
        }

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

        public void setPolicyMask(int policyMask) {
            mPolicyMask = policyMask;
        }