1PCRESTACK(3) Library Functions Manual PCRESTACK(3)
2
3
4
6 PCRE - Perl-compatible regular expressions
7
9
10 When you call pcre_exec(), it makes use of an internal function called
11 match(). This calls itself recursively at branch points in the pattern,
12 in order to remember the state of the match so that it can back up and
13 try a different alternative if the first one fails. As matching pro‐
14 ceeds deeper and deeper into the tree of possibilities, the recursion
15 depth increases.
16
17 Not all calls of match() increase the recursion depth; for an item such
18 as a* it may be called several times at the same level, after matching
19 different numbers of a's. Furthermore, in a number of cases where the
20 result of the recursive call would immediately be passed back as the
21 result of the current call (a "tail recursion"), the function is just
22 restarted instead.
23
24 The pcre_dfa_exec() function operates in an entirely different way, and
25 uses recursion only when there is a regular expression recursion or
26 subroutine call in the pattern. This includes the processing of asser‐
27 tion and "once-only" subpatterns, which are handled like subroutine
28 calls. Normally, these are never very deep, and the limit on the com‐
29 plexity of pcre_dfa_exec() is controlled by the amount of workspace it
30 is given. However, it is possible to write patterns with runaway infi‐
31 nite recursions; such patterns will cause pcre_dfa_exec() to run out of
32 stack. At present, there is no protection against this.
33
34 The comments that follow do NOT apply to pcre_dfa_exec(); they are rel‐
35 evant only for pcre_exec().
36
37 Reducing pcre_exec()'s stack usage
38
39 Each time that match() is actually called recursively, it uses memory
40 from the process stack. For certain kinds of pattern and data, very
41 large amounts of stack may be needed, despite the recognition of "tail
42 recursion". You can often reduce the amount of recursion, and there‐
43 fore the amount of stack used, by modifying the pattern that is being
44 matched. Consider, for example, this pattern:
45
46 ([^<]|<(?!inet))+
47
48 It matches from wherever it starts until it encounters "<inet" or the
49 end of the data, and is the kind of pattern that might be used when
50 processing an XML file. Each iteration of the outer parentheses matches
51 either one character that is not "<" or a "<" that is not followed by
52 "inet". However, each time a parenthesis is processed, a recursion
53 occurs, so this formulation uses a stack frame for each matched charac‐
54 ter. For a long string, a lot of stack is required. Consider now this
55 rewritten pattern, which matches exactly the same strings:
56
57 ([^<]++|<(?!inet))+
58
59 This uses very much less stack, because runs of characters that do not
60 contain "<" are "swallowed" in one item inside the parentheses. Recur‐
61 sion happens only when a "<" character that is not followed by "inet"
62 is encountered (and we assume this is relatively rare). A possessive
63 quantifier is used to stop any backtracking into the runs of non-"<"
64 characters, but that is not related to stack usage.
65
66 This example shows that one way of avoiding stack problems when match‐
67 ing long subject strings is to write repeated parenthesized subpatterns
68 to match more than one character whenever possible.
69
70 Compiling PCRE to use heap instead of stack for pcre_exec()
71
72 In environments where stack memory is constrained, you might want to
73 compile PCRE to use heap memory instead of stack for remembering back-
74 up points when pcre_exec() is running. This makes it run a lot more
75 slowly, however. Details of how to do this are given in the pcrebuild
76 documentation. When built in this way, instead of using the stack, PCRE
77 obtains and frees memory by calling the functions that are pointed to
78 by the pcre_stack_malloc and pcre_stack_free variables. By default,
79 these point to malloc() and free(), but you can replace the pointers to
80 cause PCRE to use your own functions. Since the block sizes are always
81 the same, and are always freed in reverse order, it may be possible to
82 implement customized memory handlers that are more efficient than the
83 standard functions.
84
85 Limiting pcre_exec()'s stack usage
86
87 You can set limits on the number of times that match() is called, both
88 in total and recursively. If a limit is exceeded, pcre_exec() returns
89 an error code. Setting suitable limits should prevent it from running
90 out of stack. The default values of the limits are very large, and
91 unlikely ever to operate. They can be changed when PCRE is built, and
92 they can also be set when pcre_exec() is called. For details of these
93 interfaces, see the pcrebuild documentation and the section on extra
94 data for pcre_exec() in the pcreapi documentation.
95
96 As a very rough rule of thumb, you should reckon on about 500 bytes per
97 recursion. Thus, if you want to limit your stack usage to 8Mb, you
98 should set the limit at 16000 recursions. A 64Mb stack, on the other
99 hand, can support around 128000 recursions.
100
101 In Unix-like environments, the pcretest test program has a command line
102 option (-S) that can be used to increase the size of its stack. As long
103 as the stack is large enough, another option (-M) can be used to find
104 the smallest limits that allow a particular pattern to match a given
105 subject string. This is done by calling pcre_exec() repeatedly with
106 different limits.
107
108 Changing stack size in Unix-like systems
109
110 In Unix-like environments, there is not often a problem with the stack
111 unless very long strings are involved, though the default limit on
112 stack size varies from system to system. Values from 8Mb to 64Mb are
113 common. You can find your default limit by running the command:
114
115 ulimit -s
116
117 Unfortunately, the effect of running out of stack is often SIGSEGV,
118 though sometimes a more explicit error message is given. You can nor‐
119 mally increase the limit on stack size by code such as this:
120
121 struct rlimit rlim;
122 getrlimit(RLIMIT_STACK, &rlim);
123 rlim.rlim_cur = 100*1024*1024;
124 setrlimit(RLIMIT_STACK, &rlim);
125
126 This reads the current limits (soft and hard) using getrlimit(), then
127 attempts to increase the soft limit to 100Mb using setrlimit(). You
128 must do this before calling pcre_exec().
129
130 Changing stack size in Mac OS X
131
132 Using setrlimit(), as described above, should also work on Mac OS X. It
133 is also possible to set a stack size when linking a program. There is a
134 discussion about stack sizes in Mac OS X at this web site:
135 http://developer.apple.com/qa/qa2005/qa1419.html.
136
138
139 Philip Hazel
140 University Computing Service
141 Cambridge CB2 3QH, England.
142
144
145 Last updated: 03 January 2010
146 Copyright (c) 1997-2010 University of Cambridge.
147
148
149
150 PCRESTACK(3)