From 19f7f066e726b9057c4d22f7ea855a2eec1a6826 Mon Sep 17 00:00:00 2001 From: otto <> Date: Tue, 15 Jul 2003 10:09:37 +0000 Subject: move malloc0test.c to new subdir. ok tdeval@ millert@ --- src/regress/lib/libc/malloc/malloc0test/Makefile | 5 + .../lib/libc/malloc/malloc0test/malloc0test.c | 133 +++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/regress/lib/libc/malloc/malloc0test/Makefile create mode 100644 src/regress/lib/libc/malloc/malloc0test/malloc0test.c (limited to 'src/regress/lib/libc/malloc/malloc0test') diff --git a/src/regress/lib/libc/malloc/malloc0test/Makefile b/src/regress/lib/libc/malloc/malloc0test/Makefile new file mode 100644 index 0000000000..8ed8163a79 --- /dev/null +++ b/src/regress/lib/libc/malloc/malloc0test/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2003/07/15 10:09:37 otto Exp $ + +PROG= malloc0test + +.include diff --git a/src/regress/lib/libc/malloc/malloc0test/malloc0test.c b/src/regress/lib/libc/malloc/malloc0test/malloc0test.c new file mode 100644 index 0000000000..39c3fd2e64 --- /dev/null +++ b/src/regress/lib/libc/malloc/malloc0test/malloc0test.c @@ -0,0 +1,133 @@ +/* $OpenBSD: malloc0test.c,v 1.1 2003/07/15 10:09:37 otto Exp $ */ +/* + * Public domain. 2001, Theo de Raadt + */ +#include +#include +#include +#include +#include +#include +#include +#include + +volatile sig_atomic_t got; +jmp_buf jmp; + +void +catch(int signo) +{ + got++; + longjmp(jmp, 1); +} + +int +test(char *p, int size) +{ + signal(SIGSEGV, catch); + got = 0; + if (setjmp(jmp) == 0) + *p = 0; + if (setjmp(jmp) == 0) + *(p+size-1) = 0; + return (got); +} + +char *prot_table[] = { + "unprotected", + "fuckup", + "protected" +}; + +#define SIZE 10 + +/* + * Do random memory allocations. + * + * For each one, ensure that it is at least 16 bytes in size (that + * being what our current malloc returns for the minsize of an + * object, alignment wise); + * + * For zero-byte allocations, check that they are still aligned. + * + * For each object, ensure that they are correctly protected or not + * protected. + * + * Does not regress test malloc + free combinations ... it should. + */ +int +main(int argc, char *argv[]) +{ + caddr_t rblob = malloc(1); + caddr_t zblob = malloc(0); + caddr_t *blobp, blob; + int size, rsize, tsize; + int prot; + int rval = 0, fuckup = 0; + long limit = 200000, count; + int ch, silent = 0; + char *ep; + extern char *__progname; + + while ((ch = getopt(argc, argv, "sn:")) != -1) { + switch (ch) { + case 's': + silent = 1; + break; + case 'n': + errno = 0; + limit = strtol(optarg, &ep, 10); + if (optarg[0] == '\0' || *ep != '\0' || + (errno == ERANGE && + (limit == LONG_MAX || limit == LONG_MIN))) + goto usage; + break; + default: +usage: + fprintf(stderr, "Usage: %s [-s][-n ]\n", + __progname); + exit(1); + } + } + + if (limit == 0) + limit = LONG_MAX; + + for (count = 0; count < limit; count++) { + size = arc4random() % SIZE; + blob = malloc(size); + if (blob == NULL) { + fprintf(stderr, "success: out of memory\n"); + exit(rval); + } + + if (size == 0) { + blobp = &zblob; + tsize = 16; + } else { + blobp = &rblob; + tsize = size; + } + + rsize = blob - *blobp; + fuckup = SIZE < 16 && size >= rsize; + prot = test(blob, tsize); + + if (size == 0 && rsize < 16) + fuckup = 1; + if (size == 0 && prot < 2) + fuckup = 1; + + if (fuckup) { + printf("%8p %6d %6d %20s %10s\n", blob, size, rsize, + prot_table[prot], fuckup ? "fuckup" : ""); + rval = 1; + } + *blobp = blob; + + if (!silent && count % 100000 == 0 && count != 0) + fprintf(stderr, "count = %d\n", count); + } + + return rval; +} \ No newline at end of file -- cgit v1.2.3-55-g6feb