mempool_positive.cc 4.8 KB

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