15 #include <gtest/gtest.h>
23 const size_t addr = 0x1234'5678'9abc'0000ULL;
24 const uint64_t
value = 0xdeadbeefcafef00dULL;
26 *GetOrCreateShadowAddress<0>(
sm, addr) =
value;
27 EXPECT_EQ(
value, *GetOrCreateShadowAddress<0>(
sm, addr));
32 const size_t base = 0x1000'0000ULL;
33 for (
int i = 0; i < 4096; ++i) {
34 *GetOrCreateShadowAddress<0>(
sm, base + i * 8) = uint64_t{
static_cast<uint64_t
>(i)} * 7 + 3;
36 for (
int i = 0; i < 4096; ++i) {
37 EXPECT_EQ(uint64_t{
static_cast<uint64_t
>(i)} * 7 + 3,
38 *GetOrCreateShadowAddress<0>(
sm, base + i * 8))
45 const size_t addrs[] = {
52 const int n =
sizeof(addrs) /
sizeof(addrs[0]);
53 for (
int i = 0; i < n; ++i) {
54 *GetOrCreateShadowAddress<0>(
sm, addrs[i]) = 0xa5a5a5a5'00000000ULL | i;
56 for (
int i = 0; i < n; ++i) {
57 EXPECT_EQ(0xa5a5a5a5'00000000ULL | i, *GetOrCreateShadowAddress<0>(
sm, addrs[i]));
63 const size_t addr = 0xcafeb0ba'0000ULL;
64 *GetOrCreateShadowAddress<0>(
sm, addr) = 0x5a;
65 *GetOrCreateShadowAddress<1>(
sm, addr) = 0xdeadbeef;
66 EXPECT_EQ(0x5a, *GetOrCreateShadowAddress<0>(
sm, addr));
67 EXPECT_EQ(0xdeadbeefu, *GetOrCreateShadowAddress<1>(
sm, addr));
74 const size_t addr = 0x0f0f'0000'1234ULL;
75 const uint64_t
value = 0x1122334455667788ULL;
77 *GetOrCreateShadowAddress<0>(
sm, addr) =
value;
78 auto& page =
sm.GetOrCreateShadowBaseAddress(addr);
84 const size_t addr = 0x0e0e'ffff'5678ULL;
85 const uint64_t
value = 0x8899aabbccddeeffULL;
87 auto& page =
sm.GetOrCreateShadowBaseAddress(addr);
89 EXPECT_EQ(
value, *GetOrCreateShadowAddress<0>(
sm, addr));
94 const size_t addr = 0x2222'3333'4444ULL;
95 uint64_t* p1 = GetOrCreateShadowAddress<0>(
sm, addr);
96 uint64_t* p2 = GetOrCreateShadowAddress<0>(
sm, addr);
102 const size_t base = 0x8000'0000ULL;
103 for (
int i = 0; i < 256; ++i) {
104 *GetOrCreateShadowAddress<0>(
sm, base + i * 8) = 0xf00d0000ULL + i;
106 for (
int i = 0; i < 256; ++i) {
107 EXPECT_EQ(0xf00d0000ULL + i, *GetOrCreateShadowAddress<0>(
sm, base + i * 8));
113 const size_t addr = 0x1234'5678'9abc'0000ULL;
114 const uint64_t
value = 0xdeadbeefcafef00dULL;
115 *GetOrCreateShadowAddress<0>(
sm, addr) =
value;
116 EXPECT_EQ(
value, *GetOrCreateShadowAddress<0>(
sm, addr));
121 const size_t addr = 0x0f0f'0000'1234ULL;
122 const uint64_t
value = 0x1122334455667788ULL;
123 *GetOrCreateShadowAddress<0>(
sm, addr) =
value;
124 auto& page =
sm.GetOrCreateShadowBaseAddress(addr);
130 const size_t addr = 0xcafeb0ba'0000ULL;
131 *GetOrCreateShadowAddress<0>(
sm, addr) = 0x5a;
132 *GetOrCreateShadowAddress<1>(
sm, addr) = 0xdeadbeef;
133 EXPECT_EQ(0x5a, *GetOrCreateShadowAddress<0>(
sm, addr));
134 EXPECT_EQ(0xdeadbeefu, *GetOrCreateShadowAddress<1>(
sm, addr));
142 const int nthreads = 8;
143 const int per_thread = 1024;
144 const size_t stride = 0x100'0000ULL;
146 auto writer = [&](
int tid) {
147 for (
int i = 0; i < per_thread; ++i) {
148 size_t addr = (
size_t)tid * stride + (
size_t)i * 8;
149 *GetOrCreateShadowAddress<0>(
sm, addr) = (uint64_t)tid * 1000000ULL + i;
152 std::vector<std::thread> ts;
153 ts.reserve(nthreads);
154 for (
int t = 0; t < nthreads; ++t) ts.emplace_back(writer, t);
155 for (
auto& th : ts) th.join();
157 std::atomic<int> mismatches{0};
158 auto reader = [&](
int tid) {
159 for (
int i = 0; i < per_thread; ++i) {
160 size_t addr = (
size_t)tid * stride + (
size_t)i * 8;
161 if (*GetOrCreateShadowAddress<0>(
sm, addr) != (uint64_t)tid * 1000000ULL + i) {
162 mismatches.fetch_add(1);
167 for (
int t = 0; t < nthreads; ++t) ts.emplace_back(reader, t);
168 for (
auto& th : ts) th.join();
170 EXPECT_EQ(0, mismatches.load());
175 struct FakeHandle { uint8_t type; uint32_t path; };
177 const size_t base = 0x7ff0'0000'0000ULL;
179 for (
int i = 0; i < n; ++i) {
180 FakeHandle* slot = GetOrCreateShadowAddress<0>(
sm, base + i);
182 slot->path = 0xabc00000u + (uint32_t)i;
184 for (
int i = 0; i < n; ++i) {
185 FakeHandle
h = *GetOrCreateShadowAddress<0>(
sm, base + i);
186 EXPECT_EQ(2, (
int)
h.type);
187 EXPECT_EQ(0xabc00000u + (uint32_t)i,
h.path);
static ConcurrentShadowMemory< DataHandle_t > sm
TEST(ConcurrentShadowMemory, DataHandleShape)
uint8_t value[MAX_WRITE_OP_LENGTH]
#define PAGE_OFFSET(addr)