Hunt down more bugs

After going through the MODE=dbg and MODE=zero build modes, a bunch of
little issues were identified, which have been addressed. Fixing those
issues created even more troubles for the project, because it improved
our ability to detect latent problems which are getting fixed so fast.
This commit is contained in:
Justine Tunney 2023-07-03 17:35:11 -07:00
parent 73c0faa1b5
commit 97b7116953
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
39 changed files with 557 additions and 754 deletions

View file

@ -31,14 +31,46 @@
#include "libc/x/x.h"
#include "third_party/libcxx/math.h"
ucontext_t uc;
bool gotsome;
ucontext_t uc, goback;
char testlib_enable_tmp_setup_teardown;
void itsatrap(int x, int y) {
void check_args(long x0, long x1, long x2, long x3, long x4, long x5, double f0,
double f1, double f2, double f3, double f4, double f5) {
EXPECT_EQ(LONG_MIN + 100, x0);
EXPECT_EQ(LONG_MIN + 101, x1);
EXPECT_EQ(LONG_MIN + 102, x2);
EXPECT_EQ(LONG_MIN + 103, x3);
EXPECT_EQ(LONG_MIN + 104, x4);
EXPECT_EQ(LONG_MIN + 105, x5);
EXPECT_TRUE(20. == f0);
EXPECT_TRUE(21. == f1);
EXPECT_TRUE(22. == f2);
EXPECT_TRUE(23. == f3);
EXPECT_TRUE(24. == f4);
EXPECT_TRUE(25. == f5);
gotsome = true;
}
TEST(makecontext, args) {
char stack[1024];
__interruptible = false;
getcontext(&uc);
uc.uc_link = &goback;
uc.uc_stack.ss_sp = stack;
uc.uc_stack.ss_size = sizeof(stack);
makecontext(&uc, check_args, 12, 20., 21., 22., LONG_MIN + 100,
LONG_MIN + 101, LONG_MIN + 102, LONG_MIN + 103, LONG_MIN + 104,
LONG_MIN + 105, 23., 24., 25.);
swapcontext(&goback, &uc);
EXPECT_TRUE(gotsome);
}
noasan noubsan void itsatrap(int x, int y) {
*(int *)(intptr_t)x = scalbn(x, y);
}
TEST(makecontext, test) {
TEST(makecontext, crash) {
SPAWN(fork);
getcontext(&uc);
uc.uc_link = 0;

View file

@ -156,7 +156,8 @@ TEST(pthread_create, testCustomStack_withReallySmallSize) {
ASSERT_EQ(0, pthread_create(&id, &attr, Increment, 0));
ASSERT_EQ(0, pthread_attr_destroy(&attr));
ASSERT_EQ(0, pthread_join(id, 0));
// we still own the stack memory
free(stk);
stk = malloc(siz);
ASSERT_EQ(0, pthread_attr_init(&attr));
ASSERT_EQ(0, pthread_attr_setstack(&attr, stk, siz));
ASSERT_EQ(0, pthread_create(&id, &attr, Increment, 0));

View file

@ -17,11 +17,41 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/syscall_support-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/kprintf.h"
#include "libc/runtime/runtime.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
void SetUpOnce(void) {
if (IsXnu()) {
tinyprint(2, program_invocation_short_name,
": no custom thread names on apple\n", NULL);
exit(0);
}
if (IsWindows()) {
tinyprint(2, program_invocation_short_name,
": no custom thread names on windows\n", NULL);
exit(0);
}
if (IsLinux() || IsOpenbsd()) {
char buf[512];
if (pthread_getname_np(pthread_self(), buf, sizeof(buf)) == ENOSYS) {
tinyprint(2, program_invocation_short_name,
": kernel too old for thread name support\n", NULL);
exit(0);
}
}
}
static void *SetName(void *arg) {
ASSERT_EQ(0, pthread_setname_np(pthread_self(), "justine"));
return 0;
@ -29,7 +59,6 @@ static void *SetName(void *arg) {
TEST(pthread_setname_np, SetName_SystemCallSucceeds) {
pthread_t id;
if (!IsLinux() || !IsNetbsd() || !IsFreebsd()) return;
ASSERT_EQ(0, pthread_create(&id, 0, SetName, 0));
ASSERT_EQ(0, pthread_join(id, 0));
}
@ -44,25 +73,14 @@ static void *SetGetNameOfSelf(void *arg) {
TEST(pthread_setname_np, SetGetNameOfSelf) {
pthread_t id;
if (!IsLinux() || !IsNetbsd()) return;
if (IsFreebsd()) {
// no get thread name no freebsd (why??)
return;
}
ASSERT_EQ(0, pthread_create(&id, 0, SetGetNameOfSelf, 0));
ASSERT_EQ(0, pthread_join(id, 0));
}
static void *GetDefaultName(void *arg) {
char me[16];
ASSERT_EQ(0, pthread_getname_np(pthread_self(), me, sizeof(me)));
EXPECT_STREQ("", me);
return 0;
}
TEST(pthread_setname_np, GetDefaultName_IsEmptyString) {
pthread_t id;
if (!IsLinux() || !IsNetbsd()) return;
ASSERT_EQ(0, pthread_create(&id, 0, GetDefaultName, 0));
ASSERT_EQ(0, pthread_join(id, 0));
}
atomic_char sync1, sync2;
static void *GetNameOfOtherThreadWorker(void *arg) {
@ -75,10 +93,15 @@ static void *GetNameOfOtherThreadWorker(void *arg) {
TEST(pthread_setname_np, GetNameOfOtherThread) {
char me[16];
pthread_t id;
if (!IsLinux() || !IsNetbsd()) return;
if (IsFreebsd()) {
// no get thread name no freebsd (why??)
return;
}
ASSERT_EQ(0, pthread_create(&id, 0, GetNameOfOtherThreadWorker, 0));
while (!atomic_load(&sync1)) pthread_yield();
ASSERT_EQ(0, pthread_getname_np(id, me, sizeof(me)));
errno_t e = pthread_getname_np(id, me, sizeof(me));
if (IsLinux() && e == ENOENT) return; // bah old kernel
ASSERT_EQ(0, e);
EXPECT_STREQ("justine", me);
ASSERT_EQ(0, pthread_setname_np(id, "tunney"));
ASSERT_EQ(0, pthread_getname_np(id, me, sizeof(me)));

View file

@ -108,7 +108,7 @@ TEST(sem_close, withUnnamedSemaphore_isUndefinedBehavior) {
SPAWN(fork);
IgnoreStderr();
sem_close(&sem);
EXITS(128 + SIGILL); // see __assert_fail
EXITS(128 + SIGABRT); // see __assert_fail
ASSERT_SYS(0, 0, sem_destroy(&sem));
}
@ -119,7 +119,7 @@ TEST(sem_destroy, withNamedSemaphore_isUndefinedBehavior) {
SPAWN(fork);
IgnoreStderr();
sem_destroy(sem);
EXITS(128 + SIGILL); // see __assert_fail
EXITS(128 + SIGABRT); // see __assert_fail
ASSERT_SYS(0, 0, sem_unlink("/boop"));
ASSERT_SYS(0, 0, sem_close(sem));
}

View file

@ -51,7 +51,7 @@ TEST(sem_post, afterDestroyed_isUndefinedBehavior) {
ASSERT_SYS(0, 0, sem_destroy(&sem));
IgnoreStderr();
sem_post(&sem);
EXITS(128 + SIGILL); // see __assert_fail
EXITS(128 + SIGABRT); // see __assert_fail
}
TEST(sem_trywait, afterDestroyed_isUndefinedBehavior) {
@ -63,7 +63,7 @@ TEST(sem_trywait, afterDestroyed_isUndefinedBehavior) {
ASSERT_SYS(0, 0, sem_destroy(&sem));
IgnoreStderr();
sem_trywait(&sem);
EXITS(128 + SIGILL); // see __assert_fail
EXITS(128 + SIGABRT); // see __assert_fail
}
TEST(sem_wait, afterDestroyed_isUndefinedBehavior) {
@ -75,7 +75,7 @@ TEST(sem_wait, afterDestroyed_isUndefinedBehavior) {
ASSERT_SYS(0, 0, sem_destroy(&sem));
IgnoreStderr();
sem_wait(&sem);
EXITS(128 + SIGILL); // see __assert_fail
EXITS(128 + SIGABRT); // see __assert_fail
}
TEST(sem_timedwait, afterDestroyed_isUndefinedBehavior) {
@ -87,7 +87,7 @@ TEST(sem_timedwait, afterDestroyed_isUndefinedBehavior) {
ASSERT_SYS(0, 0, sem_destroy(&sem));
IgnoreStderr();
sem_timedwait(&sem, 0);
EXITS(128 + SIGILL); // see __assert_fail
EXITS(128 + SIGABRT); // see __assert_fail
}
void *Worker(void *arg) {

View file

@ -32,6 +32,7 @@ TEST_LIBC_THREAD_DIRECTDEPS = \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_TESTLIB \
LIBC_THREAD \
LIBC_TIME \