Remove Oom Catcher

ensure all uses are removed from tests with:
find hostsidetests/securitybulletin/src -name "*.java" -exec sed -i /getOomCatcher/d {} \;
Bug: 175061433
Test: compiles

Change-Id: I9d615cc1cb2ef792f96200ab8fa7ecae410de9a3
Merged-In: I9d615cc1cb2ef792f96200ab8fa7ecae410de9a3
diff --git a/hostsidetests/securitybulletin/AndroidTest.xml b/hostsidetests/securitybulletin/AndroidTest.xml
index b13bd50..e81b403 100644
--- a/hostsidetests/securitybulletin/AndroidTest.xml
+++ b/hostsidetests/securitybulletin/AndroidTest.xml
@@ -24,7 +24,6 @@
 
     <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
         <option name="cleanup-apks" value="true" />
-        <option name="test-file-name" value="OomCatcher.apk" />
         <option name="test-file-name" value="hotspot.apk" />
     </target_preparer>
 
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/HostsideOomCatcher.java b/hostsidetests/securitybulletin/src/android/security/cts/HostsideOomCatcher.java
deleted file mode 100644
index d97c4db..0000000
--- a/hostsidetests/securitybulletin/src/android/security/cts/HostsideOomCatcher.java
+++ /dev/null
@@ -1,228 +0,0 @@
-/*
- * 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.security.cts;
-
-import com.android.tradefed.device.CollectingOutputReceiver;
-import com.android.tradefed.device.DeviceNotAvailableException;
-import com.android.tradefed.device.ITestDevice;
-import com.android.tradefed.testtype.DeviceTestCase;
-import com.android.tradefed.device.BackgroundDeviceAction;
-
-import android.platform.test.annotations.RootPermissionTest;
-
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.Scanner;
-import java.util.regex.Pattern;
-import java.util.regex.Matcher;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.concurrent.ConcurrentHashMap;
-import com.android.ddmlib.MultiLineReceiver;
-import com.android.ddmlib.Log;
-import com.android.ddmlib.TimeoutException;
-import java.lang.ref.WeakReference;
-
-/**
- * A utility to monitor the device lowmemory state and reboot when low. Without this, tests that
- * cause an OOM can sometimes cause ADB to become unresponsive indefinitely. Usage is to create an
- * instance per instance of SecurityTestCase and call start() and stop() matching to
- * SecurityTestCase setup() and teardown().
- */
-public class HostsideOomCatcher {
-
-    private static final String LOG_TAG = "HostsideOomCatcher";
-
-    private static final long LOW_MEMORY_DEVICE_THRESHOLD_KB = (long)(1.5 * 1024 * 1024); //1.5GB
-    private static Map<String, WeakReference<BackgroundDeviceAction>> oomCatchers =
-            new ConcurrentHashMap<>();
-    private static Map<String, Long> totalMemories = new ConcurrentHashMap<>();
-
-    private boolean isLowMemoryDevice = false;
-
-    private SecurityTestCase context;
-
-    /**
-     * test behavior when oom is detected.
-     */
-    public enum OomBehavior {
-        FAIL_AND_LOG, // normal behavior
-        PASS_AND_LOG, // skip tests that oom low memory devices
-        FAIL_NO_LOG,  // tests that check for oom
-    }
-    private OomBehavior oomBehavior = OomBehavior.FAIL_AND_LOG; // accessed across threads
-    private boolean oomDetected = false; // accessed across threads
-
-    public HostsideOomCatcher(SecurityTestCase context) {
-        this.context = context;
-    }
-
-    /**
-     * Utility to get the device memory total by reading /proc/meminfo and returning MemTotal
-     */
-    private static long getMemTotal(ITestDevice device) throws DeviceNotAvailableException {
-        // cache device TotalMem to avoid an adb shell for every test.
-        String serial = device.getSerialNumber();
-        Long totalMemory = totalMemories.get(serial);
-        if (totalMemory == null) {
-            String memInfo = device.executeShellCommand("cat /proc/meminfo");
-            Pattern pattern = Pattern.compile("MemTotal:\\s*(.*?)\\s*[kK][bB]");
-            Matcher matcher = pattern.matcher(memInfo);
-            if (matcher.find()) {
-                totalMemory = Long.parseLong(matcher.group(1));
-            } else {
-                throw new RuntimeException("Could not get device memory total.");
-            }
-            Log.logAndDisplay(Log.LogLevel.INFO, LOG_TAG,
-                    "Device " + serial + " has " + totalMemory + "KB total memory.");
-            totalMemories.put(serial, totalMemory);
-        }
-        return totalMemory;
-    }
-
-    /**
-     * Start the hostside oom catcher thread for the test.
-     * Match this call to SecurityTestCase.setup().
-     */
-    public synchronized void start() throws Exception {
-        long totalMemory = getMemTotal(getDevice());
-        isLowMemoryDevice = totalMemory < LOW_MEMORY_DEVICE_THRESHOLD_KB;
-
-        // reset test oom behavior
-        // Devices should fail tests that OOM so that they'll be ran again with --retry.
-        // If the test OOMs because previous tests used the memory, it will likely pass
-        // on a second try.
-        oomBehavior = OomBehavior.FAIL_AND_LOG;
-        oomDetected = false;
-
-        // Cache OOM detection in separate persistent threads for each device.
-        WeakReference<BackgroundDeviceAction> reference =
-                oomCatchers.get(getDevice().getSerialNumber());
-        BackgroundDeviceAction oomCatcher = null;
-        if (reference != null) {
-            oomCatcher = reference.get();
-        }
-        if (oomCatcher == null || !oomCatcher.isAlive() || oomCatcher.isCancelled()) {
-            AdbUtils.runCommandLine("am start com.android.cts.oomcatcher/.OomCatcher", getDevice());
-
-            oomCatcher = new BackgroundDeviceAction(
-                    "logcat -c && logcat OomCatcher:V *:S",
-                    "Oom Catcher background thread",
-                    getDevice(), new OomReceiver(getDevice()), 0);
-
-            oomCatchers.put(getDevice().getSerialNumber(), new WeakReference<>(oomCatcher));
-            oomCatcher.start();
-        }
-    }
-
-    /**
-     * Stop the hostside oom catcher thread.
-     * Match this call to SecurityTestCase.setup().
-     */
-    public static void stop(String serial) {
-        WeakReference<BackgroundDeviceAction> reference = oomCatchers.get(serial);
-        if (reference != null) {
-            BackgroundDeviceAction oomCatcher = reference.get();
-            if (oomCatcher != null) {
-                oomCatcher.cancel();
-            }
-        }
-    }
-
-    /**
-     * Check every test teardown to see if the device oomed during the test.
-     */
-    public synchronized boolean isOomDetected() {
-        return oomDetected;
-    }
-
-    /**
-     * Return the current test behavior for when oom is detected.
-     */
-    public synchronized OomBehavior getOomBehavior() {
-        return oomBehavior;
-    }
-
-    /**
-     * Flag meaning the test will likely fail on devices with low memory.
-     */
-    public synchronized void setHighMemoryTest() {
-        if (isLowMemoryDevice) {
-            oomBehavior = OomBehavior.PASS_AND_LOG;
-        } else {
-            oomBehavior = OomBehavior.FAIL_AND_LOG;
-        }
-    }
-
-    /**
-     * Flag meaning the test uses the OOM catcher to fail the test because the test vulnerability
-     * intentionally OOMs the device.
-     */
-    public synchronized void setOomTest() {
-        oomBehavior = OomBehavior.FAIL_NO_LOG;
-    }
-
-    private ITestDevice getDevice() {
-        return context.getDevice();
-    }
-
-    /**
-     * Read through logcat to find when the OomCatcher app reports low memory. Once detected, reboot
-     * the device to prevent a soft reset with the possiblity of ADB becomming unresponsive.
-     */
-    class OomReceiver extends MultiLineReceiver {
-
-        private ITestDevice device = null;
-        private boolean isCancelled = false;
-
-        public OomReceiver(ITestDevice device) {
-            this.device = device;
-        }
-
-        @Override
-        public void processNewLines(String[] lines) {
-            for (String line : lines) {
-                if (Pattern.matches(".*Low memory.*", line)) {
-                    // low memory detected, reboot device to clear memory and pass test
-                    isCancelled = true;
-                    Log.logAndDisplay(Log.LogLevel.INFO, LOG_TAG,
-                            "lowmemorykiller detected; rebooting device.");
-                    synchronized (HostsideOomCatcher.this) { // synchronized for oomDetected
-                        oomDetected = true; // set HostSideOomCatcher var
-                    }
-                    try {
-                        device.nonBlockingReboot();
-                        device.waitForDeviceOnline(60 * 2 * 1000); // 2 minutes
-                    } catch (Exception e) {
-                        Log.e(LOG_TAG, e.toString());
-                    }
-                    return; // we don't need to process remaining lines in the array
-                }
-            }
-        }
-
-        @Override
-        public boolean isCancelled() {
-            return isCancelled;
-        }
-    }
-}
-
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/Poc16_05.java b/hostsidetests/securitybulletin/src/android/security/cts/Poc16_05.java
index 39b7ada..0895607 100644
--- a/hostsidetests/securitybulletin/src/android/security/cts/Poc16_05.java
+++ b/hostsidetests/securitybulletin/src/android/security/cts/Poc16_05.java
@@ -44,7 +44,6 @@
     @Test
     @SecurityTest(minPatchLevel = "2016-05")
     public void testPocCVE_2015_1805() throws Exception {
-      getOomCatcher().setHighMemoryTest();
       AdbUtils.runPoc("CVE-2015-1805", getDevice(), TIMEOUT_NONDETERMINISTIC);
     }
 }
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/Poc16_07.java b/hostsidetests/securitybulletin/src/android/security/cts/Poc16_07.java
index 4367a61..835c1cf 100644
--- a/hostsidetests/securitybulletin/src/android/security/cts/Poc16_07.java
+++ b/hostsidetests/securitybulletin/src/android/security/cts/Poc16_07.java
@@ -58,7 +58,6 @@
     @Test
     @SecurityTest(minPatchLevel = "2016-07")
     public void testPocCVE_2016_3747() throws Exception {
-        getOomCatcher().setHighMemoryTest();
         AdbUtils.runPocAssertNoCrashes("CVE-2016-3747", getDevice(), "mediaserver");
     }
 }
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/Poc18_04.java b/hostsidetests/securitybulletin/src/android/security/cts/Poc18_04.java
index 44b0d89..dc41d7c 100644
--- a/hostsidetests/securitybulletin/src/android/security/cts/Poc18_04.java
+++ b/hostsidetests/securitybulletin/src/android/security/cts/Poc18_04.java
@@ -33,7 +33,6 @@
     @Test
     @SecurityTest(minPatchLevel = "2018-04")
     public void testPocCVE_2017_13286() throws Exception {
-        getOomCatcher().setHighMemoryTest();
         LaunchSomeWhere.launchSomeWhere("CVE_2017_13286", getDevice());
     }
 
@@ -44,7 +43,6 @@
     @Test
     @SecurityTest(minPatchLevel = "2018-04")
     public void testPocCVE_2017_13288() throws Exception {
-        getOomCatcher().setHighMemoryTest();
         LaunchSomeWhere.launchSomeWhere("CVE_2017_13288", getDevice());
     }
 
@@ -55,7 +53,6 @@
     @Test
     @SecurityTest(minPatchLevel = "2018-04")
     public void testPocCVE_2017_13289() throws Exception {
-        getOomCatcher().setHighMemoryTest();
         LaunchSomeWhere.launchSomeWhere("CVE_2017_13289", getDevice());
     }
 }
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/Poc18_05.java b/hostsidetests/securitybulletin/src/android/security/cts/Poc18_05.java
index 6b51f0a..e3128f1 100644
--- a/hostsidetests/securitybulletin/src/android/security/cts/Poc18_05.java
+++ b/hostsidetests/securitybulletin/src/android/security/cts/Poc18_05.java
@@ -33,7 +33,6 @@
     @Test
     @SecurityTest(minPatchLevel = "2018-05")
     public void testPocCVE_2017_13315() throws Exception {
-        getOomCatcher().setHighMemoryTest();
         LaunchSomeWhere.launchSomeWhere("CVE_2017_13315", getDevice());
     }
 
@@ -44,7 +43,6 @@
     @Test
     @SecurityTest(minPatchLevel = "2018-05")
     public void testPocCVE_2017_13312() throws Exception {
-        getOomCatcher().setHighMemoryTest();
         LaunchSomeWhere.launchSomeWhere("CVE_2017_13312", getDevice());
     }
 }
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/SecurityTestCase.java b/hostsidetests/securitybulletin/src/android/security/cts/SecurityTestCase.java
index 908d845..cfffde8 100644
--- a/hostsidetests/securitybulletin/src/android/security/cts/SecurityTestCase.java
+++ b/hostsidetests/securitybulletin/src/android/security/cts/SecurityTestCase.java
@@ -58,8 +58,6 @@
 
     private long kernelStartTime;
 
-    private HostsideOomCatcher oomCatcher = new HostsideOomCatcher(this);
-
     @Rule public TestName testName = new TestName();
     @Rule public PocPusher pocPusher = new PocPusher();
 
@@ -79,7 +77,6 @@
         // TODO:(badash@): Watch for other things to track.
         //     Specifically time when app framework starts
 
-        oomCatcher.start();
         sBuildInfo.put(getDevice(), getBuild());
         sAbi.put(getDevice(), getAbi());
         sTestName.put(getDevice(), testName.getMethodName());
@@ -94,8 +91,6 @@
      */
     @After
     public void tearDown() throws Exception {
-        oomCatcher.stop(getDevice().getSerialNumber());
-
         try {
             getDevice().waitForDeviceAvailable(90 * 1000);
         } catch (DeviceNotAvailableException e) {
@@ -104,27 +99,11 @@
             getDevice().waitForDeviceAvailable(30 * 1000);
         }
 
-        if (oomCatcher.isOomDetected()) {
-            // we don't need to check kernel start time if we intentionally rebooted because oom
-            updateKernelStartTime();
-            switch (oomCatcher.getOomBehavior()) {
-                case FAIL_AND_LOG:
-                    fail("The device ran out of memory.");
-                    break;
-                case PASS_AND_LOG:
-                    Log.logAndDisplay(Log.LogLevel.INFO, LOG_TAG, "Skipping test.");
-                    break;
-                case FAIL_NO_LOG:
-                    fail();
-                    break;
-            }
-        } else {
-            long deviceTime = getDeviceUptime() + kernelStartTime;
-            long hostTime = System.currentTimeMillis() / 1000;
-            assertTrue("Phone has had a hard reset", (hostTime - deviceTime) < 2);
+        long deviceTime = getDeviceUptime() + kernelStartTime;
+        long hostTime = System.currentTimeMillis() / 1000;
+        assertTrue("Phone has had a hard reset", (hostTime - deviceTime) < 2);
 
-            // TODO(badash@): add ability to catch runtime restart
-        }
+        // TODO(badash@): add ability to catch runtime restart
     }
 
     public static IBuildInfo getBuildInfo(ITestDevice device) {
@@ -306,8 +285,4 @@
         long uptime = getDeviceUptime();
         kernelStartTime = (System.currentTimeMillis() / 1000) - uptime;
     }
-
-    public HostsideOomCatcher getOomCatcher() {
-        return oomCatcher;
-    }
 }