[CTS] Fix oom catcher to remove false positives

Now with actual Android APIs and per-test cleanup!

Bug: 111403569
Test: run cts hostside tests and in the middle:
      you:    $ adb shell
      device: $ oom(){ oom $1$1;};oom oom
      device will reboot with message "lowmemorykiller detected; rebooting device."

Change-Id: I1dbc7dac8e205fae78ce39500853a43334092416
diff --git a/apps/OomCatcher/Android.mk b/apps/OomCatcher/Android.mk
new file mode 100644
index 0000000..7f47e03
--- /dev/null
+++ b/apps/OomCatcher/Android.mk
@@ -0,0 +1,33 @@
+#
+# 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.
+#
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := OomCatcher
+
+LOCAL_SDK_VERSION := current
+
+LOCAL_COMPATIBILITY_SUITE := cts sts
+
+include $(BUILD_CTS_PACKAGE)
+
diff --git a/apps/OomCatcher/AndroidManifest.xml b/apps/OomCatcher/AndroidManifest.xml
new file mode 100644
index 0000000..25513e2
--- /dev/null
+++ b/apps/OomCatcher/AndroidManifest.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!-- 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+      package="com.android.cts.oomcatcher"
+      android:versionCode="1"
+      android:versionName="1.0">
+
+    <application>
+        <activity android:name=".OomCatcher">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+</manifest>
diff --git a/apps/OomCatcher/src/com/android/cts/oomcatcher/OomCatcher.java b/apps/OomCatcher/src/com/android/cts/oomcatcher/OomCatcher.java
new file mode 100644
index 0000000..53ec140
--- /dev/null
+++ b/apps/OomCatcher/src/com/android/cts/oomcatcher/OomCatcher.java
@@ -0,0 +1,103 @@
+/*
+ * 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 com.android.cts.oomcatcher;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.app.ActivityManager;
+import android.content.Context;
+import android.content.ComponentCallbacks2;
+import android.util.Log;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/*
+ * An App to report to logcat the lowmemory status. As soon as the app detects low memory, it
+ * immediately reports. In addition, it also reports every second.
+ */
+public class OomCatcher extends Activity implements ComponentCallbacks2 {
+
+    private static final String LOG_TAG = "OomCatcher";
+
+    private AtomicBoolean isOom = new AtomicBoolean(false);
+
+    Thread logThread;
+
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        logThread = new Thread() {
+            @Override
+            public void run() {
+                while (true) {
+                    logStatus();
+                    try {
+                        Thread.sleep(1000); // 1 second
+                    } catch (InterruptedException e) {
+                        // thread has been killed
+                    }
+                }
+            }
+        };
+        logThread.setDaemon(true);
+        logThread.start();
+    }
+
+    public void onDestroy() {
+        if (logThread != null) {
+            logThread.interrupt();
+        }
+    }
+
+    /*
+     * Receive memory callbacks from the Android system. All report low memory except for
+     * TRIM_MEMORY_UI_HIDDEN, which reports when the app is in the background. We don't care about
+     * that, only when the device is at risk of OOMing.
+     *
+     * For all indications of low memory, onLowMemory() is called.
+     */
+    @Override
+    public void onTrimMemory(int level) {
+        switch (level) {
+            case TRIM_MEMORY_COMPLETE:
+            case TRIM_MEMORY_MODERATE:
+            case TRIM_MEMORY_BACKGROUND:
+            case TRIM_MEMORY_RUNNING_CRITICAL:
+            case TRIM_MEMORY_RUNNING_LOW:
+            case TRIM_MEMORY_RUNNING_MODERATE:
+                //fallthrough
+                onLowMemory();
+                break;
+            case TRIM_MEMORY_UI_HIDDEN:
+            default:
+                return;
+        }
+    }
+
+    /*
+     * An earlier API implementation of low memory callbacks. Sets oom status and logs.
+     */
+    @Override
+    public void onLowMemory() {
+        isOom.set(true);
+        logStatus();
+    }
+
+    /*
+     * Log to logcat the current lowmemory status of the app.
+     */
+    private void logStatus() {
+        Log.i(LOG_TAG, isOom.get() ? "Low memory" : "Normal memory");
+    }
+}
diff --git a/hostsidetests/securitybulletin/AndroidTest.xml b/hostsidetests/securitybulletin/AndroidTest.xml
index 70e471a..95e822ca 100644
--- a/hostsidetests/securitybulletin/AndroidTest.xml
+++ b/hostsidetests/securitybulletin/AndroidTest.xml
@@ -166,6 +166,11 @@
         <option name="append-bitness" value="true" />
     </target_preparer>
 
+    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+        <option name="cleanup-apks" value="true" />
+        <option name="test-file-name" value="OomCatcher.apk" />
+    </target_preparer>
+
     <test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
         <option name="jar" value="CtsSecurityBulletinHostTestCases.jar" />
         <option name="runtime-hint" value="8m40s" />
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/HostsideOomCatcher.java b/hostsidetests/securitybulletin/src/android/security/cts/HostsideOomCatcher.java
new file mode 100644
index 0000000..5415aff
--- /dev/null
+++ b/hostsidetests/securitybulletin/src/android/security/cts/HostsideOomCatcher.java
@@ -0,0 +1,222 @@
+/*
+ * 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 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 = 1024 * 1024; // 1GB
+    private static Map<String, WeakReference<BackgroundDeviceAction>> oomCatchers = new HashMap<>();
+    private static Map<String, Long> totalMemories = new HashMap<>();
+
+    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 Exception {
+        String memInfo = device.executeShellCommand("cat /proc/meminfo");
+        Pattern pattern = Pattern.compile("MemTotal:\\s*(.*?)\\s*[kK][bB]");
+        Matcher matcher = pattern.matcher(memInfo);
+        if (matcher.find()) {
+            return Long.parseLong(matcher.group(1));
+        } else {
+            throw new Exception("Could not get device memory total");
+        }
+    }
+
+    /**
+     * Start the hostside oom catcher thread for the test.
+     * Match this call to SecurityTestCase.setup().
+     */
+    public synchronized void start() throws Exception {
+        // cache device TotalMem to avoid and adb shell for every test.
+        Long totalMemory = totalMemories.get(getDevice().getSerialNumber());
+        if (totalMemory == null) {
+            totalMemory = getMemTotal(getDevice());
+            totalMemories.put(getDevice().getSerialNumber(), totalMemory);
+        }
+        isLowMemoryDevice = totalMemory < LOW_MEMORY_DEVICE_THRESHOLD_KB;
+
+        // reset test oom behavior
+        // Low memory devices should skip (pass) tests when OOMing and log so that the
+        // high-memory-test flag can be added. Normal 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.
+        if (isLowMemoryDevice) {
+            oomBehavior = OomBehavior.PASS_AND_LOG;
+        } else {
+            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()) {
+            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(), 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 boolean isCancelled = false;
+
+        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;
+                    }
+                    try {
+
+                        getDevice().nonBlockingReboot();
+                        getDevice().waitForDeviceOnline(60 * 2 * 1000); // 2 minutes
+                        context.updateKernelStartTime();
+                    } 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/SecurityTestCase.java b/hostsidetests/securitybulletin/src/android/security/cts/SecurityTestCase.java
index 7101905..917bb70 100644
--- a/hostsidetests/securitybulletin/src/android/security/cts/SecurityTestCase.java
+++ b/hostsidetests/securitybulletin/src/android/security/cts/SecurityTestCase.java
@@ -26,6 +26,7 @@
 import java.util.regex.Matcher;
 import java.util.Map;
 import java.util.HashMap;
+import com.android.ddmlib.Log;
 
 public class SecurityTestCase extends DeviceTestCase {
 
@@ -33,6 +34,8 @@
 
     private long kernelStartTime;
 
+    private HostsideOomCatcher oomCatcher = new HostsideOomCatcher(this);
+
     /**
      * Waits for device to be online, marks the most recent boottime of the device
      */
@@ -45,6 +48,8 @@
             Integer.parseInt(uptime.substring(0, uptime.indexOf('.')));
         //TODO:(badash@): Watch for other things to track.
         //     Specifically time when app framework starts
+
+        oomCatcher.start();
     }
 
     /**
@@ -96,6 +101,22 @@
                     - kernelStartTime < 2));
         //TODO(badash@): add ability to catch runtime restart
         getDevice().disableAdbRoot();
+
+        if (oomCatcher.isOomDetected()) {
+            switch (oomCatcher.getOomBehavior()) {
+                case FAIL_AND_LOG:
+                    fail("The device ran out of memory.");
+                    return;
+                case PASS_AND_LOG:
+                    Log.logAndDisplay(Log.LogLevel.INFO, LOG_TAG, "Skipping test.");
+                    return;
+                case FAIL_NO_LOG:
+                    fail();
+                    return;
+            }
+        }
+
+        oomCatcher.stop(getDevice().getSerialNumber());
     }
 
     public void assertMatches(String pattern, String input) throws Exception {
@@ -115,8 +136,4 @@
         assertFalse("Pattern found: " + pattern,
           Pattern.compile(pattern, Pattern.DOTALL|Pattern.MULTILINE).matcher(input).find());
     }
-
-    // Flag meaning the test will likely fail on devices with low memory.
-    public void setHighMemoryTest() {
-    }
 }