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