In a future commit we will change the fuzzing technique to use a
fuzzed data provider. After this, test cases will no longer contain
the straight output from nbdkit. Therefore the --write option will no
longer be useful. (We will need to write a test case by hand instead)
---
fuzzing/libnbd-fuzz-wrapper.c | 176 +---------------------------------
1 file changed, 3 insertions(+), 173 deletions(-)
diff --git a/fuzzing/libnbd-fuzz-wrapper.c b/fuzzing/libnbd-fuzz-wrapper.c
index 23ef131..338adc0 100644
--- a/fuzzing/libnbd-fuzz-wrapper.c
+++ b/fuzzing/libnbd-fuzz-wrapper.c
@@ -42,29 +42,11 @@
#include <libnbd.h>
#ifndef SOCK_CLOEXEC
-/* We do use exec, but only when using --write, which is a maintainer
- * operation that always runs on Linux.
- */
-#define SOCK_CLOEXEC 0
+#define SOCK_CLOEXEC 0 /* This file doesn't use exec */
#endif
-/* If defined, instead of creating the fuzzer wrapper, write a working
- * testcase to the named file. This runs nbdkit and captures the
- * input to libnbd (output from nbdkit) in the file. Use it like
- * this:
- *
- * Generate test case:
- * $ fuzzing/libnbd-fuzz-wrapper --write testcase
- * Test it:
- * $ LIBNBD_DEBUG=1 fuzzing/libnbd-fuzz-wrapper testcase
- * If it's good, copy it to the test case directory:
- * $ mv testcase fuzzing/testcase_dir/
- */
-static const char *write_testcase;
-
static void client (int s);
static void server (int fd, int s);
-static void nbdkit (int fd, int s);
int
main (int argc, char *argv[])
@@ -73,16 +55,7 @@ main (int argc, char *argv[])
pid_t pid;
int sv[2], r, status;
- if (argc == 3 && strcmp (argv[1], "--write") == 0) {
- write_testcase = argv[2];
-
- fd = open (argv[2], O_WRONLY|O_TRUNC|O_CREAT, 0644);
- if (fd == -1) {
- perror (argv[1]);
- exit (EXIT_FAILURE);
- }
- }
- else if (argc == 2) {
+ if (argc == 2) {
/* Open the test case before we fork so we know the file exists. */
fd = open (argv[1], O_RDONLY);
if (fd == -1) {
@@ -133,10 +106,7 @@ main (int argc, char *argv[])
/* Child: NBD server. */
close (sv[0]);
- if (!write_testcase)
- server (fd, sv[1]);
- else
- nbdkit (fd, sv[1]);
+ server (fd, sv[1]);
close (sv[1]);
@@ -320,143 +290,3 @@ server (int fd, int sock)
}
} /* for (;;) */
}
-
-static void xwrite (int fd, const char *buf, size_t n);
-
-/* This is used for --write mode where we capture nbdkit output into a
- * testcase file.
- */
-static void
-nbdkit (int fd, int sock)
-{
- pid_t pid;
- int rfd[2], wfd[2];
- struct pollfd pfds[2];
- char buf[512];
- ssize_t r;
- bool parent_dead = false;
-
- if (pipe (rfd) == -1) { /* Will be our input, nbdkit's stdout */
- perror ("pipe");
- _exit (EXIT_FAILURE);
- }
-
- if (pipe (wfd) == -1) { /* Will be our output, nbdkit's stdin */
- perror ("pipe");
- _exit (EXIT_FAILURE);
- }
-
- /* Run nbdkit as another subprocess. */
- pid = fork ();
- if (pid == -1) {
- perror ("fork");
- _exit (EXIT_FAILURE);
- }
-
- if (pid == 0) { /* Child - nbdkit */
- close (fd);
- close (sock);
-
- close (rfd[0]);
- dup2 (rfd[1], STDOUT_FILENO);
-
- close (wfd[1]);
- dup2 (wfd[0], STDIN_FILENO);
-
- execlp ("nbdkit",
- "nbdkit", "--exit-with-parent", "-s",
- "sparse-random",
- "--filter=cow",
- "size=1M",
- "runlength=8192", "percent=50",
"random-content=true",
- NULL);
- perror ("execlp");
- _exit (EXIT_FAILURE);
- }
-
- /* Here we shuffle the data:
- *
- * Our parent This process Our child
- * (libnbd) (nbdkit)
- * -------------> ------> -----wfd[1]---->
- * sock
- * <------------- <------ /<----rfd[0]-----
- * |
- * v
- * writes from nbdkit tee'd to fd (testcase)
- *
- * We do everything blocking because that is easier and performance
- * doesn't matter since we're only capturing a test case.
- */
- close (rfd[1]);
- close (wfd[0]);
-
- while (!parent_dead || rfd[0] >= 0) {
- pfds[0].fd = parent_dead ? -1 : sock;
- pfds[0].events = POLLIN;
- pfds[0].revents = 0;
-
- pfds[1].fd = rfd[0];
- pfds[1].events = POLLIN;
- pfds[1].revents = 0;
-
- if (poll (pfds, 2, -1) == -1) {
- if (errno == EINTR)
- continue;
- perror ("poll");
- _exit (EXIT_FAILURE);
- }
-
- if (!parent_dead && (pfds[0].revents & POLLIN) != 0) {
- r = read (sock, buf, sizeof buf);
- if (r == -1 && errno != EINTR) {
- perror ("read (libnbd)");
- _exit (EXIT_FAILURE);
- }
- else if (r == 0) {
- parent_dead = true;
- continue;
- }
- else if (r > 0)
- xwrite (wfd[1], buf, r);
- }
-
- if (rfd[0] != -1 && (pfds[1].revents & POLLIN) != 0) {
- r = read (rfd[0], buf, sizeof buf);
- if (r == -1 && errno == EINTR) {
- perror ("read (nbdkit)");
- _exit (EXIT_FAILURE);
- }
- else if (r == 0) {
- close (rfd[0]);
- rfd[0] = -1;
- continue;
- }
- else if (r > 0) {
- xwrite (fd, buf, r);
- xwrite (sock, buf, r);
- }
- }
- }
-
- if (close (fd) == -1) {
- perror ("close");
- _exit (EXIT_FAILURE);
- }
-}
-
-static void
-xwrite (int fd, const char *buf, size_t n)
-{
- ssize_t r;
-
- while (n > 0) {
- r = write (fd, buf, n);
- if (r == -1) {
- perror ("write");
- _exit (EXIT_FAILURE);
- }
- buf += r;
- n -= r;
- }
-}
--
2.43.1