droppable.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2024 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  4. */
  5. #include <assert.h>
  6. #include <stdbool.h>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <sys/mman.h>
  13. #include <linux/mman.h>
  14. #include "kselftest.h"
  15. int main(int argc, char *argv[])
  16. {
  17. size_t alloc_size = 134217728;
  18. size_t page_size = getpagesize();
  19. void *alloc;
  20. pid_t child;
  21. ksft_print_header();
  22. ksft_set_plan(1);
  23. alloc = mmap(0, alloc_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_DROPPABLE, -1, 0);
  24. assert(alloc != MAP_FAILED);
  25. memset(alloc, 'A', alloc_size);
  26. for (size_t i = 0; i < alloc_size; i += page_size)
  27. assert(*(uint8_t *)(alloc + i));
  28. child = fork();
  29. assert(child >= 0);
  30. if (!child) {
  31. for (;;)
  32. *(char *)malloc(page_size) = 'B';
  33. }
  34. for (bool done = false; !done;) {
  35. for (size_t i = 0; i < alloc_size; i += page_size) {
  36. if (!*(uint8_t *)(alloc + i)) {
  37. done = true;
  38. break;
  39. }
  40. }
  41. }
  42. kill(child, SIGTERM);
  43. ksft_test_result_pass("MAP_DROPPABLE: PASS\n");
  44. exit(KSFT_PASS);
  45. }