Merge "Fix pthread test race conditions."
diff --git a/tests/fenv_test.cpp b/tests/fenv_test.cpp
index db1bfc3..c914692 100644
--- a/tests/fenv_test.cpp
+++ b/tests/fenv_test.cpp
@@ -16,6 +16,9 @@
 
 #include <gtest/gtest.h>
 
+#include "ScopedSignalHandler.h"
+#include "utils.h"
+
 #include <fenv.h>
 #include <stdint.h>
 
@@ -83,3 +86,122 @@
 TEST(fenv, FE_DFL_ENV_macro) {
   ASSERT_EQ(0, fesetenv(FE_DFL_ENV));
 }
+
+TEST(fenv, feraiseexcept) {
+  feclearexcept(FE_ALL_EXCEPT);
+  ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
+
+  ASSERT_EQ(0, feraiseexcept(FE_DIVBYZERO | FE_OVERFLOW));
+  ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
+}
+
+TEST(fenv, fegetenv_fesetenv) {
+  // Set FE_OVERFLOW only.
+  feclearexcept(FE_ALL_EXCEPT);
+  ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
+  ASSERT_EQ(0, feraiseexcept(FE_OVERFLOW));
+
+  // fegetenv (unlike feholdexcept) leaves the current state untouched...
+  fenv_t state;
+  ASSERT_EQ(0, fegetenv(&state));
+  ASSERT_EQ(FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
+
+  // Dividing by zero sets the appropriate flag...
+  DivideByZero();
+  ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
+
+  // And fesetenv (unlike feupdateenv) clobbers that to return to where
+  // we started.
+  ASSERT_EQ(0, fesetenv(&state));
+  ASSERT_EQ(FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
+}
+
+TEST(fenv, feholdexcept_feupdateenv) {
+  // Set FE_OVERFLOW only.
+  feclearexcept(FE_ALL_EXCEPT);
+  ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
+  ASSERT_EQ(0, feraiseexcept(FE_OVERFLOW));
+
+  // feholdexcept (unlike fegetenv) clears everything...
+  fenv_t state;
+  ASSERT_EQ(0, feholdexcept(&state));
+  ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
+
+  // Dividing by zero sets the appropriate flag...
+  DivideByZero();
+  ASSERT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT));
+
+  // And feupdateenv (unlike fesetenv) merges what we started with
+  // (FE_OVERFLOW) with what we now have (FE_DIVBYZERO).
+  ASSERT_EQ(0, feupdateenv(&state));
+  ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
+}
+
+TEST(fenv, fegetexceptflag_fesetexceptflag) {
+  // Set three flags.
+  feclearexcept(FE_ALL_EXCEPT);
+  ASSERT_EQ(0, feraiseexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW));
+  ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
+
+  fexcept_t all; // FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW
+  fexcept_t two; // FE_OVERFLOW | FE_UNDERFLOW
+  ASSERT_EQ(0, fegetexceptflag(&all, FE_ALL_EXCEPT));
+  ASSERT_EQ(0, fegetexceptflag(&two, FE_OVERFLOW | FE_UNDERFLOW));
+
+  // Check we can restore all.
+  feclearexcept(FE_ALL_EXCEPT);
+  ASSERT_EQ(0, fesetexceptflag(&all, FE_ALL_EXCEPT));
+  ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
+
+  // Check that `two` only stored a subset.
+  feclearexcept(FE_ALL_EXCEPT);
+  ASSERT_EQ(0, fesetexceptflag(&two, FE_ALL_EXCEPT));
+  ASSERT_EQ(FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
+
+  // Check that we can restore a single flag.
+  feclearexcept(FE_ALL_EXCEPT);
+  ASSERT_EQ(0, fesetexceptflag(&all, FE_DIVBYZERO));
+  ASSERT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT));
+
+  // Check that we can restore a subset of flags.
+  feclearexcept(FE_ALL_EXCEPT);
+  ASSERT_EQ(0, fesetexceptflag(&all, FE_OVERFLOW | FE_UNDERFLOW));
+  ASSERT_EQ(FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
+}
+
+TEST(fenv, fedisableexcept_fegetexcept) {
+  feclearexcept(FE_ALL_EXCEPT);
+  ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
+
+  // No SIGFPE please...
+  ASSERT_EQ(0, fedisableexcept(FE_ALL_EXCEPT));
+  ASSERT_EQ(0, fegetexcept());
+  ASSERT_EQ(0, feraiseexcept(FE_INVALID));
+  ASSERT_EQ(FE_INVALID, fetestexcept(FE_ALL_EXCEPT));
+}
+
+TEST(fenv, feenableexcept_fegetexcept) {
+#if defined(__aarch64__) || defined(__arm__)
+  // Unsupported.
+  // arm:
+  // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.100403_0200_00_en/lau1442504290459.html
+  // aarch64:
+  // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0488h/way1382990760439.html
+  GTEST_LOG_(INFO) << "arm and arm64 don't support feenableexcept";
+#else
+  // We can't recover from SIGFPE, so sacrifice a child...
+  pid_t pid = fork();
+  ASSERT_NE(-1, pid) << strerror(errno);
+
+  if (pid == 0) {
+    feclearexcept(FE_ALL_EXCEPT);
+    ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
+    ASSERT_EQ(0, feenableexcept(FE_INVALID));
+    ASSERT_EQ(FE_INVALID, fegetexcept());
+    ASSERT_EQ(0, feraiseexcept(FE_INVALID));
+    _exit(123);
+  }
+
+  AssertChildExited(pid, -SIGFPE);
+#endif
+}
diff --git a/tests/math_test.cpp b/tests/math_test.cpp
index 466e697..d029311 100644
--- a/tests/math_test.cpp
+++ b/tests/math_test.cpp
@@ -177,10 +177,19 @@
 }
 
 // Historical BSD cruft that isn't exposed in <math.h> any more.
+extern "C" int __fpclassify(double);
 extern "C" int __fpclassifyd(double);
 extern "C" int __fpclassifyf(float);
 extern "C" int __fpclassifyl(long double);
 
+TEST(MATH_TEST, __fpclassify) {
+  ASSERT_EQ(FP_INFINITE, __fpclassify(HUGE_VAL));
+  ASSERT_EQ(FP_NAN, __fpclassify(nan("")));
+  ASSERT_EQ(FP_NORMAL, __fpclassify(1.0));
+  ASSERT_EQ(FP_SUBNORMAL, __fpclassify(double_subnormal()));
+  ASSERT_EQ(FP_ZERO, __fpclassify(0.0));
+}
+
 TEST(MATH_TEST, __fpclassifyd) {
 #if defined(__GLIBC__)
 #define __fpclassifyd __fpclassify
@@ -254,9 +263,15 @@
 }
 
 // Historical BSD cruft that isn't exposed in <math.h> any more.
+extern "C" int __isinf(double);
 extern "C" int __isinff(float);
 extern "C" int __isinfl(long double);
 
+TEST(MATH_TEST, __isinf) {
+  ASSERT_FALSE(__isinf(123.0));
+  ASSERT_TRUE(__isinf(HUGE_VAL));
+}
+
 TEST(MATH_TEST, __isinff) {
   ASSERT_FALSE(__isinff(123.0f));
   ASSERT_TRUE(__isinff(HUGE_VALF));
@@ -274,9 +289,15 @@
 }
 
 // Historical BSD cruft that isn't exposed in <math.h> any more.
+extern "C" int __isnan(double);
 extern "C" int __isnanf(float);
 extern "C" int __isnanl(long double);
 
+TEST(MATH_TEST, __isnan) {
+  ASSERT_FALSE(__isnan(123.0));
+  ASSERT_TRUE(__isnan(nan("")));
+}
+
 TEST(MATH_TEST, __isnanf) {
   ASSERT_FALSE(__isnanf(123.0f));
   ASSERT_TRUE(__isnanf(nanf("")));
diff --git a/tools/update_notice.sh b/tools/update_notice.sh
index 9974da2..0b8a106 100755
--- a/tools/update_notice.sh
+++ b/tools/update_notice.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 cd $DIR/..
 ./libc/tools/generate-NOTICE.py libc libdl libm linker libstdc++ > libc/NOTICE
diff --git a/tools/update_seccomp.sh b/tools/update_seccomp.sh
index d108e35..b9e53aa 100755
--- a/tools/update_seccomp.sh
+++ b/tools/update_seccomp.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 cd $DIR/..
 ./libc/tools/genseccomp.py
diff --git a/tools/update_syscalls.sh b/tools/update_syscalls.sh
index 3ad95ed..5e7eb0a 100755
--- a/tools/update_syscalls.sh
+++ b/tools/update_syscalls.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 cd $DIR/..
 ./libc/tools/gensyscalls.py
diff --git a/tools/update_version_scripts.sh b/tools/update_version_scripts.sh
index ccba475..2c3a5b4 100755
--- a/tools/update_version_scripts.sh
+++ b/tools/update_version_scripts.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 cd $DIR/..
 ./libc/tools/genversion-scripts.py