From cc5885f92b7693a5b660313ec8e978c6dc002af8 Mon Sep 17 00:00:00 2001 From: Pete Gillin Date: Thu, 15 Feb 2018 17:17:27 +0000 Subject: [PATCH] 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 --- core/java/android/os/StrictMode.java | 53 ++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/core/java/android/os/StrictMode.java b/core/java/android/os/StrictMode.java index 3b6df5df13aa8..0789afddad59d 100644 --- a/core/java/android/os/StrictMode.java +++ b/core/java/android/os/StrictMode.java @@ -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; }