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