Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(322)

Unified Diff: sandbox/linux/services/syscall_wrappers.cc

Issue 801033002: Use the libc clone wrapper in sys_clone. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sandbox/linux/services/syscall_wrappers.cc
diff --git a/sandbox/linux/services/syscall_wrappers.cc b/sandbox/linux/services/syscall_wrappers.cc
index c3685197e88c7313e713c803a962b8e2f920582c..744cdf01fd79c9e30cab4279641af95288aa66f0 100644
--- a/sandbox/linux/services/syscall_wrappers.cc
+++ b/sandbox/linux/services/syscall_wrappers.cc
@@ -4,6 +4,9 @@
#include "sandbox/linux/services/syscall_wrappers.h"
+#include <pthread.h>
+#include <sched.h>
+#include <setjmp.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/time.h>
@@ -24,12 +27,20 @@ pid_t sys_gettid(void) {
return syscall(__NR_gettid);
}
-long sys_clone(unsigned long flags) {
- return sys_clone(flags, nullptr, nullptr, nullptr, nullptr);
+namespace {
+
+int clone_helper(void* arg) {
jln (very slow on Chromium) 2014/12/15 23:31:35 Style: CloneHelper Also add a comment to explain
rickyz (no longer on Chrome) 2014/12/16 00:35:09 Done.
+ jmp_buf* env_ptr = reinterpret_cast<jmp_buf*>(arg);
+ longjmp(*env_ptr, 1);
+
+ NOTREACHED();
jln (very slow on Chromium) 2014/12/15 23:31:35 RAW_CHECK(): probably a good idea to be very conse
rickyz (no longer on Chrome) 2014/12/16 00:35:09 Done.
+ return 1;
}
+} // namespace
+
long sys_clone(unsigned long flags,
- void* child_stack,
+ decltype(nullptr) child_stack,
pid_t* ptid,
pid_t* ctid,
decltype(nullptr) tls) {
@@ -37,21 +48,31 @@ long sys_clone(unsigned long flags,
const bool invalid_ctid =
(flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) && !ctid;
const bool invalid_ptid = (flags & CLONE_PARENT_SETTID) && !ptid;
- const bool invalid_stack = (flags & CLONE_VM) && !child_stack;
- if (clone_tls_used || invalid_ctid || invalid_ptid || invalid_stack) {
+ // Since child_stack must be nullptr, we do not support CLONE_VM.
+ const bool invalid_clone_vm = flags & CLONE_VM;
jln (very slow on Chromium) 2014/12/15 23:31:35 maybe rename to clone_vm_used (to be consistent wi
rickyz (no longer on Chrome) 2014/12/16 00:35:09 Done.
+
+ if (clone_tls_used || invalid_ctid || invalid_ptid || invalid_clone_vm) {
RAW_LOG(FATAL, "Invalid usage of sys_clone");
}
-// See kernel/fork.c in Linux. There is different ordering of sys_clone
-// parameters depending on CONFIG_CLONE_BACKWARDS* configuration options.
-#if defined(ARCH_CPU_X86_64)
- return syscall(__NR_clone, flags, child_stack, ptid, ctid, tls);
-#elif defined(ARCH_CPU_X86) || defined(ARCH_CPU_ARM_FAMILY) || \
- defined(ARCH_CPU_MIPS_FAMILY) || defined(ARCH_CPU_MIPS64_FAMILY)
- // CONFIG_CLONE_BACKWARDS defined.
- return syscall(__NR_clone, flags, child_stack, ptid, tls, ctid);
-#endif
+ char stack[PTHREAD_STACK_MIN];
+ jmp_buf env;
+ if (setjmp(env) == 0) {
+ // We use the libc clone wrapper instead of making the syscall
+ // directly because making the syscall may fail to update the libc's
+ // internal pid cache. The libc interface unfortunately requires
+ // specifying a new stack, so we use setjmp/longjmp to emulate
+ // fork-like behavior.
+ return clone(&clone_helper, stack + sizeof(stack), flags, &env, ptid, tls,
mdempsky 2014/12/15 20:30:08 "stack + sizeof(stack)" is technically wrong on st
jln (very slow on Chromium) 2014/12/15 23:31:35 You mean stack grows-up architecture. How could w
mdempsky 2014/12/15 23:55:01 Yep, oops.
rickyz (no longer on Chrome) 2014/12/16 00:35:09 Here's a version that detects the direction of sta
jln (very slow on Chromium) 2014/12/16 01:11:22 Yes, I think Matthew's suggestion is best. It's fa
rickyz (no longer on Chrome) 2014/12/16 01:30:08 Done.
+ ctid);
+ }
+
+ return 0;
+}
+
+long sys_clone(unsigned long flags) {
+ return sys_clone(flags, nullptr, nullptr, nullptr, nullptr);
}
void sys_exit_group(int status) {

Powered by Google App Engine
This is Rietveld 408576698