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
|
// 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 <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "test/util/file_descriptor.h"
#include "test/util/logging.h"
#include "test/util/memory_util.h"
#include "test/util/multiprocess_util.h"
#include "test/util/posix_error.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
namespace gvisor {
namespace testing {
namespace {
void ExpectAllMappingBytes(Mapping const& m, char c) {
auto const v = m.view();
for (size_t i = 0; i < kPageSize; i++) {
ASSERT_EQ(v[i], c) << "at offset " << i;
}
}
// Equivalent to ExpectAllMappingBytes but async-signal-safe and with less
// helpful failure messages.
void CheckAllMappingBytes(Mapping const& m, char c) {
auto const v = m.view();
for (size_t i = 0; i < kPageSize; i++) {
TEST_CHECK_MSG(v[i] == c, "mapping contains wrong value");
}
}
TEST(MadviseDontneedTest, ZerosPrivateAnonPage) {
auto m = ASSERT_NO_ERRNO_AND_VALUE(
MmapAnon(kPageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE));
ExpectAllMappingBytes(m, 0);
memset(m.ptr(), 1, m.len());
ExpectAllMappingBytes(m, 1);
ASSERT_THAT(madvise(m.ptr(), m.len(), MADV_DONTNEED), SyscallSucceeds());
ExpectAllMappingBytes(m, 0);
}
TEST(MadviseDontneedTest, ZerosCOWAnonPageInCallerOnly) {
auto m = ASSERT_NO_ERRNO_AND_VALUE(
MmapAnon(kPageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE));
ExpectAllMappingBytes(m, 0);
memset(m.ptr(), 2, m.len());
ExpectAllMappingBytes(m, 2);
// Do madvise in a child process.
pid_t pid = fork();
CheckAllMappingBytes(m, 2);
if (pid == 0) {
TEST_PCHECK(madvise(m.ptr(), m.len(), MADV_DONTNEED) == 0);
CheckAllMappingBytes(m, 0);
_exit(0);
}
ASSERT_THAT(pid, SyscallSucceeds());
int status = 0;
ASSERT_THAT(waitpid(-1, &status, 0), SyscallSucceedsWithValue(pid));
EXPECT_TRUE(WIFEXITED(status));
EXPECT_EQ(WEXITSTATUS(status), 0);
// The child's madvise should not have affected the parent.
ExpectAllMappingBytes(m, 2);
}
TEST(MadviseDontneedTest, DoesNotModifySharedAnonPage) {
auto m = ASSERT_NO_ERRNO_AND_VALUE(
MmapAnon(kPageSize, PROT_READ | PROT_WRITE, MAP_SHARED));
ExpectAllMappingBytes(m, 0);
memset(m.ptr(), 3, m.len());
ExpectAllMappingBytes(m, 3);
ASSERT_THAT(madvise(m.ptr(), m.len(), MADV_DONTNEED), SyscallSucceeds());
ExpectAllMappingBytes(m, 3);
}
TEST(MadviseDontneedTest, CleansPrivateFilePage) {
TempPath f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
/* parent = */ GetAbsoluteTestTmpdir(),
/* content = */ std::string(kPageSize, 4), TempPath::kDefaultFileMode));
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(f.path(), O_RDWR));
Mapping m = ASSERT_NO_ERRNO_AND_VALUE(Mmap(
nullptr, kPageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd.get(), 0));
ExpectAllMappingBytes(m, 4);
memset(m.ptr(), 5, m.len());
ExpectAllMappingBytes(m, 5);
ASSERT_THAT(madvise(m.ptr(), m.len(), MADV_DONTNEED), SyscallSucceeds());
ExpectAllMappingBytes(m, 4);
}
TEST(MadviseDontneedTest, DoesNotModifySharedFilePage) {
TempPath f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
/* parent = */ GetAbsoluteTestTmpdir(),
/* content = */ std::string(kPageSize, 6), TempPath::kDefaultFileMode));
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(f.path(), O_RDWR));
Mapping m = ASSERT_NO_ERRNO_AND_VALUE(Mmap(
nullptr, kPageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0));
ExpectAllMappingBytes(m, 6);
memset(m.ptr(), 7, m.len());
ExpectAllMappingBytes(m, 7);
ASSERT_THAT(madvise(m.ptr(), m.len(), MADV_DONTNEED), SyscallSucceeds());
ExpectAllMappingBytes(m, 7);
}
TEST(MadviseDontneedTest, IgnoresPermissions) {
auto m =
ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, MAP_PRIVATE));
EXPECT_THAT(madvise(m.ptr(), m.len(), MADV_DONTNEED), SyscallSucceeds());
}
TEST(MadviseDontforkTest, AddressLength) {
auto m =
ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, MAP_PRIVATE));
char *addr = static_cast<char *>(m.ptr());
// Address must be page aligned.
EXPECT_THAT(madvise(addr + 1, kPageSize, MADV_DONTFORK),
SyscallFailsWithErrno(EINVAL));
// Zero length madvise always succeeds.
EXPECT_THAT(madvise(addr, 0, MADV_DONTFORK), SyscallSucceeds());
// Length must not roll over after rounding up.
size_t badlen = std::numeric_limits<std::size_t>::max() - (kPageSize / 2);
EXPECT_THAT(madvise(0, badlen, MADV_DONTFORK), SyscallFailsWithErrno(EINVAL));
// Length need not be page aligned - it is implicitly rounded up.
EXPECT_THAT(madvise(addr, 1, MADV_DONTFORK), SyscallSucceeds());
EXPECT_THAT(madvise(addr, kPageSize, MADV_DONTFORK), SyscallSucceeds());
}
TEST(MadviseDontforkTest, DontforkShared) {
// Mmap two shared file-backed pages and MADV_DONTFORK the second page.
TempPath f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
/* parent = */ GetAbsoluteTestTmpdir(),
/* content = */ std::string(kPageSize * 2, 2),
TempPath::kDefaultFileMode));
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(f.path(), O_RDWR));
Mapping m = ASSERT_NO_ERRNO_AND_VALUE(Mmap(
nullptr, kPageSize * 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0));
const Mapping ms1 = Mapping(reinterpret_cast<void *>(m.addr()), kPageSize);
const Mapping ms2 =
Mapping(reinterpret_cast<void *>(m.addr() + kPageSize), kPageSize);
m.release();
ASSERT_THAT(madvise(ms2.ptr(), kPageSize, MADV_DONTFORK), SyscallSucceeds());
const auto rest = [&] {
// First page is mapped in child and modifications are visible to parent
// via the shared mapping.
TEST_CHECK(IsMapped(ms1.addr()));
ExpectAllMappingBytes(ms1, 2);
memset(ms1.ptr(), 1, kPageSize);
ExpectAllMappingBytes(ms1, 1);
// Second page must not be mapped in child.
TEST_CHECK(!IsMapped(ms2.addr()));
};
EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
ExpectAllMappingBytes(ms1, 1); // page contents modified by child.
ExpectAllMappingBytes(ms2, 2); // page contents unchanged.
}
TEST(MadviseDontforkTest, DontforkAnonPrivate) {
// Mmap three anonymous pages and MADV_DONTFORK the middle page.
Mapping m = ASSERT_NO_ERRNO_AND_VALUE(
MmapAnon(kPageSize * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE));
const Mapping mp1 = Mapping(reinterpret_cast<void *>(m.addr()), kPageSize);
const Mapping mp2 =
Mapping(reinterpret_cast<void *>(m.addr() + kPageSize), kPageSize);
const Mapping mp3 =
Mapping(reinterpret_cast<void *>(m.addr() + 2 * kPageSize), kPageSize);
m.release();
ASSERT_THAT(madvise(mp2.ptr(), kPageSize, MADV_DONTFORK), SyscallSucceeds());
// Verify that all pages are zeroed and memset the first, second and third
// pages to 1, 2, and 3 respectively.
ExpectAllMappingBytes(mp1, 0);
memset(mp1.ptr(), 1, kPageSize);
ExpectAllMappingBytes(mp2, 0);
memset(mp2.ptr(), 2, kPageSize);
ExpectAllMappingBytes(mp3, 0);
memset(mp3.ptr(), 3, kPageSize);
const auto rest = [&] {
// Verify first page is mapped, verify its contents and then modify the
// page. The mapping is private so the modifications are not visible to
// the parent.
TEST_CHECK(IsMapped(mp1.addr()));
ExpectAllMappingBytes(mp1, 1);
memset(mp1.ptr(), 11, kPageSize);
ExpectAllMappingBytes(mp1, 11);
// Verify second page is not mapped.
TEST_CHECK(!IsMapped(mp2.addr()));
// Verify third page is mapped, verify its contents and then modify the
// page. The mapping is private so the modifications are not visible to
// the parent.
TEST_CHECK(IsMapped(mp3.addr()));
ExpectAllMappingBytes(mp3, 3);
memset(mp3.ptr(), 13, kPageSize);
ExpectAllMappingBytes(mp3, 13);
};
EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
// The fork and COW by child should not affect the parent mappings.
ExpectAllMappingBytes(mp1, 1);
ExpectAllMappingBytes(mp2, 2);
ExpectAllMappingBytes(mp3, 3);
}
} // namespace
} // namespace testing
} // namespace gvisor
|