os161-1.99
 All Data Structures
zero.c
00001 /*
00002  * Copyright (c) 2013
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * zero - check that the VM system zeros pages given to processes
00032  *
00033  * This program will be much more likely to detect a problem if you
00034  * run it *after* one of the out-of-core tests (huge, matmult, sort,
00035  * etc.)
00036  */
00037 
00038 #include <stdio.h>
00039 #include <unistd.h>
00040 #include <errno.h>
00041 #include <err.h>
00042 
00043 /*
00044  * Some initialized data. This is here to increase the chance that
00045  * zeros[] spans page boundaries.
00046  */
00047 static unsigned data_stuff[] = {
00048         1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
00049         2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
00050         1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
00051         2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
00052         1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
00053         2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
00054         1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
00055         2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
00056         1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
00057         2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
00058 };
00059 
00060 #define SUM_OF_DATA_STUFF 525
00061 
00062 /*
00063  * Some uninitialized (BSS, zero) data. Make it more than one page
00064  * even if we happen to be on a machine with 8K pages.
00065  */
00066 static unsigned bss_stuff[3000];
00067 
00068 static
00069 void
00070 check_data(void)
00071 {
00072         unsigned i, num, k;
00073 
00074         num = sizeof(data_stuff) / sizeof(data_stuff[0]);
00075         for (k=i=0; i<num; i++) {
00076                 k += data_stuff[i];
00077         }
00078         if (k != SUM_OF_DATA_STUFF) {
00079                 warnx("My initialized data sums to the wrong value!");
00080                 warnx("Got: %u  Expected: %u", k, SUM_OF_DATA_STUFF);
00081                 errx(1, "FAILED");
00082         }
00083 }
00084 
00085 static
00086 void
00087 check_bss(void)
00088 {
00089         unsigned i, num;
00090 
00091         num = sizeof(bss_stuff) / sizeof(bss_stuff[0]);
00092         for (i=0; i<num; i++) {
00093                 if (bss_stuff[i] != 0) {
00094                         warnx("BSS entry at index %u (address %p) not zero!",
00095                               i, &bss_stuff[i]);
00096                         warnx("Found: 0x%x", bss_stuff[i]);
00097                         errx(1, "FAILED");
00098                 }
00099         }
00100 }
00101 
00102 static
00103 void
00104 check_sbrk(void)
00105 {
00106         char *base;
00107         unsigned i;
00108 
00109 /* get at least one page, even if the page size is 8K */
00110 #define SBRK_SIZE 10000
00111 
00112         base = sbrk(SBRK_SIZE);
00113         if (base == (void *)-1) {
00114                 if (errno == EUNIMP) {
00115                         printf("I guess you haven't implemented sbrk yet.\n");
00116                         return;
00117                 }
00118                 err(1, "sbrk");
00119         }
00120 
00121         for (i=0; i<SBRK_SIZE; i++) {
00122                 if (base[i] != 0) {
00123                         warnx("Byte at offset %u (address %p) not zero",
00124                               i, &base[i]);
00125                         warnx("Got: 0x%x", (unsigned char)base[i]);
00126                         warnx("Base of sbrk region: %p", base);
00127                         errx(1, "FAILED");
00128                 }
00129         }
00130 }
00131 
00132 
00133 int
00134 main(void)
00135 {
00136         printf("zero: phase 1: checking .bss\n");
00137         check_data();
00138         check_bss();
00139 
00140         printf("zero: phase 2: checking sbrk()\n");
00141         check_sbrk();
00142 
00143         printf("zero: passed\n");
00144         return 0;
00145 }
 All Data Structures