• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
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  * http://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 
16 #include <ctime>
17 #include "utils/hash.h"
18 
19 #include "gtest/gtest.h"
20 #include "utils/logger.h"
21 #include "mem/mem.h"
22 #include "os/mem.h"
23 #include "utils/asan_interface.h"
24 #include <sys/mman.h>
25 
26 namespace panda {
27 
28 class HashTest : public testing::Test {
29 public:
HashTest()30     HashTest()
31     {
32 #ifdef PANDA_NIGHTLY_TEST_ON
33         seed_ = std::time(NULL);
34 #else
35         seed_ = 0xDEADBEEF;
36 #endif
37     }
~HashTest()38     ~HashTest() {}
39 
40 protected:
41     template <class T>
42     void OneObject32bitsHashTest() const;
43     template <class T>
44     void OneStringHashTest() const;
45     template <class T>
46     void StringMemHashTest() const;
47     template <class T>
48     void EndOfPageStringHashTest() const;
49     static constexpr size_t KEY40INBYTES = 5;
50     static constexpr size_t KEY32INBYTES = 4;
51     static constexpr size_t KEY8INBYTES = 1;
52 
53 #ifndef PAGE_SIZE
54     static constexpr size_t PAGE_SIZE = SIZE_1K * 4;
55 #endif
56 
57     unsigned seed_;
58 };
59 
60 template <class T>
OneObject32bitsHashTest() const61 void HashTest::OneObject32bitsHashTest() const
62 {
63     srand(seed_);
64 
65     uint32_t object32 = rand();
66     uint32_t first_hash = T::GetHash32(reinterpret_cast<uint8_t *>(&object32), KEY32INBYTES);
67     uint32_t second_hash = T::GetHash32(reinterpret_cast<uint8_t *>(&object32), KEY32INBYTES);
68     if (first_hash != second_hash) {
69         std::cout << "Failed 32bit key hash on seed = 0x" << std::hex << seed_ << std::endl;
70     }
71     ASSERT_EQ(first_hash, second_hash);
72 
73     uint8_t object8 = rand();
74     first_hash = T::GetHash32(reinterpret_cast<uint8_t *>(&object8), KEY8INBYTES);
75     second_hash = T::GetHash32(reinterpret_cast<uint8_t *>(&object8), KEY8INBYTES);
76     if (first_hash != second_hash) {
77         std::cout << "Failed 32bit key hash on seed = 0x" << std::hex << seed_ << std::endl;
78     }
79     ASSERT_EQ(first_hash, second_hash);
80 
81     // Set 64 bits value and use only 40 bits from it
82     uint64_t object40 = rand();
83     first_hash = T::GetHash32(reinterpret_cast<uint8_t *>(&object40), KEY40INBYTES);
84     second_hash = T::GetHash32(reinterpret_cast<uint8_t *>(&object40), KEY40INBYTES);
85     if (first_hash != second_hash) {
86         std::cout << "Failed 32bit key hash on seed = 0x" << std::hex << seed_ << std::endl;
87     }
88     ASSERT_EQ(first_hash, second_hash);
89 }
90 
91 template <class T>
OneStringHashTest() const92 void HashTest::OneStringHashTest() const
93 {
94     char string[] = "Over 1000!\0";
95     // Dummy check
96     if (sizeof(char) != sizeof(uint8_t)) {
97         return;
98     }
99     uint8_t *mutf8_string = reinterpret_cast<uint8_t *>(string);
100     uint32_t first_hash = T::GetHash32String(mutf8_string);
101     uint32_t second_hash = T::GetHash32String(mutf8_string);
102     ASSERT_EQ(first_hash, second_hash);
103 }
104 
105 template <class T>
StringMemHashTest() const106 void HashTest::StringMemHashTest() const
107 {
108     char string[] = "COULD YOU CREATE MORE COMPLEX TESTS,OK?\0";
109     size_t string_size = strlen(string);
110     uint8_t *mutf8_string = reinterpret_cast<uint8_t *>(string);
111     uint32_t second_hash = T::GetHash32(mutf8_string, string_size);
112     uint32_t first_hash = T::GetHash32String(mutf8_string);
113     ASSERT_EQ(first_hash, second_hash);
114 }
115 
116 template <class T>
EndOfPageStringHashTest() const117 void HashTest::EndOfPageStringHashTest() const
118 {
119     size_t string_size = 3;
120     constexpr size_t ALLOC_SIZE = PAGE_SIZE * 2;
121     void *mem = panda::os::mem::MapRWAnonymousRaw(ALLOC_SIZE);
122     ASAN_UNPOISON_MEMORY_REGION(mem, ALLOC_SIZE);
123     mprotect(reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(mem) + PAGE_SIZE), PAGE_SIZE, PROT_NONE);
124     char *string =
125         reinterpret_cast<char *>((reinterpret_cast<uintptr_t>(mem) + PAGE_SIZE) - sizeof(char) * string_size);
126     string[0] = 'O';
127     string[1] = 'K';
128     string[2U] = '\0';
129     uint8_t *mutf8_string = reinterpret_cast<uint8_t *>(string);
130     uint32_t second_hash = T::GetHash32(mutf8_string, string_size - 1);
131     uint32_t first_hash = T::GetHash32String(mutf8_string);
132     ASSERT_EQ(first_hash, second_hash);
133     auto res = panda::os::mem::UnmapRaw(mem, ALLOC_SIZE);
134     ASSERT_FALSE(res);
135 }
136 
137 // If we hash an object twice, it must return the same value
138 // Do it for 8 bits, 32 bits and 40 bits key.
TEST_F(HashTest,OneObjectHashTest)139 TEST_F(HashTest, OneObjectHashTest)
140 {
141     HashTest::OneObject32bitsHashTest<MurmurHash32<DEFAULT_SEED>>();
142 }
143 
144 // If we hash a string twice, it must return the same value
TEST_F(HashTest,OneStringHashTest)145 TEST_F(HashTest, OneStringHashTest)
146 {
147     HashTest::OneStringHashTest<MurmurHash32<DEFAULT_SEED>>();
148 }
149 
150 // If we hash a string without string method,
151 // we should get the same result as we use a pointer to string as a raw memory.
TEST_F(HashTest,StringMemHashTest)152 TEST_F(HashTest, StringMemHashTest)
153 {
154     HashTest::StringMemHashTest<MurmurHash32<DEFAULT_SEED>>();
155 }
156 
157 // Try to hash the string which is located at the end of allocated page.
158 // Check that we will not have SEGERROR here.
TEST_F(HashTest,EndOfPageStringHashTest)159 TEST_F(HashTest, EndOfPageStringHashTest)
160 {
161     HashTest::EndOfPageStringHashTest<MurmurHash32<DEFAULT_SEED>>();
162 }
163 
164 }  // namespace panda
165