root/user/testbin/zero/zero.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. check_data
  2. check_bss
  3. check_sbrk
  4. main

   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 /*
  31  * zero - check that the VM system zeros pages given to processes
  32  *
  33  * This program will be much more likely to detect a problem if you
  34  * run it *after* one of the out-of-core tests (huge, matmult, sort,
  35  * etc.)
  36  */
  37 
  38 #include <stdio.h>
  39 #include <unistd.h>
  40 #include <errno.h>
  41 #include <err.h>
  42 
  43 /*
  44  * Some initialized data. This is here to increase the chance that
  45  * zeros[] spans page boundaries.
  46  */
  47 static unsigned data_stuff[] = {
  48         1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
  49         2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
  50         1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
  51         2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
  52         1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
  53         2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
  54         1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
  55         2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
  56         1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
  57         2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
  58 };
  59 
  60 #define SUM_OF_DATA_STUFF 525
  61 
  62 /*
  63  * Some uninitialized (BSS, zero) data. Make it more than one page
  64  * even if we happen to be on a machine with 8K pages.
  65  */
  66 static unsigned bss_stuff[3000];
  67 
  68 static
  69 void
  70 check_data(void)
  71 {
  72         unsigned i, num, k;
  73 
  74         num = sizeof(data_stuff) / sizeof(data_stuff[0]);
  75         for (k=i=0; i<num; i++) {
  76                 k += data_stuff[i];
  77         }
  78         if (k != SUM_OF_DATA_STUFF) {
  79                 warnx("My initialized data sums to the wrong value!");
  80                 warnx("Got: %u  Expected: %u", k, SUM_OF_DATA_STUFF);
  81                 errx(1, "FAILED");
  82         }
  83 }
  84 
  85 static
  86 void
  87 check_bss(void)
  88 {
  89         unsigned i, num;
  90 
  91         num = sizeof(bss_stuff) / sizeof(bss_stuff[0]);
  92         for (i=0; i<num; i++) {
  93                 if (bss_stuff[i] != 0) {
  94                         warnx("BSS entry at index %u (address %p) not zero!",
  95                               i, &bss_stuff[i]);
  96                         warnx("Found: 0x%x", bss_stuff[i]);
  97                         errx(1, "FAILED");
  98                 }
  99         }
 100 }
 101 
 102 static
 103 void
 104 check_sbrk(void)
 105 {
 106         char *base;
 107         unsigned i;
 108 
 109 /* get at least one page, even if the page size is 8K */
 110 #define SBRK_SIZE 10000
 111 
 112         base = sbrk(SBRK_SIZE);
 113         if (base == (void *)-1) {
 114                 if (errno == EUNIMP) {
 115                         printf("I guess you haven't implemented sbrk yet.\n");
 116                         return;
 117                 }
 118                 err(1, "sbrk");
 119         }
 120 
 121         for (i=0; i<SBRK_SIZE; i++) {
 122                 if (base[i] != 0) {
 123                         warnx("Byte at offset %u (address %p) not zero",
 124                               i, &base[i]);
 125                         warnx("Got: 0x%x", (unsigned char)base[i]);
 126                         warnx("Base of sbrk region: %p", base);
 127                         errx(1, "FAILED");
 128                 }
 129         }
 130 }
 131 
 132 
 133 int
 134 main(void)
 135 {
 136         printf("zero: phase 1: checking .bss\n");
 137         check_data();
 138         check_bss();
 139 
 140         printf("zero: phase 2: checking sbrk()\n");
 141         check_sbrk();
 142 
 143         printf("zero: passed\n");
 144         return 0;
 145 }

/* [<][>][^][v][top][bottom][index][help] */