mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-30 06:20: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
|
@ -6,10 +6,6 @@ COSMOPOLITAN_C_START_
|
|||
|
||||
#define CheckUnsigned(x) ((x) / !((typeof(x))(-1) < 0))
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ cosmopolitan § bits ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
extern const uint8_t kReverseBits[256];
|
||||
|
||||
int _bitreverse8(int) libcesque pureconst;
|
||||
|
@ -23,138 +19,96 @@ unsigned long _roundup2log(unsigned long) libcesque pureconst;
|
|||
unsigned long _rounddown2pow(unsigned long) libcesque pureconst;
|
||||
unsigned _bextra(const unsigned *, size_t, char);
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ cosmopolitan § bits » no assembly required ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
#define READ16LE(P) \
|
||||
(__extension__({ \
|
||||
uint16_t __x; \
|
||||
__builtin_memcpy(&__x, P, 16 / 8); \
|
||||
__x; \
|
||||
}))
|
||||
|
||||
#ifdef __STRICT_ANSI__
|
||||
#define READ16LE(S) ((255 & (S)[1]) << 8 | (255 & (S)[0]))
|
||||
#define READ16BE(S) ((255 & (S)[0]) << 8 | (255 & (S)[1]))
|
||||
#define READ32LE(S) \
|
||||
((uint32_t)(255 & (S)[3]) << 030 | (uint32_t)(255 & (S)[2]) << 020 | \
|
||||
(uint32_t)(255 & (S)[1]) << 010 | (uint32_t)(255 & (S)[0]) << 000)
|
||||
#define READ32BE(S) \
|
||||
((uint32_t)(255 & (S)[0]) << 030 | (uint32_t)(255 & (S)[1]) << 020 | \
|
||||
(uint32_t)(255 & (S)[2]) << 010 | (uint32_t)(255 & (S)[3]) << 000)
|
||||
#define READ64LE(S) \
|
||||
((uint64_t)(255 & (S)[7]) << 070 | (uint64_t)(255 & (S)[6]) << 060 | \
|
||||
(uint64_t)(255 & (S)[5]) << 050 | (uint64_t)(255 & (S)[4]) << 040 | \
|
||||
(uint64_t)(255 & (S)[3]) << 030 | (uint64_t)(255 & (S)[2]) << 020 | \
|
||||
(uint64_t)(255 & (S)[1]) << 010 | (uint64_t)(255 & (S)[0]) << 000)
|
||||
#define READ64BE(S) \
|
||||
((uint64_t)(255 & (S)[0]) << 070 | (uint64_t)(255 & (S)[1]) << 060 | \
|
||||
(uint64_t)(255 & (S)[2]) << 050 | (uint64_t)(255 & (S)[3]) << 040 | \
|
||||
(uint64_t)(255 & (S)[4]) << 030 | (uint64_t)(255 & (S)[5]) << 020 | \
|
||||
(uint64_t)(255 & (S)[6]) << 010 | (uint64_t)(255 & (S)[7]) << 000)
|
||||
#else /* gcc needs help knowing above are mov if s isn't a variable */
|
||||
#define READ16LE(S) \
|
||||
({ \
|
||||
const uint8_t *Ptr = (const uint8_t *)(S); \
|
||||
Ptr[1] << 8 | Ptr[0]; \
|
||||
})
|
||||
#define READ16BE(S) \
|
||||
({ \
|
||||
const uint8_t *Ptr = (const uint8_t *)(S); \
|
||||
Ptr[0] << 8 | Ptr[1]; \
|
||||
})
|
||||
#define READ32LE(S) \
|
||||
({ \
|
||||
const uint8_t *Ptr = (const uint8_t *)(S); \
|
||||
((uint32_t)Ptr[3] << 030 | (uint32_t)Ptr[2] << 020 | \
|
||||
(uint32_t)Ptr[1] << 010 | (uint32_t)Ptr[0] << 000); \
|
||||
})
|
||||
#define READ32BE(S) \
|
||||
({ \
|
||||
const uint8_t *Ptr = (const uint8_t *)(S); \
|
||||
((uint32_t)Ptr[0] << 030 | (uint32_t)Ptr[1] << 020 | \
|
||||
(uint32_t)Ptr[2] << 010 | (uint32_t)Ptr[3] << 000); \
|
||||
})
|
||||
#define READ64LE(S) \
|
||||
({ \
|
||||
const uint8_t *Ptr = (const uint8_t *)(S); \
|
||||
((uint64_t)Ptr[7] << 070 | (uint64_t)Ptr[6] << 060 | \
|
||||
(uint64_t)Ptr[5] << 050 | (uint64_t)Ptr[4] << 040 | \
|
||||
(uint64_t)Ptr[3] << 030 | (uint64_t)Ptr[2] << 020 | \
|
||||
(uint64_t)Ptr[1] << 010 | (uint64_t)Ptr[0] << 000); \
|
||||
})
|
||||
#define READ64BE(S) \
|
||||
({ \
|
||||
const uint8_t *Ptr = (const uint8_t *)(S); \
|
||||
((uint64_t)Ptr[0] << 070 | (uint64_t)Ptr[1] << 060 | \
|
||||
(uint64_t)Ptr[2] << 050 | (uint64_t)Ptr[3] << 040 | \
|
||||
(uint64_t)Ptr[4] << 030 | (uint64_t)Ptr[5] << 020 | \
|
||||
(uint64_t)Ptr[6] << 010 | (uint64_t)Ptr[7] << 000); \
|
||||
})
|
||||
#endif
|
||||
#define READ16BE(P) \
|
||||
(__extension__({ \
|
||||
uint16_t __x; \
|
||||
__builtin_memcpy(&__x, P, 16 / 8); \
|
||||
__builtin_bswap16(__x); \
|
||||
}))
|
||||
|
||||
#define WRITE16LE(P, V) \
|
||||
((P)[0] = (0x00000000000000FF & (V)) >> 000, \
|
||||
(P)[1] = (0x000000000000FF00 & (V)) >> 010, (P) + 2)
|
||||
#define WRITE16BE(P, V) \
|
||||
((P)[0] = (0x000000000000FF00 & (V)) >> 010, \
|
||||
(P)[1] = (0x00000000000000FF & (V)) >> 000, (P) + 2)
|
||||
#define WRITE32LE(P, V) \
|
||||
((P)[0] = (0x00000000000000FF & (V)) >> 000, \
|
||||
(P)[1] = (0x000000000000FF00 & (V)) >> 010, \
|
||||
(P)[2] = (0x0000000000FF0000 & (V)) >> 020, \
|
||||
(P)[3] = (0x00000000FF000000 & (V)) >> 030, (P) + 4)
|
||||
#define WRITE32BE(P, V) \
|
||||
((P)[0] = (0x00000000FF000000 & (V)) >> 030, \
|
||||
(P)[1] = (0x0000000000FF0000 & (V)) >> 020, \
|
||||
(P)[2] = (0x000000000000FF00 & (V)) >> 010, \
|
||||
(P)[3] = (0x00000000000000FF & (V)) >> 000, (P) + 4)
|
||||
#define WRITE64LE(P, V) \
|
||||
((P)[0] = (0x00000000000000FF & (V)) >> 000, \
|
||||
(P)[1] = (0x000000000000FF00 & (V)) >> 010, \
|
||||
(P)[2] = (0x0000000000FF0000 & (V)) >> 020, \
|
||||
(P)[3] = (0x00000000FF000000 & (V)) >> 030, \
|
||||
(P)[4] = (0x000000FF00000000 & (V)) >> 040, \
|
||||
(P)[5] = (0x0000FF0000000000 & (V)) >> 050, \
|
||||
(P)[6] = (0x00FF000000000000 & (V)) >> 060, \
|
||||
(P)[7] = (0xFF00000000000000 & (V)) >> 070, (P) + 8)
|
||||
#define WRITE64BE(P, V) \
|
||||
((P)[0] = (0xFF00000000000000 & (V)) >> 070, \
|
||||
(P)[1] = (0x00FF000000000000 & (V)) >> 060, \
|
||||
(P)[2] = (0x0000FF0000000000 & (V)) >> 050, \
|
||||
(P)[3] = (0x000000FF00000000 & (V)) >> 040, \
|
||||
(P)[4] = (0x00000000FF000000 & (V)) >> 030, \
|
||||
(P)[5] = (0x0000000000FF0000 & (V)) >> 020, \
|
||||
(P)[6] = (0x000000000000FF00 & (V)) >> 010, \
|
||||
(P)[7] = (0x00000000000000FF & (V)) >> 000, (P) + 8)
|
||||
#define READ32LE(P) \
|
||||
(__extension__({ \
|
||||
uint32_t __x; \
|
||||
__builtin_memcpy(&__x, P, 32 / 8); \
|
||||
__x; \
|
||||
}))
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ cosmopolitan § bits » some assembly required ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
||||
#define READ32BE(P) \
|
||||
(__extension__({ \
|
||||
uint32_t __x; \
|
||||
__builtin_memcpy(&__x, P, 32 / 8); \
|
||||
__builtin_bswap32(__x); \
|
||||
}))
|
||||
|
||||
#define lockinc(MEM) __ArithmeticOp1("lock inc", MEM)
|
||||
#define lockdec(MEM) __ArithmeticOp1("lock dec", MEM)
|
||||
#define locknot(MEM) __ArithmeticOp1("lock not", MEM)
|
||||
#define lockneg(MEM) __ArithmeticOp1("lock neg", MEM)
|
||||
#define READ64LE(P) \
|
||||
(__extension__({ \
|
||||
uint64_t __x; \
|
||||
__builtin_memcpy(&__x, P, 64 / 8); \
|
||||
__x; \
|
||||
}))
|
||||
|
||||
#define lockaddeq(MEM, VAL) __ArithmeticOp2("lock add", VAL, MEM)
|
||||
#define locksubeq(MEM, VAL) __ArithmeticOp2("lock sub", VAL, MEM)
|
||||
#define lockxoreq(MEM, VAL) __ArithmeticOp2("lock xor", VAL, MEM)
|
||||
#define lockandeq(MEM, VAL) __ArithmeticOp2("lock and", VAL, MEM)
|
||||
#define lockoreq(MEM, VAL) __ArithmeticOp2("lock or", VAL, MEM)
|
||||
#define READ64BE(P) \
|
||||
(__extension__({ \
|
||||
uint64_t __x; \
|
||||
__builtin_memcpy(&__x, P, 64 / 8); \
|
||||
__builtin_bswap64(__x); \
|
||||
}))
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ cosmopolitan § bits » implementation details ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
#define WRITE16LE(P, X) \
|
||||
(__extension__({ \
|
||||
__typeof__(&(P)[0]) __p = (P); \
|
||||
uint16_t __x = (X); \
|
||||
__builtin_memcpy(__p, &__x, 16 / 8); \
|
||||
__p + 16 / 8; \
|
||||
}))
|
||||
|
||||
#define __ArithmeticOp1(OP, MEM) \
|
||||
({ \
|
||||
asm(OP "%z0\t%0" : "+m"(*(MEM)) : /* no inputs */ : "cc"); \
|
||||
MEM; \
|
||||
})
|
||||
#define WRITE16BE(P, X) \
|
||||
(__extension__({ \
|
||||
__typeof__(&(P)[0]) __p = (P); \
|
||||
uint16_t __x = __builtin_bswap16(X); \
|
||||
__builtin_memcpy(__p, &__x, 16 / 8); \
|
||||
__p + 16 / 8; \
|
||||
}))
|
||||
|
||||
#define __ArithmeticOp2(OP, VAL, MEM) \
|
||||
({ \
|
||||
asm(OP "%z0\t%1,%0" : "+m,m"(*(MEM)) : "i,r"(VAL) : "cc"); \
|
||||
MEM; \
|
||||
})
|
||||
#define WRITE32LE(P, X) \
|
||||
(__extension__({ \
|
||||
__typeof__(&(P)[0]) __p = (P); \
|
||||
uint32_t __x = (X); \
|
||||
__builtin_memcpy(__p, &__x, 32 / 8); \
|
||||
__p + 32 / 8; \
|
||||
}))
|
||||
|
||||
#define WRITE32BE(P, X) \
|
||||
(__extension__({ \
|
||||
__typeof__(&(P)[0]) __p = (P); \
|
||||
uint32_t __x = __builtin_bswap32(X); \
|
||||
__builtin_memcpy(__p, &__x, 32 / 8); \
|
||||
__p + 32 / 8; \
|
||||
}))
|
||||
|
||||
#define WRITE64LE(P, X) \
|
||||
(__extension__({ \
|
||||
__typeof__(&(P)[0]) __p = (P); \
|
||||
uint64_t __x = (X); \
|
||||
__builtin_memcpy(__p, &__x, 64 / 8); \
|
||||
__p + 64 / 8; \
|
||||
}))
|
||||
|
||||
#define WRITE64BE(P, X) \
|
||||
(__extension__({ \
|
||||
__typeof__(&(P)[0]) __p = (P); \
|
||||
uint64_t __x = __builtin_bswap64(X); \
|
||||
__builtin_memcpy(__p, &__x, 64 / 8); \
|
||||
__p + 64 / 8; \
|
||||
}))
|
||||
|
||||
#endif /* __GNUC__ && !__STRICT_ANSI__ */
|
||||
#endif /* COSMO */
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -16,50 +16,37 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/metatermios.internal.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/log/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/termios.h"
|
||||
#include "libc/calls/struct/winsize.h"
|
||||
#include "libc/calls/struct/winsize.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
|
||||
/**
|
||||
* @fileoverview Terminal Restoration Helper for System Five.
|
||||
*
|
||||
* This is used by the crash reporting functions, e.g. __die(), to help
|
||||
* ensure the terminal is in an unborked state after a crash happens.
|
||||
*/
|
||||
#define N 64
|
||||
|
||||
#define RESET_COLOR "\e[0m"
|
||||
#define SHOW_CURSOR "\e[?25h"
|
||||
#define DISABLE_MOUSE "\e[?1000;1002;1015;1006l"
|
||||
#define ANSI_RESTORE RESET_COLOR SHOW_CURSOR DISABLE_MOUSE
|
||||
#define append(...) o += ksnprintf(buf + o, N - o, __VA_ARGS__)
|
||||
|
||||
static bool __isrestorable;
|
||||
static union metatermios __oldtermios;
|
||||
const char *(DescribeWinsize)(char buf[N], int rc, struct winsize *ws) {
|
||||
char b64[64];
|
||||
const char *d;
|
||||
int i, j, o = 0;
|
||||
|
||||
static size_t __strlen(const char *s) {
|
||||
size_t i = 0;
|
||||
while (s[i]) ++i;
|
||||
return i;
|
||||
}
|
||||
|
||||
// called weakly by libc/calls/ioctl_tcsets.c to avoid pledge("tty")
|
||||
void __on_ioctl_tcsets(int fd) {
|
||||
int e;
|
||||
e = errno;
|
||||
if (sys_ioctl(fd, TCGETS, &__oldtermios) != -1) {
|
||||
__isrestorable = true;
|
||||
if (!ws) return "NULL";
|
||||
if (rc == -1) return "n/a";
|
||||
if ((!IsAsan() && kisdangerous(ws)) ||
|
||||
(IsAsan() && !__asan_is_valid(ws, sizeof(*ws)))) {
|
||||
ksnprintf(buf, N, "%p", ws);
|
||||
return buf;
|
||||
}
|
||||
errno = e;
|
||||
}
|
||||
|
||||
void __restore_tty(void) {
|
||||
int e;
|
||||
if (__isrestorable && !__isworker && !__nocolor) {
|
||||
e = errno;
|
||||
sys_write(0, ANSI_RESTORE, __strlen(ANSI_RESTORE));
|
||||
sys_ioctl(0, TCSETSF, &__oldtermios);
|
||||
errno = e;
|
||||
append("{.ws_row=%d, .ws_col=%d", ws->ws_row, ws->ws_col);
|
||||
if (ws->ws_xpixel | ws->ws_ypixel) {
|
||||
append(", .ws_xpixel=%d, .ws_ypixel=%d", ws->ws_xpixel, ws->ws_ypixel);
|
||||
}
|
||||
append("}");
|
||||
|
||||
return buf;
|
||||
}
|
|
@ -20,7 +20,7 @@
|
|||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/extend.internal.h"
|
||||
#include "libc/intrin/pushpop.h"
|
||||
#include "libc/intrin/pushpop.internal.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
|
|
|
@ -31,10 +31,6 @@
|
|||
.balign 4
|
||||
.underrun
|
||||
kOpenFlags:
|
||||
.e O_RDWR,"RDWR" // order matters
|
||||
.e O_RDONLY,"RDONLY" //
|
||||
.e O_WRONLY,"WRONLY" //
|
||||
.e O_ACCMODE,"ACCMODE" // mask of prev three
|
||||
.e O_CREAT,"CREAT" //
|
||||
.e O_EXCL,"EXCL" //
|
||||
.e O_TRUNC,"TRUNC" //
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
#ifdef COSMO
|
||||
|
||||
#if !defined(__GNUC__) || defined(__STRICT_ANSI__) || !defined(__x86_64__)
|
||||
#if !defined(__GNUC__) || defined(__STRICT_ANSI__) || !defined(__x86_64__) || \
|
||||
!defined(__MNO_RED_ZONE__)
|
||||
#define pushpop(x) (x)
|
||||
#else
|
||||
/**
|
||||
|
@ -32,7 +33,8 @@
|
|||
})
|
||||
#endif
|
||||
|
||||
#if !defined(__GNUC__) || defined(__STRICT_ANSI__) || !defined(__x86_64__)
|
||||
#if !defined(__GNUC__) || defined(__STRICT_ANSI__) || !defined(__x86_64__) || \
|
||||
!defined(__MNO_RED_ZONE__)
|
||||
#define pushmov(d, x) (*(d) = (x))
|
||||
#else
|
||||
#define pushmov(d, x) \
|
|
@ -19,7 +19,7 @@
|
|||
#include "libc/calls/calls.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/pushpop.h"
|
||||
#include "libc/intrin/pushpop.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/log/color.internal.h"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue