diff mbox series

[v2,4/4] tests/tcg/linux-test: Add linux-fork-trap test

Message ID 20230213125238.331881-5-iii@linux.ibm.com
State New
Headers show
Series Fix deadlock when dying because of a signal | expand

Commit Message

Ilya Leoshkevich Feb. 13, 2023, 12:52 p.m. UTC
Check that dying due to a signal does not deadlock.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 tests/tcg/multiarch/linux/linux-fork-trap.c | 48 +++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 tests/tcg/multiarch/linux/linux-fork-trap.c

Comments

Alex Bennée Feb. 14, 2023, 9:51 a.m. UTC | #1
Ilya Leoshkevich <iii@linux.ibm.com> writes:

> Check that dying due to a signal does not deadlock.
>
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  tests/tcg/multiarch/linux/linux-fork-trap.c | 48 +++++++++++++++++++++
>  1 file changed, 48 insertions(+)
>  create mode 100644 tests/tcg/multiarch/linux/linux-fork-trap.c
>
> diff --git a/tests/tcg/multiarch/linux/linux-fork-trap.c b/tests/tcg/multiarch/linux/linux-fork-trap.c
> new file mode 100644
> index 00000000000..a921f875380
> --- /dev/null
> +++ b/tests/tcg/multiarch/linux/linux-fork-trap.c
> @@ -0,0 +1,48 @@
> +/*
> + * Test that a fork()ed process terminates after __builtin_trap().
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +#include <assert.h>
> +#include <stdlib.h>
> +#include <sys/resource.h>
> +#include <sys/wait.h>
> +#include <unistd.h>
> +
> +int main(void)
> +{
> +    struct rlimit nodump;
> +    pid_t err, pid;
> +    int wstatus;
> +
> +    pid = fork();
> +    assert(pid != -1);
> +    if (pid == 0) {
> +        /* We are about to crash on purpose; disable core dumps. */

I think we could benefit from two printfs in the test so the following:

  ➜  ./qemu-aarch64 ./tests/tcg/aarch64-linux-user/linux-fork-trap 
  qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped
  🕙09:50:14 alex@zen:qemu.git/builds/arm.all  on  testing/next [$!?] 
  ➜  echo $status
  0

is a little less confusing.

> +        if (getrlimit(RLIMIT_CORE, &nodump)) {
> +            return EXIT_FAILURE;
> +        }
> +        nodump.rlim_cur = 0;
> +        if (setrlimit(RLIMIT_CORE, &nodump)) {
> +            return EXIT_FAILURE;
> +        }
> +        /*
> +         * An alternative would be to dereference a NULL pointer, but that
> +         * would be an UB in C.
> +         */

 printf("about to trigger fault...\n");

> +#if defined(__MICROBLAZE__)
> +        /*
> +         * gcc emits "bri 0", which is an endless loop.
> +         * Take glibc's ABORT_INSTRUCTION.
> +         */
> +        asm volatile("brki r0,-1");
> +#else
> +        __builtin_trap();
> +#endif
> +    }
> +    err = waitpid(pid, &wstatus, 0);
> +    assert(err == pid);
> +    assert(WIFSIGNALED(wstatus));
> +

  printf("faulting thread exited cleanly\n");

Othwerwise:

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>



> +    return EXIT_SUCCESS;
> +}
diff mbox series

Patch

diff --git a/tests/tcg/multiarch/linux/linux-fork-trap.c b/tests/tcg/multiarch/linux/linux-fork-trap.c
new file mode 100644
index 00000000000..a921f875380
--- /dev/null
+++ b/tests/tcg/multiarch/linux/linux-fork-trap.c
@@ -0,0 +1,48 @@ 
+/*
+ * Test that a fork()ed process terminates after __builtin_trap().
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <assert.h>
+#include <stdlib.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+int main(void)
+{
+    struct rlimit nodump;
+    pid_t err, pid;
+    int wstatus;
+
+    pid = fork();
+    assert(pid != -1);
+    if (pid == 0) {
+        /* We are about to crash on purpose; disable core dumps. */
+        if (getrlimit(RLIMIT_CORE, &nodump)) {
+            return EXIT_FAILURE;
+        }
+        nodump.rlim_cur = 0;
+        if (setrlimit(RLIMIT_CORE, &nodump)) {
+            return EXIT_FAILURE;
+        }
+        /*
+         * An alternative would be to dereference a NULL pointer, but that
+         * would be an UB in C.
+         */
+#if defined(__MICROBLAZE__)
+        /*
+         * gcc emits "bri 0", which is an endless loop.
+         * Take glibc's ABORT_INSTRUCTION.
+         */
+        asm volatile("brki r0,-1");
+#else
+        __builtin_trap();
+#endif
+    }
+    err = waitpid(pid, &wstatus, 0);
+    assert(err == pid);
+    assert(WIFSIGNALED(wstatus));
+
+    return EXIT_SUCCESS;
+}