Skip to contentSkip to conversation

tests/tcg/multiarch: add vma-pthread.c

PATCH In review Ilya Leoshkevich 2 participants 3 messages Dec 23, 2022 – Dec 24, 2022 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20221223115348.tgfwdlektsulebxk@heavy

#1 lore raw

PATCH 1/1 tests/tcg/multiarch: add vma-pthread.c

Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling.

---
 tests/tcg/multiarch/Makefile.target  |   3 +
 tests/tcg/multiarch/munmap-pthread.c |  16 +--
 tests/tcg/multiarch/nop_func.h       |  25 ++++
 tests/tcg/multiarch/vma-pthread.c    | 185 +++++++++++++++++++++++++++
 4 files changed, 214 insertions(+), 15 deletions(-)
 create mode 100644 tests/tcg/multiarch/nop_func.h
 create mode 100644 tests/tcg/multiarch/vma-pthread.c
tests/tcg/multiarch/Makefile.target +3 -0
index 5f0fee1aadb..e7213af4925 100644
@@ -39,6 +39,9 @@ signals: LDFLAGS+=-lrt -lpthread
 munmap-pthread: CFLAGS+=-pthread
 munmap-pthread: LDFLAGS+=-pthread
 
+vma-pthread: CFLAGS+=-pthread
+vma-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.
tests/tcg/multiarch/munmap-pthread.c +1 -15
index d7143b00d5f..1c79005846d 100644
@@ -7,21 +7,7 @@
 #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
-};
+#include "nop_func.h"
 
 static void *thread_mmap_munmap(void *arg)
 {
tests/tcg/multiarch/nop_func.h new file +25 -0
new file mode 100644
index 00000000000..f714d210000
@@ -0,0 +1,25 @@
+/*
+ * No-op functions that can be safely copied.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef NOP_FUNC_H
+#define NOP_FUNC_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
+};
+
+#endif
tests/tcg/multiarch/vma-pthread.c new file +185 -0

185 lines not on this page · raw message

Signature · 1 line
2.38.1
#2 lore raw
Quoted 11 lines · Ilya Leoshkevich

Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling.

---
 tests/tcg/multiarch/Makefile.target  |   3 +
 tests/tcg/multiarch/munmap-pthread.c |  16 +--
 tests/tcg/multiarch/nop_func.h       |  25 ++++
 tests/tcg/multiarch/vma-pthread.c    | 185
+++++++++++++++++++++++++++

Quoted 3 lines
 4 files changed, 214 insertions(+), 15 deletions(-)

 create mode 100644 tests/tcg/multiarch/nop_func.h create mode 100644 tests/tcg/multiarch/vma-pthread.c

This was meant to be a reply to the bug report for [1], but apparently I forgot to Cc: the mailing list. Copying the original message here:

---
Hi,

Wasmtime testsuite started failing randomly, complaining that clock_gettime() returns -EFAULT. Bisect points to this commit.

I could not see anything obviously wrong here with the manual review, and the failure was not reproducible when running individual testcases or using strace. So I wrote a stress test (which I will post shortly), which runs fine on the host, but reproduces the issue with qemu-user.

When run with -strace, it also triggers an assertion:

    qemu-x86_64: ../accel/tcg/tb-maint.c:595:
tb_invalidate_phys_page_unwind: Assertion `pc != 0' failed.
    qemu-x86_64: /home/iii/qemu/include/qemu/rcu.h:102:

rcu_read_unlock: Assertion `p_rcu_reader->depth != 0' failed.

I haven't tried analyzing what is causing all this yet, but at least now the reproducer is small (~200LOC) and fails faster than 1s.

Best regards,
Ilya
---

[1] https://lists.gnu.org/archive/html/qemu-devel/2022-12/msg03615.html

#3 lore raw
In reply to Ilya Leoshkevich
Quoted 6 lines · Ilya Leoshkevich
tests/tcg/multiarch/vma-pthread.c new file +6 -0
+static void *thread_write(void *arg)
+{
+    struct context *ctx = arg;
+    struct timespec *ts;
+    size_t i, j;
+    int ret;

tests/tcg/multiarch/vma-pthread.c line 77

Quoted 10 lines
tests/tcg/multiarch/vma-pthread.c new file +10 -0
+
+    for (i = 0; ctx->mutator_count; i++) {
+        j = (i & PAGE_IDX_MASK) | PAGE_IDX_W_MASK;
+        /* Write directly. */
+        memcpy(&ctx->ptr[j * ctx->pagesize], nop_func, sizeof(nop_func));
+        /* Write using a syscall. */
+        ts = (struct timespec *)(&ctx->ptr[(j + 1) * ctx->pagesize] -
+                                 sizeof(struct timespec));
+        ret = clock_gettime(CLOCK_REALTIME, ts);
+        assert(ret == 0);

With the 3 issues that you pointed out in the other email, this is the only remaining failure. This happens because of two issues:

(1) When checking for writability, we actually check for both read+write:

#define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)

This is very likely a bug, but we'd need to audit all uses to find out where we might really want read+write.

Quoted 18 lines
tests/tcg/multiarch/vma-pthread.c new file +18 -0
+static void *thread_mutate(void *arg)
+{
+    size_t i, start_idx, end_idx, page_idx, tmp;
+    struct context *ctx = arg;
+    unsigned int seed;
+    int prot, ret;
+
+    seed = (unsigned int)time(NULL);
+    for (i = 0; i < 50000; i++) {
+        start_idx = rand_r(&seed) & PAGE_IDX_MASK;
+        end_idx = rand_r(&seed) & PAGE_IDX_MASK;
+        if (start_idx > end_idx) {
+            tmp = start_idx;
+            start_idx = end_idx;
+            end_idx = tmp;
+        }
+        prot = rand_r(&seed) & (PROT_READ | PROT_WRITE | PROT_EXEC);
+        for (page_idx = start_idx & REGION_MASK; page_idx <= end_idx;

tests/tcg/multiarch/vma-pthread.c line 123

Quoted 10 lines
tests/tcg/multiarch/vma-pthread.c new file +10 -0
+             page_idx += PAGES_PER_REGION) {
+            if (page_idx & PAGE_IDX_R_MASK) {
+                prot |= PROT_READ;
+            }
+            if (page_idx & PAGE_IDX_W_MASK) {
+                prot |= PROT_WRITE;
+            }
+            if (page_idx & PAGE_IDX_X_MASK) {
+                prot |= PROT_EXEC;
+            }

... and here we can wind up with write-only pages.

(2) Certain hardware, like x86_64, does not support write-only pages -- writable implies readable -- so the testcase runs on some hardware. It is a bug that we don't model this as well.

r~