From e6306d459a638fadfebcb58bf0ee707bf5c86c5d Mon Sep 17 00:00:00 2001 From: Liav A Date: Sun, 7 Aug 2022 22:09:54 +0300 Subject: [PATCH] Tests/Kernel: Add tests to ensure we don't regress InodeVMObjects --- Tests/Kernel/CMakeLists.txt | 4 ++ .../Kernel/TestEmptyPrivateInodeVMObject.cpp | 39 +++++++++++++++ Tests/Kernel/TestEmptySharedInodeVMObject.cpp | 39 +++++++++++++++ Tests/Kernel/TestPrivateInodeVMObject.cpp | 49 +++++++++++++++++++ Tests/Kernel/TestSharedInodeVMObject.cpp | 49 +++++++++++++++++++ 5 files changed, 180 insertions(+) create mode 100644 Tests/Kernel/TestEmptyPrivateInodeVMObject.cpp create mode 100644 Tests/Kernel/TestEmptySharedInodeVMObject.cpp create mode 100644 Tests/Kernel/TestPrivateInodeVMObject.cpp create mode 100644 Tests/Kernel/TestSharedInodeVMObject.cpp diff --git a/Tests/Kernel/CMakeLists.txt b/Tests/Kernel/CMakeLists.txt index a139abfb5c8..3ec015b2c3e 100644 --- a/Tests/Kernel/CMakeLists.txt +++ b/Tests/Kernel/CMakeLists.txt @@ -33,7 +33,11 @@ serenity_test("crash.cpp" Kernel MAIN_ALREADY_DEFINED) set(LIBTEST_BASED_SOURCES TestEFault.cpp + TestEmptyPrivateInodeVMObject.cpp + TestEmptySharedInodeVMObject.cpp TestInvalidUIDSet.cpp + TestSharedInodeVMObject.cpp + TestPrivateInodeVMObject.cpp TestKernelAlarm.cpp TestKernelFilePermissions.cpp TestKernelPledge.cpp diff --git a/Tests/Kernel/TestEmptyPrivateInodeVMObject.cpp b/Tests/Kernel/TestEmptyPrivateInodeVMObject.cpp new file mode 100644 index 00000000000..7cf31bb0421 --- /dev/null +++ b/Tests/Kernel/TestEmptyPrivateInodeVMObject.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +static u8* private_ptr = nullptr; + +static void private_zero_length_inode_vmobject_sync_signal_handler(int) +{ + auto rc = msync(private_ptr, 0x1000, MS_ASYNC); + EXPECT(rc == 0); + rc = munmap(private_ptr, 0x1000); + EXPECT(rc == 0); + exit(0); +} + +TEST_CASE(private_zero_length_inode_vmobject_sync) +{ + { + struct sigaction new_action { + { private_zero_length_inode_vmobject_sync_signal_handler }, 0, 0 + }; + int rc = sigaction(SIGBUS, &new_action, nullptr); + VERIFY(rc == 0); + } + int fd = open("/tmp/private_msync_test", O_RDWR | O_CREAT); + VERIFY(fd >= 0); + private_ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, fd, 0); + EXPECT(private_ptr != MAP_FAILED); + private_ptr[0] = 0x1; + VERIFY_NOT_REACHED(); +} diff --git a/Tests/Kernel/TestEmptySharedInodeVMObject.cpp b/Tests/Kernel/TestEmptySharedInodeVMObject.cpp new file mode 100644 index 00000000000..3900463db2e --- /dev/null +++ b/Tests/Kernel/TestEmptySharedInodeVMObject.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +static u8* shared_ptr = nullptr; + +static void shared_zero_length_inode_vmobject_sync_signal_handler(int) +{ + auto rc = msync(shared_ptr, 0x1000, MS_ASYNC); + EXPECT(rc == 0); + rc = munmap(shared_ptr, 0x1000); + EXPECT(rc == 0); + exit(0); +} + +TEST_CASE(shared_zero_length_inode_vmobject_sync) +{ + { + struct sigaction new_action { + { shared_zero_length_inode_vmobject_sync_signal_handler }, 0, 0 + }; + int rc = sigaction(SIGBUS, &new_action, nullptr); + VERIFY(rc == 0); + } + int fd = open("/tmp/shared_msync_test", O_RDWR | O_CREAT); + VERIFY(fd >= 0); + shared_ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0); + EXPECT(shared_ptr != MAP_FAILED); + shared_ptr[0] = 0x1; + VERIFY_NOT_REACHED(); +} diff --git a/Tests/Kernel/TestPrivateInodeVMObject.cpp b/Tests/Kernel/TestPrivateInodeVMObject.cpp new file mode 100644 index 00000000000..f33a8bce07f --- /dev/null +++ b/Tests/Kernel/TestPrivateInodeVMObject.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static u8* private_ptr = nullptr; + +static void private_non_empty_inode_vmobject_sync_signal_handler(int) +{ + auto rc = msync(private_ptr, 0x1000, MS_ASYNC); + EXPECT(rc == 0); + rc = munmap(private_ptr, 0x1000); + EXPECT(rc == 0); + exit(0); +} + +TEST_CASE(private_non_empty_inode_vmobject_sync) +{ + { + struct sigaction new_action { + { private_non_empty_inode_vmobject_sync_signal_handler }, 0, 0 + }; + int rc = sigaction(SIGBUS, &new_action, nullptr); + VERIFY(rc == 0); + } + u8 buf[0x1000]; + memset(buf, 0, sizeof(buf)); + int fd = open("/tmp/private_non_empty_msync_test", O_RDWR | O_CREAT); + VERIFY(fd >= 0); + auto rc = write(fd, buf, sizeof(buf)); + VERIFY(rc == sizeof(buf)); + private_ptr = (u8*)mmap(nullptr, 0x2000, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, fd, 0); + EXPECT(private_ptr != MAP_FAILED); + rc = msync(private_ptr, 0x2000, MS_ASYNC); + EXPECT(rc == 0); + private_ptr[0x1001] = 0x1; + VERIFY_NOT_REACHED(); +} diff --git a/Tests/Kernel/TestSharedInodeVMObject.cpp b/Tests/Kernel/TestSharedInodeVMObject.cpp new file mode 100644 index 00000000000..716c17e23b8 --- /dev/null +++ b/Tests/Kernel/TestSharedInodeVMObject.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static u8* shared_ptr = nullptr; + +static void shared_non_empty_inode_vmobject_sync_signal_handler(int) +{ + auto rc = msync(shared_ptr, 0x1000, MS_ASYNC); + EXPECT(rc == 0); + rc = munmap(shared_ptr, 0x1000); + EXPECT(rc == 0); + exit(0); +} + +TEST_CASE(shared_non_empty_inode_vmobject_sync) +{ + { + struct sigaction new_action { + { shared_non_empty_inode_vmobject_sync_signal_handler }, 0, 0 + }; + int rc = sigaction(SIGBUS, &new_action, nullptr); + VERIFY(rc == 0); + } + u8 buf[0x1000]; + memset(buf, 0, sizeof(buf)); + int fd = open("/tmp/shared_non_empty_msync_test", O_RDWR | O_CREAT); + VERIFY(fd >= 0); + auto rc = write(fd, buf, sizeof(buf)); + VERIFY(rc == sizeof(buf)); + shared_ptr = (u8*)mmap(nullptr, 0x2000, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0); + EXPECT(shared_ptr != MAP_FAILED); + rc = msync(shared_ptr, 0x2000, MS_ASYNC); + EXPECT(rc == 0); + shared_ptr[0x1001] = 0x1; + VERIFY_NOT_REACHED(); +}