From 018f4a13108cd686b8745308ebf79471791706b4 Mon Sep 17 00:00:00 2001 From: Lev Rumyantsev Date: Mon, 1 Jun 2020 18:43:55 -0700 Subject: Narrow native bridge to clone_for_fork We are removing native bridge copy of fork.cpp, but need to replace call to clone() when it's done for bionic's fork. The code here will run all pre-/post-clone routines for *guest*, while native bridge implementation will need to run the corresponding *host* routines. Bug: 145028007 Test: bionic-unit-tests Merged-In: Ic5524e743caa287d7aaa8dc7e5d34acd1c7e1170 Change-Id: Ic5524e743caa287d7aaa8dc7e5d34acd1c7e1170 (cherry picked from commit 41127dca3d08e5eb350b678ee03eae30ab779921) --- libc/bionic/fork.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'libc/bionic/fork.cpp') diff --git a/libc/bionic/fork.cpp b/libc/bionic/fork.cpp index cda5e8533..c9a1ca839 100644 --- a/libc/bionic/fork.cpp +++ b/libc/bionic/fork.cpp @@ -33,23 +33,24 @@ #include "private/bionic_defs.h" #include "pthread_internal.h" +__BIONIC_WEAK_FOR_NATIVE_BRIDGE +int __clone_for_fork() { + pthread_internal_t* self = __get_thread(); + + return clone(nullptr, nullptr, (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD), nullptr, + nullptr, nullptr, &(self->tid)); +} + __BIONIC_WEAK_FOR_NATIVE_BRIDGE 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()); + __get_thread()->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. -- cgit v1.2.3 From 8b26ca41647096776a5170a84ac65eccadc6732d Mon Sep 17 00:00:00 2001 From: Lev Rumyantsev Date: Mon, 1 Jun 2020 18:43:55 -0700 Subject: Remove WEAK_FOR_NATIVE_BRIDGE for fork Bug: 145028007 Test: bionic-unit-tests Merged-In: I0b019cd08c9d7426a953bd199f2ecd6dd42db00e Change-Id: I0b019cd08c9d7426a953bd199f2ecd6dd42db00e (cherry picked from commit 520398ffe90a45e2f2a85f56230055f9f2f6bf02) --- libc/bionic/fork.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'libc/bionic/fork.cpp') diff --git a/libc/bionic/fork.cpp b/libc/bionic/fork.cpp index c9a1ca839..a3fdcd2d5 100644 --- a/libc/bionic/fork.cpp +++ b/libc/bionic/fork.cpp @@ -41,7 +41,6 @@ int __clone_for_fork() { nullptr, nullptr, &(self->tid)); } -__BIONIC_WEAK_FOR_NATIVE_BRIDGE int fork() { __bionic_atfork_run_prepare(); -- cgit v1.2.3 From c8c3bc58a1e2acb1048e2fb4f2914f60e0977942 Mon Sep 17 00:00:00 2001 From: Lev Rumyantsev Date: Mon, 1 Jun 2020 18:43:55 -0700 Subject: Move set_cached_pid() to __clone_for_fork() Bug: 145028007 Test: bionic-unit-tests Merged-In: I3c697924f2a3ef1804a688dd1fe9669f6b7a71bf Change-Id: I3c697924f2a3ef1804a688dd1fe9669f6b7a71bf (cherry picked from commit 3a4c2ffff80402f6c250f1491302aa7d65411349) --- libc/bionic/fork.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'libc/bionic/fork.cpp') diff --git a/libc/bionic/fork.cpp b/libc/bionic/fork.cpp index a3fdcd2d5..3814ed0ac 100644 --- a/libc/bionic/fork.cpp +++ b/libc/bionic/fork.cpp @@ -37,8 +37,16 @@ __BIONIC_WEAK_FOR_NATIVE_BRIDGE int __clone_for_fork() { pthread_internal_t* self = __get_thread(); - return clone(nullptr, nullptr, (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD), nullptr, - nullptr, nullptr, &(self->tid)); + 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() { @@ -47,10 +55,6 @@ int fork() { int result = __clone_for_fork(); if (result == 0) { - // Update the cached pid, since clone() will not set it directly (as - // self->tid is updated by the kernel). - __get_thread()->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); -- cgit v1.2.3