1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "gtest/gtest.h"
18
19 #include <thread>
20
21 #include "berberis/guest_state/guest_addr.h"
22 #include "berberis/runtime_primitives/table_of_tables.h"
23
24 namespace {
25
TEST(TableOfTables,Smoke)26 TEST(TableOfTables, Smoke) {
27 berberis::TableOfTables<berberis::GuestAddr, uintptr_t> tot(42);
28 ASSERT_EQ(42U, tot.Get(25));
29 ASSERT_EQ(1729U, *tot.Put(25, 1729));
30 ASSERT_EQ(1729U, tot.Get(25));
31 ASSERT_EQ(42U, tot.Get(255));
32 ASSERT_EQ(42U, tot.Get((25 << 16) | 25));
33 }
34
TEST(TableOfTables,GetPointer)35 TEST(TableOfTables, GetPointer) {
36 berberis::TableOfTables<berberis::GuestAddr, uintptr_t> tot(42);
37 ASSERT_EQ(42U, tot.Get(25));
38 auto* addr = tot.GetPointer(25);
39 ASSERT_EQ(42U, *addr);
40 ASSERT_EQ(42U, tot.Get(25));
41 ASSERT_EQ(1729U, *tot.Put(25, 1729));
42 ASSERT_EQ(1729U, *addr);
43 ASSERT_EQ(1729U, tot.Get(25));
44 ASSERT_EQ(42U, tot.Get(255));
45 }
46
TEST(TableOfTables,Stress)47 TEST(TableOfTables, Stress) {
48 berberis::TableOfTables<berberis::GuestAddr, uintptr_t> tot(42);
49
50 std::thread threads[64];
51
52 for (size_t i = 0; i < 64; ++i) {
53 uint32_t base_num = (i % 2 == 0) ? 0 : 65520;
54 threads[i] = std::thread(
55 [](berberis::TableOfTables<berberis::GuestAddr, uintptr_t>* tot, uint32_t base) {
56 for (uint32_t page_num = 0; page_num < 4098; ++page_num) {
57 uint32_t page = (page_num << 17);
58 ASSERT_EQ(42U, tot->Get(page | (base + 4)));
59 auto* addr = tot->GetPointer(page | (base + 5));
60 ASSERT_EQ(42U, tot->Get(page | (base + 4)));
61 ASSERT_EQ(1729U, *tot->Put(page | (base + 5), 1729));
62 ASSERT_EQ(1U, *tot->Put(page | (base + 6), 1));
63 ASSERT_EQ(42U, tot->Get(page | (base + 4)));
64 ASSERT_EQ(1729U, *addr);
65 }
66 },
67 &tot,
68 base_num);
69 }
70
71 for (auto& thread : threads) {
72 thread.join();
73 }
74
75 for (uint32_t page_num = 0; page_num < 4098; ++page_num) {
76 uint32_t page = (page_num << 17);
77 ASSERT_EQ(1729U, tot.Get(page | 5));
78 ASSERT_EQ(1U, tot.Get(page | 6));
79 ASSERT_EQ(42U, tot.Get(page | 4));
80 ASSERT_EQ(42U, tot.Get(page | 255));
81
82 ASSERT_EQ(1729U, tot.Get(page | 65525));
83 ASSERT_EQ(1U, tot.Get(page | 65526));
84 ASSERT_EQ(42U, tot.Get(page | 65524));
85 ASSERT_EQ(42U, tot.Get(page | 65535));
86 }
87 }
88
TEST(TableOfTables_DeathTest,InvalidAddress)89 TEST(TableOfTables_DeathTest, InvalidAddress) {
90 #ifdef BERBERIS_GUEST_LP64
91 berberis::TableOfTables<berberis::GuestAddr, uintptr_t> tot(42);
92
93 // Try an address with its top 16 bits nonzero.
94 EXPECT_DEATH((void)tot.Get(0xdeadbeef12345678ULL), "");
95 #endif
96 }
97
98 } // namespace
99