pcre2_jit_match.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Original API code Copyright (c) 1997-2012 University of Cambridge
  8. New API code Copyright (c) 2016-2018 University of Cambridge
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. #ifndef INCLUDED_FROM_PCRE2_JIT_COMPILE
  34. #error This file must be included from pcre2_jit_compile.c.
  35. #endif
  36. #ifdef SUPPORT_JIT
  37. static SLJIT_NOINLINE int jit_machine_stack_exec(jit_arguments *arguments, jit_function executable_func)
  38. {
  39. sljit_u8 local_space[MACHINE_STACK_SIZE];
  40. struct sljit_stack local_stack;
  41. local_stack.min_start = local_space;
  42. local_stack.start = local_space;
  43. local_stack.end = local_space + MACHINE_STACK_SIZE;
  44. local_stack.top = local_space + MACHINE_STACK_SIZE;
  45. arguments->stack = &local_stack;
  46. return executable_func(arguments);
  47. }
  48. #endif
  49. /*************************************************
  50. * Do a JIT pattern match *
  51. *************************************************/
  52. /* This function runs a JIT pattern match.
  53. Arguments:
  54. code points to the compiled expression
  55. subject points to the subject string
  56. length length of subject string (may contain binary zeros)
  57. start_offset where to start in the subject string
  58. options option bits
  59. match_data points to a match_data block
  60. mcontext points to a match context
  61. Returns: > 0 => success; value is the number of ovector pairs filled
  62. = 0 => success, but ovector is not big enough
  63. -1 => failed to match (PCRE_ERROR_NOMATCH)
  64. < -1 => some kind of unexpected problem
  65. */
  66. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  67. pcre2_jit_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
  68. PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,
  69. pcre2_match_context *mcontext)
  70. {
  71. #ifndef SUPPORT_JIT
  72. (void)code;
  73. (void)subject;
  74. (void)length;
  75. (void)start_offset;
  76. (void)options;
  77. (void)match_data;
  78. (void)mcontext;
  79. return PCRE2_ERROR_JIT_BADOPTION;
  80. #else /* SUPPORT_JIT */
  81. pcre2_real_code *re = (pcre2_real_code *)code;
  82. executable_functions *functions = (executable_functions *)re->executable_jit;
  83. pcre2_jit_stack *jit_stack;
  84. uint32_t oveccount = match_data->oveccount;
  85. uint32_t max_oveccount;
  86. union {
  87. void *executable_func;
  88. jit_function call_executable_func;
  89. } convert_executable_func;
  90. jit_arguments arguments;
  91. int rc;
  92. int index = 0;
  93. if ((options & PCRE2_PARTIAL_HARD) != 0)
  94. index = 2;
  95. else if ((options & PCRE2_PARTIAL_SOFT) != 0)
  96. index = 1;
  97. if (functions == NULL || functions->executable_funcs[index] == NULL)
  98. return PCRE2_ERROR_JIT_BADOPTION;
  99. /* Sanity checks should be handled by pcre_exec. */
  100. arguments.str = subject + start_offset;
  101. arguments.begin = subject;
  102. arguments.end = subject + length;
  103. arguments.match_data = match_data;
  104. arguments.startchar_ptr = subject;
  105. arguments.mark_ptr = NULL;
  106. arguments.options = options;
  107. if (mcontext != NULL)
  108. {
  109. arguments.callout = mcontext->callout;
  110. arguments.callout_data = mcontext->callout_data;
  111. arguments.offset_limit = mcontext->offset_limit;
  112. arguments.limit_match = (mcontext->match_limit < re->limit_match)?
  113. mcontext->match_limit : re->limit_match;
  114. if (mcontext->jit_callback != NULL)
  115. jit_stack = mcontext->jit_callback(mcontext->jit_callback_data);
  116. else
  117. jit_stack = (pcre2_jit_stack *)mcontext->jit_callback_data;
  118. }
  119. else
  120. {
  121. arguments.callout = NULL;
  122. arguments.callout_data = NULL;
  123. arguments.offset_limit = PCRE2_UNSET;
  124. arguments.limit_match = (MATCH_LIMIT < re->limit_match)?
  125. MATCH_LIMIT : re->limit_match;
  126. jit_stack = NULL;
  127. }
  128. max_oveccount = functions->top_bracket;
  129. if (oveccount > max_oveccount)
  130. oveccount = max_oveccount;
  131. arguments.oveccount = oveccount << 1;
  132. convert_executable_func.executable_func = functions->executable_funcs[index];
  133. if (jit_stack != NULL)
  134. {
  135. arguments.stack = (struct sljit_stack *)(jit_stack->stack);
  136. rc = convert_executable_func.call_executable_func(&arguments);
  137. }
  138. else
  139. rc = jit_machine_stack_exec(&arguments, convert_executable_func.call_executable_func);
  140. if (rc > (int)oveccount)
  141. rc = 0;
  142. match_data->code = re;
  143. match_data->subject = (rc >= 0 || rc == PCRE2_ERROR_PARTIAL)? subject : NULL;
  144. match_data->rc = rc;
  145. match_data->startchar = arguments.startchar_ptr - subject;
  146. match_data->leftchar = 0;
  147. match_data->rightchar = 0;
  148. match_data->mark = arguments.mark_ptr;
  149. match_data->matchedby = PCRE2_MATCHEDBY_JIT;
  150. return match_data->rc;
  151. #endif /* SUPPORT_JIT */
  152. }
  153. /* End of pcre2_jit_match.c */