00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <stdio.h>
00039 #include <unistd.h>
00040 #include <errno.h>
00041 #include <err.h>
00042
00043
00044
00045
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
00064
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
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 }