Merge "Also install STLs to thumb paths."
diff --git a/run_tests.py b/run_tests.py
index a62aa51..001bff4 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -647,7 +647,10 @@
     site.addsitedir(python_packages)
 
     if not os.path.exists(args.test_dir):
-        sys.exit('Test directory does not exist: {}'.format(args.test_dir))
+        if args.rebuild:
+            os.makedirs(args.test_dir)
+        else:
+            sys.exit('Test directory does not exist: {}'.format(args.test_dir))
 
     test_config = get_config_dict(
         args.config, args.abi, args.toolchain, args.pie)
diff --git a/sources/android/support/Android.mk b/sources/android/support/Android.mk
index c508daa..7f594b0 100644
--- a/sources/android/support/Android.mk
+++ b/sources/android/support/Android.mk
@@ -66,6 +66,7 @@
     src/musl-stdio/vsprintf.c \
     src/musl-stdio/vwprintf.c \
     src/musl-stdio/wprintf.c \
+    src/posix_memalign.cpp \
     src/stdio/stdio_impl.c \
     src/stdio/strtod.c \
     src/stdio/vfprintf.c \
diff --git a/sources/android/support/include/stdlib.h b/sources/android/support/include/stdlib.h
index 0b47898..58e5e0d 100644
--- a/sources/android/support/include/stdlib.h
+++ b/sources/android/support/include/stdlib.h
@@ -33,6 +33,10 @@
 
 __BEGIN_DECLS
 
+#if __ANDROID_API__ < 16
+int posix_memalign(void** memptr, size_t alignment, size_t size);
+#endif
+
 // These APIs made it in to L.
 #if __ANDROID_API__ < 21
 
diff --git a/sources/android/support/src/posix_memalign.cpp b/sources/android/support/src/posix_memalign.cpp
new file mode 100644
index 0000000..cf7abbb
--- /dev/null
+++ b/sources/android/support/src/posix_memalign.cpp
@@ -0,0 +1,20 @@
+#include <errno.h>
+#include <malloc.h>
+#include <stdlib.h>
+
+int posix_memalign(void** memptr, size_t alignment, size_t size) {
+  if ((alignment & (alignment - 1)) != 0 || alignment == 0) {
+    return EINVAL;
+  }
+
+  if (alignment % sizeof(void*) != 0) {
+    return EINVAL;
+  }
+
+  *memptr = memalign(alignment, size);
+  if (*memptr == NULL) {
+    return errno;
+  }
+
+  return 0;
+}
diff --git a/sources/android/support/tests/Android.mk b/sources/android/support/tests/Android.mk
index b3d697e..75ec1f8 100644
--- a/sources/android/support/tests/Android.mk
+++ b/sources/android/support/tests/Android.mk
@@ -6,6 +6,7 @@
 LOCAL_SRC_FILES := \
   ctype_test.cpp \
   math_test.cpp \
+  posix_memalign_test.cpp \
   stdio_test.cpp \
   wchar_test.cpp \
 
diff --git a/sources/android/support/tests/posix_memalign_test.cpp b/sources/android/support/tests/posix_memalign_test.cpp
new file mode 100644
index 0000000..61f4626
--- /dev/null
+++ b/sources/android/support/tests/posix_memalign_test.cpp
@@ -0,0 +1,40 @@
+#include <gtest/gtest.h>
+
+TEST(stdlib, posix_memalign_sweep) {
+  void* ptr;
+
+  // These should all fail.
+  for (size_t align = 0; align < sizeof(long); align++) {
+    ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256))
+        << "Unexpected value at align " << align;
+  }
+
+  // Verify powers of 2 up to 2048 allocate, and verify that all other
+  // alignment values between the powers of 2 fail.
+  size_t last_align = sizeof(long);
+  for (size_t align = sizeof(long); align <= 2048; align <<= 1) {
+    // Try all of the non power of 2 values from the last until this value.
+    for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
+      ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256))
+          << "Unexpected success at align " << fail_align;
+    }
+    ASSERT_EQ(0, posix_memalign(&ptr, align, 256))
+        << "Unexpected failure at align " << align;
+    ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
+        << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
+    free(ptr);
+    last_align = align;
+  }
+}
+
+TEST(stdlib, posix_memalign_various_sizes) {
+  std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000};
+  for (auto size : sizes) {
+    void* ptr;
+    ASSERT_EQ(0, posix_memalign(&ptr, 16, 1))
+        << "posix_memalign failed at size " << size;
+    ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf)
+        << "Pointer not aligned at size " << size << " ptr " << ptr;
+    free(ptr);
+  }
+}
diff --git a/tests/build/issue17144-byteswap/build.sh b/tests/build/issue17144-byteswap/build.sh
deleted file mode 100755
index 2589175..0000000
--- a/tests/build/issue17144-byteswap/build.sh
+++ /dev/null
@@ -1,54 +0,0 @@
-# Check if APP_ABI=armeabi-v7a use "rev" instructions for __swapXX
-# See https://android-review.googlesource.com/#/c/17144
-#
-
-fail_panic ()
-{
-    if [ $? != 0 ] ; then
-        echo "ERROR: $@"
-        exit 1
-    fi
-}
-
-for opt do
-    optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
-    case "$opt" in
-    APP_ABI=*)
-        APP_ABI=$optarg
-        ;;
-    esac
-done
-
-if [ -z "$APP_ABI" -o "$APP_ABI" = "all" -o "$APP_ABI" != "${APP_ABI%%armeabi-v7a*}" ]; then
-    # checking armeabi-v7a
-    $NDK/ndk-build -B APP_ABI=armeabi-v7a APP_CFLAGS=-save-temps NDK_DEBUG=1
-    fail_panic "can't compile for APP_ABI=armeabi-v7a"
-    grep -qw rev16 issue17144-byteswap.s
-    fail_panic "armeabi-v7a doesn't use rev16 instruction for __swap16()"
-    grep -qw rev issue17144-byteswap.s
-    fail_panic "armeabi-v7a doesn't use rev instruction for __swap32()"
-fi
-
-if [ -z "$APP_ABI" -o "$APP_ABI" = "all" -o "$APP_ABI" != "${APP_ABI%%x86}" ]; then
-    # checking x86
-    $NDK/ndk-build -B APP_ABI=x86 APP_CFLAGS=-save-temps NDK_DEBUG=1
-    fail_panic "can't compile for x86"
-    grep -qw 'ro[lr]w' issue17144-byteswap.s
-    fail_panic "x86 doesn't use rorw instruction for __swap16()"
-    egrep -qw 'bswapl?' issue17144-byteswap.s
-    fail_panic "x86 doesn't use bswap instruction for __swap32()"
-fi
-
-if [ -z "$APP_ABI" -o "$APP_ABI" = "all" -o "$APP_ABI" != "${APP_ABI%%mips}" ]; then
-    # checking mips
-    # Note that MD_SWAP in machine/endian.h is only defined for r2.  Add
-    # -mips32r2 because default Android toolchain support r1
-    $NDK/ndk-build -B APP_ABI=mips APP_CFLAGS="-save-temps -mips32r2" NDK_DEBUG=1
-    fail_panic "can't compile for mips"
-    grep -qw wsbh issue17144-byteswap.s
-    fail_panic "mips doesn't use wsbh instruction for __swap16()"
-    grep -wA1 wsbh issue17144-byteswap.s | egrep -qw 'rot?r'
-    fail_panic "mips doesn't use wsbh/rotr instruction for __swap32()"
-fi
-
-rm -rf libs obj issue17144-byteswap.*
diff --git a/tests/build/issue17144-byteswap/jni/Android.mk b/tests/build/issue17144-byteswap/jni/Android.mk
deleted file mode 100644
index a382402..0000000
--- a/tests/build/issue17144-byteswap/jni/Android.mk
+++ /dev/null
@@ -1,6 +0,0 @@
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-LOCAL_MODULE := issue17144-byteswap
-LOCAL_SRC_FILES := issue17144-byteswap.c
-include $(BUILD_EXECUTABLE)
diff --git a/tests/build/issue17144-byteswap/jni/Application.mk b/tests/build/issue17144-byteswap/jni/Application.mk
deleted file mode 100644
index a252a72..0000000
--- a/tests/build/issue17144-byteswap/jni/Application.mk
+++ /dev/null
@@ -1 +0,0 @@
-APP_ABI := all
diff --git a/tests/build/issue17144-byteswap/jni/issue17144-byteswap.c b/tests/build/issue17144-byteswap/jni/issue17144-byteswap.c
deleted file mode 100644
index a9d094e..0000000
--- a/tests/build/issue17144-byteswap/jni/issue17144-byteswap.c
+++ /dev/null
@@ -1,28 +0,0 @@
-#include <inttypes.h>
-#include <sys/endian.h>
-
-int16_t I16;
-int32_t I32;
-int64_t I64;
-
-int16_t f16(int16_t i)
-{
-    return __swap16(i);
-}
-
-int32_t f32(int32_t i)
-{
-    return __swap32(i);
-}
-
-/* No need to check __swap64 because in all supported
-   32-bit architectures it's implemented with two __swap32
-int64_t f64(int64_t i)
-{
-    return __swap64(i);
-}
-*/
-
-int main()
-{
-}
diff --git a/tests/build/issue17144-byteswap/test_config.py b/tests/build/issue17144-byteswap/test_config.py
deleted file mode 100644
index 81008f0..0000000
--- a/tests/build/issue17144-byteswap/test_config.py
+++ /dev/null
@@ -1,4 +0,0 @@
-def build_broken(abi, platform, toolchain):
-    if toolchain == 'clang' and abi.startswith('armeabi-v7a'):
-        return '{} {}'.format(toolchain, abi), 'http://b/26091410'
-    return None, None