1 //===----------------------- Unittests for memcpy -------------------------===// 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 "utils/CPP/ArrayRef.h" 10 #include "utils/UnitTest/Test.h" 11 #include "src/string/memcpy.h" 12 13 using __llvm_libc::cpp::Array; 14 using __llvm_libc::cpp::ArrayRef; 15 using __llvm_libc::cpp::MutableArrayRef; 16 using Data = Array<char, 2048>; 17 18 static const ArrayRef<char> kNumbers("0123456789", 10); 19 static const ArrayRef<char> kDeadcode("DEADC0DE", 8); 20 21 // Returns a Data object filled with a repetition of `filler`. 22 Data getData(ArrayRef<char> filler) { 23 Data out; 24 for (size_t i = 0; i < out.size(); ++i) 25 out[i] = filler[i % filler.size()]; 26 return out; 27 } 28 29 TEST(MemcpyTest, Thorough) { 30 const Data groundtruth = getData(kNumbers); 31 const Data dirty = getData(kDeadcode); 32 for (size_t count = 0; count < 1024; ++count) { 33 for (size_t align = 0; align < 64; ++align) { 34 auto buffer = dirty; 35 const char *const src = groundtruth.data(); 36 char *const dst = &buffer[align]; 37 __llvm_libc::memcpy(dst, src, count); 38 // Everything before copy is untouched. 39 for (size_t i = 0; i < align; ++i) 40 ASSERT_EQ(buffer[i], dirty[i]); 41 // Everything in between is copied. 42 for (size_t i = 0; i < count; ++i) 43 ASSERT_EQ(buffer[align + i], groundtruth[i]); 44 // Everything after copy is untouched. 45 for (size_t i = align + count; i < dirty.size(); ++i) 46 ASSERT_EQ(buffer[i], dirty[i]); 47 } 48 } 49 } 50 51 // FIXME: Add tests with reads and writes on the boundary of a read/write 52 // protected page to check we're not reading nor writing prior/past the allowed 53 // regions. 54