diff options
author | otto <> | 2017-01-23 11:06:32 +0000 |
---|---|---|
committer | otto <> | 2017-01-23 11:06:32 +0000 |
commit | f78a32596ded637850cfcb04eb429ddb97f1272a (patch) | |
tree | 041054b59f7b279985048621bc6a3a3145c14674 | |
parent | 034e1f9018bedb7d71df3286551c19fa5bd18c07 (diff) | |
download | openbsd-f78a32596ded637850cfcb04eb429ddb97f1272a.tar.gz openbsd-f78a32596ded637850cfcb04eb429ddb97f1272a.tar.bz2 openbsd-f78a32596ded637850cfcb04eb429ddb97f1272a.zip |
test malloc/realloc/free with some flag combo's
-rw-r--r-- | src/regress/lib/libc/malloc/malloc_general/Makefile | 26 | ||||
-rw-r--r-- | src/regress/lib/libc/malloc/malloc_general/malloc_general.c | 96 |
2 files changed, 122 insertions, 0 deletions
diff --git a/src/regress/lib/libc/malloc/malloc_general/Makefile b/src/regress/lib/libc/malloc/malloc_general/Makefile new file mode 100644 index 0000000000..18151b1ea0 --- /dev/null +++ b/src/regress/lib/libc/malloc/malloc_general/Makefile | |||
@@ -0,0 +1,26 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2017/01/23 11:06:32 otto Exp $ | ||
2 | |||
3 | REGRESS_TARGETS= t1 t2 t3 t4 t5 t6 t7 | ||
4 | |||
5 | .include <bsd.regress.mk> | ||
6 | |||
7 | t1: malloc_general | ||
8 | malloc_general "" | ||
9 | |||
10 | t2: malloc_general | ||
11 | malloc_general C | ||
12 | |||
13 | t3: malloc_general | ||
14 | malloc_general J | ||
15 | |||
16 | t4: malloc_general | ||
17 | malloc_general F | ||
18 | |||
19 | t5: malloc_general | ||
20 | malloc_general G | ||
21 | |||
22 | t6: malloc_general | ||
23 | malloc_general S | ||
24 | |||
25 | t7: malloc_general | ||
26 | malloc_general FGJ | ||
diff --git a/src/regress/lib/libc/malloc/malloc_general/malloc_general.c b/src/regress/lib/libc/malloc/malloc_general/malloc_general.c new file mode 100644 index 0000000000..336326bc57 --- /dev/null +++ b/src/regress/lib/libc/malloc/malloc_general/malloc_general.c | |||
@@ -0,0 +1,96 @@ | |||
1 | /* $OpenBSD */ | ||
2 | /* | ||
3 | * Copyright (c) 2017 Otto Moerbeek <otto@drijf.net> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <stdio.h> | ||
19 | #include <stdlib.h> | ||
20 | #include <string.h> | ||
21 | |||
22 | /* $define VERBOSE */ | ||
23 | |||
24 | #define N 1000 | ||
25 | |||
26 | size_t size(void) | ||
27 | { | ||
28 | int p = arc4random_uniform(13) + 3; | ||
29 | return arc4random_uniform(1 << p); | ||
30 | } | ||
31 | |||
32 | void *a[N]; | ||
33 | |||
34 | extern char *malloc_options; | ||
35 | |||
36 | int | ||
37 | main(int argc, char *argv[]) | ||
38 | { | ||
39 | int count, p, i; | ||
40 | void * q; | ||
41 | size_t sz; | ||
42 | |||
43 | if (argc == 1) | ||
44 | errx(1, "usage: malloc_options"); | ||
45 | |||
46 | malloc_options = argv[1]; | ||
47 | |||
48 | for (count = 0; count < 800000; count++) { | ||
49 | if (count % 10000 == 0) { | ||
50 | printf("."); | ||
51 | fflush(stdout); | ||
52 | } | ||
53 | p = arc4random_uniform(2); | ||
54 | i = arc4random_uniform(N); | ||
55 | switch (p) { | ||
56 | case 0: | ||
57 | if (a[i]) { | ||
58 | #ifdef VERBOSE | ||
59 | printf("F %p\n", a[i]); | ||
60 | #endif | ||
61 | free(a[i]); | ||
62 | a[i] = NULL; | ||
63 | } | ||
64 | sz = size(); | ||
65 | #ifdef VERBOSE | ||
66 | printf("M %zu=", sz); | ||
67 | #endif | ||
68 | a[i] = malloc(sz); | ||
69 | #ifdef VERBOSE | ||
70 | printf("%p\n", a[i]); | ||
71 | #endif | ||
72 | if (a[i]) | ||
73 | memset(a[i], 0xff, sz); | ||
74 | break; | ||
75 | case 1: | ||
76 | sz = size(); | ||
77 | #ifdef VERBOSE | ||
78 | printf("R %p %zu=", a[i], sz); | ||
79 | #endif | ||
80 | q = realloc(a[i], sz); | ||
81 | #ifdef VERBOSE | ||
82 | printf("%p\n", q); | ||
83 | #endif | ||
84 | if (q) { | ||
85 | a[i]= q; | ||
86 | if (a[i]) | ||
87 | memset(a[i], 0xff, sz); | ||
88 | } | ||
89 | break; | ||
90 | } | ||
91 | } | ||
92 | for (i = 0; i < N; i++) | ||
93 | free(a[i]); | ||
94 | printf("\n"); | ||
95 | return 0; | ||
96 | } | ||