/* * Copyright (c) 2014 * The President and Fellows of Harvard College. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #define _PATH_RANDOM "random:" /* * Caution: OS/161 doesn't provide any way to get this properly from * the kernel. The page size is 4K on almost all hardware... but not * all. If porting to certain weird machines this will need attention. */ #define PAGE_SIZE 4096 //////////////////////////////////////////////////////////// // support code static int geti(void) { int val=0; int ch, digits=0; while (1) { ch = getchar(); if (ch=='\n' || ch=='\r') { putchar('\n'); break; } else if ((ch=='\b' || ch==127) && digits>0) { printf("\b \b"); val = val/10; digits--; } else if (ch>='0' && ch<='9') { putchar(ch); val = val*10 + (ch-'0'); digits++; } else { putchar('\a'); } } if (digits==0) { return -1; } return val; } static unsigned long getseed(void) { int fd, len; unsigned long seed; fd = open(_PATH_RANDOM, O_RDONLY); if (fd < 0) { err(1, "%s", _PATH_RANDOM); } len = read(fd, &seed, sizeof(seed)); if (len < 0) { err(1, "%s", _PATH_RANDOM); } else if (len < (int)sizeof(seed)) { errx(1, "%s: Short read", _PATH_RANDOM); } close(fd); return seed; } static pid_t dofork(void) { pid_t pid; pid = fork(); if (pid < 0) { err(1, "fork"); } return pid; } static void dowait(pid_t pid) { int status; int result; result = waitpid(pid, &status, 0); if (result == -1) { err(1, "waitpid"); } if (WIFSIGNALED(status)) { errx(1, "child: Signal %d", WTERMSIG(status)); } if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { errx(1, "child: Exit %d", WEXITSTATUS(status)); } } static void say(const char *msg) { /* Use one write so it's atomic (printf usually won't be) */ write(STDOUT_FILENO, msg, strlen(msg)); } //////////////////////////////////////////////////////////// // memory checking /* * Fill a page of memory with a test pattern. */ static void markpage(volatile void *baseptr, unsigned pageoffset) { volatile char *pageptr; size_t n, i; volatile unsigned long *pl; unsigned long val; pageptr = baseptr; pageptr += (size_t)PAGE_SIZE * pageoffset; pl = (volatile unsigned long *)pageptr; n = PAGE_SIZE / sizeof(unsigned long); for (i=0; i 0; ) { (void)dosbrk(-PAGE_SIZE); for (j=0; j 0 && i % dot == 0) { printf("."); } } if (dot > 0) { printf("\n"); } printf("Testing each page.\n"); bad = false; for (i=0; i 0)) { if (dot > 0) { printf("\n"); } warnx("FAILED: data corrupt"); bad = true; } if (dot > 0 && i % dot == 0) { printf("."); } } if (dot > 0) { printf("\n"); } if (bad) { exit(1); } printf("Passed sbrk test 9 (part 2/5)\n"); printf("Freeing the memory.\n"); (void)dosbrk(-size); printf("Passed sbrk test 9 (part 3/5)\n"); printf("Allocating the memory again.\n"); (void)dosbrk(size); printf("Passed sbrk test 9 (part 4/5)\n"); printf("And really freeing it.\n"); (void)dosbrk(-size); printf("Passed sbrk test 9 (all)\n"); } /* * Allocate all of memory one page at a time. The same restrictions * and considerations apply as above. */ static void test10(void) { void *p, *op; unsigned i, n; bool bad; printf("Allocating all of memory one page at a time:\n"); op = dosbrk(0); n = 0; while ((p = sbrk(PAGE_SIZE)) != (void *)-1) { markpagelight(op, n); n++; } printf("Got %u pages (%zu bytes).\n", n, (size_t)PAGE_SIZE * n); printf("Now freeing them.\n"); bad = false; for (i=0; i (large ? 128 : 32)) { neg = 1; } if (neg) { dosbrk(-(pages * PAGE_SIZE)); num -= pages; } else { dosbrk(pages * PAGE_SIZE); for (j=0; j 1) { for (i=1; i