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