summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPete Gillin <peteg@google.com>2018-02-15 17:17:27 +0000
committerPete Gillin <peteg@google.com>2018-03-06 11:17:07 +0000
commitcc5885f92b7693a5b660313ec8e978c6dc002af8 (patch)
tree115a53909420f9fc0d21a3f2c931767d9d5828df
parent919ff2268b22e9fde5cbb08404d3a567dde86890 (diff)
downloadbase-cc5885f92b7693a5b660313ec8e978c6dc002af8.tar.gz
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
-rw-r--r--core/java/android/os/StrictMode.java53
1 files changed, 51 insertions, 2 deletions
diff --git a/core/java/android/os/StrictMode.java b/core/java/android/os/StrictMode.java
index 3b6df5df13aa..0789afddad59 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
@@ -557,6 +562,27 @@ public final class StrictMode {
}
/**
+ * 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.
*/
@@ -1094,6 +1120,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.
*
* @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled
@@ -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;
}