2021-08-15 00:05:27 -07:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-09-11 11:02:07 -07:00
|
|
|
#include "libc/atomic.h"
|
2021-08-15 00:05:27 -07:00
|
|
|
#include "libc/calls/calls.h"
|
2022-05-12 17:52:13 -07:00
|
|
|
#include "libc/errno.h"
|
2022-07-10 04:01:17 -07:00
|
|
|
#include "libc/macros.internal.h"
|
2022-05-17 07:40:00 -07:00
|
|
|
#include "libc/nexgen32e/nexgen32e.h"
|
2022-07-10 04:01:17 -07:00
|
|
|
#include "libc/runtime/internal.h"
|
2022-05-12 17:52:13 -07:00
|
|
|
#include "libc/runtime/stack.h"
|
2021-08-25 21:35:58 -07:00
|
|
|
#include "libc/testlib/testlib.h"
|
2022-07-10 04:01:17 -07:00
|
|
|
#include "libc/thread/spawn.h"
|
2021-08-15 00:05:27 -07:00
|
|
|
|
2022-07-10 04:01:17 -07:00
|
|
|
int x, me, tid;
|
2022-09-11 11:02:07 -07:00
|
|
|
atomic_int thechilde;
|
2022-04-20 09:56:53 -07:00
|
|
|
|
2022-07-24 22:34:13 -07:00
|
|
|
void SetUpOnce(void) {
|
2022-08-11 00:15:29 -07:00
|
|
|
__enable_threads();
|
|
|
|
ASSERT_SYS(0, 0, pledge("stdio rpath", 0));
|
2022-06-27 13:01:58 -07:00
|
|
|
}
|
|
|
|
|
2022-07-22 13:44:00 -07:00
|
|
|
int Hog(void *arg, int tid) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:56:53 -07:00
|
|
|
void SetUp(void) {
|
|
|
|
x = 0;
|
2022-05-12 17:52:13 -07:00
|
|
|
me = gettid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown(void) {
|
2022-04-20 09:56:53 -07:00
|
|
|
}
|
2021-08-15 00:05:27 -07:00
|
|
|
|
2022-05-28 05:50:01 -07:00
|
|
|
int DoNothing(void *arg) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-27 13:25:46 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TEST THREADS WORK
|
2022-05-19 16:57:49 -07:00
|
|
|
|
2022-07-10 04:01:17 -07:00
|
|
|
int CloneTest1(void *arg, int tid) {
|
2022-05-19 16:57:49 -07:00
|
|
|
intptr_t rsp, top, bot;
|
|
|
|
CheckStackIsAligned();
|
2022-06-25 18:17:31 -07:00
|
|
|
// PrintBacktraceUsingSymbols(2, __builtin_frame_address(0),
|
|
|
|
// GetSymbolTable());
|
2022-05-19 16:57:49 -07:00
|
|
|
rsp = (intptr_t)__builtin_frame_address(0);
|
2022-07-10 04:01:17 -07:00
|
|
|
bot = ROUNDDOWN((intptr_t)rsp, GetStackSize());
|
2022-05-19 16:57:49 -07:00
|
|
|
top = bot + GetStackSize();
|
|
|
|
ASSERT_GT(rsp, bot); // check we're on stack
|
|
|
|
ASSERT_LT(rsp, top); // check we're on stack
|
|
|
|
ASSERT_GT(rsp, top - 256); // check we're near top of stack
|
|
|
|
ASSERT_TRUE(IS2POW(GetStackSize()));
|
|
|
|
ASSERT_EQ(0, bot & (GetStackSize() - 1));
|
2022-04-20 09:56:53 -07:00
|
|
|
x = 42;
|
|
|
|
ASSERT_EQ(23, (intptr_t)arg);
|
2022-05-12 17:52:13 -07:00
|
|
|
ASSERT_NE(gettid(), getpid());
|
2022-05-16 13:20:08 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-12 17:52:13 -07:00
|
|
|
TEST(clone, test1) {
|
2022-05-19 16:57:49 -07:00
|
|
|
int ptid = 0;
|
2022-07-10 04:01:17 -07:00
|
|
|
struct spawn th;
|
|
|
|
ASSERT_SYS(0, 0, _spawn(CloneTest1, (void *)23, &th));
|
|
|
|
ASSERT_SYS(0, 0, _join(&th));
|
2022-05-23 10:15:53 -07:00
|
|
|
ASSERT_NE(gettid(), tid);
|
2022-05-19 16:57:49 -07:00
|
|
|
ASSERT_EQ(tid, ptid);
|
2021-08-25 21:35:58 -07:00
|
|
|
ASSERT_EQ(42, x);
|
2022-04-20 09:56:53 -07:00
|
|
|
ASSERT_NE(me, tid);
|
2022-05-16 13:20:08 -07:00
|
|
|
ASSERT_EQ(0, errno);
|
|
|
|
errno = 31337;
|
|
|
|
ASSERT_EQ(31337, errno);
|
2022-05-17 04:14:28 -07:00
|
|
|
errno = 0;
|
|
|
|
}
|
2022-05-16 13:20:08 -07:00
|
|
|
|
2022-05-27 13:25:46 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TEST THREADS CAN ISSUE SYSTEM CALLS WITH INDEPENDENT ERRNOS
|
|
|
|
|
2022-09-11 11:02:07 -07:00
|
|
|
atomic_int sysbarrier;
|
2022-05-27 13:25:46 -07:00
|
|
|
|
2022-07-10 04:01:17 -07:00
|
|
|
int CloneTestSys(void *arg, int tid) {
|
2022-05-27 13:25:46 -07:00
|
|
|
int i, id = (intptr_t)arg;
|
2022-05-19 16:57:49 -07:00
|
|
|
CheckStackIsAligned();
|
2023-05-14 09:32:15 -07:00
|
|
|
while (!sysbarrier) donothing;
|
2022-05-27 13:25:46 -07:00
|
|
|
for (i = 0; i < 20; ++i) {
|
|
|
|
switch (id % 3) {
|
|
|
|
case 0:
|
|
|
|
errno = 123;
|
|
|
|
open(0, 0);
|
2023-05-14 09:32:15 -07:00
|
|
|
donothing;
|
2022-05-27 13:25:46 -07:00
|
|
|
ASSERT_EQ(EFAULT, errno);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
errno = 123;
|
|
|
|
dup(-1);
|
2023-05-14 09:32:15 -07:00
|
|
|
donothing;
|
2022-05-27 13:25:46 -07:00
|
|
|
ASSERT_EQ(EBADF, errno);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
errno = 123;
|
|
|
|
dup3(0, 0, 0);
|
2023-05-14 09:32:15 -07:00
|
|
|
donothing;
|
2022-05-27 13:25:46 -07:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
break;
|
|
|
|
default:
|
2023-06-08 23:44:03 -07:00
|
|
|
__builtin_unreachable();
|
2022-05-27 13:25:46 -07:00
|
|
|
}
|
|
|
|
}
|
2022-05-17 04:14:28 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(clone, tlsSystemCallsErrno_wontClobberMainThreadBecauseTls) {
|
2022-05-27 13:25:46 -07:00
|
|
|
int i;
|
2022-07-10 04:01:17 -07:00
|
|
|
struct spawn th[8];
|
2022-05-17 04:14:28 -07:00
|
|
|
ASSERT_EQ(0, errno);
|
2022-05-27 13:25:46 -07:00
|
|
|
for (i = 0; i < 8; ++i) {
|
2022-07-10 04:01:17 -07:00
|
|
|
ASSERT_SYS(0, 0, _spawn(CloneTestSys, (void *)(intptr_t)i, th + i));
|
2022-05-27 13:25:46 -07:00
|
|
|
}
|
|
|
|
sysbarrier = 1;
|
|
|
|
for (i = 0; i < 8; ++i) {
|
2022-07-10 04:01:17 -07:00
|
|
|
ASSERT_SYS(0, 0, _join(th + i));
|
2022-05-27 13:25:46 -07:00
|
|
|
}
|
2022-05-17 04:14:28 -07:00
|
|
|
ASSERT_EQ(0, errno);
|
2022-05-27 13:25:46 -07:00
|
|
|
}
|