Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <Windows.h>
  4.  
  5. static int enable_large_pages(void)
  6. {
  7. HANDLE token = INVALID_HANDLE_VALUE;
  8. TOKEN_PRIVILEGES priv = { 0 };
  9. int ret = 1;
  10.  
  11. if (!(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)))
  12. goto error;
  13.  
  14. if (!(LookupPrivilegeValue(0, SE_LOCK_MEMORY_NAME, &priv.Privileges[0].Luid)))
  15. goto error;
  16.  
  17. priv.PrivilegeCount = 1;
  18. priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  19.  
  20. if (!(AdjustTokenPrivileges(token, FALSE, &priv, 0, 0, 0)))
  21. goto error;
  22.  
  23. ret = 0;
  24. error:
  25. CloseHandle(token);
  26. return ret;
  27. }
  28.  
  29. static void print_last_error(const char *msg)
  30. {
  31. const DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
  32. const DWORD langid = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
  33. char buf[256];
  34.  
  35. FormatMessageA(flags, 0, GetLastError(), langid, buf, sizeof(buf), 0);
  36. fprintf(stderr, "%s: %s\n", msg, buf);
  37. }
  38.  
  39. int main()
  40. {
  41. const int num_tries = 1000000;
  42.  
  43. HANDLE current_process = GetCurrentProcess();
  44. SIZE_T large_page_minimum = 0;
  45. LPVOID page = 0;
  46. int found = 0;
  47. int i, j;
  48.  
  49. if (enable_large_pages()) {
  50. print_last_error("could not enable large pages");
  51. return 1;
  52. }
  53.  
  54. large_page_minimum = GetLargePageMinimum();
  55.  
  56. for (i = 0; i < num_tries; ++i) {
  57. const DWORD large_page_flags = MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES;
  58. char buffer[256];
  59.  
  60. page = VirtualAllocEx(current_process, 0, large_page_minimum, large_page_flags, PAGE_READWRITE);
  61. if (!page) {
  62. print_last_error("could not allocate page");
  63. return 1;
  64. }
  65.  
  66. memcpy(buffer, page, sizeof(buffer));
  67. for (j = 0; j < sizeof(buffer); ++j) {
  68. if (buffer[j]) {
  69. int refreshed;
  70.  
  71. Sleep(1);
  72. refreshed = ((char *)page)[j];
  73.  
  74. printf("stale data at offset %d: %d\n", j, buffer[j]);
  75. printf("1 ms later the data is: %d\n", refreshed);
  76. printf("iterations taken: %d\n", i);
  77. found = 1;
  78. break;
  79. }
  80. }
  81. if (found)
  82. break;
  83.  
  84. memset(page, i % 256, sizeof(buffer));
  85. if (VirtualFreeEx(current_process, page, 0, MEM_RELEASE) == FALSE) {
  86. print_last_error("could not release page");
  87. return 1;
  88. }
  89. }
  90.  
  91. if (!found)
  92. puts("bug not reproduced\n");
  93.  
  94. if (page)
  95. VirtualFreeEx(current_process, page, 0, MEM_RELEASE);
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement