diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2001-12-05 09:52:02 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2001-12-05 09:52:02 +0000 |
commit | e8bdff0f8a232b63070193c5fcdaee12e71a7e9f (patch) | |
tree | 20af83cdf18b4feee0ef164ca4c45948901d5bbb /regress/lib | |
parent | 5013881a89e4a7796fbad098e2abba4eb3fb2856 (diff) |
malloc(n) regression test
Diffstat (limited to 'regress/lib')
-rw-r--r-- | regress/lib/libc/Makefile | 4 | ||||
-rw-r--r-- | regress/lib/libc/malloc/Makefile | 14 | ||||
-rw-r--r-- | regress/lib/libc/malloc/malloc0test.c | 98 |
3 files changed, 114 insertions, 2 deletions
diff --git a/regress/lib/libc/Makefile b/regress/lib/libc/Makefile index be8cde02a76..9d17a520c3a 100644 --- a/regress/lib/libc/Makefile +++ b/regress/lib/libc/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.2 2001/01/29 02:05:37 niklas Exp $ +# $OpenBSD: Makefile,v 1.3 2001/12/05 09:52:01 deraadt Exp $ # $NetBSD: Makefile,v 1.6 1995/04/24 05:52:15 cgd Exp $ -SUBDIR+= _setjmp db regex setjmp sigsetjmp +SUBDIR+= _setjmp db regex setjmp sigsetjmp malloc .if (${MACHINE_ARCH} != "vax") SUBDIR+= ieeefp .endif diff --git a/regress/lib/libc/malloc/Makefile b/regress/lib/libc/malloc/Makefile new file mode 100644 index 00000000000..ca8fb323439 --- /dev/null +++ b/regress/lib/libc/malloc/Makefile @@ -0,0 +1,14 @@ +# $OpenBSD: Makefile,v 1.1 2001/12/05 09:52:01 deraadt Exp $ +# $NetBSD: Makefile,v 1.2 1995/04/20 22:40:13 cgd Exp $ + +PROG= malloc0test +NOMAN= noman, no way, man + +.PATH: ${.CURDIR}/../malloc + +install: + +regress: ${PROG} + ./${PROG} + +.include <bsd.prog.mk> diff --git a/regress/lib/libc/malloc/malloc0test.c b/regress/lib/libc/malloc/malloc0test.c new file mode 100644 index 00000000000..5ca698aa50b --- /dev/null +++ b/regress/lib/libc/malloc/malloc0test.c @@ -0,0 +1,98 @@ +#include <sys/types.h> +#include <sys/signal.h> +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <setjmp.h> + +volatile sig_atomic_t got; +jmp_buf jmp; + +void +catch(int signo) +{ + got++; + longjmp(jmp, 0); +} + +int +test(caddr_t 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 count = 0, prot; + int rval = 0, fuckup = 0; + + while (1) { + 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 (++count % 100000 == 0) + fprintf(stderr, "count = %d\n", count); + } +}
\ No newline at end of file |