Snap for 6574919 from 13a82a04ae476bad962de11b3156d26c58f5c12f to mainline-release

Change-Id: I0d729457c135ff21d2ff3aea4d0ba0dd4d18faed
diff --git a/libc/bionic/fork.cpp b/libc/bionic/fork.cpp
index cda5e85..3814ed0 100644
--- a/libc/bionic/fork.cpp
+++ b/libc/bionic/fork.cpp
@@ -34,23 +34,27 @@
 #include "pthread_internal.h"
 
 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
+int __clone_for_fork() {
+  pthread_internal_t* self = __get_thread();
+
+  int result = clone(nullptr, nullptr, (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD),
+                     nullptr, nullptr, nullptr, &(self->tid));
+
+  if (result == 0) {
+    // Update the cached pid in child, since clone() will not set it directly (as
+    // self->tid is updated by the kernel).
+    self->set_cached_pid(gettid());
+  }
+
+  return result;
+}
+
 int fork() {
   __bionic_atfork_run_prepare();
 
-  pthread_internal_t* self = __get_thread();
+  int result = __clone_for_fork();
 
-  int result = clone(nullptr,
-                     nullptr,
-                     (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD),
-                     nullptr,
-                     nullptr,
-                     nullptr,
-                     &(self->tid));
   if (result == 0) {
-    // Update the cached pid, since clone() will not set it directly (as
-    // self->tid is updated by the kernel).
-    self->set_cached_pid(gettid());
-
     // Disable fdsan post-fork, so we don't falsely trigger on processes that
     // fork, close all of their fds blindly, and then exec.
     android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_DISABLED);