[RESTRICT AUTOMERGE] Updated Media Codec Test methods

Bug: 129068851
Test: Tested by original author
Change-Id: I3ff3fcb784128065ecbd654c82850d17aa7ef473
(cherry picked from commit 8a96968350d9076fff88b9d9ba4f277e4094e73d)
diff --git a/hostsidetests/securitybulletin/AndroidTest.xml b/hostsidetests/securitybulletin/AndroidTest.xml
index d56f65f..5fb0f40 100644
--- a/hostsidetests/securitybulletin/AndroidTest.xml
+++ b/hostsidetests/securitybulletin/AndroidTest.xml
@@ -188,9 +188,15 @@
     <target_preparer class="com.android.compatibility.common.tradefed.targetprep.FilePusher">
         <option name="cleanup" value="true" />
 
-        <option name="push" value="testhevcdec32->/data/local/tmp/testhevcdec" />
-        <option name="push" value="testavcdec32->/data/local/tmp/testavcdec" />
-        <option name="push" value="testmpeg2dec32->/data/local/tmp/testmpeg2dec" />
+        <option name="push" value="testhevc32->/data/local/tmp/testhevc" />
+        <option name="push" value="testavc32->/data/local/tmp/testavc" />
+        <option name="push" value="testmpeg232->/data/local/tmp/testmpeg2" />
+        <option name="push" value="testhevc_mem132->/data/local/tmp/testhevc_mem1" />
+        <option name="push" value="testavc_mem132->/data/local/tmp/testavc_mem1" />
+        <option name="push" value="testmpeg2_mem132->/data/local/tmp/testmpeg2_mem1" />
+        <option name="push" value="testhevc_mem232->/data/local/tmp/testhevc_mem2" />
+        <option name="push" value="testavc_mem232->/data/local/tmp/testavc_mem2" />
+        <option name="push" value="testmpeg2_mem232->/data/local/tmp/testmpeg2_mem2" />
 
         <option name="append-bitness" value="false" />
     </target_preparer>
diff --git a/hostsidetests/securitybulletin/securityPatch/avcdec/Android.mk b/hostsidetests/securitybulletin/securityPatch/avcdec/Android.mk
index a97b245..b82da08 100644
--- a/hostsidetests/securitybulletin/securityPatch/avcdec/Android.mk
+++ b/hostsidetests/securitybulletin/securityPatch/avcdec/Android.mk
@@ -1,39 +1,7 @@
-# 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.
-#=========================================================================
-# NOTE: This module uses the libavc's testbench from external folder
-# without creating a copy of the testbench locally. Hence LOCAL_SRC_FILES
-# is pointed to external folder.
-# This module is dependent on external/libavc/test
-#=========================================================================
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
 
-LOCAL_MODULE := testavcdec
-LOCAL_SRC_FILES := ../../../../../external/libavc/test/decoder/main.c
-LOCAL_MULTILIB := 32
-LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
-LOCAL_C_INCLUDES := external/libavc/common
-LOCAL_C_INCLUDES += external/libavc/decoder
-LOCAL_SHARED_LIBRARIES := liblog
-LOCAL_SHARED_LIBRARIES += libstagefright_soft_avcdec
-
-# Tag this module as a cts test artifact
-LOCAL_COMPATIBILITY_SUITE := cts sts
-LOCAL_CTS_TEST_PACKAGE := android.security.cts
-
-LOCAL_ARM_MODE := arm
-LOCAL_CFLAGS += -Wall -Werror
-LOCAL_CFLAGS += -DPROFILE_ENABLE -fPIC -DMD5_DISABLE
-include $(BUILD_CTS_EXECUTABLE)
+# avcdec testbench
+include $(LOCAL_PATH)/testavc.mk
+include $(LOCAL_PATH)/testavc_mem1.mk
+include $(LOCAL_PATH)/testavc_mem2.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/avcdec/common.mk b/hostsidetests/securitybulletin/securityPatch/avcdec/common.mk
new file mode 100644
index 0000000..f678b96
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/avcdec/common.mk
@@ -0,0 +1,37 @@
+# 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.
+#=========================================================================
+# NOTE: This module uses the libavc's testbench from external folder
+# without creating a copy of the testbench locally. Hence LOCAL_SRC_FILES
+# is pointed to external folder.
+# This module is dependent on external/libavc/test
+#=========================================================================
+
+LOCAL_MULTILIB := 32
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_SRC_FILES += ../../../../../external/libavc/test/decoder/main.c
+LOCAL_C_INCLUDES := external/libavc/common
+LOCAL_C_INCLUDES += external/libavc/decoder
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_SHARED_LIBRARIES += libstagefright_soft_avcdec
+
+# Tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts sts
+LOCAL_CTS_TEST_PACKAGE := android.security.cts
+
+LOCAL_ARM_MODE := arm
+LOCAL_CFLAGS += -Wall -Werror
+LOCAL_CFLAGS += -DPROFILE_ENABLE -fPIC -DMD5_DISABLE
+
+include $(BUILD_CTS_EXECUTABLE)
diff --git a/hostsidetests/securitybulletin/securityPatch/avcdec/testavc.mk b/hostsidetests/securitybulletin/securityPatch/avcdec/testavc.mk
new file mode 100644
index 0000000..d42819f
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/avcdec/testavc.mk
@@ -0,0 +1,5 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := testavc
+include $(LOCAL_PATH)/common.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/avcdec/testavc_mem1.mk b/hostsidetests/securitybulletin/securityPatch/avcdec/testavc_mem1.mk
new file mode 100644
index 0000000..ce10152
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/avcdec/testavc_mem1.mk
@@ -0,0 +1,7 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := testavc_mem1
+LOCAL_SRC_FILES += ../includes/memutils.c
+LOCAL_CFLAGS += -DCHECK_OVERFLOW
+include $(LOCAL_PATH)/common.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/avcdec/testavc_mem2.mk b/hostsidetests/securitybulletin/securityPatch/avcdec/testavc_mem2.mk
new file mode 100644
index 0000000..af7c08f
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/avcdec/testavc_mem2.mk
@@ -0,0 +1,7 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := testavc_mem2
+LOCAL_SRC_FILES += ../includes/memutils.c
+LOCAL_CFLAGS += -DCHECK_UNDERFLOW
+include $(LOCAL_PATH)/common.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/hevcdec/Android.mk b/hostsidetests/securitybulletin/securityPatch/hevcdec/Android.mk
index f3bd68c..be07cbf 100644
--- a/hostsidetests/securitybulletin/securityPatch/hevcdec/Android.mk
+++ b/hostsidetests/securitybulletin/securityPatch/hevcdec/Android.mk
@@ -1,41 +1,7 @@
-# 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.
-
-#=========================================================================
-# NOTE: This module uses the libhevc's testbench from external folder
-# without creating a copy of the testbench locally. Hence LOCAL_SRC_FILES
-# is pointed to external folder.
-# This module is dependent on external/libhevc/test
-#=========================================================================
-
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
 
-LOCAL_MODULE := testhevcdec
-LOCAL_SRC_FILES := ../../../../../external/libhevc/test/decoder/main.c
-LOCAL_MULTILIB := 32
-LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
-LOCAL_C_INCLUDES := external/libhevc/common
-LOCAL_C_INCLUDES += external/libhevc/decoder
-LOCAL_SHARED_LIBRARIES := liblog
-LOCAL_SHARED_LIBRARIES += libstagefright_soft_hevcdec
-
-# Tag this module as a cts test artifact
-LOCAL_COMPATIBILITY_SUITE := cts sts
-LOCAL_CTS_TEST_PACKAGE := android.security.cts
-
-LOCAL_ARM_MODE := arm
-LOCAL_CFLAGS += -Wall -Werror
-LOCAL_CFLAGS += -DPROFILE_ENABLE -fPIC -DMD5_DISABLE
-include $(BUILD_CTS_EXECUTABLE)
+# hevcdec testbench
+include $(LOCAL_PATH)/testhevc.mk
+include $(LOCAL_PATH)/testhevc_mem1.mk
+include $(LOCAL_PATH)/testhevc_mem2.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/hevcdec/common.mk b/hostsidetests/securitybulletin/securityPatch/hevcdec/common.mk
new file mode 100644
index 0000000..f866ff9
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/hevcdec/common.mk
@@ -0,0 +1,37 @@
+# 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.
+#=========================================================================
+# NOTE: This module uses the libhevc's testbench from external folder
+# without creating a copy of the testbench locally. Hence LOCAL_SRC_FILES
+# is pointed to external folder.
+# This module is dependent on external/libhevc/test
+#=========================================================================
+
+LOCAL_MULTILIB := 32
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_SRC_FILES += ../../../../../external/libhevc/test/decoder/main.c
+LOCAL_C_INCLUDES := external/libhevc/common
+LOCAL_C_INCLUDES += external/libhevc/decoder
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_SHARED_LIBRARIES += libstagefright_soft_hevcdec
+
+# Tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts sts
+LOCAL_CTS_TEST_PACKAGE := android.security.cts
+
+LOCAL_ARM_MODE := arm
+LOCAL_CFLAGS += -Wall -Werror
+LOCAL_CFLAGS += -DPROFILE_ENABLE -fPIC -DMD5_DISABLE
+
+include $(BUILD_CTS_EXECUTABLE)
diff --git a/hostsidetests/securitybulletin/securityPatch/hevcdec/testhevc.mk b/hostsidetests/securitybulletin/securityPatch/hevcdec/testhevc.mk
new file mode 100644
index 0000000..5bc3c8b
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/hevcdec/testhevc.mk
@@ -0,0 +1,5 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := testhevc
+include $(LOCAL_PATH)/common.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/hevcdec/testhevc_mem1.mk b/hostsidetests/securitybulletin/securityPatch/hevcdec/testhevc_mem1.mk
new file mode 100644
index 0000000..44b9d7f
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/hevcdec/testhevc_mem1.mk
@@ -0,0 +1,7 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := testhevc_mem1
+LOCAL_SRC_FILES += ../includes/memutils.c
+LOCAL_CFLAGS += -DCHECK_OVERFLOW
+include $(LOCAL_PATH)/common.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/hevcdec/testhevc_mem2.mk b/hostsidetests/securitybulletin/securityPatch/hevcdec/testhevc_mem2.mk
new file mode 100644
index 0000000..85197b8
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/hevcdec/testhevc_mem2.mk
@@ -0,0 +1,7 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := testhevc_mem2
+LOCAL_SRC_FILES += ../includes/memutils.c
+LOCAL_CFLAGS += -DCHECK_UNDERFLOW
+include $(LOCAL_PATH)/common.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/includes/memutils.c b/hostsidetests/securitybulletin/securityPatch/includes/memutils.c
new file mode 100644
index 0000000..10df3bd
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/includes/memutils.c
@@ -0,0 +1,138 @@
+/**
+ * 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.
+ */
+#define _GNU_SOURCE
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/mman.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include "memutils.h"
+
+void sigsegv_handler(int signum) {
+    size_t page_size = getpagesize();
+    for (int i = 0; i < s_mem_map_index; i++) {
+        if (NULL != s_mem_map[i].start_ptr) {
+            ENABLE_MEM_ACCESS(s_mem_map[i].start_ptr,
+                              (s_mem_map[i].num_pages * page_size));
+        }
+    }
+    (*old_sa.sa_handler)(signum);
+}
+
+void sighandler_init(void) {
+    sigemptyset(&new_sa.sa_mask);
+    new_sa.sa_handler = sigsegv_handler;
+    sigaction(SIGSEGV, &new_sa, &old_sa);
+}
+
+void memutils_init(void) {
+    real_memalign = dlsym(RTLD_NEXT, "memalign");
+    if (NULL == real_memalign) {
+        return;
+    }
+    real_free = dlsym(RTLD_NEXT, "free");
+    if (NULL == real_free) {
+        return;
+    }
+    memset(&s_mem_map, 0, MAX_ENTRIES * sizeof(map_struct_t));
+    sighandler_init();
+    s_memutils_initialized = 1;
+}
+
+void *memalign(size_t alignment, size_t size) {
+    if (s_memutils_initialized == 0) {
+        memutils_init();
+    }
+    char* start_ptr;
+    char* mem_ptr;
+    size_t total_size;
+    size_t aligned_size = size;
+    size_t num_pages;
+    size_t page_size = getpagesize();
+
+    /* User specified alignment is not respected and is overridden by
+     * "new_alignment". This is required to catch OOB read when read offset is
+     * less than user specified alignment. "new_alignment" is derived based on
+     * size_t, and helps to avoid bus errors due to non-aligned memory.
+     * "new_alignment", whenever used, is checked to ensure sizeof(size_t)
+     * has returned proper value                                            */
+    size_t new_alignment = sizeof(size_t);
+
+    if (s_mem_map_index == MAX_ENTRIES) {
+        return real_memalign(alignment, size);
+    }
+
+    if (alignment > page_size) {
+        return real_memalign(alignment, size);
+    }
+
+    if ((0 == page_size) || (0 == alignment) || (0 == size)
+            || (0 == new_alignment)) {
+        return real_memalign(alignment, size);
+    }
+#ifdef CHECK_OVERFLOW
+    if (0 != (size % new_alignment)) {
+        aligned_size = size + (new_alignment - (size % new_alignment));
+    }
+#endif
+
+    if (0 != (aligned_size % page_size)) {
+        num_pages = (aligned_size / page_size) + 2;
+    } else {
+        num_pages = (aligned_size / page_size) + 1;
+    }
+
+    total_size = (num_pages * page_size);
+    start_ptr = (char *) real_memalign(page_size, total_size);
+#ifdef CHECK_OVERFLOW
+    mem_ptr = (char *) start_ptr + ((num_pages - 1) * page_size) - aligned_size;
+    DISABLE_MEM_ACCESS((start_ptr + ((num_pages - 1) * page_size)), page_size);
+#endif
+#ifdef CHECK_UNDERFLOW
+    mem_ptr = (char *) start_ptr + page_size;
+    DISABLE_MEM_ACCESS(start_ptr, page_size);
+#endif
+    s_mem_map[s_mem_map_index].start_ptr = start_ptr;
+    s_mem_map[s_mem_map_index].mem_ptr = mem_ptr;
+    s_mem_map[s_mem_map_index].num_pages = num_pages;
+    s_mem_map_index++;
+    memset(mem_ptr, INITIAL_VAL, size);
+    return mem_ptr;
+}
+
+void free(void *ptr) {
+    if (s_memutils_initialized == 0) {
+        memutils_init();
+    }
+    if (ptr != NULL) {
+        int i = 0;
+        size_t page_size = getpagesize();
+        for (i = 0; i < s_mem_map_index; i++) {
+            if (ptr == s_mem_map[i].mem_ptr) {
+                ENABLE_MEM_ACCESS(s_mem_map[i].start_ptr,
+                                  (s_mem_map[i].num_pages * page_size));
+                real_free(s_mem_map[i].start_ptr);
+                memset(&s_mem_map[i], 0, sizeof(map_struct_t));
+                return;
+            }
+        }
+    }
+    real_free(ptr);
+    return;
+}
diff --git a/hostsidetests/securitybulletin/securityPatch/includes/memutils.h b/hostsidetests/securitybulletin/securityPatch/includes/memutils.h
new file mode 100644
index 0000000..ede8bb1
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/includes/memutils.h
@@ -0,0 +1,39 @@
+/**
+ * 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.
+ */
+#define MAX_ENTRIES        (1024)
+#define INITIAL_VAL        (0xBE)
+
+#define DISABLE_MEM_ACCESS(mem, size)\
+    mprotect((char *) mem, size, PROT_NONE);
+
+#define ENABLE_MEM_ACCESS(mem, size)\
+    mprotect((char *) mem, size, PROT_READ | PROT_WRITE);
+
+typedef struct _map_struct_t {
+    void *start_ptr;
+    void *mem_ptr;
+    int num_pages;
+} map_struct_t;
+
+static void* (*real_memalign)(size_t, size_t) = NULL;
+static void (*real_free)(void *) = NULL;
+static int s_memutils_initialized = 0;
+static int s_mem_map_index = 0;
+static struct sigaction new_sa, old_sa;
+map_struct_t s_mem_map[MAX_ENTRIES];
+#if (!(defined CHECK_OVERFLOW) && !(defined CHECK_UNDERFLOW))
+    #error "CHECK MACROS NOT DEFINED"
+#endif
diff --git a/hostsidetests/securitybulletin/securityPatch/mpeg2dec/Android.mk b/hostsidetests/securitybulletin/securityPatch/mpeg2dec/Android.mk
index b815e7c..5225675 100644
--- a/hostsidetests/securitybulletin/securityPatch/mpeg2dec/Android.mk
+++ b/hostsidetests/securitybulletin/securityPatch/mpeg2dec/Android.mk
@@ -1,41 +1,7 @@
-# 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.
-
-#=========================================================================
-# NOTE: This module uses the libmpeg2's testbench from external folder
-# without creating a copy of the testbench locally. Hence LOCAL_SRC_FILES
-# is pointed to external folder.
-# This module is dependent on external/libmpeg2/test
-#=========================================================================
-
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
 
-LOCAL_MODULE := testmpeg2dec
-LOCAL_SRC_FILES := ../../../../../external/libmpeg2/test/decoder/main.c
-LOCAL_MULTILIB := 32
-LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
-LOCAL_C_INCLUDES := external/libmpeg2/common
-LOCAL_C_INCLUDES += external/libmpeg2/decoder
-LOCAL_SHARED_LIBRARIES := liblog
-LOCAL_SHARED_LIBRARIES += libstagefright_soft_mpeg2dec
-
-# Tag this module as a cts test artifact
-LOCAL_COMPATIBILITY_SUITE := cts sts
-LOCAL_CTS_TEST_PACKAGE := android.security.cts
-
-LOCAL_ARM_MODE := arm
-LOCAL_CFLAGS += -Wall -Werror
-LOCAL_CFLAGS += -DPROFILE_ENABLE -fPIC -DMD5_DISABLE
-include $(BUILD_CTS_EXECUTABLE)
+# mpeg2dec testbench
+include $(LOCAL_PATH)/testmpeg2.mk
+include $(LOCAL_PATH)/testmpeg2_mem1.mk
+include $(LOCAL_PATH)/testmpeg2_mem2.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/mpeg2dec/common.mk b/hostsidetests/securitybulletin/securityPatch/mpeg2dec/common.mk
new file mode 100644
index 0000000..a9193c3
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/mpeg2dec/common.mk
@@ -0,0 +1,37 @@
+# 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.
+#=========================================================================
+# NOTE: This module uses the libmpeg2's testbench from external folder
+# without creating a copy of the testbench locally. Hence LOCAL_SRC_FILES
+# is pointed to external folder.
+# This module is dependent on external/libmpeg2/test
+#=========================================================================
+
+LOCAL_MULTILIB := 32
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_SRC_FILES += ../../../../../external/libmpeg2/test/decoder/main.c
+LOCAL_C_INCLUDES := external/libmpeg2/common
+LOCAL_C_INCLUDES += external/libmpeg2/decoder
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_SHARED_LIBRARIES += libstagefright_soft_mpeg2dec
+
+# Tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts sts
+LOCAL_CTS_TEST_PACKAGE := android.security.cts
+
+LOCAL_ARM_MODE := arm
+LOCAL_CFLAGS += -Wall -Werror
+LOCAL_CFLAGS += -DPROFILE_ENABLE -fPIC -DMD5_DISABLE
+
+include $(BUILD_CTS_EXECUTABLE)
diff --git a/hostsidetests/securitybulletin/securityPatch/mpeg2dec/testmpeg2.mk b/hostsidetests/securitybulletin/securityPatch/mpeg2dec/testmpeg2.mk
new file mode 100644
index 0000000..6da9415
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/mpeg2dec/testmpeg2.mk
@@ -0,0 +1,5 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := testmpeg2
+include $(LOCAL_PATH)/common.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/mpeg2dec/testmpeg2_mem1.mk b/hostsidetests/securitybulletin/securityPatch/mpeg2dec/testmpeg2_mem1.mk
new file mode 100644
index 0000000..a1478f0
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/mpeg2dec/testmpeg2_mem1.mk
@@ -0,0 +1,7 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := testmpeg2_mem1
+LOCAL_SRC_FILES += ../includes/memutils.c
+LOCAL_CFLAGS += -DCHECK_OVERFLOW
+include $(LOCAL_PATH)/common.mk
diff --git a/hostsidetests/securitybulletin/securityPatch/mpeg2dec/testmpeg2_mem2.mk b/hostsidetests/securitybulletin/securityPatch/mpeg2dec/testmpeg2_mem2.mk
new file mode 100644
index 0000000..44c481b
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/mpeg2dec/testmpeg2_mem2.mk
@@ -0,0 +1,7 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := testmpeg2_mem2
+LOCAL_SRC_FILES += ../includes/memutils.c
+LOCAL_CFLAGS += -DCHECK_UNDERFLOW
+include $(LOCAL_PATH)/common.mk
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/TestMediaCodec.java b/hostsidetests/securitybulletin/src/android/security/cts/TestMediaCodec.java
index 51e3632..acd8644 100644
--- a/hostsidetests/securitybulletin/src/android/security/cts/TestMediaCodec.java
+++ b/hostsidetests/securitybulletin/src/android/security/cts/TestMediaCodec.java
@@ -27,9 +27,15 @@
     final static int TIMEOUT_SEC = 9 * 60;
     final static String RESOURCE_ROOT = "/";
     final static String TMP_FILE_PATH = "/data/local/tmp/";
-    final static String HEVCDEC_BINARY = "testhevcdec";
-    final static String AVCDEC_BINARY = "testavcdec";
-    final static String MPEG2DEC_BINARY = "testmpeg2dec";
+    final static String HEVCDEC_BINARY = "testhevc";
+    final static String AVCDEC_BINARY = "testavc";
+    final static String MPEG2DEC_BINARY = "testmpeg2";
+    final static String HEVCDEC_MEMOVERFLOW_BINARY = "testhevc_mem1";
+    final static String AVCDEC_MEMOVERFLOW_BINARY = "testavc_mem1";
+    final static String MPEG2DEC_MEMOVERFLOW_BINARY = "testmpeg2_mem1";
+    final static String HEVCDEC_MEMUNDERFLOW_BINARY = "testhevc_mem2";
+    final static String AVCDEC_MEMUNDERFLOW_BINARY = "testavc_mem2";
+    final static String MPEG2DEC_MEMUNDERFLOW_BINARY = "testmpeg2_mem2";
 
     /***********************************************************
     To prevent merge conflicts, add HEVC decoder tests for N
@@ -162,6 +168,144 @@
     }
 
     /**
+    * Calls HEVC decoder memory overflow and underflow tests
+    *
+    * @param inputFiles files required as input
+    * @param arguments arguments for running the binary
+    * @param device device to be run on
+    * @param errPattern error patterns to be checked for
+    */
+   public static void runHevcDecodeMemTest(String inputFiles[],
+           String arguments, ITestDevice device, String errPattern[])
+           throws Exception {
+       runHevcDecodeMemOverflowTest(inputFiles, arguments, device, errPattern);
+       runHevcDecodeMemUnderflowTest(inputFiles, arguments, device,
+               errPattern);
+   }
+
+   /**
+    * Calls runDecodeTest with HEVC decoder overflow test binary name argument
+    *
+    * @param inputFiles files required as input
+    * @param arguments arguments for running the binary
+    * @param device device to be run on
+    * @param errPattern error patterns to be checked for
+    */
+   public static void runHevcDecodeMemOverflowTest(String inputFiles[],
+           String arguments, ITestDevice device, String errPattern[])
+           throws Exception {
+       runDecodeTest(HEVCDEC_MEMOVERFLOW_BINARY, inputFiles, arguments, device,
+               errPattern);
+   }
+
+   /**
+    * Calls runDecodeTest with HEVC decoder underflow test binary name argument
+    *
+    * @param inputFiles files required as input
+    * @param arguments arguments for running the binary
+    * @param device device to be run on
+    * @param errPattern error patterns to be checked for
+    */
+   public static void runHevcDecodeMemUnderflowTest(String inputFiles[],
+           String arguments, ITestDevice device, String errPattern[])
+           throws Exception {
+       runDecodeTest(HEVCDEC_MEMUNDERFLOW_BINARY, inputFiles, arguments, device,
+               errPattern);
+   }
+
+   /**
+    * Calls MPEG2 decoder memory overflow and underflow tests
+    *
+    * @param inputFiles files required as input
+    * @param arguments arguments for running the binary
+    * @param device device to be run on
+    * @param errPattern error patterns to be checked for
+    */
+   public static void runMpeg2DecodeMemTest(String inputFiles[],
+           String arguments, ITestDevice device, String errPattern[])
+           throws Exception {
+       runMpeg2DecodeMemOverflowTest(inputFiles, arguments, device,
+               errPattern);
+       runMpeg2DecodeMemUnderflowTest(inputFiles, arguments, device,
+               errPattern);
+   }
+
+   /**
+    * Calls runDecodeTest with MPEG2 decoder overflow test binary name argument
+    *
+    * @param inputFiles files required as input
+    * @param arguments arguments for running the binary
+    * @param device device to be run on
+    * @param errPattern error patterns to be checked for
+    */
+   public static void runMpeg2DecodeMemOverflowTest(String inputFiles[],
+           String arguments, ITestDevice device, String errPattern[])
+           throws Exception {
+       runDecodeTest(MPEG2DEC_MEMOVERFLOW_BINARY, inputFiles, arguments, device,
+               errPattern);
+   }
+
+   /**
+    * Calls runDecodeTest with MPEG2 decoder underflow test binary name argument
+    *
+    * @param inputFiles files required as input
+    * @param arguments arguments for running the binary
+    * @param device device to be run on
+    * @param errPattern error patterns to be checked for
+    */
+   public static void runMpeg2DecodeMemUnderflowTest(String inputFiles[],
+           String arguments, ITestDevice device, String errPattern[])
+           throws Exception {
+       runDecodeTest(MPEG2DEC_MEMUNDERFLOW_BINARY, inputFiles, arguments, device,
+               errPattern);
+   }
+
+   /**
+    * Calls AVC decoder memory overflow and underflow tests
+    *
+    * @param inputFiles files required as input
+    * @param arguments arguments for running the binary
+    * @param device device to be run on
+    * @param errPattern error patterns to be checked for
+    */
+   public static void runAvcDecodeMemTest(String inputFiles[],
+           String arguments, ITestDevice device, String errPattern[])
+           throws Exception {
+       runAvcDecodeMemOverflowTest(inputFiles, arguments, device, errPattern);
+       runAvcDecodeMemUnderflowTest(inputFiles, arguments, device, errPattern);
+   }
+
+   /**
+    * Calls runDecodeTest with AVC decoder overflow test binary name argument
+    *
+    * @param inputFiles files required as input
+    * @param arguments arguments for running the binary
+    * @param device device to be run on
+    * @param errPattern error patterns to be checked for
+    */
+   public static void runAvcDecodeMemOverflowTest(String inputFiles[],
+           String arguments, ITestDevice device, String errPattern[])
+           throws Exception {
+       runDecodeTest(AVCDEC_MEMOVERFLOW_BINARY, inputFiles, arguments, device,
+               errPattern);
+   }
+
+   /**
+    * Calls runDecodeTest with AVC decoder underflow test binary name argument
+    *
+    * @param inputFiles files required as input
+    * @param arguments arguments for running the binary
+    * @param device device to be run on
+    * @param errPattern error patterns to be checked for
+    */
+   public static void runAvcDecodeMemUnderflowTest(String inputFiles[],
+           String arguments, ITestDevice device, String errPattern[])
+           throws Exception {
+       runDecodeTest(AVCDEC_MEMUNDERFLOW_BINARY, inputFiles, arguments, device,
+               errPattern);
+   }
+
+    /**
      * Checks for linker errors
      *
      * @param binaryName name of the decoder binary
@@ -185,9 +329,9 @@
             String logcat) throws Exception {
         String genericCrashPattern[] = {
                 ".*name: " + binaryName + "  >>> " + TMP_FILE_PATH + binaryName
-                        + " <<<.*SIGABRT.*",
+                        + " <<<\n.*SIGABRT.*",
                 ".*name: " + binaryName + "  >>> " + TMP_FILE_PATH + binaryName
-                        + " <<<.*SIGSEGV.*"};
+                        + " <<<\n.*SIGSEGV.*"};
         AdbUtils.checkCrash(genericCrashPattern, logcat);
         if (errPattern != null) {
             AdbUtils.checkCrash(errPattern, logcat);
@@ -230,4 +374,4 @@
             checkCodecCrash(binaryName, errPattern, logcatOut);
         }
     }
-}
\ No newline at end of file
+}