From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: stefanha@gmail.com, Ilya Leoshkevich <iii@linux.ibm.com>
Subject: [PULL v2 13/13] tests/tcg/multiarch: Add munmap-pthread.c
Date: Tue, 1 Nov 2022 09:01:13 +1100 [thread overview]
Message-ID: <20221031220113.414796-4-richard.henderson@linaro.org> (raw)
In-Reply-To: <20221031220113.414796-1-richard.henderson@linaro.org>
From: Ilya Leoshkevich <iii@linux.ibm.com>
Add a test to detect races between munmap() and creating new threads.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Message-Id: <20221028124227.2354792-3-iii@linux.ibm.com>
[rth: add more return insns]
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
tests/tcg/multiarch/munmap-pthread.c | 79 ++++++++++++++++++++++++++++
tests/tcg/multiarch/Makefile.target | 3 ++
2 files changed, 82 insertions(+)
create mode 100644 tests/tcg/multiarch/munmap-pthread.c
diff --git a/tests/tcg/multiarch/munmap-pthread.c b/tests/tcg/multiarch/munmap-pthread.c
new file mode 100644
index 0000000000..d7143b00d5
--- /dev/null
+++ b/tests/tcg/multiarch/munmap-pthread.c
@@ -0,0 +1,79 @@
+/* Test that munmap() and thread creation do not race. */
+#include <assert.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+static const char nop_func[] = {
+#if defined(__aarch64__)
+ 0xc0, 0x03, 0x5f, 0xd6, /* ret */
+#elif defined(__alpha__)
+ 0x01, 0x80, 0xFA, 0x6B, /* ret */
+#elif defined(__arm__)
+ 0x1e, 0xff, 0x2f, 0xe1, /* bx lr */
+#elif defined(__riscv)
+ 0x67, 0x80, 0x00, 0x00, /* ret */
+#elif defined(__s390__)
+ 0x07, 0xfe, /* br %r14 */
+#elif defined(__i386__) || defined(__x86_64__)
+ 0xc3, /* ret */
+#endif
+};
+
+static void *thread_mmap_munmap(void *arg)
+{
+ volatile bool *run = arg;
+ char *p;
+ int ret;
+
+ while (*run) {
+ p = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ assert(p != MAP_FAILED);
+
+ /* Create a small translation block. */
+ memcpy(p, nop_func, sizeof(nop_func));
+ ((void(*)(void))p)();
+
+ ret = munmap(p, getpagesize());
+ assert(ret == 0);
+ }
+
+ return NULL;
+}
+
+static void *thread_dummy(void *arg)
+{
+ return NULL;
+}
+
+int main(void)
+{
+ pthread_t mmap_munmap, dummy;
+ volatile bool run = true;
+ int i, ret;
+
+ /* Without a template, nothing to test. */
+ if (sizeof(nop_func) == 0) {
+ return EXIT_SUCCESS;
+ }
+
+ ret = pthread_create(&mmap_munmap, NULL, thread_mmap_munmap, (void *)&run);
+ assert(ret == 0);
+
+ for (i = 0; i < 1000; i++) {
+ ret = pthread_create(&dummy, NULL, thread_dummy, NULL);
+ assert(ret == 0);
+ ret = pthread_join(dummy, NULL);
+ assert(ret == 0);
+ }
+
+ run = false;
+ ret = pthread_join(mmap_munmap, NULL);
+ assert(ret == 0);
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/tcg/multiarch/Makefile.target b/tests/tcg/multiarch/Makefile.target
index 78104f9bbb..5f0fee1aad 100644
--- a/tests/tcg/multiarch/Makefile.target
+++ b/tests/tcg/multiarch/Makefile.target
@@ -36,6 +36,9 @@ threadcount: LDFLAGS+=-lpthread
signals: LDFLAGS+=-lrt -lpthread
+munmap-pthread: CFLAGS+=-pthread
+munmap-pthread: LDFLAGS+=-pthread
+
# We define the runner for test-mmap after the individual
# architectures have defined their supported pages sizes. If no
# additional page sizes are defined we only run the default test.
--
2.34.1
next prev parent reply other threads:[~2022-10-31 22:03 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-31 21:57 [PULL v2 00/13] tcg-next patch queue Richard Henderson
2022-10-31 21:57 ` [PULL v2 01/13] tcg/sparc: Remove support for sparc32plus Richard Henderson
2022-10-31 21:57 ` [PULL v2 02/13] tcg/sparc64: Rename from tcg/sparc Richard Henderson
2022-10-31 21:57 ` [PULL v2 03/13] tcg/sparc64: Remove sparc32plus constraints Richard Henderson
2022-10-31 22:00 ` [PULL v2 04/13] tcg/tci: fix logic error when registering helpers via FFI Richard Henderson
2022-10-31 22:00 ` [PULL v2 05/13] accel/tcg: Introduce cpu_unwind_state_data Richard Henderson
2022-10-31 22:00 ` [PULL v2 06/13] target/i386: Use cpu_unwind_state_data for tpr access Richard Henderson
2022-10-31 22:00 ` [PULL v2 07/13] target/openrisc: Always exit after mtspr npc Richard Henderson
2022-10-31 22:00 ` [PULL v2 08/13] target/openrisc: Use cpu_unwind_state_data for mfspr Richard Henderson
2022-10-31 22:00 ` [PULL v2 09/13] accel/tcg: Remove will_exit argument from cpu_restore_state Richard Henderson
2022-10-31 22:01 ` [PULL v2 10/13] accel/tcg: Remove reset_icount argument from cpu_restore_state_from_tb Richard Henderson
2022-10-31 22:01 ` [PULL v2 11/13] target/i386: Expand eflags updates inline Richard Henderson
2022-10-31 22:01 ` [PULL v2 12/13] accel/tcg: Complete cpu initialization before registration Richard Henderson
2022-10-31 22:01 ` Richard Henderson [this message]
2022-11-01 15:03 ` [PULL v2 00/13] tcg-next patch queue Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221031220113.414796-4-richard.henderson@linaro.org \
--to=richard.henderson@linaro.org \
--cc=iii@linux.ibm.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).