1 /*
2  * Copyright (C) 2020 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 <android-base/test_utils.h>
20 
21 #include "get_test_data.h"
22 #include "kallsyms.h"
23 #include "test_util.h"
24 
25 using namespace simpleperf;
26 
ModulesMatch(const char * p,const char * q)27 static bool ModulesMatch(const char* p, const char* q) {
28   if (p == nullptr && q == nullptr) {
29     return true;
30   }
31   if (p != nullptr && q != nullptr) {
32     return strcmp(p, q) == 0;
33   }
34   return false;
35 }
36 
KernelSymbolsMatch(const KernelSymbol & sym1,const KernelSymbol & sym2)37 static bool KernelSymbolsMatch(const KernelSymbol& sym1, const KernelSymbol& sym2) {
38   return sym1.addr == sym2.addr && sym1.type == sym2.type && strcmp(sym1.name, sym2.name) == 0 &&
39          ModulesMatch(sym1.module, sym2.module);
40 }
41 
TEST(kallsyms,ProcessKernelSymbols)42 TEST(kallsyms, ProcessKernelSymbols) {
43   std::string data =
44       "ffffffffa005c4e4 d __warned.41698   [libsas]\n"
45       "aaaaaaaaaaaaaaaa T _text\n"
46       "cccccccccccccccc c ccccc\n";
47   KernelSymbol expected_symbol;
48   expected_symbol.addr = 0xffffffffa005c4e4ULL;
49   expected_symbol.type = 'd';
50   expected_symbol.name = "__warned.41698";
51   expected_symbol.module = "libsas";
52   ASSERT_TRUE(ProcessKernelSymbols(
53       data, std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
54 
55   expected_symbol.addr = 0xaaaaaaaaaaaaaaaaULL;
56   expected_symbol.type = 'T';
57   expected_symbol.name = "_text";
58   expected_symbol.module = nullptr;
59   ASSERT_TRUE(ProcessKernelSymbols(
60       data, std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
61 
62   expected_symbol.name = "non_existent_symbol";
63   ASSERT_FALSE(ProcessKernelSymbols(
64       data, std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
65 }
66 
67 #if defined(__ANDROID__)
TEST(kallsyms,GetKernelStartAddress)68 TEST(kallsyms, GetKernelStartAddress) {
69   TEST_REQUIRE_ROOT();
70   ASSERT_NE(GetKernelStartAddress(), 0u);
71 }
72 
TEST(kallsyms,LoadKernelSymbols)73 TEST(kallsyms, LoadKernelSymbols) {
74   TEST_REQUIRE_ROOT();
75   std::string kallsyms;
76   ASSERT_TRUE(LoadKernelSymbols(&kallsyms));
77 }
78 
TEST(kallsyms,print_warning)79 TEST(kallsyms, print_warning) {
80   TEST_REQUIRE_NON_ROOT();
81   const std::string warning_msg = "Access to kernel symbol addresses is restricted.";
82   CapturedStderr capture;
83 
84   // Call each function requiring kernel addresses once. Check if the warning is printed.
85   ResetKernelAddressWarning();
86   ASSERT_EQ(0, GetKernelStartAddress());
87   capture.Stop();
88   ASSERT_NE(capture.str().find(warning_msg), std::string::npos);
89 
90   capture.Reset();
91   capture.Start();
92   ResetKernelAddressWarning();
93   std::string kallsyms;
94   ASSERT_FALSE(LoadKernelSymbols(&kallsyms));
95   capture.Stop();
96   ASSERT_NE(capture.str().find(warning_msg), std::string::npos);
97 
98   capture.Reset();
99   capture.Start();
100   ResetKernelAddressWarning();
101   ASSERT_TRUE(GetLoadedModules().empty());
102   capture.Stop();
103   ASSERT_NE(capture.str().find(warning_msg), std::string::npos);
104 
105   // Call functions requiring kernel addresses more than once.
106   // Check if the kernel address warning is only printed once.
107   capture.Reset();
108   capture.Start();
109   ResetKernelAddressWarning();
110   for (int i = 0; i < 2; i++) {
111     ASSERT_EQ(0, GetKernelStartAddress());
112     ASSERT_FALSE(LoadKernelSymbols(&kallsyms));
113     ASSERT_TRUE(GetLoadedModules().empty());
114   }
115   capture.Stop();
116   std::string output = capture.str();
117   auto pos = output.find(warning_msg);
118   ASSERT_NE(pos, std::string::npos);
119   ASSERT_EQ(output.find(warning_msg, pos + warning_msg.size()), std::string::npos);
120 }
121 #endif  // defined(__ANDROID__)
122