mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-27 13:00:28 +00:00
Fix bugs in termios library and cleanup code
This change fixes an issue with the tcflow() magic numbers that was causing bash to freeze up on Linux. While auditing termios polyfills, several other issues were identified with XNU/BSD compatibility. Out of an abundance of caution this change undefines as much surface area from libc/calls/struct/termios.h as possible, so that autoconf scripts are less likely to detect non-POSIX teletypewriter APIs that haven't been polyfilled by Cosmopolitan. This is a *breaking change* for your static archives in /opt/cosmos if you use the cosmocc toolchain. That's because this change disables the ioctl() undiamonding trick for code outside the monorepo, specifically because it'll lead to brittle ABI breakages like this. If you're using the cosmocc toolchain, you'll need to rebuild libraries like ncurses, readline, etc. Yes diamonds cause bloat. To work around that, consider using tcgetwinsize() instead of ioctl(TIOCGWINSZ) since it'll help you avoid pulling every single ioctl-related polyfill into the linkage. The cosmocc script was specifying -DNDEBUG for some reason. It's fixed.
This commit is contained in:
parent
06b749ae03
commit
4778cd4d27
187 changed files with 1025 additions and 1848 deletions
|
@ -528,7 +528,7 @@ static int CloneLinux(int (*func)(void *arg, int rc), char *stk, size_t stksz,
|
|||
/**
|
||||
* Creates thread without malloc being linked.
|
||||
*
|
||||
* If you use clone() you're on you're own, e.g.
|
||||
* If you use clone() you're on your own. Example:
|
||||
*
|
||||
* int worker(void *arg) { return 0; }
|
||||
* struct CosmoTib tib = {.tib_self = &tib, .tib_tid = -1};
|
||||
|
|
|
@ -41,7 +41,7 @@ static bool IsMyDebugBinaryImpl(const char *path) {
|
|||
// which is currently running in memory.
|
||||
if ((size = lseek(fd, 0, SEEK_END)) != -1 &&
|
||||
(map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0)) != MAP_FAILED) {
|
||||
if (READ32LE(map) == READ32LE("\177ELF") &&
|
||||
if (READ32LE((char *)map) == READ32LE("\177ELF") &&
|
||||
GetElfSymbolValue(map, "_etext", &value)) {
|
||||
res = !_etext || value == (uintptr_t)_etext;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
/**
|
||||
* Modifies restrictions on virtual memory address range.
|
||||
|
@ -37,13 +38,11 @@ int mprotect(void *addr, size_t size, int prot) {
|
|||
int64_t rc;
|
||||
if (prot &
|
||||
~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_GROWSDOWN | PROT_GROWSUP)) {
|
||||
errno = EINVAL; // unix checks prot before checking size
|
||||
rc = -1;
|
||||
rc = einval(); // unix checks prot before checking size
|
||||
} else if (!size) {
|
||||
return 0; // make new technology consistent with unix
|
||||
} else if (UNLIKELY((intptr_t)addr & 4095)) {
|
||||
errno = EINVAL; // unix checks prot before checking size
|
||||
rc = -1;
|
||||
rc = einval(); // unix checks prot before checking size
|
||||
} else if (!IsWindows()) {
|
||||
rc = sys_mprotect(addr, size, prot);
|
||||
} else {
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "libc/dce.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/sysv/consts/auxv.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
.privileged
|
||||
|
||||
|
@ -36,12 +37,12 @@ _OpenExecutable:
|
|||
pushq __NR_open(%rip) // -0x08(%rbp)
|
||||
pushq __NR_mmap(%rip) // -0x10(%rbp)
|
||||
pushq __NR_munmap(%rip) // -0x18(%rbp)
|
||||
pushq O_RDWR(%rip) // -0x20(%rbp)
|
||||
pushq $O_RDWR // -0x20(%rbp)
|
||||
pushq MAP_ANONYMOUS(%rip) // -0x28(%rbp)
|
||||
pushq MAP_PRIVATE(%rip) // -0x30(%rbp)
|
||||
pushq MAP_FIXED(%rip) // -0x38(%rbp)
|
||||
pushq __NR_mprotect(%rip) // -0x40(%rbp)
|
||||
pushq O_RDONLY(%rip) // -0x48(%rbp)
|
||||
pushq $O_RDONLY // -0x48(%rbp)
|
||||
push %rbx // code buffer
|
||||
push %r12 // data buffer
|
||||
push %r14 // filename
|
||||
|
|
|
@ -56,7 +56,7 @@ static struct SymbolTable *OpenSymbolTableImpl(const char *filename) {
|
|||
if (filesize < 64) goto RaiseEnoexec;
|
||||
elf = map = mmap(0, filesize, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (map == MAP_FAILED) goto SystemError;
|
||||
if (READ32LE(map) != READ32LE("\177ELF")) goto RaiseEnoexec;
|
||||
if (READ32LE((char *)map) != READ32LE("\177ELF")) goto RaiseEnoexec;
|
||||
if (!(name_base = GetStrtab(map, &m))) goto RaiseEnobufs;
|
||||
if (!(symtab = GetSymtab(map, &n))) goto RaiseEnobufs;
|
||||
tsz = 0;
|
||||
|
|
|
@ -162,9 +162,9 @@ textstartup void __printargs(const char *prologue) {
|
|||
char **env;
|
||||
sigset_t ss;
|
||||
bool gotsome;
|
||||
unsigned i, n;
|
||||
int e, x, flags;
|
||||
uintptr_t *auxp;
|
||||
unsigned i, n, b;
|
||||
struct rlimit rlim;
|
||||
struct utsname uts;
|
||||
struct sigaction sa;
|
||||
|
@ -391,7 +391,7 @@ textstartup void __printargs(const char *prologue) {
|
|||
PRINT("ARGUMENTS (%p)", __argv);
|
||||
if (*__argv) {
|
||||
for (i = 0; i < __argc; ++i) {
|
||||
PRINT(" ☼ %p %s", __argv[i], __argv[i]);
|
||||
PRINT(" ☼ %s", __argv[i]);
|
||||
}
|
||||
} else {
|
||||
PRINT(" none");
|
||||
|
@ -401,7 +401,7 @@ textstartup void __printargs(const char *prologue) {
|
|||
PRINT("ENVIRONMENT (%p)", __envp);
|
||||
if (*__envp) {
|
||||
for (env = __envp; *env; ++env) {
|
||||
PRINT(" ☼ %p %s", *env, *env);
|
||||
PRINT(" ☼ %s", *env);
|
||||
}
|
||||
} else {
|
||||
PRINT(" none");
|
||||
|
@ -414,9 +414,9 @@ textstartup void __printargs(const char *prologue) {
|
|||
for (auxp = __auxv; *auxp; auxp += 2) {
|
||||
if ((auxinfo = DescribeAuxv(auxp[0]))) {
|
||||
ksnprintf(u.path, sizeof(u.path), auxinfo->fmt, auxp[1]);
|
||||
PRINT(" ☼ %p %16s[%4ld] = %s", auxp, auxinfo->name, auxp[0], u.path);
|
||||
PRINT(" ☼ %16s[%4ld] = %s", auxinfo->name, auxp[0], u.path);
|
||||
} else {
|
||||
PRINT(" ☼ %p %16s[%4ld] = %014p", auxp, "unknown", auxp[0], auxp[1]);
|
||||
PRINT(" ☼ %16s[%4ld] = %014p", "unknown", auxp[0], auxp[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -558,63 +558,65 @@ textstartup void __printargs(const char *prologue) {
|
|||
} else if ((termios.c_cflag & CSIZE) == CS8) {
|
||||
kprintf(" CS8");
|
||||
}
|
||||
if ((termios.c_cflag & CBAUD) == B0) {
|
||||
|
||||
b = cfgetospeed(&termios);
|
||||
if (b == B0) {
|
||||
kprintf(" B0");
|
||||
} else if ((termios.c_cflag & CBAUD) == B50) {
|
||||
} else if (b == B50) {
|
||||
kprintf(" B50");
|
||||
} else if ((termios.c_cflag & CBAUD) == B75) {
|
||||
} else if (b == B75) {
|
||||
kprintf(" B75");
|
||||
} else if ((termios.c_cflag & CBAUD) == B110) {
|
||||
} else if (b == B110) {
|
||||
kprintf(" B110");
|
||||
} else if ((termios.c_cflag & CBAUD) == B134) {
|
||||
} else if (b == B134) {
|
||||
kprintf(" B134");
|
||||
} else if ((termios.c_cflag & CBAUD) == B150) {
|
||||
} else if (b == B150) {
|
||||
kprintf(" B150");
|
||||
} else if ((termios.c_cflag & CBAUD) == B200) {
|
||||
} else if (b == B200) {
|
||||
kprintf(" B200");
|
||||
} else if ((termios.c_cflag & CBAUD) == B300) {
|
||||
} else if (b == B300) {
|
||||
kprintf(" B300");
|
||||
} else if ((termios.c_cflag & CBAUD) == B600) {
|
||||
} else if (b == B600) {
|
||||
kprintf(" B600");
|
||||
} else if ((termios.c_cflag & CBAUD) == B1200) {
|
||||
} else if (b == B1200) {
|
||||
kprintf(" B1200");
|
||||
} else if ((termios.c_cflag & CBAUD) == B1800) {
|
||||
} else if (b == B1800) {
|
||||
kprintf(" B1800");
|
||||
} else if ((termios.c_cflag & CBAUD) == B2400) {
|
||||
} else if (b == B2400) {
|
||||
kprintf(" B2400");
|
||||
} else if ((termios.c_cflag & CBAUD) == B4800) {
|
||||
} else if (b == B4800) {
|
||||
kprintf(" B4800");
|
||||
} else if ((termios.c_cflag & CBAUD) == B9600) {
|
||||
} else if (b == B9600) {
|
||||
kprintf(" B9600");
|
||||
} else if ((termios.c_cflag & CBAUD) == B19200) {
|
||||
} else if (b == B19200) {
|
||||
kprintf(" B19200");
|
||||
} else if ((termios.c_cflag & CBAUD) == B38400) {
|
||||
} else if (b == B38400) {
|
||||
kprintf(" B38400");
|
||||
} else if ((termios.c_cflag & CBAUD) == B57600) {
|
||||
} else if (b == B57600) {
|
||||
kprintf(" B57600");
|
||||
} else if ((termios.c_cflag & CBAUD) == B115200) {
|
||||
} else if (b == B115200) {
|
||||
kprintf(" B115200");
|
||||
} else if ((termios.c_cflag & CBAUD) == B230400) {
|
||||
} else if (b == B230400) {
|
||||
kprintf(" B230400");
|
||||
} else if ((termios.c_cflag & CBAUD) == B500000) {
|
||||
} else if (b == B500000) {
|
||||
kprintf(" B500000");
|
||||
} else if ((termios.c_cflag & CBAUD) == B576000) {
|
||||
} else if (b == B576000) {
|
||||
kprintf(" B576000");
|
||||
} else if ((termios.c_cflag & CBAUD) == B1000000) {
|
||||
} else if (b == B1000000) {
|
||||
kprintf(" B1000000");
|
||||
} else if ((termios.c_cflag & CBAUD) == B1152000) {
|
||||
} else if (b == B1152000) {
|
||||
kprintf(" B1152000");
|
||||
} else if ((termios.c_cflag & CBAUD) == B1500000) {
|
||||
} else if (b == B1500000) {
|
||||
kprintf(" B1500000");
|
||||
} else if ((termios.c_cflag & CBAUD) == B2000000) {
|
||||
} else if (b == B2000000) {
|
||||
kprintf(" B2000000");
|
||||
} else if ((termios.c_cflag & CBAUD) == B2500000) {
|
||||
} else if (b == B2500000) {
|
||||
kprintf(" B2500000");
|
||||
} else if ((termios.c_cflag & CBAUD) == B3000000) {
|
||||
} else if (b == B3000000) {
|
||||
kprintf(" B3000000");
|
||||
} else if ((termios.c_cflag & CBAUD) == B3500000) {
|
||||
} else if (b == B3500000) {
|
||||
kprintf(" B3500000");
|
||||
} else if ((termios.c_cflag & CBAUD) == B4000000) {
|
||||
} else if (b == B4000000) {
|
||||
kprintf(" B4000000");
|
||||
}
|
||||
kprintf("\n");
|
||||
|
@ -636,8 +638,8 @@ textstartup void __printargs(const char *prologue) {
|
|||
if (termios.c_lflag & PENDIN) kprintf(" PENDIN");
|
||||
if (termios.c_lflag & XCASE) kprintf(" XCASE");
|
||||
kprintf("\n");
|
||||
PRINT(" c_ispeed = %u", cfgetispeed(&termios));
|
||||
PRINT(" c_ospeed = %u", cfgetospeed(&termios));
|
||||
PRINT(" cfgetispeed() = %u", cfgetispeed(&termios));
|
||||
PRINT(" cfgetospeed() = %u", cfgetospeed(&termios));
|
||||
PRINT(" c_cc[VMIN] = %d", termios.c_cc[VMIN]);
|
||||
PRINT(" c_cc[VTIME] = %d", termios.c_cc[VTIME]);
|
||||
PRINT(" c_cc[VINTR] = CTRL-%c", CTRL(termios.c_cc[VINTR]));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue