Skip to content

Commit c2dfa1e

Browse files
committed
Probe JIT availability at runtime
SELinux or PaX/grsecurity based kernels may deny creating writable and executable mappings, leading to errors when trying to allocate JIT memory, even though JIT support is generally available. Instead of failing hard in this case, allow to use the interpreter fallback by probing and announcing the availability of JIT mode at runtime through the PCRE2_CONFIG_JIT hook. This only happens for configurations using only the default JIT memory allocator, i.e. not the SELinux aware one. However, we still mark the latter as experimental and distributions like Debian don't enable it. The current behaviour leads to nasty user visible errors on such systems, e.g. when running 'git grep': $ git grep peach fatal: Couldn't JIT the PCRE2 pattern 'peach', got '-48' With this change in place, it'll fall back to the interpreter and "just work", providing a much more pleasant user experience. Signed-off-by: Mathias Krause <[email protected]>
1 parent 5e93d28 commit c2dfa1e

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/pcre2_config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ switch (what)
145145

146146
case PCRE2_CONFIG_JIT:
147147
#ifdef SUPPORT_JIT
148-
*((uint32_t *)where) = 1;
148+
*((uint32_t *)where) = !!PRIV(jit_supported)();
149149
#else
150150
*((uint32_t *)where) = 0;
151151
#endif

src/pcre2_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,7 @@ is available. */
19921992
#define _pcre2_jit_free PCRE2_SUFFIX(_pcre2_jit_free_)
19931993
#define _pcre2_jit_get_size PCRE2_SUFFIX(_pcre2_jit_get_size_)
19941994
#define _pcre2_jit_get_target PCRE2_SUFFIX(_pcre2_jit_get_target_)
1995+
#define _pcre2_jit_supported PCRE2_SUFFIX(_pcre2_jit_supported_)
19951996
#define _pcre2_memctl_malloc PCRE2_SUFFIX(_pcre2_memctl_malloc_)
19961997
#define _pcre2_ord2utf PCRE2_SUFFIX(_pcre2_ord2utf_)
19971998
#define _pcre2_script_run PCRE2_SUFFIX(_pcre2_script_run_)
@@ -2019,6 +2020,7 @@ extern void _pcre2_jit_free_rodata(void *, void *);
20192020
extern void _pcre2_jit_free(void *, pcre2_memctl *);
20202021
extern size_t _pcre2_jit_get_size(void *);
20212022
const char * _pcre2_jit_get_target(void);
2023+
extern int _pcre2_jit_supported(void);
20222024
extern void * _pcre2_memctl_malloc(size_t, pcre2_memctl *);
20232025
extern unsigned int _pcre2_ord2utf(uint32_t, PCRE2_UCHAR *);
20242026
extern BOOL _pcre2_script_run(PCRE2_SPTR, PCRE2_SPTR, BOOL);

src/pcre2_jit_misc.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ if (jit_stack != NULL)
199199
}
200200

201201

202+
/*************************************************
203+
* Test runtime JIT support *
204+
*************************************************/
205+
206+
int
207+
PRIV(jit_supported)(void)
208+
{
209+
#ifndef SUPPORT_JIT
210+
return 0;
211+
#else /* SUPPORT_JIT */
212+
return sljit_get_runtime_support();
213+
#endif /* SUPPORT_JIT */
214+
}
215+
216+
202217
/*************************************************
203218
* Get target CPU type *
204219
*************************************************/

0 commit comments

Comments
 (0)