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