Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_mprotect: test if mprotected pages are copied #381

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Comment on lines +85 to +88
Copy link
Member

@agkaminski agkaminski Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it might be easier to do this test in reverse - modify the memory in child instead. This would be nicer, as it would only require waitpid sync point, without sleep. But I'm not 100% sure that such approach would test exactly the same thing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add separate test case for that

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
Loading