1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "exec_utils.h"
#include <sys/utsname.h>
#include <csignal>
#include <cstring>
#include <filesystem>
#include <memory>
#include <optional>
#include <tuple>
#include "android-base/logging.h"
#include "android-base/result.h"
#include "android-base/stringprintf.h"
#include "base/file_utils.h"
#include "base/memory_tool.h"
#include "common_runtime_test.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace art {
using ::android::base::Result;
using ::testing::_;
using ::testing::AllOf;
using ::testing::Gt;
using ::testing::HasSubstr;
using ::testing::InSequence;
using ::testing::MockFunction;
using ::testing::Ne;
using ::testing::Return;
std::string PrettyArguments(const char* signature);
std::string PrettyReturnType(const char* signature);
std::string GetBin(const std::string& name) {
if (kIsTargetBuild) {
std::string android_root(GetAndroidRoot());
return android_root + "/bin/" + name;
} else if (std::filesystem::exists("/usr/bin/" + name)) {
return "/usr/bin/" + name;
} else {
return "/bin/" + name;
}
}
std::tuple<int, int> GetKernelVersion() {
std::tuple<int, int> version;
utsname uts;
CHECK_EQ(uname(&uts), 0);
CHECK_EQ(sscanf(uts.release, "%d.%d", &std::get<0>(version), &std::get<1>(version)), 2);
return version;
}
class TestingExecUtils : public ExecUtils {
public:
MOCK_METHOD(std::string, GetProcStat, (pid_t pid), (const, override));
MOCK_METHOD(Result<int64_t>, DoGetUptimeMs, (), (const));
MOCK_METHOD(int64_t, GetTicksPerSec, (), (const, override));
// A workaround to avoid MOCK_METHOD on a method with an `std::string*` parameter, which will lead
// to a conflict between gmock and android-base/logging.h (b/132668253).
std::optional<int64_t> GetUptimeMs(std::string* error_msg) const override {
Result<int64_t> result = DoGetUptimeMs();
if (result.ok()) {
return *result;
}
*error_msg = result.error().message();
return std::nullopt;
}
};
class AlwaysFallbackExecUtils : public TestingExecUtils {
protected:
android::base::unique_fd PidfdOpen(pid_t) const override { return android::base::unique_fd(-1); }
};
class NeverFallbackExecUtils : public TestingExecUtils {
protected:
android::base::unique_fd PidfdOpen(pid_t pid) const override {
android::base::unique_fd pidfd = ExecUtils::PidfdOpen(pid);
CHECK_GE(pidfd.get(), 0) << strerror(errno);
return pidfd;
}
};
class ExecUtilsTest : public CommonRuntimeTest, public testing::WithParamInterface<bool> {
protected:
void SetUp() override {
CommonRuntimeTest::SetUp();
bool always_fallback = GetParam();
if (always_fallback) {
exec_utils_ = std::make_unique<AlwaysFallbackExecUtils>();
} else {
if (GetKernelVersion() >= std::make_tuple(5, 4)) {
exec_utils_ = std::make_unique<NeverFallbackExecUtils>();
} else {
GTEST_SKIP() << "Kernel version older than 5.4";
}
}
}
std::unique_ptr<TestingExecUtils> exec_utils_;
};
TEST_P(ExecUtilsTest, ExecSuccess) {
std::vector<std::string> command;
command.push_back(GetBin("id"));
std::string error_msg;
// Historical note: Running on Valgrind failed due to some memory
// that leaks in thread alternate signal stacks.
EXPECT_TRUE(exec_utils_->Exec(command, &error_msg));
EXPECT_EQ(0U, error_msg.size()) << error_msg;
}
TEST_P(ExecUtilsTest, ExecError) {
std::vector<std::string> command;
command.push_back("bogus");
std::string error_msg;
// Historical note: Running on Valgrind failed due to some memory
// that leaks in thread alternate signal stacks.
ExecResult result = exec_utils_->ExecAndReturnResult(command, /*timeout_sec=*/-1, &error_msg);
EXPECT_EQ(result.status, ExecResult::kSignaled);
EXPECT_EQ(result.signal, SIGABRT);
EXPECT_FALSE(error_msg.empty());
}
TEST_P(ExecUtilsTest, EnvSnapshotAdditionsAreNotVisible) {
static constexpr const char* kModifiedVariable = "EXEC_SHOULD_NOT_EXPORT_THIS";
static constexpr int kOverwrite = 1;
// Set an variable in the current environment.
EXPECT_EQ(setenv(kModifiedVariable, "NEVER", kOverwrite), 0);
// Test that it is not exported.
std::vector<std::string> command;
command.push_back(GetBin("printenv"));
command.push_back(kModifiedVariable);
std::string error_msg;
// Historical note: Running on Valgrind failed due to some memory
// that leaks in thread alternate signal stacks.
EXPECT_FALSE(exec_utils_->Exec(command, &error_msg));
EXPECT_NE(0U, error_msg.size()) << error_msg;
}
TEST_P(ExecUtilsTest, EnvSnapshotDeletionsAreNotVisible) {
static constexpr const char* kDeletedVariable = "PATH";
static constexpr int kOverwrite = 1;
// Save the variable's value.
const char* save_value = getenv(kDeletedVariable);
EXPECT_NE(save_value, nullptr);
// Delete the variable.
EXPECT_EQ(unsetenv(kDeletedVariable), 0);
// Test that it is not exported.
std::vector<std::string> command;
command.push_back(GetBin("printenv"));
command.push_back(kDeletedVariable);
std::string error_msg;
// Historical note: Running on Valgrind failed due to some memory
// that leaks in thread alternate signal stacks.
EXPECT_TRUE(exec_utils_->Exec(command, &error_msg));
EXPECT_EQ(0U, error_msg.size()) << error_msg;
// Restore the variable's value.
EXPECT_EQ(setenv(kDeletedVariable, save_value, kOverwrite), 0);
}
static std::vector<std::string> SleepCommand(int sleep_seconds) {
std::vector<std::string> command;
command.push_back(GetBin("sleep"));
command.push_back(android::base::StringPrintf("%d", sleep_seconds));
return command;
}
TEST_P(ExecUtilsTest, ExecTimeout) {
static constexpr int kSleepSeconds = 5;
static constexpr int kWaitSeconds = 1;
std::vector<std::string> command = SleepCommand(kSleepSeconds);
std::string error_msg;
EXPECT_EQ(exec_utils_->ExecAndReturnResult(command, kWaitSeconds, &error_msg).status,
ExecResult::kTimedOut)
<< error_msg;
EXPECT_THAT(error_msg, HasSubstr("timed out"));
}
TEST_P(ExecUtilsTest, ExecNoTimeout) {
static constexpr int kSleepSeconds = 1;
static constexpr int kWaitSeconds = 5;
std::vector<std::string> command = SleepCommand(kSleepSeconds);
std::string error_msg;
EXPECT_EQ(exec_utils_->ExecAndReturnResult(command, kWaitSeconds, &error_msg).status,
ExecResult::kExited)
<< error_msg;
}
TEST_P(ExecUtilsTest, ExecStat) {
std::vector<std::string> command;
command.push_back(GetBin("id"));
std::string error_msg;
ProcessStat stat;
// The process filename is "a) b".
EXPECT_CALL(*exec_utils_, GetProcStat(_))
.WillOnce(Return(
"14963 (a) b) Z 6067 14963 1 0 -1 4228108 105 0 0 0 94 5 0 0 39 19 1 0 162034388 0 0 "
"18446744073709551615 0 0 0 0 0 0 20999 0 0 1 0 0 17 71 0 0 0 0 0 0 0 0 0 0 0 0 9"));
EXPECT_CALL(*exec_utils_, DoGetUptimeMs()).WillOnce(Return(1620344887ll));
EXPECT_CALL(*exec_utils_, GetTicksPerSec()).WillOnce(Return(100));
ASSERT_EQ(
exec_utils_
->ExecAndReturnResult(command, /*timeout_sec=*/-1, ExecCallbacks(), &stat, &error_msg)
.status,
ExecResult::kExited)
<< error_msg;
EXPECT_EQ(stat.cpu_time_ms, 990);
EXPECT_EQ(stat.wall_time_ms, 1007);
}
TEST_P(ExecUtilsTest, ExecStatNoStartTime) {
std::vector<std::string> command;
command.push_back(GetBin("id"));
std::string error_msg;
ProcessStat stat;
// The process filename is "a) b".
EXPECT_CALL(*exec_utils_, GetProcStat(_))
.WillOnce(Return(
"14963 (a) b) Z 6067 14963 1 0 -1 4228108 105 0 0 0 94 5 0 0 39 19 1 0 0 0 0 "
"18446744073709551615 0 0 0 0 0 0 20999 0 0 1 0 0 17 71 0 0 0 0 0 0 0 0 0 0 0 0 9"));
EXPECT_CALL(*exec_utils_, DoGetUptimeMs()).WillOnce(Return(1620344887ll));
EXPECT_CALL(*exec_utils_, GetTicksPerSec()).WillOnce(Return(100));
ASSERT_EQ(
exec_utils_
->ExecAndReturnResult(command, /*timeout_sec=*/-1, ExecCallbacks(), &stat, &error_msg)
.status,
ExecResult::kExited)
<< error_msg;
EXPECT_EQ(stat.cpu_time_ms, 0);
EXPECT_EQ(stat.wall_time_ms, 0);
}
TEST_P(ExecUtilsTest, ExecStatNoUptime) {
std::vector<std::string> command;
command.push_back(GetBin("id"));
std::string error_msg;
ProcessStat stat;
EXPECT_CALL(*exec_utils_, DoGetUptimeMs())
.WillOnce(Return(Result<int64_t>(Errorf("Failed to get uptime"))));
ASSERT_EQ(
exec_utils_
->ExecAndReturnResult(command, /*timeout_sec=*/-1, ExecCallbacks(), &stat, &error_msg)
.status,
ExecResult::kExited)
<< error_msg;
EXPECT_EQ(stat.cpu_time_ms, 0);
EXPECT_EQ(stat.wall_time_ms, 0);
}
TEST_P(ExecUtilsTest, ExecStatFailed) {
std::vector<std::string> command = SleepCommand(5);
std::string error_msg;
ProcessStat stat;
EXPECT_CALL(*exec_utils_, GetProcStat(_))
.WillOnce(Return(
"14963 (a) b) Z 6067 14963 1 0 -1 4228108 105 0 0 0 94 5 0 0 39 19 1 0 162034388 0 0 "
"18446744073709551615 0 0 0 0 0 0 20999 0 0 1 0 0 17 71 0 0 0 0 0 0 0 0 0 0 0 0 9"));
EXPECT_CALL(*exec_utils_, DoGetUptimeMs()).WillOnce(Return(1620344887ll));
EXPECT_CALL(*exec_utils_, GetTicksPerSec()).WillOnce(Return(100));
// This will always time out.
ASSERT_EQ(
exec_utils_
->ExecAndReturnResult(command, /*timeout_sec=*/1, ExecCallbacks(), &stat, &error_msg)
.status,
ExecResult::kTimedOut);
EXPECT_EQ(stat.cpu_time_ms, 990);
EXPECT_EQ(stat.wall_time_ms, 1007);
}
TEST_P(ExecUtilsTest, ExecCallbacks) {
MockFunction<void(pid_t)> on_start;
MockFunction<void(pid_t)> on_end;
{
InSequence s;
EXPECT_CALL(on_start, Call(AllOf(Gt(0), Ne(getpid()))));
EXPECT_CALL(on_end, Call(AllOf(Gt(0), Ne(getpid()))));
}
std::vector<std::string> command;
command.push_back(GetBin("id"));
std::string error_msg;
exec_utils_->ExecAndReturnResult(command,
/*timeout_sec=*/-1,
ExecCallbacks{
.on_start = on_start.AsStdFunction(),
.on_end = on_end.AsStdFunction(),
},
/*stat=*/nullptr,
&error_msg);
}
INSTANTIATE_TEST_SUITE_P(AlwaysOrNeverFallback, ExecUtilsTest, testing::Values(true, false));
} // namespace art
|