1/*************************************************
2* Perl-Compatible Regular Expressions *
3*************************************************/
4
5/* PCRE is a library of functions to support regular expressions whose syntax
6and 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-2018 University of Cambridge
11
12-----------------------------------------------------------------------------
13Redistribution and use in source and binary forms, with or without
14modification, 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
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37POSSIBILITY 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
47static SLJIT_NOINLINE int jit_machine_stack_exec(jit_arguments *arguments, jit_function executable_func)
48{
49sljit_u8 local_space[MACHINE_STACK_SIZE];
50struct sljit_stack local_stack;
51
52local_stack.min_start = local_space;
53local_stack.start = local_space;
54local_stack.end = local_space + MACHINE_STACK_SIZE;
55local_stack.top = local_space + MACHINE_STACK_SIZE;
56arguments->stack = &local_stack;
57return 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
69Arguments:
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
78Returns: > 0 => success; value is the number of ovector pairs filled
79 = 0 => success, but ovector is not big enough
80 -1 => failed to match (PCRE_ERROR_NOMATCH)
81 < -1 => some kind of unexpected problem
82*/
83
84PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
85pcre2_jit_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
86 PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,
87 pcre2_match_context *mcontext)
88{
89#ifndef SUPPORT_JIT
90
91(void)code;
92(void)subject;
93(void)length;
94(void)start_offset;
95(void)options;
96(void)match_data;
97(void)mcontext;
98return PCRE2_ERROR_JIT_BADOPTION;
99
100#else /* SUPPORT_JIT */
101
102pcre2_real_code *re = (pcre2_real_code *)code;
103executable_functions *functions = (executable_functions *)re->executable_jit;
104pcre2_jit_stack *jit_stack;
105uint32_t oveccount = match_data->oveccount;
106uint32_t max_oveccount;
107union {
108 void *executable_func;
109 jit_function call_executable_func;
110} convert_executable_func;
111jit_arguments arguments;
112int rc;
113int index = 0;
114
115if ((options & PCRE2_PARTIAL_HARD) != 0)
116 index = 2;
117else if ((options & PCRE2_PARTIAL_SOFT) != 0)
118 index = 1;
119
120if (functions == NULL || functions->executable_funcs[index] == NULL)
121 return PCRE2_ERROR_JIT_BADOPTION;
122
123/* Sanity checks should be handled by pcre_exec. */
124arguments.str = subject + start_offset;
125arguments.begin = subject;
126arguments.end = subject + length;
127arguments.match_data = match_data;
128arguments.startchar_ptr = subject;
129arguments.mark_ptr = NULL;
130arguments.options = options;
131
132if (mcontext != NULL)
133 {
134 arguments.callout = mcontext->callout;
135 arguments.callout_data = mcontext->callout_data;
136 arguments.offset_limit = mcontext->offset_limit;
137 arguments.limit_match = (mcontext->match_limit < re->limit_match)?
138 mcontext->match_limit : re->limit_match;
139 if (mcontext->jit_callback != NULL)
140 jit_stack = mcontext->jit_callback(mcontext->jit_callback_data);
141 else
142 jit_stack = (pcre2_jit_stack *)mcontext->jit_callback_data;
143 }
144else
145 {
146 arguments.callout = NULL;
147 arguments.callout_data = NULL;
148 arguments.offset_limit = PCRE2_UNSET;
149 arguments.limit_match = (MATCH_LIMIT < re->limit_match)?
150 MATCH_LIMIT : re->limit_match;
151 jit_stack = NULL;
152 }
153
154
155max_oveccount = functions->top_bracket;
156if (oveccount > max_oveccount)
157 oveccount = max_oveccount;
158arguments.oveccount = oveccount << 1;
159
160
161convert_executable_func.executable_func = functions->executable_funcs[index];
162if (jit_stack != NULL)
163 {
164 arguments.stack = (struct sljit_stack *)(jit_stack->stack);
165 rc = convert_executable_func.call_executable_func(&arguments);
166 }
167else
168 rc = jit_machine_stack_exec(&arguments, convert_executable_func.call_executable_func);
169
170if (rc > (int)oveccount)
171 rc = 0;
172match_data->code = re;
173match_data->subject = (rc >= 0 || rc == PCRE2_ERROR_PARTIAL)? subject : NULL;
174match_data->rc = rc;
175match_data->startchar = arguments.startchar_ptr - subject;
176match_data->leftchar = 0;
177match_data->rightchar = 0;
178match_data->mark = arguments.mark_ptr;
179match_data->matchedby = PCRE2_MATCHEDBY_JIT;
180
181return match_data->rc;
182
183#endif /* SUPPORT_JIT */
184}
185
186/* End of pcre2_jit_match.c */
187