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/libc/malloc/malloc0test.c | |
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/libc/malloc/malloc0test.c')
-rw-r--r-- | src/regress/lib/libc/malloc/malloc0test.c | 98 |
1 files changed, 98 insertions, 0 deletions
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 | ||