diff options
author | deraadt <> | 2001-12-05 09:52:01 +0000 |
---|---|---|
committer | deraadt <> | 2001-12-05 09:52:01 +0000 |
commit | b8e9b8d08ebac2bb596ff4717b4b79653d3acd48 (patch) | |
tree | a15f5b1d69aad70bc42f49be50a2d5155320b553 /src/regress/lib | |
parent | e6969fd8586f3967bd8ccc6245fb6ea9c7d85618 (diff) | |
download | openbsd-b8e9b8d08ebac2bb596ff4717b4b79653d3acd48.tar.gz openbsd-b8e9b8d08ebac2bb596ff4717b4b79653d3acd48.tar.bz2 openbsd-b8e9b8d08ebac2bb596ff4717b4b79653d3acd48.zip |
malloc(n) regression test
Diffstat (limited to 'src/regress/lib')
-rw-r--r-- | src/regress/lib/libc/Makefile | 4 | ||||
-rw-r--r-- | src/regress/lib/libc/malloc/Makefile | 14 | ||||
-rw-r--r-- | src/regress/lib/libc/malloc/malloc0test.c | 98 |
3 files changed, 114 insertions, 2 deletions
diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile index be8cde02a7..9d17a520c3 100644 --- a/src/regress/lib/libc/Makefile +++ b/src/regress/lib/libc/Makefile | |||
@@ -1,7 +1,7 @@ | |||
1 | # $OpenBSD: Makefile,v 1.2 2001/01/29 02:05:37 niklas Exp $ | 1 | # $OpenBSD: Makefile,v 1.3 2001/12/05 09:52:01 deraadt Exp $ |
2 | # $NetBSD: Makefile,v 1.6 1995/04/24 05:52:15 cgd Exp $ | 2 | # $NetBSD: Makefile,v 1.6 1995/04/24 05:52:15 cgd Exp $ |
3 | 3 | ||
4 | SUBDIR+= _setjmp db regex setjmp sigsetjmp | 4 | SUBDIR+= _setjmp db regex setjmp sigsetjmp malloc |
5 | .if (${MACHINE_ARCH} != "vax") | 5 | .if (${MACHINE_ARCH} != "vax") |
6 | SUBDIR+= ieeefp | 6 | SUBDIR+= ieeefp |
7 | .endif | 7 | .endif |
diff --git a/src/regress/lib/libc/malloc/Makefile b/src/regress/lib/libc/malloc/Makefile new file mode 100644 index 0000000000..ca8fb32343 --- /dev/null +++ b/src/regress/lib/libc/malloc/Makefile | |||
@@ -0,0 +1,14 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2001/12/05 09:52:01 deraadt Exp $ | ||
2 | # $NetBSD: Makefile,v 1.2 1995/04/20 22:40:13 cgd Exp $ | ||
3 | |||
4 | PROG= malloc0test | ||
5 | NOMAN= noman, no way, man | ||
6 | |||
7 | .PATH: ${.CURDIR}/../malloc | ||
8 | |||
9 | install: | ||
10 | |||
11 | regress: ${PROG} | ||
12 | ./${PROG} | ||
13 | |||
14 | .include <bsd.prog.mk> | ||
diff --git a/src/regress/lib/libc/malloc/malloc0test.c b/src/regress/lib/libc/malloc/malloc0test.c new file mode 100644 index 0000000000..5ca698aa50 --- /dev/null +++ b/src/regress/lib/libc/malloc/malloc0test.c | |||
@@ -0,0 +1,98 @@ | |||
1 | #include <sys/types.h> | ||
2 | #include <sys/signal.h> | ||
3 | #include <stdio.h> | ||
4 | #include <unistd.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <setjmp.h> | ||
7 | |||
8 | volatile sig_atomic_t got; | ||
9 | jmp_buf jmp; | ||
10 | |||
11 | void | ||
12 | catch(int signo) | ||
13 | { | ||
14 | got++; | ||
15 | longjmp(jmp, 0); | ||
16 | } | ||
17 | |||
18 | int | ||
19 | test(caddr_t p, int size) | ||
20 | { | ||
21 | signal(SIGSEGV, catch); | ||
22 | got = 0; | ||
23 | if (setjmp(jmp) == 0) | ||
24 | *p = 0; | ||
25 | if (setjmp(jmp) == 0) | ||
26 | *(p+size-1) = 0; | ||
27 | return (got); | ||
28 | } | ||
29 | |||
30 | char *prot_table[] = { | ||
31 | "unprotected", | ||
32 | "fuckup", | ||
33 | "protected" | ||
34 | }; | ||
35 | |||
36 | #define SIZE 10 | ||
37 | |||
38 | /* | ||
39 | * Do random memory allocations. | ||
40 | * | ||
41 | * For each one, ensure that it is at least 16 bytes in size (that | ||
42 | * being what our current malloc returns for the minsize of an | ||
43 | * object, alignment wise); | ||
44 | * | ||
45 | * For zero-byte allocations, check that they are still aligned. | ||
46 | * | ||
47 | * For each object, ensure that they are correctly protected or not | ||
48 | * protected. | ||
49 | * | ||
50 | * Does not regress test malloc + free combinations ... it should. | ||
51 | */ | ||
52 | int | ||
53 | main(int argc, char *argv[]) | ||
54 | { | ||
55 | caddr_t rblob = malloc(1); | ||
56 | caddr_t zblob = malloc(0); | ||
57 | caddr_t *blobp, blob; | ||
58 | int size, rsize, tsize; | ||
59 | int count = 0, prot; | ||
60 | int rval = 0, fuckup = 0; | ||
61 | |||
62 | while (1) { | ||
63 | size = arc4random() % SIZE; | ||
64 | blob = malloc(size); | ||
65 | if (blob == NULL) { | ||
66 | fprintf(stderr, "success: out of memory\n"); | ||
67 | exit(rval); | ||
68 | } | ||
69 | |||
70 | if (size == 0) { | ||
71 | blobp = &zblob; | ||
72 | tsize = 16; | ||
73 | } else { | ||
74 | blobp = &rblob; | ||
75 | tsize = size; | ||
76 | } | ||
77 | |||
78 | rsize = blob - *blobp; | ||
79 | fuckup = SIZE < 16 && size >= rsize; | ||
80 | prot = test(blob, tsize); | ||
81 | |||
82 | if (size == 0 && rsize < 16) | ||
83 | fuckup = 1; | ||
84 | if (size == 0 && prot < 2) | ||
85 | fuckup = 1; | ||
86 | |||
87 | if (fuckup) { | ||
88 | printf("%8p %6d %6d %20s %10s\n", blob, size, rsize, | ||
89 | prot_table[prot], fuckup ? "fuckup" : ""); | ||
90 | rval = 1; | ||
91 | } | ||
92 | *blobp = blob; | ||
93 | |||
94 | |||
95 | if (++count % 100000 == 0) | ||
96 | fprintf(stderr, "count = %d\n", count); | ||
97 | } | ||
98 | } \ No newline at end of file | ||