zero.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2013
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * zero - check that the VM system zeros pages given to processes
  31. *
  32. * This program will be much more likely to detect a problem if you
  33. * run it *after* one of the out-of-core tests (huge, matmult, sort,
  34. * etc.)
  35. */
  36. #include <stdio.h>
  37. #include <unistd.h>
  38. #include <errno.h>
  39. #include <err.h>
  40. /*
  41. * Some initialized data. This is here to increase the chance that
  42. * zeros[] spans page boundaries.
  43. */
  44. static unsigned data_stuff[] = {
  45. 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
  46. 2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
  47. 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
  48. 2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
  49. 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
  50. 2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
  51. 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
  52. 2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
  53. 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
  54. 2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
  55. };
  56. #define SUM_OF_DATA_STUFF 525
  57. /*
  58. * Some uninitialized (BSS, zero) data. Make it more than one page
  59. * even if we happen to be on a machine with 8K pages.
  60. */
  61. static unsigned bss_stuff[3000];
  62. static
  63. void
  64. check_data(void)
  65. {
  66. unsigned i, num, k;
  67. num = sizeof(data_stuff) / sizeof(data_stuff[0]);
  68. for (k=i=0; i<num; i++) {
  69. k += data_stuff[i];
  70. }
  71. if (k != SUM_OF_DATA_STUFF) {
  72. warnx("My initialized data sums to the wrong value!");
  73. warnx("Got: %u Expected: %u", k, SUM_OF_DATA_STUFF);
  74. errx(1, "FAILED");
  75. }
  76. }
  77. static
  78. void
  79. check_bss(void)
  80. {
  81. unsigned i, num;
  82. num = sizeof(bss_stuff) / sizeof(bss_stuff[0]);
  83. for (i=0; i<num; i++) {
  84. if (bss_stuff[i] != 0) {
  85. warnx("BSS entry at index %u (address %p) not zero!",
  86. i, &bss_stuff[i]);
  87. warnx("Found: 0x%x", bss_stuff[i]);
  88. errx(1, "FAILED");
  89. }
  90. }
  91. }
  92. static
  93. void
  94. check_sbrk(void)
  95. {
  96. char *base;
  97. unsigned i;
  98. /* get at least one page, even if the page size is 8K */
  99. #define SBRK_SIZE 10000
  100. base = sbrk(SBRK_SIZE);
  101. if (base == (void *)-1) {
  102. if (errno == EUNIMP) {
  103. printf("I guess you haven't implemented sbrk yet.\n");
  104. return;
  105. }
  106. err(1, "sbrk");
  107. }
  108. for (i=0; i<SBRK_SIZE; i++) {
  109. if (base[i] != 0) {
  110. warnx("Byte at offset %u (address %p) not zero",
  111. i, &base[i]);
  112. warnx("Got: 0x%x", (unsigned char)base[i]);
  113. warnx("Base of sbrk region: %p", base);
  114. errx(1, "FAILED");
  115. }
  116. }
  117. }
  118. int
  119. main(void)
  120. {
  121. printf("zero: phase 1: checking .bss\n");
  122. check_data();
  123. check_bss();
  124. printf("zero: phase 2: checking sbrk()\n");
  125. check_sbrk();
  126. printf("zero: passed\n");
  127. return 0;
  128. }