Merge "Revert "Revert "Make libc.so global"""
diff --git a/libc/bionic/pthread_internal.cpp b/libc/bionic/pthread_internal.cpp
index 51430e7..cda52aa 100644
--- a/libc/bionic/pthread_internal.cpp
+++ b/libc/bionic/pthread_internal.cpp
@@ -110,7 +110,7 @@
 
   // Historically we'd return null, but
   if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
-    __libc_fatal("attempt to use invalid pthread_t");
+    __libc_fatal("invalid pthread_t %p passed to libc", thread);
   }
   return nullptr;
 }
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index 94860d9..dab252d 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -347,18 +347,6 @@
 }
 #endif
 
-/*
- * TODO(danalbert): Remove this once we've moved entirely off prebuilts/ndk.
- *
- * The NDK used to have a __NDK_FPABI__ that was defined to empty for most cases
- * but `__attribute__((pcs("aapcs")))` for the now defunct armeabi-v7a-hard ABI.
- *
- * During the transition from prebuilts/ndk to ndk_headers, we'll have some
- * headers that still use __NDK_FPABI__ while the libc headers have stopped
- * defining it. In the interim, just provide an empty definition to keep the
- * build working.
- */
-#define __NDK_FPABI__
 #if defined(__clang__)
 /*
  * Used when we need to check for overflow when multiplying x and y. This
diff --git a/linker/Android.bp b/linker/Android.bp
index 5c205d5..aab05b4 100644
--- a/linker/Android.bp
+++ b/linker/Android.bp
@@ -117,9 +117,17 @@
         "libutils",
         "libbase",
         "libz",
+
+        "libdebuggerd_handler_core",
+        "libdebuggerd_handler_fallback",
+        "libdebuggerd",
+        "libbacktrace",
+        "libunwind",
+        "liblzma",
+        "libcutils",
+
         "liblog",
         "libc++_static",
-        "libdebuggerd_handler",
 
         // Important: The liblinker_malloc should be the last library in the list
         // to overwrite any other malloc implementations by other static libraries.
diff --git a/linker/linker_memory.cpp b/linker/linker_memory.cpp
index 3920e8c..18ef93e 100644
--- a/linker/linker_memory.cpp
+++ b/linker/linker_memory.cpp
@@ -29,22 +29,51 @@
 #include "linker_allocator.h"
 
 #include <stdlib.h>
+#include <sys/cdefs.h>
+#include <unistd.h>
+
+#include "private/libc_logging.h"
 
 static LinkerMemoryAllocator g_linker_allocator;
+static pid_t fallback_tid = 0;
+
+// Used by libdebuggerd_handler to switch allocators during a crash dump, in
+// case the linker heap is corrupted. Do not use this function.
+extern "C" void __linker_use_fallback_allocator() {
+  if (fallback_tid != 0) {
+    __libc_format_log(ANDROID_LOG_ERROR, "libc",
+                      "attempted to set fallback allocator multiple times");
+    return;
+  }
+
+  fallback_tid = gettid();
+}
+
+static LinkerMemoryAllocator& get_fallback_allocator() {
+  static LinkerMemoryAllocator fallback_allocator;
+  return fallback_allocator;
+}
+
+static LinkerMemoryAllocator& get_allocator() {
+  if (__predict_false(fallback_tid) && __predict_false(gettid() == fallback_tid)) {
+    return get_fallback_allocator();
+  }
+  return g_linker_allocator;
+}
 
 void* malloc(size_t byte_count) {
-  return g_linker_allocator.alloc(byte_count);
+  return get_allocator().alloc(byte_count);
 }
 
 void* calloc(size_t item_count, size_t item_size) {
-  return g_linker_allocator.alloc(item_count*item_size);
+  return get_allocator().alloc(item_count*item_size);
 }
 
 void* realloc(void* p, size_t byte_count) {
-  return g_linker_allocator.realloc(p, byte_count);
+  return get_allocator().realloc(p, byte_count);
 }
 
 void free(void* ptr) {
-  g_linker_allocator.free(ptr);
+  get_allocator().free(ptr);
 }
 
diff --git a/tests/sys_ptrace_test.cpp b/tests/sys_ptrace_test.cpp
index 7483754..8fe7a29 100644
--- a/tests/sys_ptrace_test.cpp
+++ b/tests/sys_ptrace_test.cpp
@@ -17,14 +17,21 @@
 #include <sys/ptrace.h>
 
 #include <elf.h>
+#include <fcntl.h>
 #include <sched.h>
 #include <sys/prctl.h>
+#include <sys/ptrace.h>
 #include <sys/uio.h>
 #include <sys/user.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
 #include <gtest/gtest.h>
 
+#include <android-base/unique_fd.h>
+
+using android::base::unique_fd;
+
 // Host libc does not define this.
 #ifndef TRAP_HWBKPT
 #define TRAP_HWBKPT 4
@@ -313,3 +320,94 @@
   ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
   ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
 }
+
+class PtraceResumptionTest : public ::testing::Test {
+ public:
+  pid_t worker = -1;
+  PtraceResumptionTest() {
+  }
+
+  ~PtraceResumptionTest() {
+  }
+
+  void AssertDeath(int signo);
+  void Start(std::function<void()> f) {
+    unique_fd worker_pipe_read, worker_pipe_write;
+    int pipefd[2];
+    ASSERT_EQ(0, pipe2(pipefd, O_CLOEXEC));
+    worker_pipe_read.reset(pipefd[0]);
+    worker_pipe_write.reset(pipefd[1]);
+
+    worker = fork();
+    ASSERT_NE(-1, worker);
+    if (worker == 0) {
+      char buf;
+      worker_pipe_write.reset();
+      TEMP_FAILURE_RETRY(read(worker_pipe_read.get(), &buf, sizeof(buf)));
+      exit(0);
+    }
+
+    pid_t tracer = fork();
+    ASSERT_NE(-1, tracer);
+    if (tracer == 0) {
+      f();
+      if (HasFatalFailure()) {
+        exit(1);
+      }
+      exit(0);
+    }
+
+    int result;
+    pid_t rc = waitpid(tracer, &result, 0);
+    ASSERT_EQ(tracer, rc);
+    EXPECT_TRUE(WIFEXITED(result) || WIFSIGNALED(result));
+    if (WIFEXITED(result)) {
+      if (WEXITSTATUS(result) != 0) {
+        FAIL() << "tracer failed";
+      }
+    }
+
+    rc = waitpid(worker, &result, WNOHANG);
+    ASSERT_EQ(0, rc);
+
+    worker_pipe_write.reset();
+
+    rc = waitpid(worker, &result, 0);
+    ASSERT_EQ(worker, rc);
+    EXPECT_TRUE(WIFEXITED(result));
+    EXPECT_EQ(WEXITSTATUS(result), 0);
+  }
+};
+
+static void wait_for_ptrace_stop(pid_t pid) {
+  while (true) {
+    int status;
+    pid_t rc = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
+    if (rc != pid) {
+      abort();
+    }
+    if (WIFSTOPPED(status)) {
+      return;
+    }
+  }
+}
+
+TEST_F(PtraceResumptionTest, seize) {
+  Start([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
+}
+
+TEST_F(PtraceResumptionTest, seize_interrupt) {
+  Start([this]() {
+    ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
+    ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
+  });
+}
+
+TEST_F(PtraceResumptionTest, seize_interrupt_cont) {
+  Start([this]() {
+    ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
+    ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
+    wait_for_ptrace_stop(worker);
+    ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
+  });
+}