1PCREJIT(3)                 Library Functions Manual                 PCREJIT(3)
2
3
4

NAME

6       PCRE - Perl-compatible regular expressions
7

PCRE JUST-IN-TIME COMPILER SUPPORT

9
10       Just-in-time  compiling  is a heavyweight optimization that can greatly
11       speed up pattern matching. However, it comes at the cost of extra  pro‐
12       cessing before the match is performed. Therefore, it is of most benefit
13       when the same pattern is going to be matched many times. This does  not
14       necessarily  mean  many calls of a matching function; if the pattern is
15       not anchored, matching attempts may take place many  times  at  various
16       positions  in  the  subject, even for a single call.  Therefore, if the
17       subject string is very long, it may still pay to use  JIT  for  one-off
18       matches.
19
20       JIT  support  applies  only to the traditional Perl-compatible matching
21       function.  It does not apply when the DFA matching  function  is  being
22       used. The code for this support was written by Zoltan Herczeg.
23

8-BIT, 16-BIT AND 32-BIT SUPPORT

25
26       JIT  support  is available for all of the 8-bit, 16-bit and 32-bit PCRE
27       libraries. To keep this documentation simple, only the 8-bit  interface
28       is described in what follows. If you are using the 16-bit library, sub‐
29       stitute the  16-bit  functions  and  16-bit  structures  (for  example,
30       pcre16_jit_stack  instead  of  pcre_jit_stack).  If  you  are using the
31       32-bit library, substitute the 32-bit functions and  32-bit  structures
32       (for example, pcre32_jit_stack instead of pcre_jit_stack).
33

AVAILABILITY OF JIT SUPPORT

35
36       JIT  support  is  an  optional  feature of PCRE. The "configure" option
37       --enable-jit (or equivalent CMake option) must  be  set  when  PCRE  is
38       built  if  you want to use JIT. The support is limited to the following
39       hardware platforms:
40
41         ARM v5, v7, and Thumb2
42         Intel x86 32-bit and 64-bit
43         MIPS 32-bit
44         Power PC 32-bit and 64-bit
45         SPARC 32-bit (experimental)
46
47       If --enable-jit is set on an unsupported platform, compilation fails.
48
49       A program that is linked with PCRE 8.20 or later can tell if  JIT  sup‐
50       port  is  available  by  calling pcre_config() with the PCRE_CONFIG_JIT
51       option. The result is 1 when JIT is available, and  0  otherwise.  How‐
52       ever, a simple program does not need to check this in order to use JIT.
53       The normal API is implemented in a way that falls back to the interpre‐
54       tive code if JIT is not available. For programs that need the best pos‐
55       sible performance, there is also a "fast path"  API  that  is  JIT-spe‐
56       cific.
57
58       If  your program may sometimes be linked with versions of PCRE that are
59       older than 8.20, but you want to use JIT when it is available, you  can
60       test the values of PCRE_MAJOR and PCRE_MINOR, or the existence of a JIT
61       macro such as PCRE_CONFIG_JIT, for compile-time control of  your  code.
62       Also  beware that the pcre_jit_exec() function was not available at all
63       before 8.32, and may not be available at all  if  PCRE  isn't  compiled
64       with  --enable-jit.  See  the  "JIT  FAST  PATH  API" section below for
65       details.
66

SIMPLE USE OF JIT

68
69       You have to do two things to make use of the JIT support  in  the  sim‐
70       plest way:
71
72         (1) Call pcre_study() with the PCRE_STUDY_JIT_COMPILE option for
73             each compiled pattern, and pass the resulting pcre_extra block to
74             pcre_exec().
75
76         (2) Use pcre_free_study() to free the pcre_extra block when it is
77             no  longer  needed,  instead  of  just  freeing it yourself. This
78       ensures that
79             any JIT data is also freed.
80
81       For a program that may be linked with pre-8.20 versions  of  PCRE,  you
82       can insert
83
84         #ifndef PCRE_STUDY_JIT_COMPILE
85         #define PCRE_STUDY_JIT_COMPILE 0
86         #endif
87
88       so  that  no  option  is passed to pcre_study(), and then use something
89       like this to free the study data:
90
91         #ifdef PCRE_CONFIG_JIT
92             pcre_free_study(study_ptr);
93         #else
94             pcre_free(study_ptr);
95         #endif
96
97       PCRE_STUDY_JIT_COMPILE requests the JIT compiler to generate  code  for
98       complete  matches.  If  you  want  to  run  partial  matches  using the
99       PCRE_PARTIAL_HARD or  PCRE_PARTIAL_SOFT  options  of  pcre_exec(),  you
100       should  set  one  or  both  of the following options in addition to, or
101       instead of, PCRE_STUDY_JIT_COMPILE when you call pcre_study():
102
103         PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
104         PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
105
106       If using pcre_jit_exec() and supporting a pre-8.32 version of PCRE, you
107       can insert:
108
109          #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
110          pcre_jit_exec(...);
111          #else
112          pcre_exec(...)
113          #endif
114
115       but  as described in the "JIT FAST PATH API" section below this assumes
116       version 8.32 and later are compiled with --enable-jit, which may break.
117
118       The JIT compiler generates different optimized code  for  each  of  the
119       three  modes  (normal, soft partial, hard partial). When pcre_exec() is
120       called, the appropriate code is run if it is available. Otherwise,  the
121       pattern is matched using interpretive code.
122
123       In  some circumstances you may need to call additional functions. These
124       are described in the  section  entitled  "Controlling  the  JIT  stack"
125       below.
126
127       If  JIT  support  is  not  available,  PCRE_STUDY_JIT_COMPILE  etc. are
128       ignored, and no JIT data is created. Otherwise, the compiled pattern is
129       passed  to the JIT compiler, which turns it into machine code that exe‐
130       cutes much faster than the normal interpretive code.  When  pcre_exec()
131       is  passed  a  pcre_extra block containing a pointer to JIT code of the
132       appropriate mode (normal or hard/soft  partial),  it  obeys  that  code
133       instead  of  running  the interpreter. The result is identical, but the
134       compiled JIT code runs much faster.
135
136       There are some pcre_exec() options that are not supported for JIT  exe‐
137       cution.  There  are  also  some  pattern  items that JIT cannot handle.
138       Details are given below. In both cases, execution  automatically  falls
139       back  to  the  interpretive  code.  If you want to know whether JIT was
140       actually used for a particular match, you  should  arrange  for  a  JIT
141       callback  function  to  be  set up as described in the section entitled
142       "Controlling the JIT stack" below, even if you do not need to supply  a
143       non-default  JIT stack. Such a callback function is called whenever JIT
144       code is about to be obeyed. If the execution options are not right  for
145       JIT execution, the callback function is not obeyed.
146
147       If  the  JIT  compiler finds an unsupported item, no JIT data is gener‐
148       ated. You can find out if JIT execution is available after  studying  a
149       pattern  by  calling  pcre_fullinfo()  with the PCRE_INFO_JIT option. A
150       result of 1 means that JIT compilation was successful. A  result  of  0
151       means that JIT support is not available, or the pattern was not studied
152       with PCRE_STUDY_JIT_COMPILE etc., or the JIT compiler was not  able  to
153       handle the pattern.
154
155       Once a pattern has been studied, with or without JIT, it can be used as
156       many times as you like for matching different subject strings.
157

UNSUPPORTED OPTIONS AND PATTERN ITEMS

159
160       The only pcre_exec() options that are supported for JIT  execution  are
161       PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK, PCRE_NO_UTF32_CHECK, PCRE_NOT‐
162       BOL,  PCRE_NOTEOL,  PCRE_NOTEMPTY,   PCRE_NOTEMPTY_ATSTART,   PCRE_PAR‐
163       TIAL_HARD, and PCRE_PARTIAL_SOFT.
164
165       The  only  unsupported  pattern items are \C (match a single data unit)
166       when running in a UTF mode, and a callout immediately before an  asser‐
167       tion condition in a conditional group.
168

RETURN VALUES FROM JIT EXECUTION

170
171       When  a  pattern  is matched using JIT execution, the return values are
172       the same as those given by the interpretive pcre_exec() code, with  the
173       addition  of  one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means
174       that the memory used for the JIT stack was insufficient. See  "Control‐
175       ling the JIT stack" below for a discussion of JIT stack usage. For com‐
176       patibility with the interpretive pcre_exec() code, no  more  than  two-
177       thirds  of  the ovector argument is used for passing back captured sub‐
178       strings.
179
180       The error code PCRE_ERROR_MATCHLIMIT is returned by  the  JIT  code  if
181       searching  a  very large pattern tree goes on for too long, as it is in
182       the same circumstance when JIT is not used, but the details of  exactly
183       what  is  counted are not the same. The PCRE_ERROR_RECURSIONLIMIT error
184       code is never returned by JIT execution.
185

SAVING AND RESTORING COMPILED PATTERNS

187
188       The code that is generated by the  JIT  compiler  is  architecture-spe‐
189       cific,  and  is also position dependent. For those reasons it cannot be
190       saved (in a file or database) and restored later like the bytecode  and
191       other  data  of  a compiled pattern. Saving and restoring compiled pat‐
192       terns is not something many people do. More detail about this  facility
193       is  given in the pcreprecompile documentation. It should be possible to
194       run pcre_study() on a saved and restored pattern, and thereby  recreate
195       the  JIT  data, but because JIT compilation uses significant resources,
196       it is probably not worth doing this; you might as  well  recompile  the
197       original pattern.
198

CONTROLLING THE JIT STACK

200
201       When the compiled JIT code runs, it needs a block of memory to use as a
202       stack.  By default, it uses 32K on the  machine  stack.  However,  some
203       large   or   complicated  patterns  need  more  than  this.  The  error
204       PCRE_ERROR_JIT_STACKLIMIT is given when  there  is  not  enough  stack.
205       Three  functions  are provided for managing blocks of memory for use as
206       JIT stacks. There is further discussion about the use of JIT stacks  in
207       the section entitled "JIT stack FAQ" below.
208
209       The  pcre_jit_stack_alloc() function creates a JIT stack. Its arguments
210       are a starting size and a maximum size, and it returns a pointer to  an
211       opaque  structure of type pcre_jit_stack, or NULL if there is an error.
212       The pcre_jit_stack_free() function can be used to free a stack that  is
213       no  longer  needed.  (For  the technically minded: the address space is
214       allocated by mmap or VirtualAlloc.)
215
216       JIT uses far less memory for recursion than the interpretive code,  and
217       a  maximum  stack size of 512K to 1M should be more than enough for any
218       pattern.
219
220       The pcre_assign_jit_stack() function specifies  which  stack  JIT  code
221       should use. Its arguments are as follows:
222
223         pcre_extra         *extra
224         pcre_jit_callback  callback
225         void               *data
226
227       The  extra  argument  must  be  the  result  of studying a pattern with
228       PCRE_STUDY_JIT_COMPILE etc. There are three cases for the values of the
229       other two options:
230
231         (1) If callback is NULL and data is NULL, an internal 32K block
232             on the machine stack is used.
233
234         (2) If callback is NULL and data is not NULL, data must be
235             a valid JIT stack, the result of calling pcre_jit_stack_alloc().
236
237         (3) If callback is not NULL, it must point to a function that is
238             called with data as an argument at the start of matching, in
239             order to set up a JIT stack. If the return from the callback
240             function is NULL, the internal 32K stack is used; otherwise the
241             return value must be a valid JIT stack, the result of calling
242             pcre_jit_stack_alloc().
243
244       A  callback function is obeyed whenever JIT code is about to be run; it
245       is not obeyed when pcre_exec() is called with options that  are  incom‐
246       patible for JIT execution. A callback function can therefore be used to
247       determine whether a match operation was  executed  by  JIT  or  by  the
248       interpreter.
249
250       You may safely use the same JIT stack for more than one pattern (either
251       by assigning directly or by callback), as long as the patterns are  all
252       matched  sequentially in the same thread. In a multithread application,
253       if you do not specify a JIT stack, or if you assign or pass  back  NULL
254       from  a  callback, that is thread-safe, because each thread has its own
255       machine stack. However, if you assign  or  pass  back  a  non-NULL  JIT
256       stack,  this  must  be  a  different  stack for each thread so that the
257       application is thread-safe.
258
259       Strictly speaking, even more is allowed. You can assign the  same  non-
260       NULL  stack  to any number of patterns as long as they are not used for
261       matching by multiple threads at the same time.  For  example,  you  can
262       assign  the same stack to all compiled patterns, and use a global mutex
263       in the callback to wait until the stack is available for use.  However,
264       this is an inefficient solution, and not recommended.
265
266       This  is a suggestion for how a multithreaded program that needs to set
267       up non-default JIT stacks might operate:
268
269         During thread initalization
270           thread_local_var = pcre_jit_stack_alloc(...)
271
272         During thread exit
273           pcre_jit_stack_free(thread_local_var)
274
275         Use a one-line callback function
276           return thread_local_var
277
278       All the functions described in this section do nothing if  JIT  is  not
279       available,  and  pcre_assign_jit_stack()  does nothing unless the extra
280       argument is non-NULL and points to  a  pcre_extra  block  that  is  the
281       result of a successful study with PCRE_STUDY_JIT_COMPILE etc.
282

JIT STACK FAQ

284
285       (1) Why do we need JIT stacks?
286
287       PCRE  (and JIT) is a recursive, depth-first engine, so it needs a stack
288       where the local data of the current node is pushed before checking  its
289       child nodes.  Allocating real machine stack on some platforms is diffi‐
290       cult. For example, the stack chain needs to be updated every time if we
291       extend  the  stack  on  PowerPC.  Although it is possible, its updating
292       time overhead decreases performance. So we do the recursion in memory.
293
294       (2) Why don't we simply allocate blocks of memory with malloc()?
295
296       Modern operating systems have a  nice  feature:  they  can  reserve  an
297       address space instead of allocating memory. We can safely allocate mem‐
298       ory pages inside this address space, so the stack  could  grow  without
299       moving memory data (this is important because of pointers). Thus we can
300       allocate 1M address space, and use only a single memory  page  (usually
301       4K)  if  that is enough. However, we can still grow up to 1M anytime if
302       needed.
303
304       (3) Who "owns" a JIT stack?
305
306       The owner of the stack is the user program, not the JIT studied pattern
307       or  anything else. The user program must ensure that if a stack is used
308       by pcre_exec(), (that is, it is assigned to the pattern currently  run‐
309       ning), that stack must not be used by any other threads (to avoid over‐
310       writing the same memory area). The best practice for multithreaded pro‐
311       grams  is  to  allocate  a stack for each thread, and return this stack
312       through the JIT callback function.
313
314       (4) When should a JIT stack be freed?
315
316       You can free a JIT stack at any time, as long as it will not be used by
317       pcre_exec()  again.  When  you  assign  the  stack to a pattern, only a
318       pointer is set. There is no reference counting or any other magic.  You
319       can  free  the  patterns  and stacks in any order, anytime. Just do not
320       call pcre_exec() with a pattern pointing to an already freed stack,  as
321       that  will cause SEGFAULT. (Also, do not free a stack currently used by
322       pcre_exec() in another thread). You can also replace the  stack  for  a
323       pattern  at  any  time.  You  can  even  free the previous stack before
324       assigning a replacement.
325
326       (5) Should I allocate/free a  stack  every  time  before/after  calling
327       pcre_exec()?
328
329       No,  because  this  is  too  costly in terms of resources. However, you
330       could implement some clever idea which release the stack if it  is  not
331       used  in  let's  say  two minutes. The JIT callback can help to achieve
332       this without keeping a list of the currently JIT studied patterns.
333
334       (6) OK, the stack is for long term memory allocation. But what  happens
335       if  a pattern causes stack overflow with a stack of 1M? Is that 1M kept
336       until the stack is freed?
337
338       Especially on embedded sytems, it might be a good idea to release  mem‐
339       ory  sometimes  without  freeing the stack. There is no API for this at
340       the moment.  Probably a function call which returns with the  currently
341       allocated  memory for any stack and another which allows releasing mem‐
342       ory (shrinking the stack) would be a good idea if someone needs this.
343
344       (7) This is too much of a headache. Isn't there any better solution for
345       JIT stack handling?
346
347       No,  thanks to Windows. If POSIX threads were used everywhere, we could
348       throw out this complicated API.
349

EXAMPLE CODE

351
352       This is a single-threaded example that specifies a  JIT  stack  without
353       using a callback.
354
355         int rc;
356         int ovector[30];
357         pcre *re;
358         pcre_extra *extra;
359         pcre_jit_stack *jit_stack;
360
361         re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
362         /* Check for errors */
363         extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error);
364         jit_stack = pcre_jit_stack_alloc(32*1024, 512*1024);
365         /* Check for error (NULL) */
366         pcre_assign_jit_stack(extra, NULL, jit_stack);
367         rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, 30);
368         /* Check results */
369         pcre_free(re);
370         pcre_free_study(extra);
371         pcre_jit_stack_free(jit_stack);
372
373

JIT FAST PATH API

375
376       Because  the  API  described  above falls back to interpreted execution
377       when JIT is not available, it is convenient for programs that are writ‐
378       ten  for  general  use  in  many environments. However, calling JIT via
379       pcre_exec() does have a performance impact. Programs that  are  written
380       for  use  where  JIT  is known to be available, and which need the best
381       possible performance, can instead use a "fast path"  API  to  call  JIT
382       execution  directly  instead of calling pcre_exec() (obviously only for
383       patterns that have been successfully studied by JIT).
384
385       The fast path function is called pcre_jit_exec(), and it takes  exactly
386       the  same  arguments  as pcre_exec(), plus one additional argument that
387       must point to a JIT stack. The JIT stack arrangements  described  above
388       do not apply. The return values are the same as for pcre_exec().
389
390       When  you  call  pcre_exec(), as well as testing for invalid options, a
391       number of other sanity checks are performed on the arguments. For exam‐
392       ple,  if  the  subject  pointer  is NULL, or its length is negative, an
393       immediate error is given. Also, unless PCRE_NO_UTF[8|16|32] is  set,  a
394       UTF  subject  string is tested for validity. In the interests of speed,
395       these checks do not happen on the JIT fast path, and if invalid data is
396       passed, the result is undefined.
397
398       Bypassing  the  sanity  checks  and  the  pcre_exec() wrapping can give
399       speedups of more than 10%.
400
401       Note that the pcre_jit_exec() function is not available in versions  of
402       PCRE  before  8.32  (released in November 2012). If you need to support
403       versions that old you must either use the slower pcre_exec(), or switch
404       between  the  two  codepaths  by  checking the values of PCRE_MAJOR and
405       PCRE_MINOR.
406
407       Due to an unfortunate implementation oversight, even in  versions  8.32
408       and  later  there will be no pcre_jit_exec() stub function defined when
409       PCRE is compiled with --disable-jit, which is the default, and  there's
410       no  way  to  detect  whether  PCRE was compiled with --enable-jit via a
411       macro.
412
413       If you need to support versions older than 8.32, or versions  that  may
414       not   build   with   --enable-jit,  you  must  either  use  the  slower
415       pcre_exec(), or switch between the two codepaths by checking the values
416       of PCRE_MAJOR and PCRE_MINOR.
417
418       Switching  between the two by checking the version assumes that all the
419       versions being targeted are built with --enable-jit.  To  also  support
420       builds that may use --disable-jit either pcre_exec() must be used, or a
421       compile-time check for JIT via pcre_config() (which assumes the runtime
422       environment  will  be  the  same), or as the Git project decided to do,
423       simply assume that pcre_jit_exec() is present in 8.32 or later unless a
424       compile-time  flag  is  provided, see the "grep: un-break building with
425       PCRE >= 8.32 without --enable-jit" commit in git.git for an example  of
426       that.
427

SEE ALSO

429
430       pcreapi(3)
431

AUTHOR

433
434       Philip Hazel (FAQ by Zoltan Herczeg)
435       University Computing Service
436       Cambridge CB2 3QH, England.
437

REVISION

439
440       Last updated: 05 July 2017
441       Copyright (c) 1997-2017 University of Cambridge.
442
443
444
445PCRE 8.41                        05 July 2017                       PCREJIT(3)
Impressum