summaryrefslogtreecommitdiff
path: root/tests/spawn_test.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2017-11-28 19:58:00 -0800
committerElliott Hughes <enh@google.com>2017-11-29 18:36:38 -0800
commit7bfacaabf250d8aed5de82e8091b3251bd8dd53f (patch)
treed7f131953ebc2112db639e19dad7f152faa7f155 /tests/spawn_test.cpp
parent0a25aa60a2a7cc6b681dd2e31321000cedbff77e (diff)
Fix posix_spawn signal defaulting.
Add a new stress test, and fix the code to pass it. We need to ensure that we reset signal handlers for caught signals before unblocking signals in the child, we need to ensure that this happens even if you haven't passed a pthread_spawn_attr_t, and we need to ensure that this happens if you pass in an empty sigdefault set. Bug: http://b/68707996 Test: ran tests Change-Id: I348e9b17b1bdf221591da42c0ada133d98471d66
Diffstat (limited to 'tests/spawn_test.cpp')
-rw-r--r--tests/spawn_test.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/spawn_test.cpp b/tests/spawn_test.cpp
index 6a3920e39..d2e4ea1da 100644
--- a/tests/spawn_test.cpp
+++ b/tests/spawn_test.cpp
@@ -20,6 +20,7 @@
#include <fcntl.h>
#include <gtest/gtest.h>
+#include "ScopedSignalHandler.h"
#include "utils.h"
#include <android-base/file.h>
@@ -386,3 +387,41 @@ TEST(spawn, posix_spawn_POSIX_SPAWN_SETSIGDEF) {
ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
}
+
+TEST(spawn, signal_stress) {
+ // Ensure that posix_spawn doesn't restore the caller's signal mask in the
+ // child without first defaulting any caught signals (http://b/68707996).
+ static pid_t parent = getpid();
+
+ pid_t pid = fork();
+ ASSERT_NE(-1, pid);
+
+ if (pid == 0) {
+ for (size_t i = 0; i < 1024; ++i) {
+ kill(0, SIGWINCH);
+ usleep(10);
+ }
+ return;
+ }
+
+ // We test both with and without attributes, because they used to be
+ // different codepaths. We also test with an empty `sigdefault` set.
+ posix_spawnattr_t attr1;
+ posix_spawnattr_init(&attr1);
+
+ sigset_t empty_mask = {};
+ posix_spawnattr_t attr2;
+ posix_spawnattr_init(&attr2);
+ posix_spawnattr_setflags(&attr2, POSIX_SPAWN_SETSIGDEF);
+ posix_spawnattr_setsigdefault(&attr2, &empty_mask);
+
+ posix_spawnattr_t* attrs[] = { nullptr, &attr1, &attr2 };
+
+ ScopedSignalHandler ssh(SIGWINCH, [](int) { ASSERT_EQ(getpid(), parent); });
+
+ ExecTestHelper eth;
+ eth.SetArgs({"true", nullptr});
+ for (size_t i = 0; i < 128; ++i) {
+ posix_spawn(nullptr, "true", nullptr, attrs[i % 3], eth.GetArgs(), nullptr);
+ }
+}