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