Merge "Fix some bionic death tests."
diff --git a/libc/Android.bp b/libc/Android.bp
index 6a3a7c0..9427960 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -2078,6 +2078,7 @@
     srcs: ["arch-common/bionic/crtbegin_so.c"],
 
     defaults: ["crt_so_defaults"],
+    bazel_module: { bp2build_available: true },
 }
 
 cc_object {
diff --git a/libc/bionic/iconv.cpp b/libc/bionic/iconv.cpp
index 015d70f..79a429c 100644
--- a/libc/bionic/iconv.cpp
+++ b/libc/bionic/iconv.cpp
@@ -356,6 +356,10 @@
     errno = EBADF;
     return -1;
   }
+
+  // Since none of our encodings are stateful, state flushing is a no-op.
+  if (!__src_buf) return 0;
+
   return __converter->Convert(__src_buf, __src_bytes_left, __dst_buf, __dst_bytes_left);
 }
 
diff --git a/libc/private/bionic_inline_raise.h b/libc/private/bionic_inline_raise.h
index 7223b4e..8565c80 100644
--- a/libc/private/bionic_inline_raise.h
+++ b/libc/private/bionic_inline_raise.h
@@ -60,8 +60,18 @@
   register long x3 __asm__("x3") = reinterpret_cast<long>(&info);
   register long x8 __asm__("x8") = __NR_rt_tgsigqueueinfo;
   __asm__("svc #0" : "=r"(x0) : "r"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x8) : "memory");
+#elif defined(__x86_64__)
+  register long rax __asm__("rax") = __NR_rt_tgsigqueueinfo;
+  register long rdi __asm__("rdi") = pid;
+  register long rsi __asm__("rsi") = tid;
+  register long rdx __asm__("rdx") = sig;
+  register long r10 __asm__("r10") = reinterpret_cast<long>(&info);
+  __asm__("syscall"
+          : "+r"(rax)
+          : "r"(rdi), "r"(rsi), "r"(rdx), "r"(r10)
+          : "memory", "cc", "r11", "rcx");
 #else
+  // 32-bit x86 is a huge mess, so don't even bother...
   syscall(__NR_rt_tgsigqueueinfo, pid, tid, sig, &info);
 #endif
 }
-
diff --git a/linker/Android.bp b/linker/Android.bp
index 06e6fbc..8b2d842 100644
--- a/linker/Android.bp
+++ b/linker/Android.bp
@@ -391,7 +391,7 @@
 
 sh_binary {
     name: "ldd",
-    src: "ldd",
+    src: "ldd.sh",
     bazel_module: { bp2build_available: true },
 }
 
diff --git a/linker/ldd b/linker/ldd.sh
similarity index 100%
rename from linker/ldd
rename to linker/ldd.sh
diff --git a/tests/BionicDeathTest.h b/tests/BionicDeathTest.h
index 6826ab7..f70839a 100644
--- a/tests/BionicDeathTest.h
+++ b/tests/BionicDeathTest.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef BIONIC_TESTS_BIONIC_DEATH_TEST_H_
-#define BIONIC_TESTS_BIONIC_DEATH_TEST_H_
+#pragma once
 
 #include <signal.h>
 
@@ -44,5 +43,3 @@
  private:
   struct sigaction64 previous_;
 };
-
-#endif // BIONIC_TESTS_BIONIC_DEATH_TEST_H_
diff --git a/tests/assert_test.cpp b/tests/assert_test.cpp
index 9436151..714e22a 100644
--- a/tests/assert_test.cpp
+++ b/tests/assert_test.cpp
@@ -19,14 +19,18 @@
 #undef NDEBUG
 #include <assert.h>
 
+#include "BionicDeathTest.h"
+
+using assert_DeathTest = BionicDeathTest;
+
 TEST(assert, assert_true) {
   assert(true);
 }
 
-TEST(assert, assert_false) {
+TEST_F(assert_DeathTest, assert_false) {
   EXPECT_DEATH(assert(false),
                "bionic/tests/assert_test.cpp:.*: "
-               "virtual void assert_assert_false_Test::TestBody\\(\\): "
+               "virtual void assert_DeathTest_assert_false_Test::TestBody\\(\\): "
                "assertion \"false\" failed");
 }
 
diff --git a/tests/clang_fortify_tests.cpp b/tests/clang_fortify_tests.cpp
index 0d17284..0355170 100644
--- a/tests/clang_fortify_tests.cpp
+++ b/tests/clang_fortify_tests.cpp
@@ -99,37 +99,9 @@
 
 #define CONCAT2(x, y) x##y
 #define CONCAT(x, y) CONCAT2(x, y)
-#define FORTIFY_TEST_NAME CONCAT(clang_fortify_test_, _FORTIFY_SOURCE)
+#define FORTIFY_TEST_NAME CONCAT(CONCAT(clang_fortify_test_, _FORTIFY_SOURCE), _DeathTest)
 
-namespace {
-struct FORTIFY_TEST_NAME : BionicDeathTest {
- protected:
-  void SetUp() override {
-    stdin_saved = dup(STDIN_FILENO);
-    if (stdin_saved < 0) err(1, "failed to dup stdin");
-
-    int devnull = open("/dev/null", O_RDONLY);
-    if (devnull < 0) err(1, "failed to open /dev/null");
-
-    if (!dup2(devnull, STDIN_FILENO)) err(1, "failed to overwrite stdin");
-    static_cast<void>(close(devnull));
-
-    BionicDeathTest::SetUp();
-  }
-
-  void TearDown() override {
-    if (stdin_saved == -1) return;
-    if (!dup2(stdin_saved, STDIN_FILENO)) warn("failed to restore stdin");
-
-    static_cast<void>(close(stdin_saved));
-
-    BionicDeathTest::TearDown();
-  }
-
- private:
-  int stdin_saved = -1;
-};
-}  // namespace
+using FORTIFY_TEST_NAME = BionicDeathTest;
 
 template <typename Fn>
 __attribute__((noreturn)) static void ExitAfter(Fn&& f) {
@@ -153,7 +125,7 @@
 #define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_NO_DEATH
 #endif
 
-#define FORTIFY_TEST(test_name) TEST(FORTIFY_TEST_NAME, test_name)
+#define FORTIFY_TEST(test_name) TEST_F(FORTIFY_TEST_NAME, test_name)
 
 #else  // defined(COMPILATION_TESTS)
 
diff --git a/tests/fenv_test.cpp b/tests/fenv_test.cpp
index 89c7fd5..d495970 100644
--- a/tests/fenv_test.cpp
+++ b/tests/fenv_test.cpp
@@ -202,6 +202,7 @@
   ASSERT_NE(-1, pid) << strerror(errno);
 
   if (pid == 0) {
+    signal(SIGFPE, SIG_DFL);  // Disable debuggerd.
     feclearexcept(FE_ALL_EXCEPT);
     ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
     ASSERT_EQ(0, feenableexcept(FE_INVALID));
diff --git a/tests/iconv_test.cpp b/tests/iconv_test.cpp
index 768b4fd..bd99000 100644
--- a/tests/iconv_test.cpp
+++ b/tests/iconv_test.cpp
@@ -451,5 +451,13 @@
   EXPECT_EQ(0, errno);
   EXPECT_EQ(sizeof(out_buf), out_bytes);
 
+  // Is a null pointer and so is in_bytes. This isn't specified by POSIX, but
+  // glibc and macOS both allow that, where Android historically didn't.
+  // https://issuetracker.google.com/180598400
+  errno = 0;
+  ASSERT_EQ(static_cast<size_t>(0), iconv(c, nullptr, nullptr, &out, &out_bytes));
+  EXPECT_EQ(0, errno);
+  EXPECT_EQ(sizeof(out_buf), out_bytes);
+
   EXPECT_EQ(0, iconv_close(c));
 }
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index 93ba426..7c2c890 100644
--- a/tests/time_test.cpp
+++ b/tests/time_test.cpp
@@ -851,11 +851,11 @@
 }
 
 TEST(time, clock) {
-  // clock(3) is hard to test, but a 1s sleep should cost less than 10ms.
+  // clock(3) is hard to test, but a 1s sleep should cost less than 20ms.
   clock_t t0 = clock();
   sleep(1);
   clock_t t1 = clock();
-  ASSERT_LT(t1 - t0, 10 * (CLOCKS_PER_SEC / 1000));
+  ASSERT_LT(t1 - t0, 20 * (CLOCKS_PER_SEC / 1000));
 }
 
 static pid_t GetInvalidPid() {