• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "memory_utils/memory_check_utils.h"
10 #include "src/__support/macros/properties/os.h" // LIBC_TARGET_OS_IS_LINUX
11 #include "src/string/memcpy.h"
12 #include "test/UnitTest/Test.h"
13 
14 #if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
15 #include "memory_utils/protected_pages.h"
16 #endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
17 
18 namespace LIBC_NAMESPACE {
19 
20 // Adapt CheckMemcpy signature to memcpy.
Adaptor(cpp::span<char> dst,cpp::span<char> src,size_t size)21 static inline void Adaptor(cpp::span<char> dst, cpp::span<char> src,
22                            size_t size) {
23   LIBC_NAMESPACE::memcpy(dst.begin(), src.begin(), size);
24 }
25 
TEST(LlvmLibcMemcpyTest,SizeSweep)26 TEST(LlvmLibcMemcpyTest, SizeSweep) {
27   static constexpr size_t kMaxSize = 400;
28   Buffer SrcBuffer(kMaxSize);
29   Buffer DstBuffer(kMaxSize);
30   Randomize(SrcBuffer.span());
31   for (size_t size = 0; size < kMaxSize; ++size) {
32     auto src = SrcBuffer.span().subspan(0, size);
33     auto dst = DstBuffer.span().subspan(0, size);
34     ASSERT_TRUE(CheckMemcpy<Adaptor>(dst, src, size));
35   }
36 }
37 
38 #if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
39 
TEST(LlvmLibcMemcpyTest,CheckAccess)40 TEST(LlvmLibcMemcpyTest, CheckAccess) {
41   static constexpr size_t MAX_SIZE = 1024;
42   LIBC_ASSERT(MAX_SIZE < GetPageSize());
43   ProtectedPages pages;
44   const Page write_buffer = pages.GetPageA().WithAccess(PROT_WRITE);
45   const Page read_buffer = [&]() {
46     // We fetch page B in write mode.
47     auto page = pages.GetPageB().WithAccess(PROT_WRITE);
48     // And fill it with random numbers.
49     for (size_t i = 0; i < page.page_size; ++i)
50       page.page_ptr[i] = rand();
51     // Then return it in read mode.
52     return page.WithAccess(PROT_READ);
53   }();
54   for (size_t size = 0; size < MAX_SIZE; ++size) {
55     // We cross-check the function with two sources and two destinations.
56     //  - The first of them (bottom) is always page aligned and faults when
57     //    accessing bytes before it.
58     //  - The second one (top) is not necessarily aligned and faults when
59     //    accessing bytes after it.
60     const uint8_t *sources[2] = {read_buffer.bottom(size),
61                                  read_buffer.top(size)};
62     uint8_t *destinations[2] = {write_buffer.bottom(size),
63                                 write_buffer.top(size)};
64     for (const uint8_t *src : sources) {
65       for (uint8_t *dst : destinations) {
66         LIBC_NAMESPACE::memcpy(dst, src, size);
67       }
68     }
69   }
70 }
71 
72 #endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
73 
74 } // namespace LIBC_NAMESPACE
75