mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-01 08:48:29 +00:00
Fix some issues and do some code cleanup
This commit is contained in:
parent
1f229e4efc
commit
312ed5c67c
72 changed files with 880 additions and 982 deletions
55
libc/runtime/clone-linux.S
Normal file
55
libc/runtime/clone-linux.S
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
||||
│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ 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. │
|
||||
│ │
|
||||
│ 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/macros.internal.h"
|
||||
|
||||
// Invokes clone() system call on GNU/Systemd.
|
||||
//
|
||||
// @param rdi is flags
|
||||
// @param rsi is top of stack
|
||||
// @param rdx is ptid
|
||||
// @param rcx is ctid
|
||||
// @param r8 is tls
|
||||
// @param r9 is func
|
||||
// @param 8(rsp) is arg
|
||||
// @return tid of child on success, or -1 w/ errno
|
||||
sys_clone_linux:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
.profilable
|
||||
push %rbx
|
||||
mov %rcx,%r10
|
||||
mov 16(%rbp),%rbx
|
||||
mov $56,%eax # __NR_clone
|
||||
syscall
|
||||
test %rax,%rax
|
||||
jz 2f
|
||||
cmp $-4095,%rax
|
||||
jae 1f
|
||||
0: pop %rbx
|
||||
pop %rbp
|
||||
ret
|
||||
1: call systemfive_error
|
||||
jmp 0b
|
||||
2: xor %ebp,%ebp # child thread
|
||||
mov %rbx,%rdi # arg
|
||||
call *%r9 # func(arg)
|
||||
xchg %eax,%edi # func(arg) → exitcode
|
||||
mov $60,%eax # __NR_exit(exitcode)
|
||||
syscall
|
||||
.endfn sys_clone_linux,globl,hidden
|
|
@ -419,40 +419,8 @@ static int CloneNetbsd(int (*func)(void *), char *stk, size_t stksz, int flags,
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// GNU/SYSTEMD
|
||||
|
||||
int CloneLinux(int (*func)(void *), char *stk, size_t stksz, int flags,
|
||||
void *arg, int *ptid, void *tls, size_t tlssz, int *ctid) {
|
||||
#ifdef __chibicc__
|
||||
return -1; // TODO
|
||||
#else
|
||||
int ax;
|
||||
intptr_t *stack = (intptr_t *)(stk + stksz);
|
||||
*--stack = (intptr_t)arg;
|
||||
// %rax = syscall(%rax = __NR_clone,
|
||||
// %rdi = flags,
|
||||
// %rsi = child_stack,
|
||||
// %rdx = parent_tidptr,
|
||||
// %r10 = child_tidptr,
|
||||
// %r8 = new_tls);
|
||||
asm volatile("mov\t%4,%%r10\n\t" // ctid
|
||||
"mov\t%5,%%r8\n\t" // tls
|
||||
"mov\t%6,%%r9\n\t" // func
|
||||
"syscall\n\t"
|
||||
"test\t%0,%0\n\t"
|
||||
"jnz\t1f\n\t"
|
||||
"xor\t%%ebp,%%ebp\n\t"
|
||||
"pop\t%%rdi\n\t" // arg
|
||||
"call\t*%%r9\n\t" // func
|
||||
"xchg\t%%eax,%%edi\n\t"
|
||||
"mov\t$0x3c,%%eax\n\t"
|
||||
"syscall\n1:"
|
||||
: "=a"(ax)
|
||||
: "0"(__NR_clone_linux), "D"(flags), "S"(stack), "g"(ctid),
|
||||
"g"(tls), "g"(func), "d"(ptid)
|
||||
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
||||
if (ax > -4096u) errno = -ax, ax = -1;
|
||||
return ax;
|
||||
#endif
|
||||
}
|
||||
int sys_clone_linux(int flags, char *stk, int *ptid, int *ctid, void *tls,
|
||||
int (*func)(void *), void *arg);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// COSMOPOLITAN
|
||||
|
@ -542,13 +510,23 @@ int clone(int (*func)(void *), void *stk, size_t stksz, int flags, void *arg,
|
|||
int rc;
|
||||
struct CloneArgs *wt;
|
||||
|
||||
if (flags & CLONE_THREAD) {
|
||||
__threaded = true;
|
||||
}
|
||||
|
||||
// transition program to threaded state
|
||||
if ((flags & CLONE_SETTLS) && !__tls_enabled) {
|
||||
if (~flags & CLONE_THREAD) {
|
||||
STRACE("clone() tls w/o thread");
|
||||
return einval();
|
||||
}
|
||||
if (__threaded) {
|
||||
STRACE("clone() tls/non-tls mixed order");
|
||||
return einval();
|
||||
}
|
||||
__initialize_tls(tibdefault);
|
||||
*(int *)((char *)tibdefault + 0x38) = gettid();
|
||||
*(int *)((char *)tibdefault + 0x3c) = __errno;
|
||||
__install_tls(tibdefault);
|
||||
__threaded = true;
|
||||
} else if (flags & CLONE_THREAD) {
|
||||
__threaded = true;
|
||||
}
|
||||
|
||||
if (IsAsan() &&
|
||||
|
@ -566,7 +544,8 @@ int clone(int (*func)(void *), void *stk, size_t stksz, int flags, void *arg,
|
|||
((flags & CLONE_SETTLS) && (tlssz < 64 || (tlssz & 7))))) {
|
||||
rc = einval();
|
||||
} else if (IsLinux()) {
|
||||
rc = CloneLinux(func, stk, stksz, flags, arg, ptid, tls, tlssz, ctid);
|
||||
rc =
|
||||
sys_clone_linux(flags, (char *)stk + stksz, ptid, ctid, tls, func, arg);
|
||||
} else if (!IsTiny() &&
|
||||
(flags & ~(CLONE_SETTLS | CLONE_PARENT_SETTID |
|
||||
CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) !=
|
||||
|
|
|
@ -26,7 +26,9 @@
|
|||
* @assume stack addresses are always greater than heap addresses
|
||||
* @assume stack memory isn't stored beneath %rsp (-mno-red-zone)
|
||||
*/
|
||||
noasan bool _isheap(void *p) {
|
||||
return kAutomapStart <= (intptr_t)p &&
|
||||
(intptr_t)p < kAutomapStart + kAutomapSize;
|
||||
optimizesize noasan bool _isheap(void *p) {
|
||||
intptr_t x, y;
|
||||
x = kAutomapStart;
|
||||
y = x + kAutomapSize;
|
||||
return x <= (intptr_t)p && (intptr_t)p < y;
|
||||
}
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
#include "libc/bits/midpoint.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/enum/version.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/nt/version.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/sysv/consts/ss.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
@ -29,7 +28,7 @@ COSMOPOLITAN_C_START_
|
|||
ROUNDUP(VSPACE / FRAMESIZE * (intptr_t)sizeof(struct MemoryInterval), \
|
||||
FRAMESIZE)
|
||||
#define _kMem(NORMAL, WIN7) \
|
||||
(!(IsWindows() && NtGetVersion() < kNtVersionWindows10) ? NORMAL : WIN7)
|
||||
(!IsWindows() || IsAtLeastWindows10() ? NORMAL : WIN7)
|
||||
|
||||
struct MemoryInterval {
|
||||
int x;
|
||||
|
|
|
@ -16,59 +16,11 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/pushpop.h"
|
||||
#include "libc/nt/enum/version.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/sysv/consts/fileno.h"
|
||||
#include "libc/sysv/consts/nr.h"
|
||||
|
||||
#define STACK_SMASH_MESSAGE "stack smashed\n"
|
||||
|
||||
/**
|
||||
* Aborts program under enemy fire to avoid being taken alive.
|
||||
*/
|
||||
privileged noasan void __stack_chk_fail(void) {
|
||||
size_t len;
|
||||
const char *msg;
|
||||
int64_t ax, cx, si;
|
||||
if (!IsWindows()) {
|
||||
msg = STACK_SMASH_MESSAGE;
|
||||
len = pushpop(sizeof(STACK_SMASH_MESSAGE) - 1);
|
||||
if (!IsMetal()) {
|
||||
asm volatile("syscall"
|
||||
: "=a"(ax)
|
||||
: "0"(__NR_write), "D"(pushpop(STDERR_FILENO)), "S"(msg),
|
||||
"d"(len)
|
||||
: "rcx", "r11", "cc", "memory");
|
||||
asm volatile("syscall"
|
||||
: "=a"(ax)
|
||||
: "0"(__NR_exit_group), "D"(pushpop(23))
|
||||
: "rcx", "r11", "cc", "memory");
|
||||
}
|
||||
asm volatile("rep outsb"
|
||||
: "=S"(si), "=c"(cx)
|
||||
: "0"(msg), "1"(len), "d"(0x3F8 /* COM1 */)
|
||||
: "memory");
|
||||
asm("push\t$0\n\t"
|
||||
"push\t$0\n\t"
|
||||
"cli\n\t"
|
||||
"lidt\t(%rsp)");
|
||||
for (;;) asm("ud2");
|
||||
}
|
||||
if (NtGetVersion() < kNtVersionFuture) {
|
||||
do {
|
||||
asm volatile("syscall"
|
||||
: "=a"(ax), "=c"(cx)
|
||||
: "0"(NtGetVersion() < kNtVersionWindows8 ? 0x0029
|
||||
: NtGetVersion() < kNtVersionWindows81 ? 0x002a
|
||||
: NtGetVersion() < kNtVersionWindows10 ? 0x002b
|
||||
: 0x002c),
|
||||
"1"(pushpop(-1L)), "d"(42)
|
||||
: "r11", "cc", "memory");
|
||||
} while (!ax);
|
||||
}
|
||||
for (;;) {
|
||||
TerminateProcess(GetCurrentProcess(), 42);
|
||||
}
|
||||
privileged noasan noinstrument void __stack_chk_fail(void) {
|
||||
kprintf("stack smashed\n");
|
||||
__restorewintty();
|
||||
_Exit(207);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue