summaryrefslogtreecommitdiff
path: root/usr/src/test
diff options
context:
space:
mode:
authorAndy Fiddaman <illumos@fiddaman.net>2022-11-24 13:59:08 +0000
committerAndy Fiddaman <illumos@fiddaman.net>2022-12-07 10:06:14 +0000
commit3f3c90a958c5abf8ec0ed5d1fad2e40bd9905a50 (patch)
tree8c1cbe5bd3a1fa5eb83b439327432994f0c86955 /usr/src/test
parent7f5d80fd842f21a48514cca6718c3bbdaf592316 (diff)
downloadillumos-gate-3f3c90a958c5abf8ec0ed5d1fad2e40bd9905a50.tar.gz
15206 setcontext(2) should not restore %fsbase
Reviewed by: Robert Mustacchi <rm+illumos@fingolfin.org> Reviewed by: Toomas Soome <tsoome@me.com> Approved by: Patrick Mooney <pmooney@pfmooney.com>
Diffstat (limited to 'usr/src/test')
-rw-r--r--usr/src/test/os-tests/runfiles/default.run5
-rw-r--r--usr/src/test/os-tests/tests/Makefile4
-rw-r--r--usr/src/test/os-tests/tests/ucontext.c103
3 files changed, 111 insertions, 1 deletions
diff --git a/usr/src/test/os-tests/runfiles/default.run b/usr/src/test/os-tests/runfiles/default.run
index 7a4189cf48..de2b29f2f1 100644
--- a/usr/src/test/os-tests/runfiles/default.run
+++ b/usr/src/test/os-tests/runfiles/default.run
@@ -12,7 +12,7 @@
#
# Copyright (c) 2012 by Delphix. All rights reserved.
# Copyright 2020 Joyent, Inc.
-# Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
+# Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
# Copyright 2021 Tintri by DDN, Inc. All rights reserved.
# Copyright 2022 Oxide Computer Company
# Copyright 2022 MNX Cloud, Inc.
@@ -39,6 +39,9 @@ tests = ['poll_test', 'epoll_test']
[/opt/os-tests/tests/clock_gettime.32]
[/opt/os-tests/tests/clock_gettime.64]
+[/opt/os-tests/tests/ucontext.32]
+[/opt/os-tests/tests/ucontext.64]
+
[/opt/os-tests/tests/secflags]
user = root
tests = ['secflags_aslr',
diff --git a/usr/src/test/os-tests/tests/Makefile b/usr/src/test/os-tests/tests/Makefile
index afa9df81ed..a30832e8ee 100644
--- a/usr/src/test/os-tests/tests/Makefile
+++ b/usr/src/test/os-tests/tests/Makefile
@@ -46,6 +46,7 @@ PROGS = \
clock_gettime \
eventfd \
odirectory \
+ ucontext \
writev
CPPFLAGS += -D_REENTRANT
@@ -65,6 +66,9 @@ clock_gettime.32 := CSTD = $(CSTD_GNU99)
clock_gettime.64 := LDLIBS64 += -lproc
clock_gettime.64 := CSTD = $(CSTD_GNU99)
+ucontext.32 := SMOFF += unreachable
+ucontext.64 := SMOFF += unreachable
+
writev.32 := CPPFLAGS += -D_FILE_OFFSET_BITS=64
writev.32 := CSTD = $(CSTD_GNU99)
writev.64 := CSTD = $(CSTD_GNU99)
diff --git a/usr/src/test/os-tests/tests/ucontext.c b/usr/src/test/os-tests/tests/ucontext.c
new file mode 100644
index 0000000000..a6e43941c1
--- /dev/null
+++ b/usr/src/test/os-tests/tests/ucontext.c
@@ -0,0 +1,103 @@
+/*
+ * This file and its contents are supplied under the terms of the
+ * Common Development and Distribution License ("CDDL"), version 1.0.
+ * You may only use this file in accordance with the terms of version
+ * 1.0 of the CDDL.
+ *
+ * A full copy of the text of the CDDL should have accompanied this
+ * source. A copy of the CDDL is also available via the Internet at
+ * http://www.illumos.org/license/CDDL.
+ */
+
+/*
+ * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
+ */
+
+#include <atomic.h>
+#include <err.h>
+#include <pthread.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <ucontext.h>
+#include <unistd.h>
+#include <sys/debug.h>
+#include <sys/types.h>
+
+static __thread uint64_t tlsvar;
+static ucontext_t ctx;
+static uint32_t failures;
+static pthread_t tid;
+
+#define THREAD1_VALUE 0x1010
+#define THREAD2_VALUE 0x0202
+
+void
+report(char *tag)
+{
+ pthread_t ltid = pthread_self();
+
+ printf(" %-14s: thread=%x, TLS variable=%x\n",
+ tag, (uint_t)ltid, tlsvar);
+}
+
+void
+run(void)
+{
+ pthread_t ltid = pthread_self();
+
+ printf("Coroutine started from second thread\n");
+ report("coroutine");
+
+ if (ltid != tid) {
+ fprintf(stderr,
+ "FAIL: coroutine thread ID is %x, expected %x\n",
+ ltid, tid);
+ atomic_inc_32(&failures);
+ }
+
+ if (tlsvar != THREAD2_VALUE) {
+ fprintf(stderr,
+ "FAIL: coroutine TLS variable is %x, expected %x\n",
+ tlsvar, THREAD2_VALUE);
+ atomic_inc_32(&failures);
+ }
+}
+
+void *
+thread(void *arg __unused)
+{
+ tlsvar = THREAD2_VALUE;
+
+ report("second thread");
+ /*
+ * setcontext() does not return if successful, checking the return
+ * value upsets smatch.
+ */
+ (void) setcontext(&ctx);
+ errx(EXIT_FAILURE, "setcontext() returned and should not have.");
+
+ return (NULL);
+}
+
+int
+main(void)
+{
+ char stk[SIGSTKSZ];
+ void *status;
+
+ tlsvar = THREAD1_VALUE;
+
+ report("main thread");
+
+ VERIFY0(getcontext(&ctx));
+ ctx.uc_link = NULL;
+ ctx.uc_stack.ss_sp = stk;
+ ctx.uc_stack.ss_size = sizeof (stk);
+ makecontext(&ctx, run, 0);
+
+ VERIFY0(pthread_create(&tid, NULL, thread, NULL));
+ VERIFY0(pthread_join(tid, &status));
+
+ return (failures == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+}