Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b197a513 authored by Isaac Manjarres's avatar Isaac Manjarres Committed by Gerrit Code Review
Browse files

Merge changes Ia8bd5d47,I9ef7c82f,Iac923cca into main

* changes:
  ashmem_test: Always include PROT_EXEC as an expected permission
  ashmem_test: Remove tests that remove read access
  ashmem_test: Cleanup and document fork tests
parents 4220f93a 05af2766
Loading
Loading
Loading
Loading
+44 −29
Original line number Diff line number Diff line
@@ -84,6 +84,11 @@ TEST(AshmemTest, ForkTest) {
    ASSERT_EQ(0, memcmp(region1, data.data(), size));
    EXPECT_EQ(0, munmap(region1, size));

    // This part of the test forks a separate process via ASSERT_EXIT() and
    // clears the ashmem buffer.
    //
    // This is to ensure that updates to the contents of the buffer are visible
    // across processes with a reference to the buffer.
    ASSERT_EXIT(
        {
            if (!ashmem_valid(fd)) {
@@ -176,26 +181,22 @@ TEST(AshmemTest, ProtTest) {
    const size_t size = getpagesize();
    void *region;

    ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ));
    ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_EXEC));
    TestProtDenied(fd, size, PROT_WRITE);
    TestProtIs(fd, PROT_READ);
    TestProtIs(fd, PROT_READ | PROT_EXEC);
    ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ, &region));
    EXPECT_EQ(0, munmap(region, size));

    ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_WRITE));
    TestProtDenied(fd, size, PROT_READ);
    TestProtIs(fd, PROT_WRITE);
    ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_WRITE, &region));
    EXPECT_EQ(0, munmap(region, size));

    ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
    TestProtIs(fd, PROT_READ | PROT_WRITE);
    ASSERT_EQ(0, ashmem_set_prot_region(fd, PROT_READ));
    ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE |
                                             PROT_EXEC));
    TestProtIs(fd, PROT_READ | PROT_WRITE | PROT_EXEC);
    ASSERT_EQ(0, ashmem_set_prot_region(fd, PROT_READ | PROT_EXEC));
    errno = 0;
    ASSERT_EQ(-1, ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE))
    ASSERT_EQ(-1, ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE |
                                         PROT_EXEC))
        << "kernel shouldn't allow adding protection bits";
    EXPECT_EQ(EINVAL, errno);
    TestProtIs(fd, PROT_READ);
    TestProtIs(fd, PROT_READ | PROT_EXEC);
    TestProtDenied(fd, size, PROT_WRITE);
}

@@ -203,22 +204,29 @@ TEST(AshmemTest, ForkProtTest) {
    unique_fd fd;
    const size_t size = getpagesize();

    int protFlags[] = { PROT_READ, PROT_WRITE };
    for (size_t i = 0; i < arraysize(protFlags); i++) {
    ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));

    // This part of the test forks a process via ASSERT_EXIT()
    // and has the child process change the mapping permissions of the
    // buffer to read-only.
    //
    // The intent of this is to ensure that updates to the buffer's
    // mapping permissions are visible across processes that reference the
    // buffer.
    //
    // In this case, the parent process attempts to map the buffer with write
    // permission, which should fail.
    ASSERT_EXIT(
        {
            if (!ashmem_valid(fd)) {
                _exit(3);
                } else if (ashmem_set_prot_region(fd, protFlags[i]) >= 0) {
                    _exit(0);
                } else {
            } else if (ashmem_set_prot_region(fd, PROT_READ) == -1) {
                _exit(1);
            }
            _exit(0);
        },
        ::testing::ExitedWithCode(0), "");
        ASSERT_NO_FATAL_FAILURE(TestProtDenied(fd, size, protFlags[1-i]));
    }
    ASSERT_NO_FATAL_FAILURE(TestProtDenied(fd, size, PROT_WRITE));
}

TEST(AshmemTest, ForkMultiRegionTest) {
@@ -237,6 +245,13 @@ TEST(AshmemTest, ForkMultiRegionTest) {
        EXPECT_EQ(0, munmap(region, size));
    }

    // This part of the test creates a child process via ASSERT_EXIT()
    // that clears each of the ashmem buffers that were created earlier.
    //
    // The intent of this is for the parent process to check each region to make
    // sure that the updates from the child process are visible, thereby showing
    // that updates across multiple shared buffers are visible across multiple
    // processes.
    ASSERT_EXIT({
        for (int i = 0; i < nRegions; i++) {
            if (!ashmem_valid(fd[i])) {