| 1 | //===-- Unittests for memset ----------------------------------------------===// |
| 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 "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_LINUX |
| 13 | #include "src/string/memset.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 | |
| 20 | namespace LIBC_NAMESPACE_DECL { |
| 21 | |
| 22 | // Adapt CheckMemset signature to memset. |
| 23 | static inline void Adaptor(cpp::span<char> p1, uint8_t value, size_t size) { |
| 24 | LIBC_NAMESPACE::memset(p1.begin(), value, size); |
| 25 | } |
| 26 | |
| 27 | TEST(LlvmLibcMemsetTest, SizeSweep) { |
| 28 | static constexpr size_t kMaxSize = 400; |
| 29 | Buffer DstBuffer(kMaxSize); |
| 30 | for (size_t size = 0; size < kMaxSize; ++size) { |
| 31 | const uint8_t value = size % 10; |
| 32 | auto dst = DstBuffer.span().subspan(0, size); |
| 33 | ASSERT_TRUE((CheckMemset<Adaptor>(dst, value, size))); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | #if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX) |
| 38 | |
| 39 | TEST(LlvmLibcMemsetTest, CheckAccess) { |
| 40 | static constexpr size_t MAX_SIZE = 1024; |
| 41 | LIBC_ASSERT(MAX_SIZE < GetPageSize()); |
| 42 | ProtectedPages pages; |
| 43 | const Page write_buffer = pages.GetPageA().WithAccess(PROT_WRITE); |
| 44 | const cpp::array<int, 2> fill_chars = {0, 0x7F}; |
| 45 | for (int fill_char : fill_chars) { |
| 46 | for (size_t size = 0; size < MAX_SIZE; ++size) { |
| 47 | // We cross-check the function with two destinations. |
| 48 | // - The first of them (bottom) is always page aligned and faults when |
| 49 | // accessing bytes before it. |
| 50 | // - The second one (top) is not necessarily aligned and faults when |
| 51 | // accessing bytes after it. |
| 52 | uint8_t *destinations[2] = {write_buffer.bottom(size), |
| 53 | write_buffer.top(size)}; |
| 54 | for (uint8_t *dst : destinations) { |
| 55 | LIBC_NAMESPACE::memset(dst, fill_char, size); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX) |
| 62 | |
| 63 | #if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
| 64 | |
| 65 | TEST(LlvmLibcMemsetTest, CrashOnNullPtr) { |
| 66 | ASSERT_DEATH([]() { LIBC_NAMESPACE::memset(nullptr, 0, 1); }, |
| 67 | WITH_SIGNAL(-1)); |
| 68 | } |
| 69 | |
| 70 | #endif // defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
| 71 | |
| 72 | } // namespace LIBC_NAMESPACE_DECL |
| 73 | |