Fix bugs with new memory manager

This fixes a regression in mmap(MAP_FIXED) on Windows caused by a recent
revision. This change also fixes ZipOS so it no longer needs a MAP_FIXED
mapping to open files from the PKZIP store. The memory mapping mutex was
implemented incorrectly earlier which meant that ftrace and strace could
cause cause crashes. This lock and other recursive mutexes are rewritten
so that it should be provable that recursive mutexes in cosmopolitan are
asynchronous signal safe.
This commit is contained in:
Justine Tunney 2024-06-29 05:10:15 -07:00
parent 6de12c1032
commit 464858dbb4
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
34 changed files with 353 additions and 313 deletions

23
libc/thread/lock.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef COSMOPOLITAN_LIBC_THREAD_LOCK_H_
#define COSMOPOLITAN_LIBC_THREAD_LOCK_H_
COSMOPOLITAN_C_START_
#define MUTEX_DEPTH_MIN 0x00000010ull
#define MUTEX_DEPTH_MAX 0x000003f0ull
#define MUTEX_TYPE(word) ((word) & 3)
#define MUTEX_PSHARED(word) ((word) & 4)
#define MUTEX_LOCKED(word) ((word) & 8)
#define MUTEX_DEPTH(word) ((word) & MUTEX_DEPTH_MAX)
#define MUTEX_OWNER(word) ((word) >> 32)
#define MUTEX_LOCK(word) (((word) & 7) | 8)
#define MUTEX_UNLOCK(word) ((word) & 7)
#define MUTEX_SET_TYPE(word, type) (((word) & ~3ull) | (type))
#define MUTEX_SET_PSHARED(word, pshared) (((word) & ~4ull) | (pshared))
#define MUTEX_INC_DEPTH(word) ((word) + MUTEX_DEPTH_MIN)
#define MUTEX_DEC_DEPTH(word) ((word) - MUTEX_DEPTH_MIN)
#define MUTEX_SET_OWNER(word, tid) ((uint64_t)(tid) << 32 | (uint32_t)(word))
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_THREAD_LOCK_H_ */

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/thread/lock.h"
#include "libc/thread/thread.h"
#include "libc/thread/thread2.h"
#include "third_party/nsync/common.internal.h"
@ -48,12 +49,10 @@
*/
errno_t pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
const struct timespec *abstime) {
if (abstime && !(0 <= abstime->tv_nsec && abstime->tv_nsec < 1000000000)) {
if (abstime && !(0 <= abstime->tv_nsec && abstime->tv_nsec < 1000000000))
return EINVAL;
}
if (mutex->_type != PTHREAD_MUTEX_NORMAL) {
if (MUTEX_TYPE(mutex->_word) != PTHREAD_MUTEX_NORMAL)
nsync_panic_("pthread cond needs normal mutex\n");
}
return nsync_cv_wait_with_deadline(
(nsync_cv *)cond, (nsync_mu *)mutex,
abstime ? *abstime : nsync_time_no_deadline, 0);

View file

@ -15,7 +15,7 @@
#define PTHREAD_MUTEX_ROBUST 1
#define PTHREAD_PROCESS_PRIVATE 0
#define PTHREAD_PROCESS_SHARED 1
#define PTHREAD_PROCESS_SHARED 4
#define PTHREAD_CREATE_JOINABLE 0
#define PTHREAD_CREATE_DETACHED 1
@ -40,13 +40,12 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define PTHREAD_ONCE_INIT _PTHREAD_INIT
#define PTHREAD_COND_INITIALIZER _PTHREAD_INIT
#define PTHREAD_RWLOCK_INITIALIZER _PTHREAD_INIT
#define PTHREAD_MUTEX_INITIALIZER _PTHREAD_INIT
#define PTHREAD_ONCE_INIT {0}
#define PTHREAD_COND_INITIALIZER {0}
#define PTHREAD_RWLOCK_INITIALIZER {0}
#define PTHREAD_MUTEX_INITIALIZER {0}
#define _PTHREAD_INIT \
{ 0 }
#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP {0, 0, PTHREAD_MUTEX_RECURSIVE}
typedef uintptr_t pthread_t;
typedef int pthread_id_np_t;
@ -65,17 +64,13 @@ typedef struct pthread_spinlock_s {
} pthread_spinlock_t;
typedef struct pthread_mutex_s {
_Atomic(int32_t) _lock;
unsigned _type : 2;
unsigned _pshared : 1;
unsigned _depth : 6;
unsigned _owner : 23;
long _pid;
uint32_t _nsync;
int32_t _pid;
_Atomic(uint64_t) _word;
} pthread_mutex_t;
typedef struct pthread_mutexattr_s {
char _type;
char _pshared;
unsigned _word;
} pthread_mutexattr_t;
typedef struct pthread_cond_s {

View file

@ -4,7 +4,6 @@
#define TLS_ALIGNMENT 64
#define TIB_FLAG_VFORKED 1
#define TIB_FLAG_MAPLOCK 2
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -38,8 +37,9 @@ struct CosmoTib {
char *tib_sigstack_addr;
uint32_t tib_sigstack_size;
uint32_t tib_sigstack_flags;
_Atomic(int) tib_relock_maps;
void *tib_nsync;
void *tib_keys[48];
void *tib_keys[47];
} __attribute__((__aligned__(64)));
extern int __threaded;