blob: 78484dee2cabdf0ac91e2c65ac7172ce44afefca [file] [log] [blame]
Tristan Matthews04616462013-11-14 16:09:34 -05001<html>
2<head>
3<title>pcrestack specification</title>
4</head>
5<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6<h1>pcrestack man page</h1>
7<p>
8Return to the <a href="index.html">PCRE index page</a>.
9</p>
10<p>
11This page is part of the PCRE HTML documentation. It was generated automatically
12from the original man page. If there is any nonsense in it, please consult the
13man page, in case the conversion went wrong.
14<br>
15<br><b>
16PCRE DISCUSSION OF STACK USAGE
17</b><br>
18<P>
19When you call <b>pcre_exec()</b>, it makes use of an internal function called
20<b>match()</b>. This calls itself recursively at branch points in the pattern,
21in order to remember the state of the match so that it can back up and try a
22different alternative if the first one fails. As matching proceeds deeper and
23deeper into the tree of possibilities, the recursion depth increases. The
24<b>match()</b> function is also called in other circumstances, for example,
25whenever a parenthesized sub-pattern is entered, and in certain cases of
26repetition.
27</P>
28<P>
29Not all calls of <b>match()</b> increase the recursion depth; for an item such
30as a* it may be called several times at the same level, after matching
31different numbers of a's. Furthermore, in a number of cases where the result of
32the recursive call would immediately be passed back as the result of the
33current call (a "tail recursion"), the function is just restarted instead.
34</P>
35<P>
36The above comments apply when <b>pcre_exec()</b> is run in its normal
37interpretive manner. If the pattern was studied with the
38PCRE_STUDY_JIT_COMPILE option, and just-in-time compiling was successful, and
39the options passed to <b>pcre_exec()</b> were not incompatible, the matching
40process uses the JIT-compiled code instead of the <b>match()</b> function. In
41this case, the memory requirements are handled entirely differently. See the
42<a href="pcrejit.html"><b>pcrejit</b></a>
43documentation for details.
44</P>
45<P>
46The <b>pcre_dfa_exec()</b> function operates in an entirely different way, and
47uses recursion only when there is a regular expression recursion or subroutine
48call in the pattern. This includes the processing of assertion and "once-only"
49subpatterns, which are handled like subroutine calls. Normally, these are never
50very deep, and the limit on the complexity of <b>pcre_dfa_exec()</b> is
51controlled by the amount of workspace it is given. However, it is possible to
52write patterns with runaway infinite recursions; such patterns will cause
53<b>pcre_dfa_exec()</b> to run out of stack. At present, there is no protection
54against this.
55</P>
56<P>
57The comments that follow do NOT apply to <b>pcre_dfa_exec()</b>; they are
58relevant only for <b>pcre_exec()</b> without the JIT optimization.
59</P>
60<br><b>
61Reducing <b>pcre_exec()</b>'s stack usage
62</b><br>
63<P>
64Each time that <b>match()</b> is actually called recursively, it uses memory
65from the process stack. For certain kinds of pattern and data, very large
66amounts of stack may be needed, despite the recognition of "tail recursion".
67You can often reduce the amount of recursion, and therefore the amount of stack
68used, by modifying the pattern that is being matched. Consider, for example,
69this pattern:
70<pre>
71 ([^&#60;]|&#60;(?!inet))+
72</pre>
73It matches from wherever it starts until it encounters "&#60;inet" or the end of
74the data, and is the kind of pattern that might be used when processing an XML
75file. Each iteration of the outer parentheses matches either one character that
76is not "&#60;" or a "&#60;" that is not followed by "inet". However, each time a
77parenthesis is processed, a recursion occurs, so this formulation uses a stack
78frame for each matched character. For a long string, a lot of stack is
79required. Consider now this rewritten pattern, which matches exactly the same
80strings:
81<pre>
82 ([^&#60;]++|&#60;(?!inet))+
83</pre>
84This uses very much less stack, because runs of characters that do not contain
85"&#60;" are "swallowed" in one item inside the parentheses. Recursion happens only
86when a "&#60;" character that is not followed by "inet" is encountered (and we
87assume this is relatively rare). A possessive quantifier is used to stop any
88backtracking into the runs of non-"&#60;" characters, but that is not related to
89stack usage.
90</P>
91<P>
92This example shows that one way of avoiding stack problems when matching long
93subject strings is to write repeated parenthesized subpatterns to match more
94than one character whenever possible.
95</P>
96<br><b>
97Compiling PCRE to use heap instead of stack for <b>pcre_exec()</b>
98</b><br>
99<P>
100In environments where stack memory is constrained, you might want to compile
101PCRE to use heap memory instead of stack for remembering back-up points when
102<b>pcre_exec()</b> is running. This makes it run a lot more slowly, however.
103Details of how to do this are given in the
104<a href="pcrebuild.html"><b>pcrebuild</b></a>
105documentation. When built in this way, instead of using the stack, PCRE obtains
106and frees memory by calling the functions that are pointed to by the
107<b>pcre_stack_malloc</b> and <b>pcre_stack_free</b> variables. By default, these
108point to <b>malloc()</b> and <b>free()</b>, but you can replace the pointers to
109cause PCRE to use your own functions. Since the block sizes are always the
110same, and are always freed in reverse order, it may be possible to implement
111customized memory handlers that are more efficient than the standard functions.
112</P>
113<br><b>
114Limiting <b>pcre_exec()</b>'s stack usage
115</b><br>
116<P>
117You can set limits on the number of times that <b>match()</b> is called, both in
118total and recursively. If a limit is exceeded, <b>pcre_exec()</b> returns an
119error code. Setting suitable limits should prevent it from running out of
120stack. The default values of the limits are very large, and unlikely ever to
121operate. They can be changed when PCRE is built, and they can also be set when
122<b>pcre_exec()</b> is called. For details of these interfaces, see the
123<a href="pcrebuild.html"><b>pcrebuild</b></a>
124documentation and the
125<a href="pcreapi.html#extradata">section on extra data for <b>pcre_exec()</b></a>
126in the
127<a href="pcreapi.html"><b>pcreapi</b></a>
128documentation.
129</P>
130<P>
131As a very rough rule of thumb, you should reckon on about 500 bytes per
132recursion. Thus, if you want to limit your stack usage to 8Mb, you
133should set the limit at 16000 recursions. A 64Mb stack, on the other hand, can
134support around 128000 recursions.
135</P>
136<P>
137In Unix-like environments, the <b>pcretest</b> test program has a command line
138option (<b>-S</b>) that can be used to increase the size of its stack. As long
139as the stack is large enough, another option (<b>-M</b>) can be used to find the
140smallest limits that allow a particular pattern to match a given subject
141string. This is done by calling <b>pcre_exec()</b> repeatedly with different
142limits.
143</P>
144<br><b>
145Changing stack size in Unix-like systems
146</b><br>
147<P>
148In Unix-like environments, there is not often a problem with the stack unless
149very long strings are involved, though the default limit on stack size varies
150from system to system. Values from 8Mb to 64Mb are common. You can find your
151default limit by running the command:
152<pre>
153 ulimit -s
154</pre>
155Unfortunately, the effect of running out of stack is often SIGSEGV, though
156sometimes a more explicit error message is given. You can normally increase the
157limit on stack size by code such as this:
158<pre>
159 struct rlimit rlim;
160 getrlimit(RLIMIT_STACK, &rlim);
161 rlim.rlim_cur = 100*1024*1024;
162 setrlimit(RLIMIT_STACK, &rlim);
163</pre>
164This reads the current limits (soft and hard) using <b>getrlimit()</b>, then
165attempts to increase the soft limit to 100Mb using <b>setrlimit()</b>. You must
166do this before calling <b>pcre_exec()</b>.
167</P>
168<br><b>
169Changing stack size in Mac OS X
170</b><br>
171<P>
172Using <b>setrlimit()</b>, as described above, should also work on Mac OS X. It
173is also possible to set a stack size when linking a program. There is a
174discussion about stack sizes in Mac OS X at this web site:
175<a href="http://developer.apple.com/qa/qa2005/qa1419.html">http://developer.apple.com/qa/qa2005/qa1419.html.</a>
176</P>
177<br><b>
178AUTHOR
179</b><br>
180<P>
181Philip Hazel
182<br>
183University Computing Service
184<br>
185Cambridge CB2 3QH, England.
186<br>
187</P>
188<br><b>
189REVISION
190</b><br>
191<P>
192Last updated: 26 August 2011
193<br>
194Copyright &copy; 1997-2011 University of Cambridge.
195<br>
196<p>
197Return to the <a href="index.html">PCRE index page</a>.
198</p>