summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/mremap.cc
blob: 64e435cb7898c2a7c71b0133e1c58473d91a32ad (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// 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 <errno.h>
#include <string.h>
#include <sys/mman.h>

#include <string>

#include "gmock/gmock.h"
#include "absl/strings/string_view.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"

using ::testing::_;

namespace gvisor {
namespace testing {

namespace {

// Wrapper for mremap that returns a PosixErrorOr<>, since the return type of
// void* isn't directly compatible with SyscallSucceeds.
PosixErrorOr<void*> Mremap(void* old_address, size_t old_size, size_t new_size,
                           int flags, void* new_address) {
  void* rv = mremap(old_address, old_size, new_size, flags, new_address);
  if (rv == MAP_FAILED) {
    return PosixError(errno, "mremap failed");
  }
  return rv;
}

// Fixture for mremap tests parameterized by mmap flags.
using MremapParamTest = ::testing::TestWithParam<int>;

TEST_P(MremapParamTest, Noop) {
  Mapping const m =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, GetParam()));

  ASSERT_THAT(Mremap(m.ptr(), kPageSize, kPageSize, 0, nullptr),
              IsPosixErrorOkAndHolds(m.ptr()));
  EXPECT_TRUE(IsMapped(m.addr()));
}

TEST_P(MremapParamTest, InPlace_ShrinkingWholeVMA) {
  Mapping const m =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(2 * kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    // N.B. we must be in a single-threaded subprocess to ensure a
    // background thread doesn't concurrently map the second page.
    void* addr = mremap(m.ptr(), 2 * kPageSize, kPageSize, 0, nullptr);
    TEST_PCHECK_MSG(addr != MAP_FAILED, "mremap failed");
    TEST_CHECK(addr == m.ptr());
    MaybeSave();

    TEST_CHECK(IsMapped(m.addr()));
    TEST_CHECK(!IsMapped(m.addr() + kPageSize));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, InPlace_ShrinkingPartialVMA) {
  Mapping const m =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(3 * kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    void* addr = mremap(m.ptr(), 2 * kPageSize, kPageSize, 0, nullptr);
    TEST_PCHECK_MSG(addr != MAP_FAILED, "mremap failed");
    TEST_CHECK(addr == m.ptr());
    MaybeSave();

    TEST_CHECK(IsMapped(m.addr()));
    TEST_CHECK(!IsMapped(m.addr() + kPageSize));
    TEST_CHECK(IsMapped(m.addr() + 2 * kPageSize));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, InPlace_ShrinkingAcrossVMAs) {
  Mapping const m =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(3 * kPageSize, PROT_READ, GetParam()));
  // Changing permissions on the first page forces it to become a separate vma.
  ASSERT_THAT(mprotect(m.ptr(), kPageSize, PROT_NONE), SyscallSucceeds());

  const auto rest = [&] {
    // Both old_size and new_size now span two vmas; mremap
    // shouldn't care.
    void* addr = mremap(m.ptr(), 3 * kPageSize, 2 * kPageSize, 0, nullptr);
    TEST_PCHECK_MSG(addr != MAP_FAILED, "mremap failed");
    TEST_CHECK(addr == m.ptr());
    MaybeSave();

    TEST_CHECK(IsMapped(m.addr()));
    TEST_CHECK(IsMapped(m.addr() + kPageSize));
    TEST_CHECK(!IsMapped(m.addr() + 2 * kPageSize));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, InPlace_ExpansionSuccess) {
  Mapping const m =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(2 * kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    // Unmap the second page so that the first can be expanded back into it.
    //
    // N.B. we must be in a single-threaded subprocess to ensure a
    // background thread doesn't concurrently map this page.
    TEST_PCHECK(
        munmap(reinterpret_cast<void*>(m.addr() + kPageSize), kPageSize) == 0);
    MaybeSave();

    void* addr = mremap(m.ptr(), kPageSize, 2 * kPageSize, 0, nullptr);
    TEST_PCHECK_MSG(addr != MAP_FAILED, "mremap failed");
    TEST_CHECK(addr == m.ptr());
    MaybeSave();

    TEST_CHECK(IsMapped(m.addr()));
    TEST_CHECK(IsMapped(m.addr() + kPageSize));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, InPlace_ExpansionFailure) {
  Mapping const m =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(3 * kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    // Unmap the second page, leaving a one-page hole. Trying to expand the
    // first page to three pages should fail since the original third page
    // is still mapped.
    TEST_PCHECK(
        munmap(reinterpret_cast<void*>(m.addr() + kPageSize), kPageSize) == 0);
    MaybeSave();

    void* addr = mremap(m.ptr(), kPageSize, 3 * kPageSize, 0, nullptr);
    TEST_CHECK_MSG(addr == MAP_FAILED, "mremap unexpectedly succeeded");
    TEST_PCHECK_MSG(errno == ENOMEM, "mremap failed with wrong errno");
    MaybeSave();

    TEST_CHECK(IsMapped(m.addr()));
    TEST_CHECK(!IsMapped(m.addr() + kPageSize));
    TEST_CHECK(IsMapped(m.addr() + 2 * kPageSize));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, MayMove_Expansion) {
  Mapping const m =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(3 * kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    // Unmap the second page, leaving a one-page hole. Trying to expand the
    // first page to three pages with MREMAP_MAYMOVE should force the
    // mapping to be relocated since the original third page is still
    // mapped.
    TEST_PCHECK(
        munmap(reinterpret_cast<void*>(m.addr() + kPageSize), kPageSize) == 0);
    MaybeSave();

    void* addr2 =
        mremap(m.ptr(), kPageSize, 3 * kPageSize, MREMAP_MAYMOVE, nullptr);
    TEST_PCHECK_MSG(addr2 != MAP_FAILED, "mremap failed");
    MaybeSave();

    const Mapping m2 = Mapping(addr2, 3 * kPageSize);
    TEST_CHECK(m.addr() != m2.addr());

    TEST_CHECK(!IsMapped(m.addr()));
    TEST_CHECK(!IsMapped(m.addr() + kPageSize));
    TEST_CHECK(IsMapped(m.addr() + 2 * kPageSize));
    TEST_CHECK(IsMapped(m2.addr()));
    TEST_CHECK(IsMapped(m2.addr() + kPageSize));
    TEST_CHECK(IsMapped(m2.addr() + 2 * kPageSize));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, Fixed_SourceAndDestinationCannotOverlap) {
  Mapping const m =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, GetParam()));

  ASSERT_THAT(Mremap(m.ptr(), kPageSize, kPageSize,
                     MREMAP_MAYMOVE | MREMAP_FIXED, m.ptr()),
              PosixErrorIs(EINVAL, _));
  EXPECT_TRUE(IsMapped(m.addr()));
}

TEST_P(MremapParamTest, Fixed_SameSize) {
  Mapping const src =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, GetParam()));
  Mapping const dst =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    // Unmap dst to create a hole.
    TEST_PCHECK(munmap(dst.ptr(), kPageSize) == 0);
    MaybeSave();

    void* addr = mremap(src.ptr(), kPageSize, kPageSize,
                        MREMAP_MAYMOVE | MREMAP_FIXED, dst.ptr());
    TEST_PCHECK_MSG(addr != MAP_FAILED, "mremap failed");
    TEST_CHECK(addr == dst.ptr());
    MaybeSave();

    TEST_CHECK(!IsMapped(src.addr()));
    TEST_CHECK(IsMapped(dst.addr()));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, Fixed_SameSize_Unmapping) {
  // Like the Fixed_SameSize case, but expect mremap to unmap the destination
  // automatically.
  Mapping const src =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, GetParam()));
  Mapping const dst =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    void* addr = mremap(src.ptr(), kPageSize, kPageSize,
                        MREMAP_MAYMOVE | MREMAP_FIXED, dst.ptr());
    TEST_PCHECK_MSG(addr != MAP_FAILED, "mremap failed");
    TEST_CHECK(addr == dst.ptr());
    MaybeSave();

    TEST_CHECK(!IsMapped(src.addr()));
    TEST_CHECK(IsMapped(dst.addr()));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, Fixed_ShrinkingWholeVMA) {
  Mapping const src =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(2 * kPageSize, PROT_NONE, GetParam()));
  Mapping const dst =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(2 * kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    // Unmap dst so we can check that mremap does not keep the
    // second page.
    TEST_PCHECK(munmap(dst.ptr(), 2 * kPageSize) == 0);
    MaybeSave();

    void* addr = mremap(src.ptr(), 2 * kPageSize, kPageSize,
                        MREMAP_MAYMOVE | MREMAP_FIXED, dst.ptr());
    TEST_PCHECK_MSG(addr != MAP_FAILED, "mremap failed");
    TEST_CHECK(addr == dst.ptr());
    MaybeSave();

    TEST_CHECK(!IsMapped(src.addr()));
    TEST_CHECK(!IsMapped(src.addr() + kPageSize));
    TEST_CHECK(IsMapped(dst.addr()));
    TEST_CHECK(!IsMapped(dst.addr() + kPageSize));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, Fixed_ShrinkingPartialVMA) {
  Mapping const src =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(3 * kPageSize, PROT_NONE, GetParam()));
  Mapping const dst =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(2 * kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    // Unmap dst so we can check that mremap does not keep the
    // second page.
    TEST_PCHECK(munmap(dst.ptr(), 2 * kPageSize) == 0);
    MaybeSave();

    void* addr = mremap(src.ptr(), 2 * kPageSize, kPageSize,
                        MREMAP_MAYMOVE | MREMAP_FIXED, dst.ptr());
    TEST_PCHECK_MSG(addr != MAP_FAILED, "mremap failed");
    TEST_CHECK(addr == dst.ptr());
    MaybeSave();

    TEST_CHECK(!IsMapped(src.addr()));
    TEST_CHECK(!IsMapped(src.addr() + kPageSize));
    TEST_CHECK(IsMapped(src.addr() + 2 * kPageSize));
    TEST_CHECK(IsMapped(dst.addr()));
    TEST_CHECK(!IsMapped(dst.addr() + kPageSize));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, Fixed_ShrinkingAcrossVMAs) {
  Mapping const src =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(3 * kPageSize, PROT_READ, GetParam()));
  // Changing permissions on the first page forces it to become a separate vma.
  ASSERT_THAT(mprotect(src.ptr(), kPageSize, PROT_NONE), SyscallSucceeds());
  Mapping const dst =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(2 * kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    // Unlike flags=0, MREMAP_FIXED requires that [old_address,
    // old_address+new_size) only spans a single vma.
    void* addr = mremap(src.ptr(), 3 * kPageSize, 2 * kPageSize,
                        MREMAP_MAYMOVE | MREMAP_FIXED, dst.ptr());
    TEST_CHECK_MSG(addr == MAP_FAILED, "mremap unexpectedly succeeded");
    TEST_PCHECK_MSG(errno == EFAULT, "mremap failed with wrong errno");
    MaybeSave();

    TEST_CHECK(IsMapped(src.addr()));
    TEST_CHECK(IsMapped(src.addr() + kPageSize));
    // Despite failing, mremap should have unmapped [old_address+new_size,
    // old_address+old_size) (i.e. the third page).
    TEST_CHECK(!IsMapped(src.addr() + 2 * kPageSize));
    // Despite failing, mremap should have unmapped the destination pages.
    TEST_CHECK(!IsMapped(dst.addr()));
    TEST_CHECK(!IsMapped(dst.addr() + kPageSize));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST_P(MremapParamTest, Fixed_Expansion) {
  Mapping const src =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, GetParam()));
  Mapping const dst =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(2 * kPageSize, PROT_NONE, GetParam()));

  const auto rest = [&] {
    // Unmap dst so we can check that mremap actually maps all pages
    // at the destination.
    TEST_PCHECK(munmap(dst.ptr(), 2 * kPageSize) == 0);
    MaybeSave();

    void* addr = mremap(src.ptr(), kPageSize, 2 * kPageSize,
                        MREMAP_MAYMOVE | MREMAP_FIXED, dst.ptr());
    TEST_PCHECK_MSG(addr != MAP_FAILED, "mremap failed");
    TEST_CHECK(addr == dst.ptr());
    MaybeSave();

    TEST_CHECK(!IsMapped(src.addr()));
    TEST_CHECK(IsMapped(dst.addr()));
    TEST_CHECK(IsMapped(dst.addr() + kPageSize));
  };

  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

INSTANTIATE_TEST_SUITE_P(PrivateShared, MremapParamTest,
                         ::testing::Values(MAP_PRIVATE, MAP_SHARED));

// mremap with old_size == 0 only works with MAP_SHARED after Linux 4.14
// (dba58d3b8c50 "mm/mremap: fail map duplication attempts for private
// mappings").

TEST(MremapTest, InPlace_Copy) {
  Mapping const m =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, MAP_SHARED));
  EXPECT_THAT(Mremap(m.ptr(), 0, kPageSize, 0, nullptr),
              PosixErrorIs(ENOMEM, _));
}

TEST(MremapTest, MayMove_Copy) {
  Mapping const m =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, MAP_SHARED));

  // Remainder of this test executes in a subprocess to ensure that if mremap
  // incorrectly removes m, it is not remapped by another thread.
  const auto rest = [&] {
    void* ptr = mremap(m.ptr(), 0, kPageSize, MREMAP_MAYMOVE, nullptr);
    MaybeSave();
    TEST_PCHECK_MSG(ptr != MAP_FAILED, "mremap failed");
    TEST_CHECK(ptr != m.ptr());
    TEST_CHECK(IsMapped(m.addr()));
    TEST_CHECK(IsMapped(reinterpret_cast<uintptr_t>(ptr)));
  };
  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

TEST(MremapTest, MustMove_Copy) {
  Mapping const src =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, MAP_SHARED));
  Mapping const dst =
      ASSERT_NO_ERRNO_AND_VALUE(MmapAnon(kPageSize, PROT_NONE, MAP_PRIVATE));

  // Remainder of this test executes in a subprocess to ensure that if mremap
  // incorrectly removes src, it is not remapped by another thread.
  const auto rest = [&] {
    void* ptr = mremap(src.ptr(), 0, kPageSize, MREMAP_MAYMOVE | MREMAP_FIXED,
                       dst.ptr());
    MaybeSave();
    TEST_PCHECK_MSG(ptr != MAP_FAILED, "mremap failed");
    TEST_CHECK(ptr == dst.ptr());
    TEST_CHECK(IsMapped(src.addr()));
    TEST_CHECK(IsMapped(dst.addr()));
  };
  EXPECT_THAT(InForkedProcess(rest), IsPosixErrorOkAndHolds(0));
}

void ExpectAllBytesAre(absl::string_view v, char c) {
  for (size_t i = 0; i < v.size(); i++) {
    ASSERT_EQ(v[i], c) << "at offset " << i;
  }
}

TEST(MremapTest, ExpansionPreservesCOWPagesAndExposesNewFilePages) {
  // Create a file with 3 pages. The first is filled with 'a', the second is
  // filled with 'b', and the third is filled with 'c'.
  TempPath const file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  const FileDescriptor fd =
      ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR));
  ASSERT_THAT(WriteFd(fd.get(), std::string(kPageSize, 'a').c_str(), kPageSize),
              SyscallSucceedsWithValue(kPageSize));
  ASSERT_THAT(WriteFd(fd.get(), std::string(kPageSize, 'b').c_str(), kPageSize),
              SyscallSucceedsWithValue(kPageSize));
  ASSERT_THAT(WriteFd(fd.get(), std::string(kPageSize, 'c').c_str(), kPageSize),
              SyscallSucceedsWithValue(kPageSize));

  // Create a private mapping of the first 2 pages, and fill the second page
  // with 'd'.
  Mapping const src = ASSERT_NO_ERRNO_AND_VALUE(Mmap(nullptr, 2 * kPageSize,
                                                     PROT_READ | PROT_WRITE,
                                                     MAP_PRIVATE, fd.get(), 0));
  memset(reinterpret_cast<void*>(src.addr() + kPageSize), 'd', kPageSize);
  MaybeSave();

  // Move the mapping while expanding it to 3 pages. The resulting mapping
  // should contain the original first page of the file (filled with 'a'),
  // followed by the private copy of the second page (filled with 'd'), followed
  // by the newly-mapped third page of the file (filled with 'c').
  Mapping const dst = ASSERT_NO_ERRNO_AND_VALUE(
      MmapAnon(3 * kPageSize, PROT_NONE, MAP_PRIVATE));
  ASSERT_THAT(Mremap(src.ptr(), 2 * kPageSize, 3 * kPageSize,
                     MREMAP_MAYMOVE | MREMAP_FIXED, dst.ptr()),
              IsPosixErrorOkAndHolds(dst.ptr()));
  auto const v = dst.view();
  ExpectAllBytesAre(v.substr(0, kPageSize), 'a');
  ExpectAllBytesAre(v.substr(kPageSize, kPageSize), 'd');
  ExpectAllBytesAre(v.substr(2 * kPageSize, kPageSize), 'c');
}

TEST(MremapDeathTest, SharedAnon) {
  SetupGvisorDeathTest();

  // Reserve 4 pages of address space.
  Mapping const reserved = ASSERT_NO_ERRNO_AND_VALUE(
      MmapAnon(4 * kPageSize, PROT_NONE, MAP_PRIVATE));

  // Create a 2-page shared anonymous mapping at the beginning of the
  // reservation. Fill the first page with 'a' and the second with 'b'.
  Mapping const m = ASSERT_NO_ERRNO_AND_VALUE(
      Mmap(reserved.ptr(), 2 * kPageSize, PROT_READ | PROT_WRITE,
           MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0));
  memset(m.ptr(), 'a', kPageSize);
  memset(reinterpret_cast<void*>(m.addr() + kPageSize), 'b', kPageSize);
  MaybeSave();

  // Shrink the mapping to 1 page in-place.
  ASSERT_THAT(Mremap(m.ptr(), 2 * kPageSize, kPageSize, 0, m.ptr()),
              IsPosixErrorOkAndHolds(m.ptr()));

  // Expand the mapping to 3 pages, moving it forward by 1 page in the process
  // since the old and new mappings can't overlap.
  void* const new_m = reinterpret_cast<void*>(m.addr() + kPageSize);
  ASSERT_THAT(Mremap(m.ptr(), kPageSize, 3 * kPageSize,
                     MREMAP_MAYMOVE | MREMAP_FIXED, new_m),
              IsPosixErrorOkAndHolds(new_m));

  // The first 2 pages of the mapping should still contain the data we wrote
  // (i.e. shrinking should not have discarded the second page's data), while
  // touching the third page should raise SIGBUS.
  auto const v =
      absl::string_view(static_cast<char const*>(new_m), 3 * kPageSize);
  ExpectAllBytesAre(v.substr(0, kPageSize), 'a');
  ExpectAllBytesAre(v.substr(kPageSize, kPageSize), 'b');
  EXPECT_EXIT(ExpectAllBytesAre(v.substr(2 * kPageSize, kPageSize), '\0'),
              ::testing::KilledBySignal(SIGBUS), "");
}

}  // namespace

}  // namespace testing
}  // namespace gvisor