mprotect-multi-region-mprotect.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <AK/Types.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/mman.h>
  12. #include <unistd.h>
  13. int main()
  14. {
  15. printf("Testing full unnmap\n");
  16. auto* map1 = mmap(nullptr, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, 0, 0);
  17. if (map1 == MAP_FAILED) {
  18. perror("mmap 1");
  19. return 1;
  20. }
  21. auto* map2 = mmap((void*)((FlatPtr)map1 + 2 * PAGE_SIZE), 2 * PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, 0, 0);
  22. if (map2 == MAP_FAILED) {
  23. perror("mmap 2");
  24. return 1;
  25. }
  26. auto* map3 = mmap((void*)((FlatPtr)map1 + 4 * PAGE_SIZE), 2 * PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, 0, 0);
  27. if (map3 == MAP_FAILED) {
  28. perror("mmap 3");
  29. return 1;
  30. }
  31. // really allocating pages
  32. memset(map1, 0x01, 6 * PAGE_SIZE);
  33. int rc;
  34. outln("Mprotect 3 ranges [2, 2 ,2]");
  35. rc = mprotect(map1, 6 * PAGE_SIZE, PROT_READ);
  36. if (rc) {
  37. perror("mprotect full");
  38. return 1;
  39. }
  40. outln("Mprotect 3 ranges [-1, 2 ,1-]");
  41. rc = mprotect((void*)((FlatPtr)map1 + PAGE_SIZE), 4 * PAGE_SIZE, PROT_READ);
  42. if (rc) {
  43. perror("mprotect partial");
  44. return 1;
  45. }
  46. outln("unmapping");
  47. munmap(map2, 2 * PAGE_SIZE);
  48. outln("Mprotect 2 ranges [2, -- ,2] -> Error");
  49. rc = mprotect(map1, 6 * PAGE_SIZE, PROT_READ);
  50. if (!rc) {
  51. perror("mprotect full over missing succeeded");
  52. return 1;
  53. }
  54. outln("Mprotect 3 ranges [-1, -- ,1-] -> Error");
  55. rc = mprotect((void*)((FlatPtr)map1 + PAGE_SIZE), 4 * PAGE_SIZE, PROT_READ);
  56. if (!rc) {
  57. perror("mprotect partial over missing succeeded");
  58. return 1;
  59. }
  60. //cleanup
  61. munmap(map1, 6 * PAGE_SIZE);
  62. outln("PASS");
  63. return 0;
  64. }