vla_test.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Test that allocating a variable length array in a loop
  3. * does not use up a linear amount of memory
  4. */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #define LOOP_COUNT 1000
  9. #define ARRAY_SIZE 100
  10. /* Overwrite a VLA. This will overwrite the return address if SP is incorrect */
  11. void smash(char *p, int n) {
  12. memset(p, 0, n);
  13. }
  14. int test1(int n) {
  15. int i;
  16. char *array_ptrs[LOOP_COUNT];
  17. for (i = 0; i < LOOP_COUNT; ++i) {
  18. char test[n];
  19. smash(test, n);
  20. array_ptrs[i] = test;
  21. }
  22. return (array_ptrs[0]-array_ptrs[LOOP_COUNT-1] < n) ? 0 : 1;
  23. }
  24. /* ensure goto does not circumvent array free */
  25. int test2(int n) {
  26. char *array_ptrs[LOOP_COUNT];
  27. int i = 0;
  28. loop:;
  29. char test[n];
  30. smash(test, n);
  31. if (i >= LOOP_COUNT)
  32. goto end;
  33. array_ptrs[i] = test;
  34. ++i;
  35. goto loop;
  36. end:
  37. smash(test, n);
  38. char test2[n];
  39. smash(test2, n);
  40. return (array_ptrs[0] - array_ptrs[LOOP_COUNT-1] < n) ? 0 : 1;
  41. }
  42. int test3(int n) {
  43. char test[n];
  44. smash(test, n);
  45. goto label;
  46. label:
  47. smash(test, n);
  48. char test2[n];
  49. smash(test2, n);
  50. return (test-test2 >= n) ? 0 : 1;
  51. }
  52. #define RUN_TEST(t) \
  53. if (!testname || (strcmp(#t, testname) == 0)) { \
  54. fputs(#t "... ", stdout); \
  55. fflush(stdout); \
  56. if (t(ARRAY_SIZE) == 0) { \
  57. fputs("success\n", stdout); \
  58. } else { \
  59. fputs("failure\n", stdout); \
  60. retval = EXIT_FAILURE; \
  61. } \
  62. }
  63. int main(int argc, char **argv) {
  64. const char *testname = NULL;
  65. int retval = EXIT_SUCCESS;
  66. if (argc > 1)
  67. testname = argv[1];
  68. RUN_TEST(test1)
  69. RUN_TEST(test2)
  70. RUN_TEST(test3)
  71. return retval;
  72. }