2021-08-15 07:05:27 +00: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 18:02:07 +00:00
|
|
|
#include "libc/atomic.h"
|
2021-08-15 07:05:27 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2022-07-18 10:33:32 +00:00
|
|
|
#include "libc/calls/struct/timespec.h"
|
2021-08-26 04:35:58 +00:00
|
|
|
#include "libc/dce.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/errno.h"
|
2022-05-19 23:57:49 +00:00
|
|
|
#include "libc/log/backtrace.internal.h"
|
2022-07-10 11:01:17 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-05-16 20:20:08 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2022-05-17 14:40:00 +00:00
|
|
|
#include "libc/nexgen32e/nexgen32e.h"
|
2022-07-10 11:01:17 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2022-05-27 20:25:46 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/runtime/stack.h"
|
2022-05-19 23:57:49 +00:00
|
|
|
#include "libc/runtime/symbols.internal.h"
|
2022-07-18 10:33:32 +00:00
|
|
|
#include "libc/stdio/stdio.h"
|
|
|
|
#include "libc/sysv/consts/clock.h"
|
2021-08-26 04:35:58 +00:00
|
|
|
#include "libc/sysv/consts/clone.h"
|
2022-05-12 13:43:59 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
2022-07-22 20:44:00 +00:00
|
|
|
#include "libc/sysv/consts/nr.h"
|
2022-05-27 20:25:46 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2022-05-12 13:43:59 +00:00
|
|
|
#include "libc/sysv/consts/prot.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2022-05-17 14:40:00 +00:00
|
|
|
#include "libc/testlib/ezbench.h"
|
2021-08-26 04:35:58 +00:00
|
|
|
#include "libc/testlib/testlib.h"
|
2022-07-10 11:01:17 +00:00
|
|
|
#include "libc/thread/spawn.h"
|
2022-09-10 09:56:25 +00:00
|
|
|
#include "libc/thread/tls.h"
|
|
|
|
#include "libc/thread/tls2.h"
|
2022-09-11 18:02:07 +00:00
|
|
|
#include "libc/thread/wait0.internal.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/time/time.h"
|
2021-08-15 07:05:27 +00:00
|
|
|
|
2022-07-10 11:01:17 +00:00
|
|
|
int x, me, tid;
|
2022-09-11 18:02:07 +00:00
|
|
|
atomic_int thechilde;
|
2022-04-20 16:56:53 +00:00
|
|
|
|
2022-07-25 05:34:13 +00:00
|
|
|
void SetUpOnce(void) {
|
2022-08-11 07:15:29 +00:00
|
|
|
__enable_threads();
|
|
|
|
ASSERT_SYS(0, 0, pledge("stdio rpath", 0));
|
2022-06-27 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 20:44:00 +00:00
|
|
|
int Hog(void *arg, int tid) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-20 16:56:53 +00:00
|
|
|
void SetUp(void) {
|
|
|
|
x = 0;
|
2022-05-13 00:52:13 +00:00
|
|
|
me = gettid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown(void) {
|
2022-04-20 16:56:53 +00:00
|
|
|
}
|
2021-08-15 07:05:27 +00:00
|
|
|
|
2022-05-28 12:50:01 +00:00
|
|
|
int DoNothing(void *arg) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:25:46 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TEST THREADS WORK
|
2022-05-19 23:57:49 +00:00
|
|
|
|
2022-07-10 11:01:17 +00:00
|
|
|
int CloneTest1(void *arg, int tid) {
|
2022-05-19 23:57:49 +00:00
|
|
|
intptr_t rsp, top, bot;
|
|
|
|
CheckStackIsAligned();
|
2022-06-26 01:17:31 +00:00
|
|
|
// PrintBacktraceUsingSymbols(2, __builtin_frame_address(0),
|
|
|
|
// GetSymbolTable());
|
2022-05-19 23:57:49 +00:00
|
|
|
rsp = (intptr_t)__builtin_frame_address(0);
|
2022-07-10 11:01:17 +00:00
|
|
|
bot = ROUNDDOWN((intptr_t)rsp, GetStackSize());
|
2022-05-19 23:57:49 +00: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 16:56:53 +00:00
|
|
|
x = 42;
|
|
|
|
ASSERT_EQ(23, (intptr_t)arg);
|
2022-05-13 00:52:13 +00:00
|
|
|
ASSERT_NE(gettid(), getpid());
|
2022-05-16 20:20:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-13 00:52:13 +00:00
|
|
|
TEST(clone, test1) {
|
2022-05-19 23:57:49 +00:00
|
|
|
int ptid = 0;
|
2022-07-10 11:01:17 +00:00
|
|
|
struct spawn th;
|
|
|
|
ASSERT_SYS(0, 0, _spawn(CloneTest1, (void *)23, &th));
|
|
|
|
ASSERT_SYS(0, 0, _join(&th));
|
2022-05-23 17:15:53 +00:00
|
|
|
ASSERT_NE(gettid(), tid);
|
2022-05-19 23:57:49 +00:00
|
|
|
ASSERT_EQ(tid, ptid);
|
2021-08-26 04:35:58 +00:00
|
|
|
ASSERT_EQ(42, x);
|
2022-04-20 16:56:53 +00:00
|
|
|
ASSERT_NE(me, tid);
|
2022-05-16 20:20:08 +00:00
|
|
|
ASSERT_EQ(0, errno);
|
|
|
|
errno = 31337;
|
|
|
|
ASSERT_EQ(31337, errno);
|
2022-05-17 11:14:28 +00:00
|
|
|
errno = 0;
|
|
|
|
}
|
2022-05-16 20:20:08 +00:00
|
|
|
|
2022-05-27 20:25:46 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TEST THREADS CAN ISSUE SYSTEM CALLS WITH INDEPENDENT ERRNOS
|
|
|
|
|
2022-09-11 18:02:07 +00:00
|
|
|
atomic_int sysbarrier;
|
2022-05-27 20:25:46 +00:00
|
|
|
|
2022-07-10 11:01:17 +00:00
|
|
|
int CloneTestSys(void *arg, int tid) {
|
2022-05-27 20:25:46 +00:00
|
|
|
int i, id = (intptr_t)arg;
|
2022-05-19 23:57:49 +00:00
|
|
|
CheckStackIsAligned();
|
2022-05-27 20:25:46 +00:00
|
|
|
while (!sysbarrier) asm("pause");
|
|
|
|
for (i = 0; i < 20; ++i) {
|
|
|
|
switch (id % 3) {
|
|
|
|
case 0:
|
|
|
|
errno = 123;
|
|
|
|
open(0, 0);
|
|
|
|
asm("pause");
|
|
|
|
ASSERT_EQ(EFAULT, errno);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
errno = 123;
|
|
|
|
dup(-1);
|
|
|
|
asm("pause");
|
|
|
|
ASSERT_EQ(EBADF, errno);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
errno = 123;
|
|
|
|
dup3(0, 0, 0);
|
|
|
|
asm("pause");
|
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
}
|
2022-05-17 11:14:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(clone, tlsSystemCallsErrno_wontClobberMainThreadBecauseTls) {
|
2022-05-27 20:25:46 +00:00
|
|
|
int i;
|
2022-07-10 11:01:17 +00:00
|
|
|
struct spawn th[8];
|
2022-05-17 11:14:28 +00:00
|
|
|
ASSERT_EQ(0, errno);
|
2022-05-27 20:25:46 +00:00
|
|
|
for (i = 0; i < 8; ++i) {
|
2022-07-10 11:01:17 +00:00
|
|
|
ASSERT_SYS(0, 0, _spawn(CloneTestSys, (void *)(intptr_t)i, th + i));
|
2022-05-27 20:25:46 +00:00
|
|
|
}
|
|
|
|
sysbarrier = 1;
|
|
|
|
for (i = 0; i < 8; ++i) {
|
2022-07-10 11:01:17 +00:00
|
|
|
ASSERT_SYS(0, 0, _join(th + i));
|
2022-05-27 20:25:46 +00:00
|
|
|
}
|
2022-05-17 11:14:28 +00:00
|
|
|
ASSERT_EQ(0, errno);
|
2022-05-27 20:25:46 +00:00
|
|
|
}
|