#include "memory_utils/memory_check_utils.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/os.h"
#include "src/string/memcpy.h"
#include "test/UnitTest/Test.h"
#if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
#include "memory_utils/protected_pages.h"
#endif
namespace LIBC_NAMESPACE_DECL {
static inline void Adaptor(cpp::span<char> dst, cpp::span<char> src,
size_t size) {
LIBC_NAMESPACE::memcpy(dst.begin(), src.begin(), size);
}
TEST(LlvmLibcMemcpyTest, SizeSweep) {
static constexpr size_t kMaxSize = 400;
Buffer SrcBuffer(kMaxSize);
Buffer DstBuffer(kMaxSize);
Randomize(SrcBuffer.span());
for (size_t size = 0; size < kMaxSize; ++size) {
auto src = SrcBuffer.span().subspan(0, size);
auto dst = DstBuffer.span().subspan(0, size);
ASSERT_TRUE(CheckMemcpy<Adaptor>(dst, src, size));
}
}
#if !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
TEST(LlvmLibcMemcpyTest, 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 Page read_buffer = [&]() {
auto page = pages.GetPageB().WithAccess(PROT_WRITE);
for (size_t i = 0; i < page.page_size; ++i)
page.page_ptr[i] = rand();
return page.WithAccess(PROT_READ);
}();
for (size_t size = 0; size < MAX_SIZE; ++size) {
const uint8_t *sources[2] = {read_buffer.bottom(size),
read_buffer.top(size)};
uint8_t *destinations[2] = {write_buffer.bottom(size),
write_buffer.top(size)};
for (const uint8_t *src : sources) {
for (uint8_t *dst : destinations) {
LIBC_NAMESPACE::memcpy(dst, src, size);
}
}
}
}
#endif
}