github的一些开源项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

481 lines
21 KiB

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