boundtest.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define NB_ITS 1000000
  5. //#define NB_ITS 1
  6. #define TAB_SIZE 100
  7. int tab[TAB_SIZE];
  8. int ret_sum;
  9. char tab3[256];
  10. int test1(void)
  11. {
  12. int i, sum = 0;
  13. for(i=0;i<TAB_SIZE;i++) {
  14. sum += tab[i];
  15. }
  16. return sum;
  17. }
  18. /* error */
  19. int test2(void)
  20. {
  21. int i, sum = 0;
  22. for(i=0;i<TAB_SIZE + 1;i++) {
  23. sum += tab[i];
  24. }
  25. return sum;
  26. }
  27. /* actually, profiling test */
  28. int test3(void)
  29. {
  30. int sum;
  31. int i, it;
  32. sum = 0;
  33. for(it=0;it<NB_ITS;it++) {
  34. for(i=0;i<TAB_SIZE;i++) {
  35. sum += tab[i];
  36. }
  37. }
  38. return sum;
  39. }
  40. /* ok */
  41. int test4(void)
  42. {
  43. int i, sum = 0;
  44. int *tab4;
  45. fprintf(stderr, "%s start\n", __FUNCTION__);
  46. tab4 = malloc(20 * sizeof(int));
  47. for(i=0;i<20;i++) {
  48. sum += tab4[i];
  49. }
  50. free(tab4);
  51. fprintf(stderr, "%s end\n", __FUNCTION__);
  52. return sum;
  53. }
  54. /* error */
  55. int test5(void)
  56. {
  57. int i, sum = 0;
  58. int *tab4;
  59. fprintf(stderr, "%s start\n", __FUNCTION__);
  60. tab4 = malloc(20 * sizeof(int));
  61. for(i=0;i<21;i++) {
  62. sum += tab4[i];
  63. }
  64. free(tab4);
  65. fprintf(stderr, "%s end\n", __FUNCTION__);
  66. return sum;
  67. }
  68. /* error */
  69. /* XXX: currently: bug */
  70. int test6(void)
  71. {
  72. int i, sum = 0;
  73. int *tab4;
  74. tab4 = malloc(20 * sizeof(int));
  75. free(tab4);
  76. for(i=0;i<21;i++) {
  77. sum += tab4[i];
  78. }
  79. return sum;
  80. }
  81. /* error */
  82. int test7(void)
  83. {
  84. int i, sum = 0;
  85. int *p;
  86. for(i=0;i<TAB_SIZE + 1;i++) {
  87. p = &tab[i];
  88. if (i == TAB_SIZE)
  89. printf("i=%d %x\n", i, p);
  90. sum += *p;
  91. }
  92. return sum;
  93. }
  94. /* ok */
  95. int test8(void)
  96. {
  97. int i, sum = 0;
  98. int tab[10];
  99. for(i=0;i<10;i++) {
  100. sum += tab[i];
  101. }
  102. return sum;
  103. }
  104. /* error */
  105. int test9(void)
  106. {
  107. int i, sum = 0;
  108. char tab[10];
  109. for(i=0;i<11;i++) {
  110. sum += tab[i];
  111. }
  112. return sum;
  113. }
  114. /* ok */
  115. int test10(void)
  116. {
  117. char tab[10];
  118. char tab1[10];
  119. memset(tab, 0, 10);
  120. memcpy(tab, tab1, 10);
  121. memmove(tab, tab1, 10);
  122. return 0;
  123. }
  124. /* error */
  125. int test11(void)
  126. {
  127. char tab[10];
  128. memset(tab, 0, 11);
  129. return 0;
  130. }
  131. /* error */
  132. int test12(void)
  133. {
  134. void *ptr;
  135. ptr = malloc(10);
  136. free(ptr);
  137. free(ptr);
  138. return 0;
  139. }
  140. /* error */
  141. int test13(void)
  142. {
  143. char pad1 = 0;
  144. char tab[10];
  145. char pad2 = 0;
  146. memset(tab, 'a', sizeof(tab));
  147. return strlen(tab);
  148. }
  149. int test14(void)
  150. {
  151. char *p = alloca(TAB_SIZE);
  152. memset(p, 'a', TAB_SIZE);
  153. p[TAB_SIZE-1] = 0;
  154. return strlen(p);
  155. }
  156. /* error */
  157. int test15(void)
  158. {
  159. char *p = alloca(TAB_SIZE-1);
  160. memset(p, 'a', TAB_SIZE);
  161. p[TAB_SIZE-1] = 0;
  162. return strlen(p);
  163. }
  164. /* ok */
  165. int test16()
  166. {
  167. char *demo = "This is only a test.";
  168. char *p;
  169. fprintf(stderr, "%s start\n", __FUNCTION__);
  170. p = alloca(16);
  171. strcpy(p,"12345678901234");
  172. printf("alloca: p is %s\n", p);
  173. /* Test alloca embedded in a larger expression */
  174. printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
  175. fprintf(stderr, "%s end\n", __FUNCTION__);
  176. }
  177. /* error */
  178. int test17()
  179. {
  180. char *demo = "This is only a test.";
  181. char *p;
  182. fprintf(stderr, "%s start\n", __FUNCTION__);
  183. p = alloca(16);
  184. strcpy(p,"12345678901234");
  185. printf("alloca: p is %s\n", p);
  186. /* Test alloca embedded in a larger expression */
  187. printf("alloca: %s\n", strcpy(alloca(strlen(demo)),demo) );
  188. fprintf(stderr, "%s end\n", __FUNCTION__);
  189. }
  190. int (*table_test[])(void) = {
  191. test1,
  192. test2,
  193. test3,
  194. test4,
  195. test5,
  196. test6,
  197. test7,
  198. test8,
  199. test9,
  200. test10,
  201. test11,
  202. test12,
  203. test13,
  204. test14,
  205. test15,
  206. test16,
  207. test17,
  208. };
  209. int main(int argc, char **argv)
  210. {
  211. int index;
  212. int (*ftest)(void);
  213. int index_max = sizeof(table_test)/sizeof(table_test[0]);
  214. if (argc < 2) {
  215. printf(
  216. "test TCC bound checking system\n"
  217. "usage: boundtest N\n"
  218. " 1 <= N <= %d\n", index_max);
  219. exit(1);
  220. }
  221. index = 0;
  222. if (argc >= 2)
  223. index = atoi(argv[1]) - 1;
  224. if ((index < 0) || (index >= index_max)) {
  225. printf("N is outside of the valid range (%d)\n", index);
  226. exit(2);
  227. }
  228. /* well, we also use bounds on this ! */
  229. ftest = table_test[index];
  230. ftest();
  231. return 0;
  232. }
  233. /*
  234. * without bound 0.77 s
  235. * with bounds 4.73
  236. */