Path: blob/master/thirdparty/pcre2/src/pcre2_jit_match.c
9898 views
/*************************************************1* Perl-Compatible Regular Expressions *2*************************************************/34/* PCRE is a library of functions to support regular expressions whose syntax5and semantics are as close as possible to those of the Perl 5 language.67Written by Philip Hazel8Original API code Copyright (c) 1997-2012 University of Cambridge9New API code Copyright (c) 2016-2023 University of Cambridge1011-----------------------------------------------------------------------------12Redistribution and use in source and binary forms, with or without13modification, are permitted provided that the following conditions are met:1415* Redistributions of source code must retain the above copyright notice,16this list of conditions and the following disclaimer.1718* Redistributions in binary form must reproduce the above copyright19notice, this list of conditions and the following disclaimer in the20documentation and/or other materials provided with the distribution.2122* Neither the name of the University of Cambridge nor the names of its23contributors may be used to endorse or promote products derived from24this software without specific prior written permission.2526THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE36POSSIBILITY OF SUCH DAMAGE.37-----------------------------------------------------------------------------38*/3940#ifndef INCLUDED_FROM_PCRE2_JIT_COMPILE41#error This file must be included from pcre2_jit_compile.c.42#endif4344#if defined(__has_feature)45#if __has_feature(memory_sanitizer)46#include <sanitizer/msan_interface.h>47#endif /* __has_feature(memory_sanitizer) */48#endif /* defined(__has_feature) */4950#ifdef SUPPORT_JIT5152static SLJIT_NOINLINE int jit_machine_stack_exec(jit_arguments *arguments, jit_function executable_func)53{54sljit_u8 local_space[MACHINE_STACK_SIZE];55struct sljit_stack local_stack;5657local_stack.min_start = local_space;58local_stack.start = local_space;59local_stack.end = local_space + MACHINE_STACK_SIZE;60local_stack.top = local_space + MACHINE_STACK_SIZE;61arguments->stack = &local_stack;62return executable_func(arguments);63}6465#endif666768/*************************************************69* Do a JIT pattern match *70*************************************************/7172/* This function runs a JIT pattern match.7374Arguments:75code points to the compiled expression76subject points to the subject string77length length of subject string (may contain binary zeros)78start_offset where to start in the subject string79options option bits80match_data points to a match_data block81mcontext points to a match context8283Returns: > 0 => success; value is the number of ovector pairs filled84= 0 => success, but ovector is not big enough85-1 => failed to match (PCRE2_ERROR_NOMATCH)86< -1 => some kind of unexpected problem87*/8889PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION90pcre2_jit_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,91PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,92pcre2_match_context *mcontext)93{94#ifndef SUPPORT_JIT9596(void)code;97(void)subject;98(void)length;99(void)start_offset;100(void)options;101(void)match_data;102(void)mcontext;103return PCRE2_ERROR_JIT_BADOPTION;104105#else /* SUPPORT_JIT */106107pcre2_real_code *re = (pcre2_real_code *)code;108executable_functions *functions = (executable_functions *)re->executable_jit;109pcre2_jit_stack *jit_stack;110uint32_t oveccount = match_data->oveccount;111uint32_t max_oveccount;112union {113void *executable_func;114jit_function call_executable_func;115} convert_executable_func;116jit_arguments arguments;117int rc;118int index = 0;119120if ((options & PCRE2_PARTIAL_HARD) != 0)121index = 2;122else if ((options & PCRE2_PARTIAL_SOFT) != 0)123index = 1;124125if (functions == NULL || functions->executable_funcs[index] == NULL)126return PCRE2_ERROR_JIT_BADOPTION;127128/* Sanity checks should be handled by pcre2_match. */129arguments.str = subject + start_offset;130arguments.begin = subject;131arguments.end = subject + length;132arguments.match_data = match_data;133arguments.startchar_ptr = subject;134arguments.mark_ptr = NULL;135arguments.options = options;136137if (mcontext != NULL)138{139arguments.callout = mcontext->callout;140arguments.callout_data = mcontext->callout_data;141arguments.offset_limit = mcontext->offset_limit;142arguments.limit_match = (mcontext->match_limit < re->limit_match)?143mcontext->match_limit : re->limit_match;144if (mcontext->jit_callback != NULL)145jit_stack = mcontext->jit_callback(mcontext->jit_callback_data);146else147jit_stack = (pcre2_jit_stack *)mcontext->jit_callback_data;148}149else150{151arguments.callout = NULL;152arguments.callout_data = NULL;153arguments.offset_limit = PCRE2_UNSET;154arguments.limit_match = (MATCH_LIMIT < re->limit_match)?155MATCH_LIMIT : re->limit_match;156jit_stack = NULL;157}158159160max_oveccount = functions->top_bracket;161if (oveccount > max_oveccount)162oveccount = max_oveccount;163arguments.oveccount = oveccount << 1;164165166convert_executable_func.executable_func = functions->executable_funcs[index];167if (jit_stack != NULL)168{169arguments.stack = (struct sljit_stack *)(jit_stack->stack);170rc = convert_executable_func.call_executable_func(&arguments);171}172else173rc = jit_machine_stack_exec(&arguments, convert_executable_func.call_executable_func);174175if (rc > (int)oveccount)176rc = 0;177match_data->code = re;178match_data->subject = (rc >= 0 || rc == PCRE2_ERROR_PARTIAL)? subject : NULL;179match_data->subject_length = length;180match_data->rc = rc;181match_data->startchar = arguments.startchar_ptr - subject;182match_data->leftchar = 0;183match_data->rightchar = 0;184match_data->mark = arguments.mark_ptr;185match_data->matchedby = PCRE2_MATCHEDBY_JIT;186187#if defined(__has_feature)188#if __has_feature(memory_sanitizer)189if (rc > 0)190__msan_unpoison(match_data->ovector, 2 * rc * sizeof(match_data->ovector[0]));191#endif /* __has_feature(memory_sanitizer) */192#endif /* defined(__has_feature) */193194return match_data->rc;195196#endif /* SUPPORT_JIT */197}198199/* End of pcre2_jit_match.c */200201202