xref: /llvm-project/libc/test/src/string/memset_test.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
16ca54e01SGuillaume Chatelet //===-- Unittests for memset ----------------------------------------------===//
26ca54e01SGuillaume Chatelet //
36ca54e01SGuillaume Chatelet // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46ca54e01SGuillaume Chatelet // See https://llvm.org/LICENSE.txt for license information.
56ca54e01SGuillaume Chatelet // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66ca54e01SGuillaume Chatelet //
76ca54e01SGuillaume Chatelet //===----------------------------------------------------------------------===//
86ca54e01SGuillaume Chatelet 
93635195eSGuillaume Chatelet #include "memory_utils/memory_check_utils.h"
10*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
11292b300cSGuillaume Chatelet #include "src/__support/macros/properties/os.h" // LIBC_TARGET_OS_IS_LINUX
126ca54e01SGuillaume Chatelet #include "src/string/memset.h"
13af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
146ca54e01SGuillaume Chatelet 
15292b300cSGuillaume Chatelet #if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
16292b300cSGuillaume Chatelet #include "memory_utils/protected_pages.h"
17292b300cSGuillaume Chatelet #endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
18292b300cSGuillaume Chatelet 
19*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
206ca54e01SGuillaume Chatelet 
21298843cdSGuillaume Chatelet // Adapt CheckMemset signature to memset.
22298843cdSGuillaume Chatelet static inline void Adaptor(cpp::span<char> p1, uint8_t value, size_t size) {
23b6bc9d72SGuillaume Chatelet   LIBC_NAMESPACE::memset(p1.begin(), value, size);
246ca54e01SGuillaume Chatelet }
256ca54e01SGuillaume Chatelet 
263635195eSGuillaume Chatelet TEST(LlvmLibcMemsetTest, SizeSweep) {
27298843cdSGuillaume Chatelet   static constexpr size_t kMaxSize = 400;
283635195eSGuillaume Chatelet   Buffer DstBuffer(kMaxSize);
293635195eSGuillaume Chatelet   for (size_t size = 0; size < kMaxSize; ++size) {
303635195eSGuillaume Chatelet     const char value = size % 10;
313635195eSGuillaume Chatelet     auto dst = DstBuffer.span().subspan(0, size);
32298843cdSGuillaume Chatelet     ASSERT_TRUE((CheckMemset<Adaptor>(dst, value, size)));
336ca54e01SGuillaume Chatelet   }
346ca54e01SGuillaume Chatelet }
356ca54e01SGuillaume Chatelet 
36292b300cSGuillaume Chatelet #if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
37292b300cSGuillaume Chatelet 
38292b300cSGuillaume Chatelet TEST(LlvmLibcMemsetTest, CheckAccess) {
39292b300cSGuillaume Chatelet   static constexpr size_t MAX_SIZE = 1024;
40292b300cSGuillaume Chatelet   LIBC_ASSERT(MAX_SIZE < GetPageSize());
41292b300cSGuillaume Chatelet   ProtectedPages pages;
42292b300cSGuillaume Chatelet   const Page write_buffer = pages.GetPageA().WithAccess(PROT_WRITE);
43292b300cSGuillaume Chatelet   const cpp::array<int, 2> fill_chars = {0, 0x7F};
44292b300cSGuillaume Chatelet   for (int fill_char : fill_chars) {
45292b300cSGuillaume Chatelet     for (size_t size = 0; size < MAX_SIZE; ++size) {
46292b300cSGuillaume Chatelet       // We cross-check the function with two destinations.
47292b300cSGuillaume Chatelet       // - The first of them (bottom) is always page aligned and faults when
48292b300cSGuillaume Chatelet       //   accessing bytes before it.
49292b300cSGuillaume Chatelet       // - The second one (top) is not necessarily aligned and faults when
50292b300cSGuillaume Chatelet       //   accessing bytes after it.
51292b300cSGuillaume Chatelet       uint8_t *destinations[2] = {write_buffer.bottom(size),
52292b300cSGuillaume Chatelet                                   write_buffer.top(size)};
53292b300cSGuillaume Chatelet       for (uint8_t *dst : destinations) {
54292b300cSGuillaume Chatelet         LIBC_NAMESPACE::memset(dst, fill_char, size);
55292b300cSGuillaume Chatelet       }
56292b300cSGuillaume Chatelet     }
57292b300cSGuillaume Chatelet   }
58292b300cSGuillaume Chatelet }
59292b300cSGuillaume Chatelet 
60292b300cSGuillaume Chatelet #endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
61292b300cSGuillaume Chatelet 
62*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
63