mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 11:18:30 +00:00
Eliminate cyclic locks in runtime
This change introduces a new deadlock detector for Cosmo's POSIX threads implementation. Error check mutexes will now track a DAG of nested locks and report EDEADLK when a deadlock is theoretically possible. These will occur rarely, but it's important for production hardening your code. You don't even need to change your mutexes to use the POSIX error check mode because `cosmocc -mdbg` will enable error checking on mutexes by default globally. When cycles are found, an error message showing your demangled symbols describing the strongly connected component are printed and then the SIGTRAP is raised, which means you'll also get a backtrace if you're using ShowCrashReports() too. This new error checker is so low-level and so pure that it's able to verify the relationships of every libc runtime lock, including those locks upon which the mutex implementation depends.
This commit is contained in:
parent
26c051c297
commit
af7bd80430
141 changed files with 2094 additions and 1601 deletions
|
@ -21,24 +21,23 @@
|
|||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/intrin/cxaatexit.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/nt/accounting.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
#define CTOR __attribute__((__constructor__(99)))
|
||||
#define FT(x) (x.dwLowDateTime | (uint64_t)x.dwHighDateTime << 32)
|
||||
|
||||
static int cpus;
|
||||
static double load;
|
||||
static pthread_spinlock_t lock;
|
||||
static struct NtFileTime idle1, kern1, user1;
|
||||
|
||||
textwindows int sys_getloadavg_nt(double *a, int n) {
|
||||
int i, rc;
|
||||
uint64_t elapsed, used;
|
||||
struct NtFileTime idle, kern, user;
|
||||
BLOCK_SIGNALS;
|
||||
pthread_spin_lock(&lock);
|
||||
__cxa_lock();
|
||||
if (GetSystemTimes(&idle, &kern, &user)) {
|
||||
elapsed = (FT(kern) - FT(kern1)) + (FT(user) - FT(user1));
|
||||
if (elapsed) {
|
||||
|
@ -54,12 +53,11 @@ textwindows int sys_getloadavg_nt(double *a, int n) {
|
|||
} else {
|
||||
rc = __winerr();
|
||||
}
|
||||
pthread_spin_unlock(&lock);
|
||||
ALLOW_SIGNALS;
|
||||
__cxa_unlock();
|
||||
return rc;
|
||||
}
|
||||
|
||||
__attribute__((__constructor__(40))) static textstartup void ntinitload(void) {
|
||||
CTOR static textstartup void sys_getloadavg_nt_init(void) {
|
||||
if (IsWindows()) {
|
||||
load = 1;
|
||||
cpus = __get_cpu_count() / 2;
|
||||
|
|
|
@ -96,9 +96,8 @@ static int OldApeLoader(char *s) {
|
|||
static int CopyWithCwd(const char *q, char *p, char *e) {
|
||||
char c;
|
||||
if (*q != '/') {
|
||||
if (q[0] == '.' && q[1] == '/') {
|
||||
if (q[0] == '.' && q[1] == '/')
|
||||
q += 2;
|
||||
}
|
||||
int got = __getcwd(p, e - p - 1 /* '/' */);
|
||||
if (got != -1) {
|
||||
p += got - 1;
|
||||
|
@ -118,9 +117,10 @@ static int CopyWithCwd(const char *q, char *p, char *e) {
|
|||
|
||||
// if q exists then turn it into an absolute path.
|
||||
static int TryPath(const char *q) {
|
||||
if (!CopyWithCwd(q, g_prog.u.buf, g_prog.u.buf + sizeof(g_prog.u.buf))) {
|
||||
if (!q)
|
||||
return 0;
|
||||
if (!CopyWithCwd(q, g_prog.u.buf, g_prog.u.buf + sizeof(g_prog.u.buf)))
|
||||
return 0;
|
||||
}
|
||||
return !sys_faccessat(AT_FDCWD, g_prog.u.buf, F_OK, 0);
|
||||
}
|
||||
|
||||
|
@ -129,9 +129,8 @@ static int TryPath(const char *q) {
|
|||
void __init_program_executable_name(void) {
|
||||
if (__program_executable_name && *__program_executable_name != '/' &&
|
||||
CopyWithCwd(__program_executable_name, g_prog.u.buf,
|
||||
g_prog.u.buf + sizeof(g_prog.u.buf))) {
|
||||
g_prog.u.buf + sizeof(g_prog.u.buf)))
|
||||
__program_executable_name = g_prog.u.buf;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void InitProgramExecutableNameImpl(void) {
|
||||
|
@ -212,14 +211,12 @@ static inline void InitProgramExecutableNameImpl(void) {
|
|||
}
|
||||
|
||||
// don't trust argv or envp if set-id.
|
||||
if (issetugid()) {
|
||||
if (issetugid())
|
||||
goto UseEmpty;
|
||||
}
|
||||
|
||||
// try argv[0], then then $_.
|
||||
if (TryPath(__argv[0]) || TryPath(__getenv(__envp, "_").s)) {
|
||||
if (TryPath(__argv[0]) || TryPath(__getenv(__envp, "_").s))
|
||||
goto UseBuf;
|
||||
}
|
||||
|
||||
// give up and just copy argv[0] into it
|
||||
if ((q = __argv[0])) {
|
||||
|
|
|
@ -13,7 +13,6 @@ extern unsigned __sighandflags[NSIG + 1];
|
|||
extern uint64_t __sighandmask[NSIG + 1];
|
||||
extern const struct NtSecurityAttributes kNtIsInheritable;
|
||||
|
||||
void __fds_wipe(void);
|
||||
void __fds_lock(void);
|
||||
void __fds_unlock(void);
|
||||
|
||||
|
|
|
@ -5,27 +5,15 @@
|
|||
#include "libc/sysv/consts/sig.h"
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#ifndef MODE_DBG
|
||||
/* block sigs because theoretical edge cases */
|
||||
#define BLOCK_SIGNALS \
|
||||
do { \
|
||||
sigset_t _SigMask; \
|
||||
_SigMask = __sig_block()
|
||||
|
||||
#define ALLOW_SIGNALS \
|
||||
__sig_unblock(_SigMask); \
|
||||
} \
|
||||
while (0)
|
||||
#else
|
||||
/* doesn't block signals so we can get a crash
|
||||
report, when a core runtime library crashes */
|
||||
#define BLOCK_SIGNALS \
|
||||
do { \
|
||||
sigset_t _SigMask; \
|
||||
sigprocmask(SIG_SETMASK, 0, &_SigMask)
|
||||
#define ALLOW_SIGNALS \
|
||||
} \
|
||||
while (0)
|
||||
#endif
|
||||
|
||||
sigset_t __sig_block(void);
|
||||
void __sig_unblock(sigset_t);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue