mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-03 17:58: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
|
@ -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])) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue