Merge "Add testcase for issue 52805 about missing set_new_handler"
diff --git a/sources/cxx-stl/gabi++/include/new b/sources/cxx-stl/gabi++/include/new
index 7687c9b..d66fef7 100644
--- a/sources/cxx-stl/gabi++/include/new
+++ b/sources/cxx-stl/gabi++/include/new
@@ -48,6 +48,9 @@
   virtual const char* what() const throw();
 };
 
+typedef void (*new_handler)();
+new_handler set_new_handler(new_handler) throw();
+
 }
 
 void* operator new(std::size_t size) throw(std::bad_alloc);
diff --git a/sources/cxx-stl/gabi++/src/new.cc b/sources/cxx-stl/gabi++/src/new.cc
index 8148556..f65a291 100644
--- a/sources/cxx-stl/gabi++/src/new.cc
+++ b/sources/cxx-stl/gabi++/src/new.cc
@@ -29,6 +29,11 @@
 #include <stdlib.h>
 #include <new>
 
+using std::new_handler;
+namespace {
+  new_handler cur_handler;
+}
+
 namespace std {
 
 #if !defined(GABIXX_LIBCXX)
@@ -45,21 +50,37 @@
     return "std::bad_alloc";
   }
 
+  new_handler set_new_handler(new_handler next_handler) throw() {
+    new_handler old_handler = cur_handler;
+    cur_handler = next_handler;
+    return old_handler;
+  }
+
 } // namespace std
 
 __attribute__ ((weak))
 void* operator new(std::size_t size) throw(std::bad_alloc) {
-  void* space = ::operator new(size, std::nothrow_t());
-  if (space) {
-    return space;
-  } else {
-    throw std::bad_alloc();
-  }
+  void* space;
+  do {
+    space = malloc(size);
+    if (space) {
+      return space;
+    }
+    new_handler handler = cur_handler;
+    if (handler == NULL) {
+      throw std::bad_alloc();
+    }
+    handler();
+  } while (space == 0);
 }
 
 __attribute__ ((weak))
 void* operator new(std::size_t size, const std::nothrow_t& no) throw() {
-  return malloc(size);
+  try {
+    ::operator new(size);
+  } catch (const std::bad_alloc&) {
+    return 0;
+  }
 }
 
 __attribute__ ((weak))
diff --git a/tests/run-tests.sh b/tests/run-tests.sh
index 8a5d3e6..6c510b7 100755
--- a/tests/run-tests.sh
+++ b/tests/run-tests.sh
@@ -669,7 +669,6 @@
         local DSTDIR="$4/ndk-tests"
         local SRCFILE
         local DSTFILE
-        local PROGRAMS=
         local PROGRAM
         # Do not run the test if BROKEN_RUN is defined
         if [ -z "$RUN_TESTS" ]; then
@@ -704,11 +703,35 @@
         # First, copy all files to the device, except for gdbserver, gdb.setup, and
         # those declared in $TEST/BROKEN_RUN
         adb_shell_mkdir "$DEVICE" $DSTDIR
+
+        for SRCFILE in `ls $SRCDIR`; do
+            DSTFILE=`basename $SRCFILE`
+            echo "$DSTFILE" | grep -q -e '\.so$'
+            if [ $? != 0 ] ; then
+                continue
+            fi
+            SRCFILE="$SRCDIR/$SRCFILE"
+            if [ $HOST_OS = cygwin ]; then
+                SRCFILE=`cygpath -m $SRCFILE`
+            fi
+            DSTFILE="$DSTDIR/$DSTFILE"
+            run $ADB_CMD -s "$DEVICE" push "$SRCFILE" "$DSTFILE" &&
+            run $ADB_CMD -s "$DEVICE" shell chmod 0755 $DSTFILE
+            if [ $? != 0 ] ; then
+                dump "ERROR: Could not install $SRCFILE to device $DEVICE!"
+                exit 1
+            fi
+        done
+
         for SRCFILE in `ls $SRCDIR`; do
             DSTFILE=`basename $SRCFILE`
             if [ "$DSTFILE" = "gdbserver" -o "$DSTFILE" = "gdb.setup" ] ; then
                 continue
             fi
+            echo "$DSTFILE" | grep -q -e '\.so$'
+            if [ $? = 0 ] ; then
+              continue
+            fi
             if [ -z "$RUN_TESTS" -a -f "$TEST/BROKEN_RUN" ]; then
                 grep -q -w -e "$DSTFILE" "$TEST/BROKEN_RUN"
                 if [ $? = 0 ] ; then
@@ -726,13 +749,7 @@
                 dump "ERROR: Could not install $SRCFILE to device $DEVICE!"
                 exit 1
             fi
-            # If its name doesn't end with .so, add it to PROGRAMS
-            echo "$DSTFILE" | grep -q -e '\.so$'
-            if [ $? != 0 ] ; then
-                PROGRAMS="$PROGRAMS `basename $DSTFILE`"
-            fi
-        done
-        for PROGRAM in $PROGRAMS; do
+            PROGRAM="`basename $DSTFILE`"
             dump "Running device test [$CPU_ABI]: $TEST_NAME (`basename $PROGRAM`)"
             adb_var_shell_cmd "$DEVICE" "" "cd $DSTDIR && LD_LIBRARY_PATH=$DSTDIR ./$PROGRAM"
             if [ $? != 0 ] ; then