summaryrefslogtreecommitdiffstats
path: root/regress/lib/libc/malloc/malloc_general/malloc_general.c
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2017-01-23 11:06:32 +0000
committerotto <otto@openbsd.org>2017-01-23 11:06:32 +0000
commit6a13ef69787db04ae501a22e92fa10865b44fd7c (patch)
tree694e868454d37eab17f6b6db0917e5ca3948aa7a /regress/lib/libc/malloc/malloc_general/malloc_general.c
parentWrap function declarations with __{BEGIN,END}_HIDDEN_DECLS to reduce (diff)
downloadwireguard-openbsd-6a13ef69787db04ae501a22e92fa10865b44fd7c.tar.xz
wireguard-openbsd-6a13ef69787db04ae501a22e92fa10865b44fd7c.zip
test malloc/realloc/free with some flag combo's
Diffstat (limited to 'regress/lib/libc/malloc/malloc_general/malloc_general.c')
-rw-r--r--regress/lib/libc/malloc/malloc_general/malloc_general.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/regress/lib/libc/malloc/malloc_general/malloc_general.c b/regress/lib/libc/malloc/malloc_general/malloc_general.c
new file mode 100644
index 00000000000..336326bc57c
--- /dev/null
+++ b/regress/lib/libc/malloc/malloc_general/malloc_general.c
@@ -0,0 +1,96 @@
+/* $OpenBSD */
+/*
+ * Copyright (c) 2017 Otto Moerbeek <otto@drijf.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* $define VERBOSE */
+
+#define N 1000
+
+size_t size(void)
+{
+ int p = arc4random_uniform(13) + 3;
+ return arc4random_uniform(1 << p);
+}
+
+void *a[N];
+
+extern char *malloc_options;
+
+int
+main(int argc, char *argv[])
+{
+ int count, p, i;
+ void * q;
+ size_t sz;
+
+ if (argc == 1)
+ errx(1, "usage: malloc_options");
+
+ malloc_options = argv[1];
+
+ for (count = 0; count < 800000; count++) {
+ if (count % 10000 == 0) {
+ printf(".");
+ fflush(stdout);
+ }
+ p = arc4random_uniform(2);
+ i = arc4random_uniform(N);
+ switch (p) {
+ case 0:
+ if (a[i]) {
+#ifdef VERBOSE
+ printf("F %p\n", a[i]);
+#endif
+ free(a[i]);
+ a[i] = NULL;
+ }
+ sz = size();
+#ifdef VERBOSE
+ printf("M %zu=", sz);
+#endif
+ a[i] = malloc(sz);
+#ifdef VERBOSE
+ printf("%p\n", a[i]);
+#endif
+ if (a[i])
+ memset(a[i], 0xff, sz);
+ break;
+ case 1:
+ sz = size();
+#ifdef VERBOSE
+ printf("R %p %zu=", a[i], sz);
+#endif
+ q = realloc(a[i], sz);
+#ifdef VERBOSE
+ printf("%p\n", q);
+#endif
+ if (q) {
+ a[i]= q;
+ if (a[i])
+ memset(a[i], 0xff, sz);
+ }
+ break;
+ }
+ }
+ for (i = 0; i < N; i++)
+ free(a[i]);
+ printf("\n");
+ return 0;
+}