aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/string/memset_test.cpp
blob: 8268d31e6ac7f75baccec2fe349a2c5716bd61d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//===-- Unittests for memset ----------------------------------------------===//
//
// 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 "hdr/signal_macros.h"
#include "memory_utils/memory_check_utils.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/os.h" // LIBC_TARGET_OS_IS_LINUX
#include "src/string/memset.h"
#include "test/UnitTest/Test.h"

#if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
#include "memory_utils/protected_pages.h"
#endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)

namespace LIBC_NAMESPACE_DECL {

// Adapt CheckMemset signature to memset.
static inline void Adaptor(cpp::span<char> p1, uint8_t value, size_t size) {
  LIBC_NAMESPACE::memset(p1.begin(), value, size);
}

TEST(LlvmLibcMemsetTest, SizeSweep) {
  static constexpr size_t kMaxSize = 400;
  Buffer DstBuffer(kMaxSize);
  for (size_t size = 0; size < kMaxSize; ++size) {
    const uint8_t value = size % 10;
    auto dst = DstBuffer.span().subspan(0, size);
    ASSERT_TRUE((CheckMemset<Adaptor>(dst, value, size)));
  }
}

#if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)

TEST(LlvmLibcMemsetTest, CheckAccess) {
  static constexpr size_t MAX_SIZE = 1024;
  LIBC_ASSERT(MAX_SIZE < GetPageSize());
  ProtectedPages pages;
  const Page write_buffer = pages.GetPageA().WithAccess(PROT_WRITE);
  const cpp::array<int, 2> fill_chars = {0, 0x7F};
  for (int fill_char : fill_chars) {
    for (size_t size = 0; size < MAX_SIZE; ++size) {
      // We cross-check the function with two destinations.
      // - The first of them (bottom) is always page aligned and faults when
      //   accessing bytes before it.
      // - The second one (top) is not necessarily aligned and faults when
      //   accessing bytes after it.
      uint8_t *destinations[2] = {write_buffer.bottom(size),
                                  write_buffer.top(size)};
      for (uint8_t *dst : destinations) {
        LIBC_NAMESPACE::memset(dst, fill_char, size);
      }
    }
  }
}

#endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)

#if defined(LIBC_ADD_NULL_CHECKS)

TEST(LlvmLibcMemsetTest, CrashOnNullPtr) {
  ASSERT_DEATH([]() { LIBC_NAMESPACE::memset(nullptr, 0, 1); },
               WITH_SIGNAL(-1));
}

#endif // defined(LIBC_ADD_NULL_CHECKS)

} // namespace LIBC_NAMESPACE_DECL