1 // Copyright 2022 The Abseil Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/crc/internal/crc_memcpy.h"
16 
17 #include <cstddef>
18 #include <cstdint>
19 #include <cstring>
20 #include <limits>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 
25 #include "gtest/gtest.h"
26 #include "absl/crc/crc32c.h"
27 #include "absl/memory/memory.h"
28 #include "absl/random/distributions.h"
29 #include "absl/random/random.h"
30 #include "absl/strings/str_cat.h"
31 #include "absl/strings/string_view.h"
32 
33 namespace {
34 
35 enum CrcEngine {
36   X86 = 0,
37   NONTEMPORAL = 1,
38   FALLBACK = 2,
39 };
40 
41 // Correctness tests:
42 // - Every source/destination byte alignment 0-15, every size 0-511 bytes
43 // - Arbitrarily aligned source, large size
44 template <size_t max_size>
45 class CrcMemcpyTest : public testing::Test {
46  protected:
CrcMemcpyTest()47   CrcMemcpyTest() {
48     source_ = std::make_unique<char[]>(kSize);
49     destination_ = std::make_unique<char[]>(kSize);
50   }
51   static constexpr size_t kAlignment = 16;
52   static constexpr size_t kMaxCopySize = max_size;
53   static constexpr size_t kSize = kAlignment + kMaxCopySize;
54   std::unique_ptr<char[]> source_;
55   std::unique_ptr<char[]> destination_;
56 
57   absl::BitGen gen_;
58 };
59 
60 // Small test is slightly larger 4096 bytes to allow coverage of the "large"
61 // copy function.  The minimum size to exercise all code paths in that function
62 // would be around 256 consecutive tests (getting every possible tail value
63 // and 0-2 small copy loops after the main block), so testing from 4096-4500
64 // will cover all of those code paths multiple times.
65 typedef CrcMemcpyTest<4500> CrcSmallTest;
66 typedef CrcMemcpyTest<(1 << 24)> CrcLargeTest;
67 // Parametrize the small test so that it can be done with all configurations.
68 template <typename ParamsT>
69 class x86ParamTestTemplate : public CrcSmallTest,
70                              public ::testing::WithParamInterface<ParamsT> {
71  protected:
x86ParamTestTemplate()72   x86ParamTestTemplate() {
73     if (GetParam().crc_engine_selector == FALLBACK) {
74       engine_ = std::make_unique<absl::crc_internal::FallbackCrcMemcpyEngine>();
75     } else if (GetParam().crc_engine_selector == NONTEMPORAL) {
76       engine_ =
77           std::make_unique<absl::crc_internal::CrcNonTemporalMemcpyEngine>();
78     } else {
79       engine_ = absl::crc_internal::CrcMemcpy::GetTestEngine(
80           GetParam().vector_lanes, GetParam().integer_lanes);
81     }
82   }
83 
84   // Convenience method.
GetParam() const85   ParamsT GetParam() const {
86     return ::testing::WithParamInterface<ParamsT>::GetParam();
87   }
88 
89   std::unique_ptr<absl::crc_internal::CrcMemcpyEngine> engine_;
90 };
91 struct TestParams {
92   CrcEngine crc_engine_selector = X86;
93   int vector_lanes = 0;
94   int integer_lanes = 0;
95 };
96 using x86ParamTest = x86ParamTestTemplate<TestParams>;
97 // SmallCorrectness is designed to exercise every possible set of code paths
98 // in the memcpy code, not including the loop.
TEST_P(x86ParamTest,SmallCorrectnessCheckSourceAlignment)99 TEST_P(x86ParamTest, SmallCorrectnessCheckSourceAlignment) {
100   constexpr size_t kTestSizes[] = {0, 100, 255, 512, 1024, 4000, kMaxCopySize};
101 
102   for (size_t source_alignment = 0; source_alignment < kAlignment;
103        source_alignment++) {
104     for (auto size : kTestSizes) {
105       char* base_data = static_cast<char*>(source_.get()) + source_alignment;
106       for (size_t i = 0; i < size; i++) {
107         *(base_data + i) =
108             static_cast<char>(absl::Uniform<unsigned char>(gen_));
109       }
110       absl::crc32c_t initial_crc =
111           absl::crc32c_t{absl::Uniform<uint32_t>(gen_)};
112       absl::crc32c_t experiment_crc =
113           engine_->Compute(destination_.get(), source_.get() + source_alignment,
114                            size, initial_crc);
115       // Check the memory region to make sure it is the same
116       int mem_comparison =
117           memcmp(destination_.get(), source_.get() + source_alignment, size);
118       SCOPED_TRACE(absl::StrCat("Error in memcpy of size: ", size,
119                                 " with source alignment: ", source_alignment));
120       ASSERT_EQ(mem_comparison, 0);
121       absl::crc32c_t baseline_crc = absl::ExtendCrc32c(
122           initial_crc,
123           absl::string_view(
124               static_cast<char*>(source_.get()) + source_alignment, size));
125       ASSERT_EQ(baseline_crc, experiment_crc);
126     }
127   }
128 }
129 
TEST_P(x86ParamTest,SmallCorrectnessCheckDestAlignment)130 TEST_P(x86ParamTest, SmallCorrectnessCheckDestAlignment) {
131   constexpr size_t kTestSizes[] = {0, 100, 255, 512, 1024, 4000, kMaxCopySize};
132 
133   for (size_t dest_alignment = 0; dest_alignment < kAlignment;
134        dest_alignment++) {
135     for (auto size : kTestSizes) {
136       char* base_data = static_cast<char*>(source_.get());
137       for (size_t i = 0; i < size; i++) {
138         *(base_data + i) =
139             static_cast<char>(absl::Uniform<unsigned char>(gen_));
140       }
141       absl::crc32c_t initial_crc =
142           absl::crc32c_t{absl::Uniform<uint32_t>(gen_)};
143       absl::crc32c_t experiment_crc =
144           engine_->Compute(destination_.get() + dest_alignment, source_.get(),
145                            size, initial_crc);
146       // Check the memory region to make sure it is the same
147       int mem_comparison =
148           memcmp(destination_.get() + dest_alignment, source_.get(), size);
149       SCOPED_TRACE(absl::StrCat("Error in memcpy of size: ", size,
150                                 " with dest alignment: ", dest_alignment));
151       ASSERT_EQ(mem_comparison, 0);
152       absl::crc32c_t baseline_crc = absl::ExtendCrc32c(
153           initial_crc,
154           absl::string_view(static_cast<char*>(source_.get()), size));
155       ASSERT_EQ(baseline_crc, experiment_crc);
156     }
157   }
158 }
159 
160 INSTANTIATE_TEST_SUITE_P(x86ParamTest, x86ParamTest,
161                          ::testing::Values(
162                              // Tests for configurations that may occur in prod.
163                              TestParams{X86, 3, 0}, TestParams{X86, 1, 2},
164                              // Fallback test.
165                              TestParams{FALLBACK, 0, 0},
166                              // Non Temporal
167                              TestParams{NONTEMPORAL, 0, 0}));
168 
169 }  // namespace
170