scripts/unit-test: Detect problematic valgrind

Instead of permanently blacklisting valgrind from running in our unit
test environment for all powerpc platforms, run a problematic test case
to determine if we can use valgrind or not.

Tested:
    Ran on amd64 and ppc64el to verify that valgrind is still run on the
    currently working amd64 and not on the currently broken ppc64el.

Change-Id: Ie028a5cc9ec9138c78a87f58c70af274adca2c95
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/scripts/unit-test.py b/scripts/unit-test.py
index da64a81..094c40c 100755
--- a/scripts/unit-test.py
+++ b/scripts/unit-test.py
@@ -554,7 +554,31 @@
     """
     Returns whether it is safe to run valgrind on our platform
     """
-    return re.match('ppc64', platform.machine()) is None
+    src = 'unit-test-vg.c'
+    exe = './unit-test-vg'
+    with open(src, 'w') as h:
+        h.write('#include <stdlib.h>\n')
+        h.write('#include <string.h>\n')
+        h.write('int main() {\n')
+        h.write('char *heap_str = malloc(16);\n')
+        h.write('strcpy(heap_str, "RandString");\n')
+        h.write('int res = strcmp("RandString", heap_str);\n')
+        h.write('free(heap_str);\n')
+        h.write('return res;\n')
+        h.write('}\n')
+    try:
+        with open(os.devnull, 'w') as devnull:
+            check_call(['gcc', '-O2', '-o', exe, src],
+                       stdout=devnull, stderr=devnull)
+            check_call(['valgrind', '--error-exitcode=99', exe],
+                       stdout=devnull, stderr=devnull)
+        return True
+    except:
+        sys.stderr.write("###### Platform is not valgrind safe ######\n")
+        return False
+    finally:
+        os.remove(src)
+        os.remove(exe)
 
 def is_sanitize_safe():
     """