Home | History | Annotate | Line # | Download | only in regex
t_exhaust.c revision 1.12
      1 /*	$NetBSD: t_exhaust.c,v 1.12 2021/06/09 20:48:37 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: t_exhaust.c,v 1.12 2021/06/09 20:48:37 christos Exp $");
     41 
     42 #include <sys/resource.h>
     43 #include <err.h>
     44 
     45 #ifdef TEST
     46 # include <assert.h>
     47 # define ATF_REQUIRE(a) assert(a)
     48 # define ATF_REQUIRE_MSG(a, fmt, ...) \
     49     if (!(a)) err(EXIT_FAILURE, fmt, __VA_ARGS__)
     50 #else
     51 # include <atf-c.h>
     52 #endif
     53 
     54 #include <regex.h>
     55 #include <dlfcn.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <signal.h>
     60 
     61 #ifndef REGEX_MAXSIZE
     62 #define REGEX_MAXSIZE	9999
     63 #endif
     64 
     65 #ifdef TRACE
     66 void *
     67 malloc(size_t l)
     68 {
     69 	static void *(*m)(size_t);
     70 	static int q;
     71 	if (m == NULL) m = dlsym(RTLD_NEXT, "malloc");
     72 	void *p = (*m)(l);
     73 	if (q)
     74 		return p;
     75 	q = 1;
     76 	printf("%p m %zu\n", p, l);
     77 	if (p == (void *)0x7f7ff7e21ac0)
     78 		kill(0, SIGSTOP);
     79 	q = 0;
     80 	return p;
     81 }
     82 
     83 void
     84 free(void *p)
     85 {
     86 	static void (*f)(void *);
     87 	if (f == NULL) f = dlsym(RTLD_NEXT, "malloc");
     88 	printf("%p f\n", p);
     89 	(*f)(p);
     90 }
     91 #endif
     92 
     93 static char *
     94 mkstr(const char *str, size_t len)
     95 {
     96 	size_t slen = strlen(str);
     97 	char *p = malloc(slen * len + 1);
     98 	ATF_REQUIRE_MSG(p != NULL, "slen=%zu, len=%zu", slen, len);
     99 	for (size_t i = 0; i < len; i++)
    100 		strcpy(&p[i * slen], str);
    101 	return p;
    102 }
    103 
    104 static char *
    105 concat(const char *d, const char *s)
    106 {
    107 	size_t dlen = strlen(d);
    108 	size_t slen = strlen(s);
    109 	char *p = malloc(dlen + slen + 1);
    110 
    111 	ATF_REQUIRE_MSG(p != NULL, "slen=%zu, dlen=%zu", slen, dlen);
    112 	strcpy(p, d);
    113 	strcpy(p + dlen, s);
    114 	return p;
    115 }
    116 
    117 static char *
    118 p0(size_t len)
    119 {
    120 	char *d, *s1, *s2;
    121 	s1 = mkstr("\\(", len);
    122 	s2 = concat(s1, ")");
    123 	free(s1);
    124 	d = concat("(", s2);
    125 	free(s2);
    126 	return d;
    127 }
    128 
    129 static char *
    130 p1(size_t len)
    131 {
    132 	char *d, *s1, *s2, *s3;
    133 	s1 = mkstr("\\(", 60);
    134 	s2 = mkstr("(.*)", len);
    135 	s3 = concat(s1, s2);
    136 	free(s2);
    137 	free(s1);
    138 	s1 = concat(s3, ")");
    139 	free(s3);
    140 	d = concat("(", s1);
    141 	free(s1);
    142 	return d;
    143 }
    144 
    145 static char *
    146 ps(const char *m, const char *s, size_t len)
    147 {
    148 	char *d, *s1, *s2, *s3;
    149 	s1 = mkstr(m, len);
    150 	s2 = mkstr(s, len);
    151 	s3 = concat(s1, s2);
    152 	free(s2);
    153 	free(s1);
    154 	d = concat("(.?)", s3);
    155 	free(s3);
    156 	return d;
    157 }
    158 
    159 static char *
    160 p2(size_t len)
    161 {
    162 	return ps("((.*){0,255}", ")", len);
    163 }
    164 
    165 static char *
    166 p3(size_t len)
    167 {
    168 	return ps("(.\\{0,}", ")", len);
    169 }
    170 
    171 static char *
    172 p4(size_t len)
    173 {
    174 	return ps("((.*){1,255}", ")", len);
    175 }
    176 
    177 static char *
    178 p5(size_t len)
    179 {
    180 	return ps("(", "){1,100}", len);
    181 }
    182 
    183 static char *
    184 p6(size_t len)
    185 {
    186 	char *d, *s1, *s2;
    187 	s1 = mkstr("(?:(.*)|", len);
    188 	s2 = concat(s1, "(.*)");
    189 	free(s1);
    190 	s1 = mkstr(")", len);
    191 	d = concat(s2, s1);
    192 	free(s1);
    193 	free(s2);
    194 	return d;
    195 }
    196 
    197 static const struct {
    198 	char *(*pattern)(size_t);
    199 	int type;
    200 } tests[] = {
    201 	{ p0, REG_EXTENDED },
    202 	{ p1, REG_EXTENDED },
    203 	{ p2, REG_EXTENDED },
    204 	{ p3, REG_EXTENDED },
    205 	{ p4, REG_EXTENDED },
    206 	{ p5, REG_EXTENDED },
    207 	{ p6, REG_BASIC },
    208 };
    209 
    210 static void
    211 run(void)
    212 {
    213 	regex_t re;
    214 	int e;
    215 	struct rlimit limit;
    216 	char *patterns[__arraycount(tests)];
    217 
    218 	for (size_t i = 0; i < __arraycount(patterns); i++) {
    219 		patterns[i] = (*tests[i].pattern)(REGEX_MAXSIZE);
    220 	}
    221 
    222 	limit.rlim_cur = limit.rlim_max = 256 * 1024 * 1024;
    223 	ATF_REQUIRE(setrlimit(RLIMIT_VMEM, &limit) != -1);
    224 
    225 	for (size_t i = 0; i < __arraycount(tests); i++) {
    226 		e = regcomp(&re, patterns[i], tests[i].type);
    227 		if (e) {
    228 			char ebuf[1024];
    229 			(void)regerror(e, &re, ebuf, sizeof(ebuf));
    230 			ATF_REQUIRE_MSG(e == REG_ESPACE,
    231 			    "regcomp returned %d (%s) for pattern %zu [%s]", e,
    232 			    ebuf, i, patterns[i]);
    233 			continue;
    234 		}
    235 		(void)regexec(&re, "aaaaaaaaaaa", 0, NULL, 0);
    236 		regfree(&re);
    237 	}
    238 	for (size_t i = 0; i < __arraycount(patterns); i++) {
    239 		free(patterns[i]);
    240 	}
    241 }
    242 
    243 #ifndef TEST
    244 
    245 ATF_TC(regcomp_too_big);
    246 
    247 ATF_TC_HEAD(regcomp_too_big, tc)
    248 {
    249 
    250 	atf_tc_set_md_var(tc, "descr", "Check that large patterns don't"
    251 	    " crash, but return a proper error code");
    252 	// libtre needs it.
    253 	atf_tc_set_md_var(tc, "timeout", "600");
    254 	atf_tc_set_md_var(tc, "require.memory", "256M");
    255 }
    256 
    257 ATF_TC_BODY(regcomp_too_big, tc)
    258 {
    259 	run();
    260 }
    261 
    262 ATF_TP_ADD_TCS(tp)
    263 {
    264 
    265 	ATF_TP_ADD_TC(tp, regcomp_too_big);
    266 	return atf_no_error();
    267 }
    268 #else
    269 int
    270 main(void)
    271 {
    272 	run();
    273 	return 0;
    274 }
    275 #endif
    276