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       .berberis_flags = kMmapBerberis32Bit,
61   }));
62 }
63 
TEST(CodePool,Smoke)64 TEST(CodePool, Smoke) {
65   MockExecRegionFactory exec_region_factory_mock;
66   MockExecRegionFactory::SetImpl(&exec_region_factory_mock);
67   auto* first_exec_region_memory_write = AllocWritableRegion();
68   auto* first_exec_region_memory_exec = AllocExecutableRegion();
69   auto* second_exec_region_memory_write = AllocWritableRegion();
70   auto* second_exec_region_memory_exec = AllocExecutableRegion();
71 
72   EXPECT_CALL(exec_region_factory_mock, CreateImpl(MockExecRegionFactory::kExecRegionSize))
73       .WillOnce([&](size_t) {
74         return ExecRegion{first_exec_region_memory_exec,
75                           first_exec_region_memory_write,
76                           MockExecRegionFactory::kExecRegionSize};
77       })
78       .WillOnce([&](size_t) {
79         return ExecRegion{second_exec_region_memory_exec,
80                           second_exec_region_memory_write,
81                           MockExecRegionFactory::kExecRegionSize};
82       });
83 
84   CodePool<MockExecRegionFactory> code_pool;
85   {
86     MachineCode machine_code;
87     constexpr std::string_view kCode = "test1";
88     machine_code.AddSequence(kCode.data(), kCode.size());
89     auto host_code = code_pool.Add(&machine_code);
90     ASSERT_EQ(host_code, AsHostCodeAddr(first_exec_region_memory_exec));
91     EXPECT_EQ(std::string_view{reinterpret_cast<const char*>(first_exec_region_memory_write)},
92               kCode);
93   }
94 
95   code_pool.ResetExecRegion();
96 
97   {
98     MachineCode machine_code;
99     constexpr std::string_view kCode = "test2";
100     machine_code.AddSequence(kCode.data(), kCode.size());
101     auto host_code = code_pool.Add(&machine_code);
102     ASSERT_EQ(host_code, AsHostCodeAddr(second_exec_region_memory_exec));
103     EXPECT_EQ(std::string_view{reinterpret_cast<const char*>(second_exec_region_memory_write)},
104               kCode);
105   }
106 }
107 
TEST(DataPool,Smoke)108 TEST(DataPool, Smoke) {
109   DataPool data_pool;
110   static uint32_t kConst1 = 0x12345678;
111   static uint32_t kConst2 = 0x87654321;
112   uint32_t kVar = kConst2;
113   uint32_t* ptr = data_pool.Add(kVar);
114   EXPECT_EQ(kConst2, *ptr);
115   *ptr = kConst1;
116   EXPECT_EQ(kConst1, *ptr);
117 }
118 
119 }  // namespace
120 
121 }  // namespace berberis
122