new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / external / pcre2-10.32 / doc / pcre2jit.3
1 .TH PCRE2JIT 3 "28 June 2018" "PCRE2 10.32"
2 .SH NAME
3 PCRE2 - Perl-compatible regular expressions (revised API)
4 .SH "PCRE2 JUST-IN-TIME COMPILER SUPPORT"
5 .rs
6 .sp
7 Just-in-time compiling is a heavyweight optimization that can greatly speed up
8 pattern matching. However, it comes at the cost of extra processing before the
9 match is performed, so it is of most benefit when the same pattern is going to
10 be matched many times. This does not necessarily mean many calls of a matching
11 function; if the pattern is not anchored, matching attempts may take place many
12 times at various positions in the subject, even for a single call. Therefore,
13 if the subject string is very long, it may still pay to use JIT even for
14 one-off matches. JIT support is available for all of the 8-bit, 16-bit and
15 32-bit PCRE2 libraries.
16 .P
17 JIT support applies only to the traditional Perl-compatible matching function.
18 It does not apply when the DFA matching function is being used. The code for
19 this support was written by Zoltan Herczeg.
20 .
21 .
22 .SH "AVAILABILITY OF JIT SUPPORT"
23 .rs
24 .sp
25 JIT support is an optional feature of PCRE2. The "configure" option
26 --enable-jit (or equivalent CMake option) must be set when PCRE2 is built if
27 you want to use JIT. The support is limited to the following hardware
28 platforms:
29 .sp
30   ARM 32-bit (v5, v7, and Thumb2)
31   ARM 64-bit
32   Intel x86 32-bit and 64-bit
33   MIPS 32-bit and 64-bit
34   Power PC 32-bit and 64-bit
35   SPARC 32-bit
36 .sp
37 If --enable-jit is set on an unsupported platform, compilation fails.
38 .P
39 A program can tell if JIT support is available by calling \fBpcre2_config()\fP
40 with the PCRE2_CONFIG_JIT option. The result is 1 when JIT is available, and 0
41 otherwise. However, a simple program does not need to check this in order to
42 use JIT. The API is implemented in a way that falls back to the interpretive
43 code if JIT is not available. For programs that need the best possible
44 performance, there is also a "fast path" API that is JIT-specific.
45 .
46 .
47 .SH "SIMPLE USE OF JIT"
48 .rs
49 .sp
50 To make use of the JIT support in the simplest way, all you have to do is to
51 call \fBpcre2_jit_compile()\fP after successfully compiling a pattern with
52 \fBpcre2_compile()\fP. This function has two arguments: the first is the
53 compiled pattern pointer that was returned by \fBpcre2_compile()\fP, and the
54 second is zero or more of the following option bits: PCRE2_JIT_COMPLETE,
55 PCRE2_JIT_PARTIAL_HARD, or PCRE2_JIT_PARTIAL_SOFT.
56 .P
57 If JIT support is not available, a call to \fBpcre2_jit_compile()\fP does
58 nothing and returns PCRE2_ERROR_JIT_BADOPTION. Otherwise, the compiled pattern
59 is passed to the JIT compiler, which turns it into machine code that executes
60 much faster than the normal interpretive code, but yields exactly the same
61 results. The returned value from \fBpcre2_jit_compile()\fP is zero on success,
62 or a negative error code.
63 .P
64 There is a limit to the size of pattern that JIT supports, imposed by the size
65 of machine stack that it uses. The exact rules are not documented because they
66 may change at any time, in particular, when new optimizations are introduced.
67 If a pattern is too big, a call to \fBpcre2_jit_compile()\fB returns
68 PCRE2_ERROR_NOMEMORY.
69 .P
70 PCRE2_JIT_COMPLETE requests the JIT compiler to generate code for complete
71 matches. If you want to run partial matches using the PCRE2_PARTIAL_HARD or
72 PCRE2_PARTIAL_SOFT options of \fBpcre2_match()\fP, you should set one or both
73 of the other options as well as, or instead of PCRE2_JIT_COMPLETE. The JIT
74 compiler generates different optimized code for each of the three modes
75 (normal, soft partial, hard partial). When \fBpcre2_match()\fP is called, the
76 appropriate code is run if it is available. Otherwise, the pattern is matched
77 using interpretive code.
78 .P
79 You can call \fBpcre2_jit_compile()\fP multiple times for the same compiled
80 pattern. It does nothing if it has previously compiled code for any of the
81 option bits. For example, you can call it once with PCRE2_JIT_COMPLETE and
82 (perhaps later, when you find you need partial matching) again with
83 PCRE2_JIT_COMPLETE and PCRE2_JIT_PARTIAL_HARD. This time it will ignore
84 PCRE2_JIT_COMPLETE and just compile code for partial matching. If
85 \fBpcre2_jit_compile()\fP is called with no option bits set, it immediately
86 returns zero. This is an alternative way of testing whether JIT is available.
87 .P
88 At present, it is not possible to free JIT compiled code except when the entire
89 compiled pattern is freed by calling \fBpcre2_code_free()\fP.
90 .P
91 In some circumstances you may need to call additional functions. These are
92 described in the section entitled
93 .\" HTML <a href="#stackcontrol">
94 .\" </a>
95 "Controlling the JIT stack"
96 .\"
97 below.
98 .P
99 There are some \fBpcre2_match()\fP options that are not supported by JIT, and
100 there are also some pattern items that JIT cannot handle. Details are given
101 below. In both cases, matching automatically falls back to the interpretive
102 code. If you want to know whether JIT was actually used for a particular match,
103 you should arrange for a JIT callback function to be set up as described in the
104 section entitled
105 .\" HTML <a href="#stackcontrol">
106 .\" </a>
107 "Controlling the JIT stack"
108 .\"
109 below, even if you do not need to supply a non-default JIT stack. Such a
110 callback function is called whenever JIT code is about to be obeyed. If the
111 match-time options are not right for JIT execution, the callback function is
112 not obeyed.
113 .P
114 If the JIT compiler finds an unsupported item, no JIT data is generated. You
115 can find out if JIT matching is available after compiling a pattern by calling
116 \fBpcre2_pattern_info()\fP with the PCRE2_INFO_JITSIZE option. A non-zero
117 result means that JIT compilation was successful. A result of 0 means that JIT
118 support is not available, or the pattern was not processed by
119 \fBpcre2_jit_compile()\fP, or the JIT compiler was not able to handle the
120 pattern.
121 .
122 .
123 .SH "UNSUPPORTED OPTIONS AND PATTERN ITEMS"
124 .rs
125 .sp
126 The \fBpcre2_match()\fP options that are supported for JIT matching are
127 PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY, PCRE2_NOTEMPTY_ATSTART,
128 PCRE2_NO_UTF_CHECK, PCRE2_PARTIAL_HARD, and PCRE2_PARTIAL_SOFT. The
129 PCRE2_ANCHORED option is not supported at match time.
130 .P
131 If the PCRE2_NO_JIT option is passed to \fBpcre2_match()\fP it disables the
132 use of JIT, forcing matching by the interpreter code.
133 .P
134 The only unsupported pattern items are \eC (match a single data unit) when
135 running in a UTF mode, and a callout immediately before an assertion condition
136 in a conditional group.
137 .
138 .
139 .SH "RETURN VALUES FROM JIT MATCHING"
140 .rs
141 .sp
142 When a pattern is matched using JIT matching, the return values are the same
143 as those given by the interpretive \fBpcre2_match()\fP code, with the addition
144 of one new error code: PCRE2_ERROR_JIT_STACKLIMIT. This means that the memory
145 used for the JIT stack was insufficient. See
146 .\" HTML <a href="#stackcontrol">
147 .\" </a>
148 "Controlling the JIT stack"
149 .\"
150 below for a discussion of JIT stack usage.
151 .P
152 The error code PCRE2_ERROR_MATCHLIMIT is returned by the JIT code if searching
153 a very large pattern tree goes on for too long, as it is in the same
154 circumstance when JIT is not used, but the details of exactly what is counted
155 are not the same. The PCRE2_ERROR_DEPTHLIMIT error code is never returned
156 when JIT matching is used.
157 .
158 .
159 .\" HTML <a name="stackcontrol"></a>
160 .SH "CONTROLLING THE JIT STACK"
161 .rs
162 .sp
163 When the compiled JIT code runs, it needs a block of memory to use as a stack.
164 By default, it uses 32KiB on the machine stack. However, some large or
165 complicated patterns need more than this. The error PCRE2_ERROR_JIT_STACKLIMIT
166 is given when there is not enough stack. Three functions are provided for
167 managing blocks of memory for use as JIT stacks. There is further discussion
168 about the use of JIT stacks in the section entitled
169 .\" HTML <a href="#stackfaq">
170 .\" </a>
171 "JIT stack FAQ"
172 .\"
173 below.
174 .P
175 The \fBpcre2_jit_stack_create()\fP function creates a JIT stack. Its arguments
176 are a starting size, a maximum size, and a general context (for memory
177 allocation functions, or NULL for standard memory allocation). It returns a
178 pointer to an opaque structure of type \fBpcre2_jit_stack\fP, or NULL if there
179 is an error. The \fBpcre2_jit_stack_free()\fP function is used to free a stack
180 that is no longer needed. If its argument is NULL, this function returns
181 immediately, without doing anything. (For the technically minded: the address
182 space is allocated by mmap or VirtualAlloc.) A maximum stack size of 512KiB to
183 1MiB should be more than enough for any pattern.
184 .P
185 The \fBpcre2_jit_stack_assign()\fP function specifies which stack JIT code
186 should use. Its arguments are as follows:
187 .sp
188   pcre2_match_context  *mcontext
189   pcre2_jit_callback    callback
190   void                 *data
191 .sp
192 The first argument is a pointer to a match context. When this is subsequently
193 passed to a matching function, its information determines which JIT stack is
194 used. If this argument is NULL, the function returns immediately, without doing
195 anything. There are three cases for the values of the other two options:
196 .sp
197   (1) If \fIcallback\fP is NULL and \fIdata\fP is NULL, an internal 32KiB block
198       on the machine stack is used. This is the default when a match
199       context is created.
200 .sp
201   (2) If \fIcallback\fP is NULL and \fIdata\fP is not NULL, \fIdata\fP must be
202       a pointer to a valid JIT stack, the result of calling
203       \fBpcre2_jit_stack_create()\fP.
204 .sp
205   (3) If \fIcallback\fP is not NULL, it must point to a function that is
206       called with \fIdata\fP as an argument at the start of matching, in
207       order to set up a JIT stack. If the return from the callback
208       function is NULL, the internal 32KiB stack is used; otherwise the
209       return value must be a valid JIT stack, the result of calling
210       \fBpcre2_jit_stack_create()\fP.
211 .sp
212 A callback function is obeyed whenever JIT code is about to be run; it is not
213 obeyed when \fBpcre2_match()\fP is called with options that are incompatible
214 for JIT matching. A callback function can therefore be used to determine
215 whether a match operation was executed by JIT or by the interpreter.
216 .P
217 You may safely use the same JIT stack for more than one pattern (either by
218 assigning directly or by callback), as long as the patterns are matched
219 sequentially in the same thread. Currently, the only way to set up
220 non-sequential matches in one thread is to use callouts: if a callout function
221 starts another match, that match must use a different JIT stack to the one used
222 for currently suspended match(es).
223 .P
224 In a multithread application, if you do not
225 specify a JIT stack, or if you assign or pass back NULL from a callback, that
226 is thread-safe, because each thread has its own machine stack. However, if you
227 assign or pass back a non-NULL JIT stack, this must be a different stack for
228 each thread so that the application is thread-safe.
229 .P
230 Strictly speaking, even more is allowed. You can assign the same non-NULL stack
231 to a match context that is used by any number of patterns, as long as they are
232 not used for matching by multiple threads at the same time. For example, you
233 could use the same stack in all compiled patterns, with a global mutex in the
234 callback to wait until the stack is available for use. However, this is an
235 inefficient solution, and not recommended.
236 .P
237 This is a suggestion for how a multithreaded program that needs to set up
238 non-default JIT stacks might operate:
239 .sp
240   During thread initalization
241     thread_local_var = pcre2_jit_stack_create(...)
242 .sp
243   During thread exit
244     pcre2_jit_stack_free(thread_local_var)
245 .sp
246   Use a one-line callback function
247     return thread_local_var
248 .sp
249 All the functions described in this section do nothing if JIT is not available.
250 .
251 .
252 .\" HTML <a name="stackfaq"></a>
253 .SH "JIT STACK FAQ"
254 .rs
255 .sp
256 (1) Why do we need JIT stacks?
257 .sp
258 PCRE2 (and JIT) is a recursive, depth-first engine, so it needs a stack where
259 the local data of the current node is pushed before checking its child nodes.
260 Allocating real machine stack on some platforms is difficult. For example, the
261 stack chain needs to be updated every time if we extend the stack on PowerPC.
262 Although it is possible, its updating time overhead decreases performance. So
263 we do the recursion in memory.
264 .P
265 (2) Why don't we simply allocate blocks of memory with \fBmalloc()\fP?
266 .sp
267 Modern operating systems have a nice feature: they can reserve an address space
268 instead of allocating memory. We can safely allocate memory pages inside this
269 address space, so the stack could grow without moving memory data (this is
270 important because of pointers). Thus we can allocate 1MiB address space, and
271 use only a single memory page (usually 4KiB) if that is enough. However, we can
272 still grow up to 1MiB anytime if needed.
273 .P
274 (3) Who "owns" a JIT stack?
275 .sp
276 The owner of the stack is the user program, not the JIT studied pattern or
277 anything else. The user program must ensure that if a stack is being used by
278 \fBpcre2_match()\fP, (that is, it is assigned to a match context that is passed
279 to the pattern currently running), that stack must not be used by any other
280 threads (to avoid overwriting the same memory area). The best practice for
281 multithreaded programs is to allocate a stack for each thread, and return this
282 stack through the JIT callback function.
283 .P
284 (4) When should a JIT stack be freed?
285 .sp
286 You can free a JIT stack at any time, as long as it will not be used by
287 \fBpcre2_match()\fP again. When you assign the stack to a match context, only a
288 pointer is set. There is no reference counting or any other magic. You can free
289 compiled patterns, contexts, and stacks in any order, anytime. Just \fIdo
290 not\fP call \fBpcre2_match()\fP with a match context pointing to an already
291 freed stack, as that will cause SEGFAULT. (Also, do not free a stack currently
292 used by \fBpcre2_match()\fP in another thread). You can also replace the stack
293 in a context at any time when it is not in use. You should free the previous
294 stack before assigning a replacement.
295 .P
296 (5) Should I allocate/free a stack every time before/after calling
297 \fBpcre2_match()\fP?
298 .sp
299 No, because this is too costly in terms of resources. However, you could
300 implement some clever idea which release the stack if it is not used in let's
301 say two minutes. The JIT callback can help to achieve this without keeping a
302 list of patterns.
303 .P
304 (6) OK, the stack is for long term memory allocation. But what happens if a
305 pattern causes stack overflow with a stack of 1MiB? Is that 1MiB kept until the
306 stack is freed?
307 .sp
308 Especially on embedded sytems, it might be a good idea to release memory
309 sometimes without freeing the stack. There is no API for this at the moment.
310 Probably a function call which returns with the currently allocated memory for
311 any stack and another which allows releasing memory (shrinking the stack) would
312 be a good idea if someone needs this.
313 .P
314 (7) This is too much of a headache. Isn't there any better solution for JIT
315 stack handling?
316 .sp
317 No, thanks to Windows. If POSIX threads were used everywhere, we could throw
318 out this complicated API.
319 .
320 .
321 .SH "FREEING JIT SPECULATIVE MEMORY"
322 .rs
323 .sp
324 .nf
325 .B void pcre2_jit_free_unused_memory(pcre2_general_context *\fIgcontext\fP);
326 .fi
327 .P
328 The JIT executable allocator does not free all memory when it is possible.
329 It expects new allocations, and keeps some free memory around to improve
330 allocation speed. However, in low memory conditions, it might be better to free
331 all possible memory. You can cause this to happen by calling
332 pcre2_jit_free_unused_memory(). Its argument is a general context, for custom
333 memory management, or NULL for standard memory management.
334 .
335 .
336 .SH "EXAMPLE CODE"
337 .rs
338 .sp
339 This is a single-threaded example that specifies a JIT stack without using a
340 callback. A real program should include error checking after all the function
341 calls.
342 .sp
343   int rc;
344   pcre2_code *re;
345   pcre2_match_data *match_data;
346   pcre2_match_context *mcontext;
347   pcre2_jit_stack *jit_stack;
348 .sp
349   re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0,
350     &errornumber, &erroffset, NULL);
351   rc = pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);
352   mcontext = pcre2_match_context_create(NULL);
353   jit_stack = pcre2_jit_stack_create(32*1024, 512*1024, NULL);
354   pcre2_jit_stack_assign(mcontext, NULL, jit_stack);
355   match_data = pcre2_match_data_create(re, 10);
356   rc = pcre2_match(re, subject, length, 0, 0, match_data, mcontext);
357   /* Process result */
358 .sp
359   pcre2_code_free(re);
360   pcre2_match_data_free(match_data);
361   pcre2_match_context_free(mcontext);
362   pcre2_jit_stack_free(jit_stack);
363 .sp
364 .
365 .
366 .SH "JIT FAST PATH API"
367 .rs
368 .sp
369 Because the API described above falls back to interpreted matching when JIT is
370 not available, it is convenient for programs that are written for general use
371 in many environments. However, calling JIT via \fBpcre2_match()\fP does have a
372 performance impact. Programs that are written for use where JIT is known to be
373 available, and which need the best possible performance, can instead use a
374 "fast path" API to call JIT matching directly instead of calling
375 \fBpcre2_match()\fP (obviously only for patterns that have been successfully
376 processed by \fBpcre2_jit_compile()\fP).
377 .P
378 The fast path function is called \fBpcre2_jit_match()\fP, and it takes exactly
379 the same arguments as \fBpcre2_match()\fP. The return values are also the same,
380 plus PCRE2_ERROR_JIT_BADOPTION if a matching mode (partial or complete) is
381 requested that was not compiled. Unsupported option bits (for example,
382 PCRE2_ANCHORED) are ignored, as is the PCRE2_NO_JIT option.
383 .P
384 When you call \fBpcre2_match()\fP, as well as testing for invalid options, a
385 number of other sanity checks are performed on the arguments. For example, if
386 the subject pointer is NULL, an immediate error is given. Also, unless
387 PCRE2_NO_UTF_CHECK is set, a UTF subject string is tested for validity. In the
388 interests of speed, these checks do not happen on the JIT fast path, and if
389 invalid data is passed, the result is undefined.
390 .P
391 Bypassing the sanity checks and the \fBpcre2_match()\fP wrapping can give
392 speedups of more than 10%.
393 .
394 .
395 .SH "SEE ALSO"
396 .rs
397 .sp
398 \fBpcre2api\fP(3)
399 .
400 .
401 .SH AUTHOR
402 .rs
403 .sp
404 .nf
405 Philip Hazel (FAQ by Zoltan Herczeg)
406 University Computing Service
407 Cambridge, England.
408 .fi
409 .
410 .
411 .SH REVISION
412 .rs
413 .sp
414 .nf
415 Last updated: 28 June 2018
416 Copyright (c) 1997-2018 University of Cambridge.
417 .fi