blob: e57afad065ba261c56c96d6858f2c0cba95315e4 [file] [log] [blame]
Janis Danisevskis112c9cc2016-03-31 13:35:25 +01001/*************************************************
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 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
42#ifndef INCLUDED_FROM_PCRE2_JIT_COMPILE
43#error This file must be included from pcre2_jit_compile.c.
44#endif
45
46
47
48/*************************************************
49* Free JIT read-only data *
50*************************************************/
51
52void
53PRIV(jit_free_rodata)(void *current, void *allocator_data)
54{
55#ifndef SUPPORT_JIT
56(void)current;
57(void)allocator_data;
58#else /* SUPPORT_JIT */
59void *next;
60
61SLJIT_UNUSED_ARG(allocator_data);
62
63while (current != NULL)
64 {
65 next = *(void**)current;
66 SLJIT_FREE(current, allocator_data);
67 current = next;
68 }
69
70#endif /* SUPPORT_JIT */
71}
72
73/*************************************************
74* Free JIT compiled code *
75*************************************************/
76
77void
78PRIV(jit_free)(void *executable_jit, pcre2_memctl *memctl)
79{
80#ifndef SUPPORT_JIT
81(void)executable_jit;
82(void)memctl;
83#else /* SUPPORT_JIT */
84
85executable_functions *functions = (executable_functions *)executable_jit;
86void *allocator_data = memctl;
87int i;
88
89for (i = 0; i < JIT_NUMBER_OF_COMPILE_MODES; i++)
90 {
91 if (functions->executable_funcs[i] != NULL)
Elliott Hughes3435c422020-12-04 13:18:28 -080092 sljit_free_code(functions->executable_funcs[i], NULL);
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010093 PRIV(jit_free_rodata)(functions->read_only_data_heads[i], allocator_data);
94 }
95
96SLJIT_FREE(functions, allocator_data);
97
98#endif /* SUPPORT_JIT */
99}
100
101
102/*************************************************
103* Free unused JIT memory *
104*************************************************/
105
106PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
107pcre2_jit_free_unused_memory(pcre2_general_context *gcontext)
108{
109#ifndef SUPPORT_JIT
110(void)gcontext; /* Suppress warning */
111#else /* SUPPORT_JIT */
112SLJIT_UNUSED_ARG(gcontext);
113sljit_free_unused_memory_exec();
114#endif /* SUPPORT_JIT */
115}
116
117
118
119/*************************************************
120* Allocate a JIT stack *
121*************************************************/
122
123PCRE2_EXP_DEFN pcre2_jit_stack * PCRE2_CALL_CONVENTION
124pcre2_jit_stack_create(size_t startsize, size_t maxsize,
125 pcre2_general_context *gcontext)
126{
127#ifndef SUPPORT_JIT
128
129(void)gcontext;
130(void)startsize;
131(void)maxsize;
132return NULL;
133
134#else /* SUPPORT_JIT */
135
136pcre2_jit_stack *jit_stack;
137
Elliott Hughes4e19c8e2022-04-15 15:11:02 -0700138if (startsize == 0 || maxsize == 0 || maxsize > SIZE_MAX - STACK_GROWTH_RATE)
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100139 return NULL;
140if (startsize > maxsize)
141 startsize = maxsize;
142startsize = (startsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1);
143maxsize = (maxsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1);
144
145jit_stack = PRIV(memctl_malloc)(sizeof(pcre2_real_jit_stack), (pcre2_memctl *)gcontext);
146if (jit_stack == NULL) return NULL;
147jit_stack->stack = sljit_allocate_stack(startsize, maxsize, &jit_stack->memctl);
Elliott Hughes2dbd7d22020-06-03 14:32:37 -0700148if (jit_stack->stack == NULL)
149 {
150 jit_stack->memctl.free(jit_stack, jit_stack->memctl.memory_data);
151 return NULL;
152 }
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100153return jit_stack;
154
155#endif
156}
157
158
159/*************************************************
160* Assign a JIT stack to a pattern *
161*************************************************/
162
163PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
164pcre2_jit_stack_assign(pcre2_match_context *mcontext, pcre2_jit_callback callback,
165 void *callback_data)
166{
167#ifndef SUPPORT_JIT
168(void)mcontext;
169(void)callback;
170(void)callback_data;
171#else /* SUPPORT_JIT */
172
173if (mcontext == NULL) return;
174mcontext->jit_callback = callback;
175mcontext->jit_callback_data = callback_data;
176
177#endif /* SUPPORT_JIT */
178}
179
180
181/*************************************************
182* Free a JIT stack *
183*************************************************/
184
185PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
186pcre2_jit_stack_free(pcre2_jit_stack *jit_stack)
187{
188#ifndef SUPPORT_JIT
189(void)jit_stack;
190#else /* SUPPORT_JIT */
191if (jit_stack != NULL)
192 {
193 sljit_free_stack((struct sljit_stack *)(jit_stack->stack), &jit_stack->memctl);
194 jit_stack->memctl.free(jit_stack, jit_stack->memctl.memory_data);
195 }
196#endif /* SUPPORT_JIT */
197}
198
199
200/*************************************************
201* Get target CPU type *
202*************************************************/
203
204const char*
205PRIV(jit_get_target)(void)
206{
207#ifndef SUPPORT_JIT
208return "JIT is not supported";
209#else /* SUPPORT_JIT */
210return sljit_get_platform_name();
211#endif /* SUPPORT_JIT */
212}
213
214
215/*************************************************
216* Get size of JIT code *
217*************************************************/
218
219size_t
220PRIV(jit_get_size)(void *executable_jit)
221{
222#ifndef SUPPORT_JIT
223(void)executable_jit;
224return 0;
225#else /* SUPPORT_JIT */
226sljit_uw *executable_sizes = ((executable_functions *)executable_jit)->executable_sizes;
227SLJIT_COMPILE_ASSERT(JIT_NUMBER_OF_COMPILE_MODES == 3, number_of_compile_modes_changed);
228return executable_sizes[0] + executable_sizes[1] + executable_sizes[2];
229#endif
230}
231
232/* End of pcre2_jit_misc.c */