From c5009e38f12810141f1ece9d64544af59d68a3d3 Mon Sep 17 00:00:00 2001 From: Jianfeng Jiang Date: Wed, 11 Sep 2024 09:50:56 +0000 Subject: [PATCH] Add pipe/short_rw test Co-authored-by: Ruihan Li --- kernel/src/fs/utils/channel.rs | 20 ++++------------ test/apps/pipe/short_rw.c | 43 ++++++++++++++++++++++++++++++++++ test/apps/scripts/fs.sh | 1 + 3 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 test/apps/pipe/short_rw.c diff --git a/kernel/src/fs/utils/channel.rs b/kernel/src/fs/utils/channel.rs index 7b11d383..37bff34c 100644 --- a/kernel/src/fs/utils/channel.rs +++ b/kernel/src/fs/utils/channel.rs @@ -155,7 +155,7 @@ impl Producer { return_errno_with_message!(Errno::EPIPE, "the channel is shut down"); } - let written_len = self.0.write(reader); + let written_len = self.0.write(reader)?; self.update_pollee(); if written_len > 0 { @@ -241,7 +241,7 @@ impl Consumer { // This must be recorded before the actual operation to avoid race conditions. let is_shutdown = self.is_shutdown(); - let read_len = self.0.read(writer); + let read_len = self.0.read(writer)?; self.update_pollee(); if read_len > 0 { @@ -301,25 +301,13 @@ impl Fifo { #[require(R > Read)] pub fn read(&self, writer: &mut dyn MultiWrite) -> Result { let mut rb = self.common.consumer.rb(); - match rb.read_fallible(writer) { - Ok(len) => len, - Err(e) => { - error!("memory read failed on the ring buffer, error: {e:?}"); - 0 - } - } + rb.read_fallible(writer) } #[require(R > Write)] pub fn write(&self, reader: &mut dyn MultiRead) -> Result { let mut rb = self.common.producer.rb(); - match rb.write_fallible(reader) { - Ok(len) => len, - Err(e) => { - error!("memory write failed on the ring buffer, error: {e:?}"); - 0 - } - } + rb.write_fallible(reader) } } diff --git a/test/apps/pipe/short_rw.c b/test/apps/pipe/short_rw.c new file mode 100644 index 00000000..dfea4d12 --- /dev/null +++ b/test/apps/pipe/short_rw.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: MPL-2.0 + +#include +#include +#include + +#include "../network/test.h" + +#define PAGE_SIZE 4096 + +static void *page; +static int rfd, wfd; + +FN_SETUP(short_read_and_write) +{ + int fildes[2]; + + page = mmap((void *)0x20000000, PAGE_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0); + CHECK(page == NULL ? -1 : 0); + + CHECK(pipe(fildes)); + rfd = fildes[0]; + wfd = fildes[1]; + + CHECK_WITH(write(wfd, "ab", 2), _ret == 2); +} +END_SETUP() + +FN_TEST(short_read_and_write) +{ + char *buf = page + PAGE_SIZE - 1; + buf[0] = 'x'; + + TEST_ERRNO(read(rfd, buf, 2), EFAULT); + + TEST_RES(read(rfd, buf, 1), _ret == 1 && buf[0] == 'a'); + + TEST_ERRNO(write(wfd, buf, 2), EFAULT); + + TEST_RES(write(wfd, buf, 1), _ret == 1); +} +END_TEST() \ No newline at end of file diff --git a/test/apps/scripts/fs.sh b/test/apps/scripts/fs.sh index 21b4d548..55c6dda5 100755 --- a/test/apps/scripts/fs.sh +++ b/test/apps/scripts/fs.sh @@ -62,4 +62,5 @@ test_fdatasync echo "All fdatasync test passed." pipe/pipe_err +pipe/short_rw epoll/epoll_err