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

Commit be11d570 authored by Greg Hackmann's avatar Greg Hackmann
Browse files

AshmemTest: test lseek() and read() on ashmem fds



ashmem has in-kernel handlers for lseek() and read(), but they aren't
currently being tested.

Add tests for various seeks on a buffer containing holes.  If we land
inside data, then check that we read() the expected data at that offset.

Test:  /data/nativetest64/libcutils_test/libcutils_test64 \
       --gtest_filter=AshmemTest.* (on hikey)
Test:  /data/nativetest/libcutils_test/libcutils_test32 \
       --gtest_filter=AshmemTest.* (on hikey)

Bug: 37254818
Change-Id: I96135a8cea2ce99932e3bc97b5254c95ef6b264a
Signed-off-by: default avatarGreg Hackmann <ghackmann@google.com>
parent 1dcd257a
Loading
Loading
Loading
Loading
+62 −4
Original line number Original line Diff line number Diff line
@@ -14,10 +14,11 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


#include <sys/mman.h>
#include <android-base/unique_fd.h>
#include <cutils/ashmem.h>
#include <cutils/ashmem.h>
#include <gtest/gtest.h>
#include <gtest/gtest.h>
#include <android-base/unique_fd.h>
#include <linux/fs.h>
#include <sys/mman.h>


using android::base::unique_fd;
using android::base::unique_fd;


@@ -29,8 +30,8 @@ void TestCreateRegion(size_t size, unique_fd &fd, int prot) {
    ASSERT_EQ(0, ashmem_set_prot_region(fd, prot));
    ASSERT_EQ(0, ashmem_set_prot_region(fd, prot));
}
}


void TestMmap(const unique_fd &fd, size_t size, int prot, void **region) {
void TestMmap(const unique_fd& fd, size_t size, int prot, void** region, off_t off = 0) {
    *region = mmap(nullptr, size, prot, MAP_SHARED, fd, 0);
    *region = mmap(nullptr, size, prot, MAP_SHARED, fd, off);
    ASSERT_NE(MAP_FAILED, *region);
    ASSERT_NE(MAP_FAILED, *region);
}
}


@@ -101,6 +102,63 @@ TEST(AshmemTest, ForkTest) {
    EXPECT_EQ(0, munmap(region2, size));
    EXPECT_EQ(0, munmap(region2, size));
}
}


TEST(AshmemTest, FileOperationsTest) {
    unique_fd fd;
    void* region;

    // Allocate a 4-page buffer, but leave page-sized holes on either side
    constexpr size_t size = PAGE_SIZE * 4;
    constexpr size_t dataSize = PAGE_SIZE * 2;
    constexpr size_t holeSize = PAGE_SIZE;
    ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
    ASSERT_NO_FATAL_FAILURE(TestMmap(fd, dataSize, PROT_READ | PROT_WRITE, &region, holeSize));

    uint8_t data[dataSize];
    FillData(data, dataSize);
    memcpy(region, data, dataSize);

    constexpr off_t dataStart = holeSize;
    constexpr off_t dataEnd = dataStart + dataSize;

    // The sequence of seeks below looks something like this:
    //
    // [    ][data][data][    ]
    // --^                          lseek(99, SEEK_SET)
    //   ------^                    lseek(dataStart, SEEK_CUR)
    // ------^                      lseek(0, SEEK_DATA)
    //       ------------^          lseek(dataStart, SEEK_HOLE)
    //                      ^--     lseek(-99, SEEK_END)
    //                ^------       lseek(-dataStart, SEEK_CUR)
    const struct {
        // lseek() parameters
        off_t offset;
        int whence;
        // Expected lseek() return value
        off_t ret;
    } seeks[] = {
        {99, SEEK_SET, 99},         {dataStart, SEEK_CUR, dataStart + 99},
        {0, SEEK_DATA, dataStart},  {dataStart, SEEK_HOLE, dataEnd},
        {-99, SEEK_END, size - 99}, {-dataStart, SEEK_CUR, dataEnd - 99},
    };
    for (const auto& cfg : seeks) {
        errno = 0;
        auto off = lseek(fd, cfg.offset, cfg.whence);
        ASSERT_EQ(cfg.ret, off) << "lseek(" << cfg.offset << ", " << cfg.whence << ") failed"
                                << (errno ? ": " : "") << (errno ? strerror(errno) : "");

        if (off >= dataStart && off < dataEnd) {
            off_t dataOff = off - dataStart;
            ssize_t readSize = dataSize - dataOff;
            uint8_t buf[readSize];

            ASSERT_EQ(readSize, TEMP_FAILURE_RETRY(read(fd, buf, readSize)));
            EXPECT_EQ(0, memcmp(buf, data + dataOff, readSize));
        }
    }

    EXPECT_EQ(0, munmap(region, dataSize));
}

TEST(AshmemTest, ProtTest) {
TEST(AshmemTest, ProtTest) {
    unique_fd fd;
    unique_fd fd;
    constexpr size_t size = PAGE_SIZE;
    constexpr size_t size = PAGE_SIZE;