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
|
// Copyright 2018 The gVisor Authors.
//
// 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 "test/util/test_util.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <ctime>
#include <iostream>
#include <vector>
#include "absl/base/attributes.h"
#include "absl/flags/flag.h" // IWYU pragma: keep
#include "absl/flags/parse.h" // IWYU pragma: keep
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/time/time.h"
#include "test/util/fs_util.h"
#include "test/util/posix_error.h"
namespace gvisor {
namespace testing {
#define TEST_ON_GVISOR "TEST_ON_GVISOR"
#define GVISOR_NETWORK "GVISOR_NETWORK"
bool IsRunningOnGvisor() { return GvisorPlatform() != Platform::kNative; }
Platform GvisorPlatform() {
// Set by runner.go.
char* env = getenv(TEST_ON_GVISOR);
if (!env) {
return Platform::kNative;
}
if (strcmp(env, "ptrace") == 0) {
return Platform::kPtrace;
}
if (strcmp(env, "kvm") == 0) {
return Platform::kKVM;
}
std::cerr << "unknown platform " << env;
abort();
}
bool IsRunningWithHostinet() {
char* env = getenv(GVISOR_NETWORK);
return env && strcmp(env, "host") == 0;
}
// Inline cpuid instruction. Preserve %ebx/%rbx register. In PIC compilations
// %ebx contains the address of the global offset table. %rbx is occasionally
// used to address stack variables in presence of dynamic allocas.
#if defined(__x86_64__)
#define GETCPUID(a, b, c, d, a_inp, c_inp) \
asm("mov %%rbx, %%rdi\n" \
"cpuid\n" \
"xchg %%rdi, %%rbx\n" \
: "=a"(a), "=D"(b), "=c"(c), "=d"(d) \
: "a"(a_inp), "2"(c_inp))
#endif // defined(__x86_64__)
CPUVendor GetCPUVendor() {
uint32 eax, ebx, ecx, edx;
std::string vendor_str;
// Get vendor string (issue CPUID with eax = 0)
GETCPUID(eax, ebx, ecx, edx, 0, 0);
vendor_str.append(reinterpret_cast<char*>(&ebx), 4);
vendor_str.append(reinterpret_cast<char*>(&edx), 4);
vendor_str.append(reinterpret_cast<char*>(&ecx), 4);
if (vendor_str == "GenuineIntel") {
return CPUVendor::kIntel;
} else if (vendor_str == "AuthenticAMD") {
return CPUVendor::kAMD;
}
return CPUVendor::kUnknownVendor;
}
bool operator==(const KernelVersion& first, const KernelVersion& second) {
return first.major == second.major && first.minor == second.minor &&
first.micro == second.micro;
}
PosixErrorOr<KernelVersion> ParseKernelVersion(absl::string_view vers_str) {
KernelVersion version = {};
std::vector<std::string> values =
absl::StrSplit(vers_str, absl::ByAnyChar(".-"));
if (values.size() == 2) {
ASSIGN_OR_RETURN_ERRNO(version.major, Atoi<int>(values[0]));
ASSIGN_OR_RETURN_ERRNO(version.minor, Atoi<int>(values[1]));
return version;
} else if (values.size() >= 3) {
ASSIGN_OR_RETURN_ERRNO(version.major, Atoi<int>(values[0]));
ASSIGN_OR_RETURN_ERRNO(version.minor, Atoi<int>(values[1]));
ASSIGN_OR_RETURN_ERRNO(version.micro, Atoi<int>(values[2]));
return version;
}
return PosixError(EINVAL, absl::StrCat("Unknown kernel release: ", vers_str));
}
PosixErrorOr<KernelVersion> GetKernelVersion() {
utsname buf;
RETURN_ERROR_IF_SYSCALL_FAIL(uname(&buf));
return ParseKernelVersion(buf.release);
}
std::string CPUSetToString(const cpu_set_t& set, size_t cpus) {
std::string str = "cpuset[";
for (unsigned int n = 0; n < cpus; n++) {
if (CPU_ISSET(n, &set)) {
if (n != 0) {
absl::StrAppend(&str, " ");
}
absl::StrAppend(&str, n);
}
}
absl::StrAppend(&str, "]");
return str;
}
// An overloaded operator<< makes it easy to dump the value of an OpenFd.
std::ostream& operator<<(std::ostream& out, OpenFd const& ofd) {
out << ofd.fd << " -> " << ofd.link;
return out;
}
// An overloaded operator<< makes it easy to dump a vector of OpenFDs.
std::ostream& operator<<(std::ostream& out, std::vector<OpenFd> const& v) {
for (const auto& ofd : v) {
out << ofd << std::endl;
}
return out;
}
PosixErrorOr<std::vector<OpenFd>> GetOpenFDs() {
// Get the results from /proc/self/fd.
ASSIGN_OR_RETURN_ERRNO(auto dir_list,
ListDir("/proc/self/fd", /*skipdots=*/true));
std::vector<OpenFd> ret_fds;
for (const auto& str_fd : dir_list) {
OpenFd open_fd = {};
ASSIGN_OR_RETURN_ERRNO(open_fd.fd, Atoi<int>(str_fd));
std::string path = absl::StrCat("/proc/self/fd/", open_fd.fd);
// Resolve the link.
char buf[PATH_MAX] = {};
int ret = readlink(path.c_str(), buf, sizeof(buf));
if (ret < 0) {
if (errno == ENOENT) {
// The FD may have been closed, let's be resilient.
continue;
}
return PosixError(
errno, absl::StrCat("readlink of ", path, " returned errno ", errno));
}
open_fd.link = std::string(buf, ret);
ret_fds.emplace_back(std::move(open_fd));
}
return ret_fds;
}
PosixErrorOr<uint64> Links(const std::string& path) {
struct stat st;
if (stat(path.c_str(), &st)) {
return PosixError(errno, absl::StrCat("Failed to stat ", path));
}
return static_cast<uint64>(st.st_nlink);
}
void RandomizeBuffer(void* buffer, size_t len) {
struct timespec ts = {};
clock_gettime(CLOCK_MONOTONIC, &ts);
uint32 seed = static_cast<uint32>(ts.tv_nsec);
char* const buf = static_cast<char*>(buffer);
for (size_t i = 0; i < len; i++) {
buf[i] = rand_r(&seed) % 255;
}
}
std::vector<std::vector<struct iovec>> GenerateIovecs(uint64 total_size,
void* buf,
size_t buflen) {
std::vector<std::vector<struct iovec>> result;
for (uint64 offset = 0; offset < total_size;) {
auto& iovec_array = *result.emplace(result.end());
for (; offset < total_size && iovec_array.size() < IOV_MAX;
offset += buflen) {
struct iovec iov = {};
iov.iov_base = buf;
iov.iov_len = std::min<uint64>(total_size - offset, buflen);
iovec_array.push_back(iov);
}
}
return result;
}
uint64 Megabytes(uint64 n) {
// Overflow check, upper 20 bits in n shouldn't be set.
TEST_CHECK(!(0xfffff00000000000 & n));
return n << 20;
}
bool Equivalent(uint64 current, uint64 target, double tolerance) {
auto abs_diff = target > current ? target - current : current - target;
return abs_diff <= static_cast<uint64>(tolerance * target);
}
} // namespace testing
} // namespace gvisor
|