From 571c3f6e358246390574e31b8c68e947e9c2d97f Mon Sep 17 00:00:00 2001 From: Kurt Nelson Date: Mon, 10 Jul 2017 11:09:04 -0700 Subject: [PATCH] Version guard Application.onCreate strict mode fix In I30b3eafa4a833fc42d0a938da9446007ade80480 we fixed an outstanding bug around clobbering strict mode in Application.onCreate and allowing disk writes to go unreported. This guards that fix to only take effect once an app targets > SDK 26 and applies a ThreadPolicy inside Application.onCreate. Bug: 63436909 Test: Ran apps with violations Change-Id: Ib0ac1626e2ef322c4e61bbdf493d1f724b08652d --- core/java/android/app/ActivityThread.java | 25 ++++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 4a4bab55f0540..43693e1983b7f 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -5718,6 +5718,7 @@ public final class ActivityThread { // probably end up doing the same disk access. Application app; final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); + final StrictMode.ThreadPolicy writesAllowedPolicy = StrictMode.getThreadPolicy(); try { // If the app is being launched for full backup or restore, bring it up in // a restricted environment with the base application class. @@ -5745,17 +5746,21 @@ public final class ActivityThread { "Exception thrown in onCreate() of " + data.instrumentationName + ": " + e.toString(), e); } + try { + mInstrumentation.callApplicationOnCreate(app); + } catch (Exception e) { + if (!mInstrumentation.onException(app, e)) { + throw new RuntimeException( + "Unable to create application " + app.getClass().getName() + + ": " + e.toString(), e); + } + } } finally { - StrictMode.setThreadPolicy(savedPolicy); - } - - try { - mInstrumentation.callApplicationOnCreate(app); - } catch (Exception e) { - if (!mInstrumentation.onException(app, e)) { - throw new RuntimeException( - "Unable to create application " + app.getClass().getName() - + ": " + e.toString(), e); + // If the app targets < O-MR1, or doesn't change the thread policy + // during startup, clobber the policy to maintain behavior of b/36951662 + if (data.appInfo.targetSdkVersion <= Build.VERSION_CODES.O + || StrictMode.getThreadPolicy().equals(writesAllowedPolicy)) { + StrictMode.setThreadPolicy(savedPolicy); } }