summaryrefslogtreecommitdiff
path: root/runtime/jit/jit_memory_region_test.cc
blob: bf943f99815d4a7435a189a0f9b77c565c3f694a (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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
 * Copyright (C) 2019 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 "jit/jit_memory_region.h"

#include <signal.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>

#include <android-base/unique_fd.h>
#include <gtest/gtest.h>

#include "base/globals.h"
#include "base/memfd.h"
#include "base/utils.h"
#include "common_runtime_test.h"

namespace art {
namespace jit {

// These tests only run on bionic.
#if defined(__BIONIC__)
static constexpr int kReturnFromFault = 42;

// These globals are only set in child processes.
void* gAddrToFaultOn = nullptr;

[[noreturn]] void handler([[maybe_unused]] int, siginfo_t* info, [[maybe_unused]] void*) {
  CHECK_EQ(info->si_addr, gAddrToFaultOn);
  exit(kReturnFromFault);
}

static void registerSignalHandler() {
  struct sigaction sa;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = SA_SIGINFO;
  sa.sa_sigaction = handler;
  sigaction(SIGSEGV, &sa, nullptr);
}

class TestZygoteMemory : public testing::Test {
 public:
  void BasicTest() {
    // Zygote JIT memory only works on kernels that don't segfault on flush.
    TEST_DISABLED_FOR_KERNELS_WITH_CACHE_SEGFAULT();
    std::string error_msg;
    const size_t page_size = GetPageSizeSlow();
    android::base::unique_fd fd(JitMemoryRegion::CreateZygoteMemory(page_size, &error_msg));
    CHECK_NE(fd.get(), -1);

    // Create a writable mapping.
    int32_t* addr = reinterpret_cast<int32_t*>(
        mmap(nullptr, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0));
    CHECK(addr != nullptr);
    CHECK_NE(addr, MAP_FAILED);

    // Test that we can write into the mapping.
    addr[0] = 42;
    CHECK_EQ(addr[0], 42);

    // Protect the memory.
    bool res = JitMemoryRegion::ProtectZygoteMemory(fd.get(), &error_msg);
    CHECK(res);

    // Test that we can still write into the mapping.
    addr[0] = 2;
    CHECK_EQ(addr[0], 2);

    // Test that we cannot create another writable mapping.
    int32_t* addr2 = reinterpret_cast<int32_t*>(
        mmap(nullptr, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0));
    CHECK_EQ(addr2, MAP_FAILED);

    // With the existing mapping, we can toggle read/write.
    CHECK_EQ(mprotect(addr, page_size, PROT_READ), 0) << strerror(errno);
    CHECK_EQ(mprotect(addr, page_size, PROT_READ | PROT_WRITE), 0) << strerror(errno);

    // Test mremap with old_size = 0. From the man pages:
    //    If the value of old_size is zero, and old_address refers to a shareable mapping
    //    (see mmap(2) MAP_SHARED), then mremap() will create a new mapping of the same pages.
    addr2 = reinterpret_cast<int32_t*>(mremap(addr, 0, page_size, MREMAP_MAYMOVE));
    CHECK_NE(addr2, MAP_FAILED);

    // Test that we can  write into the remapped mapping.
    addr2[0] = 3;
    CHECK_EQ(addr2[0], 3);

    addr2 = reinterpret_cast<int32_t*>(mremap(addr, page_size, 2 * page_size, MREMAP_MAYMOVE));
    CHECK_NE(addr2, MAP_FAILED);

    // Test that we can  write into the remapped mapping.
    addr2[0] = 4;
    CHECK_EQ(addr2[0], 4);
  }

  void TestUnmapWritableAfterFork() {
    // Zygote JIT memory only works on kernels that don't segfault on flush.
    TEST_DISABLED_FOR_KERNELS_WITH_CACHE_SEGFAULT();
    std::string error_msg;
    const size_t page_size = GetPageSizeSlow();
    int32_t* addr = nullptr;
    int32_t* addr2 = nullptr;
    {
      android::base::unique_fd fd(JitMemoryRegion::CreateZygoteMemory(page_size, &error_msg));
      CHECK_NE(fd.get(), -1);

      // Create a writable mapping.
      addr = reinterpret_cast<int32_t*>(
          mmap(nullptr, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0));
      CHECK(addr != nullptr);
      CHECK_NE(addr, MAP_FAILED);

      // Test that we can write into the mapping.
      addr[0] = 42;
      CHECK_EQ(addr[0], 42);

      // Create a read-only mapping.
      addr2 = reinterpret_cast<int32_t*>(
          mmap(nullptr, page_size, PROT_READ, MAP_SHARED, fd.get(), 0));
      CHECK(addr2 != nullptr);

      // Protect the memory.
      bool res = JitMemoryRegion::ProtectZygoteMemory(fd.get(), &error_msg);
      CHECK(res);
    }
    // At this point, the fd has been dropped, but the memory mappings are still
    // there.

    // Create a mapping of atomic ints to communicate between processes.
    android::base::unique_fd fd2(JitMemoryRegion::CreateZygoteMemory(page_size, &error_msg));
    CHECK_NE(fd2.get(), -1);
    std::atomic<int32_t>* shared = reinterpret_cast<std::atomic<int32_t>*>(
        mmap(nullptr, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd2.get(), 0));

    // Values used for the tests below.
    const int32_t parent_value = 66;
    const int32_t child_value = 33;
    const int32_t starting_value = 22;

    shared[0] = 0;
    addr[0] = starting_value;
    CHECK_EQ(addr[0], starting_value);
    CHECK_EQ(addr2[0], starting_value);
    pid_t pid = fork();
    if (pid == 0) {
      // Test that we can write into the mapping.
      addr[0] = child_value;
      CHECK_EQ(addr[0], child_value);
      CHECK_EQ(addr2[0], child_value);

      // Unmap the writable mappping.
      munmap(addr, page_size);

      CHECK_EQ(addr2[0], child_value);

      // Notify parent process.
      shared[0] = 1;

      // Wait for parent process for a new value.
      while (shared[0] != 2) {
        sched_yield();
      }
      CHECK_EQ(addr2[0], parent_value);

      // Test that we cannot write into the mapping. The signal handler will
      // exit the process.
      gAddrToFaultOn = addr;
      registerSignalHandler();
      // This write will trigger a fault, as `addr` is unmapped.
      addr[0] = child_value + 1;
      exit(0);
    } else {
      while (shared[0] != 1) {
        sched_yield();
      }
      CHECK_EQ(addr[0], child_value);
      CHECK_EQ(addr2[0], child_value);
      addr[0] = parent_value;
      // Notify the child if the new value.
      shared[0] = 2;
      int status;
      CHECK_EQ(waitpid(pid, &status, 0), pid);
      CHECK(WIFEXITED(status)) << strerror(errno);
      CHECK_EQ(WEXITSTATUS(status), kReturnFromFault);
      CHECK_EQ(addr[0], parent_value);
      CHECK_EQ(addr2[0], parent_value);
      munmap(addr, page_size);
      munmap(addr2, page_size);
      munmap(shared, page_size);
    }
  }

  void TestMadviseDontFork() {
    // Zygote JIT memory only works on kernels that don't segfault on flush.
    TEST_DISABLED_FOR_KERNELS_WITH_CACHE_SEGFAULT();
    std::string error_msg;
    const size_t page_size = GetPageSizeSlow();
    int32_t* addr = nullptr;
    int32_t* addr2 = nullptr;
    {
      android::base::unique_fd fd(JitMemoryRegion::CreateZygoteMemory(page_size, &error_msg));
      CHECK_NE(fd.get(), -1);

      // Create a writable mapping.
      addr = reinterpret_cast<int32_t*>(
          mmap(nullptr, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0));
      CHECK(addr != nullptr);
      CHECK_NE(addr, MAP_FAILED);
      CHECK_EQ(madvise(addr, page_size, MADV_DONTFORK), 0);

      // Test that we can write into the mapping.
      addr[0] = 42;
      CHECK_EQ(addr[0], 42);

      // Create a read-only mapping.
      addr2 = reinterpret_cast<int32_t*>(
          mmap(nullptr, page_size, PROT_READ, MAP_SHARED, fd.get(), 0));
      CHECK(addr2 != nullptr);

      // Protect the memory.
      bool res = JitMemoryRegion::ProtectZygoteMemory(fd.get(), &error_msg);
      CHECK(res);
    }
    // At this point, the fd has been dropped, but the memory mappings are still
    // there.

    // Create a mapping of atomic ints to communicate between processes.
    android::base::unique_fd fd2(JitMemoryRegion::CreateZygoteMemory(page_size, &error_msg));
    CHECK_NE(fd2.get(), -1);
    std::atomic<int32_t>* shared = reinterpret_cast<std::atomic<int32_t>*>(
        mmap(nullptr, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd2.get(), 0));

    // Values used for the tests below.
    const int32_t parent_value = 66;
    const int32_t child_value = 33;
    const int32_t starting_value = 22;

    shared[0] = 0;
    addr[0] = starting_value;
    CHECK_EQ(addr[0], starting_value);
    CHECK_EQ(addr2[0], starting_value);
    pid_t pid = fork();
    if (pid == 0) {
      CHECK_EQ(addr2[0], starting_value);

      // Notify parent process.
      shared[0] = 1;

      // Wait for parent process for new value.
      while (shared[0] != 2) {
        sched_yield();
      }

      CHECK_EQ(addr2[0], parent_value);
      // Test that we cannot write into the mapping. The signal handler will
      // exit the process.
      gAddrToFaultOn = addr;
      registerSignalHandler();
      addr[0] = child_value + 1;
      exit(0);
    } else {
      while (shared[0] != 1) {
        sched_yield();
      }
      CHECK_EQ(addr[0], starting_value);
      CHECK_EQ(addr2[0], starting_value);
      addr[0] = parent_value;
      // Notify the child of the new value.
      shared[0] = 2;
      int status;
      CHECK_EQ(waitpid(pid, &status, 0), pid);
      CHECK(WIFEXITED(status)) << strerror(errno);
      CHECK_EQ(WEXITSTATUS(status), kReturnFromFault);
      CHECK_EQ(addr[0], parent_value);
      CHECK_EQ(addr2[0], parent_value);

      munmap(addr, page_size);
      munmap(addr2, page_size);
      munmap(shared, page_size);
    }
  }

  // This code is testing some behavior that ART could potentially use: get a
  // copy-on-write mapping that can incorporate changes from a shared mapping
  // owned by another process.
  void TestFromSharedToPrivate() {
    // Zygote JIT memory only works on kernels that don't segfault on flush.
    TEST_DISABLED_FOR_KERNELS_WITH_CACHE_SEGFAULT();
    // This test is only for memfd with future write sealing support:
    // 1) ashmem with PROT_READ doesn't permit mapping MAP_PRIVATE | PROT_WRITE
    // 2) ashmem mapped MAP_PRIVATE discards the contents already written.
    if (!art::IsSealFutureWriteSupported()) {
      return;
    }
    std::string error_msg;
    const size_t page_size = GetPageSizeSlow();
    int32_t* addr = nullptr;
    android::base::unique_fd fd(JitMemoryRegion::CreateZygoteMemory(page_size, &error_msg));
    CHECK_NE(fd.get(), -1);

    // Create a writable mapping.
    addr = reinterpret_cast<int32_t*>(
        mmap(nullptr, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0));
    CHECK(addr != nullptr);
    CHECK_NE(addr, MAP_FAILED);

    // Test that we can write into the mapping.
    addr[0] = 42;
    CHECK_EQ(addr[0], 42);

    // Create another mapping of atomic ints to communicate between processes.
    android::base::unique_fd fd2(JitMemoryRegion::CreateZygoteMemory(page_size, &error_msg));
    CHECK_NE(fd2.get(), -1);
    std::atomic<int32_t>* shared = reinterpret_cast<std::atomic<int32_t>*>(
        mmap(nullptr, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd2.get(), 0));

    // Protect the memory.
    CHECK(JitMemoryRegion::ProtectZygoteMemory(fd.get(), &error_msg));

    // Values used for the tests below.
    const int32_t parent_value = 66;
    const int32_t child_value = 33;
    const int32_t starting_value = 22;

    // Check that updates done by a child mapping write-private are not visible
    // to the parent.
    addr[0] = starting_value;
    shared[0] = 0;
    pid_t pid = fork();
    if (pid == 0) {
      CHECK_EQ(mmap(addr, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd.get(), 0),
               addr);
      addr[0] = child_value;
      exit(0);
    } else {
      int status;
      CHECK_EQ(waitpid(pid, &status, 0), pid);
      CHECK(WIFEXITED(status)) << strerror(errno);
      CHECK_EQ(addr[0], starting_value);
    }

    addr[0] = starting_value;
    shared[0] = 0;

    // Check getting back and forth on shared mapping.
    pid = fork();
    if (pid == 0) {
      // Map it private with write access. MAP_FIXED will replace the existing
      // mapping.
      CHECK_EQ(mmap(addr, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd.get(), 0),
               addr);
      addr[0] = child_value;
      CHECK_EQ(addr[0], child_value);

      // Check that mapping shared with write access fails.
      CHECK_EQ(mmap(addr, page_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd.get(), 0),
               MAP_FAILED);
      CHECK_EQ(errno, EPERM);

      // Map shared with read access.
      CHECK_EQ(mmap(addr, page_size, PROT_READ, MAP_SHARED | MAP_FIXED, fd.get(), 0), addr);
      CHECK_NE(addr[0], child_value);

      // Wait for the parent to notify.
      while (shared[0] != 1) {
        sched_yield();
      }
      CHECK_EQ(addr[0], parent_value);

      // Notify the parent for getting a new update of the buffer.
      shared[0] = 2;

      // Map it private again.
      CHECK_EQ(mmap(addr, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd.get(), 0),
               addr);
      addr[0] = child_value + 1;
      CHECK_EQ(addr[0], child_value + 1);

      // And map it back shared.
      CHECK_EQ(mmap(addr, page_size, PROT_READ, MAP_SHARED | MAP_FIXED, fd.get(), 0), addr);
      while (shared[0] != 3) {
        sched_yield();
      }
      CHECK_EQ(addr[0], parent_value + 1);
      exit(0);
    } else {
      addr[0] = parent_value;
      CHECK_EQ(addr[0], parent_value);

      // Notify the child of the new value.
      shared[0] = 1;

      // Wait for the child to ask for a new value;
      while (shared[0] != 2) {
        sched_yield();
      }
      addr[0] = parent_value + 1;
      CHECK_EQ(addr[0], parent_value + 1);

      // Notify the child of a new value.
      shared[0] = 3;
      int status;
      CHECK_EQ(waitpid(pid, &status, 0), pid);
      CHECK(WIFEXITED(status)) << strerror(errno);
      CHECK_EQ(addr[0], parent_value + 1);
    }

    // Check that updates done by the parent are visible after a new mmap
    // write-private.
    shared[0] = 0;
    addr[0] = starting_value;
    pid = fork();
    if (pid == 0) {
      CHECK_EQ(mmap(addr, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd.get(), 0),
               addr);
      CHECK_EQ(addr[0], starting_value);
      addr[0] = child_value;
      CHECK_EQ(addr[0], child_value);

      // Notify the parent to update the buffer.
      shared[0] = 1;

      // Wait for the parent update.
      while (shared[0] != 2) {
        sched_yield();
      }
      // Test the buffer still contains our own data, and not the parent's.
      CHECK_EQ(addr[0], child_value);

      // Test the buffer contains the parent data after a new mmap.
      CHECK_EQ(mmap(addr, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd.get(), 0),
               addr);
      CHECK_EQ(addr[0], parent_value);
      exit(0);
    } else {
      // Wait for the child to start
      while (shared[0] != 1) {
        sched_yield();
      }
      CHECK_EQ(addr[0], starting_value);
      addr[0] = parent_value;
      // Notify the child that the buffer has been written.
      shared[0] = 2;
      int status;
      CHECK_EQ(waitpid(pid, &status, 0), pid);
      CHECK(WIFEXITED(status)) << strerror(errno);
      CHECK_EQ(addr[0], parent_value);
    }

    // Check that updates done by the parent are visible for a new mmap
    // write-private that hasn't written to the buffer yet.
    shared[0] = 0;
    addr[0] = starting_value;
    pid = fork();
    if (pid == 0) {
      CHECK_EQ(mmap(addr, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd.get(), 0),
               addr);
      CHECK_EQ(addr[0], starting_value);
      // Notify the parent for a new update of the buffer.
      shared[0] = 1;
      while (addr[0] != parent_value) {
        sched_yield();
      }
      addr[0] = child_value;
      CHECK_EQ(addr[0], child_value);
      exit(0);
    } else {
      while (shared[0] != 1) {
        sched_yield();
      }
      CHECK_EQ(addr[0], starting_value);
      addr[0] = parent_value;
      int status;
      CHECK_EQ(waitpid(pid, &status, 0), pid);
      CHECK(WIFEXITED(status)) << strerror(errno);
      CHECK_EQ(addr[0], parent_value);
    }
    munmap(addr, page_size);
    munmap(shared, page_size);
  }

  // Test that a readable mapping created befire sealing future writes, can be
  // changed into a writable mapping.
  void TestVmMayWriteBefore() {
    // Zygote JIT memory only works on kernels that don't segfault on flush.
    TEST_DISABLED_FOR_KERNELS_WITH_CACHE_SEGFAULT();
    std::string error_msg;
    const size_t page_size = GetPageSizeSlow();
    int32_t* addr = nullptr;
    {
      android::base::unique_fd fd(JitMemoryRegion::CreateZygoteMemory(page_size, &error_msg));
      CHECK_NE(fd.get(), -1);

      // Create a shared readable mapping.
      addr = reinterpret_cast<int32_t*>(
          mmap(nullptr, page_size, PROT_READ, MAP_SHARED, fd.get(), 0));
      CHECK(addr != nullptr);
      CHECK_NE(addr, MAP_FAILED);

      // Protect the memory.
      bool res = JitMemoryRegion::ProtectZygoteMemory(fd.get(), &error_msg);
      CHECK(res);
    }
    // At this point, the fd has been dropped, but the memory mappings are still
    // there.
    int res = mprotect(addr, page_size, PROT_WRITE);
    CHECK_EQ(res, 0);
  }

  // Test that we cannot create a writable mapping after sealing future writes.
  void TestVmMayWriteAfter() {
    // Zygote JIT memory only works on kernels that don't segfault on flush.
    TEST_DISABLED_FOR_KERNELS_WITH_CACHE_SEGFAULT();
    std::string error_msg;
    const size_t page_size = GetPageSizeSlow();
    int32_t* addr = nullptr;
    {
      android::base::unique_fd fd(JitMemoryRegion::CreateZygoteMemory(page_size, &error_msg));
      CHECK_NE(fd.get(), -1);

      // Protect the memory.
      bool res = JitMemoryRegion::ProtectZygoteMemory(fd.get(), &error_msg);
      CHECK(res);

      // Create a shared readable mapping.
      addr = reinterpret_cast<int32_t*>(
          mmap(nullptr, page_size, PROT_READ, MAP_SHARED, fd.get(), 0));
      CHECK(addr != nullptr);
      CHECK_NE(addr, MAP_FAILED);
    }
    // At this point, the fd has been dropped, but the memory mappings are still
    // there.
    int res = mprotect(addr, page_size, PROT_WRITE);
    CHECK_EQ(res, -1);
    CHECK_EQ(errno, EACCES);
  }
};

TEST_F(TestZygoteMemory, BasicTest) {
  BasicTest();
}

TEST_F(TestZygoteMemory, TestUnmapWritableAfterFork) {
  TestUnmapWritableAfterFork();
}

TEST_F(TestZygoteMemory, TestMadviseDontFork) {
  TestMadviseDontFork();
}

TEST_F(TestZygoteMemory, TestFromSharedToPrivate) {
  TestFromSharedToPrivate();
}

TEST_F(TestZygoteMemory, TestVmMayWriteBefore) {
  TestVmMayWriteBefore();
}

TEST_F(TestZygoteMemory, TestVmMayWriteAfter) {
  TestVmMayWriteAfter();
}

#endif  // defined (__BIONIC__)

}  // namespace jit
}  // namespace art