mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 19:28:29 +00:00
Remove _Hide keyword
It never did anything and isn't worthwhile as documentation.
This commit is contained in:
parent
4fb6cbc1fe
commit
e0c2b91b3e
131 changed files with 716 additions and 917 deletions
|
@ -23,7 +23,7 @@
|
|||
#ifdef __x86_64__
|
||||
|
||||
// kudos rich felker for the brilliant design
|
||||
_Hide int __sigsetjmp_tail(sigjmp_buf jb, int rc) {
|
||||
int __sigsetjmp_tail(sigjmp_buf jb, int rc) {
|
||||
_Static_assert(
|
||||
sizeof(sigjmp_buf) == sizeof(jmp_buf) + 8 + 8 + sizeof(sigset_t),
|
||||
"please recompute sigjmp_buf w.r.t. sigset_t");
|
||||
|
|
|
@ -17,12 +17,8 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "ape/sections.internal.h"
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/runtime/morph.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
|
@ -30,8 +26,7 @@ extern int __threadcalls_end[] __attribute__((__weak__));
|
|||
extern int __threadcalls_start[] __attribute__((__weak__));
|
||||
|
||||
static privileged dontinline void FixupLockNops(void) {
|
||||
sigset_t mask;
|
||||
__morph_begin(&mask);
|
||||
__morph_begin();
|
||||
/*
|
||||
* _NOPL("__threadcalls", func)
|
||||
*
|
||||
|
@ -55,7 +50,7 @@ static privileged dontinline void FixupLockNops(void) {
|
|||
__executable_start[*p + 1] = 0x67;
|
||||
__executable_start[*p + 2] = 0xe8;
|
||||
}
|
||||
__morph_end(&mask);
|
||||
__morph_end();
|
||||
}
|
||||
|
||||
void __enable_threads(void) {
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
|
||||
extern _Hide struct SymbolTable *__symtab;
|
||||
extern struct SymbolTable *__symtab;
|
||||
|
||||
/**
|
||||
* Returns low index into symbol table for address.
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
STATIC_YOINK("__get_symbol");
|
||||
|
||||
static pthread_spinlock_t g_lock;
|
||||
_Hide struct SymbolTable *__symtab; // for kprintf
|
||||
struct SymbolTable *__symtab; // for kprintf
|
||||
|
||||
/**
|
||||
* Looks for `.symtab` in zip central directory.
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/runtime/morph.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
|
||||
#ifdef __x86_64__
|
||||
|
@ -109,11 +109,10 @@ static privileged void HookFunction(code_t *func, void *dest) {
|
|||
privileged dontinstrument noasan int __hook(void *dest,
|
||||
struct SymbolTable *st) {
|
||||
long i;
|
||||
sigset_t mask;
|
||||
code_t *p, *pe;
|
||||
intptr_t lowest;
|
||||
if (!st) return -1;
|
||||
__morph_begin(&mask);
|
||||
__morph_begin();
|
||||
lowest = MAX((intptr_t)__executable_start, (intptr_t)_ereal);
|
||||
for (i = 0; i < st->count; ++i) {
|
||||
if (st->symbols[i].x < 9) continue;
|
||||
|
@ -129,6 +128,6 @@ privileged dontinstrument noasan int __hook(void *dest,
|
|||
// kprintf("can't hook %t at %lx\n", p, p);
|
||||
}
|
||||
}
|
||||
__morph_end(&mask);
|
||||
__morph_end();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -31,25 +31,25 @@ extern unsigned char _tbss_offset[] __attribute__((__weak__));
|
|||
extern unsigned char _tls_size[] __attribute__((__weak__));
|
||||
extern unsigned char _tls_content[] __attribute__((__weak__));
|
||||
|
||||
void _init(void) _Hide;
|
||||
int ftrace_init(void) _Hide;
|
||||
void ftrace_hook(void) _Hide;
|
||||
void __morph_tls(void) _Hide;
|
||||
void __enable_tls(void) _Hide;
|
||||
void __enable_threads(void) _Hide;
|
||||
void *__cxa_finalize(void *) _Hide;
|
||||
void _init(void);
|
||||
int ftrace_init(void);
|
||||
void ftrace_hook(void);
|
||||
void __morph_tls(void);
|
||||
void __enable_tls(void);
|
||||
void __enable_threads(void);
|
||||
void *__cxa_finalize(void *);
|
||||
void __stack_chk_fail(void) wontreturn relegated;
|
||||
void __stack_chk_fail_local(void) wontreturn relegated _Hide;
|
||||
void __asan_init(int, char **, char **, intptr_t *) _Hide;
|
||||
void _jmpstack(void *, void *, ...) _Hide wontreturn;
|
||||
long _setstack(void *, void *, ...) _Hide;
|
||||
void __stack_chk_fail_local(void) wontreturn relegated;
|
||||
void __asan_init(int, char **, char **, intptr_t *);
|
||||
void _jmpstack(void *, void *, ...) wontreturn;
|
||||
long _setstack(void *, void *, ...);
|
||||
int GetDosArgv(const char16_t *, char *, size_t, char **, size_t);
|
||||
int GetDosEnviron(const char16_t *, char *, size_t, char **, size_t);
|
||||
bool __intercept_flag(int *, char *[], const char *);
|
||||
int sys_mprotect_nt(void *, size_t, int) _Hide;
|
||||
int sys_mprotect_nt(void *, size_t, int);
|
||||
int __inflate(void *, size_t, const void *, size_t);
|
||||
noasan void *_Mmap(void *, size_t, int, int, int, int64_t) _Hide;
|
||||
noasan int _Munmap(char *, size_t) _Hide;
|
||||
noasan void *_Mmap(void *, size_t, int, int, int, int64_t);
|
||||
noasan int _Munmap(char *, size_t);
|
||||
void __on_arithmetic_overflow(void);
|
||||
void __init_fds(void);
|
||||
|
||||
|
|
|
@ -1,81 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 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/calls/blockcancel.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/elf/def.h"
|
||||
#include "libc/elf/elf.h"
|
||||
#include "libc/elf/struct/ehdr.h"
|
||||
#include "libc/elf/struct/phdr.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
|
||||
/**
|
||||
* Returns true if ELF executable uses dynamic loading magic.
|
||||
*/
|
||||
bool _IsDynamicExecutable(const char *prog) {
|
||||
bool res;
|
||||
Elf64_Ehdr *e;
|
||||
Elf64_Phdr *p;
|
||||
struct stat st;
|
||||
int i, fd, err;
|
||||
BLOCK_CANCELLATIONS;
|
||||
fd = -1;
|
||||
err = errno;
|
||||
e = MAP_FAILED;
|
||||
if ((fd = open(prog, O_RDONLY)) == -1) {
|
||||
res = false;
|
||||
goto Finish;
|
||||
}
|
||||
if (fstat(fd, &st) == -1 || st.st_size < 64) {
|
||||
res = false;
|
||||
goto Finish;
|
||||
}
|
||||
if ((e = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
|
||||
res = false;
|
||||
goto Finish;
|
||||
}
|
||||
if (READ32LE(e->e_ident) != READ32LE(ELFMAG)) {
|
||||
res = false;
|
||||
goto Finish;
|
||||
}
|
||||
if (e->e_type == ET_DYN) {
|
||||
res = true;
|
||||
goto Finish;
|
||||
}
|
||||
for (i = 0; i < e->e_phnum; ++i) {
|
||||
p = GetElfProgramHeaderAddress(e, st.st_size, i);
|
||||
if (p->p_type == PT_INTERP || p->p_type == PT_DYNAMIC) {
|
||||
res = true;
|
||||
goto Finish;
|
||||
}
|
||||
}
|
||||
res = false;
|
||||
goto Finish;
|
||||
Finish:
|
||||
if (e != MAP_FAILED) munmap(e, st.st_size);
|
||||
if (fd != -1) close(fd);
|
||||
errno = err;
|
||||
ALLOW_CANCELLATIONS;
|
||||
return res;
|
||||
}
|
|
@ -36,7 +36,7 @@
|
|||
* This only supports %d and %s. It'll will work even if .rodata hasn't
|
||||
* been loaded into memory yet.
|
||||
*/
|
||||
_Hide textreal void(MetalPrintf)(const char *fmt, ...) {
|
||||
textreal void(MetalPrintf)(const char *fmt, ...) {
|
||||
int i;
|
||||
char c;
|
||||
unsigned u;
|
||||
|
|
|
@ -18,22 +18,16 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#define ShouldUseMsabiAttribute() 1
|
||||
#include "ape/sections.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/asmflag.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/nt/enum/pageflags.h"
|
||||
#include "libc/nt/memory.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/thunk/msabi.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/nr.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
|
||||
__msabi extern typeof(VirtualProtect) *const __imp_VirtualProtect;
|
||||
|
||||
|
@ -77,24 +71,37 @@ __funline void __morph_mprotect(void *addr, size_t size, int prot, int ntprot) {
|
|||
/**
|
||||
* Begins code morphing executable.
|
||||
*
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* The following example
|
||||
*
|
||||
* #include <cosmo.h>
|
||||
* #include <stdlib.h>
|
||||
*
|
||||
* privileged int main() { // privileged code is unmodifiable
|
||||
* ShowCrashReports(); // print report if trapped
|
||||
* __morph_begin(0); // make executable code R+W
|
||||
* *(char *)exit = 0xCC; // turn exit() into an INT3 trap
|
||||
* __morph_end(); // make executable code R+X
|
||||
* exit(0); // won't actually exit
|
||||
* }
|
||||
*
|
||||
* shows how the exit() function can be recompiled at runtime to become
|
||||
* an int3 (x86-64) debugger trap. What makes it tricky is Cosmopolitan
|
||||
* maintains a R^X invariant, in order to support OpenBSD. So when code
|
||||
* wants to modify some part of the executable image in memory the vast
|
||||
* majority of the code stops being executable during that time, unless
|
||||
* it's been linked into a special privileged section of the binary. It
|
||||
* is only possible to code morph from privileged functions. Privileged
|
||||
* functions are also only allowed to call other privileged functions.
|
||||
*/
|
||||
privileged void __morph_begin(sigset_t *save) {
|
||||
int ax;
|
||||
bool cf;
|
||||
intptr_t dx;
|
||||
sigset_t ss = {{-1, -1}};
|
||||
privileged void __morph_begin(void) {
|
||||
__morph_mprotect(__executable_start, __privileged_start - __executable_start,
|
||||
PROT_READ | PROT_WRITE, kNtPageWritecopy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Begins code morphing executable.
|
||||
* Finishes code morphing executable.
|
||||
*/
|
||||
privileged void __morph_end(sigset_t *save) {
|
||||
int ax;
|
||||
long dx;
|
||||
bool cf;
|
||||
privileged void __morph_end(void) {
|
||||
__morph_mprotect(__executable_start, __privileged_start - __executable_start,
|
||||
PROT_READ | PROT_EXEC, kNtPageExecuteRead);
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_RUNTIME_MORPH_H_
|
||||
#define COSMOPOLITAN_LIBC_RUNTIME_MORPH_H_
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
void __morph_begin(sigset_t *);
|
||||
void __morph_end(sigset_t *);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_RUNTIME_MORPH_H_ */
|
|
@ -17,11 +17,9 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "ape/sections.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/morph.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(1)));
|
||||
|
@ -47,10 +45,9 @@ privileged void __morph_tls(void) {
|
|||
// have actually been linked into this program.
|
||||
int n;
|
||||
uint64_t w;
|
||||
sigset_t mask;
|
||||
unsigned m, dis;
|
||||
unsigned char *p;
|
||||
__morph_begin(&mask);
|
||||
__morph_begin();
|
||||
|
||||
if (IsXnu()) {
|
||||
// Apple is quite straightforward to patch. We basically
|
||||
|
@ -114,6 +111,6 @@ privileged void __morph_tls(void) {
|
|||
}
|
||||
}
|
||||
|
||||
__morph_end(&mask);
|
||||
__morph_end();
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -103,7 +103,6 @@ int _cocmd(int, char **, char **);
|
|||
char *GetProgramExecutableName(void);
|
||||
char *GetInterpreterExecutableName(char *, size_t);
|
||||
int _OpenExecutable(void);
|
||||
bool _IsDynamicExecutable(const char *);
|
||||
/* execution control */
|
||||
int verynice(void);
|
||||
axdx_t setlongerjmp(jmp_buf)
|
||||
|
@ -121,6 +120,9 @@ void *_mapanon(size_t) attributeallocsize((1)) mallocesque;
|
|||
void *_mapshared(size_t) attributeallocsize((1)) mallocesque;
|
||||
void __oom_hook(size_t);
|
||||
bool _isheap(void *);
|
||||
/* code morphing */
|
||||
void __morph_begin(void);
|
||||
void __morph_end(void);
|
||||
/* portability */
|
||||
int NtGetVersion(void) pureconst;
|
||||
bool IsGenuineBlink(void);
|
||||
|
|
|
@ -123,13 +123,15 @@ o/$(MODE)/libc/runtime/enable_tls.o: private \
|
|||
endif
|
||||
|
||||
# privileged functions
|
||||
o/$(MODE)/libc/runtime/morph.o \
|
||||
o/$(MODE)/libc/runtime/getsymbol.o \
|
||||
o/$(MODE)/libc/runtime/enable_threads.o \
|
||||
o/$(MODE)/libc/runtime/morph_tls.o: private \
|
||||
CFLAGS += \
|
||||
-ffreestanding \
|
||||
-fno-sanitize=all \
|
||||
-fno-stack-protector
|
||||
-fno-stack-protector \
|
||||
-fpatchable-function-entry=0,0
|
||||
|
||||
# TODO(jart): We need a way to avoid WinThreadEntry() being hooked.
|
||||
o/$(MODE)/libc/runtime/clone.o: private \
|
||||
|
|
|
@ -67,7 +67,7 @@ __msabi extern typeof(VirtualProtect) *const __imp_VirtualProtect;
|
|||
|
||||
extern int64_t __wincrashearly;
|
||||
extern const char kConsoleHandles[3];
|
||||
extern void cosmo(int, char **, char **, long (*)[2]) _Hide wontreturn;
|
||||
extern void cosmo(int, char **, char **, long (*)[2]) wontreturn;
|
||||
|
||||
static const short kConsoleModes[3] = {
|
||||
kNtEnableProcessedInput | kNtEnableLineInput | kNtEnableEchoInput |
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue