Skip to content

Commit

Permalink
test_mprotect: add pages_in_child_copied test
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.

JIRA: RTOS-953
  • Loading branch information
badochov committed Oct 16, 2024
1 parent 036b90f commit 82db1e7
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 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,49 @@ 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 * PAGES, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
TEST_ASSERT(area != MAP_FAILED);

for (int page = 0; page < PAGES; page++) {
area[page * page_size] = 0x42;
}

TEST_ASSERT_EQUAL_INT(0, mprotect(area, page_size * PAGES, PROT_READ));

pid_t pid = fork();
TEST_ASSERT(pid >= 0);
if (pid == 0) {
/* Wait for modifications in parent. */
sleep(1);
for (int page = 0; page < PAGES; page++) {
if (area[page * page_size] != 0x42) {
printf("WTH %p!\n", area);
exit(1);
}
}
exit(0);
}

TEST_ASSERT_EQUAL_INT(0, mprotect(area, page_size * PAGES, PROT_READ | PROT_WRITE));
for (int page = 0; page < PAGES; page++) {
area[page * page_size] = 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 * PAGES));
}


TEST_GROUP_RUNNER(test_mprotect)
{
RUN_TEST_CASE(test_mprotect, test_mprotect_singlecore);
RUN_TEST_CASE(test_mprotect, pages_in_child_copied);
}


Expand Down

0 comments on commit 82db1e7

Please sign in to comment.