vm-stack2.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define PAGE_SIZE (4096)
  4. #define STACK_PAGES_USED (9)
  5. #define STACK_ARRAY_ELEMS (PAGE_SIZE * STACK_PAGES_USED / sizeof(int))
  6. #define UNINIT_PAGES (9)
  7. #define UNINIT_ARRAY_ELEMS (PAGE_SIZE * UNINIT_PAGES / sizeof(int))
  8. unsigned int uninit[UNINIT_ARRAY_ELEMS];
  9. unsigned int init[] = {
  10. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  11. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  12. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  13. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  14. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  15. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  16. };
  17. #define INIT_ARRAY_ELEMS (sizeof(init) / sizeof(int))
  18. int
  19. main()
  20. {
  21. unsigned int array[STACK_ARRAY_ELEMS];
  22. unsigned int i = 0;
  23. /* check the uninitialized array before initialization */
  24. for (i=0; i<UNINIT_ARRAY_ELEMS; i++) {
  25. if (uninit[i] != 0) {
  26. printf("FAILED uninit[%d] = %u != %d\n", i, uninit[i], 0);
  27. exit(1);
  28. }
  29. }
  30. /* initialize the uninitialized data */
  31. for (i=0; i<UNINIT_ARRAY_ELEMS; i++) {
  32. uninit[i] = i * 100;
  33. }
  34. /* initialize the array on the stack */
  35. for (i=0; i<STACK_ARRAY_ELEMS; i++) {
  36. array[i] = i * 1000;
  37. }
  38. /* check the array on the stack */
  39. for (i=0; i<STACK_ARRAY_ELEMS; i++) {
  40. if (array[i] != i * 1000) {
  41. printf("FAILED array[%d] = %u != %d\n", i, array[i], i);
  42. exit(1);
  43. }
  44. }
  45. /* check the uninitialized array after initialization */
  46. for (i=0; i<UNINIT_ARRAY_ELEMS; i++) {
  47. if (uninit[i] != i * 100) {
  48. printf("FAILED uninit[%d] = %u != %d\n", i, uninit[i], i);
  49. exit(1);
  50. }
  51. }
  52. /* check the initialized array */
  53. for (i=0; i<INIT_ARRAY_ELEMS; i++) {
  54. if (init[i] != i) {
  55. printf("FAILED init[%d] = %u != %d\n", i, init[i], i);
  56. exit(1);
  57. }
  58. }
  59. printf("SUCCEEDED\n");
  60. exit(0);
  61. }