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 "gmock/gmock.h"
18 #include "gtest/gtest.h"
19
20 #include <string_view>
21
22 #include "berberis/runtime_primitives/code_pool.h"
23
24 namespace berberis {
25
26 class MockExecRegionFactory {
27 public:
SetImpl(MockExecRegionFactory * impl)28 static void SetImpl(MockExecRegionFactory* impl) { impl_ = impl; }
29
30 static const uint32_t kExecRegionSize;
31
32 // Gmock is not able to mock static methods so we call *Impl counterpart
33 // and mock it instead.
Create(size_t size)34 static ExecRegion Create(size_t size) { return impl_->CreateImpl(size); }
35
36 MOCK_METHOD(ExecRegion, CreateImpl, (size_t));
37
38 private:
39 static MockExecRegionFactory* impl_;
40 };
41
42 const uint32_t MockExecRegionFactory::kExecRegionSize = sysconf(_SC_PAGESIZE);
43 MockExecRegionFactory* MockExecRegionFactory::impl_ = nullptr;
44
45 namespace {
46
AllocWritableRegion()47 uint8_t* AllocWritableRegion() {
48 return reinterpret_cast<uint8_t*>(MmapImplOrDie({
49 .size = MockExecRegionFactory::kExecRegionSize,
50 .prot = PROT_READ | PROT_WRITE,
51 .flags = MAP_PRIVATE | MAP_ANONYMOUS,
52 }));
53 }
54
AllocExecutableRegion()55 uint8_t* AllocExecutableRegion() {
56 return reinterpret_cast<uint8_t*>(MmapImplOrDie({
57 .size = MockExecRegionFactory::kExecRegionSize,
58 .prot = PROT_NONE,
59 .flags = MAP_PRIVATE | MAP_ANONYMOUS,
60 }));
61 }
62
TEST(CodePool,Smoke)63 TEST(CodePool, Smoke) {
64 MockExecRegionFactory exec_region_factory_mock;
65 MockExecRegionFactory::SetImpl(&exec_region_factory_mock);
66 auto* first_exec_region_memory_write = AllocWritableRegion();
67 auto* first_exec_region_memory_exec = AllocExecutableRegion();
68 auto* second_exec_region_memory_write = AllocWritableRegion();
69 auto* second_exec_region_memory_exec = AllocExecutableRegion();
70
71 EXPECT_CALL(exec_region_factory_mock, CreateImpl(MockExecRegionFactory::kExecRegionSize))
72 .WillOnce([&](size_t) {
73 return ExecRegion{first_exec_region_memory_exec,
74 first_exec_region_memory_write,
75 MockExecRegionFactory::kExecRegionSize};
76 })
77 .WillOnce([&](size_t) {
78 return ExecRegion{second_exec_region_memory_exec,
79 second_exec_region_memory_write,
80 MockExecRegionFactory::kExecRegionSize};
81 });
82
83 CodePool<MockExecRegionFactory> code_pool;
84 {
85 MachineCode machine_code;
86 constexpr std::string_view kCode = "test1";
87 machine_code.AddSequence(kCode.data(), kCode.size());
88 auto host_code = code_pool.Add(&machine_code);
89 ASSERT_EQ(host_code, first_exec_region_memory_exec);
90 EXPECT_EQ(std::string_view{reinterpret_cast<const char*>(first_exec_region_memory_write)},
91 kCode);
92 }
93
94 code_pool.ResetExecRegion();
95
96 {
97 MachineCode machine_code;
98 constexpr std::string_view kCode = "test2";
99 machine_code.AddSequence(kCode.data(), kCode.size());
100 auto host_code = code_pool.Add(&machine_code);
101 ASSERT_EQ(host_code, second_exec_region_memory_exec);
102 EXPECT_EQ(std::string_view{reinterpret_cast<const char*>(second_exec_region_memory_write)},
103 kCode);
104 }
105 }
106
TEST(DataPool,Smoke)107 TEST(DataPool, Smoke) {
108 DataPool data_pool;
109 static uint32_t kConst1 = 0x12345678;
110 static uint32_t kConst2 = 0x87654321;
111 uint32_t kVar = kConst2;
112 uint32_t* ptr = data_pool.Add(kVar);
113 EXPECT_EQ(kConst2, *ptr);
114 *ptr = kConst1;
115 EXPECT_EQ(kConst1, *ptr);
116 }
117
118 } // namespace
119
120 } // namespace berberis
121