[RESTRICT AUTOMERGE] Updated CTS test for Android Security b/150159669

Bug: 150159669
Bug: 160693070
Bug: 185090843
Bug: 207035735
Test: Ran the new testcase on android-9.0.0_r39 to test with/without patch

Change-Id: I9d1f68004487bb138fa268a7776e35f517d9c370
diff --git a/hostsidetests/securitybulletin/securityPatch/CVE-2020-0381/poc.cpp b/hostsidetests/securitybulletin/securityPatch/CVE-2020-0381/poc.cpp
index 23d73c3..d6aa888 100644
--- a/hostsidetests/securitybulletin/securityPatch/CVE-2020-0381/poc.cpp
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2020-0381/poc.cpp
@@ -17,9 +17,6 @@
 #include <dlfcn.h>
 #include <media/DataSource.h>
 #include <media/MediaExtractor.h>
-#include <signal.h>
-#include <stdlib.h>
-
 #include "../includes/common.h"
 #include "../includes/memutils.h"
 
@@ -33,6 +30,22 @@
 
 using namespace android;
 
+bool isTestInProgress = false;
+
+struct sigaction new_action, old_action;
+
+int fdData, fdInfo;
+
+void *libHandle = nullptr;
+
+void sigsegv_handler(int signum, siginfo_t *info, void *context) {
+  if (isTestInProgress && info->si_signo == SIGSEGV) {
+    (*old_action.sa_sigaction)(signum, info, context);
+    return;
+  }
+  _exit(EXIT_FAILURE);
+}
+
 class XMFDataSource : public DataSource {
  public:
   int mFdData;
@@ -60,7 +73,7 @@
   virtual status_t initCheck() const { return 0; }
 };
 
-void close_resources(int fdData, int fdInfo, void *libHandle) {
+void close_resources() {
   if (fdData >= 0) {
     ::close(fdData);
   }
@@ -73,44 +86,39 @@
 }
 
 int main(int argc, char **argv) {
-  if (argc < 3) {
-    return EXIT_FAILURE;
-  }
-  enable_selective_overload = ENABLE_ALL;
+  atexit(close_resources);
+
+  sigemptyset(&new_action.sa_mask);
+  new_action.sa_flags = SA_SIGINFO;
+  new_action.sa_sigaction = sigsegv_handler;
+  sigaction(SIGSEGV, &new_action, &old_action);
+
+  FAIL_CHECK(argc == 3);
   void *libHandle = dlopen(LIBNAME, RTLD_NOW | RTLD_LOCAL);
-  if (!libHandle) {
-    return EXIT_FAILURE;
-  }
+  FAIL_CHECK(libHandle);
 
   MediaExtractor::GetExtractorDef getDef =
       (MediaExtractor::GetExtractorDef)dlsym(libHandle, "GETEXTRACTORDEF");
-  if (!getDef) {
-    dlclose(libHandle);
-    return EXIT_FAILURE;
-  }
+  FAIL_CHECK(getDef);
 
-  int fdData = open(argv[1], O_RDONLY);
-  if (fdData < 0) {
-    dlclose(libHandle);
-    return EXIT_FAILURE;
-  }
-  int fdInfo = open(argv[2], O_RDONLY);
-  if (fdInfo < 0) {
-    close_resources(fdData, fdInfo, libHandle);
-    return EXIT_FAILURE;
-  }
+  fdData = open(argv[1], O_RDONLY);
+  FAIL_CHECK(fdData >= 0);
+
+  fdInfo = open(argv[2], O_RDONLY);
+  FAIL_CHECK(fdInfo >= 0);
 
   sp<DataSource> dataSource = (sp<DataSource>)new XMFDataSource(fdData, fdInfo);
-  if (!dataSource) {
-    close_resources(fdData, fdInfo, libHandle);
-    return EXIT_FAILURE;
-  }
+  FAIL_CHECK(dataSource);
+
+  enable_selective_overload = ENABLE_ALL;
+  isTestInProgress = true;
 
   void *meta = nullptr;
   MediaExtractor::FreeMetaFunc freeMeta = nullptr;
   float confidence = 0.0f;
   getDef().sniff(dataSource.get(), &confidence, &meta, &freeMeta);
-  close_resources(fdData, fdInfo, libHandle);
-  enable_selective_overload = ENABLE_NONE;
+
+  isTestInProgress = false;
+  enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
   return EXIT_SUCCESS;
 }