2020-06-16 13:38:43 +00:00
|
|
|
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
2020-06-15 14:18:57 +00:00
|
|
|
│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2020-10-27 10:39:46 +00:00
|
|
|
#include "libc/dce.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-10-16 19:05:08 +00:00
|
|
|
#include "libc/thread/tls.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Forks process without copying page tables.
|
|
|
|
//
|
|
|
|
// This is the same as fork() except it's optimized for the case
|
|
|
|
// where the caller invokes execve() immediately afterwards. You
|
2022-10-03 05:14:33 +00:00
|
|
|
// can also call functions like close(), dup2(), etc. Call _exit
|
2022-10-16 19:05:08 +00:00
|
|
|
// but don't call exit. Look for vforksafe function annotations,
|
|
|
|
// For example pthread mutexes are @vforksafe because they don't
|
|
|
|
// do anything in a vfork()'d child process. TLS memory must not
|
|
|
|
// be disabled (it's enabled by default) since vfork() needs it.
|
2021-02-08 17:19:00 +00:00
|
|
|
//
|
2022-10-17 18:02:04 +00:00
|
|
|
// What makes vfork() dangerous is that any changes to memory in
|
|
|
|
// the child process can happen in the parent too. The exception
|
|
|
|
// to this rule is `errno` which is saved/restored in a register
|
|
|
|
// by this implementation. However, despite its dangers, vfork's
|
|
|
|
// performance is irresistible and wonderous to behold. If safer
|
|
|
|
// code is desired, consider posix_spawn() which uses vfork().
|
|
|
|
//
|
2021-02-08 17:19:00 +00:00
|
|
|
// Do not make the assumption that the parent is suspended until
|
2022-10-17 18:02:04 +00:00
|
|
|
// the child terminates since this uses the raw fork system call
|
|
|
|
// on Windows, OpenBSD, and MacOS. In that case the process will
|
|
|
|
// proceed without blocking the parent; however, the `__vforked`
|
|
|
|
// variable is still set to true in the child, so lock functions
|
|
|
|
// won't do anything, and other functions shall change behavior.
|
|
|
|
// This ensures that, even if the operating system does not give
|
|
|
|
// us the performance of vfork(), we'll still be able to cut out
|
|
|
|
// the libc overhead, e.g. pthread_atfork().
|
2021-02-08 17:19:00 +00:00
|
|
|
//
|
|
|
|
// @return pid of child process or 0 if forked process
|
|
|
|
// @returnstwice
|
2022-10-16 19:05:08 +00:00
|
|
|
// @threadsafe
|
2021-02-08 17:19:00 +00:00
|
|
|
// @vforksafe
|
2023-06-06 06:35:31 +00:00
|
|
|
.ftrace1
|
2022-10-17 18:02:04 +00:00
|
|
|
vfork:
|
2023-06-06 06:35:31 +00:00
|
|
|
.ftrace2
|
2023-05-09 08:56:56 +00:00
|
|
|
|
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2022-10-17 18:02:04 +00:00
|
|
|
#if !IsTiny()
|
|
|
|
push %rbp
|
|
|
|
mov %rsp,%rbp
|
|
|
|
call __require_tls
|
|
|
|
#ifdef SYSDEBUG
|
|
|
|
ezlea .Llog,di
|
|
|
|
call __stracef
|
|
|
|
#endif
|
|
|
|
pop %rbp
|
|
|
|
#endif
|
2023-06-04 15:19:45 +00:00
|
|
|
mov %fs:0,%r9 // get thread information block
|
2021-01-25 21:08:05 +00:00
|
|
|
#if SupportsWindows()
|
|
|
|
testb IsWindows()
|
2023-06-04 15:19:45 +00:00
|
|
|
jnz 6f // and we're lucky to have that
|
2022-10-17 18:02:04 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __SANITIZE_ADDRESS__
|
2023-06-04 15:19:45 +00:00
|
|
|
jmp 5f // TODO: asan and vfork don't mix?
|
2021-01-25 21:08:05 +00:00
|
|
|
#endif
|
2022-06-23 08:18:47 +00:00
|
|
|
#if SupportsXnu()
|
|
|
|
testb IsXnu()
|
2022-10-17 18:02:04 +00:00
|
|
|
jnz 5f
|
2022-06-23 08:18:47 +00:00
|
|
|
#endif
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
#if SupportsOpenbsd()
|
|
|
|
testb IsOpenbsd()
|
2023-06-04 15:19:45 +00:00
|
|
|
jnz 5f // fake vfork plus msyscall issues
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
#endif
|
2023-06-04 15:19:45 +00:00
|
|
|
mov 0x3c(%r9),%r8d // avoid question of @vforksafe errno
|
|
|
|
pop %rsi // saves return address in a register
|
2022-10-16 19:05:08 +00:00
|
|
|
mov __NR_vfork(%rip),%eax
|
2021-01-25 21:08:05 +00:00
|
|
|
#if SupportsBsd()
|
2022-10-16 19:05:08 +00:00
|
|
|
clc
|
2021-01-25 21:08:05 +00:00
|
|
|
#endif
|
2020-10-27 10:39:46 +00:00
|
|
|
syscall
|
2022-10-16 19:05:08 +00:00
|
|
|
#if SupportsBsd()
|
|
|
|
jnc 0f
|
|
|
|
neg %rax
|
|
|
|
0:
|
|
|
|
#endif
|
2023-06-04 15:19:45 +00:00
|
|
|
push %rsi // note it happens twice in same page
|
2021-01-25 21:08:05 +00:00
|
|
|
cmp $-4095,%eax
|
2021-02-08 17:19:00 +00:00
|
|
|
jae systemfive_error
|
2023-06-04 15:19:45 +00:00
|
|
|
mov %r8d,0x3c(%r9) // restore errno
|
2022-10-17 18:02:04 +00:00
|
|
|
1: test %eax,%eax
|
|
|
|
jnz .Lpar
|
|
|
|
.Lchi: orb $TIB_FLAG_VFORKED,0x40(%r9)
|
2022-10-16 19:05:08 +00:00
|
|
|
ret
|
2022-10-17 18:02:04 +00:00
|
|
|
.Lpar: andb $~TIB_FLAG_VFORKED,0x40(%r9)
|
2022-10-16 19:05:08 +00:00
|
|
|
ret
|
2022-10-17 18:02:04 +00:00
|
|
|
#if SupportsXnu() || SupportsOpenbsd() || defined(__SANITIZE_ADDRESS__)
|
|
|
|
5: push %rbp
|
|
|
|
mov %rsp,%rbp
|
|
|
|
push %r9
|
|
|
|
push %r9
|
|
|
|
call sys_fork
|
|
|
|
pop %r9
|
|
|
|
pop %r9
|
|
|
|
pop %rbp
|
|
|
|
jmp 1b
|
|
|
|
#endif
|
|
|
|
#if SupportsWindows()
|
|
|
|
6: push %rbp
|
|
|
|
mov %rsp,%rbp
|
|
|
|
push %r9
|
|
|
|
push %r9
|
2023-06-04 15:19:45 +00:00
|
|
|
xor %edi,%edi // dwCreationFlags
|
2022-10-17 18:02:04 +00:00
|
|
|
call sys_fork_nt
|
|
|
|
pop %r9
|
|
|
|
pop %r9
|
|
|
|
pop %rbp
|
|
|
|
jmp 1b
|
2023-05-09 08:56:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
|
2023-06-04 15:19:45 +00:00
|
|
|
adrp x0,__hostos
|
|
|
|
ldr w0,[x0,#:lo12:__hostos]
|
|
|
|
tbz x0,3,1f // bit 3 is xnu
|
|
|
|
b fork // which doesn't support vfork()
|
|
|
|
1: mov x8,#220 // __NR_clone
|
2023-05-09 08:56:56 +00:00
|
|
|
mov x0,#0x4111 // SIGCHLD | CLONE_VM | CLONE_VFORK
|
|
|
|
mov x1,#0
|
|
|
|
svc 0
|
2023-06-04 15:19:45 +00:00
|
|
|
|
|
|
|
// if (!rc) {
|
|
|
|
// __get_tls()->tib_flags |= TIB_FLAG_VFORKED;
|
|
|
|
// } else {
|
|
|
|
// __get_tls()->tib_flags &= ~TIB_FLAG_VFORKED;
|
|
|
|
// }
|
|
|
|
sub x1,x28,#1152
|
|
|
|
ldr x2,[x1,0x40]
|
|
|
|
cbnz x0,2f
|
|
|
|
orr x2,x2,#TIB_FLAG_VFORKED
|
|
|
|
1: str x2,[x1,0x40]
|
|
|
|
b 3f
|
|
|
|
2: and x2,x2,#~TIB_FLAG_VFORKED
|
|
|
|
b 1b
|
|
|
|
|
|
|
|
// if (rc < 0) errno = -rc, rc = -1;
|
|
|
|
3: .hidden _sysret
|
2023-05-09 08:56:56 +00:00
|
|
|
b _sysret
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error "architecture unsupported"
|
2022-10-17 18:02:04 +00:00
|
|
|
#endif
|
2020-10-27 10:39:46 +00:00
|
|
|
.endfn vfork,globl
|
|
|
|
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
#ifdef SYSDEBUG
|
|
|
|
.rodata.str1.1
|
|
|
|
.Llog: .ascii STRACE_PROLOGUE
|
2022-04-24 16:59:22 +00:00
|
|
|
.asciz "vfork()\n"
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
.previous
|
|
|
|
#endif /* DEBUGSYS */
|