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"
|
2021-02-06 11:17:41 +00:00
|
|
|
.privileged
|
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
|
|
|
//
|
|
|
|
// Do not make the assumption that the parent is suspended until
|
2022-10-03 05:14:33 +00:00
|
|
|
// the child terminates since this impl calls fork() on Windows,
|
|
|
|
// OpenBSD, and MacOS.
|
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
|
2022-10-16 19:05:08 +00:00
|
|
|
vfork: call __require_tls
|
|
|
|
xor %edi,%edi # dwCreationFlags
|
2022-03-21 10:46:16 +00:00
|
|
|
#ifdef __SANITIZE_ADDRESS__
|
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
|
|
|
jmp fork # TODO: asan and vfork don't mix?
|
|
|
|
.endfn vfork,globl
|
|
|
|
#else
|
2021-01-25 21:08:05 +00:00
|
|
|
#if SupportsWindows()
|
|
|
|
testb IsWindows()
|
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
|
|
|
jnz sys_fork_nt # and we're lucky to have that
|
2021-01-25 21:08:05 +00:00
|
|
|
#endif
|
2022-06-23 08:18:47 +00:00
|
|
|
#if SupportsXnu()
|
|
|
|
testb IsXnu()
|
|
|
|
jnz fork
|
|
|
|
#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()
|
|
|
|
jnz fork # fake vfork plus msyscall issues
|
|
|
|
#endif
|
|
|
|
#ifdef SYSDEBUG
|
|
|
|
ezlea .Llog,di
|
|
|
|
call __stracef
|
|
|
|
#endif /* SYSDEBUG */
|
2022-10-16 19:05:08 +00:00
|
|
|
mov %fs:0,%r9 # get thread information block
|
|
|
|
mov 0x3c(%r9),%r8d # avoid question of @vforksafe errno
|
2021-01-25 21:08:05 +00:00
|
|
|
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
|
2021-01-25 21:08:05 +00:00
|
|
|
push %rsi # note it happens twice in same page
|
|
|
|
cmp $-4095,%eax
|
2021-02-08 17:19:00 +00:00
|
|
|
jae systemfive_error
|
2022-10-16 19:05:08 +00:00
|
|
|
mov %r8d,0x3c(%r9) # restore errno
|
2021-01-25 21:08:05 +00:00
|
|
|
test %eax,%eax
|
2022-10-16 19:05:08 +00:00
|
|
|
jnz .Lprnt
|
|
|
|
.Lchld: orb $TIB_FLAG_VFORKED,0x40(%r9)
|
|
|
|
ret
|
|
|
|
.Lprnt: andb $~TIB_FLAG_VFORKED,0x40(%r9)
|
|
|
|
ret
|
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 */
|
|
|
|
|
2022-03-21 10:46:16 +00:00
|
|
|
#endif /* __SANITIZE_ADDRESS__ */
|