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

@ -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)));