Module Name:    src
Committed By:   christos
Date:           Wed Jun  9 20:48:37 UTC 2021

Modified Files:
        src/tests/lib/libc/regex: t_exhaust.c

Log Message:
1. Preallocate the patterns so that out of memory conditions don't happen
   during their allocation, which the test cannot handle properly.
2. Enable building the test without atf so that we can easily debug with
   atf memory allocations interfering
3. Add memory tracing (disabled)


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/tests/lib/libc/regex/t_exhaust.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libc/regex/t_exhaust.c
diff -u src/tests/lib/libc/regex/t_exhaust.c:1.11 src/tests/lib/libc/regex/t_exhaust.c:1.12
--- src/tests/lib/libc/regex/t_exhaust.c:1.11	Mon Jun  7 07:45:35 2021
+++ src/tests/lib/libc/regex/t_exhaust.c	Wed Jun  9 16:48:37 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_exhaust.c,v 1.11 2021/06/07 11:45:35 christos Exp $	*/
+/*	$NetBSD: t_exhaust.c,v 1.12 2021/06/09 20:48:37 christos Exp $	*/
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -37,20 +37,59 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_exhaust.c,v 1.11 2021/06/07 11:45:35 christos Exp $");
+__RCSID("$NetBSD: t_exhaust.c,v 1.12 2021/06/09 20:48:37 christos Exp $");
 
 #include <sys/resource.h>
-#include <atf-c.h>
 #include <err.h>
+
+#ifdef TEST
+# include <assert.h>
+# define ATF_REQUIRE(a) assert(a)
+# define ATF_REQUIRE_MSG(a, fmt, ...) \
+    if (!(a)) err(EXIT_FAILURE, fmt, __VA_ARGS__)
+#else
+# include <atf-c.h>
+#endif
+
 #include <regex.h>
+#include <dlfcn.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <signal.h>
 
 #ifndef REGEX_MAXSIZE
 #define REGEX_MAXSIZE	9999
 #endif
 
+#ifdef TRACE
+void *
+malloc(size_t l)
+{
+	static void *(*m)(size_t);
+	static int q;
+	if (m == NULL) m = dlsym(RTLD_NEXT, "malloc");
+	void *p = (*m)(l);
+	if (q)
+		return p;
+	q = 1;
+	printf("%p m %zu\n", p, l);
+	if (p == (void *)0x7f7ff7e21ac0)
+		kill(0, SIGSTOP);
+	q = 0;
+	return p;
+}
+
+void
+free(void *p)
+{
+	static void (*f)(void *);
+	if (f == NULL) f = dlsym(RTLD_NEXT, "malloc");
+	printf("%p f\n", p);
+	(*f)(p);
+}
+#endif
+
 static char *
 mkstr(const char *str, size_t len)
 {
@@ -168,43 +207,56 @@ static const struct {
 	{ p6, REG_BASIC },
 };
 
-ATF_TC(regcomp_too_big);
-
-ATF_TC_HEAD(regcomp_too_big, tc)
-{
-
-	atf_tc_set_md_var(tc, "descr", "Check that large patterns don't"
-	    " crash, but return a proper error code");
-	// libtre needs it.
-	atf_tc_set_md_var(tc, "timeout", "600");
-	atf_tc_set_md_var(tc, "require.memory", "256M");
-}
-
-ATF_TC_BODY(regcomp_too_big, tc)
+static void
+run(void)
 {
 	regex_t re;
 	int e;
 	struct rlimit limit;
+	char *patterns[__arraycount(tests)];
+
+	for (size_t i = 0; i < __arraycount(patterns); i++) {
+		patterns[i] = (*tests[i].pattern)(REGEX_MAXSIZE);
+	}
 
 	limit.rlim_cur = limit.rlim_max = 256 * 1024 * 1024;
 	ATF_REQUIRE(setrlimit(RLIMIT_VMEM, &limit) != -1);
 
 	for (size_t i = 0; i < __arraycount(tests); i++) {
-		char *d = (*tests[i].pattern)(REGEX_MAXSIZE);
-		e = regcomp(&re, d, tests[i].type);
+		e = regcomp(&re, patterns[i], tests[i].type);
 		if (e) {
 			char ebuf[1024];
 			(void)regerror(e, &re, ebuf, sizeof(ebuf));
 			ATF_REQUIRE_MSG(e == REG_ESPACE,
-			    "regcomp returned %d (%s) for pattern %zu [%s]", e, ebuf,
-			    i, d);
-			free(d);
+			    "regcomp returned %d (%s) for pattern %zu [%s]", e,
+			    ebuf, i, patterns[i]);
 			continue;
 		}
-		free(d);
 		(void)regexec(&re, "aaaaaaaaaaa", 0, NULL, 0);
 		regfree(&re);
 	}
+	for (size_t i = 0; i < __arraycount(patterns); i++) {
+		free(patterns[i]);
+	}
+}
+
+#ifndef TEST
+
+ATF_TC(regcomp_too_big);
+
+ATF_TC_HEAD(regcomp_too_big, tc)
+{
+
+	atf_tc_set_md_var(tc, "descr", "Check that large patterns don't"
+	    " crash, but return a proper error code");
+	// libtre needs it.
+	atf_tc_set_md_var(tc, "timeout", "600");
+	atf_tc_set_md_var(tc, "require.memory", "256M");
+}
+
+ATF_TC_BODY(regcomp_too_big, tc)
+{
+	run();
 }
 
 ATF_TP_ADD_TCS(tp)
@@ -213,3 +265,11 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, regcomp_too_big);
 	return atf_no_error();
 }
+#else
+int
+main(void)
+{
+	run();
+	return 0;
+}
+#endif

Reply via email to