diff mbox series

[2/2] selftests/nolibc: add testcase for pipe.

Message ID 160ddef0313e11085ee906144d6d9678b8156171.1690307717.git.tanyuan@tinylab.org (mailing list archive)
State New
Headers show
Series tools/nolibc: add pipe() and its testcase | expand

Commit Message

Yuan Tan July 25, 2023, 6:01 p.m. UTC
Add a testcase of pipe that child process sends message to parent process.

Signed-off-by: Yuan Tan <tanyuan@tinylab.org>
---
 tools/testing/selftests/nolibc/nolibc-test.c | 34 ++++++++++++++++++++
 1 file changed, 34 insertions(+)

Comments

Thomas Weißschuh July 29, 2023, 10:17 p.m. UTC | #1
On 2023-07-25 14:01:30-0400, Yuan Tan wrote:
> Add a testcase of pipe that child process sends message to parent process.
> 
> Signed-off-by: Yuan Tan <tanyuan@tinylab.org>
> ---
>  tools/testing/selftests/nolibc/nolibc-test.c | 34 ++++++++++++++++++++
>  1 file changed, 34 insertions(+)
> 
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index 03b1d30f5507..43ba2884fd1e 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -767,6 +767,39 @@ int test_mmap_munmap(void)
>  	return ret;
>  }
>  
> +int test_pipe(void)
> +{
> +	int pipefd[2];
> +	char buf[32];
> +	pid_t pid;
> +	char *msg = "hello, nolibc";

const char * const

> +
> +	if (pipe(pipefd) == -1)
> +		return 1;
> +
> +	pid = fork();
> +
> +	switch (pid) {
> +	case -1:
> +		return 1;
> +
> +	case 0:
> +		close(pipefd[0]);
> +		write(pipefd[1], msg, strlen(msg));

Isn't this missing to write trailing the 0 byte?
Also check the return value.

> +		close(pipefd[1]);

Do we need to close the pipefds? The process is exiting anyways.

> +		exit(EXIT_SUCCESS);
> +
> +	default:
> +		close(pipefd[1]);
> +		read(pipefd[0], buf, 32);

Use sizeof(buf). Check return value == strlen(msg).

> +		close(pipefd[0]);
> +		wait(NULL);

waitpid(pid, NULL, 0);

> +
> +		if (strcmp(buf, msg))
> +			return 1;
> +		return 0;

return !!strcmp(buf, msg);

> +	}
> +}
>  
>  /* Run syscall tests between IDs <min> and <max>.
>   * Return 0 on success, non-zero on failure.
> @@ -851,6 +884,7 @@ int run_syscall(int min, int max)
>  		CASE_TEST(mmap_munmap_good);  EXPECT_SYSZR(1, test_mmap_munmap()); break;
>  		CASE_TEST(open_tty);          EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break;
>  		CASE_TEST(open_blah);         EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break;
> +		CASE_TEST(pipe);              EXPECT_SYSZR(1, test_pipe()); break;
>  		CASE_TEST(poll_null);         EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;
>  		CASE_TEST(poll_stdout);       EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break;
>  		CASE_TEST(poll_fault);        EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;
> -- 
> 2.39.2
>
Willy Tarreau July 30, 2023, 3:33 a.m. UTC | #2
On Sun, Jul 30, 2023 at 12:17:24AM +0200, Thomas Weißschuh wrote:
> > +	case 0:
> > +		close(pipefd[0]);
> > +		write(pipefd[1], msg, strlen(msg));
> 
> Isn't this missing to write trailing the 0 byte?

It depends if the other side expects to get the trailing 0.
In general it's better to avoid sending it since it's only
used for internal representation, and the other side must
be prepared to receive anything anyway.

> Also check the return value.

Indeed!

> > +		close(pipefd[1]);
> 
> Do we need to close the pipefds? The process is exiting anyways.

It's better to, because we could imagine looping over the tests for
example. Thus each test shoulld have as little impact as possible
on other tests.

> > +		exit(EXIT_SUCCESS);
> > +
> > +	default:
> > +		close(pipefd[1]);
> > +		read(pipefd[0], buf, 32);
> 
> Use sizeof(buf). Check return value == strlen(msg).
> 
> > +		close(pipefd[0]);
> > +		wait(NULL);
> 
> waitpid(pid, NULL, 0);
> 
> > +
> > +		if (strcmp(buf, msg))
> > +			return 1;
> > +		return 0;
> 
> return !!strcmp(buf, msg);

In fact before that we need to terminate the output buffer. If for any
reason the transfer fails (e.g. the syscall fails or transfers data at
another location or of another length, we could end up comparing past
the end of the buffer. Thus I suggest adding this immediately after the
read():

		buf[sizeof(buf) - 1] = 0;

Willy
Thomas Weißschuh July 30, 2023, 6:55 a.m. UTC | #3
On 2023-07-30 05:33:43+0200, Willy Tarreau wrote:
> On Sun, Jul 30, 2023 at 12:17:24AM +0200, Thomas Weißschuh wrote:
> > > +	case 0:
> > > +		close(pipefd[0]);
> > > +		write(pipefd[1], msg, strlen(msg));
> > 
> > Isn't this missing to write trailing the 0 byte?
> 
> It depends if the other side expects to get the trailing 0.
> In general it's better to avoid sending it since it's only
> used for internal representation, and the other side must
> be prepared to receive anything anyway.
> 
> > Also check the return value.
> 
> Indeed!
> 
> > > +		close(pipefd[1]);
> > 
> > Do we need to close the pipefds? The process is exiting anyways.
> 
> It's better to, because we could imagine looping over the tests for
> example. Thus each test shoulld have as little impact as possible
> on other tests.

I meant the newly forked child exiting, not nolibc-test in general.
The exit is just below, so the fds in the child are close here anyways.
                |
		|
		v
> > > +		exit(EXIT_SUCCESS);
> > > +
> > > +	default:
> > > +		close(pipefd[1]);
> > > +		read(pipefd[0], buf, 32);
> > 
> > Use sizeof(buf). Check return value == strlen(msg).
> > 
> > > +		close(pipefd[0]);
> > > +		wait(NULL);
> > 
> > waitpid(pid, NULL, 0);
> > 
> > > +
> > > +		if (strcmp(buf, msg))
> > > +			return 1;
> > > +		return 0;
> > 
> > return !!strcmp(buf, msg);
> 
> In fact before that we need to terminate the output buffer. If for any
> reason the transfer fails (e.g. the syscall fails or transfers data at
> another location or of another length, we could end up comparing past
> the end of the buffer. Thus I suggest adding this immediately after the
> read():
> 
> 		buf[sizeof(buf) - 1] = 0;

This would still access uninitialized memory and lead to UB in strcmp as
not all bytes in buf were written to by read().

If we want to be really sure we should use memcmp() instead of strcmp().
For memcmp() I would prefer to transfer and check without the '\0', so
my review comments from before need to be adapted a bit.
Willy Tarreau July 30, 2023, 7:12 a.m. UTC | #4
On Sun, Jul 30, 2023 at 08:55:47AM +0200, Thomas Weißschuh wrote:
> On 2023-07-30 05:33:43+0200, Willy Tarreau wrote:
> > On Sun, Jul 30, 2023 at 12:17:24AM +0200, Thomas Weißschuh wrote:
> > > > +	case 0:
> > > > +		close(pipefd[0]);
> > > > +		write(pipefd[1], msg, strlen(msg));
> > > 
> > > Isn't this missing to write trailing the 0 byte?
> > 
> > It depends if the other side expects to get the trailing 0.
> > In general it's better to avoid sending it since it's only
> > used for internal representation, and the other side must
> > be prepared to receive anything anyway.
> > 
> > > Also check the return value.
> > 
> > Indeed!
> > 
> > > > +		close(pipefd[1]);
> > > 
> > > Do we need to close the pipefds? The process is exiting anyways.
> > 
> > It's better to, because we could imagine looping over the tests for
> > example. Thus each test shoulld have as little impact as possible
> > on other tests.
> 
> I meant the newly forked child exiting, not nolibc-test in general.
> The exit is just below, so the fds in the child are close here anyways.

Ah OK, but still it remains cleaner with it IMHO (i.e. better rely on
explicit things in tests, that's less doubts when they fail).

> > > > +	default:
> > > > +		close(pipefd[1]);
> > > > +		read(pipefd[0], buf, 32);
> > > 
> > > Use sizeof(buf). Check return value == strlen(msg).
> > > 
> > > > +		close(pipefd[0]);
> > > > +		wait(NULL);
> > > 
> > > waitpid(pid, NULL, 0);
> > > 
> > > > +
> > > > +		if (strcmp(buf, msg))
> > > > +			return 1;
> > > > +		return 0;
> > > 
> > > return !!strcmp(buf, msg);
> > 
> > In fact before that we need to terminate the output buffer. If for any
> > reason the transfer fails (e.g. the syscall fails or transfers data at
> > another location or of another length, we could end up comparing past
> > the end of the buffer. Thus I suggest adding this immediately after the
> > read():
> > 
> > 		buf[sizeof(buf) - 1] = 0;
> 
> This would still access uninitialized memory and lead to UB in strcmp as
> not all bytes in buf were written to by read().
> 
> If we want to be really sure we should use memcmp() instead of strcmp().
> For memcmp() I would prefer to transfer and check without the '\0', so
> my review comments from before need to be adapted a bit.

In fact you make a good point regarding the fact that the test doesn't
use read()'s return value. This problem totally goes away if the return
value is used, e.g.:

      len = read(pipefd[0], buf, sizeof(buf));
      close(pipefd[0]);
      waitpid(pid, NULL, 0);
      return len < 0 || len > sizeof(buf) || len > strlen(msg) || memcmp(buf, msg, len) != 0;

Willy
Thomas Weißschuh July 30, 2023, 8:07 a.m. UTC | #5
On 2023-07-30 09:12:27+0200, Willy Tarreau wrote:
> On Sun, Jul 30, 2023 at 08:55:47AM +0200, Thomas Weißschuh wrote:
> > On 2023-07-30 05:33:43+0200, Willy Tarreau wrote:
> > > On Sun, Jul 30, 2023 at 12:17:24AM +0200, Thomas Weißschuh wrote:
> > > > > +	case 0:
> > > > > +		close(pipefd[0]);
> > > > > +		write(pipefd[1], msg, strlen(msg));
> > > > 
> > > > Isn't this missing to write trailing the 0 byte?
> > > 
> > > It depends if the other side expects to get the trailing 0.
> > > In general it's better to avoid sending it since it's only
> > > used for internal representation, and the other side must
> > > be prepared to receive anything anyway.
> > > 
> > > > Also check the return value.
> > > 
> > > Indeed!
> > > 
> > > > > +		close(pipefd[1]);
> > > > 
> > > > Do we need to close the pipefds? The process is exiting anyways.
> > > 
> > > It's better to, because we could imagine looping over the tests for
> > > example. Thus each test shoulld have as little impact as possible
> > > on other tests.
> > 
> > I meant the newly forked child exiting, not nolibc-test in general.
> > The exit is just below, so the fds in the child are close here anyways.
> 
> Ah OK, but still it remains cleaner with it IMHO (i.e. better rely on
> explicit things in tests, that's less doubts when they fail).

Accepted :-)

> > > > > +	default:
> > > > > +		close(pipefd[1]);
> > > > > +		read(pipefd[0], buf, 32);
> > > > 
> > > > Use sizeof(buf). Check return value == strlen(msg).
> > > > 
> > > > > +		close(pipefd[0]);
> > > > > +		wait(NULL);
> > > > 
> > > > waitpid(pid, NULL, 0);
> > > > 
> > > > > +
> > > > > +		if (strcmp(buf, msg))
> > > > > +			return 1;
> > > > > +		return 0;
> > > > 
> > > > return !!strcmp(buf, msg);
> > > 
> > > In fact before that we need to terminate the output buffer. If for any
> > > reason the transfer fails (e.g. the syscall fails or transfers data at
> > > another location or of another length, we could end up comparing past
> > > the end of the buffer. Thus I suggest adding this immediately after the
> > > read():
> > > 
> > > 		buf[sizeof(buf) - 1] = 0;
> > 
> > This would still access uninitialized memory and lead to UB in strcmp as
> > not all bytes in buf were written to by read().
> > 
> > If we want to be really sure we should use memcmp() instead of strcmp().
> > For memcmp() I would prefer to transfer and check without the '\0', so
> > my review comments from before need to be adapted a bit.
> 
> In fact you make a good point regarding the fact that the test doesn't
> use read()'s return value. This problem totally goes away if the return
> value is used, e.g.:
> 
>       len = read(pipefd[0], buf, sizeof(buf));
>       close(pipefd[0]);
>       waitpid(pid, NULL, 0);
>       return len < 0 || len > sizeof(buf) || len > strlen(msg) || memcmp(buf, msg, len) != 0;

Wouldn't this happily accept len == 0?

Why not just:

if (len != strlen(msg))
  return 1;
return !!memcmp(buf, msg, len);

Also so far we have assumed that one call one call to read() is enough.
But looking at pipe(7) this is not guaranteed by the spec.
If we want to be really sure, a loop around read() seems to be necessary.
Willy Tarreau July 30, 2023, 11:08 a.m. UTC | #6
On Sun, Jul 30, 2023 at 10:07:24AM +0200, Thomas Weißschuh wrote:
> > In fact you make a good point regarding the fact that the test doesn't
> > use read()'s return value. This problem totally goes away if the return
> > value is used, e.g.:
> > 
> >       len = read(pipefd[0], buf, sizeof(buf));
> >       close(pipefd[0]);
> >       waitpid(pid, NULL, 0);
> >       return len < 0 || len > sizeof(buf) || len > strlen(msg) || memcmp(buf, msg, len) != 0;
> 
> Wouldn't this happily accept len == 0?
> 
> Why not just:
> 
> if (len != strlen(msg))
>   return 1;
> return !!memcmp(buf, msg, len);

Indeed, works for me.

> Also so far we have assumed that one call one call to read() is enough.
> But looking at pipe(7) this is not guaranteed by the spec.
> If we want to be really sure, a loop around read() seems to be necessary.

In practice it will be OK as the message is small and sent in one syscall,
so let's not care too much about this for now.

Willy
diff mbox series

Patch

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 03b1d30f5507..43ba2884fd1e 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -767,6 +767,39 @@  int test_mmap_munmap(void)
 	return ret;
 }
 
+int test_pipe(void)
+{
+	int pipefd[2];
+	char buf[32];
+	pid_t pid;
+	char *msg = "hello, nolibc";
+
+	if (pipe(pipefd) == -1)
+		return 1;
+
+	pid = fork();
+
+	switch (pid) {
+	case -1:
+		return 1;
+
+	case 0:
+		close(pipefd[0]);
+		write(pipefd[1], msg, strlen(msg));
+		close(pipefd[1]);
+		exit(EXIT_SUCCESS);
+
+	default:
+		close(pipefd[1]);
+		read(pipefd[0], buf, 32);
+		close(pipefd[0]);
+		wait(NULL);
+
+		if (strcmp(buf, msg))
+			return 1;
+		return 0;
+	}
+}
 
 /* Run syscall tests between IDs <min> and <max>.
  * Return 0 on success, non-zero on failure.
@@ -851,6 +884,7 @@  int run_syscall(int min, int max)
 		CASE_TEST(mmap_munmap_good);  EXPECT_SYSZR(1, test_mmap_munmap()); break;
 		CASE_TEST(open_tty);          EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break;
 		CASE_TEST(open_blah);         EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break;
+		CASE_TEST(pipe);              EXPECT_SYSZR(1, test_pipe()); break;
 		CASE_TEST(poll_null);         EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;
 		CASE_TEST(poll_stdout);       EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break;
 		CASE_TEST(poll_fault);        EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;