From 2ff2ee7185285f4b3baa6d89c8a007b46e172e43 Mon Sep 17 00:00:00 2001 From: Hubert Badocha Date: Wed, 16 Oct 2024 19:28:26 +0200 Subject: [PATCH] test_mprotect: test if mprotected pages are copied pages_in_child_copied test checks if pages with protection changed to read from read-write by mprotect are copied by child. pages_in_parent_copied test checks if pages with protection changed to read from read-write by mprotect are copied by parent. JIRA: RTOS-953 --- mem/test_mprotect.c | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/mem/test_mprotect.c b/mem/test_mprotect.c index 923ac5ed..d89a20fe 100644 --- a/mem/test_mprotect.c +++ b/mem/test_mprotect.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "unity_fixture.h" @@ -67,9 +68,70 @@ TEST(test_mprotect, test_mprotect_singlecore) TEST_ASSERT_EQUAL(0, munmap(area, page_size * PAGES)); } + +TEST(test_mprotect, pages_in_child_copied) +{ + unsigned char *area = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + TEST_ASSERT(area != MAP_FAILED); + + area[0] = 0x42; + + TEST_ASSERT_EQUAL_INT(0, mprotect(area, page_size, PROT_READ)); + + pid_t pid = fork(); + TEST_ASSERT(pid >= 0); + if (pid == 0) { + /* Wait for modifications in parent. */ + sleep(1); + if (area[0] != 0x42) { + exit(1); + } + exit(0); + } + + TEST_ASSERT_EQUAL_INT(0, mprotect(area, page_size, PROT_READ | PROT_WRITE)); + area[0] = 0x41; + + int returnStatus; + TEST_ASSERT(pid == waitpid(pid, &returnStatus, 0)); + TEST_ASSERT_EQUAL_INT(0, WEXITSTATUS(returnStatus)); + + TEST_ASSERT_EQUAL_INT(0, munmap(area, page_size)); +} + + +TEST(test_mprotect, pages_in_parent_copied) +{ + unsigned char *area = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + TEST_ASSERT(area != MAP_FAILED); + + area[0] = 0x42; + + TEST_ASSERT_EQUAL_INT(0, mprotect(area, page_size, PROT_READ)); + + pid_t pid = fork(); + TEST_ASSERT(pid >= 0); + if (pid == 0) { + TEST_ASSERT_EQUAL_INT(0, mprotect(area, page_size, PROT_READ | PROT_WRITE)); + area[0] = 0x41; + exit(0); + } + + int returnStatus; + TEST_ASSERT(pid == waitpid(pid, &returnStatus, 0)); + TEST_ASSERT_EQUAL_INT(0, WEXITSTATUS(returnStatus)); + + TEST_ASSERT_EQUAL_INT(0x42, area[0]); + + TEST_ASSERT_EQUAL_INT(0, munmap(area, page_size)); +} + + TEST_GROUP_RUNNER(test_mprotect) { RUN_TEST_CASE(test_mprotect, test_mprotect_singlecore); + RUN_TEST_CASE(test_mprotect, pages_in_child_copied); + RUN_TEST_CASE(test_mprotect, pages_in_parent_copied); }