Updated memutils files

Test: sts-tradefed
Bug: 159497582

Added a macro to disable malloc overloading
Ensured 16 byte memory alignment

Change-Id: I10e7a1eaa066e9a78478eb2e245665b6f6f2d145
diff --git a/hostsidetests/securitybulletin/securityPatch/includes/memutils.c b/hostsidetests/securitybulletin/securityPatch/includes/memutils.c
index 650d2f6..65e1e90 100644
--- a/hostsidetests/securitybulletin/securityPatch/includes/memutils.c
+++ b/hostsidetests/securitybulletin/securityPatch/includes/memutils.c
@@ -61,6 +61,7 @@
     if (NULL == real_memalign) {
         return;
     }
+#ifndef DISABLE_MALLOC_OVERLOADING
     real_calloc = dlsym(RTLD_NEXT, "calloc");
     if (NULL == real_calloc) {
         return;
@@ -73,6 +74,7 @@
     if (NULL == real_realloc) {
         return;
     }
+#endif /* DISABLE_MALLOC_OVERLOADING */
     real_free = dlsym(RTLD_NEXT, "free");
     if (NULL == real_free) {
         return;
@@ -99,14 +101,6 @@
     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);
     }
@@ -115,13 +109,16 @@
         return real_memalign(alignment, size);
     }
 
-    if ((0 == page_size) || (0 == alignment) || (0 == size)
-            || (0 == new_alignment)) {
+    if ((0 == page_size) || (0 == alignment) || (0 == size)) {
         return real_memalign(alignment, size);
     }
 #ifdef CHECK_OVERFLOW
-    if (0 != (size % new_alignment)) {
-        aligned_size = size + (new_alignment - (size % new_alignment));
+    /* User specified alignment is not respected and is overridden by
+     * MINIMUM_ALIGNMENT. This is required to catch OOB read when read offset
+     * is less than user specified alignment. "MINIMUM_ALIGNMENT" helps to
+     * avoid bus errors due to non-aligned memory.                         */
+    if (0 != (size % MINIMUM_ALIGNMENT)) {
+        aligned_size = size + (MINIMUM_ALIGNMENT - (size % MINIMUM_ALIGNMENT));
     }
 #endif
 
@@ -134,11 +131,7 @@
     total_size = (num_pages * page_size);
     start_ptr = (char *) real_memalign(page_size, total_size);
 #ifdef CHECK_OVERFLOW
-#ifdef FORCE_UNALIGN
-    mem_ptr = (char *) start_ptr + ((num_pages - 1) * page_size) - size;
-#else
     mem_ptr = (char *) start_ptr + ((num_pages - 1) * page_size) - aligned_size;
-#endif /* FORCE_UNALIGN */
     DISABLE_MEM_ACCESS((start_ptr + ((num_pages - 1) * page_size)), page_size);
 #endif /* CHECK_OVERFLOW */
 #ifdef CHECK_UNDERFLOW
@@ -154,6 +147,7 @@
     return mem_ptr;
 }
 
+#ifndef DISABLE_MALLOC_OVERLOADING
 void *malloc(size_t size) {
     if (s_memutils_initialized == 0) {
         memutils_init();
@@ -163,7 +157,7 @@
         return real_malloc(size);
     }
 #endif /* ENABLE_SELECTIVE_OVERLOADING */
-    return memalign(sizeof(size_t), size);
+    return memalign(MINIMUM_ALIGNMENT, size);
 }
 
 void *calloc(size_t nitems, size_t size) {
@@ -210,6 +204,7 @@
     }
     return real_realloc(ptr, size);
 }
+#endif /* DISABLE_MALLOC_OVERLOADING */
 
 void free(void *ptr) {
     if (s_memutils_initialized == 0) {
diff --git a/hostsidetests/securitybulletin/securityPatch/includes/memutils.h b/hostsidetests/securitybulletin/securityPatch/includes/memutils.h
index 10ee31e..4d3791e 100644
--- a/hostsidetests/securitybulletin/securityPatch/includes/memutils.h
+++ b/hostsidetests/securitybulletin/securityPatch/includes/memutils.h
@@ -19,6 +19,7 @@
 #endif /* __cplusplus */
 #define MAX_ENTRIES        (1024 * 1024)
 #define INITIAL_VAL        (0xBE)
+#define MINIMUM_ALIGNMENT  (16)
 
 #define DISABLE_MEM_ACCESS(mem, size)\
     mprotect((char *) mem, size, PROT_NONE);
@@ -43,9 +44,11 @@
 } map_struct_t;
 
 static void* (*real_memalign)(size_t, size_t) = NULL;
+#ifndef DISABLE_MALLOC_OVERLOADING
 static void* (*real_calloc)(size_t, size_t) = NULL;
 static void* (*real_malloc)(size_t) = NULL;
 static void* (*real_realloc)(void *ptr, size_t size) = NULL;
+#endif /* DISABLE_MALLOC_OVERLOADING */
 static void (*real_free)(void *) = NULL;
 static int s_memutils_initialized = 0;
 static int s_mem_map_index = 0;