on-fault-limit.c 842 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <sys/mman.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/time.h>
  7. #include <sys/resource.h>
  8. #ifndef MCL_ONFAULT
  9. #define MCL_ONFAULT (MCL_FUTURE << 1)
  10. #endif
  11. static int test_limit(void)
  12. {
  13. int ret = 1;
  14. struct rlimit lims;
  15. void *map;
  16. if (getrlimit(RLIMIT_MEMLOCK, &lims)) {
  17. perror("getrlimit");
  18. return ret;
  19. }
  20. if (mlockall(MCL_ONFAULT | MCL_FUTURE)) {
  21. perror("mlockall");
  22. return ret;
  23. }
  24. map = mmap(NULL, 2 * lims.rlim_max, PROT_READ | PROT_WRITE,
  25. MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
  26. if (map != MAP_FAILED)
  27. printf("mmap should have failed, but didn't\n");
  28. else {
  29. ret = 0;
  30. munmap(map, 2 * lims.rlim_max);
  31. }
  32. munlockall();
  33. return ret;
  34. }
  35. int main(int argc, char **argv)
  36. {
  37. int ret = 0;
  38. ret += test_limit();
  39. return ret;
  40. }