Skip to content

Commit

Permalink
Probe JIT availability at runtime
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
minipli-oss committed Nov 9, 2022
1 parent c833353 commit 9dee7e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/pcre2_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ switch (what)

case PCRE2_CONFIG_JIT:
#ifdef SUPPORT_JIT
*((uint32_t *)where) = 1;
*((uint32_t *)where) = PRIV(jit_supported)();
#else
*((uint32_t *)where) = 0;
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/pcre2_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,7 @@ is available. */
#define _pcre2_jit_free PCRE2_SUFFIX(_pcre2_jit_free_)
#define _pcre2_jit_get_size PCRE2_SUFFIX(_pcre2_jit_get_size_)
#define _pcre2_jit_get_target PCRE2_SUFFIX(_pcre2_jit_get_target_)
#define _pcre2_jit_supported PCRE2_SUFFIX(_pcre2_jit_supported_)
#define _pcre2_memctl_malloc PCRE2_SUFFIX(_pcre2_memctl_malloc_)
#define _pcre2_ord2utf PCRE2_SUFFIX(_pcre2_ord2utf_)
#define _pcre2_script_run PCRE2_SUFFIX(_pcre2_script_run_)
Expand Down Expand Up @@ -2019,6 +2020,7 @@ extern void _pcre2_jit_free_rodata(void *, void *);
extern void _pcre2_jit_free(void *, pcre2_memctl *);
extern size_t _pcre2_jit_get_size(void *);
const char * _pcre2_jit_get_target(void);
extern int _pcre2_jit_supported(void);
extern void * _pcre2_memctl_malloc(size_t, pcre2_memctl *);
extern unsigned int _pcre2_ord2utf(uint32_t, PCRE2_UCHAR *);
extern BOOL _pcre2_script_run(PCRE2_SPTR, PCRE2_SPTR, BOOL);
Expand Down
15 changes: 15 additions & 0 deletions src/pcre2_jit_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ if (jit_stack != NULL)
}


/*************************************************
* Test runtime JIT support *
*************************************************/

int
PRIV(jit_supported)(void)
{
#ifndef SUPPORT_JIT
return 0;
#else /* SUPPORT_JIT */
return sljit_get_runtime_support();
#endif /* SUPPORT_JIT */
}


/*************************************************
* Get target CPU type *
*************************************************/
Expand Down

0 comments on commit 9dee7e8

Please sign in to comment.