85 lines · cpp
1//===-- Unittests for memcpy ----------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/signal_macros.h"10#include "memory_utils/memory_check_utils.h"11#include "src/__support/macros/config.h"12#include "src/__support/macros/properties/os.h" // LIBC_TARGET_OS_IS_LINUX13#include "src/string/memcpy.h"14#include "test/UnitTest/Test.h"15 16#if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)17#include "memory_utils/protected_pages.h"18#endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)19 20namespace LIBC_NAMESPACE_DECL {21 22// Adapt CheckMemcpy signature to memcpy.23static inline void Adaptor(cpp::span<char> dst, cpp::span<char> src,24 size_t size) {25 LIBC_NAMESPACE::memcpy(dst.begin(), src.begin(), size);26}27 28TEST(LlvmLibcMemcpyTest, SizeSweep) {29 static constexpr size_t kMaxSize = 400;30 Buffer SrcBuffer(kMaxSize);31 Buffer DstBuffer(kMaxSize);32 Randomize(SrcBuffer.span());33 for (size_t size = 0; size < kMaxSize; ++size) {34 auto src = SrcBuffer.span().subspan(0, size);35 auto dst = DstBuffer.span().subspan(0, size);36 ASSERT_TRUE(CheckMemcpy<Adaptor>(dst, src, size));37 }38}39 40#if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)41 42TEST(LlvmLibcMemcpyTest, CheckAccess) {43 static constexpr size_t MAX_SIZE = 1024;44 LIBC_ASSERT(MAX_SIZE < GetPageSize());45 ProtectedPages pages;46 const Page write_buffer = pages.GetPageA().WithAccess(PROT_WRITE);47 const Page read_buffer = [&]() {48 // We fetch page B in write mode.49 auto page = pages.GetPageB().WithAccess(PROT_WRITE);50 // And fill it with random numbers.51 for (size_t i = 0; i < page.page_size; ++i)52 page.page_ptr[i] = static_cast<uint8_t>(rand());53 // Then return it in read mode.54 return page.WithAccess(PROT_READ);55 }();56 for (size_t size = 0; size < MAX_SIZE; ++size) {57 // We cross-check the function with two sources and two destinations.58 // - The first of them (bottom) is always page aligned and faults when59 // accessing bytes before it.60 // - The second one (top) is not necessarily aligned and faults when61 // accessing bytes after it.62 const uint8_t *sources[2] = {read_buffer.bottom(size),63 read_buffer.top(size)};64 uint8_t *destinations[2] = {write_buffer.bottom(size),65 write_buffer.top(size)};66 for (const uint8_t *src : sources) {67 for (uint8_t *dst : destinations) {68 LIBC_NAMESPACE::memcpy(dst, src, size);69 }70 }71 }72}73 74#endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)75 76#if defined(LIBC_ADD_NULL_CHECKS)77 78TEST(LlvmLibcMemcpyTest, CrashOnNullPtr) {79 ASSERT_DEATH([]() { LIBC_NAMESPACE::memcpy(nullptr, nullptr, 1); },80 WITH_SIGNAL(-1));81}82#endif // defined(LIBC_ADD_NULL_CHECKS)83 84} // namespace LIBC_NAMESPACE_DECL85