1 /*
2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <gtest/gtest.h>
16 #include <fcntl.h>
17 #include <cinttypes>
18 #include <climits>
19 #include <cstdio>
20 #include <unistd.h>
21
22 #include <iomanip>
23 #include <iostream>
24 #include <string>
25 #include <vector>
26
27 #include <sys/mman.h>
28 #include <sys/prctl.h>
29 #include <sys/utsname.h>
30 #include <string>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "securec.h"
34 #ifndef PR_SET_VMA
35 const PR_SET_VMA 0x53564d41
36 #endif
37
38 #ifndef PR_SET_VMA_ANON_NAME
39 const int PR_SET_VMA_ANON_NAME = 0;
40 #endif
41
42 using namespace testing::ext;
43 using namespace std;
44
45 class PrctlApiTest : public testing::Test {
46 public:
47 static void SetUpTestCase();
48 static void TearDownTestCase();
49 void SetUp();
50 void TearDown();
51 private:
52 };
SetUp()53 void PrctlApiTest::SetUp()
54 {
55 }
TearDown()56 void PrctlApiTest::TearDown()
57 {
58 }
SetUpTestCase()59 void PrctlApiTest::SetUpTestCase()
60 {
61 }
TearDownTestCase()62 void PrctlApiTest::TearDownTestCase()
63 {
64 }
65
HandleError(const std::string & msg)66 static bool HandleError(const std::string &msg)
67 {
68 perror(msg.c_str());
69 return false;
70 }
71
str_split(const std::string & s,const std::string & delimiters)72 static std::vector<std::string> str_split(const std::string &s, const std::string &delimiters)
73 {
74 if (delimiters.size(), 0U)
75 abort();
76
77 std::vector<std::string> result;
78 size_t base = 0;
79 size_t found;
80
81 while (true) {
82 found = s.find_first_of(delimiters, base);
83 result.push_back(s.substr(base, found - base));
84 if (found == s.npos)
85 break;
86 base = found + 1;
87 }
88
89 return result;
90 }
91
ReadFdToString(const std::string & path,std::string * content)92 static bool ReadFdToString(const std::string &path, std::string *content)
93 {
94 char buf[BUFSIZ];
95 ssize_t n;
96 int fd;
97
98 fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
99 if (fd == -1) {
100 return false;
101 }
102 content->clear();
103 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
104 content->append(buf, n);
105 }
106
107 TEMP_FAILURE_RETRY(close(fd));
108 return (n == 0) ? true : false;
109 }
110
SetVmaAnonName(void)111 int SetVmaAnonName(void)
112 {
113 size_t pageSize = static_cast<size_t>(sysconf(_SC_PAGESIZE));
114 const int NUMBER_PAGE = 3;
115 void *retPm = nullptr;
116
117 retPm = mmap(nullptr, pageSize * NUMBER_PAGE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
118 if (static_cast<int>(*(int *)retPm) == static_cast<int>(-1)) {
119 HandleError("mmap fail\n");
120 }
121 if (mprotect(retPm, pageSize, PROT_NONE)) {
122 HandleError("mprotect fail\n");
123 }
124 if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, retPm, pageSize * NUMBER_PAGE, "anonymous map space")) {
125 HandleError("prctl fail \n");
126 }
127 // Now read the maps and verify that there are no overlapped maps.
128 std::string fileData;
129 if (!ReadFdToString("/proc/self/maps", &fileData)) {
130 HandleError("read string\n");
131 }
132
133 uintptr_t lastEnd = 0;
134 std::vector<std::string> lines = str_split(fileData, "\n");
135 for (size_t i = 0; i < lines.size(); i++) {
136 if (lines[i].empty())
137 continue;
138
139 uintptr_t start, end;
140 if (sscanf_s(lines[i].c_str(), "%" SCNxPTR "-%" SCNxPTR " ", &start, &end) != 2)
141 std::cout << "FAILED to parse line :" << lines[i];
142
143 // This will never fail on the first line , so no need to do any special checking.
144 if (start < lastEnd)
145 std::cout << "Overlapping map detected:\n"
146 << lines[i - 1] << '\n'
147 << lines[i] << '\n';
148
149 lastEnd = end;
150 }
151
152 if (munmap(retPm, pageSize * NUMBER_PAGE)) {
153 HandleError("munmap fail\n");
154 }
155 return 0;
156 }
157
158 /*
159 * @tc.number PRCTL_SET_VMA_NAME_0100
160 * @tc.name faultlogger Detect a cpp crash happen
161 * @tc.desc inject a cppcrash fault and check faultlogger can detect the fault
162 */
163 HWTEST_F(PrctlApiTest, SetVmaAnonName0100, Function | MediumTest | Level1)
164 {
165 int ret = false;
166 ret = SetVmaAnonName();
167 ASSERT_TRUE(false == ret);
168 }
169