Skip to content

Commit

Permalink
test_mprotect: test if mprotected pages are copied
Browse files Browse the repository at this point in the history
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
  • Loading branch information
badochov committed Oct 17, 2024
1 parent 036b90f commit 2ff2ee7
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions mem/test_mprotect.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/wait.h>

#include "unity_fixture.h"

Expand Down Expand Up @@ -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);
}


Expand Down

0 comments on commit 2ff2ee7

Please sign in to comment.