protect.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "protect.h"
  2. #include <util/generic/yexception.h>
  3. #include <util/generic/string.h>
  4. #include <util/stream/output.h>
  5. #include "yassert.h"
  6. #if defined(_unix_) || defined(_darwin_)
  7. #include <sys/mman.h>
  8. #endif
  9. #ifdef _win_
  10. #include <Windows.h>
  11. #endif
  12. static TString ModeToString(const EProtectMemory mode) {
  13. TString strMode;
  14. if (mode == PM_NONE) {
  15. return "PM_NONE";
  16. }
  17. if (mode & PM_READ) {
  18. strMode += "PM_READ|";
  19. }
  20. if (mode & PM_WRITE) {
  21. strMode += "PM_WRITE|";
  22. }
  23. if (mode & PM_EXEC) {
  24. strMode += "PM_EXEC|";
  25. }
  26. return strMode.substr(0, strMode.size() - 1);
  27. }
  28. void ProtectMemory(void* addr, const size_t length, const EProtectMemory mode) {
  29. Y_VERIFY(!(mode & ~(PM_READ | PM_WRITE | PM_EXEC)), "Invalid memory protection flag combination. ");
  30. #if defined(_unix_) || defined(_darwin_)
  31. int mpMode = PROT_NONE;
  32. if (mode & PM_READ) {
  33. mpMode |= PROT_READ;
  34. }
  35. if (mode & PM_WRITE) {
  36. mpMode |= PROT_WRITE;
  37. }
  38. if (mode & PM_EXEC) {
  39. mpMode |= PROT_EXEC;
  40. }
  41. // some old manpages for mprotect say 'const void* addr', but that's wrong
  42. if (mprotect(addr, length, mpMode) == -1) {
  43. ythrow TSystemError() << "Memory protection failed for mode " << ModeToString(mode) << ". ";
  44. }
  45. #endif
  46. #ifdef _win_
  47. DWORD mpMode = PAGE_NOACCESS;
  48. // windows developers are not aware of bit flags :(
  49. /*
  50. * It's unclear that we should NOT fail on Windows that does not support write-only
  51. * memory protection. As we don't know, what behavior is more correct, we choose
  52. * one of them. A discussion was here: REVIEW: 39725
  53. */
  54. switch (mode.ToBaseType()) {
  55. case PM_READ:
  56. mpMode = PAGE_READONLY;
  57. break;
  58. case PM_WRITE:
  59. mpMode = PAGE_READWRITE;
  60. break; // BUG: no write-only support
  61. /*case PM_WRITE:
  62. ythrow TSystemError() << "Write-only protection mode is not supported under Windows. ";*/
  63. case PM_READ | PM_WRITE:
  64. mpMode = PAGE_READWRITE;
  65. break;
  66. case PM_EXEC:
  67. mpMode = PAGE_EXECUTE;
  68. break;
  69. case PM_READ | PM_EXEC:
  70. mpMode = PAGE_EXECUTE_READ;
  71. break;
  72. case PM_WRITE | PM_EXEC:
  73. mpMode = PAGE_EXECUTE_READWRITE;
  74. break; // BUG: no write-only support
  75. /*case PM_WRITE | PM_EXEC:
  76. ythrow TSystemError() << "Write-execute-only protection mode is not supported under Windows. ";*/
  77. case PM_READ | PM_WRITE | PM_EXEC:
  78. mpMode = PAGE_EXECUTE_READWRITE;
  79. break;
  80. }
  81. DWORD oldMode = 0;
  82. if (!VirtualProtect(addr, length, mpMode, &oldMode))
  83. ythrow TSystemError() << "Memory protection failed for mode " << ModeToString(mode) << ". ";
  84. #endif
  85. }