mempool_negative.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <string.h>
  2. #include <assert.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <setjmp.h>
  6. #include "vtv_malloc.h"
  7. #include "../../../include/vtv-change-permission.h"
  8. volatile static int signal_count = 0;
  9. sigjmp_buf before_segv;
  10. unsigned int vtv_debug = 0;
  11. static void
  12. handler(int sig, siginfo_t *si, void *unused)
  13. {
  14. signal_count++;
  15. /* You are not supposed to longjmp out of a signal handler but it seems
  16. to work for this test case and it simplifies it */
  17. siglongjmp(before_segv, 1);
  18. }
  19. /* Try to modify the memory pointed by "s" but dont actually change the values.
  20. Assumes and verifies the memory to be modified is mprotected */
  21. void mempoke(void * s, size_t n)
  22. {
  23. volatile char * p = (char *)s;
  24. int ret;
  25. signal_count = 0;
  26. ret = sigsetjmp(before_segv, 1);
  27. if (ret == 0)
  28. p[0] = p[0];
  29. assert(ret == 1 && signal_count == 1);
  30. ret = sigsetjmp(before_segv, 1);
  31. if (ret == 0)
  32. p[n - 1] = p[n - 1];
  33. assert(ret == 1 && signal_count == 2);
  34. }
  35. int main()
  36. {
  37. char * ptr;
  38. int size;
  39. /* Set up handler for SIGSEGV. */
  40. struct sigaction sa;
  41. sa.sa_flags = SA_SIGINFO;
  42. sigemptyset(&sa.sa_mask);
  43. sa.sa_sigaction = handler;
  44. if (sigaction(SIGSEGV, &sa, NULL) == -1)
  45. assert(0);
  46. /* Make the 'bookkeeping' vars read-write. */
  47. __VLTChangePermission (__VLTP_READ_WRITE);
  48. __vtv_malloc_init();
  49. size = 10;
  50. /* Verify not writable after unprotect */
  51. __vtv_malloc_unprotect();
  52. ptr = (char *)__vtv_malloc(size);
  53. memset(ptr, 'a', size);
  54. __vtv_malloc_protect();
  55. mempoke(ptr, size);
  56. __vtv_free(ptr);
  57. /* verify not-writable after protect, unprotect */
  58. __vtv_malloc_unprotect();
  59. ptr = (char *)__vtv_malloc(size);
  60. memset(ptr, 'a', size);
  61. __vtv_malloc_protect();
  62. __vtv_malloc_unprotect();
  63. memset(ptr, 'a', size);
  64. assert(ptr[size - 1] == 'a');
  65. __vtv_malloc_protect();
  66. assert(ptr[size - 1] == 'a');
  67. mempoke(ptr,size);
  68. __vtv_free(ptr);
  69. /* Allocate a bunch of small objects.
  70. Make sure the alignment is correct.
  71. Verify data has not been corrupted.
  72. Make sure the data cannot modified */
  73. {
  74. int s;
  75. for (s = 3; s < 28; s += 3)
  76. {
  77. size = s;
  78. {
  79. int i;
  80. #define ITERS 1000
  81. char * ptrs[ITERS];
  82. __vtv_malloc_unprotect();
  83. for (i = 0; i < ITERS; i++)
  84. {
  85. ptr = (char *)__vtv_malloc(size);
  86. assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0);
  87. memset(ptr, (i & 127), size);
  88. assert(ptr[size - 1] == (i & 127));
  89. ptrs[i] = ptr;
  90. }
  91. __vtv_malloc_protect();
  92. for (i = 0; i < ITERS; i++)
  93. mempoke(ptrs[i], size);
  94. __vtv_malloc_unprotect();
  95. for (i = 0; i < ITERS; i++)
  96. __vtv_free(ptrs[i]);
  97. __vtv_malloc_protect();
  98. }
  99. }
  100. }
  101. /* Allocate a bunch of medium size objects.
  102. Make sure the alignment is correct.
  103. Verify data has not been corrupted.
  104. Try to modify the data to verify everything gets unprotected */
  105. {
  106. int s;
  107. for (s = 501; s < 2500; s += 91)
  108. {
  109. size = s;
  110. {
  111. int i;
  112. #define ITERS2 100
  113. char * ptrs[ITERS2];
  114. __vtv_malloc_unprotect();
  115. for (i = 0; i < ITERS2; i++)
  116. {
  117. ptr = (char *)__vtv_malloc(size);
  118. assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0);
  119. memset(ptr, i & 127, size);
  120. assert(ptr[size - 1] == i & 127);
  121. ptrs[i] = ptr;
  122. }
  123. __vtv_malloc_protect();
  124. for (i = 0; i < ITERS2; i++)
  125. mempoke(ptrs[i], size);
  126. __vtv_malloc_unprotect();
  127. for (i = 0; i < ITERS2; i++)
  128. __vtv_free(ptrs[i]);
  129. __vtv_malloc_protect();
  130. }
  131. }
  132. }
  133. /* Allocate a bunch of medium size objects. Make sure the alignment is correct */
  134. {
  135. int s;
  136. for (s = 3001; s < 15000; s += 307)
  137. {
  138. size = s;
  139. {
  140. int i;
  141. #define ITERS3 50
  142. char * ptrs[ITERS3];
  143. __vtv_malloc_unprotect();
  144. for (i = 0; i < ITERS3; i++)
  145. {
  146. ptr = (char *)__vtv_malloc(size);
  147. assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0);
  148. memset(ptr, i & 127, size);
  149. assert(ptr[size - 1] == i & 127);
  150. ptrs[i] = ptr;
  151. }
  152. __vtv_malloc_protect();
  153. for (i = 0; i < ITERS3; i++)
  154. mempoke(ptrs[i], size);
  155. __vtv_malloc_unprotect();
  156. for (i = 0; i < ITERS3; i++)
  157. __vtv_free(ptrs[i]);
  158. __vtv_malloc_protect();
  159. }
  160. }
  161. }
  162. return 0;
  163. }