mincore.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #include "defaults.h"
  3. /**
  4. * Fills a vector that indicates whether pages of the calling process's virtual memory are resident in RAM. Each byte
  5. * in the vector contains the status of a single page. The page size can be obtained via the NSystemInfo::GetPageSize()
  6. * function. Use the IsPageInCore function to interpret the page status byte.
  7. *
  8. * Can be overly pessimistic:
  9. * - Assumes nothing is in RAM on platforms other than Linux
  10. * - Recent Linux kernels (4.21 and some backports) may return zeroes if the process doesn't have writing permissions
  11. * for the given file. See CVE-2019-5489.
  12. *
  13. * @param[in] addr starting address of the memory range to be examined
  14. * @param[in] len length (bytes) of the memory range to be examined
  15. * @param[out] vec vector of bytes to store statuses of memory pages
  16. * @param[in] vecLen length (bytes) of the vec, should be large enough to hold the requested pages count
  17. * @throws yexception if there was a system error or if the vecLen is too small
  18. *
  19. * @note this is only a snapshot, results may be stale by the time they're used
  20. * @see man 2 mincore
  21. */
  22. void InCoreMemory(const void* addr, size_t len, unsigned char* vec, size_t vecLen);
  23. /**
  24. * Takes as an argument an element of the vector previously filled by InCoreMemory.
  25. *
  26. * @param[in] byte corresponding to the status of a single page
  27. *
  28. * @returns true if this page was resident in memory at the time out the InCoreMemory execution
  29. */
  30. inline bool IsPageInCore(unsigned char s) {
  31. /* From mincore(2): On return, the least significant bit of each byte will be set if the corresponding page is
  32. * currently resident in memory, and be clear otherwise. (The settings of the other bits in each byte are
  33. * undefined; these bits are reserved for possible later use.)
  34. */
  35. return s & 1;
  36. }