2014-03-07 18:28:52 +01:00
|
|
|
/*************************************************
|
|
|
|
* Perl-Compatible Regular Expressions *
|
|
|
|
*************************************************/
|
|
|
|
|
|
|
|
/* PCRE is a library of functions to support regular expressions whose syntax
|
|
|
|
and semantics are as close as possible to those of the Perl 5 language.
|
|
|
|
|
|
|
|
Written by Philip Hazel
|
|
|
|
Original API code Copyright (c) 1997-2012 University of Cambridge
|
2016-01-12 15:44:34 +01:00
|
|
|
New API code Copyright (c) 2016 University of Cambridge
|
2014-03-07 18:28:52 +01:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the University of Cambridge nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-09-30 09:06:48 +02:00
|
|
|
#ifndef INCLUDED_FROM_PCRE2_JIT_COMPILE
|
|
|
|
#error This file must be included from pcre2_jit_compile.c.
|
2014-03-07 18:28:52 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-10-07 15:07:20 +02:00
|
|
|
|
2015-03-06 08:41:36 +01:00
|
|
|
/*************************************************
|
|
|
|
* Free JIT read-only data *
|
|
|
|
*************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
PRIV(jit_free_rodata)(void *current, void *allocator_data)
|
|
|
|
{
|
2015-03-06 13:49:18 +01:00
|
|
|
#ifndef SUPPORT_JIT
|
|
|
|
(void)current;
|
|
|
|
(void)allocator_data;
|
|
|
|
#else /* SUPPORT_JIT */
|
2015-03-06 08:41:36 +01:00
|
|
|
void *next;
|
|
|
|
|
|
|
|
SLJIT_UNUSED_ARG(allocator_data);
|
|
|
|
|
|
|
|
while (current != NULL)
|
|
|
|
{
|
|
|
|
next = *(void**)current;
|
|
|
|
SLJIT_FREE(current, allocator_data);
|
|
|
|
current = next;
|
|
|
|
}
|
2015-03-06 13:49:18 +01:00
|
|
|
|
|
|
|
#endif /* SUPPORT_JIT */
|
2015-03-06 08:41:36 +01:00
|
|
|
}
|
|
|
|
|
2014-10-07 15:07:20 +02:00
|
|
|
/*************************************************
|
|
|
|
* Free JIT compiled code *
|
|
|
|
*************************************************/
|
|
|
|
|
|
|
|
void
|
2014-10-09 12:06:19 +02:00
|
|
|
PRIV(jit_free)(void *executable_jit, pcre2_memctl *memctl)
|
2014-10-07 15:07:20 +02:00
|
|
|
{
|
|
|
|
#ifndef SUPPORT_JIT
|
|
|
|
(void)executable_jit;
|
2014-10-09 12:06:19 +02:00
|
|
|
(void)memctl;
|
2014-10-07 15:07:20 +02:00
|
|
|
#else /* SUPPORT_JIT */
|
|
|
|
|
2014-10-09 12:06:19 +02:00
|
|
|
executable_functions *functions = (executable_functions *)executable_jit;
|
|
|
|
void *allocator_data = memctl;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < JIT_NUMBER_OF_COMPILE_MODES; i++)
|
|
|
|
{
|
|
|
|
if (functions->executable_funcs[i] != NULL)
|
2020-08-27 08:19:17 +02:00
|
|
|
sljit_free_code(functions->executable_funcs[i], NULL);
|
2015-03-06 08:41:36 +01:00
|
|
|
PRIV(jit_free_rodata)(functions->read_only_data_heads[i], allocator_data);
|
2014-10-09 12:06:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SLJIT_FREE(functions, allocator_data);
|
2014-10-07 15:07:20 +02:00
|
|
|
|
|
|
|
#endif /* SUPPORT_JIT */
|
|
|
|
}
|
2014-03-07 18:28:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Free unused JIT memory *
|
|
|
|
*************************************************/
|
|
|
|
|
2014-08-16 11:46:58 +02:00
|
|
|
PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
|
2014-04-18 17:37:56 +02:00
|
|
|
pcre2_jit_free_unused_memory(pcre2_general_context *gcontext)
|
2014-03-07 18:28:52 +01:00
|
|
|
{
|
|
|
|
#ifndef SUPPORT_JIT
|
2014-04-18 17:37:56 +02:00
|
|
|
(void)gcontext; /* Suppress warning */
|
2014-03-07 18:28:52 +01:00
|
|
|
#else /* SUPPORT_JIT */
|
2014-10-04 19:05:21 +02:00
|
|
|
SLJIT_UNUSED_ARG(gcontext);
|
2014-09-30 09:06:48 +02:00
|
|
|
sljit_free_unused_memory_exec();
|
2014-03-07 18:28:52 +01:00
|
|
|
#endif /* SUPPORT_JIT */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Allocate a JIT stack *
|
|
|
|
*************************************************/
|
|
|
|
|
2014-08-16 11:46:58 +02:00
|
|
|
PCRE2_EXP_DEFN pcre2_jit_stack * PCRE2_CALL_CONVENTION
|
2014-11-28 13:46:16 +01:00
|
|
|
pcre2_jit_stack_create(size_t startsize, size_t maxsize,
|
|
|
|
pcre2_general_context *gcontext)
|
2014-03-07 18:28:52 +01:00
|
|
|
{
|
|
|
|
#ifndef SUPPORT_JIT
|
2014-08-16 11:46:58 +02:00
|
|
|
|
2014-04-18 17:37:56 +02:00
|
|
|
(void)gcontext;
|
2014-03-07 18:28:52 +01:00
|
|
|
(void)startsize;
|
|
|
|
(void)maxsize;
|
|
|
|
return NULL;
|
2014-08-16 11:46:58 +02:00
|
|
|
|
2014-03-07 18:28:52 +01:00
|
|
|
#else /* SUPPORT_JIT */
|
|
|
|
|
2014-09-30 09:06:48 +02:00
|
|
|
pcre2_jit_stack *jit_stack;
|
|
|
|
|
2022-01-06 14:46:43 +01:00
|
|
|
if (startsize == 0 || maxsize == 0 || maxsize > SIZE_MAX - STACK_GROWTH_RATE)
|
2014-09-30 09:06:48 +02:00
|
|
|
return NULL;
|
|
|
|
if (startsize > maxsize)
|
|
|
|
startsize = maxsize;
|
|
|
|
startsize = (startsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1);
|
|
|
|
maxsize = (maxsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1);
|
|
|
|
|
|
|
|
jit_stack = PRIV(memctl_malloc)(sizeof(pcre2_real_jit_stack), (pcre2_memctl *)gcontext);
|
2014-10-25 16:16:46 +02:00
|
|
|
if (jit_stack == NULL) return NULL;
|
2014-09-30 09:06:48 +02:00
|
|
|
jit_stack->stack = sljit_allocate_stack(startsize, maxsize, &jit_stack->memctl);
|
2020-01-24 09:28:23 +01:00
|
|
|
if (jit_stack->stack == NULL)
|
|
|
|
{
|
|
|
|
jit_stack->memctl.free(jit_stack, jit_stack->memctl.memory_data);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-09-30 09:06:48 +02:00
|
|
|
return jit_stack;
|
2014-08-16 11:46:58 +02:00
|
|
|
|
2014-03-07 18:28:52 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Assign a JIT stack to a pattern *
|
|
|
|
*************************************************/
|
|
|
|
|
2014-10-05 08:20:41 +02:00
|
|
|
PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
|
2014-11-06 15:37:09 +01:00
|
|
|
pcre2_jit_stack_assign(pcre2_match_context *mcontext, pcre2_jit_callback callback,
|
2014-04-18 17:37:56 +02:00
|
|
|
void *callback_data)
|
2014-03-07 18:28:52 +01:00
|
|
|
{
|
|
|
|
#ifndef SUPPORT_JIT
|
2014-11-11 11:21:23 +01:00
|
|
|
(void)mcontext;
|
2014-03-07 18:28:52 +01:00
|
|
|
(void)callback;
|
|
|
|
(void)callback_data;
|
|
|
|
#else /* SUPPORT_JIT */
|
|
|
|
|
2014-11-06 15:37:09 +01:00
|
|
|
if (mcontext == NULL) return;
|
|
|
|
mcontext->jit_callback = callback;
|
|
|
|
mcontext->jit_callback_data = callback_data;
|
2014-08-16 11:46:58 +02:00
|
|
|
|
2014-03-07 18:28:52 +01:00
|
|
|
#endif /* SUPPORT_JIT */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Free a JIT stack *
|
|
|
|
*************************************************/
|
|
|
|
|
2014-10-05 08:20:41 +02:00
|
|
|
PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
|
2014-04-18 17:37:56 +02:00
|
|
|
pcre2_jit_stack_free(pcre2_jit_stack *jit_stack)
|
2014-03-07 18:28:52 +01:00
|
|
|
{
|
|
|
|
#ifndef SUPPORT_JIT
|
|
|
|
(void)jit_stack;
|
|
|
|
#else /* SUPPORT_JIT */
|
2014-09-30 09:06:48 +02:00
|
|
|
if (jit_stack != NULL)
|
|
|
|
{
|
|
|
|
sljit_free_stack((struct sljit_stack *)(jit_stack->stack), &jit_stack->memctl);
|
|
|
|
jit_stack->memctl.free(jit_stack, jit_stack->memctl.memory_data);
|
|
|
|
}
|
2014-03-07 18:28:52 +01:00
|
|
|
#endif /* SUPPORT_JIT */
|
|
|
|
}
|
|
|
|
|
2014-08-08 20:18:18 +02:00
|
|
|
|
2014-10-13 08:43:00 +02:00
|
|
|
/*************************************************
|
|
|
|
* Get target CPU type *
|
|
|
|
*************************************************/
|
|
|
|
|
|
|
|
const char*
|
|
|
|
PRIV(jit_get_target)(void)
|
|
|
|
{
|
2014-10-15 17:57:49 +02:00
|
|
|
#ifndef SUPPORT_JIT
|
|
|
|
return "JIT is not supported";
|
|
|
|
#else /* SUPPORT_JIT */
|
2014-10-13 08:43:00 +02:00
|
|
|
return sljit_get_platform_name();
|
2014-10-15 17:57:49 +02:00
|
|
|
#endif /* SUPPORT_JIT */
|
2014-10-13 08:43:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-08 20:18:18 +02:00
|
|
|
/*************************************************
|
|
|
|
* Get size of JIT code *
|
|
|
|
*************************************************/
|
|
|
|
|
2014-08-16 11:46:58 +02:00
|
|
|
size_t
|
2014-08-08 20:18:18 +02:00
|
|
|
PRIV(jit_get_size)(void *executable_jit)
|
|
|
|
{
|
|
|
|
#ifndef SUPPORT_JIT
|
|
|
|
(void)executable_jit;
|
|
|
|
return 0;
|
|
|
|
#else /* SUPPORT_JIT */
|
2014-10-08 10:57:26 +02:00
|
|
|
sljit_uw *executable_sizes = ((executable_functions *)executable_jit)->executable_sizes;
|
2014-10-09 12:06:19 +02:00
|
|
|
SLJIT_COMPILE_ASSERT(JIT_NUMBER_OF_COMPILE_MODES == 3, number_of_compile_modes_changed);
|
2014-10-08 10:57:26 +02:00
|
|
|
return executable_sizes[0] + executable_sizes[1] + executable_sizes[2];
|
2014-08-08 20:18:18 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-03-07 18:28:52 +01:00
|
|
|
/* End of pcre2_jit_misc.c */
|