Skip to content

Commit

Permalink
Add error code for unsupported jit features (#441)
Browse files Browse the repository at this point in the history
Co-authored-by: Zoltan Herczeg <hzmester@freemail.hu>
  • Loading branch information
zherczeg and Zoltan Herczeg authored Aug 22, 2024
1 parent 325f991 commit 5faff98
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 16 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ The condition could erroneously be treated as true if a branch matched but
overran the current position. This bug was in the interpreter only; matching
with JIT was correct.

12. Add a new error code (PCRE2_ERROR_JIT_UNSUPPORTED) which is yielded
for unsupported jit features.


Version 10.44 07-June-2024
--------------------------
Expand Down
14 changes: 8 additions & 6 deletions doc/pcre2_jit_compile.3
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ details are given in the
documentation.
.P
The availability of JIT support can be tested by calling
\fBpcre2_compile_jit()\fP with a NULL first argument and the single option
PCRE2_JIT_TEST_ALLOC. Such a call returns zero if JIT is available and has a
working allocator. Otherwise it returns PCRE2_ERROR_NOMEMORY if JIT is
available but cannot allocate executable memory, or PCRE2_ERROR_NULL if JIT
support is not compiled.
\fBpcre2_compile_jit()\fP with a single option PCRE2_JIT_TEST_ALLOC (the
code argument is ignored, so a NULL value is accepted). Such a call
returns zero if JIT is available and has a working allocator. Otherwise
it returns PCRE2_ERROR_NOMEMORY if JIT is available but cannot allocate
executable memory, or PCRE2_ERROR_JIT_UNSUPPORTED if JIT support is not
compiled.
.P
Otherwise, the first argument must be a pointer that was returned by a
successful call to \fBpcre2_compile()\fP, and the second must contain one or
Expand All @@ -46,7 +47,8 @@ for success, or a negative error code otherwise. In particular,
PCRE2_ERROR_JIT_BADOPTION is returned if JIT is not supported or if an unknown
bit is set in \fIoptions\fP. The function can also return PCRE2_ERROR_NOMEMORY
if JIT is unable to allocate executable memory for the compiler, even if it was
because of a system security restriction.
because of a system security restriction. In a few cases, the function may
return with PCRE2_ERROR_JIT_UNSUPPORTED for unsupported features.
.P
There is a complete description of the PCRE2 native API in the
.\" HREF
Expand Down
10 changes: 5 additions & 5 deletions doc/pcre2jit.3
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ executable memory in which to build its compiled code. The only guarantee from
be used.
.P
As of release 10.45 there is a more informative way to test for JIT support. If
\fBpcre2_compile_jit()\fP is called with a NULL first argument and the single
option PCRE2_JIT_TEST_ALLOC, it returns zero if JIT is available and has a
working allocator. Otherwise it returns PCRE2_ERROR_NOMEMORY if JIT is
available but cannot allocate executable memory, or PCRE2_ERROR_NULL if JIT
support is not compiled.
\fBpcre2_compile_jit()\fP is called with the single option PCRE2_JIT_TEST_ALLOC
it returns zero if JIT is available and has a working allocator. Otherwise it
returns PCRE2_ERROR_NOMEMORY if JIT is available but cannot allocate executable
memory, or PCRE2_ERROR_JIT_UNSUPPORTED if JIT support is not compiled. The
code argument is ignored, so it can be a NULL value.
.P
A simple program does not need to check availability in order to use JIT when
possible. The API is implemented in a way that falls back to the interpretive
Expand Down
1 change: 1 addition & 0 deletions src/pcre2.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ released, the numbers must not be changed. */
#define PCRE2_ERROR_INTERNAL_DUPMATCH (-65)
#define PCRE2_ERROR_DFA_UINVALID_UTF (-66)
#define PCRE2_ERROR_INVALIDOFFSET (-67)
#define PCRE2_ERROR_JIT_UNSUPPORTED (-68)


/* Request types for pcre2_pattern_info() */
Expand Down
1 change: 1 addition & 0 deletions src/pcre2_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ static const unsigned char match_error_texts[] =
"internal error - duplicate substitution match\0"
"PCRE2_MATCH_INVALID_UTF is not supported for DFA matching\0"
"INTERNAL ERROR: invalid substring offset\0"
"feature is not supported by the JIT compiler\0"
;


Expand Down
8 changes: 6 additions & 2 deletions src/pcre2_jit_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -14280,7 +14280,7 @@ common->ovector_start += sizeof(sljit_sw);
if (!check_opcode_types(common, common->start, ccend))
{
SLJIT_FREE(common->optimized_cbracket, allocator_data);
return PCRE2_ERROR_NOMEMORY;
return PCRE2_ERROR_JIT_UNSUPPORTED;
}

/* Checking flags and updating ovector_start. */
Expand Down Expand Up @@ -14865,15 +14865,19 @@ if (executable_allocator_is_working == -1)
}
else executable_allocator_is_working = 0;
}
#endif

if (options & PCRE2_JIT_TEST_ALLOC)
{
if (options != PCRE2_JIT_TEST_ALLOC)
return PCRE2_ERROR_JIT_BADOPTION;

#ifdef SUPPORT_JIT
return executable_allocator_is_working ? 0 : PCRE2_ERROR_NOMEMORY;
}
#else
return PCRE2_ERROR_JIT_UNSUPPORTED;
#endif
}

if (code == NULL)
return PCRE2_ERROR_NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/pcre2test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8616,7 +8616,7 @@ if (arg != NULL && arg[0] != CHAR_MINUS)
{
case 0: break;
case PCRE2_ERROR_NOMEMORY: yield = 1; break;
case PCRE2_ERROR_NULL: yield = 2; break;
case PCRE2_ERROR_JIT_UNSUPPORTED: yield = 2; break;
default: yield = 3; break;
}
printf("%d\n", yield);
Expand Down
4 changes: 2 additions & 2 deletions testdata/testoutput17
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
# JIT does not support this pattern (callout at start of condition).

/(?(?C1)(?=a)a)/I
JIT compilation was not successful (no more memory)
JIT compilation was not successful (feature is not supported by the JIT compiler)
Capture group count = 0
May match empty string
Subject length lower bound = 0
JIT compilation was not successful (no more memory)
JIT compilation was not successful (feature is not supported by the JIT compiler)

# The following pattern cannot be compiled by JIT.

Expand Down

0 comments on commit 5faff98

Please sign in to comment.