1 /*
2  * Copyright (C) 2017 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 /*
18  * End-to-end test to ensure that mapping of vsoc regions works on the guest.
19  */
20 
21 #include "common/vsoc/lib/e2e_test_region_view.h"
22 // TODO(b/64462568) Move the manager tests to a separate target
23 #include "guest/vsoc/lib/manager_region_view.h"
24 
25 #include <android-base/logging.h>
26 #include <gtest/gtest.h>
27 
28 #define DEATH_TEST_MESSAGE "abort converted to exit of 2 during death test"
29 
30 using vsoc::layout::e2e_test::E2EManagedTestRegionLayout;
31 using vsoc::layout::e2e_test::E2EManagerTestRegionLayout;
32 
33 static inline void disable_tombstones() {
34   // We don't want a tombstone, and we're already in the child, so we modify the
35   // behavior of LOG(ABORT) to print the well known message and do an
36   // error-based exit.
37   android::base::SetAborter([](const char*) {
38     fputs(DEATH_TEST_MESSAGE, stderr);
39     fflush(stderr);
40     exit(2);
41   });
42 }
43 
44 template <typename View>
45 void DeathTestView() {
46   disable_tombstones();
47   // View::GetInstance should never return.
48   EXPECT_FALSE(!!View::GetInstance());
49 }
50 
51 // Here is a summary of the two regions interrupt and write test:
52 // 1. Write our strings to the first region
53 // 2. Ensure that our peer hasn't signalled the second region. That would
54 //    indicate that it didn't wait for our interrupt.
55 // 3. Send the interrupt on the first region
56 // 4. Wait for our peer's interrupt on the first region
57 // 5. Confirm that we can see our peer's writes in the first region
58 // 6. Initialize our strings in the second region
59 // 7. Send an interrupt on the second region to our peer
60 // 8. Wait for our peer's interrupt on the second region
61 // 9. Confirm that we can see our peer's writes in the second region
62 // 10. Repeat the process for signaling.
63 // 11. Confirm that no interrupt is pending in the first region
64 // 12. Confirm that no interrupt is pending in the second region
65 
66 template <typename View>
67 void SetGuestStrings(View* in) {
68   size_t num_data = in->string_size();
69   EXPECT_LE(2U, num_data);
70   for (size_t i = 0; i < num_data; ++i) {
71     EXPECT_TRUE(!in->guest_string(i)[0] ||
72                 !strcmp(in->guest_string(i), View::Layout::guest_pattern));
73     in->set_guest_string(i, View::Layout::guest_pattern);
74     EXPECT_STREQ(in->guest_string(i), View::Layout::guest_pattern);
75   }
76 }
77 
78 template <typename View>
79 void CheckPeerStrings(View* in) {
80   size_t num_data = in->string_size();
81   EXPECT_LE(2U, num_data);
82   for (size_t i = 0; i < num_data; ++i) {
83     EXPECT_STREQ(View::Layout::host_pattern, in->host_string(i));
84   }
85 }
86 
87 TEST(RegionTest, BasicPeerTests) {
88   auto primary = vsoc::E2EPrimaryRegionView::GetInstance();
89   auto secondary = vsoc::E2ESecondaryRegionView::GetInstance();
90   ASSERT_TRUE(!!primary);
91   ASSERT_TRUE(!!secondary);
92   LOG(INFO) << "Regions are open";
93   SetGuestStrings(primary);
94   LOG(INFO) << "Primary guest strings are set";
95   EXPECT_FALSE(secondary->HasIncomingInterrupt());
96   LOG(INFO) << "Verified no early second interrupt";
97   EXPECT_TRUE(primary->MaybeInterruptPeer());
98   LOG(INFO) << "Interrupt sent. Waiting for first interrupt from peer";
99   primary->WaitForInterrupt();
100   LOG(INFO) << "First interrupt received";
101   CheckPeerStrings(primary);
102   LOG(INFO) << "Verified peer's primary strings";
103   SetGuestStrings(secondary);
104   LOG(INFO) << "Secondary guest strings are set";
105   EXPECT_TRUE(secondary->MaybeInterruptPeer());
106   LOG(INFO) << "Second interrupt sent";
107   secondary->WaitForInterrupt();
108   LOG(INFO) << "Second interrupt received";
109   CheckPeerStrings(secondary);
110   LOG(INFO) << "Verified peer's secondary strings";
111 
112   // Test signals
113   EXPECT_FALSE(secondary->HasIncomingInterrupt());
114   LOG(INFO) << "Verified no early second signal";
115   primary->SendSignal(vsoc::layout::Sides::Peer,
116                       &primary->data()->guest_to_host_signal);
117   LOG(INFO) << "Signal sent. Waiting for first signal from peer";
118   primary->WaitForInterrupt();
119   int count = 0;  // counts the number of signals received.
120   primary->ProcessSignalsFromPeer(
121       [&primary, &count](uint32_t offset) {
122         ++count;
123         EXPECT_TRUE(offset == primary->host_to_guest_signal_offset());
124       });
125   EXPECT_TRUE(count == 1);
126   LOG(INFO) << "Signal received on primary region";
127   secondary->SendSignal(vsoc::layout::Sides::Peer,
128                         &secondary->data()->guest_to_host_signal);
129   LOG(INFO) << "Signal sent. Waiting for second signal from peer";
130   secondary->WaitForInterrupt();
131   count = 0;
132   secondary->ProcessSignalsFromPeer(
133       [&secondary, &count](uint32_t offset) {
134         ++count;
135         EXPECT_TRUE(offset == secondary->host_to_guest_signal_offset());
136       });
137   EXPECT_TRUE(count == 1);
138   LOG(INFO) << "Signal received on secondary region";
139 
140   EXPECT_FALSE(primary->HasIncomingInterrupt());
141   EXPECT_FALSE(secondary->HasIncomingInterrupt());
142   LOG(INFO) << "PASS: BasicPeerTests";
143 }
144 
145 TEST(RegionTest, MissingRegionDeathTest) {
146   // EXPECT_DEATH creates a child for the test, so we do it out here.
147   // DeathTestGuestRegion will actually do the deadly call after ensuring
148   // that we don't create an unwanted tombstone.
149   EXPECT_EXIT(DeathTestView<vsoc::E2EUnfindableRegionView>(),
150               testing::ExitedWithCode(2),
151               ".*" DEATH_TEST_MESSAGE ".*");
152 }
153 
154 // Region view classes to allow calling the Open() function from the test.
155 class E2EManagedTestRegionView
156     : public vsoc::TypedRegionView<
157         E2EManagedTestRegionView,
158         E2EManagedTestRegionLayout> {
159  public:
160   using vsoc::TypedRegionView<
161       E2EManagedTestRegionView, E2EManagedTestRegionLayout>::Open;
162 };
163 class E2EManagerTestRegionView
164     : public vsoc::ManagerRegionView<
165         E2EManagerTestRegionView,
166         E2EManagerTestRegionLayout> {
167  public:
168   using vsoc::ManagerRegionView<
169       E2EManagerTestRegionView, E2EManagerTestRegionLayout>::Open;
170 };
171 
172 class ManagedRegionTest {
173  public:
174   void testManagedRegionFailMap() {
175     E2EManagedTestRegionView managed_region;
176     disable_tombstones();
177     // managed_region.Open should never return.
178     EXPECT_FALSE(managed_region.Open());
179   }
180 
181   void testManagedRegionMap() {
182     EXPECT_TRUE(manager_region_.Open());
183 
184     // Maps correctly with permission
185     const uint32_t owned_value = 65, begin_offset = 4096, end_offset = 8192;
186     int perm_fd = manager_region_.CreateFdScopedPermission(
187         &manager_region_.data()->data[0], owned_value, begin_offset,
188         end_offset);
189     EXPECT_TRUE(perm_fd >= 0);
190     fd_scoped_permission perm;
191     ASSERT_TRUE(ioctl(perm_fd, VSOC_GET_FD_SCOPED_PERMISSION, &perm) == 0);
192     void* mapped_ptr = mmap(NULL, perm.end_offset - perm.begin_offset,
193                             PROT_WRITE | PROT_READ, MAP_SHARED, perm_fd, 0);
194     EXPECT_FALSE(mapped_ptr == MAP_FAILED);
195 
196     // Owned value gets written
197     EXPECT_TRUE(manager_region_.data()->data[0] == owned_value);
198 
199     // Data written to the mapped memory stays there after unmap
200     std::string str = "managed by e2e_manager";
201     strcpy(reinterpret_cast<char*>(mapped_ptr), str.c_str());
202     EXPECT_TRUE(munmap(mapped_ptr, end_offset - begin_offset) == 0);
203     mapped_ptr = mmap(NULL, end_offset - begin_offset, PROT_WRITE | PROT_READ,
204                       MAP_SHARED, perm_fd, 0);
205     EXPECT_FALSE(mapped_ptr == MAP_FAILED);
206     EXPECT_TRUE(strcmp(reinterpret_cast<char*>(mapped_ptr), str.c_str()) == 0);
207 
208     // Create permission elsewhere in the region, map same offset and length,
209     // ensure data isn't there
210     EXPECT_TRUE(munmap(mapped_ptr, end_offset - begin_offset) == 0);
211     close(perm_fd);
212     EXPECT_TRUE(manager_region_.data()->data[0] == 0);
213     perm_fd = manager_region_.CreateFdScopedPermission(
214         &manager_region_.data()->data[1], owned_value, begin_offset + 4096,
215         end_offset + 4096);
216     EXPECT_TRUE(perm_fd >= 0);
217     mapped_ptr = mmap(NULL, end_offset - begin_offset, PROT_WRITE | PROT_READ,
218                       MAP_SHARED, perm_fd, 0);
219     EXPECT_FALSE(mapped_ptr == MAP_FAILED);
220     EXPECT_FALSE(strcmp(reinterpret_cast<char*>(mapped_ptr), str.c_str()) == 0);
221   }
222   ManagedRegionTest() {}
223 
224  private:
225   E2EManagerTestRegionView manager_region_;
226 };
227 
228 TEST(ManagedRegionTest, ManagedRegionFailMap) {
229   ManagedRegionTest test;
230   EXPECT_EXIT(test.testManagedRegionFailMap(), testing::ExitedWithCode(2),
231               ".*" DEATH_TEST_MESSAGE ".*");
232 }
233 
234 TEST(ManagedRegionTest, ManagedRegionMap) {
235   ManagedRegionTest test;
236   test.testManagedRegionMap();
237 }
238 
239 int main(int argc, char** argv) {
240   android::base::InitLogging(argv);
241   testing::InitGoogleTest(&argc, argv);
242   int rval = RUN_ALL_TESTS();
243   if (!rval) {
244     auto region = vsoc::E2EPrimaryRegionView::GetInstance();
245     region->guest_status(vsoc::layout::e2e_test::E2E_MEMORY_FILLED);
246     LOG(INFO) << "stage_1_guest_region_e2e_tests PASSED";
247   }
248   return rval;
249 }
250