//===-- Unittests for madvise ---------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "src/errno/libc_errno.h" #include "src/sys/mman/madvise.h" #include "src/sys/mman/mmap.h" #include "src/sys/mman/munmap.h" #include "test/UnitTest/ErrnoSetterMatcher.h" #include "test/UnitTest/Test.h" #include using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; TEST(LlvmLibcMadviseTest, NoError) { size_t alloc_size = 128; LIBC_NAMESPACE::libc_errno = 0; void *addr = LIBC_NAMESPACE::mmap(nullptr, alloc_size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); ASSERT_ERRNO_SUCCESS(); EXPECT_NE(addr, MAP_FAILED); EXPECT_THAT(LIBC_NAMESPACE::madvise(addr, alloc_size, MADV_RANDOM), Succeeds()); int *array = reinterpret_cast(addr); // Reading from the memory should not crash the test. // Since we used the MAP_ANONYMOUS flag, the contents of the newly // allocated memory should be initialized to zero. EXPECT_EQ(array[0], 0); EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, alloc_size), Succeeds()); } TEST(LlvmLibcMadviseTest, Error_BadPtr) { LIBC_NAMESPACE::libc_errno = 0; EXPECT_THAT(LIBC_NAMESPACE::madvise(nullptr, 8, MADV_SEQUENTIAL), Fails(ENOMEM)); }