1 //===-- Utils to test conformance of mem functions ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LIBC_TEST_SRC_STRING_MEMORY_UTILS_MEMORY_CHECK_UTILS_H
10 #define LIBC_TEST_SRC_STRING_MEMORY_UTILS_MEMORY_CHECK_UTILS_H
12 #include "src/__support/CPP/span.h"
13 #include "src/__support/libc_assert.h" // LIBC_ASSERT
14 #include "src/__support/macros/sanitizer.h"
15 #include "src/string/memory_utils/utils.h"
16 #include <stddef.h> // size_t
17 #include <stdint.h> // uintxx_t
18 #include <stdlib.h> // malloc/free
20 namespace LIBC_NAMESPACE
{
22 // Simple structure to allocate a buffer of a particular size.
23 // When ASAN is present it also poisons the whole memory.
24 // This is a utility class to be used by Buffer below, do not use directly.
25 struct PoisonedBuffer
{
26 PoisonedBuffer(size_t size
) : ptr((char *)malloc(size
)) {
27 ASAN_POISON_MEMORY_REGION(ptr
, size
);
29 ~PoisonedBuffer() { free(ptr
); }
35 // Simple structure to allocate a buffer (aligned or not) of a particular size.
36 // It is backed by a wider buffer that is marked poisoned when ASAN is present.
37 // The requested region is unpoisoned, this allows catching out of bounds
39 enum class Aligned
: bool { NO
= false, YES
= true };
40 struct Buffer
: private PoisonedBuffer
{
41 static constexpr size_t kAlign
= 64;
42 static constexpr size_t kLeeway
= 2 * kAlign
;
43 Buffer(size_t size
, Aligned aligned
= Aligned::YES
)
44 : PoisonedBuffer(size
+ kLeeway
), size(size
) {
46 offset_ptr
+= distance_to_next_aligned
<kAlign
>(ptr
);
47 if (aligned
== Aligned::NO
)
49 ASAN_UNPOISON_MEMORY_REGION(offset_ptr
, size
);
51 cpp::span
<char> span() { return cpp::span
<char>(offset_ptr
, size
); }
55 char *offset_ptr
= nullptr;
58 inline char GetRandomChar() {
59 static constexpr const uint64_t a
= 1103515245;
60 static constexpr const uint64_t c
= 12345;
61 static constexpr const uint64_t m
= 1ULL << 31;
62 static uint64_t seed
= 123456789;
63 seed
= (a
* seed
+ c
) % m
;
64 return static_cast<char>(seed
);
67 // Randomize the content of the buffer.
68 inline void Randomize(cpp::span
<char> buffer
) {
69 for (auto ¤t
: buffer
)
70 current
= GetRandomChar();
73 // Copy one span to another.
74 inline void ReferenceCopy(cpp::span
<char> dst
, const cpp::span
<char> src
) {
75 for (size_t i
= 0; i
< dst
.size(); ++i
)
79 inline bool IsEqual(const cpp::span
<char> a
, const cpp::span
<char> b
) {
80 LIBC_ASSERT(a
.size() == b
.size());
81 for (size_t i
= 0; i
< a
.size(); ++i
)
87 // Checks that FnImpl implements the memcpy semantic.
88 template <auto FnImpl
>
89 inline bool CheckMemcpy(cpp::span
<char> dst
, cpp::span
<char> src
, size_t size
) {
91 FnImpl(dst
, src
, size
);
92 return IsEqual(dst
, src
);
95 // Checks that FnImpl implements the memset semantic.
96 template <auto FnImpl
>
97 inline bool CheckMemset(cpp::span
<char> dst
, uint8_t value
, size_t size
) {
99 FnImpl(dst
, value
, size
);
101 if (c
!= (char)value
)
106 // Checks that FnImpl implements the bcmp semantic.
107 template <auto FnImpl
>
108 inline bool CheckBcmp(cpp::span
<char> span1
, cpp::span
<char> span2
,
110 ReferenceCopy(span2
, span1
);
112 if (int cmp
= FnImpl(span1
, span2
, size
); cmp
!= 0)
114 // Compare not equal if any byte differs
115 for (size_t i
= 0; i
< size
; ++i
) {
117 if (int cmp
= FnImpl(span1
, span2
, size
); cmp
== 0)
119 if (int cmp
= FnImpl(span2
, span1
, size
); cmp
== 0)
126 // Checks that FnImpl implements the memcmp semantic.
127 template <auto FnImpl
>
128 inline bool CheckMemcmp(cpp::span
<char> span1
, cpp::span
<char> span2
,
130 ReferenceCopy(span2
, span1
);
132 if (int cmp
= FnImpl(span1
, span2
, size
); cmp
!= 0)
134 // Compare not equal if any byte differs
135 for (size_t i
= 0; i
< size
; ++i
) {
137 int ground_truth
= __builtin_memcmp(span1
.data(), span2
.data(), size
);
138 if (ground_truth
> 0) {
139 if (int cmp
= FnImpl(span1
, span2
, size
); cmp
<= 0)
141 if (int cmp
= FnImpl(span2
, span1
, size
); cmp
>= 0)
144 if (int cmp
= FnImpl(span1
, span2
, size
); cmp
>= 0)
146 if (int cmp
= FnImpl(span2
, span1
, size
); cmp
<= 0)
154 inline uint16_t Checksum(cpp::span
<char> dst
) {
155 // We use Fletcher16 as it is trivial to implement.
159 sum1
= (sum1
+ c
) % 255U;
160 sum2
= (sum2
+ sum1
) % 255U;
162 return static_cast<uint16_t>((sum2
<< 8) | sum1
);
165 template <auto FnImpl
>
166 inline bool CheckMemmove(cpp::span
<char> dst
, cpp::span
<char> src
) {
167 LIBC_ASSERT(dst
.size() == src
.size());
168 // Memmove can override the src buffer. Technically we should save it into a
169 // temporary buffer so we can check that 'dst' is equal to what 'src' was
170 // before we called the function. To save on allocation and copy we use a
172 const auto src_checksum
= Checksum(src
);
173 FnImpl(dst
, src
, dst
.size());
174 return Checksum(dst
) == src_checksum
;
177 // Checks that FnImpl implements the memmove semantic.
178 // - Buffer size should be greater than 2 * size + 1.
179 // - Overlap refers to the number of bytes in common between the two buffers:
180 // - Negative means buffers are disjoint
181 // - zero mean they overlap exactly
182 // - Caller is responsible for randomizing the buffer.
183 template <auto FnImpl
>
184 inline bool CheckMemmove(cpp::span
<char> buffer
, size_t size
, int overlap
) {
185 LIBC_ASSERT(buffer
.size() > (2 * size
+ 1));
186 const size_t half_size
= buffer
.size() / 2;
187 LIBC_ASSERT((size_t)(overlap
>= 0 ? overlap
: -overlap
) < half_size
);
188 cpp::span
<char> head
= buffer
.first(half_size
+ overlap
).last(size
);
189 cpp::span
<char> tail
= buffer
.last(half_size
).first(size
);
190 LIBC_ASSERT(head
.size() == size
);
191 LIBC_ASSERT(tail
.size() == size
);
193 if (!CheckMemmove
<FnImpl
>(head
, tail
))
196 if (!CheckMemmove
<FnImpl
>(tail
, head
))
201 } // namespace LIBC_NAMESPACE
203 #endif // LIBC_TEST_SRC_STRING_MEMORY_UTILS_MEMORY_CHECK_UTILS_H