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