test-jit.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <sys/mman.h>
  6. #include <sys/user.h>
  7. #include <unistd.h>
  8. int fib(int n)
  9. {
  10. int first = 0, second = 1, next = 0, i = 0;
  11. while(i <= n)
  12. {
  13. if(i < 2)
  14. {
  15. next = i;
  16. }
  17. else
  18. {
  19. next = first + second;
  20. first = second;
  21. second = next;
  22. }
  23. i++;
  24. }
  25. return next;
  26. }
  27. int pass_test()
  28. {
  29. return 0x42;
  30. }
  31. void fatal(char *msg)
  32. {
  33. fprintf(stderr, "*** FATAL ERROR: %s\n", (msg ? msg : "no message"));
  34. fflush(stderr);
  35. abort();
  36. }
  37. void test_shared()
  38. {
  39. static char filename[] = "/tmp/DoubleMapXXXXXX";
  40. int fd = mkstemp(filename);
  41. if(fd == -1)
  42. {
  43. fatal("mkstemp");
  44. }
  45. if(ftruncate(fd, PAGE_SIZE) == -1)
  46. {
  47. fatal("ftruncate");
  48. }
  49. uint8_t *const write_addr = mmap(0, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  50. uint8_t *const exec_addr = mmap(write_addr+PAGE_SIZE, PAGE_SIZE,
  51. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_FIXED, fd, 0);
  52. if(write_addr == MAP_FAILED || exec_addr == MAP_FAILED)
  53. {
  54. fatal("mmap");
  55. }
  56. size_t size = PAGE_SIZE;
  57. memcpy(write_addr, fib, size);
  58. int (*fun_pointer)() = (void*)exec_addr;
  59. // Give the JIT something to potentially cache
  60. for(int i = 0; i < 15000; i++)
  61. {
  62. if(fun_pointer(20) != 6765)
  63. {
  64. fatal("fibonacci");
  65. }
  66. }
  67. memcpy(write_addr, pass_test, size);
  68. if(fun_pointer() == 0x42)
  69. {
  70. printf("test_shared passed\n");
  71. }
  72. munmap(write_addr, 2 * size);
  73. munmap(exec_addr, size);
  74. }
  75. void test_consecutive()
  76. {
  77. uint8_t *const page0 = mmap(NULL,
  78. 2 * PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  79. // throwaway mmap to reduce likelhood of page0 and page1 mapping to consecutive physical frames
  80. uint8_t *const throwaway = mmap(NULL,
  81. PAGE_SIZE, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  82. uint8_t *const page1 = mmap(page0 + PAGE_SIZE, PAGE_SIZE,
  83. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  84. if(page0 == MAP_FAILED || throwaway == MAP_FAILED || page1 == MAP_FAILED)
  85. {
  86. fatal("mmap");
  87. }
  88. // Attempt to influence virtual to physical mapping - we want page0->page1 to not be contiguous
  89. // physically
  90. page0[0] = 0;
  91. throwaway[0] = 0;
  92. page1[0] = 0;
  93. for(int32_t i = 0; i < 100; i++)
  94. {
  95. uint8_t* start = (uint8_t*)(page1 - i - 1);
  96. memcpy(start, fib, PAGE_SIZE);
  97. int (*fun_pointer)() = (void*)start;
  98. for(int j = 0; j < 15000; j++)
  99. {
  100. if(fun_pointer(20) != 6765)
  101. {
  102. fatal("fibonacci");
  103. }
  104. }
  105. }
  106. printf("test_consecutive passed\n");
  107. }
  108. void test_consecutive_written()
  109. {
  110. uint8_t *const page0 = mmap(NULL,
  111. 2 * PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  112. // throwaway mmap to reduce likelhood of page0 and page1 mapping to consecutive physical frames
  113. uint8_t *const throwaway = mmap(NULL,
  114. PAGE_SIZE, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  115. uint8_t *const page1 = mmap(page0 + PAGE_SIZE, PAGE_SIZE,
  116. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  117. if(page0 == MAP_FAILED || throwaway == MAP_FAILED || page1 == MAP_FAILED)
  118. {
  119. fatal("mmap");
  120. }
  121. // Attempt to influence virtual to physical mapping - we want page0->page1 to not be contiguous
  122. // physically
  123. page0[0] = 0;
  124. throwaway[0] = 0;
  125. page1[0] = 0;
  126. uint8_t* start = page1 - 8;
  127. uint8_t* ptr = start;
  128. const int32_t INC_COUNT = 16;
  129. // xor eax, eax
  130. *ptr++ = 0x31;
  131. *ptr++ = 0xc0;
  132. for(int i = 0; i < INC_COUNT; i++)
  133. {
  134. // inc eax
  135. *ptr++ = 0x40;
  136. }
  137. // ret
  138. *ptr++ = 0xC3;
  139. int (*fun_pointer)() = (void*)start;
  140. for(int i = 0; i < 15000; i++)
  141. {
  142. int32_t result = fun_pointer();
  143. if(result != INC_COUNT)
  144. {
  145. fatal("test_consecutive_written");
  146. }
  147. }
  148. // overwrite one INC at the start of the second page with a NOP
  149. *page1 = 0x90;
  150. int32_t result = fun_pointer();
  151. if(result != INC_COUNT - 1)
  152. {
  153. fatal("test_consecutive_written after overwrite");
  154. }
  155. printf("test_consecutive_written passed\n");
  156. }
  157. int main()
  158. {
  159. test_shared();
  160. // disabled for now, takes long and not sure if it actually catches bugs
  161. //test_consecutive();
  162. test_consecutive_written();
  163. return 0;
  164. }